blob: 75dc4e4a2d75477e05657354a48f299e0f38b438 [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;
Eric Jeongbd5fb562020-12-21 13:49:40 -080024import android.util.Slog;
Keun-young Parkd462a912019-02-11 08:53:42 -080025
Eric Jeongbc351e22020-07-31 13:54:17 -070026import com.android.car.power.CarPowerManagementService;
Keun-young Parkd462a912019-02-11 08:53:42 -080027import 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 */
33public class CarLocalServices {
Keun young Parkbf6298e2021-02-19 14:51:23 -080034 private static final boolean DBG = false;
35
36 private static final String TAG = CarLog.tagFor(CarLocalServices.class);
37
Keun-young Parkd462a912019-02-11 08:53:42 -080038 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 Parkbf6298e2021-02-19 14:51:23 -080051 if (DBG) {
52 Slog.d(TAG, " getService " + type.getSimpleName());
53 }
Keun-young Parkd462a912019-02-11 08:53:42 -080054 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 Parkbf6298e2021-02-19 14:51:23 -080067 if (DBG) {
68 Slog.d(TAG, " Adding " + type.getSimpleName());
69 }
Keun-young Parkd462a912019-02-11 08:53:42 -080070 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 Parkbf6298e2021-02-19 14:51:23 -080079 if (DBG) {
80 Slog.d(TAG, " Removing " + type.getSimpleName());
81 }
Keun-young Parkd462a912019-02-11 08:53:42 -080082 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 Parkbf6298e2021-02-19 14:51:23 -080091 if (DBG) {
92 Slog.d(TAG, " removeAllServices");
93 }
Keun-young Parkd462a912019-02-11 08:53:42 -080094 synchronized (sLocalServiceObjects) {
95 sLocalServiceObjects.clear();
96 }
97 }
Keun young Parka5fa4782019-04-16 18:56:27 -070098
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 Parkc8c565a2019-10-11 16:13:34 -0700107 // 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 Parka5fa4782019-04-16 18:56:27 -0700109 CarPowerManagementService service = getService(CarPowerManagementService.class);
110 if (service == null) {
111 return null;
112 }
Keun young Parkc8c565a2019-10-11 16:13:34 -0700113 return new CarPowerManager(car, service);
Keun young Parka5fa4782019-04-16 18:56:27 -0700114 }
Keun-young Parkd462a912019-02-11 08:53:42 -0800115}