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.Slog; 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") getService(Class<T> type)50 public static <T> T getService(Class<T> type) { 51 if (DBG) { 52 Slog.d(TAG, " getService " + type.getSimpleName()); 53 } 54 synchronized (sLocalServiceObjects) { 55 return (T) sLocalServiceObjects.get(type); 56 } 57 } 58 59 /** 60 * Adds a service instance of the specified interface to the global registry of local services. 61 */ addService(Class<T> type, T service)62 public static <T> void addService(Class<T> type, T service) { 63 synchronized (sLocalServiceObjects) { 64 if (sLocalServiceObjects.containsKey(type)) { 65 throw new IllegalStateException("Overriding service registration"); 66 } 67 if (DBG) { 68 Slog.d(TAG, " Adding " + type.getSimpleName()); 69 } 70 sLocalServiceObjects.put(type, service); 71 } 72 } 73 74 /** 75 * Remove a service instance, must be only used in tests. 76 */ 77 @VisibleForTesting removeServiceForTest(Class<T> type)78 public static <T> void removeServiceForTest(Class<T> type) { 79 if (DBG) { 80 Slog.d(TAG, " Removing " + type.getSimpleName()); 81 } 82 synchronized (sLocalServiceObjects) { 83 sLocalServiceObjects.remove(type); 84 } 85 } 86 87 /** 88 * Remove all registered services. Should be called when car service restarts. 89 */ removeAllServices()90 public static void removeAllServices() { 91 if (DBG) { 92 Slog.d(TAG, " removeAllServices"); 93 } 94 synchronized (sLocalServiceObjects) { 95 sLocalServiceObjects.clear(); 96 } 97 } 98 99 /** 100 * Create CarPowerManager from registered CarPowerManagementService. 101 * @param context 102 * @return Newly created CarPowerManager. It will return null if CarPowerManagementService is 103 * not registered, which can only happen in test setup. 104 */ 105 @Nullable createCarPowerManager(Context context)106 public static CarPowerManager createCarPowerManager(Context context) { 107 // This does not require connection as binder will be passed to CarPowerManager directly. 108 Car car = new Car(context, /* service= */null, /* handler= */ null); 109 CarPowerManagementService service = getService(CarPowerManagementService.class); 110 if (service == null) { 111 return null; 112 } 113 return new CarPowerManager(car, service); 114 } 115 } 116