• 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 static android.car.feature.Flags.asyncAudioServiceInit;
20 
21 import android.annotation.Nullable;
22 import android.car.Car;
23 import android.car.builtin.util.Slogf;
24 import android.car.hardware.power.CarPowerManager;
25 import android.content.Context;
26 import android.util.ArrayMap;
27 import android.util.Log;
28 
29 import com.android.car.audio.CarAudioService;
30 import com.android.car.power.CarPowerManagementService;
31 import com.android.internal.annotations.VisibleForTesting;
32 
33 /**
34  * Copy of frameworks/base/core/java/com/android/server/LocalServices.java
35  * This is for accessing other car service components.
36  */
37 public final class CarLocalServices {
38 
39     private static final String TAG = CarLog.tagFor(CarLocalServices.class);
40 
41     private static final boolean DBG = Slogf.isLoggable(TAG, Log.DEBUG);
42 
CarLocalServices()43     private CarLocalServices() {}
44 
45     private static final ArrayMap<Class<?>, Object> sLocalServiceObjects =
46             new ArrayMap<Class<?>, Object>();
47 
48     /**
49      * Returns a local service instance that implements the specified interface.
50      *
51      * @param type The type of service.
52      * @return The service object.
53      */
54     @SuppressWarnings("unchecked")
55     @Nullable
getService(Class<T> type)56     public static <T> T getService(Class<T> type) {
57         if (DBG) {
58             Slogf.d(TAG, " getService " + type.getSimpleName());
59         }
60         if (asyncAudioServiceInit()) {
61             if (type.equals(CarAudioService.class)) {
62                 throw new UnsupportedOperationException("Use getCarAudioService instead.");
63             }
64         }
65         synchronized (sLocalServiceObjects) {
66             return (T) sLocalServiceObjects.get(type);
67         }
68     }
69 
70      /**
71      * Returns a {@link CarAudioService}.
72      *
73      * <p>Note that the returned service is not fully initialized. Caller should call
74      * {@link CarAudioService#waitForInitComplete} to make sure the initialization is complete.
75      */
76     @SuppressWarnings("unchecked")
77     @Nullable
getCarAudioService()78     public static CarAudioService getCarAudioService() {
79         if (DBG) {
80             Slogf.d(TAG, " getService CarAudioService");
81         }
82         synchronized (sLocalServiceObjects) {
83             return (CarAudioService) sLocalServiceObjects.get(CarAudioService.class);
84         }
85     }
86 
87     /**
88      * Adds a service instance of the specified interface to the global registry of local services.
89      */
addService(Class<T> type, T service)90     public static <T> void addService(Class<T> type, T service) {
91         synchronized (sLocalServiceObjects) {
92             if (sLocalServiceObjects.containsKey(type)) {
93                 throw new IllegalStateException("Overriding service registration");
94             }
95             if (DBG) {
96                 Slogf.d(TAG, " Adding " + type.getSimpleName());
97             }
98             sLocalServiceObjects.put(type, service);
99         }
100     }
101 
102     /**
103      * Remove a service instance, must be only used in tests.
104      */
105     @VisibleForTesting
removeServiceForTest(Class<T> type)106     public static <T> void removeServiceForTest(Class<T> type) {
107         if (DBG) {
108             Slogf.d(TAG, " Removing " + type.getSimpleName());
109         }
110         synchronized (sLocalServiceObjects) {
111             sLocalServiceObjects.remove(type);
112         }
113     }
114 
115     /**
116      * Remove all registered services. Should be called when car service restarts.
117      */
removeAllServices()118     public static void removeAllServices() {
119         if (DBG) {
120             Slogf.d(TAG, " removeAllServices");
121         }
122         synchronized (sLocalServiceObjects) {
123             sLocalServiceObjects.clear();
124         }
125     }
126 
127     /**
128      * Create CarPowerManager from registered CarPowerManagementService.
129      * @param context
130      * @return Newly created CarPowerManager. It will return null if CarPowerManagementService is
131      * not registered, which can only happen in test setup.
132      */
133     @Nullable
createCarPowerManager(Context context)134     public static CarPowerManager createCarPowerManager(Context context) {
135         // This does not require connection as binder will be passed to CarPowerManager directly.
136         Car car = new Car(context, /* service= */null, /* handler= */ null);
137         CarPowerManagementService service = getService(CarPowerManagementService.class);
138         if (service == null) {
139             return null;
140         }
141         return new CarPowerManager(car, service);
142     }
143 }
144