App.xaml.cs 8.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
/*
App.xaml.cs
Copyright (C) 2015  Belledonne Communications, Grenoble, France
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
14
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
15 16 17
*/

using System;
18 19 20 21 22
using Windows.ApplicationModel;
using Windows.ApplicationModel.Activation;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;
23 24 25 26
using BelledonneCommunications.Linphone.Native;
using Linphone.Model;
using System.Diagnostics;
using Windows.UI.Core;
27
using System.Collections.Generic;
28 29 30 31 32 33

namespace Linphone
{
    /// <summary>
    /// Provides application-specific behavior to supplement the default Application class.
    /// </summary>
34
    sealed partial class App : Application, CallControllerListener
35
    {
36
        Frame rootFrame;
37 38
        bool acceptCall;
        String sipAddress;
39

40 41 42 43 44 45 46
        /// <summary>
        /// Initializes the singleton application object.  This is the first line of authored code
        /// executed, and as such is the logical equivalent of main() or WinMain().
        /// </summary>
        public App()
        {
            this.InitializeComponent();
47
            this.UnhandledException += App_UnhandledException; ;
48
            this.Suspending += OnSuspending;
49
            SettingsManager.InstallConfigFile();
50
            acceptCall = false;
51 52 53 54 55
        }

        private void App_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            e.Handled = true;
Margaux Clerc's avatar
Margaux Clerc committed
56
        }
57 58 59 60 61 62 63

        private void Back_requested(object sender, BackRequestedEventArgs e)
        {
            if (rootFrame.CanGoBack)
            {
                rootFrame.GoBack();
                e.Handled = true;
64
            }    
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
        }

        public void CallEnded(Call call)
        {
            Debug.WriteLine("[CallListener] Call ended, can go back ? " + rootFrame.CanGoBack);
            

            if (rootFrame.CanGoBack)
            {
                rootFrame.GoBack();
            }
            else
            {
                // Launch the Dialer and remove the incall view from the backstack
                rootFrame.Navigate(typeof(Views.Dialer), null);
Margaux Clerc's avatar
Margaux Clerc committed
80 81 82 83
                if (rootFrame.BackStack.Count > 0)
                {
                    rootFrame.BackStack.Clear();
                }
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
            }
        }

        public void CallUpdatedByRemote(Call call, bool isVideoAdded)
        {
            //throw new NotImplementedException();
        }

        public void MuteStateChanged(bool isMicMuted)
        {
            //throw new NotImplementedException();
        }

        public void NewCallStarted(string callerNumber)
        {
            Debug.WriteLine("[CallListener] NewCallStarted " + callerNumber);
100 101 102
            List<String> parameters = new List<String>();
            parameters.Add(callerNumber);
            rootFrame.Navigate(typeof(Views.InCall), parameters);
103 104 105 106 107 108 109 110 111
        }

        public void PauseStateChanged(Call call, bool isCallPaused, bool isCallPausedByRemote)
        {
            Debug.WriteLine("Pausestatechanged");
           // if (this.PauseListener != null)
           //     this.PauseListener.PauseStateChanged(call, isCallPaused, isCallPausedByRemote);
        }

112 113 114 115 116 117 118
        /// <summary>
        /// Invoked when the application is launched normally by the end user.  Other entry points
        /// will be used such as when the application is launched to open a specific file.
        /// </summary>
        /// <param name="e">Details about the launch request and process.</param>
        protected override void OnLaunched(LaunchActivatedEventArgs e)
        {
119 120 121 122 123
            Initialize(e, null);
        }

        private void Initialize(IActivatedEventArgs e, String args)
        {       
124 125 126 127

#if DEBUG
            if (System.Diagnostics.Debugger.IsAttached)
            {
128
                this.DebugSettings.EnableFrameRateCounter = false;
129 130
            }
#endif
131
            //Start linphone
132 133 134
            var currentView = SystemNavigationManager.GetForCurrentView();
            currentView.AppViewBackButtonVisibility = AppViewBackButtonVisibility.Visible;

135 136
            LinphoneManager.Instance.InitLinphoneCore();
            LinphoneManager.Instance.CallListener = this;
137 138
            LinphoneManager.Instance.CoreDispatcher = Windows.ApplicationModel.Core.CoreApplication.GetCurrentView().CoreWindow.Dispatcher;
            
139 140

            rootFrame = Window.Current.Content as Frame;
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159

            // Do not repeat app initialization when the Window already has content,
            // just ensure that the window is active
            if (rootFrame == null)
            {
                // Create a Frame to act as the navigation context and navigate to the first page
                rootFrame = new Frame();

                rootFrame.NavigationFailed += OnNavigationFailed;

                if (e.PreviousExecutionState == ApplicationExecutionState.Terminated)
                {
                    //TODO: Load state from previously suspended application
                }

                // Place the frame in the current Window
                Window.Current.Content = rootFrame;
            }

160 161
            SystemNavigationManager.GetForCurrentView().BackRequested += Back_requested;

162 163
            if (rootFrame.Content == null)
            {
164 165
                if (args != null)
                {
166 167
                    if (args.StartsWith("chat"))
                    {
Margaux Clerc's avatar
Margaux Clerc committed
168 169
                        var sipAddr = args.Split('=')[1];
                        rootFrame.Navigate(typeof(Views.Chat), sipAddr);
170 171 172 173 174 175 176 177 178 179
                    }
                    else
                    {                    
                        if (args.StartsWith("answer"))
                        {
                            acceptCall = true;
                            sipAddress = args.Split('=')[1];
                        }
                        rootFrame.Navigate(typeof(Views.Dialer), null);
                    }   
180 181 182 183 184
                }
                else
                {
                    rootFrame.Navigate(typeof(Views.Dialer), args);
                }
185 186 187 188
            }
            Window.Current.Activate();
        }

Margaux Clerc's avatar
Margaux Clerc committed
189 190
        protected override void OnActivated(IActivatedEventArgs args)
        {
191
            if (args.Kind == ActivationKind.ToastNotification)
Margaux Clerc's avatar
Margaux Clerc committed
192 193 194
            {
                var toastArgs = args as ToastNotificationActivatedEventArgs;
                var arguments = toastArgs.Argument;
195
                Initialize(args, arguments);
Margaux Clerc's avatar
Margaux Clerc committed
196 197 198 199
            }

        }

200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
        /// <summary>
        /// Invoked when Navigation to a certain page fails
        /// </summary>
        /// <param name="sender">The Frame which failed navigation</param>
        /// <param name="e">Details about the navigation failure</param>
        void OnNavigationFailed(object sender, NavigationFailedEventArgs e)
        {
            throw new Exception("Failed to load Page " + e.SourcePageType.FullName);
        }

        /// <summary>
        /// Invoked when application execution is being suspended.  Application state is saved
        /// without knowing whether the application will be terminated or resumed with the contents
        /// of memory still intact.
        /// </summary>
        /// <param name="sender">The source of the suspend request.</param>
        /// <param name="e">Details about the suspend request.</param>
        private void OnSuspending(object sender, SuspendingEventArgs e)
        {
            var deferral = e.SuspendingOperation.GetDeferral();
            //TODO: Save application state and stop any background activity
            deferral.Complete();
        }
223 224 225

        public void CallIncoming(Call call)
        {
226 227 228 229 230 231 232 233
            if(acceptCall)
            {
                if (sipAddress != "")
                {
                    Address addr = LinphoneManager.Instance.Core.InterpretURL(sipAddress);
                    if (addr != null && addr.AsStringUriOnly().Equals(call.RemoteAddress.AsStringUriOnly()))
                    {
                        LinphoneManager.Instance.Core.AcceptCall(call);
234 235 236
                        List<String> parameters = new List<String>();
                        parameters.Add(call.RemoteAddress.AsString());
                        rootFrame.Navigate(typeof(Views.InCall), parameters);
237 238 239 240 241 242 243
                        acceptCall = false;
                    }
                }
            } else
            {
                rootFrame.Navigate(typeof(Views.IncomingCall), call.RemoteAddress.AsString());
            }
244
        }
245 246
    }
}