Skip to content

Commit

Permalink
//xplat/js/react-native-github/packages/react-native/ReactAndroid/src…
Browse files Browse the repository at this point in the history
…/main/java/com/facebook/react/modules/vibration:vibrationAndroid (#43918)

Summary:
Pull Request resolved: #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
  • Loading branch information
andrewdacenko authored and pull[bot] committed Apr 8, 2024
1 parent 34d5e5c commit 9204662
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 73 deletions.
2 changes: 1 addition & 1 deletion packages/react-native/ReactAndroid/api/ReactAndroid.api
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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?
}
}

0 comments on commit 9204662

Please sign in to comment.