summaryrefslogtreecommitdiff
path: root/pjsip-apps/src/pjsua/winrt/gui/uwp/VoipTasks/AppService.cs
blob: 87607733a7ee983d138a859e6fbbd8dff85a7f99 (plain)
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
//*********************************************************
//
// Copyright (c) Microsoft. All rights reserved.
// This code is licensed under the MIT License (MIT).
// THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
// IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
// PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************
using System;
using VoipTasks.BackgroundOperations;
using VoipTasks.Helpers;
using Windows.ApplicationModel.AppService;
using Windows.ApplicationModel.Background;
using Windows.ApplicationModel.Calls;
using Windows.Foundation.Collections;
using VoipBackEnd;

namespace VoipTasks
{
    public sealed class AppService : IBackgroundTask
    {
        AppServiceConnection _connection;
        BackgroundTaskDeferral _deferral;

        public void Run(IBackgroundTaskInstance taskInstance)
        {
            AppServiceTriggerDetails triggerDetail = taskInstance.TriggerDetails as AppServiceTriggerDetails;
            _deferral = taskInstance.GetDeferral();

            // Register for Task Cancel callback
            taskInstance.Canceled += TaskInstance_Canceled;

            AppServiceConnection connection = triggerDetail.AppServiceConnection;
            _connection = connection;

            connection.RequestReceived += Connection_RequestReceived;
            Current.AppConnection = connection;
        }

        private async void Connection_RequestReceived(AppServiceConnection sender, AppServiceRequestReceivedEventArgs args)
        {
            var deferral = args.GetDeferral();
            var response = new ValueSet();
            bool stop = false;
            try
            {
                var request = args.Request;
                var message = request.Message;
                if (message.ContainsKey(BackgroundOperation.NewBackgroundRequest))
                {
                    switch ((BackgroundRequest)message[BackgroundOperation.NewBackgroundRequest])
                    {
                        case BackgroundRequest.NewOutgoingCall:
                            Current.AppRequest = args.Request;
                            Current.Request = BackgroundRequest.NewOutgoingCall;
                            Current.AppRequestDeferal = deferral;
                            await Current.RequestNewCallAsync(message[NewCallArguments.DstURI.ToString()] as String);
                            break;

                        case BackgroundRequest.EndCall:
                            Current.AppRequest = args.Request;
                            Current.Request = BackgroundRequest.EndCall;
                            Current.AppRequestDeferal = deferral;
                            Current.EndCallAsync();
                            break;

                        case BackgroundRequest.StartService:
                            Current.AppRequest = args.Request;
                            Current.Request = BackgroundRequest.StartService;
                            Current.AppRequestDeferal = deferral;
                            Current.StartService();                            

                            break;
                        case BackgroundRequest.StopService:
                            Current.AppRequest = args.Request;
                            Current.Request = BackgroundRequest.StopService;
                            Current.AppRequestDeferal = deferral;
                            Current.StopService();
                            break;

                        case BackgroundRequest.GetAccountInfo:
                            Current.AppRequest = args.Request;
                            Current.Request = BackgroundRequest.GetAccountInfo;
                            Current.AppRequestDeferal = deferral;

                            Current.GetAccountInfo();
                            break;

                        case BackgroundRequest.ModifyAccount:
                            Current.AppRequest = args.Request;
                            Current.Request = BackgroundRequest.ModifyAccount;
                            Current.AppRequestDeferal = deferral;
                            Current.ModifyAccount(message[ModifyAccountArguments.id.ToString()] as String,
                                    message[ModifyAccountArguments.registrar.ToString()] as String,
                                    message[ModifyAccountArguments.proxy.ToString()] as String,
                                    message[ModifyAccountArguments.username.ToString()] as String,
                                    message[ModifyAccountArguments.password.ToString()] as String);

                            break;

                        default:
                            stop = true;
                            break;
                    }
                }
            }
            finally
            {
               
                if (stop)
                {
                    _deferral.Complete();
                }
            }
        }

        private void TaskInstance_Canceled(IBackgroundTaskInstance sender, BackgroundTaskCancellationReason reason)
        {
            if (_deferral != null)
            {
                _deferral.Complete();
            }
        }

        private static void Call_ResumeRequested(VoipPhoneCall sender, CallStateChangeEventArgs args)
        {
            sender.NotifyCallActive();
        }

        private static void Call_RejectRequested(VoipPhoneCall sender, CallRejectEventArgs args)
        {
            sender.NotifyCallEnded();
        }

        private static void Call_HoldRequested(VoipPhoneCall sender, CallStateChangeEventArgs args)
        {
            sender.NotifyCallHeld();
        }

        private static void Call_EndRequested(VoipPhoneCall sender, CallStateChangeEventArgs args)
        {
            sender.NotifyCallEnded();
        }

        private static void Call_AnswerRequested(VoipPhoneCall sender, CallAnswerEventArgs args)
        {
            sender.NotifyCallActive();
        }
    }
}