Debian Codegraph @

core/java/android/hardware/input/InputDeviceVibrator.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2020 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.CallbackExecutor;
20 import android.annotation.NonNull;
21 import android.app.ActivityThread;
22 import android.content.Context;
23 import android.hardware.vibrator.IVibrator;
24 import android.os.Binder;
25 import android.os.IVibratorStateListener;
26 import android.os.VibrationAttributes;
27 import android.os.VibrationEffect;
28 import android.os.Vibrator;
29 import android.os.VibratorInfo;
30 import android.util.ArrayMap;
31 import android.util.Log;
32
33 import com.android.internal.annotations.GuardedBy;
34 import com.android.internal.util.Preconditions;
35
36 import java.util.concurrent.Executor;
37
38 /**
39 * Vibrator implementation that communicates with the input device vibrators.
40 */
41 final class InputDeviceVibrator extends Vibrator {
42 private static final String TAG = "InputDeviceVibrator";
43
44 // mDeviceId represents InputDevice ID the vibrator belongs to
45 private final int mDeviceId;
46 private final VibratorInfo mVibratorInfo;
47 private final Binder mToken;
48 private final InputManager mInputManager;
49
50 @GuardedBy("mDelegates")
51 private final ArrayMap<OnVibratorStateChangedListener,
52 OnVibratorStateChangedListenerDelegate> mDelegates = new ArrayMap<>();
53
54 InputDeviceVibrator(InputManager inputManager, int deviceId, int vibratorId) {
55 mInputManager = inputManager;
56 mDeviceId = deviceId;
57 mVibratorInfo = new VibratorInfo.Builder(vibratorId)
58 .setCapabilities(IVibrator.CAP_AMPLITUDE_CONTROL)
59 // The supported effect and braking lists are known to be empty for input devices,
60 // which is different from not being set (that means the device support is unknown).
61 .setSupportedEffects(new int[0])
62 .setSupportedBraking(new int[0])
63 .build();
64 mToken = new Binder();
65 }
66
67 private class OnVibratorStateChangedListenerDelegate extends
68 IVibratorStateListener.Stub {
69 private final Executor mExecutor;
70 private final OnVibratorStateChangedListener mListener;
71
72 OnVibratorStateChangedListenerDelegate(@NonNull OnVibratorStateChangedListener listener,
73 @NonNull Executor executor) {
74 mExecutor = executor;
75 mListener = listener;
76 }
77
78 @Override
79 public void onVibrating(boolean isVibrating) {
80 mExecutor.execute(() -> mListener.onVibratorStateChanged(isVibrating));
81 }
82 }
83
84 @Override
85 protected VibratorInfo getInfo() {
86 return mVibratorInfo;
87 }
88
89 @Override
90 public boolean hasVibrator() {
91 return true;
92 }
93
94 @Override
95 public boolean isVibrating() {
96 return mInputManager.isVibrating(mDeviceId);
97 }
98
99 /**
100 * Adds a listener for vibrator state changes. Callbacks will be executed on the main thread.
101 * If the listener was previously added and not removed, this call will be ignored.
102 *
103 * @param listener listener to be added
104 */
105 @Override
106 public void addVibratorStateListener(@NonNull OnVibratorStateChangedListener listener) {
107 Preconditions.checkNotNull(listener);
108 Context context = ActivityThread.currentApplication();
109 addVibratorStateListener(context.getMainExecutor(), listener);
110 }
111
112 /**
113 * Adds a listener for vibrator state change. If the listener was previously added and not
114 * removed, this call will be ignored.
115 *
116 * @param listener Listener to be added.
117 * @param executor The {@link Executor} on which the listener's callbacks will be executed on.
118 */
119 @Override
120 public void addVibratorStateListener(
121 @NonNull @CallbackExecutor Executor executor,
122 @NonNull OnVibratorStateChangedListener listener) {
123 Preconditions.checkNotNull(listener);
124 Preconditions.checkNotNull(executor);
125
126 synchronized (mDelegates) {
127 // If listener is already registered, reject and return.
128 if (mDelegates.containsKey(listener)) {
129 Log.w(TAG, "Listener already registered.");
130 return;
131 }
132
133 final OnVibratorStateChangedListenerDelegate delegate =
134 new OnVibratorStateChangedListenerDelegate(listener, executor);
135 if (!mInputManager.registerVibratorStateListener(mDeviceId, delegate)) {
136 Log.w(TAG, "Failed to register vibrate state listener");
137 return;
138 }
139 mDelegates.put(listener, delegate);
140
141 }
142 }
143
144 /**
145 * Removes the listener for vibrator state changes. If the listener was not previously
146 * registered, this call will do nothing.
147 *
148 * @param listener Listener to be removed.
149 */
150 @Override
151 public void removeVibratorStateListener(@NonNull OnVibratorStateChangedListener listener) {
152 Preconditions.checkNotNull(listener);
153
154 synchronized (mDelegates) {
155 // Check if the listener is registered, otherwise will return.
156 if (mDelegates.containsKey(listener)) {
157 final OnVibratorStateChangedListenerDelegate delegate = mDelegates.get(listener);
158
159 if (!mInputManager.unregisterVibratorStateListener(mDeviceId, delegate)) {
160 Log.w(TAG, "Failed to unregister vibrate state listener");
161 return;
162 }
163 mDelegates.remove(listener);
164 }
165 }
166 }
167
168 @Override
169 public boolean hasAmplitudeControl() {
170 return mVibratorInfo.hasCapability(IVibrator.CAP_AMPLITUDE_CONTROL);
171 }
172
173 /**
174 * @hide
175 */
176 @Override
177 public void vibrate(int uid, String opPkg, @NonNull VibrationEffect effect, String reason,
178 @NonNull VibrationAttributes attributes) {
179 mInputManager.vibrate(mDeviceId, effect, mToken);
180 }
181
182 @Override
183 public void cancel() {
184 mInputManager.cancelVibrate(mDeviceId, mToken);
185 }
186
187 @Override
188 public void cancel(int usageFilter) {
189 cancel();
190 }
191 }