|
| 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 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 | + // blink api - doesn't look like there is an equivalent api we can use... |
| 58 | + |
| 59 | + [DataContract] |
| 60 | + public class AlertOptions |
| 61 | + { |
| 62 | + [OnDeserializing] |
| 63 | + public void OnDeserializing(StreamingContext context) |
| 64 | + { |
| 65 | + // set defaults |
| 66 | + this.message = "message"; |
| 67 | + this.title = "Alert"; |
| 68 | + this.buttonLabel = "ok"; |
| 69 | + } |
| 70 | + |
| 71 | + /// <summary> |
| 72 | + /// message to display in the alert box |
| 73 | + /// </summary> |
| 74 | + [DataMember] |
| 75 | + public string message; |
| 76 | + |
| 77 | + /// <summary> |
| 78 | + /// title displayed on the alert window |
| 79 | + /// </summary> |
| 80 | + [DataMember] |
| 81 | + public string title; |
| 82 | + |
| 83 | + /// <summary> |
| 84 | + /// text to display on the button |
| 85 | + /// </summary> |
| 86 | + [DataMember] |
| 87 | + public string buttonLabel; |
| 88 | + } |
| 89 | + |
| 90 | + public void alert(string options) |
| 91 | + { |
| 92 | + string[] args = JSON.JsonHelper.Deserialize<string[]>(options); |
| 93 | + AlertOptions alertOpts = new AlertOptions(); |
| 94 | + alertOpts.message = args[0]; |
| 95 | + alertOpts.title = args[1]; |
| 96 | + alertOpts.buttonLabel = args[2]; |
| 97 | + string aliasCurrentCommandCallbackId = args[3]; |
| 98 | + |
| 99 | + Deployment.Current.Dispatcher.BeginInvoke(() => |
| 100 | + { |
| 101 | + PhoneApplicationPage page = Page; |
| 102 | + if (page != null) |
| 103 | + { |
| 104 | + Grid grid = page.FindName("LayoutRoot") as Grid; |
| 105 | + if (grid != null) |
| 106 | + { |
| 107 | + var previous = notifyBox; |
| 108 | + notifyBox = new NotificationBox(); |
| 109 | + notifyBox.Tag = new NotifBoxData { previous = previous, callbackId = aliasCurrentCommandCallbackId }; |
| 110 | + notifyBox.PageTitle.Text = alertOpts.title; |
| 111 | + notifyBox.SubTitle.Text = alertOpts.message; |
| 112 | + Button btnOK = new Button(); |
| 113 | + btnOK.Content = alertOpts.buttonLabel; |
| 114 | + btnOK.Click += new RoutedEventHandler(btnOK_Click); |
| 115 | + btnOK.Tag = 1; |
| 116 | + notifyBox.ButtonPanel.Children.Add(btnOK); |
| 117 | + grid.Children.Add(notifyBox); |
| 118 | + |
| 119 | + if (previous == null) |
| 120 | + { |
| 121 | + page.BackKeyPress += page_BackKeyPress; |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + else |
| 126 | + { |
| 127 | + DispatchCommandResult(new PluginResult(PluginResult.Status.INSTANTIATION_EXCEPTION)); |
| 128 | + } |
| 129 | + }); |
| 130 | + } |
| 131 | + |
| 132 | + public void confirm(string options) |
| 133 | + { |
| 134 | + string[] args = JSON.JsonHelper.Deserialize<string[]>(options); |
| 135 | + AlertOptions alertOpts = new AlertOptions(); |
| 136 | + alertOpts.message = args[0]; |
| 137 | + alertOpts.title = args[1]; |
| 138 | + alertOpts.buttonLabel = args[2]; |
| 139 | + string aliasCurrentCommandCallbackId = args[3]; |
| 140 | + |
| 141 | + Deployment.Current.Dispatcher.BeginInvoke(() => |
| 142 | + { |
| 143 | + PhoneApplicationPage page = Page; |
| 144 | + if (page != null) |
| 145 | + { |
| 146 | + Grid grid = page.FindName("LayoutRoot") as Grid; |
| 147 | + if (grid != null) |
| 148 | + { |
| 149 | + var previous = notifyBox; |
| 150 | + notifyBox = new NotificationBox(); |
| 151 | + notifyBox.Tag = new NotifBoxData { previous = previous, callbackId = aliasCurrentCommandCallbackId }; |
| 152 | + notifyBox.PageTitle.Text = alertOpts.title; |
| 153 | + notifyBox.SubTitle.Text = alertOpts.message; |
| 154 | + |
| 155 | + string[] labels = JSON.JsonHelper.Deserialize<string[]>(alertOpts.buttonLabel); |
| 156 | + |
| 157 | + if (labels == null) |
| 158 | + { |
| 159 | + labels = alertOpts.buttonLabel.Split(','); |
| 160 | + } |
| 161 | + |
| 162 | + for (int n = 0; n < labels.Length; n++) |
| 163 | + { |
| 164 | + Button btn = new Button(); |
| 165 | + btn.Content = labels[n]; |
| 166 | + btn.Tag = n; |
| 167 | + btn.Click += new RoutedEventHandler(btnOK_Click); |
| 168 | + notifyBox.ButtonPanel.Children.Add(btn); |
| 169 | + } |
| 170 | + |
| 171 | + grid.Children.Add(notifyBox); |
| 172 | + if (previous == null) |
| 173 | + { |
| 174 | + page.BackKeyPress += page_BackKeyPress; |
| 175 | + } |
| 176 | + } |
| 177 | + } |
| 178 | + else |
| 179 | + { |
| 180 | + DispatchCommandResult(new PluginResult(PluginResult.Status.INSTANTIATION_EXCEPTION)); |
| 181 | + } |
| 182 | + }); |
| 183 | + } |
| 184 | + |
| 185 | + void page_BackKeyPress(object sender, System.ComponentModel.CancelEventArgs e) |
| 186 | + { |
| 187 | + PhoneApplicationPage page = sender as PhoneApplicationPage; |
| 188 | + string callbackId = ""; |
| 189 | + if (page != null && notifyBox != null) |
| 190 | + { |
| 191 | + Grid grid = page.FindName("LayoutRoot") as Grid; |
| 192 | + if (grid != null) |
| 193 | + { |
| 194 | + grid.Children.Remove(notifyBox); |
| 195 | + NotifBoxData notifBoxData = notifyBox.Tag as NotifBoxData; |
| 196 | + notifyBox = notifBoxData.previous as NotificationBox; |
| 197 | + callbackId = notifBoxData.callbackId as string; |
| 198 | + } |
| 199 | + if (notifyBox == null) |
| 200 | + { |
| 201 | + page.BackKeyPress -= page_BackKeyPress; |
| 202 | + } |
| 203 | + e.Cancel = true; |
| 204 | + } |
| 205 | + |
| 206 | + DispatchCommandResult(new PluginResult(PluginResult.Status.OK, 0), callbackId); |
| 207 | + } |
| 208 | + |
| 209 | + void btnOK_Click(object sender, RoutedEventArgs e) |
| 210 | + { |
| 211 | + Button btn = sender as Button; |
| 212 | + FrameworkElement notifBoxParent = null; |
| 213 | + int retVal = 0; |
| 214 | + string callbackId = ""; |
| 215 | + if (btn != null) |
| 216 | + { |
| 217 | + retVal = (int)btn.Tag + 1; |
| 218 | + |
| 219 | + notifBoxParent = btn.Parent as FrameworkElement; |
| 220 | + while ((notifBoxParent = notifBoxParent.Parent as FrameworkElement) != null && |
| 221 | + !(notifBoxParent is NotificationBox)) ; |
| 222 | + } |
| 223 | + if (notifBoxParent != null) |
| 224 | + { |
| 225 | + PhoneApplicationPage page = Page; |
| 226 | + if (page != null) |
| 227 | + { |
| 228 | + Grid grid = page.FindName("LayoutRoot") as Grid; |
| 229 | + if (grid != null) |
| 230 | + { |
| 231 | + grid.Children.Remove(notifBoxParent); |
| 232 | + } |
| 233 | + |
| 234 | + NotifBoxData notifBoxData = notifBoxParent.Tag as NotifBoxData; |
| 235 | + notifyBox = notifBoxData.previous as NotificationBox; |
| 236 | + callbackId = notifBoxData.callbackId as string; |
| 237 | + |
| 238 | + if (notifyBox == null) |
| 239 | + { |
| 240 | + page.BackKeyPress -= page_BackKeyPress; |
| 241 | + } |
| 242 | + } |
| 243 | + |
| 244 | + } |
| 245 | + DispatchCommandResult(new PluginResult(PluginResult.Status.OK, retVal), callbackId); |
| 246 | + } |
| 247 | + |
| 248 | + |
| 249 | + |
| 250 | + public void beep(string options) |
| 251 | + { |
| 252 | + string[] args = JSON.JsonHelper.Deserialize<string[]>(options); |
| 253 | + int times = int.Parse(args[0]); |
| 254 | + |
| 255 | + string resourcePath = BaseCommand.GetBaseURL() + "resources/notification-beep.wav"; |
| 256 | + |
| 257 | + StreamResourceInfo sri = Application.GetResourceStream(new Uri(resourcePath, UriKind.Relative)); |
| 258 | + |
| 259 | + if (sri != null) |
| 260 | + { |
| 261 | + SoundEffect effect = SoundEffect.FromStream(sri.Stream); |
| 262 | + SoundEffectInstance inst = effect.CreateInstance(); |
| 263 | + ThreadPool.QueueUserWorkItem((o) => |
| 264 | + { |
| 265 | + // cannot interact with UI !! |
| 266 | + do |
| 267 | + { |
| 268 | + inst.Play(); |
| 269 | + Thread.Sleep(effect.Duration + TimeSpan.FromMilliseconds(100)); |
| 270 | + } |
| 271 | + while (--times > 0); |
| 272 | + |
| 273 | + }); |
| 274 | + |
| 275 | + } |
| 276 | + |
| 277 | + // TODO: may need a listener to trigger DispatchCommandResult after the alarm has finished executing... |
| 278 | + DispatchCommandResult(); |
| 279 | + } |
| 280 | + |
| 281 | + // Display an indeterminate progress indicator |
| 282 | + public void activityStart(string unused) |
| 283 | + { |
| 284 | + |
| 285 | + Deployment.Current.Dispatcher.BeginInvoke(() => |
| 286 | + { |
| 287 | + PhoneApplicationFrame frame = Application.Current.RootVisual as PhoneApplicationFrame; |
| 288 | + |
| 289 | + if (frame != null) |
| 290 | + { |
| 291 | + PhoneApplicationPage page = frame.Content as PhoneApplicationPage; |
| 292 | + |
| 293 | + if (page != null) |
| 294 | + { |
| 295 | + var temp = page.FindName("LayoutRoot"); |
| 296 | + Grid grid = temp as Grid; |
| 297 | + if (grid != null) |
| 298 | + { |
| 299 | + if (progressBar != null) |
| 300 | + { |
| 301 | + grid.Children.Remove(progressBar); |
| 302 | + } |
| 303 | + progressBar = new ProgressBar(); |
| 304 | + progressBar.IsIndeterminate = true; |
| 305 | + progressBar.IsEnabled = true; |
| 306 | + |
| 307 | + grid.Children.Add(progressBar); |
| 308 | + } |
| 309 | + } |
| 310 | + } |
| 311 | + }); |
| 312 | + } |
| 313 | + |
| 314 | + |
| 315 | + // Remove our indeterminate progress indicator |
| 316 | + public void activityStop(string unused) |
| 317 | + { |
| 318 | + Deployment.Current.Dispatcher.BeginInvoke(() => |
| 319 | + { |
| 320 | + if (progressBar != null) |
| 321 | + { |
| 322 | + progressBar.IsEnabled = false; |
| 323 | + PhoneApplicationFrame frame = Application.Current.RootVisual as PhoneApplicationFrame; |
| 324 | + if (frame != null) |
| 325 | + { |
| 326 | + PhoneApplicationPage page = frame.Content as PhoneApplicationPage; |
| 327 | + if (page != null) |
| 328 | + { |
| 329 | + Grid grid = page.FindName("LayoutRoot") as Grid; |
| 330 | + if (grid != null) |
| 331 | + { |
| 332 | + grid.Children.Remove(progressBar); |
| 333 | + } |
| 334 | + } |
| 335 | + } |
| 336 | + progressBar = null; |
| 337 | + } |
| 338 | + }); |
| 339 | + } |
| 340 | + |
| 341 | + public void vibrate(string vibrateDuration) |
| 342 | + { |
| 343 | + |
| 344 | + int msecs = 200; // set default |
| 345 | + |
| 346 | + try |
| 347 | + { |
| 348 | + string[] args = JSON.JsonHelper.Deserialize<string[]>(vibrateDuration); |
| 349 | + |
| 350 | + msecs = int.Parse(args[0]); |
| 351 | + if (msecs < 1) |
| 352 | + { |
| 353 | + msecs = 1; |
| 354 | + } |
| 355 | + } |
| 356 | + catch (FormatException) |
| 357 | + { |
| 358 | + |
| 359 | + } |
| 360 | + |
| 361 | + VibrateController.Default.Start(TimeSpan.FromMilliseconds(msecs)); |
| 362 | + |
| 363 | + // TODO: may need to add listener to trigger DispatchCommandResult when the vibration ends... |
| 364 | + DispatchCommandResult(); |
| 365 | + } |
| 366 | + } |
| 367 | +} |
0 commit comments