1 using Microsoft.Phone.Networking.Voip;
2 using Microsoft.Phone.Scheduler;
3 using System;
4 using System.Diagnostics;
5 
6 namespace Mediastreamer2.Agents
7 {
8     public class Mediastreamer2ScheduledAgent : ScheduledTaskAgent
9     {
10         // Indicates if this agent instance is handling an incoming call or not
11         private bool isIncomingCallAgent;
12 
Mediastreamer2ScheduledAgent()13         public Mediastreamer2ScheduledAgent()
14         {
15 
16         }
17 
OnInvoke(ScheduledTask task)18         protected override void OnInvoke(ScheduledTask task)
19         {
20             Debug.WriteLine("[Mediastreamer2ScheduledAgent] ScheduledAgentImpl has been invoked with argument of type {0}.", task.GetType());
21             VoipHttpIncomingCallTask incomingCallTask = task as VoipHttpIncomingCallTask;
22             if (incomingCallTask != null)
23             {
24                 this.isIncomingCallAgent = true;
25                 Debug.WriteLine("[IncomingCallAgent] Received VoIP Incoming Call task");
26             }
27             else
28             {
29                 VoipKeepAliveTask keepAliveTask = task as VoipKeepAliveTask;
30                 if (keepAliveTask != null)
31                 {
32                     this.isIncomingCallAgent = false;
33                     Debug.WriteLine("[KeepAliveAgent] Keep Alive task");
34                     base.NotifyComplete();
35                 }
36                 else
37                 {
38                     throw new InvalidOperationException(string.Format("Unknown scheduled task type {0}", task.GetType()));
39                 }
40             }
41         }
42 
43         // This method is called when the incoming call processing is complete to kill the background process if needed
OnIncomingCallDialogDismissed()44         private void OnIncomingCallDialogDismissed()
45         {
46             Debug.WriteLine("[IncomingCallAgent] Incoming call processing is now complete.");
47             base.NotifyComplete();
48         }
49 
OnCancel()50         protected override void OnCancel()
51         {
52             Debug.WriteLine("[{0}] Cancel requested.", this.isIncomingCallAgent ? "IncomingCallAgent" : "KeepAliveAgent");
53             base.NotifyComplete();
54         }
55     }
56 }
57