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