1 /*
2  * Copyright (C) 2015 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.car;
18 
19 import static android.car.CarLibLog.TAG_CAR;
20 
21 import android.content.Context;
22 import android.os.Handler;
23 import android.os.RemoteException;
24 import android.util.Log;
25 
26 /**
27  * Common base class for Car*Manager
28  * @hide
29  */
30 public abstract class CarManagerBase {
31 
32     protected final Car mCar;
33 
CarManagerBase(Car car)34     public CarManagerBase(Car car) {
35         mCar = car;
36     }
37 
getContext()38     protected Context getContext() {
39         return mCar.getContext();
40     }
41 
getEventHandler()42     protected Handler getEventHandler() {
43         return mCar.getEventHandler();
44     }
45 
handleRemoteExceptionFromCarService(RemoteException e, T returnValue)46     protected <T> T handleRemoteExceptionFromCarService(RemoteException e, T returnValue) {
47         return mCar.handleRemoteExceptionFromCarService(e, returnValue);
48     }
49 
handleRemoteExceptionFromCarService(RemoteException e)50     protected void handleRemoteExceptionFromCarService(RemoteException e) {
51         mCar.handleRemoteExceptionFromCarService(e);
52     }
53 
54     /**
55      * Handles runtime and remote exception from CarService.
56      */
handleExceptionFromCarService(Exception e, T returnValue)57     protected <T> T handleExceptionFromCarService(Exception e, T returnValue) {
58         if (e instanceof RemoteException) {
59             return handleRemoteExceptionFromCarService((RemoteException) e, returnValue);
60         }
61 
62         if (e instanceof RuntimeException) {
63             Log.w(TAG_CAR, "Car service threw Runtime Exception.", e);
64             return returnValue;
65         }
66 
67         // exception should be either runtime or remote exception
68         Log.wtf(TAG_CAR, "Car service threw Exception.", e);
69 
70         return returnValue;
71     }
72 
73     /**
74      * Handle disconnection of car service (=crash). As car service has crashed already, this call
75      * should only clean up any listeners / callbacks passed from client. Clearing object passed
76      * to car service is not necessary as car service has crashed. Note that Car*Manager will not
77      * work any more as all binders are invalid. Client should re-create all Car*Managers when
78      * car service is restarted.
79      */
onCarDisconnected()80     protected abstract void onCarDisconnected();
81 }
82