Skip to content

Commit dcf9cb8

Browse files
committed
[Android] vibration patterns with examples
1 parent d0f6a1a commit dcf9cb8

File tree

3 files changed

+106
-16
lines changed

3 files changed

+106
-16
lines changed

Examples/UIExplorer/VibrationExample.js

Lines changed: 57 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,64 @@ var {
2727
exports.framework = 'React';
2828
exports.title = 'Vibration';
2929
exports.description = 'Vibration API';
30-
exports.examples = [{
31-
title: 'Vibration.vibrate()',
32-
render() {
33-
return (
34-
<TouchableHighlight
35-
style={styles.wrapper}
36-
onPress={() => Vibration.vibrate()}>
37-
<View style={styles.button}>
38-
<Text>Vibrate</Text>
39-
</View>
40-
</TouchableHighlight>
41-
);
30+
exports.examples = [
31+
{
32+
title: 'Vibration.vibrate()',
33+
render() {
34+
return (
35+
<TouchableHighlight
36+
style={styles.wrapper}
37+
onPress={() => Vibration.vibrate()}>
38+
<View style={styles.button}>
39+
<Text>Vibrate</Text>
40+
</View>
41+
</TouchableHighlight>
42+
);
43+
},
4244
},
43-
}];
45+
{
46+
title: 'Vibration.vibrate([0, 500, 200, 500], -1)',
47+
render() {
48+
return (
49+
<TouchableHighlight
50+
style={styles.wrapper}
51+
onPress={() => Vibration.vibrate([0, 500, 200, 500], -1)}>
52+
<View style={styles.button}>
53+
<Text>Vibrate once</Text>
54+
</View>
55+
</TouchableHighlight>
56+
);
57+
},
58+
},
59+
{
60+
title: 'Vibration.vibrate([0, 500, 200, 500], 0)',
61+
render() {
62+
return (
63+
<TouchableHighlight
64+
style={styles.wrapper}
65+
onPress={() => Vibration.vibrate([0, 500, 200, 500], 0)}>
66+
<View style={styles.button}>
67+
<Text>Vibrate until cancel</Text>
68+
</View>
69+
</TouchableHighlight>
70+
);
71+
},
72+
},
73+
{
74+
title: 'Vibration.cancel()',
75+
render() {
76+
return (
77+
<TouchableHighlight
78+
style={styles.wrapper}
79+
onPress={() => Vibration.cancel()}>
80+
<View style={styles.button}>
81+
<Text>Cancel</Text>
82+
</View>
83+
</TouchableHighlight>
84+
);
85+
},
86+
},
87+
];
4488

4589
var styles = StyleSheet.create({
4690
wrapper: {

Libraries/Vibration/Vibration.js

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,35 @@ var Platform = require('Platform');
2727
*/
2828

2929
var Vibration = {
30-
vibrate: function(duration: number = 400) {
30+
vibrate: function(pattern: number | Array<number> = 400, repeat: number = -1) {
3131
if (Platform.OS === 'android') {
32-
RCTVibration.vibrate(duration);
32+
if (typeof pattern === 'number') {
33+
RCTVibration.vibrate(pattern);
34+
} else if (Array.isArray(pattern)) {
35+
RCTVibration.vibrateByPattern(pattern, repeat);
36+
} else {
37+
throw new Error('Vibration pattern should be a number or array');
38+
}
3339
} else {
34-
RCTVibration.vibrate();
40+
if (typeof pattern === 'number') {
41+
RCTVibration.vibrate();
42+
} else if (Array.isArray(pattern)) {
43+
console.warn('Vibration patterns are not supported on iOS');
44+
} else {
45+
throw new Error('Vibration pattern should be a number or array');
46+
}
47+
}
48+
},
49+
/**
50+
* Stop vibration
51+
*
52+
* @platform android
53+
*/
54+
cancel: function() {
55+
if (Platform.OS === 'ios') {
56+
console.warn('Vibration.cancel is not supported on iOS');
57+
} else {
58+
RCTVibration.cancel();
3559
}
3660
}
3761
};

ReactAndroid/src/main/java/com/facebook/react/modules/vibration/VibrationModule.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import com.facebook.react.bridge.ReactApplicationContext;
1616
import com.facebook.react.bridge.ReactContextBaseJavaModule;
1717
import com.facebook.react.bridge.ReactMethod;
18+
import com.facebook.react.bridge.ReadableArray;
1819

1920
public class VibrationModule extends ReactContextBaseJavaModule {
2021

@@ -34,4 +35,25 @@ public void vibrate(int duration) {
3435
v.vibrate(duration);
3536
}
3637
}
38+
39+
@ReactMethod
40+
public void vibrateByPattern(ReadableArray pattern, int repeat) {
41+
long[] patternLong = new long[pattern.size()];
42+
for (int i = 0; i < pattern.size(); i++) {
43+
patternLong[i] = pattern.getInt(i);
44+
}
45+
46+
Vibrator v = (Vibrator) getReactApplicationContext().getSystemService(Context.VIBRATOR_SERVICE);
47+
if (v != null) {
48+
v.vibrate(patternLong, repeat);
49+
}
50+
}
51+
52+
@ReactMethod
53+
public void cancel() {
54+
Vibrator v = (Vibrator) getReactApplicationContext().getSystemService(Context.VIBRATOR_SERVICE);
55+
if (v != null) {
56+
v.cancel();
57+
}
58+
}
3759
}

0 commit comments

Comments
 (0)