Touchgui/plugins/org.apache.cordova.vibration/src/android/Vibration.java

Thu, 04 Jun 2015 14:50:33 +0200

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 04 Jun 2015 14:50:33 +0200
changeset 0
e8ccd40d0ef6
permissions
-rw-r--r--

Genesis of lecture sources for Droidcon Berlin 2015 in Postbahnhof.

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

mercurial