Keun-young Park | d462a91 | 2019-02-11 08:53:42 -0800 | [diff] [blame] | 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 | |
Keun young Park | a5fa478 | 2019-04-16 18:56:27 -0700 | [diff] [blame] | 19 | import android.annotation.Nullable; |
Keun young Park | c8c565a | 2019-10-11 16:13:34 -0700 | [diff] [blame] | 20 | import android.car.Car; |
Keun young Park | a5fa478 | 2019-04-16 18:56:27 -0700 | [diff] [blame] | 21 | import android.car.hardware.power.CarPowerManager; |
| 22 | import android.content.Context; |
Keun-young Park | d462a91 | 2019-02-11 08:53:42 -0800 | [diff] [blame] | 23 | import android.util.ArrayMap; |
Eric Jeong | bd5fb56 | 2020-12-21 13:49:40 -0800 | [diff] [blame] | 24 | import android.util.Slog; |
Keun-young Park | d462a91 | 2019-02-11 08:53:42 -0800 | [diff] [blame] | 25 | |
Eric Jeong | bc351e2 | 2020-07-31 13:54:17 -0700 | [diff] [blame] | 26 | import com.android.car.power.CarPowerManagementService; |
Keun-young Park | d462a91 | 2019-02-11 08:53:42 -0800 | [diff] [blame] | 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 { |
Keun young Park | bf6298e | 2021-02-19 14:51:23 -0800 | [diff] [blame] | 34 | private static final boolean DBG = false; |
| 35 | |
| 36 | private static final String TAG = CarLog.tagFor(CarLocalServices.class); |
| 37 | |
Keun-young Park | d462a91 | 2019-02-11 08:53:42 -0800 | [diff] [blame] | 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 | public static <T> T getService(Class<T> type) { |
Keun young Park | bf6298e | 2021-02-19 14:51:23 -0800 | [diff] [blame] | 51 | if (DBG) { |
| 52 | Slog.d(TAG, " getService " + type.getSimpleName()); |
| 53 | } |
Keun-young Park | d462a91 | 2019-02-11 08:53:42 -0800 | [diff] [blame] | 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 | */ |
| 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 | } |
Keun young Park | bf6298e | 2021-02-19 14:51:23 -0800 | [diff] [blame] | 67 | if (DBG) { |
| 68 | Slog.d(TAG, " Adding " + type.getSimpleName()); |
| 69 | } |
Keun-young Park | d462a91 | 2019-02-11 08:53:42 -0800 | [diff] [blame] | 70 | sLocalServiceObjects.put(type, service); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Remove a service instance, must be only used in tests. |
| 76 | */ |
| 77 | @VisibleForTesting |
| 78 | public static <T> void removeServiceForTest(Class<T> type) { |
Keun young Park | bf6298e | 2021-02-19 14:51:23 -0800 | [diff] [blame] | 79 | if (DBG) { |
| 80 | Slog.d(TAG, " Removing " + type.getSimpleName()); |
| 81 | } |
Keun-young Park | d462a91 | 2019-02-11 08:53:42 -0800 | [diff] [blame] | 82 | synchronized (sLocalServiceObjects) { |
| 83 | sLocalServiceObjects.remove(type); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Remove all registered services. Should be called when car service restarts. |
| 89 | */ |
| 90 | public static void removeAllServices() { |
Keun young Park | bf6298e | 2021-02-19 14:51:23 -0800 | [diff] [blame] | 91 | if (DBG) { |
| 92 | Slog.d(TAG, " removeAllServices"); |
| 93 | } |
Keun-young Park | d462a91 | 2019-02-11 08:53:42 -0800 | [diff] [blame] | 94 | synchronized (sLocalServiceObjects) { |
| 95 | sLocalServiceObjects.clear(); |
| 96 | } |
| 97 | } |
Keun young Park | a5fa478 | 2019-04-16 18:56:27 -0700 | [diff] [blame] | 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 |
| 106 | public static CarPowerManager createCarPowerManager(Context context) { |
Keun young Park | c8c565a | 2019-10-11 16:13:34 -0700 | [diff] [blame] | 107 | // This does not require connection as binder will be passed to CarPowerManager directly. |
| 108 | Car car = new Car(context, /* service= */null, /* handler= */ null); |
Keun young Park | a5fa478 | 2019-04-16 18:56:27 -0700 | [diff] [blame] | 109 | CarPowerManagementService service = getService(CarPowerManagementService.class); |
| 110 | if (service == null) { |
| 111 | return null; |
| 112 | } |
Keun young Park | c8c565a | 2019-10-11 16:13:34 -0700 | [diff] [blame] | 113 | return new CarPowerManager(car, service); |
Keun young Park | a5fa478 | 2019-04-16 18:56:27 -0700 | [diff] [blame] | 114 | } |
Keun-young Park | d462a91 | 2019-02-11 08:53:42 -0800 | [diff] [blame] | 115 | } |