|
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 |
|
5 |
|
6 http://www.apache.org/licenses/LICENSE-2.0 |
|
7 |
|
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 */ |
|
14 |
|
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; |
|
24 |
|
25 |
|
26 namespace WPCordovaClassLib.Cordova.Commands |
|
27 { |
|
28 public class Vibration : BaseCommand |
|
29 { |
|
30 private static readonly int DEFAULT_DURATION = 200; |
|
31 |
|
32 public void vibrate(string vibrateDuration) |
|
33 { |
|
34 int msecs = DEFAULT_DURATION; // set default |
|
35 |
|
36 try |
|
37 { |
|
38 string[] args = JSON.JsonHelper.Deserialize<string[]>(vibrateDuration); |
|
39 |
|
40 msecs = int.Parse(args[0]); |
|
41 if (msecs < 1) |
|
42 { |
|
43 msecs = 1; |
|
44 } |
|
45 } |
|
46 catch (FormatException) |
|
47 { |
|
48 |
|
49 } |
|
50 |
|
51 vibrateMs(msecs); |
|
52 |
|
53 // TODO: may need to add listener to trigger DispatchCommandResult when the vibration ends... |
|
54 DispatchCommandResult(); |
|
55 } |
|
56 |
|
57 private static void vibrateMs(int msecs) |
|
58 { |
|
59 VibrateController.Default.Start(TimeSpan.FromMilliseconds(msecs)); |
|
60 } |
|
61 |
|
62 public void vibrateWithPattern(string options) |
|
63 { |
|
64 // falling back to vibrate |
|
65 vibrateMs(DEFAULT_DURATION); |
|
66 |
|
67 // TODO: may need to add listener to trigger DispatchCommandResult when the vibration ends... |
|
68 DispatchCommandResult(); |
|
69 } |
|
70 |
|
71 public void cancelVibration(string options) |
|
72 { |
|
73 VibrateController.Default.Stop(); |
|
74 DispatchCommandResult(); |
|
75 } |
|
76 } |
|
77 } |