You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by pu...@apache.org on 2015/05/19 00:33:32 UTC

[8/8] cordova-plugin-vibration git commit: used min/max statics in vibrate with pattern. Use callbackId in callbacks, catch json format exceptions

used min/max statics in vibrate with pattern. Use callbackId in callbacks, catch json format exceptions


Project: http://git-wip-us.apache.org/repos/asf/cordova-plugin-vibration/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-plugin-vibration/commit/e29d45c1
Tree: http://git-wip-us.apache.org/repos/asf/cordova-plugin-vibration/tree/e29d45c1
Diff: http://git-wip-us.apache.org/repos/asf/cordova-plugin-vibration/diff/e29d45c1

Branch: refs/heads/master
Commit: e29d45c1cc0652613852b201d5dff9ef72279ebb
Parents: e9e96ff
Author: Jesse MacFadyen <pu...@gmail.com>
Authored: Mon May 18 15:32:14 2015 -0700
Committer: Jesse MacFadyen <pu...@gmail.com>
Committed: Mon May 18 15:32:14 2015 -0700

----------------------------------------------------------------------
 src/wp/Vibration.cs | 48 ++++++++++++++++++++++++++++++------------------
 1 file changed, 30 insertions(+), 18 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-plugin-vibration/blob/e29d45c1/src/wp/Vibration.cs
----------------------------------------------------------------------
diff --git a/src/wp/Vibration.cs b/src/wp/Vibration.cs
index 5a3b0d9..258dbc4 100644
--- a/src/wp/Vibration.cs
+++ b/src/wp/Vibration.cs
@@ -39,12 +39,14 @@ namespace WPCordovaClassLib.Cordova.Commands
         public void vibrate(string vibrateDuration)
         {
             int msecs = DEFAULT_DURATION; // set default
+            string callbackId = CurrentCommandCallbackId;
 
             try
             {
                 string[] args = JSON.JsonHelper.Deserialize<string[]>(vibrateDuration);
-
                 msecs = int.Parse(args[0]);
+                callbackId = args[1];
+
                 if (msecs < MIN_DURATION)
                 {
                     msecs = MIN_DURATION;
@@ -56,13 +58,13 @@ namespace WPCordovaClassLib.Cordova.Commands
             }
             catch (FormatException)
             {
-
+                 DispatchCommandResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION), callbackId);
             }
 
             vibrateMs(msecs);
 
             // TODO: may need to add listener to trigger DispatchCommandResult when the vibration ends...
-            DispatchCommandResult();
+            DispatchCommandResult(new PluginResult(PluginResult.Status.OK),callbackId);
         }
 
         private static void vibrateMs(int msecs)
@@ -72,34 +74,44 @@ namespace WPCordovaClassLib.Cordova.Commands
 
         public async Task vibrateWithPattern(string options)
         {
+            string callbackId = CurrentCommandCallbackId;
             // clear the cancelWasCalled flag
             cancelWasCalled = false;
             // get options
-            string[] args = JSON.JsonHelper.Deserialize<string[]>(options);
-            int[] pattern = JSON.JsonHelper.Deserialize<int[]>(args[0]);
-
-            for (int i = 0; i < pattern.Length && !cancelWasCalled; i++)
+            try
             {
-                int msecs = pattern[i];
-                if (msecs < 1)
-                {
-                    msecs = 1;
-                }
-                if (i % 2 == 0)
+                string[] args = JSON.JsonHelper.Deserialize<string[]>(options);
+                int[] pattern = JSON.JsonHelper.Deserialize<int[]>(args[0]);
+                callbackId = args[1];
+
+                for (int i = 0; i < pattern.Length && !cancelWasCalled; i++)
                 {
-                    msecs = (msecs > 5000) ? 5000 : msecs;
-                    VibrateController.Default.Start(TimeSpan.FromMilliseconds(msecs));
+                    int msecs = pattern[i];
+                    if (msecs < MIN_DURATION)
+                    {
+                        msecs = MIN_DURATION;
+                    }
+                    if (i % 2 == 0)
+                    {
+                        msecs = (msecs > MAX_DURATION) ? MAX_DURATION : msecs;
+                        VibrateController.Default.Start(TimeSpan.FromMilliseconds(msecs));
+                    }
+                    await Task.Delay(TimeSpan.FromMilliseconds(msecs));
                 }
-                await Task.Delay(TimeSpan.FromMilliseconds(msecs));
+                DispatchCommandResult(new PluginResult(PluginResult.Status.OK), callbackId);
+            }
+            catch (FormatException)
+            {
+                DispatchCommandResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION), callbackId);
             }
-            DispatchCommandResult();
         }
 
         public void cancelVibration(string options)
         {
+            string callbackId = JSON.JsonHelper.Deserialize<string[]>(options)[0];
             VibrateController.Default.Stop();
             cancelWasCalled = true;
-            DispatchCommandResult();
+            DispatchCommandResult(new PluginResult(PluginResult.Status.OK), callbackId);
         }
     }
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cordova.apache.org
For additional commands, e-mail: commits-help@cordova.apache.org