Skip to content

Commit 1bab7c5

Browse files
skv-headlessFacebook Github Bot 7
authored andcommitted
vibration module
Summary:I will fix other notes from facebook#2794 if I get positive feedback. Closes facebook#6061 Reviewed By: nicklockwood Differential Revision: D2982173 Pulled By: dmmiller fb-gh-sync-id: d1e9407798b0293b090897a10996085b0f0c1b3e shipit-source-id: d1e9407798b0293b090897a10996085b0f0c1b3e
1 parent d2d00e0 commit 1bab7c5

File tree

13 files changed

+169
-2
lines changed

13 files changed

+169
-2
lines changed

Examples/UIExplorer/UIExplorerList.android.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,10 @@ const APIExamples = [
162162
key: 'ToastAndroidExample',
163163
module: require('./ToastAndroidExample'),
164164
},
165+
{
166+
key: 'VibrationExample',
167+
module: require('./VibrationExample'),
168+
},
165169
{
166170
key: 'XHRExample',
167171
module: require('./XHRExample'),

Examples/UIExplorer/UIExplorerList.ios.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,8 +245,8 @@ var APIExamples: Array<UIExplorerExample> = [
245245
module: require('./TransformExample'),
246246
},
247247
{
248-
key: 'VibrationIOSExample',
249-
module: require('./VibrationIOSExample'),
248+
key: 'VibrationExample',
249+
module: require('./VibrationExample'),
250250
},
251251
{
252252
key: 'XHRExample',
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/**
2+
* The examples provided by Facebook are for non-commercial testing and
3+
* evaluation purposes only.
4+
*
5+
* Facebook reserves all rights not expressly granted.
6+
*
7+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
8+
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
9+
* FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL
10+
* FACEBOOK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
11+
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
12+
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
13+
*
14+
* @flow
15+
*/
16+
'use strict';
17+
18+
var React = require('react-native');
19+
var {
20+
StyleSheet,
21+
View,
22+
Text,
23+
TouchableHighlight,
24+
Vibration,
25+
} = React;
26+
27+
exports.framework = 'React';
28+
exports.title = 'Vibration';
29+
exports.description = 'Vibration API';
30+
exports.examples = [{
31+
title: 'Vibration.vibrate()',
32+
render() {
33+
return (
34+
<TouchableHighlight
35+
style={styles.wrapper}
36+
onPress={() => Vibration.vibrate()}>
37+
<View style={styles.button}>
38+
<Text>Vibrate</Text>
39+
</View>
40+
</TouchableHighlight>
41+
);
42+
},
43+
}];
44+
45+
var styles = StyleSheet.create({
46+
wrapper: {
47+
borderRadius: 5,
48+
marginBottom: 5,
49+
},
50+
button: {
51+
backgroundColor: '#eeeeee',
52+
padding: 10,
53+
},
54+
});

Examples/UIExplorer/android/app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
77
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
88
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
9+
<uses-permission android:name="android.permission.VIBRATE"/>
910

1011
<application
1112
android:allowBackup="true"

Libraries/Vibration/Vibration.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* Copyright (c) 2015-present, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*
9+
* @providesModule Vibration
10+
* @flow
11+
*/
12+
'use strict';
13+
14+
var RCTVibration = require('NativeModules').Vibration;
15+
var Platform = require('Platform');
16+
17+
/**
18+
* The Vibration API is exposed at `Vibration.vibrate()`.
19+
* The vibration is asynchronous so this method will return immediately.
20+
*
21+
* There will be no effect on devices that do not support Vibration, eg. the simulator.
22+
*
23+
* Note for android
24+
* add `<uses-permission android:name="android.permission.VIBRATE"/>` to `AndroidManifest.xml`
25+
*
26+
* Vibration patterns are currently unsupported.
27+
*/
28+
29+
var Vibration = {
30+
vibrate: function(duration: number = 400) {
31+
if (Platform.OS === 'android') {
32+
RCTVibration.vibrate(duration);
33+
} else {
34+
RCTVibration.vibrate();
35+
}
36+
}
37+
};
38+
39+
module.exports = Vibration;

Libraries/Vibration/VibrationIOS.ios.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ var RCTVibration = require('NativeModules').Vibration;
1616
var invariant = require('fbjs/lib/invariant');
1717

1818
/**
19+
* NOTE: `VibrationIOS` is being deprecated. Use `Vibration` instead.
20+
*
1921
* The Vibration API is exposed at `VibrationIOS.vibrate()`. On iOS, calling this
2022
* function will trigger a one second vibration. The vibration is asynchronous
2123
* so this method will return immediately.
@@ -27,6 +29,9 @@ var invariant = require('fbjs/lib/invariant');
2729
*/
2830

2931
var VibrationIOS = {
32+
/**
33+
* @deprecated
34+
*/
3035
vibrate: function() {
3136
invariant(
3237
arguments[0] === undefined,

Libraries/react-native/react-native.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ var ReactNative = {
8686
get StyleSheet() { return require('StyleSheet'); },
8787
get TimePickerAndroid() { return require('TimePickerAndroid'); },
8888
get UIManager() { return require('UIManager'); },
89+
get Vibration() { return require('Vibration'); },
8990
get VibrationIOS() { return require('VibrationIOS'); },
9091

9192
// Plugins

Libraries/react-native/react-native.js.flow

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ var ReactNative = Object.assign(Object.create(require('react')), {
9898
StyleSheet: require('StyleSheet'),
9999
TimePickerAndroid: require('TimePickerAndroid'),
100100
UIManager: require('UIManager'),
101+
Vibration: require('Vibration'),
101102
VibrationIOS: require('VibrationIOS'),
102103

103104
// Plugins
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
include_defs('//ReactAndroid/DEFS')
2+
3+
android_library(
4+
name = 'vibration',
5+
srcs = glob(['**/*.java']),
6+
deps = [
7+
react_native_target('java/com/facebook/react/bridge:bridge'),
8+
react_native_target('java/com/facebook/react/common:common'),
9+
react_native_target('java/com/facebook/react/modules/core:core'),
10+
react_native_dep('libraries/fbcore/src/main/java/com/facebook/common/logging:logging'),
11+
react_native_dep('third-party/java/infer-annotations:infer-annotations'),
12+
react_native_dep('third-party/java/jsr-305:jsr-305'),
13+
],
14+
visibility = [
15+
'PUBLIC',
16+
],
17+
)
18+
19+
project_config(
20+
src_target = ':vibration',
21+
)
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* Copyright (c) 2015-present, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*/
9+
10+
package com.facebook.react.modules.vibration;
11+
12+
import android.content.Context;
13+
import android.os.Vibrator;
14+
15+
import com.facebook.react.bridge.ReactApplicationContext;
16+
import com.facebook.react.bridge.ReactContextBaseJavaModule;
17+
import com.facebook.react.bridge.ReactMethod;
18+
19+
public class VibrationModule extends ReactContextBaseJavaModule {
20+
21+
public VibrationModule(ReactApplicationContext reactContext) {
22+
super(reactContext);
23+
}
24+
25+
@Override
26+
public String getName() {
27+
return "Vibration";
28+
}
29+
30+
@ReactMethod
31+
public void vibrate(int duration) {
32+
Vibrator v = (Vibrator) getReactApplicationContext().getSystemService(Context.VIBRATOR_SERVICE);
33+
if (v != null) {
34+
v.vibrate(duration);
35+
}
36+
}
37+
}

0 commit comments

Comments
 (0)