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