mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
//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 <DIFF>`. 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 fbshipit-source-id: f0960e42cf248ea78613299c16c6a47aa6169c7a
This commit is contained in:
committed by
Facebook GitHub Bot
parent
49ea2a43c2
commit
dc8b7f8322
@@ -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 <init> (Lcom/facebook/react/bridge/ReactApplicationContext;)V
|
||||
public fun cancel ()V
|
||||
public fun vibrate (D)V
|
||||
|
||||
-72
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
+64
@@ -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?
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user