From aa1d31ebca778e5d1074bf3de07cbd99eadf02d7 Mon Sep 17 00:00:00 2001 From: Anton Bryukhov Date: Fri, 31 Jul 2020 11:23:29 -0700 Subject: [PATCH] Use recent Vibrator Android API (#29534) Summary: Android's `VibrationModule` uses deprecated `vibrate(long milliseconds)` and `vibrate(long[] pattern, int repeat)` methods. Deprecation notes: [[1]](https://developer.android.com/reference/android/os/Vibrator#vibrate(long)) [[2]](https://developer.android.com/reference/android/os/Vibrator#vibrate(long[],%20int)). Changes in this pull request use recent `Vibrator` API for devices with API Level >= 26 (since mentioned methods were depreceted in API Level 26). ## Changelog [Android] [Internal] - Use non-deprecated `Vibrator` API in `VibrationModule` Pull Request resolved: https://github.com/facebook/react-native/pull/29534 Test Plan: API is the same as before, but it uses recent `Vibrator` API. Reviewed By: makovkastar Differential Revision: D22857382 Pulled By: mdvacca fbshipit-source-id: 6793a7d165fa73d81064865861ed55af2de83d52 --- .../modules/vibration/VibrationModule.java | 27 ++++++++++++++----- 1 file changed, 21 insertions(+), 6 deletions(-) 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 909ced4fc61..b911b7b0b50 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 @@ -9,6 +9,8 @@ package com.facebook.react.modules.vibration; import android.annotation.SuppressLint; import android.content.Context; +import android.os.Build; +import android.os.VibrationEffect; import android.os.Vibrator; import com.facebook.fbreact.specs.NativeVibrationSpec; import com.facebook.react.bridge.ReactApplicationContext; @@ -35,7 +37,13 @@ public class VibrationModule extends NativeVibrationSpec { int duration = (int) durationDouble; Vibrator v = (Vibrator) getReactApplicationContext().getSystemService(Context.VIBRATOR_SERVICE); - if (v != null) { + if (v == null) { + return; + } + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + v.vibrate(VibrationEffect.createOneShot(duration, VibrationEffect.DEFAULT_AMPLITUDE)); + } else { v.vibrate(duration); } } @@ -45,11 +53,18 @@ public class VibrationModule extends NativeVibrationSpec { int repeat = (int) repeatDouble; Vibrator v = (Vibrator) getReactApplicationContext().getSystemService(Context.VIBRATOR_SERVICE); - if (v != null) { - long[] patternLong = new long[pattern.size()]; - for (int i = 0; i < pattern.size(); i++) { - patternLong[i] = pattern.getInt(i); - } + if (v == null) { + return; + } + + long[] patternLong = new long[pattern.size()]; + for (int i = 0; i < pattern.size(); i++) { + patternLong[i] = pattern.getInt(i); + } + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + v.vibrate(VibrationEffect.createWaveform(patternLong, repeat)); + } else { v.vibrate(patternLong, repeat); } }