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

changeset 0
e8ccd40d0ef6
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/Touchgui/plugins/org.apache.cordova.vibration/src/wp/Vibration.cs	Thu Jun 04 14:50:33 2015 +0200
     1.3 @@ -0,0 +1,77 @@
     1.4 +/*  
     1.5 +	Licensed under the Apache License, Version 2.0 (the "License");
     1.6 +	you may not use this file except in compliance with the License.
     1.7 +	You may obtain a copy of the License at
     1.8 +	
     1.9 +	http://www.apache.org/licenses/LICENSE-2.0
    1.10 +	
    1.11 +	Unless required by applicable law or agreed to in writing, software
    1.12 +	distributed under the License is distributed on an "AS IS" BASIS,
    1.13 +	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    1.14 +	See the License for the specific language governing permissions and
    1.15 +	limitations under the License.
    1.16 +*/
    1.17 +
    1.18 +using System;
    1.19 +using System.Windows;
    1.20 +using System.Windows.Controls;
    1.21 +using Microsoft.Devices;
    1.22 +using System.Runtime.Serialization;
    1.23 +using System.Threading;
    1.24 +using System.Windows.Resources;
    1.25 +using Microsoft.Phone.Controls;
    1.26 +using System.Diagnostics;
    1.27 +
    1.28 +
    1.29 +namespace WPCordovaClassLib.Cordova.Commands
    1.30 +{
    1.31 +    public class Vibration : BaseCommand
    1.32 +    {
    1.33 +        private static readonly int DEFAULT_DURATION = 200;
    1.34 +
    1.35 +        public void vibrate(string vibrateDuration)
    1.36 +        {
    1.37 +            int msecs = DEFAULT_DURATION; // set default
    1.38 +
    1.39 +            try
    1.40 +            {
    1.41 +                string[] args = JSON.JsonHelper.Deserialize<string[]>(vibrateDuration);
    1.42 +
    1.43 +                msecs = int.Parse(args[0]);
    1.44 +                if (msecs < 1)
    1.45 +                {
    1.46 +                    msecs = 1;
    1.47 +                }
    1.48 +            }
    1.49 +            catch (FormatException)
    1.50 +            {
    1.51 +
    1.52 +            }
    1.53 +
    1.54 +            vibrateMs(msecs);
    1.55 +
    1.56 +            // TODO: may need to add listener to trigger DispatchCommandResult when the vibration ends...
    1.57 +            DispatchCommandResult();
    1.58 +        }
    1.59 +
    1.60 +        private static void vibrateMs(int msecs)
    1.61 +        {
    1.62 +            VibrateController.Default.Start(TimeSpan.FromMilliseconds(msecs));
    1.63 +        }
    1.64 +
    1.65 +        public void vibrateWithPattern(string options)
    1.66 +        {
    1.67 +            // falling back to vibrate
    1.68 +            vibrateMs(DEFAULT_DURATION);
    1.69 +
    1.70 +            // TODO: may need to add listener to trigger DispatchCommandResult when the vibration ends...
    1.71 +            DispatchCommandResult();
    1.72 +        }
    1.73 +
    1.74 +        public void cancelVibration(string options)
    1.75 +        {
    1.76 +            VibrateController.Default.Stop();
    1.77 +            DispatchCommandResult();
    1.78 +        }
    1.79 +    }
    1.80 +}

mercurial