1 //*********************************************************
2 //
3 // Copyright (c) Microsoft. All rights reserved.
4 // This code is licensed under the MIT License (MIT).
5 // THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
6 // ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
7 // IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
8 // PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
9 //
10 //*********************************************************
11 using System;
12 using System.Threading.Tasks;
13 using VoipTasks.BackgroundOperations;
14 using Windows.ApplicationModel.AppService;
15 using Windows.Foundation.Collections;
16 using Windows.ApplicationModel.Core;
17 using Windows.UI.Core;
18 
19 namespace VoipUI.Helpers
20 {
21     class AppServiceHelper
22     {
~AppServiceHelper()23         ~AppServiceHelper()
24         {
25             if (_appConnection != null)
26             {
27                 _appConnection.Dispose();
28                 _appConnection = null;
29             }
30         }
31 
SendMessageAsync(ValueSet message)32         public async Task<ValueSet> SendMessageAsync(ValueSet message)
33         {
34             ValueSet returnValue = null;
35             AppServiceConnection appConnection = await GetAppConnectionAsync();
36 
37             if (appConnection != null)
38             {
39                 AppServiceResponse response = await appConnection.SendMessageAsync(message);
40 
41                 if (response.Status == AppServiceResponseStatus.Success)
42                 {
43                     if (response.Message.Keys.Contains(BackgroundOperation.Result))
44                     {
45                         returnValue = response.Message;
46                     }
47                 }
48             }
49 
50             return returnValue;
51         }
52 
SendMessage(ValueSet message)53         public async void SendMessage(ValueSet message)
54         {
55             AppServiceConnection appConnection = await GetAppConnectionAsync();
56 
57             if (appConnection != null)
58             {
59                 await appConnection.SendMessageAsync(message);
60             }
61         }
62 
GetAppConnectionAsync()63         private async Task<AppServiceConnection> GetAppConnectionAsync()
64         {
65             AppServiceConnection appConnection = _appConnection;
66 
67             if (appConnection == null)
68             {
69                 appConnection = new AppServiceConnection();
70 
71                 appConnection.ServiceClosed += AppConnection_ServiceClosed;
72 
73                 appConnection.AppServiceName = BackgroundOperation.AppServiceName;
74 
75                 appConnection.PackageFamilyName = Windows.ApplicationModel.Package.Current.Id.FamilyName;
76 
77                 AppServiceConnectionStatus status = await appConnection.OpenAsync();
78 
79                 if (status == AppServiceConnectionStatus.Success)
80                 {
81                     _appConnection = appConnection;
82                     _appConnection.RequestReceived += Connection_RequestReceived;
83                 }
84             }
85 
86             return appConnection;
87         }
88 
AppConnection_ServiceClosed(AppServiceConnection sender, AppServiceClosedEventArgs args)89         private void AppConnection_ServiceClosed(AppServiceConnection sender, AppServiceClosedEventArgs args)
90         {
91             _appConnection = null;
92         }
93 
Connection_RequestReceived(AppServiceConnection sender, AppServiceRequestReceivedEventArgs args)94         private async void Connection_RequestReceived(AppServiceConnection sender, AppServiceRequestReceivedEventArgs args)
95         {
96             var deferral = args.GetDeferral();
97             var response = new ValueSet();
98             //bool stop = false;
99             try
100             {
101                 var request = args.Request;
102                 var message = request.Message;
103                 if (message.ContainsKey(ForegroundOperation.NewForegroundRequest))
104                 {
105                     switch ((ForegroundReguest)message[ForegroundOperation.NewForegroundRequest])
106                     {
107                         case ForegroundReguest.UpdateCallState:
108                             AppRequest = args.Request;
109                             Request = ForegroundReguest.UpdateCallState;
110                             AppRequestDeferal = deferral;
111 
112                             await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => {
113 
114                                 MainPage.Current.UpdateCallState(message[UpdateCallStateArguments.CallState.ToString()] as String);
115                             });
116 
117                             break;
118 
119                         case ForegroundReguest.UpdateRegState:
120                             AppRequest = args.Request;
121                             Request = ForegroundReguest.UpdateRegState;
122                             AppRequestDeferal = deferral;
123 
124                             await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => {
125 
126                                 MainPage.Current.UpdateRegState(message[UpdateRegStateArguments.RegState.ToString()] as String);
127                             });
128 
129                             break;
130 
131                         case ForegroundReguest.UpdateAcccountInfo:
132                             AppRequest = args.Request;
133                             Request = ForegroundReguest.UpdateAcccountInfo;
134                             AppRequestDeferal = deferral;
135 
136                             await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => {
137 
138                                 MainPage.Current.UpdateAccountInfo(message[UpdateAccountInfoArguments.id.ToString()] as String,
139                                                                    message[UpdateAccountInfoArguments.registrar.ToString()] as String,
140                                                                    message[UpdateAccountInfoArguments.proxy.ToString()] as String,
141                                                                    message[UpdateAccountInfoArguments.username.ToString()] as String,
142                                                                    message[UpdateAccountInfoArguments.password.ToString()] as String);
143                             });
144 
145                             break;
146 
147                         default:
148                             break;
149                     }
150                 }
151             }
152             finally
153             {
154             }
155         }
156 
157         public static AppServiceRequest AppRequest
158         {
159             set
160             {
161                 lock (_lock)
162                 {
163                     _appRequest = value;
164                 }
165             }
166             get
167             {
168                 lock (_lock)
169                 {
170                     return _appRequest;
171                 }
172             }
173         }
174 
175         public static AppServiceDeferral AppRequestDeferal
176         {
177             set
178             {
179                 lock (_lock)
180                 {
181                     _appDeferral = value;
182                 }
183             }
184             get
185             {
186                 lock (_lock)
187                 {
188                     return _appDeferral;
189                 }
190             }
191         }
192 
193         public static ForegroundReguest Request
194         {
195             set
196             {
197                 lock (_lock)
198                 {
199                     _request = value;
200                 }
201             }
202             get
203             {
204                 lock (_lock)
205                 {
206                     return _request;
207                 }
208             }
209         }
210 
211         private AppServiceConnection _appConnection = null;
212 
213         private static AppServiceRequest _appRequest = null;
214         private static AppServiceDeferral _appDeferral = null;
215         private static ForegroundReguest _request = ForegroundReguest.InValid;
216         private static Object _lock = new Object();
217     }
218 }
219