diff --git a/packages/react-native/ReactAndroid/api/ReactAndroid.api b/packages/react-native/ReactAndroid/api/ReactAndroid.api index 3d69c3bd0c80b4..c8b1cd648c79f6 100644 --- a/packages/react-native/ReactAndroid/api/ReactAndroid.api +++ b/packages/react-native/ReactAndroid/api/ReactAndroid.api @@ -3519,7 +3519,7 @@ public class com/facebook/react/modules/toast/ToastModule : com/facebook/fbreact public fun showWithGravityAndOffset (Ljava/lang/String;DDDD)V } -public class com/facebook/react/modules/vibration/VibrationModule : com/facebook/fbreact/specs/NativeVibrationSpec { +public final class com/facebook/react/modules/vibration/VibrationModule : com/facebook/fbreact/specs/NativeVibrationSpec { public fun (Lcom/facebook/react/bridge/ReactApplicationContext;)V public fun cancel ()V public fun vibrate (D)V diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/vibration/VibrationModule.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/vibration/VibrationModule.java deleted file mode 100644 index 1ffc35800a5f8c..00000000000000 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/vibration/VibrationModule.java +++ /dev/null @@ -1,72 +0,0 @@ -/* - * Copyright (c) Meta Platforms, Inc. and affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -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; -import com.facebook.react.bridge.ReadableArray; -import com.facebook.react.module.annotations.ReactModule; - -@SuppressLint("MissingPermission") -@ReactModule(name = NativeVibrationSpec.NAME) -public class VibrationModule extends NativeVibrationSpec { - - public VibrationModule(ReactApplicationContext reactContext) { - super(reactContext); - } - - @Override - public void vibrate(double durationDouble) { - int duration = (int) durationDouble; - - Vibrator v = (Vibrator) getReactApplicationContext().getSystemService(Context.VIBRATOR_SERVICE); - 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); - } - } - - @Override - public void vibrateByPattern(ReadableArray pattern, double repeatDouble) { - int repeat = (int) repeatDouble; - - Vibrator v = (Vibrator) getReactApplicationContext().getSystemService(Context.VIBRATOR_SERVICE); - 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); - } - } - - @Override - public void cancel() { - Vibrator v = (Vibrator) getReactApplicationContext().getSystemService(Context.VIBRATOR_SERVICE); - if (v != null) { - v.cancel(); - } - } -} diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/vibration/VibrationModule.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/vibration/VibrationModule.kt new file mode 100644 index 00000000000000..8f91ee9fbe21d8 --- /dev/null +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/vibration/VibrationModule.kt @@ -0,0 +1,64 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +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 android.os.VibratorManager +import com.facebook.fbreact.specs.NativeVibrationSpec +import com.facebook.react.bridge.ReactApplicationContext +import com.facebook.react.bridge.ReadableArray +import com.facebook.react.module.annotations.ReactModule + +@SuppressLint("MissingPermission") +@ReactModule(name = NativeVibrationSpec.NAME) +public class VibrationModule(reactContext: ReactApplicationContext) : + NativeVibrationSpec(reactContext) { + + public override fun vibrate(durationDouble: Double) { + val duration = durationDouble.toInt() + val v = getVibrator() ?: return + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + v.vibrate(VibrationEffect.createOneShot(duration.toLong(), VibrationEffect.DEFAULT_AMPLITUDE)) + } else { + @Suppress("DEPRECATION") v.vibrate(duration.toLong()) + } + } + + public override fun vibrateByPattern(pattern: ReadableArray, repeatDouble: Double) { + val repeat = repeatDouble.toInt() + val v = getVibrator() ?: return + val patternLong = LongArray(pattern.size()) + for (i in 0 until pattern.size()) { + patternLong[i] = pattern.getInt(i).toLong() + } + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + v.vibrate(VibrationEffect.createWaveform(patternLong, repeat)) + } else { + @Suppress("DEPRECATION") v.vibrate(patternLong, repeat) + } + } + + public override fun cancel() { + getVibrator()?.cancel() + } + + private fun getVibrator(): Vibrator? = + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { + val vibratorManager = + getReactApplicationContext().getSystemService(Context.VIBRATOR_MANAGER_SERVICE) + as VibratorManager? + vibratorManager?.defaultVibrator + } else { + @Suppress("DEPRECATION") + getReactApplicationContext().getSystemService(Context.VIBRATOR_SERVICE) as Vibrator? + } +}