1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/Touchgui/plugins/org.apache.cordova.vibration/www/vibration.js Thu Jun 04 14:50:33 2015 +0200 1.3 @@ -0,0 +1,119 @@ 1.4 +/* 1.5 + * 1.6 + * Licensed to the Apache Software Foundation (ASF) under one 1.7 + * or more contributor license agreements. See the NOTICE file 1.8 + * distributed with this work for additional information 1.9 + * regarding copyright ownership. The ASF licenses this file 1.10 + * to you under the Apache License, Version 2.0 (the 1.11 + * "License"); you may not use this file except in compliance 1.12 + * with the License. You may obtain a copy of the License at 1.13 + * 1.14 + * http://www.apache.org/licenses/LICENSE-2.0 1.15 + * 1.16 + * Unless required by applicable law or agreed to in writing, 1.17 + * software distributed under the License is distributed on an 1.18 + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 1.19 + * KIND, either express or implied. See the License for the 1.20 + * specific language governing permissions and limitations 1.21 + * under the License. 1.22 + * 1.23 +*/ 1.24 + 1.25 +var exec = require('cordova/exec'); 1.26 + 1.27 +/** 1.28 + * Provides access to the vibration mechanism on the device. 1.29 + */ 1.30 + 1.31 +module.exports = { 1.32 + 1.33 + /** 1.34 + * Vibrates the device for a given amount of time or for a given pattern or immediately cancels any ongoing vibrations (depending on the parameter). 1.35 + * 1.36 + * @param {Integer} param The number of milliseconds to vibrate (if 0, cancels vibration) 1.37 + * 1.38 + * 1.39 + * @param {Array of Integer} param Pattern with which to vibrate the device. 1.40 + * Pass in an array of integers that 1.41 + * are the durations for which to 1.42 + * turn on or off the vibrator in 1.43 + * milliseconds. The FIRST value 1.44 + * indicates the 1.45 + * number of milliseconds for which 1.46 + * to keep the vibrator ON before 1.47 + * turning it off. The NEXT value indicates the 1.48 + * number of milliseconds for which 1.49 + * to keep the vibrator OFF before 1.50 + * turning it on. Subsequent values 1.51 + * alternate between durations in 1.52 + * milliseconds to turn the vibrator 1.53 + * off or to turn the vibrator on. 1.54 + * (if empty, cancels vibration) 1.55 + */ 1.56 + vibrate: function(param) { 1.57 + 1.58 + /* Aligning with w3c spec */ 1.59 + 1.60 + //vibrate 1.61 + if ((typeof param == 'number') && param != 0) 1.62 + exec(null, null, "Vibration", "vibrate", [param]); 1.63 + 1.64 + //vibrate with array ( i.e. vibrate([3000]) ) 1.65 + else if ((typeof param == 'object') && param.length == 1) 1.66 + { 1.67 + //cancel if vibrate([0]) 1.68 + if (param[0] == 0) 1.69 + exec(null, null, "Vibration", "cancelVibration", []); 1.70 + 1.71 + //else vibrate 1.72 + else 1.73 + exec(null, null, "Vibration", "vibrate", [param[0]]); 1.74 + } 1.75 + 1.76 + //vibrate with a pattern 1.77 + else if ((typeof param == 'object') && param.length > 1) 1.78 + { 1.79 + var repeat = -1; //no repeat 1.80 + exec(null, null, "Vibration", "vibrateWithPattern", [param, repeat]); 1.81 + } 1.82 + 1.83 + //cancel vibration (param = 0 or []) 1.84 + else 1.85 + exec(null, null, "Vibration", "cancelVibration", []); 1.86 + }, 1.87 + 1.88 + /** 1.89 + * Vibrates the device with a given pattern. 1.90 + * 1.91 + * @param {Array of Integer} pattern Pattern with which to vibrate the device. 1.92 + * Pass in an array of integers that 1.93 + * are the durations for which to 1.94 + * turn on or off the vibrator in 1.95 + * milliseconds. The first value 1.96 + * indicates the number of milliseconds 1.97 + * to wait before turning the vibrator 1.98 + * on. The next value indicates the 1.99 + * number of milliseconds for which 1.100 + * to keep the vibrator on before 1.101 + * turning it off. Subsequent values 1.102 + * alternate between durations in 1.103 + * milliseconds to turn the vibrator 1.104 + * off or to turn the vibrator on. 1.105 + * 1.106 + * @param {Integer} repeat Optional index into the pattern array at which 1.107 + * to start repeating (will repeat until canceled), 1.108 + * or -1 for no repetition (default). 1.109 + */ 1.110 + vibrateWithPattern: function(pattern, repeat) { 1.111 + repeat = (typeof repeat !== "undefined") ? repeat : -1; 1.112 + pattern.unshift(0); //add a 0 at beginning for backwards compatibility from w3c spec 1.113 + exec(null, null, "Vibration", "vibrateWithPattern", [pattern, repeat]); 1.114 + }, 1.115 + 1.116 + /** 1.117 + * Immediately cancels any currently running vibration. 1.118 + */ 1.119 + cancelVibration: function() { 1.120 + exec(null, null, "Vibration", "cancelVibration", []); 1.121 + } 1.122 +};