Adding Null Check and Error Enum Updation
[platform/core/csapi/push.git] / Tizen.Messaging.Push / Tizen.Messaging.Push / PushImpl.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Threading.Tasks;
4 using System.Runtime.InteropServices;
5 using Tizen;
6
7 namespace Tizen.Messaging.Push
8 {
9     internal class PushImpl
10     {
11         private static readonly object _lock = new object();
12         private static PushImpl _instance;
13
14         internal static PushImpl Instance
15         {
16             get
17             {
18                 lock (_lock)
19                 {
20                     if (_instance == null)
21                     {
22                         Log.Info(Interop.Push.LogTag, "Creating New Instance");
23                         _instance = new PushImpl();
24                     }
25                 }
26                 return _instance;
27             }
28         }
29
30         internal PushImpl()
31         {
32             //Empty
33         }
34
35         private IntPtr _connection;
36
37         internal void PushServiceConnect(string pushAppId)
38         {
39             Interop.Push.VoidStateChangedCallback stateDelegate = (int state, string err, IntPtr userData) =>
40             {
41                 if (err == null)
42                 {
43                     err = "";
44                 }
45                 PushConnectionStateEventArgs args = new PushConnectionStateEventArgs((PushConnectionStateEventArgs.PushState)state, err);
46                 Push.StateChange(args);
47             };
48             Interop.Push.VoidNotifyCallback notifyDelegate = (IntPtr notification, IntPtr userData) =>
49             {
50                 Interop.Push.ServiceError result;
51                 PushMessageEventArgs ob = new PushMessageEventArgs();
52                 string data;
53                 result = Interop.Push.GetNotificationData(notification, out data);
54                 if ((result == Interop.Push.ServiceError.None) && !(String.IsNullOrEmpty(data)))
55                 {
56                     ob.AppData = data;
57                 }
58                 else
59                 {
60                     ob.AppData = "";
61                 }
62                 string message;
63                 result = Interop.Push.GetNotificationMessage(notification, out message);
64                 if ((result == Interop.Push.ServiceError.None) && !(String.IsNullOrEmpty(message)))
65                 {
66                     ob.Message = message;
67                 }
68                 else
69                 {
70                     ob.Message = "";
71                 }
72                 string sender;
73                 result = Interop.Push.GetNotificationSender(notification, out sender);
74                 if ((result == Interop.Push.ServiceError.None) && !(String.IsNullOrEmpty(sender)))
75                 {
76                     ob.Sender = sender;
77                 }
78                 else
79                 {
80                     ob.Sender = "";
81                 }
82                 string sessioninfo;
83                 result = Interop.Push.GetNotificationSessionInfo(notification, out sessioninfo);
84                 if ((result == Interop.Push.ServiceError.None) && !(String.IsNullOrEmpty(sessioninfo)))
85                 {
86                     ob.SessionInfo = sessioninfo;
87                 }
88                 else
89                 {
90                     ob.SessionInfo = "";
91                 }
92                 string requestid;
93                 result = Interop.Push.GetNotificationRequestId(notification, out requestid);
94                 if ((result == Interop.Push.ServiceError.None) && !(String.IsNullOrEmpty(requestid)))
95                 {
96                     ob.RequestId = requestid;
97                 }
98                 else
99                 {
100                     ob.RequestId = "";
101                 }
102                 int time;
103                 result = Interop.Push.GetNotificationTime(notification, out time);
104                 DateTime utc;
105                 if ((result == Interop.Push.ServiceError.None) && (time != 0))
106                 {
107                     Log.Info(Interop.Push.LogTag, "Ticks received: " + time);
108                     utc = DateTime.SpecifyKind(new DateTime(1970, 1, 1).AddSeconds(time), DateTimeKind.Utc);
109                     ob.ReceivedAt = utc.ToLocalTime();
110                 }
111                 else
112                 {
113                     Log.Info(Interop.Push.LogTag, "No Date received");
114                     ob.ReceivedAt = DateTime.Now;
115                 }
116                 int type = -1;
117                 result = Interop.Push.GetNotificationType(notification, out type);
118                 if (result == Interop.Push.ServiceError.None)
119                 {
120                     ob.Type = type;
121                 }
122                 Push.Notify(ob);
123                 //Interop.Push.FreeNotification(notification);
124                 Log.Info(Interop.Push.LogTag, "Free Notification Done");
125             };
126             Interop.Push.ServiceError connectResult = Interop.Push.ServiceConnect(pushAppId, stateDelegate, notifyDelegate, IntPtr.Zero, out _connection);
127             if (connectResult != Interop.Push.ServiceError.None)
128             {
129                 Log.Error(Interop.Push.LogTag, "Connect failed with " + connectResult);
130                 throw PushExceptionFactory.CreateResponseException(connectResult);
131             }
132         }
133
134         internal void PushServiceDisconnect()
135         {
136             Interop.Push.ServiceDisconnect(_connection);
137             Log.Info(Interop.Push.LogTag, "PushServiceDisconnect Completed");
138         }
139
140         internal async Task<ServerResponse> PushServerRegister()
141         {
142             Log.Info(Interop.Push.LogTag, "Register Called");
143             var task = new TaskCompletionSource<ServerResponse>();
144             Interop.Push.VoidResultCallback registerResult = (Interop.Push.Result regResult, IntPtr msgPtr, IntPtr userData) =>
145             {
146                 Log.Info(Interop.Push.LogTag, "Register Callback Called");
147                 string msg = "";
148                 if (msgPtr != IntPtr.Zero)
149                 {
150                     msg = Marshal.PtrToStringAnsi(msgPtr);
151                 }
152                 ServerResponse response = new ServerResponse();
153                 response.ServerResult = (ServerResponse.Result)regResult;
154                 response.ServerMessage = msg;
155                 if (task.TrySetResult(response) == false)
156                 {
157                     Log.Error(Interop.Push.LogTag, "Unable to set the Result for register");
158                 }
159             };
160             Interop.Push.ServiceError result = Interop.Push.ServiceRegister(_connection, registerResult, IntPtr.Zero);
161             Log.Info(Interop.Push.LogTag, "Interop.Push.ServiceRegister Completed");
162             if (result != Interop.Push.ServiceError.None)
163             {
164                 Log.Error(Interop.Push.LogTag, "Register failed with " + result);
165                 task.SetException(PushExceptionFactory.CreateResponseException(result));
166             }
167             return await task.Task;
168         }
169
170         internal async Task<ServerResponse> PushServerUnregister()
171         {
172             var task = new TaskCompletionSource<ServerResponse>();
173             Interop.Push.VoidResultCallback registerResult = (Interop.Push.Result regResult, IntPtr msgPtr, IntPtr userData) =>
174             {
175                 Log.Info(Interop.Push.LogTag, "Unregister Callback Called");
176                 string msg = "";
177                 if (msgPtr != IntPtr.Zero)
178                 {
179                     msg = Marshal.PtrToStringAnsi(msgPtr);
180                 }
181                 ServerResponse response = new ServerResponse();
182                 response.ServerResult = (ServerResponse.Result)regResult;
183                 response.ServerMessage = msg;
184                 if (task.TrySetResult(response) == false)
185                 {
186                     Log.Error(Interop.Push.LogTag, "Unable to set the Result for Unregister");
187                 }
188             };
189             Interop.Push.ServiceError result = Interop.Push.ServiceDeregister(_connection, registerResult, IntPtr.Zero);
190             if (result != Interop.Push.ServiceError.None)
191             {
192                 task.SetException(PushExceptionFactory.CreateResponseException(result));
193             }
194             return await task.Task;
195         }
196
197         internal string GetRegistrationId()
198         {
199             string regID = "";
200             Interop.Push.ServiceError result = Interop.Push.GetRegistrationId(_connection, out regID);
201             if (result != Interop.Push.ServiceError.None)
202             {
203                 throw PushExceptionFactory.CreateResponseException(result);
204             }
205             Log.Info(Interop.Push.LogTag, "Returning Reg Id: " + regID);
206             return regID;
207         }
208
209         internal void GetUnreadNotifications()
210         {
211             Interop.Push.ServiceError result = Interop.Push.RequestUnreadNotification(_connection);
212             if (result != Interop.Push.ServiceError.None)
213             {
214                 throw PushExceptionFactory.CreateResponseException(result);
215             }
216         }
217
218         internal static void Reset()
219         {
220             lock (_lock)
221             {
222                 Log.Info(Interop.Push.LogTag, "Making _instance as null");
223                 _instance = null;
224             }
225         }
226     }
227 }