From e902761a27efb0a0c9e15e693723f09e9a5c0b5d Mon Sep 17 00:00:00 2001 From: Andrew Datsenko Date: Fri, 5 Apr 2024 08:41:55 -0700 Subject: [PATCH] //xplat/js/react-native-github/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/vibration:vibrationAndroid (#43918) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/43918 Changelog: [Internal] _____ ## Why? We recommend to use Kotlin for any new code and are actively migrating Java code to Kotlin. This codemod service attempts to migrate existing Java code to Kotlin. ## How was this diff generated? This codemod service scans through qualified paths and looks for Java modules. Then it runs `kotlinator.sh` on each module, which generated this diff. ## What if I see problems in this diff? We recommend commandeering and fixing the diff. If you reject or abandon the diff, the codemod service will regenerate it in a few days - Script for easily commandeer & open diff: In fbandroid, `scripts/commandeer_and_checkout.sh `. It not only commandeer the diff, but also rebase & open diff in Android Studio. - Report repeating issues in [Kotlinator Papercut](https://fburl.com/papercuts/1g4f4qas) See more useful tips & scripts in [Kotlin Auto-Conversion Codemod Wiki](https://fburl.com/wiki/c68ka0pu) _____ ## Questions / Comments / Feedback? **Your feedback is important to us! Give feedback about this diff by clicking the "Provide Feedback" button below.** * Returning back to author or abandoning this diff will only cause the diff to be regenerated in the future. * Do **NOT** post in the CodemodService Feedback group about this specific diff. _____ ## Codemod Metadata NOTE: You won't need to read this section to review this diff. https://www.internalfb.com/intern/sandcastle/job/1239558926/ |Oncall|[kotlin_in_fb4a](https://our.intern.facebook.com/intern/oncall3/?shortname=kotlin_in_fb4a)| |CodemodConfig|[fbsource/kotlinator.json](https://www.internalfb.com/codemod_service/fbsource%2Fkotlinator.json)| |ConfigType|configerator| Rules run: - CodemodTransformerFBSourceScript This diff was created with [CodemodService](https://fburl.com/CodemodService). Reviewed By: javache Differential Revision: D55725326 --- .../ReactAndroid/api/ReactAndroid.api | 2 +- .../modules/vibration/VibrationModule.java | 72 ------------------- .../modules/vibration/VibrationModule.kt | 64 +++++++++++++++++ 3 files changed, 65 insertions(+), 73 deletions(-) delete mode 100644 packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/vibration/VibrationModule.java create mode 100644 packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/vibration/VibrationModule.kt 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? + } +}