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
This commit is contained in:
Sokovikov 2016-03-03 04:08:10 -08:00 committed by Facebook Github Bot 7
parent d2d00e0fcd
commit 1bab7c5182
13 changed files with 169 additions and 2 deletions

View File

@ -162,6 +162,10 @@ const APIExamples = [
key: 'ToastAndroidExample', key: 'ToastAndroidExample',
module: require('./ToastAndroidExample'), module: require('./ToastAndroidExample'),
}, },
{
key: 'VibrationExample',
module: require('./VibrationExample'),
},
{ {
key: 'XHRExample', key: 'XHRExample',
module: require('./XHRExample'), module: require('./XHRExample'),

View File

@ -245,8 +245,8 @@ var APIExamples: Array<UIExplorerExample> = [
module: require('./TransformExample'), module: require('./TransformExample'),
}, },
{ {
key: 'VibrationIOSExample', key: 'VibrationExample',
module: require('./VibrationIOSExample'), module: require('./VibrationExample'),
}, },
{ {
key: 'XHRExample', key: 'XHRExample',

View File

@ -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 (
<TouchableHighlight
style={styles.wrapper}
onPress={() => Vibration.vibrate()}>
<View style={styles.button}>
<Text>Vibrate</Text>
</View>
</TouchableHighlight>
);
},
}];
var styles = StyleSheet.create({
wrapper: {
borderRadius: 5,
marginBottom: 5,
},
button: {
backgroundColor: '#eeeeee',
padding: 10,
},
});

View File

@ -6,6 +6,7 @@
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/> <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
<uses-permission android:name="android.permission.VIBRATE"/>
<application <application
android:allowBackup="true" android:allowBackup="true"

View File

@ -0,0 +1,39 @@
/**
* 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.
*
* @providesModule Vibration
* @flow
*/
'use strict';
var RCTVibration = require('NativeModules').Vibration;
var Platform = require('Platform');
/**
* The Vibration API is exposed at `Vibration.vibrate()`.
* The vibration is asynchronous so this method will return immediately.
*
* There will be no effect on devices that do not support Vibration, eg. the simulator.
*
* Note for android
* add `<uses-permission android:name="android.permission.VIBRATE"/>` 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;

View File

@ -16,6 +16,8 @@ var RCTVibration = require('NativeModules').Vibration;
var invariant = require('fbjs/lib/invariant'); 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 * The Vibration API is exposed at `VibrationIOS.vibrate()`. On iOS, calling this
* function will trigger a one second vibration. The vibration is asynchronous * function will trigger a one second vibration. The vibration is asynchronous
* so this method will return immediately. * so this method will return immediately.
@ -27,6 +29,9 @@ var invariant = require('fbjs/lib/invariant');
*/ */
var VibrationIOS = { var VibrationIOS = {
/**
* @deprecated
*/
vibrate: function() { vibrate: function() {
invariant( invariant(
arguments[0] === undefined, arguments[0] === undefined,

View File

@ -86,6 +86,7 @@ var ReactNative = {
get StyleSheet() { return require('StyleSheet'); }, get StyleSheet() { return require('StyleSheet'); },
get TimePickerAndroid() { return require('TimePickerAndroid'); }, get TimePickerAndroid() { return require('TimePickerAndroid'); },
get UIManager() { return require('UIManager'); }, get UIManager() { return require('UIManager'); },
get Vibration() { return require('Vibration'); },
get VibrationIOS() { return require('VibrationIOS'); }, get VibrationIOS() { return require('VibrationIOS'); },
// Plugins // Plugins

View File

@ -98,6 +98,7 @@ var ReactNative = Object.assign(Object.create(require('react')), {
StyleSheet: require('StyleSheet'), StyleSheet: require('StyleSheet'),
TimePickerAndroid: require('TimePickerAndroid'), TimePickerAndroid: require('TimePickerAndroid'),
UIManager: require('UIManager'), UIManager: require('UIManager'),
Vibration: require('Vibration'),
VibrationIOS: require('VibrationIOS'), VibrationIOS: require('VibrationIOS'),
// Plugins // Plugins

View File

@ -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',
)

View File

@ -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);
}
}
}

View File

@ -26,6 +26,7 @@ android_library(
react_native_target('java/com/facebook/react/views/viewpager:viewpager'), 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/views/webview:webview'),
react_native_target('java/com/facebook/react/modules/appstate:appstate'), 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/camera:camera'),
react_native_target('java/com/facebook/react/modules/clipboard:clipboard'), react_native_target('java/com/facebook/react/modules/clipboard:clipboard'),
react_native_target('java/com/facebook/react/modules/core:core'), react_native_target('java/com/facebook/react/modules/core:core'),

View File

@ -33,6 +33,7 @@ import com.facebook.react.modules.statusbar.StatusBarModule;
import com.facebook.react.modules.storage.AsyncStorageModule; import com.facebook.react.modules.storage.AsyncStorageModule;
import com.facebook.react.modules.timepicker.TimePickerDialogModule; import com.facebook.react.modules.timepicker.TimePickerDialogModule;
import com.facebook.react.modules.toast.ToastModule; 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.modules.websocket.WebSocketModule;
import com.facebook.react.uimanager.ViewManager; import com.facebook.react.uimanager.ViewManager;
import com.facebook.react.views.art.ARTRenderableViewManager; import com.facebook.react.views.art.ARTRenderableViewManager;
@ -81,6 +82,7 @@ public class MainReactPackage implements ReactPackage {
new StatusBarModule(reactContext), new StatusBarModule(reactContext),
new TimePickerDialogModule(reactContext), new TimePickerDialogModule(reactContext),
new ToastModule(reactContext), new ToastModule(reactContext),
new VibrationModule(reactContext),
new WebSocketModule(reactContext) new WebSocketModule(reactContext)
); );
} }

View File

@ -252,6 +252,7 @@ var apis = [
'../Libraries/Components/TimePickerAndroid/TimePickerAndroid.android.js', '../Libraries/Components/TimePickerAndroid/TimePickerAndroid.android.js',
'../Libraries/Components/ToastAndroid/ToastAndroid.android.js', '../Libraries/Components/ToastAndroid/ToastAndroid.android.js',
'../Libraries/Vibration/VibrationIOS.ios.js', '../Libraries/Vibration/VibrationIOS.ios.js',
'../Libraries/Vibration/Vibration.js',
]; ];
var stylesWithPermalink = [ var stylesWithPermalink = [