Thu, 04 Jun 2015 14:50:33 +0200
Genesis of lecture sources for Droidcon Berlin 2015 in Postbahnhof.
michael@0 | 1 | /* |
michael@0 | 2 | Licensed to the Apache Software Foundation (ASF) under one |
michael@0 | 3 | or more contributor license agreements. See the NOTICE file |
michael@0 | 4 | distributed with this work for additional information |
michael@0 | 5 | regarding copyright ownership. The ASF licenses this file |
michael@0 | 6 | to you under the Apache License, Version 2.0 (the |
michael@0 | 7 | "License"); you may not use this file except in compliance |
michael@0 | 8 | with the License. You may obtain a copy of the License at |
michael@0 | 9 | |
michael@0 | 10 | http://www.apache.org/licenses/LICENSE-2.0 |
michael@0 | 11 | |
michael@0 | 12 | Unless required by applicable law or agreed to in writing, |
michael@0 | 13 | software distributed under the License is distributed on an |
michael@0 | 14 | "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
michael@0 | 15 | KIND, either express or implied. See the License for the |
michael@0 | 16 | specific language governing permissions and limitations |
michael@0 | 17 | under the License. |
michael@0 | 18 | */ |
michael@0 | 19 | package org.apache.cordova.vibration; |
michael@0 | 20 | |
michael@0 | 21 | import org.apache.cordova.CallbackContext; |
michael@0 | 22 | import org.apache.cordova.CordovaPlugin; |
michael@0 | 23 | import org.json.JSONArray; |
michael@0 | 24 | import org.json.JSONException; |
michael@0 | 25 | import android.content.Context; |
michael@0 | 26 | import android.os.Vibrator; |
michael@0 | 27 | |
michael@0 | 28 | /** |
michael@0 | 29 | * This class provides access to vibration on the device. |
michael@0 | 30 | */ |
michael@0 | 31 | public class Vibration extends CordovaPlugin { |
michael@0 | 32 | |
michael@0 | 33 | /** |
michael@0 | 34 | * Constructor. |
michael@0 | 35 | */ |
michael@0 | 36 | public Vibration() { |
michael@0 | 37 | } |
michael@0 | 38 | |
michael@0 | 39 | /** |
michael@0 | 40 | * Executes the request and returns PluginResult. |
michael@0 | 41 | * |
michael@0 | 42 | * @param action The action to execute. |
michael@0 | 43 | * @param args JSONArray of arguments for the plugin. |
michael@0 | 44 | * @param callbackContext The callback context used when calling back into JavaScript. |
michael@0 | 45 | * @return True when the action was valid, false otherwise. |
michael@0 | 46 | */ |
michael@0 | 47 | public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException { |
michael@0 | 48 | if (action.equals("vibrate")) { |
michael@0 | 49 | this.vibrate(args.getLong(0)); |
michael@0 | 50 | } |
michael@0 | 51 | else if (action.equals("vibrateWithPattern")) { |
michael@0 | 52 | JSONArray pattern = args.getJSONArray(0); |
michael@0 | 53 | int repeat = args.getInt(1); |
michael@0 | 54 | //add a 0 at the beginning of pattern to align with w3c |
michael@0 | 55 | long[] patternArray = new long[pattern.length()+1]; |
michael@0 | 56 | patternArray[0] = 0; |
michael@0 | 57 | for (int i = 0; i < pattern.length(); i++) { |
michael@0 | 58 | patternArray[i+1] = pattern.getLong(i); |
michael@0 | 59 | } |
michael@0 | 60 | this.vibrateWithPattern(patternArray, repeat); |
michael@0 | 61 | } |
michael@0 | 62 | else if (action.equals("cancelVibration")) { |
michael@0 | 63 | this.cancelVibration(); |
michael@0 | 64 | } |
michael@0 | 65 | else { |
michael@0 | 66 | return false; |
michael@0 | 67 | } |
michael@0 | 68 | |
michael@0 | 69 | // Only alert and confirm are async. |
michael@0 | 70 | callbackContext.success(); |
michael@0 | 71 | |
michael@0 | 72 | return true; |
michael@0 | 73 | } |
michael@0 | 74 | |
michael@0 | 75 | //-------------------------------------------------------------------------- |
michael@0 | 76 | // LOCAL METHODS |
michael@0 | 77 | //-------------------------------------------------------------------------- |
michael@0 | 78 | |
michael@0 | 79 | /** |
michael@0 | 80 | * Vibrates the device for a given amount of time. |
michael@0 | 81 | * |
michael@0 | 82 | * @param time Time to vibrate in ms. |
michael@0 | 83 | */ |
michael@0 | 84 | public void vibrate(long time) { |
michael@0 | 85 | // Start the vibration, 0 defaults to half a second. |
michael@0 | 86 | if (time == 0) { |
michael@0 | 87 | time = 500; |
michael@0 | 88 | } |
michael@0 | 89 | Vibrator vibrator = (Vibrator) this.cordova.getActivity().getSystemService(Context.VIBRATOR_SERVICE); |
michael@0 | 90 | vibrator.vibrate(time); |
michael@0 | 91 | } |
michael@0 | 92 | |
michael@0 | 93 | /** |
michael@0 | 94 | * Vibrates the device with a given pattern. |
michael@0 | 95 | * |
michael@0 | 96 | * @param pattern Pattern with which to vibrate the device. |
michael@0 | 97 | * Pass in an array of longs that |
michael@0 | 98 | * are the durations for which to |
michael@0 | 99 | * turn on or off the vibrator in |
michael@0 | 100 | * milliseconds. The first value |
michael@0 | 101 | * indicates the number of milliseconds |
michael@0 | 102 | * to wait before turning the vibrator |
michael@0 | 103 | * on. The next value indicates the |
michael@0 | 104 | * number of milliseconds for which |
michael@0 | 105 | * to keep the vibrator on before |
michael@0 | 106 | * turning it off. Subsequent values |
michael@0 | 107 | * alternate between durations in |
michael@0 | 108 | * milliseconds to turn the vibrator |
michael@0 | 109 | * off or to turn the vibrator on. |
michael@0 | 110 | * |
michael@0 | 111 | * @param repeat Optional index into the pattern array at which |
michael@0 | 112 | * to start repeating, or -1 for no repetition (default). |
michael@0 | 113 | */ |
michael@0 | 114 | public void vibrateWithPattern(long[] pattern, int repeat) { |
michael@0 | 115 | Vibrator vibrator = (Vibrator) this.cordova.getActivity().getSystemService(Context.VIBRATOR_SERVICE); |
michael@0 | 116 | vibrator.vibrate(pattern, repeat); |
michael@0 | 117 | } |
michael@0 | 118 | |
michael@0 | 119 | /** |
michael@0 | 120 | * Immediately cancels any currently running vibration. |
michael@0 | 121 | */ |
michael@0 | 122 | public void cancelVibration() { |
michael@0 | 123 | Vibrator vibrator = (Vibrator) this.cordova.getActivity().getSystemService(Context.VIBRATOR_SERVICE); |
michael@0 | 124 | vibrator.cancel(); |
michael@0 | 125 | } |
michael@0 | 126 | } |