/
ShadowVibrator.java
105 lines (91 loc) · 2.7 KB
/
ShadowVibrator.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
package org.robolectric.shadows;
import static android.os.Build.VERSION_CODES.R;
import android.os.Vibrator;
import android.os.vibrator.VibrationEffectSegment;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;
@Implements(Vibrator.class)
public class ShadowVibrator {
boolean vibrating;
boolean cancelled;
long milliseconds;
protected long[] pattern;
protected final List<VibrationEffectSegment> vibrationEffectSegments = new ArrayList<>();
protected final List<Integer> supportedPrimitives = new ArrayList<>();
int repeat;
boolean hasVibrator = true;
boolean hasAmplitudeControl = false;
int effectId;
/**
* Controls the return value of {@link Vibrator#hasVibrator()} the default is true.
*/
public void setHasVibrator(boolean hasVibrator) {
this.hasVibrator = hasVibrator;
}
/**
* Controls the return value of {@link Vibrator#hasAmplitudeControl()} the default is false.
*/
public void setHasAmplitudeControl(boolean hasAmplitudeControl) {
this.hasAmplitudeControl = hasAmplitudeControl;
}
/**
* Returns true if the Vibrator is currently vibrating as controlled by {@link Vibrator#vibrate(long)}
*/
public boolean isVibrating() {
return vibrating;
}
/**
* Returns true if the Vibrator has been cancelled.
*/
public boolean isCancelled() {
return cancelled;
}
/**
* Returns the last vibration duration in MS.
*/
public long getMilliseconds() {
return milliseconds;
}
/**
* Returns the last vibration pattern.
*/
public long[] getPattern() {
return pattern;
}
/**
* Returns the last vibration effect ID of a {@link VibrationEffect#Prebaked} (e.g. {@link
* VibrationEffect#EFFECT_CLICK}).
*
* <p>This field is non-zero only if a {@link VibrationEffect#Prebaked} was ever requested.
*/
public int getEffectId() {
return effectId;
}
/**
* Returns the last vibration repeat times.
*/
public int getRepeat() {
return repeat;
}
/** Returns the last list of {@link VibrationEffectSegment}. */
public List<VibrationEffectSegment> getVibrationEffectSegments() {
return vibrationEffectSegments;
}
@Implementation(minSdk = R)
protected boolean areAllPrimitivesSupported(int... primitiveIds) {
for (int i = 0; i < primitiveIds.length; i++) {
if (!supportedPrimitives.contains(primitiveIds[i])) {
return false;
}
}
return true;
}
/** Adds supported vibration primitives. */
public void setSupportedPrimitives(Collection<Integer> primitives) {
supportedPrimitives.clear();
supportedPrimitives.addAll(primitives);
}
}