michael@0: /* michael@0: * michael@0: * Licensed to the Apache Software Foundation (ASF) under one michael@0: * or more contributor license agreements. See the NOTICE file michael@0: * distributed with this work for additional information michael@0: * regarding copyright ownership. The ASF licenses this file michael@0: * to you under the Apache License, Version 2.0 (the michael@0: * "License"); you may not use this file except in compliance michael@0: * with the License. You may obtain a copy of the License at michael@0: * michael@0: * http://www.apache.org/licenses/LICENSE-2.0 michael@0: * michael@0: * Unless required by applicable law or agreed to in writing, michael@0: * software distributed under the License is distributed on an michael@0: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY michael@0: * KIND, either express or implied. See the License for the michael@0: * specific language governing permissions and limitations michael@0: * under the License. michael@0: * michael@0: */ michael@0: michael@0: var exec = require('cordova/exec'); michael@0: michael@0: /** michael@0: * Provides access to the vibration mechanism on the device. michael@0: */ michael@0: michael@0: module.exports = { michael@0: michael@0: /** michael@0: * 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: * michael@0: * @param {Integer} param The number of milliseconds to vibrate (if 0, cancels vibration) michael@0: * michael@0: * michael@0: * @param {Array of Integer} param Pattern with which to vibrate the device. michael@0: * Pass in an array of integers that michael@0: * are the durations for which to michael@0: * turn on or off the vibrator in michael@0: * milliseconds. The FIRST value michael@0: * indicates the michael@0: * number of milliseconds for which michael@0: * to keep the vibrator ON before michael@0: * turning it off. The NEXT value indicates the michael@0: * number of milliseconds for which michael@0: * to keep the vibrator OFF before michael@0: * turning it on. Subsequent values michael@0: * alternate between durations in michael@0: * milliseconds to turn the vibrator michael@0: * off or to turn the vibrator on. michael@0: * (if empty, cancels vibration) michael@0: */ michael@0: vibrate: function(param) { michael@0: michael@0: /* Aligning with w3c spec */ michael@0: michael@0: //vibrate michael@0: if ((typeof param == 'number') && param != 0) michael@0: exec(null, null, "Vibration", "vibrate", [param]); michael@0: michael@0: //vibrate with array ( i.e. vibrate([3000]) ) michael@0: else if ((typeof param == 'object') && param.length == 1) michael@0: { michael@0: //cancel if vibrate([0]) michael@0: if (param[0] == 0) michael@0: exec(null, null, "Vibration", "cancelVibration", []); michael@0: michael@0: //else vibrate michael@0: else michael@0: exec(null, null, "Vibration", "vibrate", [param[0]]); michael@0: } michael@0: michael@0: //vibrate with a pattern michael@0: else if ((typeof param == 'object') && param.length > 1) michael@0: { michael@0: var repeat = -1; //no repeat michael@0: exec(null, null, "Vibration", "vibrateWithPattern", [param, repeat]); michael@0: } michael@0: michael@0: //cancel vibration (param = 0 or []) michael@0: else michael@0: exec(null, null, "Vibration", "cancelVibration", []); michael@0: }, michael@0: michael@0: /** michael@0: * Vibrates the device with a given pattern. michael@0: * michael@0: * @param {Array of Integer} pattern Pattern with which to vibrate the device. michael@0: * Pass in an array of integers that michael@0: * are the durations for which to michael@0: * turn on or off the vibrator in michael@0: * milliseconds. The first value michael@0: * indicates the number of milliseconds michael@0: * to wait before turning the vibrator michael@0: * on. The next value indicates the michael@0: * number of milliseconds for which michael@0: * to keep the vibrator on before michael@0: * turning it off. Subsequent values michael@0: * alternate between durations in michael@0: * milliseconds to turn the vibrator michael@0: * off or to turn the vibrator on. michael@0: * michael@0: * @param {Integer} repeat Optional index into the pattern array at which michael@0: * to start repeating (will repeat until canceled), michael@0: * or -1 for no repetition (default). michael@0: */ michael@0: vibrateWithPattern: function(pattern, repeat) { michael@0: repeat = (typeof repeat !== "undefined") ? repeat : -1; michael@0: pattern.unshift(0); //add a 0 at beginning for backwards compatibility from w3c spec michael@0: exec(null, null, "Vibration", "vibrateWithPattern", [pattern, repeat]); michael@0: }, michael@0: michael@0: /** michael@0: * Immediately cancels any currently running vibration. michael@0: */ michael@0: cancelVibration: function() { michael@0: exec(null, null, "Vibration", "cancelVibration", []); michael@0: } michael@0: };