blob: fe3dc0734d803defb78d3161c0626c1480e23622 [file] [log] [blame]
Keun-young Parkd462a912019-02-11 08:53:42 -08001/*
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
17package com.android.car;
18
19import android.util.ArrayMap;
Ram Periathiruvadide0ca082019-03-20 11:16:44 -070020import android.util.Log;
Keun-young Parkd462a912019-02-11 08:53:42 -080021
22import 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 */
28public 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 Periathiruvadide0ca082019-03-20 11:16:44 -070042 Log.d("CarLocalServices", " getService " + type.getSimpleName());
Keun-young Parkd462a912019-02-11 08:53:42 -080043 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 Periathiruvadide0ca082019-03-20 11:16:44 -070056 Log.d("CarLocalServices", " Adding " + type.getSimpleName());
Keun-young Parkd462a912019-02-11 08:53:42 -080057 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 Periathiruvadide0ca082019-03-20 11:16:44 -070066 Log.d("CarLocalServices", " Removing " + type.getSimpleName());
Keun-young Parkd462a912019-02-11 08:53:42 -080067 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 Periathiruvadide0ca082019-03-20 11:16:44 -070076 Log.d("CarLocalServices", " removeAllServices");
Keun-young Parkd462a912019-02-11 08:53:42 -080077 synchronized (sLocalServiceObjects) {
78 sLocalServiceObjects.clear();
79 }
80 }
81}