1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/Touchgui/plugins/org.apache.cordova.dialogs/src/wp/Notification.cs Thu Jun 04 14:50:33 2015 +0200 1.3 @@ -0,0 +1,482 @@ 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 Microsoft.Xna.Framework.Audio; 1.27 +using WPCordovaClassLib.Cordova.UI; 1.28 +using System.Diagnostics; 1.29 + 1.30 + 1.31 +namespace WPCordovaClassLib.Cordova.Commands 1.32 +{ 1.33 + public class Notification : BaseCommand 1.34 + { 1.35 + static ProgressBar progressBar = null; 1.36 + const int DEFAULT_DURATION = 5; 1.37 + 1.38 + private NotificationBox notifyBox; 1.39 + 1.40 + private class NotifBoxData 1.41 + { 1.42 + public NotificationBox previous {get;set;} 1.43 + public string callbackId { get; set; } 1.44 + } 1.45 + 1.46 + private PhoneApplicationPage Page 1.47 + { 1.48 + get 1.49 + { 1.50 + PhoneApplicationPage page = null; 1.51 + PhoneApplicationFrame frame = Application.Current.RootVisual as PhoneApplicationFrame; 1.52 + if (frame != null) 1.53 + { 1.54 + page = frame.Content as PhoneApplicationPage; 1.55 + } 1.56 + return page; 1.57 + } 1.58 + } 1.59 + 1.60 + // blink api - doesn't look like there is an equivalent api we can use... 1.61 + 1.62 + [DataContract] 1.63 + public class AlertOptions 1.64 + { 1.65 + [OnDeserializing] 1.66 + public void OnDeserializing(StreamingContext context) 1.67 + { 1.68 + // set defaults 1.69 + this.message = "message"; 1.70 + this.title = "Alert"; 1.71 + this.buttonLabel = "ok"; 1.72 + } 1.73 + 1.74 + /// <summary> 1.75 + /// message to display in the alert box 1.76 + /// </summary> 1.77 + [DataMember] 1.78 + public string message; 1.79 + 1.80 + /// <summary> 1.81 + /// title displayed on the alert window 1.82 + /// </summary> 1.83 + [DataMember] 1.84 + public string title; 1.85 + 1.86 + /// <summary> 1.87 + /// text to display on the button 1.88 + /// </summary> 1.89 + [DataMember] 1.90 + public string buttonLabel; 1.91 + } 1.92 + 1.93 + [DataContract] 1.94 + public class PromptResult 1.95 + { 1.96 + [DataMember] 1.97 + public int buttonIndex; 1.98 + 1.99 + [DataMember] 1.100 + public string input1; 1.101 + 1.102 + public PromptResult(int index, string text) 1.103 + { 1.104 + this.buttonIndex = index; 1.105 + this.input1 = text; 1.106 + } 1.107 + } 1.108 + 1.109 + public void alert(string options) 1.110 + { 1.111 + string[] args = JSON.JsonHelper.Deserialize<string[]>(options); 1.112 + AlertOptions alertOpts = new AlertOptions(); 1.113 + alertOpts.message = args[0]; 1.114 + alertOpts.title = args[1]; 1.115 + alertOpts.buttonLabel = args[2]; 1.116 + string aliasCurrentCommandCallbackId = args[3]; 1.117 + 1.118 + Deployment.Current.Dispatcher.BeginInvoke(() => 1.119 + { 1.120 + PhoneApplicationPage page = Page; 1.121 + if (page != null) 1.122 + { 1.123 + Grid grid = page.FindName("LayoutRoot") as Grid; 1.124 + if (grid != null) 1.125 + { 1.126 + var previous = notifyBox; 1.127 + notifyBox = new NotificationBox(); 1.128 + notifyBox.Tag = new NotifBoxData { previous = previous, callbackId = aliasCurrentCommandCallbackId }; 1.129 + notifyBox.PageTitle.Text = alertOpts.title; 1.130 + notifyBox.SubTitle.Text = alertOpts.message; 1.131 + Button btnOK = new Button(); 1.132 + btnOK.Content = alertOpts.buttonLabel; 1.133 + btnOK.Click += new RoutedEventHandler(btnOK_Click); 1.134 + btnOK.Tag = 1; 1.135 + notifyBox.ButtonPanel.Children.Add(btnOK); 1.136 + grid.Children.Add(notifyBox); 1.137 + 1.138 + if (previous == null) 1.139 + { 1.140 + page.BackKeyPress += page_BackKeyPress; 1.141 + } 1.142 + } 1.143 + } 1.144 + else 1.145 + { 1.146 + DispatchCommandResult(new PluginResult(PluginResult.Status.INSTANTIATION_EXCEPTION)); 1.147 + } 1.148 + }); 1.149 + } 1.150 + 1.151 + public void prompt(string options) 1.152 + { 1.153 + string[] args = JSON.JsonHelper.Deserialize<string[]>(options); 1.154 + string message = args[0]; 1.155 + string title = args[1]; 1.156 + string buttonLabelsArray = args[2]; 1.157 + string[] buttonLabels = JSON.JsonHelper.Deserialize<string[]>(buttonLabelsArray); 1.158 + string defaultText = args[3]; 1.159 + string aliasCurrentCommandCallbackId = args[4]; 1.160 + 1.161 + Deployment.Current.Dispatcher.BeginInvoke(() => 1.162 + { 1.163 + PhoneApplicationPage page = Page; 1.164 + if (page != null) 1.165 + { 1.166 + Grid grid = page.FindName("LayoutRoot") as Grid; 1.167 + if (grid != null) 1.168 + { 1.169 + var previous = notifyBox; 1.170 + notifyBox = new NotificationBox(); 1.171 + notifyBox.Tag = new NotifBoxData { previous = previous, callbackId = aliasCurrentCommandCallbackId }; 1.172 + notifyBox.PageTitle.Text = title; 1.173 + notifyBox.SubTitle.Text = message; 1.174 + 1.175 + //TextBox textBox = new TextBox(); 1.176 + //textBox.Text = defaultText; 1.177 + //textBox.AcceptsReturn = true; 1.178 + //notifyBox.ContentScroller.Content = textBox; 1.179 + 1.180 + notifyBox.InputText.Text = defaultText; 1.181 + notifyBox.InputText.Visibility = Visibility.Visible; 1.182 + 1.183 + for (int i = 0; i < buttonLabels.Length; ++i) 1.184 + { 1.185 + Button button = new Button(); 1.186 + button.Content = buttonLabels[i]; 1.187 + button.Tag = i + 1; 1.188 + button.Click += promptBoxbutton_Click; 1.189 + notifyBox.ButtonPanel.Orientation = Orientation.Vertical; 1.190 + notifyBox.ButtonPanel.Children.Add(button); 1.191 + } 1.192 + 1.193 + grid.Children.Add(notifyBox); 1.194 + if (previous != null) 1.195 + { 1.196 + page.BackKeyPress += page_BackKeyPress; 1.197 + } 1.198 + } 1.199 + } 1.200 + else 1.201 + { 1.202 + DispatchCommandResult(new PluginResult(PluginResult.Status.INSTANTIATION_EXCEPTION)); 1.203 + } 1.204 + }); 1.205 + } 1.206 + 1.207 + public void confirm(string options) 1.208 + { 1.209 + string[] args = JSON.JsonHelper.Deserialize<string[]>(options); 1.210 + AlertOptions alertOpts = new AlertOptions(); 1.211 + alertOpts.message = args[0]; 1.212 + alertOpts.title = args[1]; 1.213 + alertOpts.buttonLabel = args[2]; 1.214 + string aliasCurrentCommandCallbackId = args[3]; 1.215 + 1.216 + Deployment.Current.Dispatcher.BeginInvoke(() => 1.217 + { 1.218 + PhoneApplicationPage page = Page; 1.219 + if (page != null) 1.220 + { 1.221 + Grid grid = page.FindName("LayoutRoot") as Grid; 1.222 + if (grid != null) 1.223 + { 1.224 + var previous = notifyBox; 1.225 + notifyBox = new NotificationBox(); 1.226 + notifyBox.Tag = new NotifBoxData { previous = previous, callbackId = aliasCurrentCommandCallbackId }; 1.227 + notifyBox.PageTitle.Text = alertOpts.title; 1.228 + notifyBox.SubTitle.Text = alertOpts.message; 1.229 + 1.230 + string[] labels = JSON.JsonHelper.Deserialize<string[]>(alertOpts.buttonLabel); 1.231 + 1.232 + if (labels == null) 1.233 + { 1.234 + labels = alertOpts.buttonLabel.Split(','); 1.235 + } 1.236 + 1.237 + for (int n = 0; n < labels.Length; n++) 1.238 + { 1.239 + Button btn = new Button(); 1.240 + btn.Content = labels[n]; 1.241 + btn.Tag = n; 1.242 + btn.Click += new RoutedEventHandler(btnOK_Click); 1.243 + notifyBox.ButtonPanel.Children.Add(btn); 1.244 + } 1.245 + 1.246 + grid.Children.Add(notifyBox); 1.247 + if (previous == null) 1.248 + { 1.249 + page.BackKeyPress += page_BackKeyPress; 1.250 + } 1.251 + } 1.252 + } 1.253 + else 1.254 + { 1.255 + DispatchCommandResult(new PluginResult(PluginResult.Status.INSTANTIATION_EXCEPTION)); 1.256 + } 1.257 + }); 1.258 + } 1.259 + 1.260 + void promptBoxbutton_Click(object sender, RoutedEventArgs e) 1.261 + { 1.262 + Button button = sender as Button; 1.263 + FrameworkElement promptBox = null; 1.264 + int buttonIndex = 0; 1.265 + string callbackId = string.Empty; 1.266 + string text = string.Empty; 1.267 + if (button != null) 1.268 + { 1.269 + buttonIndex = (int)button.Tag; 1.270 + promptBox = button.Parent as FrameworkElement; 1.271 + while ((promptBox = promptBox.Parent as FrameworkElement) != null && 1.272 + !(promptBox is NotificationBox)) ; 1.273 + } 1.274 + 1.275 + if (promptBox != null) 1.276 + { 1.277 + NotificationBox box = promptBox as NotificationBox; 1.278 + 1.279 + text = box.InputText.Text; 1.280 + 1.281 + PhoneApplicationPage page = Page; 1.282 + if (page != null) 1.283 + { 1.284 + Grid grid = page.FindName("LayoutRoot") as Grid; 1.285 + if (grid != null) 1.286 + { 1.287 + grid.Children.Remove(promptBox); 1.288 + } 1.289 + 1.290 + NotifBoxData data = promptBox.Tag as NotifBoxData; 1.291 + promptBox = data.previous as NotificationBox; 1.292 + callbackId = data.callbackId as string; 1.293 + 1.294 + if (promptBox == null) 1.295 + { 1.296 + page.BackKeyPress -= page_BackKeyPress; 1.297 + } 1.298 + } 1.299 + } 1.300 + DispatchCommandResult(new PluginResult(PluginResult.Status.OK, new PromptResult(buttonIndex, text)), callbackId); 1.301 + } 1.302 + 1.303 + void page_BackKeyPress(object sender, System.ComponentModel.CancelEventArgs e) 1.304 + { 1.305 + PhoneApplicationPage page = sender as PhoneApplicationPage; 1.306 + string callbackId = ""; 1.307 + if (page != null && notifyBox != null) 1.308 + { 1.309 + Grid grid = page.FindName("LayoutRoot") as Grid; 1.310 + if (grid != null) 1.311 + { 1.312 + grid.Children.Remove(notifyBox); 1.313 + NotifBoxData notifBoxData = notifyBox.Tag as NotifBoxData; 1.314 + notifyBox = notifBoxData.previous as NotificationBox; 1.315 + callbackId = notifBoxData.callbackId as string; 1.316 + } 1.317 + if (notifyBox == null) 1.318 + { 1.319 + page.BackKeyPress -= page_BackKeyPress; 1.320 + } 1.321 + e.Cancel = true; 1.322 + } 1.323 + 1.324 + DispatchCommandResult(new PluginResult(PluginResult.Status.OK, 0), callbackId); 1.325 + } 1.326 + 1.327 + void btnOK_Click(object sender, RoutedEventArgs e) 1.328 + { 1.329 + Button btn = sender as Button; 1.330 + FrameworkElement notifBoxParent = null; 1.331 + int retVal = 0; 1.332 + string callbackId = ""; 1.333 + if (btn != null) 1.334 + { 1.335 + retVal = (int)btn.Tag + 1; 1.336 + 1.337 + notifBoxParent = btn.Parent as FrameworkElement; 1.338 + while ((notifBoxParent = notifBoxParent.Parent as FrameworkElement) != null && 1.339 + !(notifBoxParent is NotificationBox)) ; 1.340 + } 1.341 + if (notifBoxParent != null) 1.342 + { 1.343 + PhoneApplicationPage page = Page; 1.344 + if (page != null) 1.345 + { 1.346 + Grid grid = page.FindName("LayoutRoot") as Grid; 1.347 + if (grid != null) 1.348 + { 1.349 + grid.Children.Remove(notifBoxParent); 1.350 + } 1.351 + 1.352 + NotifBoxData notifBoxData = notifBoxParent.Tag as NotifBoxData; 1.353 + notifyBox = notifBoxData.previous as NotificationBox; 1.354 + callbackId = notifBoxData.callbackId as string; 1.355 + 1.356 + if (notifyBox == null) 1.357 + { 1.358 + page.BackKeyPress -= page_BackKeyPress; 1.359 + } 1.360 + } 1.361 + 1.362 + } 1.363 + DispatchCommandResult(new PluginResult(PluginResult.Status.OK, retVal), callbackId); 1.364 + } 1.365 + 1.366 + 1.367 + 1.368 + public void beep(string options) 1.369 + { 1.370 + string[] args = JSON.JsonHelper.Deserialize<string[]>(options); 1.371 + int times = int.Parse(args[0]); 1.372 + 1.373 + string resourcePath = BaseCommand.GetBaseURL() + "Plugins/org.apache.cordova.dialogs/notification-beep.wav"; 1.374 + 1.375 + StreamResourceInfo sri = Application.GetResourceStream(new Uri(resourcePath, UriKind.Relative)); 1.376 + 1.377 + if (sri != null) 1.378 + { 1.379 + SoundEffect effect = SoundEffect.FromStream(sri.Stream); 1.380 + SoundEffectInstance inst = effect.CreateInstance(); 1.381 + ThreadPool.QueueUserWorkItem((o) => 1.382 + { 1.383 + // cannot interact with UI !! 1.384 + do 1.385 + { 1.386 + inst.Play(); 1.387 + Thread.Sleep(effect.Duration + TimeSpan.FromMilliseconds(100)); 1.388 + } 1.389 + while (--times > 0); 1.390 + 1.391 + }); 1.392 + 1.393 + } 1.394 + 1.395 + // TODO: may need a listener to trigger DispatchCommandResult after the alarm has finished executing... 1.396 + DispatchCommandResult(); 1.397 + } 1.398 + 1.399 + // Display an indeterminate progress indicator 1.400 + public void activityStart(string unused) 1.401 + { 1.402 + 1.403 + Deployment.Current.Dispatcher.BeginInvoke(() => 1.404 + { 1.405 + PhoneApplicationFrame frame = Application.Current.RootVisual as PhoneApplicationFrame; 1.406 + 1.407 + if (frame != null) 1.408 + { 1.409 + PhoneApplicationPage page = frame.Content as PhoneApplicationPage; 1.410 + 1.411 + if (page != null) 1.412 + { 1.413 + var temp = page.FindName("LayoutRoot"); 1.414 + Grid grid = temp as Grid; 1.415 + if (grid != null) 1.416 + { 1.417 + if (progressBar != null) 1.418 + { 1.419 + grid.Children.Remove(progressBar); 1.420 + } 1.421 + progressBar = new ProgressBar(); 1.422 + progressBar.IsIndeterminate = true; 1.423 + progressBar.IsEnabled = true; 1.424 + 1.425 + grid.Children.Add(progressBar); 1.426 + } 1.427 + } 1.428 + } 1.429 + }); 1.430 + } 1.431 + 1.432 + 1.433 + // Remove our indeterminate progress indicator 1.434 + public void activityStop(string unused) 1.435 + { 1.436 + Deployment.Current.Dispatcher.BeginInvoke(() => 1.437 + { 1.438 + if (progressBar != null) 1.439 + { 1.440 + progressBar.IsEnabled = false; 1.441 + PhoneApplicationFrame frame = Application.Current.RootVisual as PhoneApplicationFrame; 1.442 + if (frame != null) 1.443 + { 1.444 + PhoneApplicationPage page = frame.Content as PhoneApplicationPage; 1.445 + if (page != null) 1.446 + { 1.447 + Grid grid = page.FindName("LayoutRoot") as Grid; 1.448 + if (grid != null) 1.449 + { 1.450 + grid.Children.Remove(progressBar); 1.451 + } 1.452 + } 1.453 + } 1.454 + progressBar = null; 1.455 + } 1.456 + }); 1.457 + } 1.458 + 1.459 + public void vibrate(string vibrateDuration) 1.460 + { 1.461 + 1.462 + int msecs = 200; // set default 1.463 + 1.464 + try 1.465 + { 1.466 + string[] args = JSON.JsonHelper.Deserialize<string[]>(vibrateDuration); 1.467 + 1.468 + msecs = int.Parse(args[0]); 1.469 + if (msecs < 1) 1.470 + { 1.471 + msecs = 1; 1.472 + } 1.473 + } 1.474 + catch (FormatException) 1.475 + { 1.476 + 1.477 + } 1.478 + 1.479 + VibrateController.Default.Start(TimeSpan.FromMilliseconds(msecs)); 1.480 + 1.481 + // TODO: may need to add listener to trigger DispatchCommandResult when the vibration ends... 1.482 + DispatchCommandResult(); 1.483 + } 1.484 + } 1.485 +}