Debian Codegraph @

core/java/android/hardware/lights/SystemLightsManager.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2021 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 android.hardware.lights;
18
19 import android.Manifest;
20 import android.annotation.NonNull;
21 import android.annotation.RequiresPermission;
22 import android.content.Context;
23 import android.hardware.lights.LightsManager.LightsSession;
24 import android.os.RemoteException;
25 import android.os.ServiceManager;
26 import android.os.ServiceManager.ServiceNotFoundException;
27 import android.util.CloseGuard;
28
29 import com.android.internal.annotations.VisibleForTesting;
30 import com.android.internal.util.Preconditions;
31
32 import java.lang.ref.Reference;
33 import java.util.List;
34
35 /**
36 * The LightsManager class allows control over device lights.
37 *
38 * @hide
39 */
40 public final class SystemLightsManager extends LightsManager {
41 private static final String TAG = "LightsManager";
42
43 @NonNull private final ILightsManager mService;
44
45 /**
46 * Creates a SystemLightsManager.
47 *
48 * @hide
49 */
50 public SystemLightsManager(@NonNull Context context) throws ServiceNotFoundException {
51 this(context, ILightsManager.Stub.asInterface(
52 ServiceManager.getServiceOrThrow(Context.LIGHTS_SERVICE)));
53 }
54
55 /**
56 * Creates a SystemLightsManager with a provided service implementation.
57 *
58 * @hide
59 */
60 @VisibleForTesting
61 public SystemLightsManager(@NonNull Context context, @NonNull ILightsManager service) {
62 super(context);
63 mService = Preconditions.checkNotNull(service);
64 }
65
66 /**
67 * Returns the lights available on the device.
68 *
69 * @return A list of available lights
70 */
71 @RequiresPermission(Manifest.permission.CONTROL_DEVICE_LIGHTS)
72 @Override
73 public @NonNull List<Light> getLights() {
74 try {
75 return mService.getLights();
76 } catch (RemoteException e) {
77 throw e.rethrowFromSystemServer();
78 }
79 }
80
81 /**
82 * Returns the state of a specified light.
83 *
84 * @hide
85 */
86 @RequiresPermission(Manifest.permission.CONTROL_DEVICE_LIGHTS)
87 @Override
88 public @NonNull LightState getLightState(@NonNull Light light) {
89 Preconditions.checkNotNull(light);
90 try {
91 return mService.getLightState(light.getId());
92 } catch (RemoteException e) {
93 throw e.rethrowFromSystemServer();
94 }
95 }
96
97 /**
98 * Creates a new LightsSession that can be used to control the device lights.
99 */
100 @RequiresPermission(Manifest.permission.CONTROL_DEVICE_LIGHTS)
101 @Override
102 public @NonNull LightsSession openSession() {
103 try {
104 final LightsSession session = new SystemLightsSession();
105 mService.openSession(session.getToken(), 0);
106 return session;
107 } catch (RemoteException e) {
108 throw e.rethrowFromSystemServer();
109 }
110 }
111
112 /**
113 *
114 * Creates a new {@link LightsSession}
115 *
116 * @param priority the larger this number, the higher the priority of this session when multiple
117 * light state requests arrive simultaneously.
118 *
119 * @hide
120 */
121 @RequiresPermission(Manifest.permission.CONTROL_DEVICE_LIGHTS)
122 @Override
123 public @NonNull LightsSession openSession(int priority) {
124 try {
125 final LightsSession session = new SystemLightsSession();
126 mService.openSession(session.getToken(), priority);
127 return session;
128 } catch (RemoteException e) {
129 throw e.rethrowFromSystemServer();
130 }
131 }
132
133 /**
134 * Encapsulates a session that can be used to control device lights and represents the lifetime
135 * of the requests.
136 */
137 public final class SystemLightsSession extends LightsManager.LightsSession
138 implements AutoCloseable {
139
140 private final CloseGuard mCloseGuard = new CloseGuard();
141 private boolean mClosed = false;
142
143 /**
144 * Instantiated by {@link LightsManager#openSession()}.
145 */
146 @RequiresPermission(Manifest.permission.CONTROL_DEVICE_LIGHTS)
147 private SystemLightsSession() {
148 mCloseGuard.open("SystemLightsSession.close");
149 }
150
151 /**
152 * Sends a request to modify the states of multiple lights.
153 *
154 * <p>This method only controls lights that aren't overridden by higher-priority sessions.
155 * Additionally, lights not controlled by this session can be controlled by lower-priority
156 * sessions.
157 *
158 * @param request the settings for lights that should change
159 */
160 @RequiresPermission(Manifest.permission.CONTROL_DEVICE_LIGHTS)
161 @Override
162 public void requestLights(@NonNull LightsRequest request) {
163 Preconditions.checkNotNull(request);
164 if (!mClosed) {
165 try {
166 List<Integer> idList = request.getLights();
167 List<LightState> stateList = request.getLightStates();
168 int[] ids = new int[idList.size()];
169 for (int i = 0; i < idList.size(); i++) {
170 ids[i] = idList.get(i);
171 }
172 LightState[] states = new LightState[stateList.size()];
173 for (int i = 0; i < stateList.size(); i++) {
174 states[i] = stateList.get(i);
175 }
176 mService.setLightStates(getToken(), ids, states);
177 } catch (RemoteException e) {
178 throw e.rethrowFromSystemServer();
179 }
180 }
181 }
182
183 /**
184 * Closes the session, reverting all changes made through it.
185 */
186 @RequiresPermission(Manifest.permission.CONTROL_DEVICE_LIGHTS)
187 @Override
188 public void close() {
189 if (!mClosed) {
190 try {
191 mService.closeSession(getToken());
192 mClosed = true;
193 mCloseGuard.close();
194 } catch (RemoteException e) {
195 throw e.rethrowFromSystemServer();
196 }
197 }
198 Reference.reachabilityFence(this);
199 }
200
201 /** @hide */
202 @Override
203 protected void finalize() throws Throwable {
204 try {
205 mCloseGuard.warnIfOpen();
206 close();
207 } finally {
208 super.finalize();
209 }
210 }
211 }
212 }