2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 using System.Collections.Generic;
19 using System.Threading.Tasks;
20 using System.Runtime.InteropServices;
23 namespace Tizen.Messaging.Push
25 internal class PushImpl
27 private static readonly object _lock = new object();
28 private static PushImpl _instance;
29 private static Interop.PushClient.VoidResultCallback registerResult = null;
30 private static Interop.PushClient.VoidResultCallback unregisterResult = null;
32 internal static PushImpl Instance
38 if (_instance == null)
40 Log.Info(Interop.PushClient.LogTag, "Creating New Instance");
41 _instance = new PushImpl();
53 private IntPtr _connection;
55 internal void PushServiceConnect(string pushAppId)
57 Interop.PushClient.VoidStateChangedCallback stateDelegate = (int state, string err, IntPtr userData) =>
63 PushConnectionStateEventArgs args = new PushConnectionStateEventArgs((PushConnectionStateEventArgs.PushState)state, err);
64 PushClient.StateChange(args);
66 Interop.PushClient.VoidNotifyCallback notifyDelegate = (IntPtr notification, IntPtr userData) =>
68 Interop.PushClient.ServiceError result;
69 PushMessageEventArgs ob = new PushMessageEventArgs();
71 result = Interop.PushClient.GetNotificationData(notification, out data);
72 if ((result == Interop.PushClient.ServiceError.None) && !(String.IsNullOrEmpty(data)))
81 result = Interop.PushClient.GetNotificationMessage(notification, out message);
82 if ((result == Interop.PushClient.ServiceError.None) && !(String.IsNullOrEmpty(message)))
91 result = Interop.PushClient.GetNotificationSender(notification, out sender);
92 if ((result == Interop.PushClient.ServiceError.None) && !(String.IsNullOrEmpty(sender)))
101 result = Interop.PushClient.GetNotificationSessionInfo(notification, out sessioninfo);
102 if ((result == Interop.PushClient.ServiceError.None) && !(String.IsNullOrEmpty(sessioninfo)))
104 ob.SessionInfo = sessioninfo;
111 result = Interop.PushClient.GetNotificationRequestId(notification, out requestid);
112 if ((result == Interop.PushClient.ServiceError.None) && !(String.IsNullOrEmpty(requestid)))
114 ob.RequestId = requestid;
121 result = Interop.PushClient.GetNotificationTime(notification, out time);
123 if ((result == Interop.PushClient.ServiceError.None) && (time != 0))
125 Log.Info(Interop.PushClient.LogTag, "Ticks received: " + time);
126 utc = DateTime.SpecifyKind(new DateTime(1970, 1, 1).AddSeconds(time), DateTimeKind.Utc);
127 ob.ReceivedAt = utc.ToLocalTime();
131 Log.Info(Interop.PushClient.LogTag, "No Date received");
132 ob.ReceivedAt = DateTime.Now;
135 result = Interop.PushClient.GetNotificationType(notification, out type);
136 if (result == Interop.PushClient.ServiceError.None)
140 PushClient.Notify(ob);
142 Interop.PushClient.ServiceError connectResult = Interop.PushClient.ServiceConnect(pushAppId, stateDelegate, notifyDelegate, IntPtr.Zero, out _connection);
143 if (connectResult != Interop.PushClient.ServiceError.None)
145 Log.Error(Interop.PushClient.LogTag, "Connect failed with " + connectResult);
146 throw PushExceptionFactory.CreateResponseException(connectResult);
150 internal void PushServiceDisconnect()
152 Interop.PushClient.ServiceDisconnect(_connection);
153 Log.Info(Interop.PushClient.LogTag, "PushServiceDisconnect Completed");
156 internal async Task<ServerResponse> PushServerRegister()
158 Log.Info(Interop.PushClient.LogTag, "Register Called");
159 var task = new TaskCompletionSource<ServerResponse>();
160 if (registerResult != null)
162 Log.Error(Interop.PushClient.LogTag, "Register callback was already registered with same callback");
163 return await task.Task;
166 registerResult = (Interop.PushClient.Result regResult, IntPtr msgPtr, IntPtr userData) =>
168 Log.Info(Interop.PushClient.LogTag, "Register Callback Called with " + regResult);
170 if (msgPtr != IntPtr.Zero)
172 msg = Marshal.PtrToStringAnsi(msgPtr);
174 ServerResponse response = new ServerResponse();
175 response.ServerResult = (ServerResponse.Result)regResult;
176 response.ServerMessage = msg;
177 if (task.TrySetResult(response) == false)
179 Log.Error(Interop.PushClient.LogTag, "Unable to set the Result for register");
183 Log.Error(Interop.PushClient.LogTag, "resigterResult is unset");
184 registerResult = null;
187 Interop.PushClient.ServiceError result = Interop.PushClient.ServiceRegister(_connection, registerResult, IntPtr.Zero);
188 Log.Info(Interop.PushClient.LogTag, "Interop.PushClient.ServiceRegister Completed");
189 if (result != Interop.PushClient.ServiceError.None)
191 Log.Error(Interop.PushClient.LogTag, "Register failed with " + result);
192 task.SetException(PushExceptionFactory.CreateResponseException(result));
195 Log.Error(Interop.PushClient.LogTag, "resigterResult is unset (failed)");
196 registerResult = null;
199 return await task.Task;
202 internal async Task<ServerResponse> PushServerUnregister()
204 var task = new TaskCompletionSource<ServerResponse>();
205 unregisterResult = (Interop.PushClient.Result regResult, IntPtr msgPtr, IntPtr userData) =>
207 Log.Info(Interop.PushClient.LogTag, "Unregister Callback Called");
209 if (msgPtr != IntPtr.Zero)
211 msg = Marshal.PtrToStringAnsi(msgPtr);
213 ServerResponse response = new ServerResponse();
214 response.ServerResult = (ServerResponse.Result)regResult;
215 response.ServerMessage = msg;
216 if (task.TrySetResult(response) == false)
218 Log.Error(Interop.PushClient.LogTag, "Unable to set the Result for Unregister");
221 Interop.PushClient.ServiceError result = Interop.PushClient.ServiceDeregister(_connection, unregisterResult, IntPtr.Zero);
222 if (result != Interop.PushClient.ServiceError.None)
224 task.SetException(PushExceptionFactory.CreateResponseException(result));
226 return await task.Task;
229 internal string GetRegistrationId()
232 Interop.PushClient.ServiceError result = Interop.PushClient.GetRegistrationId(_connection, out regID);
233 if (result != Interop.PushClient.ServiceError.None)
235 throw PushExceptionFactory.CreateResponseException(result);
237 Log.Info(Interop.PushClient.LogTag, "Returning Reg Id: " + regID);
241 internal void GetUnreadNotifications()
243 Interop.PushClient.ServiceError result = Interop.PushClient.RequestUnreadNotification(_connection);
244 if (result != Interop.PushClient.ServiceError.None)
246 throw PushExceptionFactory.CreateResponseException(result);
250 internal static void Reset()
254 Log.Info(Interop.PushClient.LogTag, "Making _instance as null");