From 1bab7c5182415ea6f1bee41ad2cc7bec629005af Mon Sep 17 00:00:00 2001 From: Sokovikov Date: Thu, 3 Mar 2016 04:08:10 -0800 Subject: [PATCH] vibration module Summary:I will fix other notes from https://github.com/facebook/react-native/pull/2794 if I get positive feedback. Closes https://github.com/facebook/react-native/pull/6061 Reviewed By: nicklockwood Differential Revision: D2982173 Pulled By: dmmiller fb-gh-sync-id: d1e9407798b0293b090897a10996085b0f0c1b3e shipit-source-id: d1e9407798b0293b090897a10996085b0f0c1b3e --- Examples/UIExplorer/UIExplorerList.android.js | 4 ++ Examples/UIExplorer/UIExplorerList.ios.js | 4 +- Examples/UIExplorer/VibrationExample.js | 54 +++++++++++++++++++ .../android/app/src/main/AndroidManifest.xml | 1 + Libraries/Vibration/Vibration.js | 39 ++++++++++++++ Libraries/Vibration/VibrationIOS.ios.js | 5 ++ Libraries/react-native/react-native.js | 1 + Libraries/react-native/react-native.js.flow | 1 + .../com/facebook/react/modules/vibration/BUCK | 21 ++++++++ .../modules/vibration/VibrationModule.java | 37 +++++++++++++ .../main/java/com/facebook/react/shell/BUCK | 1 + .../react/shell/MainReactPackage.java | 2 + website/server/extractDocs.js | 1 + 13 files changed, 169 insertions(+), 2 deletions(-) create mode 100644 Examples/UIExplorer/VibrationExample.js create mode 100644 Libraries/Vibration/Vibration.js create mode 100644 ReactAndroid/src/main/java/com/facebook/react/modules/vibration/BUCK create mode 100644 ReactAndroid/src/main/java/com/facebook/react/modules/vibration/VibrationModule.java diff --git a/Examples/UIExplorer/UIExplorerList.android.js b/Examples/UIExplorer/UIExplorerList.android.js index 3dfd365d1..a912ae9ab 100644 --- a/Examples/UIExplorer/UIExplorerList.android.js +++ b/Examples/UIExplorer/UIExplorerList.android.js @@ -162,6 +162,10 @@ const APIExamples = [ key: 'ToastAndroidExample', module: require('./ToastAndroidExample'), }, + { + key: 'VibrationExample', + module: require('./VibrationExample'), + }, { key: 'XHRExample', module: require('./XHRExample'), diff --git a/Examples/UIExplorer/UIExplorerList.ios.js b/Examples/UIExplorer/UIExplorerList.ios.js index b2fefd125..8d770d1f4 100644 --- a/Examples/UIExplorer/UIExplorerList.ios.js +++ b/Examples/UIExplorer/UIExplorerList.ios.js @@ -245,8 +245,8 @@ var APIExamples: Array = [ module: require('./TransformExample'), }, { - key: 'VibrationIOSExample', - module: require('./VibrationIOSExample'), + key: 'VibrationExample', + module: require('./VibrationExample'), }, { key: 'XHRExample', diff --git a/Examples/UIExplorer/VibrationExample.js b/Examples/UIExplorer/VibrationExample.js new file mode 100644 index 000000000..11f17d368 --- /dev/null +++ b/Examples/UIExplorer/VibrationExample.js @@ -0,0 +1,54 @@ +/** + * The examples provided by Facebook are for non-commercial testing and + * evaluation purposes only. + * + * Facebook reserves all rights not expressly granted. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL + * FACEBOOK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN + * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + * @flow + */ +'use strict'; + +var React = require('react-native'); +var { + StyleSheet, + View, + Text, + TouchableHighlight, + Vibration, +} = React; + +exports.framework = 'React'; +exports.title = 'Vibration'; +exports.description = 'Vibration API'; +exports.examples = [{ + title: 'Vibration.vibrate()', + render() { + return ( + Vibration.vibrate()}> + + Vibrate + + + ); + }, +}]; + +var styles = StyleSheet.create({ + wrapper: { + borderRadius: 5, + marginBottom: 5, + }, + button: { + backgroundColor: '#eeeeee', + padding: 10, + }, +}); diff --git a/Examples/UIExplorer/android/app/src/main/AndroidManifest.xml b/Examples/UIExplorer/android/app/src/main/AndroidManifest.xml index 968a554cf..c1e847515 100644 --- a/Examples/UIExplorer/android/app/src/main/AndroidManifest.xml +++ b/Examples/UIExplorer/android/app/src/main/AndroidManifest.xml @@ -6,6 +6,7 @@ + ` to `AndroidManifest.xml` + * + * Vibration patterns are currently unsupported. + */ + +var Vibration = { + vibrate: function(duration: number = 400) { + if (Platform.OS === 'android') { + RCTVibration.vibrate(duration); + } else { + RCTVibration.vibrate(); + } + } +}; + +module.exports = Vibration; diff --git a/Libraries/Vibration/VibrationIOS.ios.js b/Libraries/Vibration/VibrationIOS.ios.js index 1e0a17942..56da5a815 100644 --- a/Libraries/Vibration/VibrationIOS.ios.js +++ b/Libraries/Vibration/VibrationIOS.ios.js @@ -16,6 +16,8 @@ var RCTVibration = require('NativeModules').Vibration; var invariant = require('fbjs/lib/invariant'); /** + * NOTE: `VibrationIOS` is being deprecated. Use `Vibration` instead. + * * The Vibration API is exposed at `VibrationIOS.vibrate()`. On iOS, calling this * function will trigger a one second vibration. The vibration is asynchronous * so this method will return immediately. @@ -27,6 +29,9 @@ var invariant = require('fbjs/lib/invariant'); */ var VibrationIOS = { + /** + * @deprecated + */ vibrate: function() { invariant( arguments[0] === undefined, diff --git a/Libraries/react-native/react-native.js b/Libraries/react-native/react-native.js index e8eca3c72..2b5a0bc96 100644 --- a/Libraries/react-native/react-native.js +++ b/Libraries/react-native/react-native.js @@ -86,6 +86,7 @@ var ReactNative = { get StyleSheet() { return require('StyleSheet'); }, get TimePickerAndroid() { return require('TimePickerAndroid'); }, get UIManager() { return require('UIManager'); }, + get Vibration() { return require('Vibration'); }, get VibrationIOS() { return require('VibrationIOS'); }, // Plugins diff --git a/Libraries/react-native/react-native.js.flow b/Libraries/react-native/react-native.js.flow index e7aa80590..70d05ead7 100644 --- a/Libraries/react-native/react-native.js.flow +++ b/Libraries/react-native/react-native.js.flow @@ -98,6 +98,7 @@ var ReactNative = Object.assign(Object.create(require('react')), { StyleSheet: require('StyleSheet'), TimePickerAndroid: require('TimePickerAndroid'), UIManager: require('UIManager'), + Vibration: require('Vibration'), VibrationIOS: require('VibrationIOS'), // Plugins diff --git a/ReactAndroid/src/main/java/com/facebook/react/modules/vibration/BUCK b/ReactAndroid/src/main/java/com/facebook/react/modules/vibration/BUCK new file mode 100644 index 000000000..19ceed59d --- /dev/null +++ b/ReactAndroid/src/main/java/com/facebook/react/modules/vibration/BUCK @@ -0,0 +1,21 @@ +include_defs('//ReactAndroid/DEFS') + +android_library( + name = 'vibration', + srcs = glob(['**/*.java']), + deps = [ + react_native_target('java/com/facebook/react/bridge:bridge'), + react_native_target('java/com/facebook/react/common:common'), + react_native_target('java/com/facebook/react/modules/core:core'), + react_native_dep('libraries/fbcore/src/main/java/com/facebook/common/logging:logging'), + react_native_dep('third-party/java/infer-annotations:infer-annotations'), + react_native_dep('third-party/java/jsr-305:jsr-305'), + ], + visibility = [ + 'PUBLIC', + ], +) + +project_config( + src_target = ':vibration', +) 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 new file mode 100644 index 000000000..1a0023ac0 --- /dev/null +++ b/ReactAndroid/src/main/java/com/facebook/react/modules/vibration/VibrationModule.java @@ -0,0 +1,37 @@ +/** + * Copyright (c) 2015-present, Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + */ + +package com.facebook.react.modules.vibration; + +import android.content.Context; +import android.os.Vibrator; + +import com.facebook.react.bridge.ReactApplicationContext; +import com.facebook.react.bridge.ReactContextBaseJavaModule; +import com.facebook.react.bridge.ReactMethod; + +public class VibrationModule extends ReactContextBaseJavaModule { + + public VibrationModule(ReactApplicationContext reactContext) { + super(reactContext); + } + + @Override + public String getName() { + return "Vibration"; + } + + @ReactMethod + public void vibrate(int duration) { + Vibrator v = (Vibrator) getReactApplicationContext().getSystemService(Context.VIBRATOR_SERVICE); + if (v != null) { + v.vibrate(duration); + } + } +} diff --git a/ReactAndroid/src/main/java/com/facebook/react/shell/BUCK b/ReactAndroid/src/main/java/com/facebook/react/shell/BUCK index ec0c816b8..198de5a74 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/shell/BUCK +++ b/ReactAndroid/src/main/java/com/facebook/react/shell/BUCK @@ -26,6 +26,7 @@ android_library( react_native_target('java/com/facebook/react/views/viewpager:viewpager'), react_native_target('java/com/facebook/react/views/webview:webview'), react_native_target('java/com/facebook/react/modules/appstate:appstate'), + react_native_target('java/com/facebook/react/modules/vibration:vibration'), react_native_target('java/com/facebook/react/modules/camera:camera'), react_native_target('java/com/facebook/react/modules/clipboard:clipboard'), react_native_target('java/com/facebook/react/modules/core:core'), diff --git a/ReactAndroid/src/main/java/com/facebook/react/shell/MainReactPackage.java b/ReactAndroid/src/main/java/com/facebook/react/shell/MainReactPackage.java index 01a30148d..f75c8d92b 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/shell/MainReactPackage.java +++ b/ReactAndroid/src/main/java/com/facebook/react/shell/MainReactPackage.java @@ -33,6 +33,7 @@ import com.facebook.react.modules.statusbar.StatusBarModule; import com.facebook.react.modules.storage.AsyncStorageModule; import com.facebook.react.modules.timepicker.TimePickerDialogModule; import com.facebook.react.modules.toast.ToastModule; +import com.facebook.react.modules.vibration.VibrationModule; import com.facebook.react.modules.websocket.WebSocketModule; import com.facebook.react.uimanager.ViewManager; import com.facebook.react.views.art.ARTRenderableViewManager; @@ -81,6 +82,7 @@ public class MainReactPackage implements ReactPackage { new StatusBarModule(reactContext), new TimePickerDialogModule(reactContext), new ToastModule(reactContext), + new VibrationModule(reactContext), new WebSocketModule(reactContext) ); } diff --git a/website/server/extractDocs.js b/website/server/extractDocs.js index be0c8e2f5..782d006fb 100644 --- a/website/server/extractDocs.js +++ b/website/server/extractDocs.js @@ -252,6 +252,7 @@ var apis = [ '../Libraries/Components/TimePickerAndroid/TimePickerAndroid.android.js', '../Libraries/Components/ToastAndroid/ToastAndroid.android.js', '../Libraries/Vibration/VibrationIOS.ios.js', + '../Libraries/Vibration/Vibration.js', ]; var stylesWithPermalink = [