예제 #1
0
        private async Task <bool> DoSendFile(RemoteDevice.Connection connection, string path)
        {
            try {
                using (var fileStream = new FileStream(path, FileMode.Open)) {
                    var fileName = Path.GetFileName(path);
                    await connection.SendJson(new JObject {
                        ["type"] = "file",
                        ["name"] = fileName,
                        ["size"] = fileStream.Length
                    });

                    var notification = new SystemNotification {
                        Title = fileName,
                        Text  = string.Format(Properties.Resources.SendingFileTo, _device.Name)
                    };
                    await App.RunOnUiThreadAsync(() => { App.SystemNotifier.ShowNotification(notification); });

                    try {
                        var buffer = new byte[SendBufferSize];
                        int count;
                        do
                        {
                            count = await fileStream.ReadAsync(buffer, 0, buffer.Length);

                            if (_cancel)
                            {
                                throw new CancellationException();
                            }
                            var base64String = Convert.ToBase64String(buffer, 0, count);
                            await connection.SendJson(new JObject {
                                ["type"]  = "file",
                                ["chunk"] = base64String
                            });
                        } while (count == buffer.Length);

                        await connection.SendJson(new JObject {
                            ["type"]   = "file",
                            ["status"] = "complete"
                        });
                    } catch (Exception) {
                        try {
                            await connection.SendJson(new JObject {
                                ["type"]   = "file",
                                ["status"] = "cancel"
                            });
                        }
                        catch (Exception) {
                            // Ignore
                        }

                        throw;
                    } finally {
                        await App.RunOnUiThreadAsync(() => {
                            App.SystemNotifier.DismissNotification(notification.Tag);
                        });
                    }

                    await App.RunOnUiThreadAsync(() => {
                        App.SystemNotifier.ShowNotification(new SystemNotification {
                            Title = fileName,
                            Text  = string.Format(Properties.Resources.FileSentTo, _device.Name)
                        });
                    });
                }
            } catch (IOException e) {
                await App.RunOnUiThreadAsync(() => {
                    App.SystemNotifier.ShowNotification(new SystemNotification {
                        Title = _device.Name,
                        Text  = string.Format(Properties.Resources.UnableToSendFile, e.Message)
                    });
                });

                return(false);
            } catch (CancellationException) {
                return(false);
            }
            return(true);
        }
예제 #2
0
        public async Task HandleJson(RemoteDevice.Connection connection, dynamic json)
        {
            var    device = connection.RemoteDevice;
            string action = json.action;

            switch (action)
            {
            case "posted": {
                string key        = json.key;
                long   timestamp  = json.timestamp;
                string appName    = json.appName;
                string title      = json.title;
                string message    = json.message;
                string base64Icon = json.icon;
                JArray actions    = json.actions;
                var    pngIcon    = base64Icon != null?Convert.FromBase64String(base64Icon) : null;

                var dateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
                dateTime = dateTime.AddMilliseconds(timestamp);
                await Application.Current.Dispatcher.InvokeAsync(() => {
                        BitmapImage icon = null;
                        if (pngIcon != null)
                        {
                            using (var stream = new MemoryStream(pngIcon)) {
                                icon = new BitmapImage();
                                icon.BeginInit();
                                icon.StreamSource = stream;
                                icon.CacheOption  = BitmapCacheOption.OnLoad;
                                icon.EndInit();
                            }
                        }

                        var item = new NotificationItem(key, appName, title, message, icon, dateTime)
                        {
                            Actions = actions.Select(act => {
                                dynamic a        = act;
                                int index        = a.index;
                                string aTitle    = a.title;
                                bool isTextInput = a.isTextInput;
                                return(new NotificationItem.Action(index, aTitle, isTextInput));
                            }).ToArray()
                        };
                        NotificationItem oldNotificationItem = null;
                        int i;
                        for (i = 0; i < _notifications.Count; i++)
                        {
                            if (_notifications[i].Key != item.Key)
                            {
                                continue;
                            }
                            oldNotificationItem = _notifications[i];
                            _notifications[i]   = item;
                            break;
                        }
                        if (i == _notifications.Count)
                        {
                            _notifications.Add(item);
                        }

                        var systemNotification = new SystemNotification {
                            AppName   = item.AppName,
                            Title     = item.Title,
                            Text      = item.Message,
                            IconData  = pngIcon,
                            Timestamp = item.Timestamp,
                            Actions   = item.Actions.Select(act => new SystemNotification.Action {
                                Index       = act.Index,
                                Title       = act.Title,
                                IsTextInput = act.IsTextInput
                            }).ToArray()
                        };

                        /* systemNotification.Activated += sender => {
                         *      if (Settings.Default.DismissNotificationsByClick) {
                         *              Task.Run(async () => {
                         *                      await connection.SendJson(new JObject {
                         *                              ["type"] = "notification",
                         *                              ["key"] = key
                         *                      });
                         *              });
                         *      } else {
                         *              App.ShowDeviceWindow(device);
                         *      }
                         * }; */
                        systemNotification.ActionActivated += (sender, index, text) => {
                            Task.Run(async() => {
                                await connection.SendJson(new JObject {
                                    ["type"]        = "notification",
                                    ["key"]         = key,
                                    ["actionIndex"] = index,
                                    ["actionText"]  = text
                                });
                            });
                        };
                        if (oldNotificationItem?.SystemNotificationTag != null)
                        {
                            systemNotification.Tag = oldNotificationItem.SystemNotificationTag;
                        }
                        App.SystemNotifier.ShowNotification(systemNotification);
                        item.SystemNotificationTag = systemNotification.Tag;
                    });

                break;
            }

            case "removed": {
                string key = json.key;
                await Application.Current.Dispatcher.InvokeAsync(() => {
                        for (var i = 0; i < _notifications.Count; i++)
                        {
                            var notification = _notifications[i];
                            if (notification.Key != key)
                            {
                                continue;
                            }
                            _notifications.RemoveAt(i);
                            if (notification.SystemNotificationTag != null)
                            {
                                App.SystemNotifier.DismissNotification(notification.SystemNotificationTag);
                            }
                            break;
                        }
                    });

                break;
            }
            }
        }