1 | |
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
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 Microsoft.Xna.Framework.Audio;
|
24 | using WPCordovaClassLib.Cordova.UI;
|
25 | using System.Diagnostics;
|
26 |
|
27 |
|
28 | namespace WPCordovaClassLib.Cordova.Commands
|
29 | {
|
30 | public class Notification : BaseCommand
|
31 | {
|
32 | static ProgressBar progressBar = null;
|
33 | const int DEFAULT_DURATION = 5;
|
34 |
|
35 | private NotificationBox notifyBox;
|
36 |
|
37 | private class NotifBoxData
|
38 | {
|
39 | public NotificationBox previous {get;set;}
|
40 | public string callbackId { get; set; }
|
41 | }
|
42 |
|
43 | private PhoneApplicationPage Page
|
44 | {
|
45 | get
|
46 | {
|
47 | PhoneApplicationPage page = null;
|
48 | PhoneApplicationFrame frame = Application.Current.RootVisual as PhoneApplicationFrame;
|
49 | if (frame != null)
|
50 | {
|
51 | page = frame.Content as PhoneApplicationPage;
|
52 | }
|
53 | return page;
|
54 | }
|
55 | }
|
56 |
|
57 |
|
58 |
|
59 | [DataContract]
|
60 | public class AlertOptions
|
61 | {
|
62 | [OnDeserializing]
|
63 | public void OnDeserializing(StreamingContext context)
|
64 | {
|
65 |
|
66 | this.message = "message";
|
67 | this.title = "Alert";
|
68 | this.buttonLabel = "ok";
|
69 | }
|
70 |
|
71 |
|
72 |
|
73 |
|
74 | [DataMember]
|
75 | public string message;
|
76 |
|
77 |
|
78 |
|
79 |
|
80 | [DataMember]
|
81 | public string title;
|
82 |
|
83 |
|
84 |
|
85 |
|
86 | [DataMember]
|
87 | public string buttonLabel;
|
88 | }
|
89 |
|
90 | [DataContract]
|
91 | public class PromptResult
|
92 | {
|
93 | [DataMember]
|
94 | public int buttonIndex;
|
95 |
|
96 | [DataMember]
|
97 | public string input1;
|
98 |
|
99 | public PromptResult(int index, string text)
|
100 | {
|
101 | this.buttonIndex = index;
|
102 | this.input1 = text;
|
103 | }
|
104 | }
|
105 |
|
106 | public void alert(string options)
|
107 | {
|
108 | string[] args = JSON.JsonHelper.Deserialize<string[]>(options);
|
109 | AlertOptions alertOpts = new AlertOptions();
|
110 | alertOpts.message = args[0];
|
111 | alertOpts.title = args[1];
|
112 | alertOpts.buttonLabel = args[2];
|
113 | string aliasCurrentCommandCallbackId = args[3];
|
114 |
|
115 | Deployment.Current.Dispatcher.BeginInvoke(() =>
|
116 | {
|
117 | PhoneApplicationPage page = Page;
|
118 | if (page != null)
|
119 | {
|
120 | Grid grid = page.FindName("LayoutRoot") as Grid;
|
121 | if (grid != null)
|
122 | {
|
123 | var previous = notifyBox;
|
124 | notifyBox = new NotificationBox();
|
125 | notifyBox.Tag = new NotifBoxData { previous = previous, callbackId = aliasCurrentCommandCallbackId };
|
126 | notifyBox.PageTitle.Text = alertOpts.title;
|
127 | notifyBox.SubTitle.Text = alertOpts.message;
|
128 | Button btnOK = new Button();
|
129 | btnOK.Content = alertOpts.buttonLabel;
|
130 | btnOK.Click += new RoutedEventHandler(btnOK_Click);
|
131 | btnOK.Tag = 1;
|
132 | notifyBox.ButtonPanel.Children.Add(btnOK);
|
133 | grid.Children.Add(notifyBox);
|
134 |
|
135 | if (previous == null)
|
136 | {
|
137 | page.BackKeyPress += page_BackKeyPress;
|
138 | }
|
139 | }
|
140 | }
|
141 | else
|
142 | {
|
143 | DispatchCommandResult(new PluginResult(PluginResult.Status.INSTANTIATION_EXCEPTION));
|
144 | }
|
145 | });
|
146 | }
|
147 |
|
148 | public void prompt(string options)
|
149 | {
|
150 | string[] args = JSON.JsonHelper.Deserialize<string[]>(options);
|
151 | string message = args[0];
|
152 | string title = args[1];
|
153 | string buttonLabelsArray = args[2];
|
154 | string[] buttonLabels = JSON.JsonHelper.Deserialize<string[]>(buttonLabelsArray);
|
155 | string defaultText = args[3];
|
156 | string aliasCurrentCommandCallbackId = args[4];
|
157 |
|
158 | Deployment.Current.Dispatcher.BeginInvoke(() =>
|
159 | {
|
160 | PhoneApplicationPage page = Page;
|
161 | if (page != null)
|
162 | {
|
163 | Grid grid = page.FindName("LayoutRoot") as Grid;
|
164 | if (grid != null)
|
165 | {
|
166 | var previous = notifyBox;
|
167 | notifyBox = new NotificationBox();
|
168 | notifyBox.Tag = new NotifBoxData { previous = previous, callbackId = aliasCurrentCommandCallbackId };
|
169 | notifyBox.PageTitle.Text = title;
|
170 | notifyBox.SubTitle.Text = message;
|
171 |
|
172 |
|
173 |
|
174 |
|
175 |
|
176 |
|
177 | notifyBox.InputText.Text = defaultText;
|
178 | notifyBox.InputText.Visibility = Visibility.Visible;
|
179 |
|
180 | for (int i = 0; i < buttonLabels.Length; ++i)
|
181 | {
|
182 | Button button = new Button();
|
183 | button.Content = buttonLabels[i];
|
184 | button.Tag = i + 1;
|
185 | button.Click += promptBoxbutton_Click;
|
186 | notifyBox.ButtonPanel.Orientation = Orientation.Vertical;
|
187 | notifyBox.ButtonPanel.Children.Add(button);
|
188 | }
|
189 |
|
190 | grid.Children.Add(notifyBox);
|
191 | if (previous != null)
|
192 | {
|
193 | page.BackKeyPress += page_BackKeyPress;
|
194 | }
|
195 | }
|
196 | }
|
197 | else
|
198 | {
|
199 | DispatchCommandResult(new PluginResult(PluginResult.Status.INSTANTIATION_EXCEPTION));
|
200 | }
|
201 | });
|
202 | }
|
203 |
|
204 | public void confirm(string options)
|
205 | {
|
206 | string[] args = JSON.JsonHelper.Deserialize<string[]>(options);
|
207 | AlertOptions alertOpts = new AlertOptions();
|
208 | alertOpts.message = args[0];
|
209 | alertOpts.title = args[1];
|
210 | alertOpts.buttonLabel = args[2];
|
211 | string aliasCurrentCommandCallbackId = args[3];
|
212 |
|
213 | Deployment.Current.Dispatcher.BeginInvoke(() =>
|
214 | {
|
215 | PhoneApplicationPage page = Page;
|
216 | if (page != null)
|
217 | {
|
218 | Grid grid = page.FindName("LayoutRoot") as Grid;
|
219 | if (grid != null)
|
220 | {
|
221 | var previous = notifyBox;
|
222 | notifyBox = new NotificationBox();
|
223 | notifyBox.Tag = new NotifBoxData { previous = previous, callbackId = aliasCurrentCommandCallbackId };
|
224 | notifyBox.PageTitle.Text = alertOpts.title;
|
225 | notifyBox.SubTitle.Text = alertOpts.message;
|
226 |
|
227 | string[] labels = JSON.JsonHelper.Deserialize<string[]>(alertOpts.buttonLabel);
|
228 |
|
229 | if (labels == null)
|
230 | {
|
231 | labels = alertOpts.buttonLabel.Split(',');
|
232 | }
|
233 |
|
234 | for (int n = 0; n < labels.Length; n++)
|
235 | {
|
236 | Button btn = new Button();
|
237 | btn.Content = labels[n];
|
238 | btn.Tag = n;
|
239 | btn.Click += new RoutedEventHandler(btnOK_Click);
|
240 | notifyBox.ButtonPanel.Children.Add(btn);
|
241 | }
|
242 |
|
243 | grid.Children.Add(notifyBox);
|
244 | if (previous == null)
|
245 | {
|
246 | page.BackKeyPress += page_BackKeyPress;
|
247 | }
|
248 | }
|
249 | }
|
250 | else
|
251 | {
|
252 | DispatchCommandResult(new PluginResult(PluginResult.Status.INSTANTIATION_EXCEPTION));
|
253 | }
|
254 | });
|
255 | }
|
256 |
|
257 | void promptBoxbutton_Click(object sender, RoutedEventArgs e)
|
258 | {
|
259 | Button button = sender as Button;
|
260 | FrameworkElement promptBox = null;
|
261 | int buttonIndex = 0;
|
262 | string callbackId = string.Empty;
|
263 | string text = string.Empty;
|
264 | if (button != null)
|
265 | {
|
266 | buttonIndex = (int)button.Tag;
|
267 | promptBox = button.Parent as FrameworkElement;
|
268 | while ((promptBox = promptBox.Parent as FrameworkElement) != null &&
|
269 | !(promptBox is NotificationBox)) ;
|
270 | }
|
271 |
|
272 | if (promptBox != null)
|
273 | {
|
274 | NotificationBox box = promptBox as NotificationBox;
|
275 |
|
276 | text = box.InputText.Text;
|
277 |
|
278 | PhoneApplicationPage page = Page;
|
279 | if (page != null)
|
280 | {
|
281 | Grid grid = page.FindName("LayoutRoot") as Grid;
|
282 | if (grid != null)
|
283 | {
|
284 | grid.Children.Remove(promptBox);
|
285 | }
|
286 |
|
287 | NotifBoxData data = promptBox.Tag as NotifBoxData;
|
288 | promptBox = data.previous as NotificationBox;
|
289 | callbackId = data.callbackId as string;
|
290 |
|
291 | if (promptBox == null)
|
292 | {
|
293 | page.BackKeyPress -= page_BackKeyPress;
|
294 | }
|
295 | }
|
296 | }
|
297 | DispatchCommandResult(new PluginResult(PluginResult.Status.OK, new PromptResult(buttonIndex, text)), callbackId);
|
298 | }
|
299 |
|
300 | void page_BackKeyPress(object sender, System.ComponentModel.CancelEventArgs e)
|
301 | {
|
302 | PhoneApplicationPage page = sender as PhoneApplicationPage;
|
303 | string callbackId = "";
|
304 | if (page != null && notifyBox != null)
|
305 | {
|
306 | Grid grid = page.FindName("LayoutRoot") as Grid;
|
307 | if (grid != null)
|
308 | {
|
309 | grid.Children.Remove(notifyBox);
|
310 | NotifBoxData notifBoxData = notifyBox.Tag as NotifBoxData;
|
311 | notifyBox = notifBoxData.previous as NotificationBox;
|
312 | callbackId = notifBoxData.callbackId as string;
|
313 | }
|
314 | if (notifyBox == null)
|
315 | {
|
316 | page.BackKeyPress -= page_BackKeyPress;
|
317 | }
|
318 | e.Cancel = true;
|
319 | }
|
320 |
|
321 | DispatchCommandResult(new PluginResult(PluginResult.Status.OK, 0), callbackId);
|
322 | }
|
323 |
|
324 | void btnOK_Click(object sender, RoutedEventArgs e)
|
325 | {
|
326 | Button btn = sender as Button;
|
327 | FrameworkElement notifBoxParent = null;
|
328 | int retVal = 0;
|
329 | string callbackId = "";
|
330 | if (btn != null)
|
331 | {
|
332 | retVal = (int)btn.Tag + 1;
|
333 |
|
334 | notifBoxParent = btn.Parent as FrameworkElement;
|
335 | while ((notifBoxParent = notifBoxParent.Parent as FrameworkElement) != null &&
|
336 | !(notifBoxParent is NotificationBox)) ;
|
337 | }
|
338 | if (notifBoxParent != null)
|
339 | {
|
340 | PhoneApplicationPage page = Page;
|
341 | if (page != null)
|
342 | {
|
343 | Grid grid = page.FindName("LayoutRoot") as Grid;
|
344 | if (grid != null)
|
345 | {
|
346 | grid.Children.Remove(notifBoxParent);
|
347 | }
|
348 |
|
349 | NotifBoxData notifBoxData = notifBoxParent.Tag as NotifBoxData;
|
350 | notifyBox = notifBoxData.previous as NotificationBox;
|
351 | callbackId = notifBoxData.callbackId as string;
|
352 |
|
353 | if (notifyBox == null)
|
354 | {
|
355 | page.BackKeyPress -= page_BackKeyPress;
|
356 | }
|
357 | }
|
358 |
|
359 | }
|
360 | DispatchCommandResult(new PluginResult(PluginResult.Status.OK, retVal), callbackId);
|
361 | }
|
362 |
|
363 |
|
364 |
|
365 | public void beep(string options)
|
366 | {
|
367 | string[] args = JSON.JsonHelper.Deserialize<string[]>(options);
|
368 | int times = int.Parse(args[0]);
|
369 |
|
370 | string resourcePath = BaseCommand.GetBaseURL() + "Plugins/cordova-plugin-dialogs/notification-beep.wav";
|
371 |
|
372 | StreamResourceInfo sri = Application.GetResourceStream(new Uri(resourcePath, UriKind.Relative));
|
373 |
|
374 | if (sri != null)
|
375 | {
|
376 | SoundEffect effect = SoundEffect.FromStream(sri.Stream);
|
377 | SoundEffectInstance inst = effect.CreateInstance();
|
378 | ThreadPool.QueueUserWorkItem((o) =>
|
379 | {
|
380 |
|
381 | do
|
382 | {
|
383 | inst.Play();
|
384 | Thread.Sleep(effect.Duration + TimeSpan.FromMilliseconds(100));
|
385 | }
|
386 | while (--times > 0);
|
387 |
|
388 | });
|
389 |
|
390 | }
|
391 |
|
392 |
|
393 | DispatchCommandResult();
|
394 | }
|
395 |
|
396 |
|
397 | public void activityStart(string unused)
|
398 | {
|
399 |
|
400 | Deployment.Current.Dispatcher.BeginInvoke(() =>
|
401 | {
|
402 | PhoneApplicationFrame frame = Application.Current.RootVisual as PhoneApplicationFrame;
|
403 |
|
404 | if (frame != null)
|
405 | {
|
406 | PhoneApplicationPage page = frame.Content as PhoneApplicationPage;
|
407 |
|
408 | if (page != null)
|
409 | {
|
410 | var temp = page.FindName("LayoutRoot");
|
411 | Grid grid = temp as Grid;
|
412 | if (grid != null)
|
413 | {
|
414 | if (progressBar != null)
|
415 | {
|
416 | grid.Children.Remove(progressBar);
|
417 | }
|
418 | progressBar = new ProgressBar();
|
419 | progressBar.IsIndeterminate = true;
|
420 | progressBar.IsEnabled = true;
|
421 |
|
422 | grid.Children.Add(progressBar);
|
423 | }
|
424 | }
|
425 | }
|
426 | });
|
427 | }
|
428 |
|
429 |
|
430 |
|
431 | public void activityStop(string unused)
|
432 | {
|
433 | Deployment.Current.Dispatcher.BeginInvoke(() =>
|
434 | {
|
435 | if (progressBar != null)
|
436 | {
|
437 | progressBar.IsEnabled = false;
|
438 | PhoneApplicationFrame frame = Application.Current.RootVisual as PhoneApplicationFrame;
|
439 | if (frame != null)
|
440 | {
|
441 | PhoneApplicationPage page = frame.Content as PhoneApplicationPage;
|
442 | if (page != null)
|
443 | {
|
444 | Grid grid = page.FindName("LayoutRoot") as Grid;
|
445 | if (grid != null)
|
446 | {
|
447 | grid.Children.Remove(progressBar);
|
448 | }
|
449 | }
|
450 | }
|
451 | progressBar = null;
|
452 | }
|
453 | });
|
454 | }
|
455 |
|
456 | public void vibrate(string vibrateDuration)
|
457 | {
|
458 |
|
459 | int msecs = 200;
|
460 |
|
461 | try
|
462 | {
|
463 | string[] args = JSON.JsonHelper.Deserialize<string[]>(vibrateDuration);
|
464 |
|
465 | msecs = int.Parse(args[0]);
|
466 | if (msecs < 1)
|
467 | {
|
468 | msecs = 1;
|
469 | }
|
470 | }
|
471 | catch (FormatException)
|
472 | {
|
473 |
|
474 | }
|
475 |
|
476 | VibrateController.Default.Start(TimeSpan.FromMilliseconds(msecs));
|
477 |
|
478 |
|
479 | DispatchCommandResult();
|
480 | }
|
481 | }
|
482 | }
|