-
Notifications
You must be signed in to change notification settings - Fork 25k
Add vibration support for android #2794
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 |
|---|---|---|
|
|
@@ -6,18 +6,28 @@ | |
| * 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. | ||
| * | ||
| * Stub of VibrationIOS for Android. | ||
| * | ||
| * @providesModule VibrationIOS | ||
| * @providesModule VibrationAndroid | ||
| */ | ||
|
|
||
| 'use strict'; | ||
|
|
||
| var warning = require('warning'); | ||
| var RCTVibrationAndroid = require('NativeModules').VibrationAndroid; | ||
|
|
||
| /** | ||
| * This exposes the native VibrationAndroid module as a JS module. This has a function 'vibrate' | ||
| * which takes the following parameters: | ||
|
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. Can you write the documentation more from a point of a JS developer? They probably won't mind this calls the native |
||
| * | ||
| * 1. int duration: The duration of the vibration in milliseconds. | ||
| */ | ||
|
|
||
| var VibrationAndroid = { | ||
|
|
||
| vibrate: function ( | ||
| duration: number | ||
|
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. VibrationIOS.vibrate doesn't take an argument so you need to make sure this works also when no argument is passed so that we are consistent between iOS and Android. |
||
| ): void { | ||
| RCTVibrationAndroid.vibrate(duration); | ||
| }, | ||
|
|
||
| var VibrationIOS = { | ||
| vibrate: function() { | ||
| warning('VibrationIOS is not supported on this platform!'); | ||
| } | ||
| }; | ||
|
|
||
| module.exports = VibrationIOS; | ||
| module.exports = VibrationAndroid; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.facebook.react"> | ||
|
|
||
| <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/> | ||
| <uses-permission android:name="android.permission.VIBRATE"/> | ||
|
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 don't add the permission directly to |
||
|
|
||
| <application /> | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| /** | ||
| * 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.os.Vibrator; | ||
| import android.content.Context; | ||
|
|
||
| import com.facebook.react.bridge.NativeModule; | ||
| import com.facebook.react.bridge.ReactApplicationContext; | ||
| import com.facebook.react.bridge.ReactContext; | ||
| import com.facebook.react.bridge.ReactContextBaseJavaModule; | ||
| import com.facebook.react.bridge.ReactMethod; | ||
| import com.facebook.react.common.MapBuilder; | ||
|
|
||
| public class VibrationModule extends ReactContextBaseJavaModule { | ||
|
|
||
| ReactApplicationContext reactContext; | ||
|
|
||
| public VibrationModule(ReactApplicationContext reactContext) { | ||
| super(reactContext); | ||
| this.reactContext = reactContext; | ||
| } | ||
|
|
||
| @Override | ||
| public String getName() { | ||
| return "VibrationAndroid"; | ||
| } | ||
|
|
||
| @ReactMethod | ||
| public void vibrate(int duration) { | ||
| Vibrator v = (Vibrator) reactContext.getSystemService(Context.VIBRATOR_SERVICE); | ||
|
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 check for
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. Good point! |
||
| v.vibrate(duration); | ||
| } | ||
| } | ||
|
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. This looks good to me 👍 |
||
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.
Please don't use the
RCTprefix in JS, it's an Obj-C convention we're still using in JS and we should move away from using it in JS.