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 | |
| 19 | import android.util.ArrayMap; |
Ram Periathiruvadi | de0ca08 | 2019-03-20 11:16:44 -0700 | [diff] [blame] | 20 | import android.util.Log; |
Keun-young Park | d462a91 | 2019-02-11 08:53:42 -0800 | [diff] [blame] | 21 | |
| 22 | import com.android.internal.annotations.VisibleForTesting; |
| 23 | |
| 24 | /** |
| 25 | * Copy of frameworks/base/core/java/com/android/server/LocalServices.java |
| 26 | * This is for accessing other car service components. |
| 27 | */ |
| 28 | public class CarLocalServices { |
| 29 | private CarLocalServices() {} |
| 30 | |
| 31 | private static final ArrayMap<Class<?>, Object> sLocalServiceObjects = |
| 32 | new ArrayMap<Class<?>, Object>(); |
| 33 | |
| 34 | /** |
| 35 | * Returns a local service instance that implements the specified interface. |
| 36 | * |
| 37 | * @param type The type of service. |
| 38 | * @return The service object. |
| 39 | */ |
| 40 | @SuppressWarnings("unchecked") |
| 41 | public static <T> T getService(Class<T> type) { |
Ram Periathiruvadi | de0ca08 | 2019-03-20 11:16:44 -0700 | [diff] [blame] | 42 | Log.d("CarLocalServices", " getService " + type.getSimpleName()); |
Keun-young Park | d462a91 | 2019-02-11 08:53:42 -0800 | [diff] [blame] | 43 | synchronized (sLocalServiceObjects) { |
| 44 | return (T) sLocalServiceObjects.get(type); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Adds a service instance of the specified interface to the global registry of local services. |
| 50 | */ |
| 51 | public static <T> void addService(Class<T> type, T service) { |
| 52 | synchronized (sLocalServiceObjects) { |
| 53 | if (sLocalServiceObjects.containsKey(type)) { |
| 54 | throw new IllegalStateException("Overriding service registration"); |
| 55 | } |
Ram Periathiruvadi | de0ca08 | 2019-03-20 11:16:44 -0700 | [diff] [blame] | 56 | Log.d("CarLocalServices", " Adding " + type.getSimpleName()); |
Keun-young Park | d462a91 | 2019-02-11 08:53:42 -0800 | [diff] [blame] | 57 | sLocalServiceObjects.put(type, service); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Remove a service instance, must be only used in tests. |
| 63 | */ |
| 64 | @VisibleForTesting |
| 65 | public static <T> void removeServiceForTest(Class<T> type) { |
Ram Periathiruvadi | de0ca08 | 2019-03-20 11:16:44 -0700 | [diff] [blame] | 66 | Log.d("CarLocalServices", " Removing " + type.getSimpleName()); |
Keun-young Park | d462a91 | 2019-02-11 08:53:42 -0800 | [diff] [blame] | 67 | synchronized (sLocalServiceObjects) { |
| 68 | sLocalServiceObjects.remove(type); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Remove all registered services. Should be called when car service restarts. |
| 74 | */ |
| 75 | public static void removeAllServices() { |
Ram Periathiruvadi | de0ca08 | 2019-03-20 11:16:44 -0700 | [diff] [blame] | 76 | Log.d("CarLocalServices", " removeAllServices"); |
Keun-young Park | d462a91 | 2019-02-11 08:53:42 -0800 | [diff] [blame] | 77 | synchronized (sLocalServiceObjects) { |
| 78 | sLocalServiceObjects.clear(); |
| 79 | } |
| 80 | } |
| 81 | } |