From f89c5098bec5ba1a3b4bee54b51d639beff559f6 Mon Sep 17 00:00:00 2001 From: Bruno Barbieri Date: Tue, 7 Apr 2020 18:23:10 -0700 Subject: [PATCH] Make Vibration.vibrate compatible with TurboModules (#27951) Summary: This PR fixes a compatibility issue with the Vibration module and TurboModules. The TurboModules spec doesn't allow nullable arguments of type Number, causing the following problem: ![IMG_3758](https://user-images.githubusercontent.com/1247834/73803879-10be6f80-4790-11ea-92d4-a008f0007681.PNG) [iOS] [Fixed] - Make Vibration library compatible with TurboModules. Pull Request resolved: https://github.com/facebook/react-native/pull/27951 Test Plan: Just submitted a PR to my own app to fix the issue [here](https://github.com/rainbow-me/rainbow/pull/340) The problem should be reproducible on RNTester due to this line: https://github.com/facebook/react-native/blob/91f139b94118fe8db29728ea8ad855fc4a13f743/RNTester/js/examples/Vibration/VibrationExample.js#L66 and should be working on this branch. Reviewed By: TheSavior Differential Revision: D19761064 Pulled By: hramos fbshipit-source-id: 84f6b62a2734cc09d450e906b5866d4e9ce61124 --- .../FBReactNativeSpec/FBReactNativeSpec.h | 2 +- Libraries/Vibration/NativeVibration.js | 2 +- Libraries/Vibration/RCTVibration.mm | 2 +- Libraries/Vibration/Vibration.js | 9 +++++---- .../com/facebook/fbreact/specs/NativeVibrationSpec.java | 2 +- .../react/modules/vibration/VibrationModule.java | 4 +++- 6 files changed, 12 insertions(+), 9 deletions(-) diff --git a/Libraries/FBReactNativeSpec/FBReactNativeSpec/FBReactNativeSpec.h b/Libraries/FBReactNativeSpec/FBReactNativeSpec/FBReactNativeSpec.h index bb1c8d2d99a67f..873ce233db451a 100644 --- a/Libraries/FBReactNativeSpec/FBReactNativeSpec/FBReactNativeSpec.h +++ b/Libraries/FBReactNativeSpec/FBReactNativeSpec/FBReactNativeSpec.h @@ -2785,7 +2785,7 @@ namespace facebook { } // namespace facebook @protocol NativeVibrationSpec -- (void)vibrate:(NSNumber *)pattern; +- (void)vibrate:(double)pattern; - (void)vibrateByPattern:(NSArray *)pattern repeat:(double)repeat; - (void)cancel; diff --git a/Libraries/Vibration/NativeVibration.js b/Libraries/Vibration/NativeVibration.js index abdfa680c114cc..6de5352ec82f8e 100644 --- a/Libraries/Vibration/NativeVibration.js +++ b/Libraries/Vibration/NativeVibration.js @@ -15,7 +15,7 @@ import * as TurboModuleRegistry from '../TurboModule/TurboModuleRegistry'; export interface Spec extends TurboModule { +getConstants: () => {||}; - +vibrate: (pattern?: ?number) => void; + +vibrate: (pattern: number) => void; // Android only +vibrateByPattern: (pattern: Array, repeat: number) => void; diff --git a/Libraries/Vibration/RCTVibration.mm b/Libraries/Vibration/RCTVibration.mm index a26424693d71a0..f78599df385774 100644 --- a/Libraries/Vibration/RCTVibration.mm +++ b/Libraries/Vibration/RCTVibration.mm @@ -25,7 +25,7 @@ - (void)vibrate AudioServicesPlaySystemSound(kSystemSoundID_Vibrate); } -RCT_EXPORT_METHOD(vibrate:(NSNumber *)pattern) +RCT_EXPORT_METHOD(vibrate:(double)pattern) { [self vibrate]; } diff --git a/Libraries/Vibration/Vibration.js b/Libraries/Vibration/Vibration.js index 2f20cd7ae6061e..6165258ddfffa6 100644 --- a/Libraries/Vibration/Vibration.js +++ b/Libraries/Vibration/Vibration.js @@ -22,6 +22,7 @@ const Platform = require('../Utilities/Platform'); let _vibrating: boolean = false; let _id: number = 0; // _id is necessary to prevent race condition. +const _default_vibration_length = 400; function vibrateByPattern(pattern: Array, repeat: boolean = false) { if (_vibrating) { @@ -29,7 +30,7 @@ function vibrateByPattern(pattern: Array, repeat: boolean = false) { } _vibrating = true; if (pattern[0] === 0) { - NativeVibration.vibrate(); + NativeVibration.vibrate(_default_vibration_length); pattern = pattern.slice(1); } if (pattern.length === 0) { @@ -48,7 +49,7 @@ function vibrateScheduler( if (!_vibrating || id !== _id) { return; } - NativeVibration.vibrate(); + NativeVibration.vibrate(_default_vibration_length); if (nextIndex >= pattern.length) { if (repeat) { nextIndex = 0; @@ -70,7 +71,7 @@ const Vibration = { * See https://facebook.github.io/react-native/docs/vibration.html#vibrate */ vibrate: function( - pattern: number | Array = 400, + pattern: number | Array = _default_vibration_length, repeat: boolean = false, ) { if (Platform.OS === 'android') { @@ -86,7 +87,7 @@ const Vibration = { return; } if (typeof pattern === 'number') { - NativeVibration.vibrate(); + NativeVibration.vibrate(pattern); } else if (Array.isArray(pattern)) { vibrateByPattern(pattern, repeat); } else { diff --git a/ReactAndroid/src/main/java/com/facebook/fbreact/specs/NativeVibrationSpec.java b/ReactAndroid/src/main/java/com/facebook/fbreact/specs/NativeVibrationSpec.java index 4214a3c30fed47..abd9fbbb5022dd 100644 --- a/ReactAndroid/src/main/java/com/facebook/fbreact/specs/NativeVibrationSpec.java +++ b/ReactAndroid/src/main/java/com/facebook/fbreact/specs/NativeVibrationSpec.java @@ -31,5 +31,5 @@ public NativeVibrationSpec(ReactApplicationContext reactContext) { public abstract void vibrateByPattern(ReadableArray pattern, double repeat); @ReactMethod - public abstract void vibrate(Double pattern); + public abstract void vibrate(double pattern); } diff --git a/ReactAndroid/src/main/java/com/facebook/react/modules/vibration/VibrationModule.java b/ReactAndroid/src/main/java/com/facebook/react/modules/vibration/VibrationModule.java index 5f8eb0c1ed8037..1417cc3c7bb6d9 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/modules/vibration/VibrationModule.java +++ b/ReactAndroid/src/main/java/com/facebook/react/modules/vibration/VibrationModule.java @@ -32,7 +32,9 @@ public String getName() { } @ReactMethod - public void vibrate(int duration) { + public void vibrate(double durationDouble) { + int duration = (int) durationDouble; + Vibrator v = (Vibrator) getReactApplicationContext().getSystemService(Context.VIBRATOR_SERVICE); if (v != null) { v.vibrate(duration);