/
VibrationModule.java
64 lines (53 loc) · 1.76 KB
/
VibrationModule.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
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
package com.facebook.react.modules.vibration;
import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Vibrator;
import com.facebook.fbreact.specs.NativeVibrationSpec;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.module.annotations.ReactModule;
@SuppressLint("MissingPermission")
@ReactModule(name = VibrationModule.NAME)
public class VibrationModule extends NativeVibrationSpec {
public static final String NAME = "Vibration";
public VibrationModule(ReactApplicationContext reactContext) {
super(reactContext);
}
@Override
public String getName() {
return NAME;
}
@Override
public void vibrate(double durationDouble) {
int duration = (int) durationDouble;
Vibrator v = (Vibrator) getReactApplicationContext().getSystemService(Context.VIBRATOR_SERVICE);
if (v != null) {
v.vibrate(duration);
}
}
@Override
public void vibrateByPattern(ReadableArray pattern, double repeatDouble) {
int repeat = (int) repeatDouble;
Vibrator v = (Vibrator) getReactApplicationContext().getSystemService(Context.VIBRATOR_SERVICE);
if (v != null) {
long[] patternLong = new long[pattern.size()];
for (int i = 0; i < pattern.size(); i++) {
patternLong[i] = pattern.getInt(i);
}
v.vibrate(patternLong, repeat);
}
}
@Override
public void cancel() {
Vibrator v = (Vibrator) getReactApplicationContext().getSystemService(Context.VIBRATOR_SERVICE);
if (v != null) {
v.cancel();
}
}
}