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: package org.apache.cordova.vibration; michael@0: michael@0: import org.apache.cordova.CallbackContext; michael@0: import org.apache.cordova.CordovaPlugin; michael@0: import org.json.JSONArray; michael@0: import org.json.JSONException; michael@0: import android.content.Context; michael@0: import android.os.Vibrator; michael@0: michael@0: /** michael@0: * This class provides access to vibration on the device. michael@0: */ michael@0: public class Vibration extends CordovaPlugin { michael@0: michael@0: /** michael@0: * Constructor. michael@0: */ michael@0: public Vibration() { michael@0: } michael@0: michael@0: /** michael@0: * Executes the request and returns PluginResult. michael@0: * michael@0: * @param action The action to execute. michael@0: * @param args JSONArray of arguments for the plugin. michael@0: * @param callbackContext The callback context used when calling back into JavaScript. michael@0: * @return True when the action was valid, false otherwise. michael@0: */ michael@0: public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException { michael@0: if (action.equals("vibrate")) { michael@0: this.vibrate(args.getLong(0)); michael@0: } michael@0: else if (action.equals("vibrateWithPattern")) { michael@0: JSONArray pattern = args.getJSONArray(0); michael@0: int repeat = args.getInt(1); michael@0: //add a 0 at the beginning of pattern to align with w3c michael@0: long[] patternArray = new long[pattern.length()+1]; michael@0: patternArray[0] = 0; michael@0: for (int i = 0; i < pattern.length(); i++) { michael@0: patternArray[i+1] = pattern.getLong(i); michael@0: } michael@0: this.vibrateWithPattern(patternArray, repeat); michael@0: } michael@0: else if (action.equals("cancelVibration")) { michael@0: this.cancelVibration(); michael@0: } michael@0: else { michael@0: return false; michael@0: } michael@0: michael@0: // Only alert and confirm are async. michael@0: callbackContext.success(); michael@0: michael@0: return true; michael@0: } michael@0: michael@0: //-------------------------------------------------------------------------- michael@0: // LOCAL METHODS michael@0: //-------------------------------------------------------------------------- michael@0: michael@0: /** michael@0: * Vibrates the device for a given amount of time. michael@0: * michael@0: * @param time Time to vibrate in ms. michael@0: */ michael@0: public void vibrate(long time) { michael@0: // Start the vibration, 0 defaults to half a second. michael@0: if (time == 0) { michael@0: time = 500; michael@0: } michael@0: Vibrator vibrator = (Vibrator) this.cordova.getActivity().getSystemService(Context.VIBRATOR_SERVICE); michael@0: vibrator.vibrate(time); michael@0: } michael@0: michael@0: /** michael@0: * Vibrates the device with a given pattern. michael@0: * michael@0: * @param pattern Pattern with which to vibrate the device. michael@0: * Pass in an array of longs 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 repeat Optional index into the pattern array at which michael@0: * to start repeating, or -1 for no repetition (default). michael@0: */ michael@0: public void vibrateWithPattern(long[] pattern, int repeat) { michael@0: Vibrator vibrator = (Vibrator) this.cordova.getActivity().getSystemService(Context.VIBRATOR_SERVICE); michael@0: vibrator.vibrate(pattern, repeat); michael@0: } michael@0: michael@0: /** michael@0: * Immediately cancels any currently running vibration. michael@0: */ michael@0: public void cancelVibration() { michael@0: Vibrator vibrator = (Vibrator) this.cordova.getActivity().getSystemService(Context.VIBRATOR_SERVICE); michael@0: vibrator.cancel(); michael@0: } michael@0: }