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