blob: ea1b6f11953c25caeabc972fd4faa4d00718b36e [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
Keun young Parka5fa4782019-04-16 18:56:27 -070019import android.annotation.Nullable;
Keun young Parkc8c565a2019-10-11 16:13:34 -070020import android.car.Car;
Keun young Parka5fa4782019-04-16 18:56:27 -070021import android.car.hardware.power.CarPowerManager;
22import android.content.Context;
Keun-young Parkd462a912019-02-11 08:53:42 -080023import android.util.ArrayMap;
Ram Periathiruvadide0ca082019-03-20 11:16:44 -070024import android.util.Log;
Keun-young Parkd462a912019-02-11 08:53:42 -080025
26import 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 */
32public 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 Periathiruvadide0ca082019-03-20 11:16:44 -070046 Log.d("CarLocalServices", " getService " + type.getSimpleName());
Keun-young Parkd462a912019-02-11 08:53:42 -080047 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 Periathiruvadide0ca082019-03-20 11:16:44 -070060 Log.d("CarLocalServices", " Adding " + type.getSimpleName());
Keun-young Parkd462a912019-02-11 08:53:42 -080061 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 Periathiruvadide0ca082019-03-20 11:16:44 -070070 Log.d("CarLocalServices", " Removing " + type.getSimpleName());
Keun-young Parkd462a912019-02-11 08:53:42 -080071 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 Periathiruvadide0ca082019-03-20 11:16:44 -070080 Log.d("CarLocalServices", " removeAllServices");
Keun-young Parkd462a912019-02-11 08:53:42 -080081 synchronized (sLocalServiceObjects) {
82 sLocalServiceObjects.clear();
83 }
84 }
Keun young Parka5fa4782019-04-16 18:56:27 -070085
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 Parkc8c565a2019-10-11 16:13:34 -070094 // 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 Parka5fa4782019-04-16 18:56:27 -070096 CarPowerManagementService service = getService(CarPowerManagementService.class);
97 if (service == null) {
98 return null;
99 }
Keun young Parkc8c565a2019-10-11 16:13:34 -0700100 return new CarPowerManager(car, service);
Keun young Parka5fa4782019-04-16 18:56:27 -0700101 }
Keun-young Parkd462a912019-02-11 08:53:42 -0800102}