2016-03-03 12:08:10 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
*
|
2018-02-17 02:24:55 +00:00
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2016-03-03 12:08:10 +00:00
|
|
|
*
|
|
|
|
* @providesModule Vibration
|
|
|
|
* @flow
|
2017-08-24 03:39:23 +00:00
|
|
|
* @jsdoc
|
2016-03-03 12:08:10 +00:00
|
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var RCTVibration = require('NativeModules').Vibration;
|
|
|
|
var Platform = require('Platform');
|
|
|
|
|
|
|
|
/**
|
2018-01-30 00:10:49 +00:00
|
|
|
* Vibration API
|
2018-02-22 15:04:35 +00:00
|
|
|
*
|
2018-01-30 00:10:49 +00:00
|
|
|
* See https://facebook.github.io/react-native/docs/vibration.html
|
2016-03-03 12:08:10 +00:00
|
|
|
*/
|
|
|
|
|
Ios: complete iOS vibration pattern supports (js)
Summary:
This is a revised follow up version from #8574 ( originally implemented in `objc` )
This PR change the implementation in JS suggested by javache
**motivation**
To supports vibration pattern like android.
The [iOS vibration implementation link](http://stackoverflow.com/questions/12966467/are-there-apis-for-custom-vibrations-in-ios/13047464#13047464) mentioned by skv-headless at https://github.com/facebook/react-native/pull/6061#discussion_r54062592, which will not be accepted by apple since that implementation uses a private API. Thus, I use pure public API `NSTimer` to implement it.
**Note**
Since vibration time on iOS is not configurable, there are slightly differences with android.
for example:
**Android Usage:**
`Vibration.vibrate([0, 500, 200, 500])`
==> V(0.5s) --wait(0.2s)--> V(0.5s)
`Vibration.vibrate([300, 500, 200, 500])`
==> --wait(0.3s)--> V(0.5s) --wait(0.2s)--> V(0.5s)
**iOS Usage:**
if first argument is 0, it will not be included in pattern array.
( vibration
Closes https://github.com/facebook/react-native/pull/9233
Differential Revision: D3775085
Pulled By: javache
fbshipit-source-id: 370495857d5581399de32d2bed1ea1bcce193e9d
2016-08-26 01:27:06 +00:00
|
|
|
var _vibrating: boolean = false;
|
|
|
|
var _id: number = 0; // _id is necessary to prevent race condition.
|
|
|
|
|
|
|
|
function vibrateByPattern(pattern: Array<number>, repeat: boolean = false) {
|
|
|
|
if (_vibrating) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_vibrating = true;
|
|
|
|
if (pattern[0] === 0) {
|
|
|
|
RCTVibration.vibrate();
|
|
|
|
pattern = pattern.slice(1);
|
|
|
|
}
|
|
|
|
if (pattern.length === 0) {
|
|
|
|
_vibrating = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
setTimeout(() => vibrateScheduler(++_id, pattern, repeat, 1), pattern[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
function vibrateScheduler(id, pattern: Array<number>, repeat: boolean, nextIndex: number) {
|
|
|
|
if (!_vibrating || id !== _id) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
RCTVibration.vibrate();
|
|
|
|
if (nextIndex >= pattern.length) {
|
|
|
|
if (repeat) {
|
|
|
|
nextIndex = 0;
|
|
|
|
} else {
|
|
|
|
_vibrating = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2017-10-10 00:37:08 +00:00
|
|
|
setTimeout(() => vibrateScheduler(id, pattern, repeat, nextIndex + 1), pattern[nextIndex]);
|
Ios: complete iOS vibration pattern supports (js)
Summary:
This is a revised follow up version from #8574 ( originally implemented in `objc` )
This PR change the implementation in JS suggested by javache
**motivation**
To supports vibration pattern like android.
The [iOS vibration implementation link](http://stackoverflow.com/questions/12966467/are-there-apis-for-custom-vibrations-in-ios/13047464#13047464) mentioned by skv-headless at https://github.com/facebook/react-native/pull/6061#discussion_r54062592, which will not be accepted by apple since that implementation uses a private API. Thus, I use pure public API `NSTimer` to implement it.
**Note**
Since vibration time on iOS is not configurable, there are slightly differences with android.
for example:
**Android Usage:**
`Vibration.vibrate([0, 500, 200, 500])`
==> V(0.5s) --wait(0.2s)--> V(0.5s)
`Vibration.vibrate([300, 500, 200, 500])`
==> --wait(0.3s)--> V(0.5s) --wait(0.2s)--> V(0.5s)
**iOS Usage:**
if first argument is 0, it will not be included in pattern array.
( vibration
Closes https://github.com/facebook/react-native/pull/9233
Differential Revision: D3775085
Pulled By: javache
fbshipit-source-id: 370495857d5581399de32d2bed1ea1bcce193e9d
2016-08-26 01:27:06 +00:00
|
|
|
}
|
|
|
|
|
2016-03-03 12:08:10 +00:00
|
|
|
var Vibration = {
|
2017-08-24 03:39:23 +00:00
|
|
|
/**
|
|
|
|
* Trigger a vibration with specified `pattern`.
|
2018-02-22 15:04:35 +00:00
|
|
|
*
|
2018-01-30 00:10:49 +00:00
|
|
|
* See https://facebook.github.io/react-native/docs/vibration.html#vibrate
|
2017-08-24 03:39:23 +00:00
|
|
|
*/
|
2016-03-30 04:44:33 +00:00
|
|
|
vibrate: function(pattern: number | Array<number> = 400, repeat: boolean = false) {
|
2016-03-03 12:08:10 +00:00
|
|
|
if (Platform.OS === 'android') {
|
2016-03-30 04:44:33 +00:00
|
|
|
if (typeof pattern === 'number') {
|
|
|
|
RCTVibration.vibrate(pattern);
|
|
|
|
} else if (Array.isArray(pattern)) {
|
|
|
|
RCTVibration.vibrateByPattern(pattern, repeat ? 0 : -1);
|
|
|
|
} else {
|
|
|
|
throw new Error('Vibration pattern should be a number or array');
|
|
|
|
}
|
2016-03-03 12:08:10 +00:00
|
|
|
} else {
|
Ios: complete iOS vibration pattern supports (js)
Summary:
This is a revised follow up version from #8574 ( originally implemented in `objc` )
This PR change the implementation in JS suggested by javache
**motivation**
To supports vibration pattern like android.
The [iOS vibration implementation link](http://stackoverflow.com/questions/12966467/are-there-apis-for-custom-vibrations-in-ios/13047464#13047464) mentioned by skv-headless at https://github.com/facebook/react-native/pull/6061#discussion_r54062592, which will not be accepted by apple since that implementation uses a private API. Thus, I use pure public API `NSTimer` to implement it.
**Note**
Since vibration time on iOS is not configurable, there are slightly differences with android.
for example:
**Android Usage:**
`Vibration.vibrate([0, 500, 200, 500])`
==> V(0.5s) --wait(0.2s)--> V(0.5s)
`Vibration.vibrate([300, 500, 200, 500])`
==> --wait(0.3s)--> V(0.5s) --wait(0.2s)--> V(0.5s)
**iOS Usage:**
if first argument is 0, it will not be included in pattern array.
( vibration
Closes https://github.com/facebook/react-native/pull/9233
Differential Revision: D3775085
Pulled By: javache
fbshipit-source-id: 370495857d5581399de32d2bed1ea1bcce193e9d
2016-08-26 01:27:06 +00:00
|
|
|
if (_vibrating) {
|
|
|
|
return;
|
|
|
|
}
|
2016-03-30 04:44:33 +00:00
|
|
|
if (typeof pattern === 'number') {
|
|
|
|
RCTVibration.vibrate();
|
|
|
|
} else if (Array.isArray(pattern)) {
|
Ios: complete iOS vibration pattern supports (js)
Summary:
This is a revised follow up version from #8574 ( originally implemented in `objc` )
This PR change the implementation in JS suggested by javache
**motivation**
To supports vibration pattern like android.
The [iOS vibration implementation link](http://stackoverflow.com/questions/12966467/are-there-apis-for-custom-vibrations-in-ios/13047464#13047464) mentioned by skv-headless at https://github.com/facebook/react-native/pull/6061#discussion_r54062592, which will not be accepted by apple since that implementation uses a private API. Thus, I use pure public API `NSTimer` to implement it.
**Note**
Since vibration time on iOS is not configurable, there are slightly differences with android.
for example:
**Android Usage:**
`Vibration.vibrate([0, 500, 200, 500])`
==> V(0.5s) --wait(0.2s)--> V(0.5s)
`Vibration.vibrate([300, 500, 200, 500])`
==> --wait(0.3s)--> V(0.5s) --wait(0.2s)--> V(0.5s)
**iOS Usage:**
if first argument is 0, it will not be included in pattern array.
( vibration
Closes https://github.com/facebook/react-native/pull/9233
Differential Revision: D3775085
Pulled By: javache
fbshipit-source-id: 370495857d5581399de32d2bed1ea1bcce193e9d
2016-08-26 01:27:06 +00:00
|
|
|
vibrateByPattern(pattern, repeat);
|
2016-03-30 04:44:33 +00:00
|
|
|
} else {
|
|
|
|
throw new Error('Vibration pattern should be a number or array');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Stop vibration
|
2018-02-22 15:04:35 +00:00
|
|
|
*
|
2018-01-30 00:10:49 +00:00
|
|
|
* See https://facebook.github.io/react-native/docs/vibration.html#cancel
|
2016-03-30 04:44:33 +00:00
|
|
|
*/
|
|
|
|
cancel: function() {
|
|
|
|
if (Platform.OS === 'ios') {
|
Ios: complete iOS vibration pattern supports (js)
Summary:
This is a revised follow up version from #8574 ( originally implemented in `objc` )
This PR change the implementation in JS suggested by javache
**motivation**
To supports vibration pattern like android.
The [iOS vibration implementation link](http://stackoverflow.com/questions/12966467/are-there-apis-for-custom-vibrations-in-ios/13047464#13047464) mentioned by skv-headless at https://github.com/facebook/react-native/pull/6061#discussion_r54062592, which will not be accepted by apple since that implementation uses a private API. Thus, I use pure public API `NSTimer` to implement it.
**Note**
Since vibration time on iOS is not configurable, there are slightly differences with android.
for example:
**Android Usage:**
`Vibration.vibrate([0, 500, 200, 500])`
==> V(0.5s) --wait(0.2s)--> V(0.5s)
`Vibration.vibrate([300, 500, 200, 500])`
==> --wait(0.3s)--> V(0.5s) --wait(0.2s)--> V(0.5s)
**iOS Usage:**
if first argument is 0, it will not be included in pattern array.
( vibration
Closes https://github.com/facebook/react-native/pull/9233
Differential Revision: D3775085
Pulled By: javache
fbshipit-source-id: 370495857d5581399de32d2bed1ea1bcce193e9d
2016-08-26 01:27:06 +00:00
|
|
|
_vibrating = false;
|
2016-03-30 04:44:33 +00:00
|
|
|
} else {
|
|
|
|
RCTVibration.cancel();
|
2016-03-03 12:08:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = Vibration;
|