IncomingCall.xaml.cs 4.29 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Linphone.Model;
using System.Threading.Tasks;

namespace Linphone.Views
{
    /// <summary>
    /// View to use instead of Windows Phone 8 VoIP incoming call view
    /// </summary>
    public partial class IncomingCall : PhoneApplicationPage
    {
        private String _callingNumber;

        /// <summary>
        /// Public constructor.
        /// </summary>
        public IncomingCall()
        {
            InitializeComponent();
28

29
            if (!LinphoneManager.Instance.LinphoneCore.VideoSupported || !LinphoneManager.Instance.LinphoneCore.VideoEnabled)
30 31 32
            {
                AnswerVideo.Visibility = Visibility.Collapsed;
            }
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
        }

        /// <summary>
        /// Forbid the user to back when this view is visible
        /// </summary>
        protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
        {
            e.Cancel = true;
            base.OnBackKeyPress(e);
        }

        /// <summary>
        /// Remove this entry from the back stack to ensure the user won't navigate to it with the back button
        /// </summary>
        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
49 50 51 52 53 54
            if (!e.IsNavigationInitiator)
            {
                //if we leave the application, we consider it as a call rejection
                LinphoneManager.Instance.EndCurrentCall();
            }

55 56 57 58 59 60 61 62
            base.OnNavigatedFrom(e);
            NavigationService.RemoveBackEntry(); //To prevent a new click on back button to start again the incoming call view (simulate a back click)
        }

        /// <summary>
        /// Method called when the page is displayed.
        /// Searches for a matching contact using the current call address or number and display information if found.
        /// </summary>
Sylvain Berfini's avatar
Sylvain Berfini committed
63
        protected override void OnNavigatedTo(NavigationEventArgs nee)
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
        {
            base.OnNavigatedTo(nee);

            if (NavigationContext.QueryString.ContainsKey("sip"))
            {
                _callingNumber = NavigationContext.QueryString["sip"];
                if (_callingNumber.StartsWith("sip:"))
                {
                    _callingNumber = _callingNumber.Substring(4);
                }
                // While we dunno if the number matches a contact one, we consider it won't and we display the phone number as username
                Contact.Text = _callingNumber;

                if (_callingNumber != null && _callingNumber.Length > 0)
                {
                    ContactManager cm = ContactManager.Instance;
                    cm.ContactFound += cm_ContactFound;
                    cm.FindContact(_callingNumber);
                }
            }
        }

        /// <summary>
        /// Callback called when the search on a phone number for a contact has a match
        /// </summary>
        private void cm_ContactFound(object sender, ContactFoundEventArgs e)
        {
            if (e.ContactFound != null)
            {
                Contact.Text = e.ContactFound.DisplayName;
                if (e.PhoneLabel != null)
                {
                    Number.Text = e.PhoneLabel + " : " + e.PhoneNumber;
                }
                else
                {
                    Number.Text = e.PhoneNumber;
                }
            }
        }

        private void Answer_Click(object sender, RoutedEventArgs e)
        {
107
            LinphoneManager.Instance.LinphoneCore.AcceptCall(LinphoneManager.Instance.LinphoneCore.CurrentCall);
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
            NavigationService.Navigate(new Uri("/Views/InCall.xaml?sip=" + _callingNumber, UriKind.RelativeOrAbsolute));
        }

        private void AnswerVideo_Click(object sender, RoutedEventArgs e)
        {
        }

        private void Decline_Click(object sender, RoutedEventArgs e)
        {
            LinphoneManager.Instance.EndCurrentCall();
            if (NavigationService.CanGoBack)
            {
                NavigationService.GoBack();
            }
        }
    }
}