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