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;
18 
19 import android.annotation.Nullable;
20 import android.car.Car;
21 import android.car.hardware.power.CarPowerManager;
22 import android.content.Context;
23 import android.util.ArrayMap;
24 import android.util.Log;
25 
26 import com.android.internal.annotations.VisibleForTesting;
27 
28 /**
29  * Copy of frameworks/base/core/java/com/android/server/LocalServices.java
30  * This is for accessing other car service components.
31  */
32 public class CarLocalServices {
CarLocalServices()33     private CarLocalServices() {}
34 
35     private static final ArrayMap<Class<?>, Object> sLocalServiceObjects =
36             new ArrayMap<Class<?>, Object>();
37 
38     /**
39      * Returns a local service instance that implements the specified interface.
40      *
41      * @param type The type of service.
42      * @return The service object.
43      */
44     @SuppressWarnings("unchecked")
getService(Class<T> type)45     public static <T> T getService(Class<T> type) {
46         Log.d("CarLocalServices", " getService " + type.getSimpleName());
47         synchronized (sLocalServiceObjects) {
48             return (T) sLocalServiceObjects.get(type);
49         }
50     }
51 
52     /**
53      * Adds a service instance of the specified interface to the global registry of local services.
54      */
addService(Class<T> type, T service)55     public static <T> void addService(Class<T> type, T service) {
56         synchronized (sLocalServiceObjects) {
57             if (sLocalServiceObjects.containsKey(type)) {
58                 throw new IllegalStateException("Overriding service registration");
59             }
60             Log.d("CarLocalServices", " Adding " + type.getSimpleName());
61             sLocalServiceObjects.put(type, service);
62         }
63     }
64 
65     /**
66      * Remove a service instance, must be only used in tests.
67      */
68     @VisibleForTesting
removeServiceForTest(Class<T> type)69     public static <T> void removeServiceForTest(Class<T> type) {
70         Log.d("CarLocalServices", " Removing " + type.getSimpleName());
71         synchronized (sLocalServiceObjects) {
72             sLocalServiceObjects.remove(type);
73         }
74     }
75 
76     /**
77      * Remove all registered services. Should be called when car service restarts.
78      */
removeAllServices()79     public static void removeAllServices() {
80         Log.d("CarLocalServices", " removeAllServices");
81         synchronized (sLocalServiceObjects) {
82             sLocalServiceObjects.clear();
83         }
84     }
85 
86     /**
87      * Create CarPowerManager from registered CarPowerManagementService.
88      * @param context
89      * @return Newly created CarPowerManager. It will return null if CarPowerManagementService is
90      * not registered, which can only happen in test setup.
91      */
92     @Nullable
createCarPowerManager(Context context)93     public static CarPowerManager createCarPowerManager(Context context) {
94         // This does not require connection as binder will be passed to CarPowerManager directly.
95         Car car = new Car(context, /* service= */null, /* handler= */ null);
96         CarPowerManagementService service = getService(CarPowerManagementService.class);
97         if (service == null) {
98             return null;
99         }
100         return new CarPowerManager(car, service);
101     }
102 }
103