Touchgui/plugins/org.apache.cordova.vibration/src/wp/Vibration.cs

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 under the Apache License, Version 2.0 (the "License");
     3 	you may not use this file except in compliance with the License.
     4 	You may obtain a copy of the License at
     6 	http://www.apache.org/licenses/LICENSE-2.0
     8 	Unless required by applicable law or agreed to in writing, software
     9 	distributed under the License is distributed on an "AS IS" BASIS,
    10 	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    11 	See the License for the specific language governing permissions and
    12 	limitations under the License.
    13 */
    15 using System;
    16 using System.Windows;
    17 using System.Windows.Controls;
    18 using Microsoft.Devices;
    19 using System.Runtime.Serialization;
    20 using System.Threading;
    21 using System.Windows.Resources;
    22 using Microsoft.Phone.Controls;
    23 using System.Diagnostics;
    26 namespace WPCordovaClassLib.Cordova.Commands
    27 {
    28     public class Vibration : BaseCommand
    29     {
    30         private static readonly int DEFAULT_DURATION = 200;
    32         public void vibrate(string vibrateDuration)
    33         {
    34             int msecs = DEFAULT_DURATION; // set default
    36             try
    37             {
    38                 string[] args = JSON.JsonHelper.Deserialize<string[]>(vibrateDuration);
    40                 msecs = int.Parse(args[0]);
    41                 if (msecs < 1)
    42                 {
    43                     msecs = 1;
    44                 }
    45             }
    46             catch (FormatException)
    47             {
    49             }
    51             vibrateMs(msecs);
    53             // TODO: may need to add listener to trigger DispatchCommandResult when the vibration ends...
    54             DispatchCommandResult();
    55         }
    57         private static void vibrateMs(int msecs)
    58         {
    59             VibrateController.Default.Start(TimeSpan.FromMilliseconds(msecs));
    60         }
    62         public void vibrateWithPattern(string options)
    63         {
    64             // falling back to vibrate
    65             vibrateMs(DEFAULT_DURATION);
    67             // TODO: may need to add listener to trigger DispatchCommandResult when the vibration ends...
    68             DispatchCommandResult();
    69         }
    71         public void cancelVibration(string options)
    72         {
    73             VibrateController.Default.Stop();
    74             DispatchCommandResult();
    75         }
    76     }
    77 }

mercurial