Skip to content

Commit

Permalink
Merge pull request #121 from benjamindean/remaster
Browse files Browse the repository at this point in the history
Add vibration presets
  • Loading branch information
benjamindean authored Jan 22, 2025
2 parents 5621497 + 08bb2f6 commit 2fedcd8
Show file tree
Hide file tree
Showing 17 changed files with 584 additions and 81 deletions.
6 changes: 6 additions & 0 deletions vibration/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
### 3.1.0

- Add common vibration patterns for Android and iOS.
- Add `sharpness` parameter for iOS.
- Suppress deprecation warnings for `vibrate` method on Android.

### 3.0.0

- The plugin has been recreated from scratch to align with the latest Flutter and Dart features.
Expand Down
43 changes: 42 additions & 1 deletion vibration/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ A plugin for handling Vibration API on iOS, Android, and web. [API docs.](https:

```yml
dependencies:
vibration: ^3.0.0
vibration: ^3.1.0
```
2. Import package:
Expand Down Expand Up @@ -60,6 +60,16 @@ if (await Vibration.hasCustomVibrationsSupport()) {

### vibrate

#### Method Arguments

- `duration`: Duration of the vibration in milliseconds. Default is 500ms.
- `pattern`: List of integers representing the vibration pattern. Alternates between wait and vibrate durations.
- `repeat`: Index in the pattern at which to repeat, or -1 for no repeat. Default is -1.
- `intensities`: List of integers representing the vibration intensities for each segment in the pattern.
- `amplitude`: Amplitude of the vibration. Range is 1 to 255. Default is -1 (use platform default).
- `sharpness`: Sharpness of the vibration. iOS only. Range is 0.0 to 1.0. Default is 0.5.
- `preset`: Predefined vibration preset. Overrides other parameters if provided.

#### With specific duration (for example, 1 second):

```dart
Expand All @@ -86,6 +96,37 @@ Vibration.vibrate(pattern: [500, 1000, 500, 2000]);
Vibration.vibrate(pattern: [500, 1000, 500, 2000], intensities: [1, 255]);
```

#### With vibration presets:

You can use predefined vibration presets for common use cases.

```dart
Vibration.vibrate(preset: VibrationPreset.alarm);
```

Available presets:

- `VibrationPreset.alarm`
- `VibrationPreset.notification`
- `VibrationPreset.heartbeat`
- `VibrationPreset.singleShortBuzz`
- `VibrationPreset.doubleBuzz`
- `VibrationPreset.tripleBuzz`
- `VibrationPreset.longAlarmBuzz`
- `VibrationPreset.pulseWave`
- `VibrationPreset.progressiveBuzz`
- `VibrationPreset.rhythmicBuzz`
- `VibrationPreset.gentleReminder`
- `VibrationPreset.quickSuccessAlert`
- `VibrationPreset.zigZagAlert`
- `VibrationPreset.softPulse`
- `VibrationPreset.emergencyAlert`
- `VibrationPreset.heartbeatVibration`
- `VibrationPreset.countdownTimerAlert`
- `VibrationPreset.rapidTapFeedback`
- `VibrationPreset.dramaticNotification`
- `VibrationPreset.urgentBuzzWave`

### cancel

Stop ongoing vibration.
Expand Down
6 changes: 6 additions & 0 deletions vibration/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ android {

compileSdk = 35

gradle.projectsEvaluated {
tasks.withType(JavaCompile) {
options.compilerArgs << "-Xlint:deprecation"
}
}

compileOptions {
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class Vibration {
this.vibrator = vibrator;
}

@SuppressWarnings("deprecation")
void vibrate(long duration, int amplitude) {
if (vibrator.hasVibrator()) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
Expand All @@ -34,6 +35,7 @@ void vibrate(long duration, int amplitude) {
}
}

@SuppressWarnings("deprecation")
void vibrate(List<Integer> pattern, int repeat) {
long[] patternLong = new long[pattern.size()];

Expand All @@ -53,6 +55,7 @@ void vibrate(List<Integer> pattern, int repeat) {
}
}

@SuppressWarnings("deprecation")
void vibrate(List<Integer> pattern, int repeat, List<Integer> intensities) {
long[] patternLong = new long[pattern.size()];
int[] intensitiesArray = new int[intensities.size()];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.benjaminabel.vibration;

import android.os.Build;
import androidx.annotation.NonNull;

import java.util.List;

Expand All @@ -16,7 +17,7 @@ class VibrationMethodChannelHandler implements MethodChannel.MethodCallHandler {
}

@Override
public void onMethodCall(MethodCall call, MethodChannel.Result result) {
public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result result) {
switch (call.method) {
case "hasAmplitudeControl":
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
Expand All @@ -34,13 +35,13 @@ public void onMethodCall(MethodCall call, MethodChannel.Result result) {

break;
case "vibrate":
int duration = call.argument("duration");
Integer duration = call.argument("duration");
List<Integer> pattern = call.argument("pattern");
int repeat = call.argument("repeat");
Integer repeat = call.argument("repeat");
List<Integer> intensities = call.argument("intensities");
int amplitude = call.argument("amplitude");
Integer amplitude = call.argument("amplitude");

if (pattern.size() > 0 && intensities.size() > 0) {
if (!pattern.isEmpty() && !intensities.isEmpty()) {
vibration.vibrate(pattern, repeat, intensities);
} else if (pattern.size() > 0) {
vibration.vibrate(pattern, repeat);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,38 @@

import android.content.Context;
import android.os.Vibrator;
import android.os.Build;
import android.os.VibratorManager;

import androidx.annotation.NonNull;

import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;

public class VibrationPlugin implements FlutterPlugin, MethodCallHandler {
public class VibrationPlugin implements FlutterPlugin {
private static final String CHANNEL = "vibration";
private MethodChannel methodChannel;

@SuppressWarnings("deprecation")
public Vibrator getVibrator(@NonNull FlutterPluginBinding flutterPluginBinding) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.S) {
return (Vibrator) flutterPluginBinding.getApplicationContext().getSystemService(Context.VIBRATOR_SERVICE);
} else {
final VibratorManager vibratorManager = (VibratorManager) flutterPluginBinding.getApplicationContext().getSystemService(Context.VIBRATOR_MANAGER_SERVICE);

return vibratorManager.getDefaultVibrator();
}
}

@Override
public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) {
final Vibrator vibrator = (Vibrator) flutterPluginBinding.getApplicationContext().getSystemService(Context.VIBRATOR_SERVICE);
final Vibrator vibrator = this.getVibrator(flutterPluginBinding);
final VibrationMethodChannelHandler methodChannelHandler = new VibrationMethodChannelHandler(new Vibration(vibrator));

this.methodChannel = new MethodChannel(flutterPluginBinding.getBinaryMessenger(), CHANNEL);
this.methodChannel.setMethodCallHandler(methodChannelHandler);
}

@Override
public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
if (call.method.equals("getPlatformVersion")) {
result.success("Android " + android.os.Build.VERSION.RELEASE);
} else {
result.notImplemented();
}
}

@Override
public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
this.methodChannel.setMethodCallHandler(null);
Expand Down
Loading

0 comments on commit 2fedcd8

Please sign in to comment.