/
ShadowSystemVibrator.java
181 lines (157 loc) · 6.22 KB
/
ShadowSystemVibrator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package org.robolectric.shadows;
import static android.os.Build.VERSION_CODES.JELLY_BEAN_MR1;
import static android.os.Build.VERSION_CODES.JELLY_BEAN_MR2;
import static android.os.Build.VERSION_CODES.KITKAT_WATCH;
import static android.os.Build.VERSION_CODES.LOLLIPOP;
import static android.os.Build.VERSION_CODES.N_MR1;
import static android.os.Build.VERSION_CODES.O;
import static android.os.Build.VERSION_CODES.P;
import static android.os.Build.VERSION_CODES.Q;
import static android.os.Build.VERSION_CODES.R;
import static android.os.Build.VERSION_CODES.S;
import android.media.AudioAttributes;
import android.os.Handler;
import android.os.Looper;
import android.os.SystemVibrator;
import android.os.VibrationAttributes;
import android.os.VibrationEffect;
import android.os.vibrator.VibrationEffectSegment;
import java.util.List;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;
import org.robolectric.util.ReflectionHelpers;
@Implements(value = SystemVibrator.class, isInAndroidSdk = false)
public class ShadowSystemVibrator extends ShadowVibrator {
private final Handler handler = new Handler(Looper.getMainLooper());
private final Runnable stopVibratingRunnable = () -> vibrating = false;
@Implementation
protected boolean hasVibrator() {
return hasVibrator;
}
@Implementation(minSdk = O)
protected boolean hasAmplitudeControl() {
return hasAmplitudeControl;
}
@Implementation(maxSdk = JELLY_BEAN_MR1)
protected void vibrate(long[] pattern, int repeat) {
recordVibratePattern(pattern, repeat);
}
@Implementation(minSdk = JELLY_BEAN_MR2, maxSdk = KITKAT_WATCH)
protected void vibrate(int owningUid, String owningPackage, long[] pattern, int repeat) {
recordVibratePattern(pattern, repeat);
}
@Implementation(minSdk = LOLLIPOP, maxSdk = N_MR1)
protected void vibrate(
int uid, String opPkg, long[] pattern, int repeat, AudioAttributes attributes) {
recordVibratePattern(pattern, repeat);
}
@Implementation(maxSdk = JELLY_BEAN_MR1)
public void vibrate(long milliseconds) {
recordVibrate(milliseconds);
}
@Implementation(minSdk = JELLY_BEAN_MR2, maxSdk = KITKAT_WATCH)
public void vibrate(int owningUid, String owningPackage, long milliseconds) {
recordVibrate(milliseconds);
}
@Implementation(minSdk = LOLLIPOP, maxSdk = N_MR1)
protected void vibrate(int uid, String opPkg, long milliseconds, AudioAttributes attributes) {
recordVibrate(milliseconds);
}
@Implementation(minSdk = O, maxSdk = P)
protected void vibrate(
int uid, String opPkg, VibrationEffect effect, AudioAttributes attributes) {
vibrate(uid, opPkg, effect, null, attributes);
}
@Implementation(minSdk = Q, maxSdk = R)
protected void vibrate(
int uid, String opPkg, VibrationEffect effect, String reason, AudioAttributes attributes) {
try {
Class<?> waveformClass = Class.forName("android.os.VibrationEffect$Waveform");
Class<?> prebakedClass = Class.forName("android.os.VibrationEffect$Prebaked");
Class<?> oneShotClass = Class.forName("android.os.VibrationEffect$OneShot");
if (waveformClass.isInstance(effect)) {
recordVibratePattern(
(long[]) ReflectionHelpers.callInstanceMethod(effect, "getTimings"),
ReflectionHelpers.callInstanceMethod(effect, "getRepeatIndex"));
} else if (prebakedClass.isInstance(effect)) {
recordVibratePredefined(
ReflectionHelpers.callInstanceMethod(effect, "getDuration"),
ReflectionHelpers.callInstanceMethod(effect, "getId"));
} else if (oneShotClass.isInstance(effect)) {
long timing;
if (RuntimeEnvironment.getApiLevel() >= P) {
timing = ReflectionHelpers.callInstanceMethod(effect, "getDuration");
} else {
timing = ReflectionHelpers.callInstanceMethod(effect, "getTiming");
}
recordVibrate(timing);
} else {
throw new UnsupportedOperationException(
"unrecognized effect type " + effect.getClass().getName());
}
} catch (ClassNotFoundException e) {
throw new UnsupportedOperationException(
"unrecognized effect type " + effect.getClass().getName(), e);
}
}
@Implementation(minSdk = S)
protected void vibrate(
int uid,
String opPkg,
VibrationEffect effect,
String reason,
VibrationAttributes attributes) {
if (effect instanceof VibrationEffect.Composed) {
VibrationEffect.Composed composedEffect = (VibrationEffect.Composed) effect;
recordVibratePattern(composedEffect.getSegments(), composedEffect.getRepeatIndex());
} else {
throw new UnsupportedOperationException(
"unrecognized effect type " + effect.getClass().getName());
}
}
private void recordVibratePattern(List<VibrationEffectSegment> segments, int repeatIndex) {
long[] pattern = new long[segments.size()];
int i = 0;
for (VibrationEffectSegment segment : segments) {
pattern[i] = segment.getDuration();
i++;
}
vibrationEffectSegments.clear();
vibrationEffectSegments.addAll(segments);
recordVibratePattern(pattern, repeatIndex);
}
private void recordVibratePredefined(long milliseconds, int effectId) {
vibrating = true;
this.effectId = effectId;
this.milliseconds = milliseconds;
handler.removeCallbacks(stopVibratingRunnable);
handler.postDelayed(stopVibratingRunnable, this.milliseconds);
}
private void recordVibrate(long milliseconds) {
vibrating = true;
this.milliseconds = milliseconds;
handler.removeCallbacks(stopVibratingRunnable);
handler.postDelayed(stopVibratingRunnable, this.milliseconds);
}
protected void recordVibratePattern(long[] pattern, int repeat) {
vibrating = true;
this.pattern = pattern;
this.repeat = repeat;
handler.removeCallbacks(stopVibratingRunnable);
if (repeat < 0) {
long endDelayMillis = 0;
for (long t : pattern) {
endDelayMillis += t;
}
this.milliseconds = endDelayMillis;
handler.postDelayed(stopVibratingRunnable, endDelayMillis);
}
}
@Implementation
protected void cancel() {
cancelled = true;
vibrating = false;
handler.removeCallbacks(stopVibratingRunnable);
}
}