Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 19 additions & 9 deletions Libraries/Vibration/VibrationIOS.android.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Copy link
Contributor

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 RCT prefix in JS, it's an Obj-C convention we're still using in JS and we should move away from using it in JS.


/**
* This exposes the native VibrationAndroid module as a JS module. This has a function 'vibrate'
* which takes the following parameters:
Copy link
Contributor

Choose a reason for hiding this comment

The 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 VibrationAndroid. Example: https://github.com/facebook/react-native/blob/master/Libraries/Vibration/VibrationIOS.ios.js

*
* 1. int duration: The duration of the vibration in milliseconds.
*/

var VibrationAndroid = {

vibrate: function (
duration: number
Copy link
Contributor

Choose a reason for hiding this comment

The 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;
1 change: 1 addition & 0 deletions Libraries/react-native/react-native.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ var ReactNative = Object.assign(Object.create(require('React')), {
Settings: require('Settings'),
StatusBarIOS: require('StatusBarIOS'),
StyleSheet: require('StyleSheet'),
VibrationAndroid: require('VibrationAndroid'),
VibrationIOS: require('VibrationIOS'),

// Plugins
Expand Down
1 change: 1 addition & 0 deletions ReactAndroid/src/main/AndroidManifest.xml
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"/>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't add the permission directly to ReactAndroid's manifest but rather document that using the vibration module requires people to add this permission to their app's manifest.


<application />

Expand Down
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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please check for getSystemService() returning null. We shouldn't crash if it does.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point!

v.vibrate(duration);
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to me 👍

Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.facebook.react.modules.network.NetworkingModule;
import com.facebook.react.modules.storage.AsyncStorageModule;
import com.facebook.react.modules.toast.ToastModule;
import com.facebook.react.modules.vibration.VibrationModule;
import com.facebook.react.uimanager.ViewManager;
import com.facebook.react.views.drawer.ReactDrawerLayoutManager;
import com.facebook.react.views.image.ReactImageManager;
Expand All @@ -47,6 +48,8 @@ public List<NativeModule> createNativeModules(ReactApplicationContext reactConte
new FrescoModule(reactContext),
new NetworkingModule(reactContext),
new ToastModule(reactContext));
new VibrationModule(reactContext));

}

@Override
Expand Down