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

<!-- Help reviewers and the release process by writing your own changelog entry. For an example, see:
https://github.com/facebook/react-native/wiki/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
This commit is contained in:
Anton Bryukhov
2020-07-31 11:23:29 -07:00
committed by Facebook GitHub Bot
parent 9b34aa261c
commit aa1d31ebca
@@ -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);
}
}