add flutter_vibrate
This commit is contained in:
@@ -0,0 +1,2 @@
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
</manifest>
|
||||
+83
@@ -0,0 +1,83 @@
|
||||
package flutter.plugins.vibrate;
|
||||
|
||||
import android.os.Build;
|
||||
import android.os.Vibrator;
|
||||
import android.os.VibrationEffect;
|
||||
import android.view.HapticFeedbackConstants;
|
||||
|
||||
import io.flutter.plugin.common.MethodCall;
|
||||
import io.flutter.plugin.common.MethodChannel;
|
||||
|
||||
class VibrateMethodCallHandler implements MethodChannel.MethodCallHandler {
|
||||
private final Vibrator vibrator;
|
||||
private final boolean hasVibrator;
|
||||
private final boolean legacyVibrator;
|
||||
|
||||
VibrateMethodCallHandler(Vibrator vibrator) {
|
||||
assert (vibrator != null);
|
||||
this.vibrator = vibrator;
|
||||
this.hasVibrator = vibrator.hasVibrator();
|
||||
this.legacyVibrator = Build.VERSION.SDK_INT < 26;
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
private void vibrate(int duration) {
|
||||
if (hasVibrator) {
|
||||
if (legacyVibrator) {
|
||||
vibrator.vibrate(duration);
|
||||
} else {
|
||||
vibrator.vibrate(VibrationEffect.createOneShot(duration, VibrationEffect.DEFAULT_AMPLITUDE));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onMethodCall(MethodCall call, MethodChannel.Result result) {
|
||||
switch (call.method) {
|
||||
case "canVibrate":
|
||||
result.success(hasVibrator);
|
||||
break;
|
||||
case "vibrate":
|
||||
final int duration = call.argument("duration");
|
||||
vibrate(duration);
|
||||
result.success(null);
|
||||
break;
|
||||
case "impact":
|
||||
vibrate(HapticFeedbackConstants.VIRTUAL_KEY);
|
||||
result.success(null);
|
||||
break;
|
||||
case "selection":
|
||||
vibrate(HapticFeedbackConstants.KEYBOARD_TAP);
|
||||
result.success(null);
|
||||
break;
|
||||
case "success":
|
||||
vibrate(50);
|
||||
result.success(null);
|
||||
break;
|
||||
case "warning":
|
||||
vibrate(250);
|
||||
result.success(null);
|
||||
break;
|
||||
case "error":
|
||||
vibrate(500);
|
||||
result.success(null);
|
||||
break;
|
||||
case "heavy":
|
||||
vibrate(100);
|
||||
result.success(null);
|
||||
break;
|
||||
case "medium":
|
||||
vibrate(40);
|
||||
result.success(null);
|
||||
break;
|
||||
case "light":
|
||||
vibrate(10);
|
||||
result.success(null);
|
||||
break;
|
||||
default:
|
||||
result.notImplemented();
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
package flutter.plugins.vibrate;
|
||||
|
||||
import android.content.Context;
|
||||
import android.os.Vibrator;
|
||||
|
||||
import io.flutter.embedding.engine.plugins.FlutterPlugin;
|
||||
import io.flutter.plugin.common.BinaryMessenger;
|
||||
import io.flutter.plugin.common.MethodChannel;
|
||||
|
||||
public class VibratePlugin implements FlutterPlugin {
|
||||
private MethodChannel methodChannel;
|
||||
|
||||
@Override
|
||||
public void onAttachedToEngine(FlutterPluginBinding binding) {
|
||||
final Context context = binding.getApplicationContext();
|
||||
final BinaryMessenger messenger = binding.getBinaryMessenger();
|
||||
final Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
|
||||
final VibrateMethodCallHandler methodCallHandler = new VibrateMethodCallHandler(vibrator);
|
||||
|
||||
this.methodChannel = new MethodChannel(messenger, "vibrate");
|
||||
this.methodChannel.setMethodCallHandler(methodCallHandler);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDetachedFromEngine(FlutterPluginBinding binding) {
|
||||
this.methodChannel.setMethodCallHandler(null);
|
||||
this.methodChannel = null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user