Debian Codegraph @

core/java/android/hardware/input/InputDeviceVibratorManager.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.NonNull;
20 import android.annotation.Nullable;
21 import android.os.Binder;
22 import android.os.CombinedVibration;
23 import android.os.NullVibrator;
24 import android.os.VibrationAttributes;
25 import android.os.Vibrator;
26 import android.os.VibratorManager;
27 import android.util.SparseArray;
28 import android.view.InputDevice;
29
30 import com.android.internal.annotations.GuardedBy;
31
32 /**
33 * Vibrator manager implementation that communicates with the input device vibrators.
34 *
35 * @hide
36 */
37 public class InputDeviceVibratorManager extends VibratorManager
38 implements InputManager.InputDeviceListener {
39 private static final String TAG = "InputDeviceVibratorManager";
40 private static final boolean DEBUG = false;
41
42 private final Binder mToken;
43 private final InputManager mInputManager;
44
45 // The input device Id.
46 private final int mDeviceId;
47 // Vibrator map from Vibrator Id to Vibrator
48 @GuardedBy("mVibrators")
49 private final SparseArray<Vibrator> mVibrators = new SparseArray<>();
50
51 public InputDeviceVibratorManager(InputManager inputManager, int deviceId) {
52 mInputManager = inputManager;
53 mDeviceId = deviceId;
54 mToken = new Binder();
55
56 initializeVibrators();
57 }
58
59 private void initializeVibrators() {
60 synchronized (mVibrators) {
61 mVibrators.clear();
62 InputDevice inputDevice = InputDevice.getDevice(mDeviceId);
63 final int[] vibratorIds =
64 mInputManager.getVibratorIds(mDeviceId);
65 for (int i = 0; i < vibratorIds.length; i++) {
66 mVibrators.put(vibratorIds[i],
67 new InputDeviceVibrator(mInputManager, mDeviceId, vibratorIds[i]));
68 }
69 }
70 }
71
72 @Override
73 public void onInputDeviceAdded(int deviceId) {
74 }
75
76 @Override
77 public void onInputDeviceRemoved(int deviceId) {
78 synchronized (mVibrators) {
79 if (deviceId == mDeviceId) {
80 mVibrators.clear();
81 }
82 }
83 }
84
85 @Override
86 public void onInputDeviceChanged(int deviceId) {
87 if (deviceId == mDeviceId) {
88 initializeVibrators();
89 }
90 }
91
92 @NonNull
93 @Override
94 public int[] getVibratorIds() {
95 synchronized (mVibrators) {
96 int[] vibratorIds = new int[mVibrators.size()];
97 for (int idx = 0; idx < mVibrators.size(); idx++) {
98 vibratorIds[idx++] = mVibrators.keyAt(idx);
99 }
100 return vibratorIds;
101 }
102 }
103
104 @NonNull
105 @Override
106 public Vibrator getVibrator(int vibratorId) {
107 synchronized (mVibrators) {
108 if (mVibrators.contains(vibratorId)) {
109 return mVibrators.get(vibratorId);
110 }
111 }
112 return NullVibrator.getInstance();
113 }
114
115 @NonNull
116 @Override
117 public Vibrator getDefaultVibrator() {
118 // Returns vibrator ID 0
119 synchronized (mVibrators) {
120 if (mVibrators.size() > 0) {
121 return mVibrators.valueAt(0);
122 }
123 }
124 return NullVibrator.getInstance();
125 }
126
127 @Override
128 public void vibrate(int uid, String opPkg, @NonNull CombinedVibration effect,
129 String reason, @Nullable VibrationAttributes attributes) {
130 mInputManager.vibrate(mDeviceId, effect, mToken);
131 }
132
133 @Override
134 public void cancel() {
135 mInputManager.cancelVibrate(mDeviceId, mToken);
136 }
137
138 @Override
139 public void cancel(int usageFilter) {
140 cancel();
141 }
142 }