Skip to content

Commit

Permalink
Extended vibrateWithPattern to allow for pattern repetition, implemen…
Browse files Browse the repository at this point in the history
…ted a complementary cancelVibration function and adapted documentation.
  • Loading branch information
dematerializer authored and stevengill committed Jun 4, 2014
1 parent e731cbb commit 78f95e1
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 11 deletions.
19 changes: 14 additions & 5 deletions doc/index.md
Expand Up @@ -29,12 +29,13 @@ This plugin provides a way to vibrate the device.

navigator.notification.vibrate
- Amazon Fire OS
- Android
- BlackBerry 10
- Firefox OS
- iOS
- Windows Phone 7 and 8

navigator.notification.vibrate and navigator.notification.vibrateWithPattern
navigator.notification.vibrateWithPattern,<br />navigator.notification.cancelVibration
- Android

## notification.vibrate
Expand All @@ -61,18 +62,26 @@ Vibrates the device for a given amount of time.

Vibrates the device with a given pattern.

navigator.notification.vibrateWithPattern(pattern)
navigator.notification.vibrateWithPattern(pattern, repeat)

- __pattern__: Sequence of durations (in milliseconds) for which to turn on or off the vibrator. _(Array of Numbers)_
- __repeat__: Optional index into the pattern array at which to start repeating (will repeat until canceled), or -1 for no repetition (default). _(Number)_

### Example

// Immediately start vibrating
// vibrate for 200ms,
// vibrate for 100ms,
// wait for 100ms,
// vibrate for 200ms,
// wait for 100ms,
// vibrate for 400ms,
// wait for 100ms,
// vibrate for 800ms
navigator.notification.vibrateWithPattern([0, 200, 100, 200, 100, 400, 100, 800]);
// vibrate for 800ms,
// (do not repeat)
navigator.notification.vibrateWithPattern([0, 100, 100, 200, 100, 400, 100, 800]);

## notification.cancelVibration

Immediately cancels any currently running vibration.

navigator.notification.cancelVibration()
24 changes: 20 additions & 4 deletions src/android/Vibration.java
Expand Up @@ -49,19 +49,24 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo
this.vibrate(args.getLong(0));
}
else if (action.equals("vibrateWithPattern")) {
JSONArray pattern = args;
JSONArray pattern = args.getJSONArray(0);
int repeat = args.getInt(1);
long[] patternArray = new long[pattern.length()];
for (int i = 0; i < pattern.length(); i++) {
patternArray[i] = pattern.getLong(i);
}
this.vibrateWithPattern(patternArray);
this.vibrateWithPattern(patternArray, repeat);
}
else if (action.equals("cancelVibration")) {
this.cancelVibration();
}
else {
return false;
}

// Only alert and confirm are async.
callbackContext.success();

return true;
}

Expand Down Expand Up @@ -100,9 +105,20 @@ public void vibrate(long time) {
* alternate between durations in
* milliseconds to turn the vibrator
* off or to turn the vibrator on.
*
* @param repeat Optional index into the pattern array at which
* to start repeating, or -1 for no repetition (default).
*/
public void vibrateWithPattern(long[] pattern, int repeat) {
Vibrator vibrator = (Vibrator) this.cordova.getActivity().getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(pattern, repeat);
}

/**
* Immediately cancels any currently running vibration.
*/
public void vibrateWithPattern(long[] pattern) {
public void cancelVibration() {
Vibrator vibrator = (Vibrator) this.cordova.getActivity().getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(pattern, -1);
vibrator.cancel();
}
}
16 changes: 14 additions & 2 deletions www/vibration.js
Expand Up @@ -53,8 +53,20 @@ module.exports = {
* alternate between durations in
* milliseconds to turn the vibrator
* off or to turn the vibrator on.
*
* @param {Integer} repeat Optional index into the pattern array at which
* to start repeating (will repeat until canceled),
* or -1 for no repetition (default).
*/
vibrateWithPattern: function(pattern, repeat) {
repeat = (typeof repeat !== "undefined") ? repeat : -1;
exec(null, null, "Vibration", "vibrateWithPattern", [pattern, repeat]);
},

/**
* Immediately cancels any currently running vibration.
*/
vibrateWithPattern: function(pattern) {
exec(null, null, "Vibration", "vibrateWithPattern", pattern);
cancelVibration: function() {
exec(null, null, "Vibration", "cancelVibration", []);
},
};

0 comments on commit 78f95e1

Please sign in to comment.