Touchgui/plugins/org.apache.cordova.dialogs/src/wp/Notification.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.

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

mercurial