1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/Touchgui/plugins/org.apache.cordova.vibration/src/android/Vibration.java Thu Jun 04 14:50:33 2015 +0200 1.3 @@ -0,0 +1,126 @@ 1.4 +/* 1.5 + Licensed to the Apache Software Foundation (ASF) under one 1.6 + or more contributor license agreements. See the NOTICE file 1.7 + distributed with this work for additional information 1.8 + regarding copyright ownership. The ASF licenses this file 1.9 + to you under the Apache License, Version 2.0 (the 1.10 + "License"); you may not use this file except in compliance 1.11 + with the License. You may obtain a copy of the License at 1.12 + 1.13 + http://www.apache.org/licenses/LICENSE-2.0 1.14 + 1.15 + Unless required by applicable law or agreed to in writing, 1.16 + software distributed under the License is distributed on an 1.17 + "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 1.18 + KIND, either express or implied. See the License for the 1.19 + specific language governing permissions and limitations 1.20 + under the License. 1.21 +*/ 1.22 +package org.apache.cordova.vibration; 1.23 + 1.24 +import org.apache.cordova.CallbackContext; 1.25 +import org.apache.cordova.CordovaPlugin; 1.26 +import org.json.JSONArray; 1.27 +import org.json.JSONException; 1.28 +import android.content.Context; 1.29 +import android.os.Vibrator; 1.30 + 1.31 +/** 1.32 + * This class provides access to vibration on the device. 1.33 + */ 1.34 +public class Vibration extends CordovaPlugin { 1.35 + 1.36 + /** 1.37 + * Constructor. 1.38 + */ 1.39 + public Vibration() { 1.40 + } 1.41 + 1.42 + /** 1.43 + * Executes the request and returns PluginResult. 1.44 + * 1.45 + * @param action The action to execute. 1.46 + * @param args JSONArray of arguments for the plugin. 1.47 + * @param callbackContext The callback context used when calling back into JavaScript. 1.48 + * @return True when the action was valid, false otherwise. 1.49 + */ 1.50 + public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException { 1.51 + if (action.equals("vibrate")) { 1.52 + this.vibrate(args.getLong(0)); 1.53 + } 1.54 + else if (action.equals("vibrateWithPattern")) { 1.55 + JSONArray pattern = args.getJSONArray(0); 1.56 + int repeat = args.getInt(1); 1.57 + //add a 0 at the beginning of pattern to align with w3c 1.58 + long[] patternArray = new long[pattern.length()+1]; 1.59 + patternArray[0] = 0; 1.60 + for (int i = 0; i < pattern.length(); i++) { 1.61 + patternArray[i+1] = pattern.getLong(i); 1.62 + } 1.63 + this.vibrateWithPattern(patternArray, repeat); 1.64 + } 1.65 + else if (action.equals("cancelVibration")) { 1.66 + this.cancelVibration(); 1.67 + } 1.68 + else { 1.69 + return false; 1.70 + } 1.71 + 1.72 + // Only alert and confirm are async. 1.73 + callbackContext.success(); 1.74 + 1.75 + return true; 1.76 + } 1.77 + 1.78 + //-------------------------------------------------------------------------- 1.79 + // LOCAL METHODS 1.80 + //-------------------------------------------------------------------------- 1.81 + 1.82 + /** 1.83 + * Vibrates the device for a given amount of time. 1.84 + * 1.85 + * @param time Time to vibrate in ms. 1.86 + */ 1.87 + public void vibrate(long time) { 1.88 + // Start the vibration, 0 defaults to half a second. 1.89 + if (time == 0) { 1.90 + time = 500; 1.91 + } 1.92 + Vibrator vibrator = (Vibrator) this.cordova.getActivity().getSystemService(Context.VIBRATOR_SERVICE); 1.93 + vibrator.vibrate(time); 1.94 + } 1.95 + 1.96 + /** 1.97 + * Vibrates the device with a given pattern. 1.98 + * 1.99 + * @param pattern Pattern with which to vibrate the device. 1.100 + * Pass in an array of longs that 1.101 + * are the durations for which to 1.102 + * turn on or off the vibrator in 1.103 + * milliseconds. The first value 1.104 + * indicates the number of milliseconds 1.105 + * to wait before turning the vibrator 1.106 + * on. The next value indicates the 1.107 + * number of milliseconds for which 1.108 + * to keep the vibrator on before 1.109 + * turning it off. Subsequent values 1.110 + * alternate between durations in 1.111 + * milliseconds to turn the vibrator 1.112 + * off or to turn the vibrator on. 1.113 + * 1.114 + * @param repeat Optional index into the pattern array at which 1.115 + * to start repeating, or -1 for no repetition (default). 1.116 + */ 1.117 + public void vibrateWithPattern(long[] pattern, int repeat) { 1.118 + Vibrator vibrator = (Vibrator) this.cordova.getActivity().getSystemService(Context.VIBRATOR_SERVICE); 1.119 + vibrator.vibrate(pattern, repeat); 1.120 + } 1.121 + 1.122 + /** 1.123 + * Immediately cancels any currently running vibration. 1.124 + */ 1.125 + public void cancelVibration() { 1.126 + Vibrator vibrator = (Vibrator) this.cordova.getActivity().getSystemService(Context.VIBRATOR_SERVICE); 1.127 + vibrator.cancel(); 1.128 + } 1.129 +}