Example #1
0
        private void imageSelectionTask_Completed(object sender, PhotoResult e)
        {
            if (e.TaskResult == TaskResult.OK)
            {
                string fileName = e.OriginalFileName.Substring(e.OriginalFileName.LastIndexOf("\\") + 1);
                BitmapImage image = new BitmapImage();
                image.SetSource(e.ChosenPhoto);

                ChatSettingsManager chatMgr = new ChatSettingsManager();
                chatMgr.Load();
                if ((bool)chatMgr.ScaleDownSentPictures)
                {
                    // Resize down the image
                    WriteableBitmap bm = new WriteableBitmap(image);
                    MemoryStream ms = new MemoryStream();
                    int w = image.PixelWidth;
                    int h = image.PixelHeight;
                    if (w > h && w > 500)
                    {
                        h = (500 * h) / w;
                        w = 500;
                    }
                    else if (h > w && h > 500)
                    {
                        w = (500 * w) / h;
                        h = 500;
                    }
                    bm.SaveJpeg(ms, w, h, 0, 100);
                    image.SetSource(ms);
                }
                string filePath = Utils.SaveImageInLocalFolder(image, fileName);
                MessageBox.SetImage(image);
                MessageBox.ImageName = fileName;
                MessageBox.ImageLocalPath = filePath;
                RefreshSendMessageButtonEnabledState();
            }
        }
        /// <summary>
        /// Callback for LinphoneCoreListener
        /// </summary>
        public void MessageReceived(LinphoneChatMessage message)
        {
            if (BaseModel.UIDispatcher == null) return;
            BaseModel.UIDispatcher.BeginInvoke(() =>
            {
                LinphoneAddress fromAddress = message.From;
                string sipAddress = String.Format("{0}@{1}", fromAddress.UserName, fromAddress.Domain);
                Logger.Msg("[LinphoneManager] Message received from " + sipAddress + ": " + message.Text + "\r\n");

                //Vibrate
                ChatSettingsManager settings = new ChatSettingsManager();
                settings.Load();
                if ((bool)settings.VibrateOnIncomingMessage)
                {
                    VibrationDevice vibrator = VibrationDevice.GetDefault();
                    vibrator.Vibrate(TimeSpan.FromSeconds(1));
                }

                if (MessageListener != null && MessageListener.GetSipAddressAssociatedWithDisplayConversation() != null && MessageListener.GetSipAddressAssociatedWithDisplayConversation().Equals(sipAddress))
                {
                    MessageListener.MessageReceived(message);
                }
                else
                {
                    DateTime date = new DateTime(message.Time * TimeSpan.TicksPerSecond, DateTimeKind.Utc).AddYears(1969).ToLocalTime();
                    DateTime now = DateTime.Now;
                    string dateStr;
                    if (now.Year == date.Year && now.Month == date.Month && now.Day == date.Day)
                        dateStr = String.Format("{0:HH:mm}", date);
                    else if (now.Year == date.Year)
                        dateStr = String.Format("{0:ddd d MMM, HH:mm}", date);
                    else
                        dateStr = String.Format("{0:ddd d MMM yyyy, HH:mm}", date);

                    //TODO: Temp hack to remove
                    string url = message.ExternalBodyUrl;
                    url = url.Replace("\"", "");

                    //Displays the message as a popup
                    if (MessageReceivedNotification != null)
                    {
                        MessageReceivedNotification.Dismiss();
                    }

                    MessageReceivedNotification = new CustomMessageBox()
                    {
                        Title = dateStr,
                        Caption = url.Length > 0 ? AppResources.ImageMessageReceived : AppResources.MessageReceived,
                        Message = url.Length > 0 ? "" : message.Text,
                        LeftButtonContent = AppResources.Close,
                        RightButtonContent = AppResources.Show
                    };

                    MessageReceivedNotification.Dismissed += (s, e) =>
                    {
                        switch (e.Result)
                        {
                            case CustomMessageBoxResult.RightButton:
                                BaseModel.CurrentPage.NavigationService.Navigate(new Uri("/Views/Chat.xaml?sip=" + Utils.ReplacePlusInUri(message.PeerAddress.AsStringUriOnly()), UriKind.RelativeOrAbsolute));
                                break;
                        }
                    };

                    MessageReceivedNotification.Show();
                }
            });
        }