Debian Codegraph @

core/java/android/hardware/input/InputDeviceLightsManager.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.input;
18
19 import android.annotation.NonNull;
20 import android.app.ActivityThread;
21 import android.hardware.lights.Light;
22 import android.hardware.lights.LightState;
23 import android.hardware.lights.LightsManager;
24 import android.hardware.lights.LightsRequest;
25 import android.util.CloseGuard;
26
27 import com.android.internal.util.Preconditions;
28
29 import java.lang.ref.Reference;
30 import java.util.List;
31
32 /**
33 * LightsManager manages an input device's lights {@link android.hardware.input.Light}.
34 */
35 class InputDeviceLightsManager extends LightsManager {
36 private static final String TAG = "InputDeviceLightsManager";
37 private static final boolean DEBUG = false;
38
39 private final InputManager mInputManager;
40
41 // The input device ID.
42 private final int mDeviceId;
43 // Package name
44 private final String mPackageName;
45
46 InputDeviceLightsManager(InputManager inputManager, int deviceId) {
47 super(ActivityThread.currentActivityThread().getSystemContext());
48 mInputManager = inputManager;
49 mDeviceId = deviceId;
50 mPackageName = ActivityThread.currentPackageName();
51 }
52
53 /**
54 * Returns the lights available on the device.
55 *
56 * @return A list of available lights
57 */
58 @Override
59 public @NonNull List<Light> getLights() {
60 return mInputManager.getLights(mDeviceId);
61 }
62
63 /**
64 * Returns the state of a specified light.
65 *
66 * @hide
67 */
68 @Override
69 public @NonNull LightState getLightState(@NonNull Light light) {
70 Preconditions.checkNotNull(light);
71 return mInputManager.getLightState(mDeviceId, light);
72 }
73
74 /**
75 * Creates a new LightsSession that can be used to control the device lights.
76 */
77 @Override
78 public @NonNull LightsSession openSession() {
79 final LightsSession session = new InputDeviceLightsSession();
80 mInputManager.openLightSession(mDeviceId, mPackageName, session.getToken());
81 return session;
82 }
83
84 @Override
85 public @NonNull LightsSession openSession(int priority) {
86 throw new UnsupportedOperationException();
87 }
88
89 /**
90 * Encapsulates a session that can be used to control device lights and represents the lifetime
91 * of the requests.
92 */
93 public final class InputDeviceLightsSession extends LightsManager.LightsSession
94 implements AutoCloseable {
95
96 private final CloseGuard mCloseGuard = new CloseGuard();
97 private boolean mClosed = false;
98
99 /**
100 * Instantiated by {@link LightsManager#openSession()}.
101 */
102 private InputDeviceLightsSession() {
103 mCloseGuard.open("InputDeviceLightsSession.close");
104 }
105
106 /**
107 * Sends a request to modify the states of multiple lights.
108 *
109 * @param request the settings for lights that should change
110 */
111 @Override
112 public void requestLights(@NonNull LightsRequest request) {
113 Preconditions.checkNotNull(request);
114 Preconditions.checkArgument(!mClosed);
115
116 mInputManager.requestLights(mDeviceId, request, getToken());
117 }
118
119 /**
120 * Closes the session, reverting all changes made through it.
121 */
122 @Override
123 public void close() {
124 if (!mClosed) {
125 mInputManager.closeLightSession(mDeviceId, getToken());
126 mClosed = true;
127 mCloseGuard.close();
128 }
129 Reference.reachabilityFence(this);
130 }
131
132 /** @hide */
133 @Override
134 protected void finalize() throws Throwable {
135 try {
136 mCloseGuard.warnIfOpen();
137 close();
138 } finally {
139 super.finalize();
140 }
141 }
142 }
143
144 }