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