/*
 * Copyright (c) Facebook, Inc. and its 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.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 = VibrationModule.NAME)
public class VibrationModule extends NativeVibrationSpec {

  public static final String NAME = "Vibration";

  public VibrationModule(ReactApplicationContext reactContext) {
    super(reactContext);
  }

  @Override
  public String getName() {
    return NAME;
  }

  @Override
  public void vibrate(double durationDouble) {
    int duration = (int) durationDouble;

    Vibrator v = (Vibrator) getReactApplicationContext().getSystemService(Context.VIBRATOR_SERVICE);
    if (v != null) {
      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) {
      long[] patternLong = new long[pattern.size()];
      for (int i = 0; i < pattern.size(); i++) {
        patternLong[i] = pattern.getInt(i);
      }
      v.vibrate(patternLong, repeat);
    }
  }

  @Override
  public void cancel() {
    Vibrator v = (Vibrator) getReactApplicationContext().getSystemService(Context.VIBRATOR_SERVICE);
    if (v != null) {
      v.cancel();
    }
  }
}

@@ -297,24 +297,84 @@
 nt.Context;%0A
+import android.os.Build;%0Aimport android.os.VibrationEffect;%0A
 import andro
@@ -389,16 +389,16 @@
 brator;%0A
-
 import c
@@ -1157,33 +1157,33 @@
 ICE);%0A    if (v 
-!
+=
 = null) %7B%0A      
@@ -1168,32 +1168,217 @@
 f (v == null) %7B%0A
+      return;%0A    %7D%0A%0A    if (Build.VERSION.SDK_INT %3E= Build.VERSION_CODES.O) %7B%0A      v.vibrate(VibrationEffect.createOneShot(duration, VibrationEffect.DEFAULT_AMPLITUDE));%0A    %7D else %7B%0A
       v.vibrate(
@@ -1629,33 +1629,33 @@
 ICE);%0A    if (v 
-!
+=
 = null) %7B%0A      
@@ -1646,24 +1646,43 @@
 = null) %7B%0A  
+    return;%0A    %7D%0A%0A
     long%5B%5D p
@@ -1724,18 +1724,16 @@
 )%5D;%0A    
-  
 for (int
@@ -1773,18 +1773,16 @@
 %7B%0A      
-  
 patternL
@@ -1813,19 +1813,159 @@
 i);%0A    
-  %7D
+%7D%0A%0A    if (Build.VERSION.SDK_INT %3E= Build.VERSION_CODES.O) %7B%0A      v.vibrate(VibrationEffect.createWaveform(patternLong, repeat));%0A    %7D else %7B
 %0A      v

Use recent Vibrator Android API (#29534)