1 /*
2  * Copyright (C) 2019 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 com.android.car.ui.paintbooth.overlays;
18 
19 import android.content.Context;
20 import android.os.RemoteException;
21 import android.util.Log;
22 
23 import androidx.annotation.NonNull;
24 
25 import java.lang.reflect.InvocationTargetException;
26 import java.util.List;
27 import java.util.Map;
28 
29 /**
30  * Abstraction around {@link IOverlayManager}, used to deal with the fact that this is a hidden
31  * API. {@link OverlayManagerImpl} should be excluded when compiling Paintbooth outside of the
32  * system image.
33  */
34 public interface OverlayManager {
35     String TAG = OverlayManager.class.getSimpleName();
36 
37     /** Information about a single overlay affecting a target APK */
38     interface OverlayInfo {
39         /** Name of the overlay */
40         @NonNull
getPackageName()41         String getPackageName();
42         /** Whether this overlay is enabled or not */
isEnabled()43         boolean isEnabled();
44     }
45 
46     /**
47      * Returns a map of available overlays, indexed by the package name each overlay applies to
48      */
49     @NonNull
getOverlays()50     Map<String, List<OverlayInfo>> getOverlays() throws RemoteException;
51 
52     /**
53      * Enables/disables a given overlay
54      * @param packageName an overlay package name (obtained from
55      * {@link OverlayInfo#getPackageName()})
56      */
applyOverlay(@onNull String packageName, boolean enable)57     void applyOverlay(@NonNull String packageName, boolean enable) throws RemoteException;
58 
59     /** A null {@link OverlayManager} */
60     final class OverlayManagerStub implements OverlayManager {
61         @Override
62         @NonNull
getOverlays()63         public Map<String, List<OverlayInfo>> getOverlays() throws RemoteException {
64             throw new RemoteException("Overlay manager is not available");
65         }
66 
67         @Override
applyOverlay(@onNull String packageName, boolean enable)68         public void applyOverlay(@NonNull String packageName, boolean enable)
69                 throws RemoteException {
70             throw new RemoteException("Overlay manager is not available");
71         }
72     }
73 
74     /** Returns a valid {@link OverlayManager} for this environment */
75     @NonNull
getInstance(Context context)76     static OverlayManager getInstance(Context context) {
77         try {
78             return (OverlayManager) Class
79                     .forName("com.android.car.ui.paintbooth.overlays.OverlayManagerImpl")
80                     .getConstructor(Context.class)
81                     .newInstance(context);
82         } catch (ClassNotFoundException | IllegalAccessException | InstantiationException
83                 | NoSuchMethodException | InvocationTargetException e) {
84             Log.i(TAG, "Overlay Manager is not available");
85             return new OverlayManagerStub();
86         }
87     }
88 }
89