Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 57 additions & 13 deletions Examples/UIExplorer/VibrationExample.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,64 @@ var {
exports.framework = 'React';
exports.title = 'Vibration';
exports.description = 'Vibration API';
exports.examples = [{
title: 'Vibration.vibrate()',
render() {
return (
<TouchableHighlight
style={styles.wrapper}
onPress={() => Vibration.vibrate()}>
<View style={styles.button}>
<Text>Vibrate</Text>
</View>
</TouchableHighlight>
);
exports.examples = [
{
title: 'Vibration.vibrate()',
render() {
return (
<TouchableHighlight
style={styles.wrapper}
onPress={() => Vibration.vibrate()}>
<View style={styles.button}>
<Text>Vibrate</Text>
</View>
</TouchableHighlight>
);
},
},
}];
{
title: 'Vibration.vibrate([0, 500, 200, 500])',
render() {
return (
<TouchableHighlight
style={styles.wrapper}
onPress={() => Vibration.vibrate([0, 500, 200, 500])}>
<View style={styles.button}>
<Text>Vibrate once</Text>
</View>
</TouchableHighlight>
);
},
},
{
title: 'Vibration.vibrate([0, 500, 200, 500], true)',
render() {
return (
<TouchableHighlight
style={styles.wrapper}
onPress={() => Vibration.vibrate([0, 500, 200, 500], true)}>
<View style={styles.button}>
<Text>Vibrate until cancel</Text>
</View>
</TouchableHighlight>
);
},
},
{
title: 'Vibration.cancel()',
render() {
return (
<TouchableHighlight
style={styles.wrapper}
onPress={() => Vibration.cancel()}>
<View style={styles.button}>
<Text>Cancel</Text>
</View>
</TouchableHighlight>
);
},
},
];

var styles = StyleSheet.create({
wrapper: {
Expand Down
30 changes: 27 additions & 3 deletions Libraries/Vibration/Vibration.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,35 @@ var Platform = require('Platform');
*/

var Vibration = {
vibrate: function(duration: number = 400) {
vibrate: function(pattern: number | Array<number> = 400, repeat: boolean = false) {
if (Platform.OS === 'android') {
RCTVibration.vibrate(duration);
if (typeof pattern === 'number') {
RCTVibration.vibrate(pattern);
} else if (Array.isArray(pattern)) {
RCTVibration.vibrateByPattern(pattern, repeat ? 0 : -1);
} else {
throw new Error('Vibration pattern should be a number or array');
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If pattern is not a number or array, let's throw an error.

} else {
RCTVibration.vibrate();
if (typeof pattern === 'number') {
RCTVibration.vibrate();
} else if (Array.isArray(pattern)) {
console.warn('Vibration patterns are not supported on iOS');
} else {
throw new Error('Vibration pattern should be a number or array');
}
}
},
/**
* Stop vibration
*
* @platform android
*/
cancel: function() {
if (Platform.OS === 'ios') {
console.warn('Vibration.cancel is not supported on iOS');
} else {
RCTVibration.cancel();
}
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.ReadableArray;

public class VibrationModule extends ReactContextBaseJavaModule {

Expand All @@ -34,4 +35,25 @@ public void vibrate(int duration) {
v.vibrate(duration);
}
}

@ReactMethod
public void vibrateByPattern(ReadableArray pattern, int repeat) {
long[] patternLong = new long[pattern.size()];
for (int i = 0; i < pattern.size(); i++) {
patternLong[i] = pattern.getInt(i);
}

Vibrator v = (Vibrator) getReactApplicationContext().getSystemService(Context.VIBRATOR_SERVICE);
if (v != null) {
v.vibrate(patternLong, repeat);
}
}

@ReactMethod
public void cancel() {
Vibrator v = (Vibrator) getReactApplicationContext().getSystemService(Context.VIBRATOR_SERVICE);
if (v != null) {
v.cancel();
}
}
}