Skip to content

Commit

Permalink
vibration module
Browse files Browse the repository at this point in the history
Summary:I will fix other notes from #2794 if I get positive feedback.
Closes #6061

Reviewed By: nicklockwood

Differential Revision: D2982173

Pulled By: dmmiller

fb-gh-sync-id: d1e9407798b0293b090897a10996085b0f0c1b3e
shipit-source-id: d1e9407798b0293b090897a10996085b0f0c1b3e
  • Loading branch information
skv-headless authored and Facebook Github Bot 7 committed Mar 3, 2016
1 parent d2d00e0 commit 1bab7c5
Show file tree
Hide file tree
Showing 13 changed files with 169 additions and 2 deletions.
4 changes: 4 additions & 0 deletions Examples/UIExplorer/UIExplorerList.android.js
Expand Up @@ -162,6 +162,10 @@ const APIExamples = [
key: 'ToastAndroidExample',
module: require('./ToastAndroidExample'),
},
{
key: 'VibrationExample',
module: require('./VibrationExample'),
},
{
key: 'XHRExample',
module: require('./XHRExample'),
Expand Down
4 changes: 2 additions & 2 deletions Examples/UIExplorer/UIExplorerList.ios.js
Expand Up @@ -245,8 +245,8 @@ var APIExamples: Array<UIExplorerExample> = [
module: require('./TransformExample'),
},
{
key: 'VibrationIOSExample',
module: require('./VibrationIOSExample'),
key: 'VibrationExample',
module: require('./VibrationExample'),
},
{
key: 'XHRExample',
Expand Down
54 changes: 54 additions & 0 deletions 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 (
<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,
},
});
Expand Up @@ -6,6 +6,7 @@
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
<uses-permission android:name="android.permission.VIBRATE"/>

<application
android:allowBackup="true"
Expand Down
39 changes: 39 additions & 0 deletions Libraries/Vibration/Vibration.js
@@ -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;
5 changes: 5 additions & 0 deletions Libraries/Vibration/VibrationIOS.ios.js
Expand Up @@ -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.
Expand All @@ -27,6 +29,9 @@ var invariant = require('fbjs/lib/invariant');
*/

var VibrationIOS = {
/**
* @deprecated
*/
vibrate: function() {
invariant(
arguments[0] === undefined,
Expand Down
1 change: 1 addition & 0 deletions Libraries/react-native/react-native.js
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions Libraries/react-native/react-native.js.flow
Expand Up @@ -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
Expand Down
@@ -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',
)
@@ -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);
}
}
}
1 change: 1 addition & 0 deletions ReactAndroid/src/main/java/com/facebook/react/shell/BUCK
Expand Up @@ -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'),
Expand Down
Expand Up @@ -33,6 +33,7 @@
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;
Expand Down Expand Up @@ -81,6 +82,7 @@ public List<NativeModule> createNativeModules(ReactApplicationContext reactConte
new StatusBarModule(reactContext),
new TimePickerDialogModule(reactContext),
new ToastModule(reactContext),
new VibrationModule(reactContext),
new WebSocketModule(reactContext)
);
}
Expand Down
1 change: 1 addition & 0 deletions website/server/extractDocs.js
Expand Up @@ -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 = [
Expand Down

0 comments on commit 1bab7c5

Please sign in to comment.