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