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.annotation.Nullable;
22 import android.app.Activity;
23 import android.content.Context;
24 import android.os.Handler;
25 import android.os.RemoteException;
26 import android.util.Dumpable;
27 import android.util.DumpableContainer;
28 import android.util.Log;
29 import android.util.Slog;
30 
31 import com.android.car.internal.ICarBase;
32 
33 import java.util.function.Supplier;
34 
35 /**
36  * Common base class for Car*Manager
37  * @hide
38  */
39 public abstract class CarManagerBase {
40 
41     private static final boolean DEBUG = Log.isLoggable(TAG_CAR, Log.DEBUG);
42 
43     protected final ICarBase mCar;
44 
CarManagerBase(ICarBase car)45     public CarManagerBase(ICarBase car) {
46         mCar = car;
47     }
48 
getContext()49     protected Context getContext() {
50         return mCar.getContext();
51     }
52 
getEventHandler()53     protected Handler getEventHandler() {
54         return mCar.getEventHandler();
55     }
56 
handleRemoteExceptionFromCarService(RemoteException e, T returnValue)57     protected <T> T handleRemoteExceptionFromCarService(RemoteException e, T returnValue) {
58         return mCar.handleRemoteExceptionFromCarService(e, returnValue);
59     }
60 
handleRemoteExceptionFromCarService(RemoteException e)61     protected void handleRemoteExceptionFromCarService(RemoteException e) {
62         mCar.handleRemoteExceptionFromCarService(e);
63     }
64 
65     /**
66      * Handles runtime and remote exception from CarService.
67      */
handleExceptionFromCarService(Exception e, T returnValue)68     protected <T> T handleExceptionFromCarService(Exception e, T returnValue) {
69         if (e instanceof RemoteException) {
70             return handleRemoteExceptionFromCarService((RemoteException) e, returnValue);
71         }
72 
73         if (e instanceof RuntimeException) {
74             Slog.w(TAG_CAR, "Car service threw Runtime Exception.", e);
75             return returnValue;
76         }
77 
78         // exception should be either runtime or remote exception
79         Slog.wtf(TAG_CAR, "Car service threw Exception.", e);
80 
81         return returnValue;
82     }
83 
84     /**
85      * Handle disconnection of car service (=crash). As car service has crashed already, this call
86      * should only clean up any listeners / callbacks passed from client. Clearing object passed
87      * to car service is not necessary as car service has crashed. Note that Car*Manager will not
88      * work any more as all binders are invalid. Client should re-create all Car*Managers when
89      * car service is restarted.
90      */
onCarDisconnected()91     protected abstract void onCarDisconnected();
92 
93     /**
94      * Adds a {@link Dumpable} to a "compatible" container (i.e., an object that extends
95      * {@link DumpableContainer}, {@code Activity}, etc...).
96      *
97      * @return supplied dumpable, or {@code null} if {@code container} is not compatible.
98      */
99     @Nullable
addDumpable(Object container, Supplier<T> dumpableSupplier)100     protected <T extends Dumpable> T addDumpable(Object container, Supplier<T> dumpableSupplier) {
101         if (container instanceof Activity) {
102             T dumpable = dumpableSupplier.get();
103             if (DEBUG) {
104                 Slog.d(TAG_CAR, "Adding " + dumpable.getDumpableName() + " to actvity "
105                         + container);
106             }
107             ((Activity) container).addDumpable(dumpable);
108             return dumpable;
109         }
110         if (container instanceof DumpableContainer) {
111             T dumpable = dumpableSupplier.get();
112             if (DEBUG) {
113                 Slog.d(TAG_CAR, "Adding " + dumpable.getDumpableName() + " to DumpableContainer "
114                         + container);
115             }
116             ((DumpableContainer) container).addDumpable(dumpable);
117             return dumpable;
118         }
119         Slog.v(TAG_CAR, "NOT adding dumpable to object (" + container
120                 + ") that doesn't implement addDumpable");
121         return null;
122     }
123 }
124