LinphoneScheduledAgent.cs 7.31 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
/*
LinphoneScheduledAgent.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 Linphone.Core;
18
using Linphone.Core.OutOfProcess;
19
using Microsoft.Phone.Net.NetworkInformation;
Sylvain Berfini's avatar
Sylvain Berfini committed
20 21
using Microsoft.Phone.Networking.Voip;
using Microsoft.Phone.Scheduler;
22
using System;
Sylvain Berfini's avatar
Sylvain Berfini committed
23
using System.Diagnostics;
24
using Windows.Storage;
25 26 27

namespace Linphone.Agents
{
28
    public class LinphoneScheduledAgent : ScheduledTaskAgent, LinphoneCoreListener
29 30 31 32 33 34 35
    {
        // Indicates if this agent instance is handling an incoming call or not
        private bool isIncomingCallAgent;

        public LinphoneScheduledAgent()
        {

36 37 38 39 40
        }

        protected override void OnInvoke(ScheduledTask task)
        {
            Debug.WriteLine("[LinphoneScheduledAgent] ScheduledAgentImpl has been invoked with argument of type {0}.", task.GetType());
Ghislain MARY's avatar
Ghislain MARY committed
41
            AgentHost.OnAgentStarted();
42 43 44 45
            VoipHttpIncomingCallTask incomingCallTask = task as VoipHttpIncomingCallTask;
            if (incomingCallTask != null)
            {
                this.isIncomingCallAgent = true;
46 47
                Debug.WriteLine("[IncomingCallAgent] Received VoIP Incoming Call task");

Sylvain Berfini's avatar
Sylvain Berfini committed
48
                BackgroundManager.Instance.OopServer.CallController.IncomingCallViewDismissed = OnIncomingCallDialogDismissed;
49
                BackgroundManager.Instance.InitLinphoneCore();
50 51 52 53 54 55 56
            }
            else
            {
                VoipKeepAliveTask keepAliveTask = task as VoipKeepAliveTask;
                if (keepAliveTask != null)
                {
                    this.isIncomingCallAgent = false;
57 58 59 60 61
                    Debug.WriteLine("[KeepAliveAgent] Keep Alive task");

                    if (DeviceNetworkInformation.IsNetworkAvailable)
                    {
                        var server = BackgroundManager.Instance.OopServer;
62
                        InitManager.CreateLinphoneCore(server, this, OutputTraceLevel.Message);
63
                        server.LinphoneCore.NetworkReachable = true;
64
                        server.LinphoneCore.IterateEnabled = true;
65 66 67 68 69 70 71
                        Debug.WriteLine("[KeepAliveAgent] Linphone Core created");
                    }
                    else
                    {
                        Debug.WriteLine("[KeepAliveAgent] Not connected, can't refresh register");
                        base.NotifyComplete();
                    }
72 73 74 75 76 77
                }
                else
                {
                    throw new InvalidOperationException(string.Format("Unknown scheduled task type {0}", task.GetType()));
                }
            }
78 79
        }

80
        // This method is called when the incoming call processing is complete to kill the background process if needed
81 82 83
        private void OnIncomingCallDialogDismissed()
        {
            Debug.WriteLine("[IncomingCallAgent] Incoming call processing is now complete.");
Sylvain Berfini's avatar
Sylvain Berfini committed
84
            BackgroundManager.Instance.OopServer.CallController.IncomingCallViewDismissed = null;
85

86
            TileManager.Instance.UpdateCount(BackgroundManager.Instance.OopServer.LinphoneCore.MissedCallsCount);
87

88
            if (BackgroundManager.Instance.OopServer.LinphoneCore.CallsNb == 0)
89
            {
90
                BackgroundManager.Instance.OopServer.LinphoneCore.NetworkReachable = false; // To prevent the core from unregister
91 92
                BackgroundManager.Instance.OopServer.LinphoneCoreFactory.Destroy();
            }
93 94 95
            base.NotifyComplete();
        } 

96 97 98 99 100 101
        protected override void OnCancel()
        {
            Debug.WriteLine("[{0}] Cancel requested.", this.isIncomingCallAgent ? "IncomingCallAgent" : "KeepAliveAgent");

            base.NotifyComplete();
        }
102 103 104 105 106

        #region LinphoneCoreListener Callbacks
        /// <summary>
        /// Callback for LinphoneCoreListener
        /// </summary>
Sylvain Berfini's avatar
Sylvain Berfini committed
107
        public void AuthInfoRequested(string realm, string username, string domain)
108 109 110 111 112 113 114 115 116 117 118 119 120
        {
        }

        /// <summary>
        /// Callback for LinphoneCoreListener
        /// </summary>
        public void GlobalState(GlobalState state, string message)
        {
        }

        /// <summary>
        /// Callback for LinphoneCoreListener
        /// </summary>
121
        public void CallState(LinphoneCall call, LinphoneCallState state, string message)
122 123 124 125 126 127 128 129
        {
        }

        /// <summary>
        /// Callback for LinphoneCoreListener
        /// </summary>
        public void RegistrationState(LinphoneProxyConfig config, RegistrationState state, string message)
        {
130
            if (state == Linphone.Core.RegistrationState.RegistrationOk || state == Linphone.Core.RegistrationState.RegistrationFailed)
131
            {
132
                Debug.WriteLine("[KeepAliveAgent] Notify complete (" + state.ToString() + ")");
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
                base.NotifyComplete();
            }
        }

        /// <summary>
        /// Callback for LinphoneCoreListener
        /// </summary>
        public void DTMFReceived(LinphoneCall call, Char dtmf)
        {
        }

        /// <summary>
        /// Callback for LinphoneCoreListener
        /// </summary>
        public void EcCalibrationStatus(EcCalibratorStatus status, int delayMs)
        {
        }

        /// <summary>
        /// Callback for LinphoneCoreListener
        /// </summary>
        public void CallEncryptionChanged(LinphoneCall call, bool encrypted, string authenticationToken)
        {
        }

        /// <summary>
        /// Callback for LinphoneCoreListener
        /// </summary>
        public void CallStatsUpdated(LinphoneCall call, LinphoneCallStats stats)
        {
        }

        /// <summary>
        /// Callback for LinphoneCoreListener
        /// </summary>
        public void MessageReceived(LinphoneChatMessage message)
        {
        }
171 172 173 174 175 176 177

        /// <summary>
        /// Callback for LinphoneCoreListener
        /// </summary>
        public void ComposingReceived(LinphoneChatRoom room)
        {
        }
178

179 180 181 182 183 184 185
        /// <summary>
        /// Callback for LinphoneCoreListener
        /// </summary>
        public void FileTransferProgressIndication(LinphoneChatMessage message, int offset, int total)
        {
        }

186 187 188
        /// <summary>
        /// Callback for LinphoneCoreListener
        /// </summary>
189
        public void LogUploadStatusChanged(LinphoneCoreLogCollectionUploadState state, string info)
190 191
        {
        }
192 193 194 195

        /// <summary>
        /// Callback for LinphoneCoreListener
        /// </summary>
196
        public void LogUploadProgressIndication(int offset, int total)
197 198
        {
        }
199
        #endregion
200 201
    }
}