-
Notifications
You must be signed in to change notification settings - Fork 25k
[Android] vibration module #6061
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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, | ||
| }, | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: e.g.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sorry didn't understand you 😞 |
||
| * | ||
| * 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(); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it is not that easy to make vibration duration in ios. I would rather make separate pr for that.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @nicklockwood http://stackoverflow.com/a/13047464/1410905 if it is good solution I can make ios vibration duration here. |
||
| } | ||
| } | ||
| }; | ||
|
|
||
| module.exports = Vibration; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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'); }, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please also update
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
| get VibrationIOS() { return require('VibrationIOS'); }, | ||
|
|
||
| // Plugins | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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', | ||
| ) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should add the default license header stuff please.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
|
|
||
| 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); | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
property
VibrationProperty not found in Object.createThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
property
VibrationProperty not found in Object.create