|
1 /* |
|
2 * |
|
3 * Licensed to the Apache Software Foundation (ASF) under one |
|
4 * or more contributor license agreements. See the NOTICE file |
|
5 * distributed with this work for additional information |
|
6 * regarding copyright ownership. The ASF licenses this file |
|
7 * to you under the Apache License, Version 2.0 (the |
|
8 * "License"); you may not use this file except in compliance |
|
9 * with the License. You may obtain a copy of the License at |
|
10 * |
|
11 * http://www.apache.org/licenses/LICENSE-2.0 |
|
12 * |
|
13 * Unless required by applicable law or agreed to in writing, |
|
14 * software distributed under the License is distributed on an |
|
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
|
16 * KIND, either express or implied. See the License for the |
|
17 * specific language governing permissions and limitations |
|
18 * under the License. |
|
19 * |
|
20 */ |
|
21 |
|
22 var exec = require('cordova/exec'); |
|
23 |
|
24 /** |
|
25 * Provides access to the vibration mechanism on the device. |
|
26 */ |
|
27 |
|
28 module.exports = { |
|
29 |
|
30 /** |
|
31 * Vibrates the device for a given amount of time or for a given pattern or immediately cancels any ongoing vibrations (depending on the parameter). |
|
32 * |
|
33 * @param {Integer} param The number of milliseconds to vibrate (if 0, cancels vibration) |
|
34 * |
|
35 * |
|
36 * @param {Array of Integer} param Pattern with which to vibrate the device. |
|
37 * Pass in an array of integers that |
|
38 * are the durations for which to |
|
39 * turn on or off the vibrator in |
|
40 * milliseconds. The FIRST value |
|
41 * indicates the |
|
42 * number of milliseconds for which |
|
43 * to keep the vibrator ON before |
|
44 * turning it off. The NEXT value indicates the |
|
45 * number of milliseconds for which |
|
46 * to keep the vibrator OFF before |
|
47 * turning it on. Subsequent values |
|
48 * alternate between durations in |
|
49 * milliseconds to turn the vibrator |
|
50 * off or to turn the vibrator on. |
|
51 * (if empty, cancels vibration) |
|
52 */ |
|
53 vibrate: function(param) { |
|
54 |
|
55 /* Aligning with w3c spec */ |
|
56 |
|
57 //vibrate |
|
58 if ((typeof param == 'number') && param != 0) |
|
59 exec(null, null, "Vibration", "vibrate", [param]); |
|
60 |
|
61 //vibrate with array ( i.e. vibrate([3000]) ) |
|
62 else if ((typeof param == 'object') && param.length == 1) |
|
63 { |
|
64 //cancel if vibrate([0]) |
|
65 if (param[0] == 0) |
|
66 exec(null, null, "Vibration", "cancelVibration", []); |
|
67 |
|
68 //else vibrate |
|
69 else |
|
70 exec(null, null, "Vibration", "vibrate", [param[0]]); |
|
71 } |
|
72 |
|
73 //vibrate with a pattern |
|
74 else if ((typeof param == 'object') && param.length > 1) |
|
75 { |
|
76 var repeat = -1; //no repeat |
|
77 exec(null, null, "Vibration", "vibrateWithPattern", [param, repeat]); |
|
78 } |
|
79 |
|
80 //cancel vibration (param = 0 or []) |
|
81 else |
|
82 exec(null, null, "Vibration", "cancelVibration", []); |
|
83 }, |
|
84 |
|
85 /** |
|
86 * Vibrates the device with a given pattern. |
|
87 * |
|
88 * @param {Array of Integer} pattern Pattern with which to vibrate the device. |
|
89 * Pass in an array of integers that |
|
90 * are the durations for which to |
|
91 * turn on or off the vibrator in |
|
92 * milliseconds. The first value |
|
93 * indicates the number of milliseconds |
|
94 * to wait before turning the vibrator |
|
95 * on. The next value indicates the |
|
96 * number of milliseconds for which |
|
97 * to keep the vibrator on before |
|
98 * turning it off. Subsequent values |
|
99 * alternate between durations in |
|
100 * milliseconds to turn the vibrator |
|
101 * off or to turn the vibrator on. |
|
102 * |
|
103 * @param {Integer} repeat Optional index into the pattern array at which |
|
104 * to start repeating (will repeat until canceled), |
|
105 * or -1 for no repetition (default). |
|
106 */ |
|
107 vibrateWithPattern: function(pattern, repeat) { |
|
108 repeat = (typeof repeat !== "undefined") ? repeat : -1; |
|
109 pattern.unshift(0); //add a 0 at beginning for backwards compatibility from w3c spec |
|
110 exec(null, null, "Vibration", "vibrateWithPattern", [pattern, repeat]); |
|
111 }, |
|
112 |
|
113 /** |
|
114 * Immediately cancels any currently running vibration. |
|
115 */ |
|
116 cancelVibration: function() { |
|
117 exec(null, null, "Vibration", "cancelVibration", []); |
|
118 } |
|
119 }; |