From e20e8a3cc87cbb624add4de00149b1f55bbdcfba Mon Sep 17 00:00:00 2001 From: Sokovikov Date: Tue, 29 Mar 2016 21:44:33 -0700 Subject: [PATCH] Add support for vibration patterns Summary:and "not supported" warnings for ios Closes https://github.com/facebook/react-native/pull/6400 Differential Revision: D3113988 fb-gh-sync-id: 169623f157ec59c38b4368cbc668c85201b67ed8 fbshipit-source-id: 169623f157ec59c38b4368cbc668c85201b67ed8 --- Examples/UIExplorer/VibrationExample.js | 70 +++++++++++++++---- Libraries/Vibration/Vibration.js | 30 +++++++- .../modules/vibration/VibrationModule.java | 22 ++++++ 3 files changed, 106 insertions(+), 16 deletions(-) diff --git a/Examples/UIExplorer/VibrationExample.js b/Examples/UIExplorer/VibrationExample.js index 11f17d368..4e2a9f51c 100644 --- a/Examples/UIExplorer/VibrationExample.js +++ b/Examples/UIExplorer/VibrationExample.js @@ -27,20 +27,64 @@ var { exports.framework = 'React'; exports.title = 'Vibration'; exports.description = 'Vibration API'; -exports.examples = [{ - title: 'Vibration.vibrate()', - render() { - return ( - Vibration.vibrate()}> - - Vibrate - - - ); +exports.examples = [ + { + title: 'Vibration.vibrate()', + render() { + return ( + Vibration.vibrate()}> + + Vibrate + + + ); + }, }, -}]; + { + title: 'Vibration.vibrate([0, 500, 200, 500])', + render() { + return ( + Vibration.vibrate([0, 500, 200, 500])}> + + Vibrate once + + + ); + }, + }, + { + title: 'Vibration.vibrate([0, 500, 200, 500], true)', + render() { + return ( + Vibration.vibrate([0, 500, 200, 500], true)}> + + Vibrate until cancel + + + ); + }, + }, + { + title: 'Vibration.cancel()', + render() { + return ( + Vibration.cancel()}> + + Cancel + + + ); + }, + }, +]; var styles = StyleSheet.create({ wrapper: { diff --git a/Libraries/Vibration/Vibration.js b/Libraries/Vibration/Vibration.js index 9258eadaa..a60f75453 100644 --- a/Libraries/Vibration/Vibration.js +++ b/Libraries/Vibration/Vibration.js @@ -27,11 +27,35 @@ var Platform = require('Platform'); */ var Vibration = { - vibrate: function(duration: number = 400) { + vibrate: function(pattern: number | Array = 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'); + } } 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(); } } }; diff --git a/ReactAndroid/src/main/java/com/facebook/react/modules/vibration/VibrationModule.java b/ReactAndroid/src/main/java/com/facebook/react/modules/vibration/VibrationModule.java index 1a0023ac0..21c54a188 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/modules/vibration/VibrationModule.java +++ b/ReactAndroid/src/main/java/com/facebook/react/modules/vibration/VibrationModule.java @@ -15,6 +15,7 @@ import android.os.Vibrator; 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 { @@ -34,4 +35,25 @@ public class VibrationModule extends ReactContextBaseJavaModule { 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(); + } + } }