summaryrefslogtreecommitdiff
path: root/Tizen.Applications.Notification/Tizen.Applications.Notifications/NotificationManager.cs
blob: 1a890f3801e04ad65a0cae5a0d2799c308fa8a9e (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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
/*
 * Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved
 *
 * Licensed under the Apache License, Version 2.0 (the License);
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an AS IS BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

namespace Tizen.Applications.Notifications
{
    using System;
    using System.ComponentModel;

    /// <summary>
    /// NotificationManager class to post, update, delete and get Notification.
    /// </summary>
    public static class NotificationManager
    {
        /// <summary>
        /// Posts a new Notification.
        /// </summary>
        /// <param name="notification">Notification to post</param>
        /// <exception cref="ArgumentException">Thrown when argument is invalid</exception>
        /// <exception cref="UnauthorizedAccessException">Thrown in case of permission denied.</exception>
        /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
        /// <example>
        /// <code>
        /// Notification notification = new Notification
        /// {
        ///     Title = "title",
        ///     Content = "content",
        ///     Icon = "absolute icon path",
        ///     Tag = "first notification"
        /// };
        ///
        /// Notification.AccessorySet accessory = new Notification.AccessorySet
        /// {
        ///     SoundOption = AccessoryOption.On,
        ///     CanVibrate = true
        /// };
        /// notification.Accessory = accessory;
        ///
        ///     // do something
        ///
        /// NotificationManager.Post(notification);
        /// </code>
        /// </example>
        /// <privilege>http://tizen.org/privilege/notification</privilege>
        public static void Post(Notification notification)
        {
            if (notification == null)
            {
                throw NotificationErrorFactory.GetException(NotificationError.InvalidParameter, "invalid argument to post method");
            }

            notification.Make();

            NotificationError ret = Interop.Notification.Post(notification.Handle);
            if (ret != NotificationError.None)
            {
                throw NotificationErrorFactory.GetException(ret, "post notification failed");
            }

            int priv_id, group_id;
            Interop.Notification.GetID(notification.Handle, out group_id, out priv_id);
            notification.PrivID = priv_id;
        }

        /// <summary>
        /// Updates a posted Notification.
        /// </summary>
        /// <param name="notification">Notification to update</param>
        /// <exception cref="ArgumentException">Thrown when argument is invalid</exception>
        /// <exception cref="UnauthorizedAccessException">Thrown in case of permission denied.</exception>
        /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
        /// <example>
        /// <code>
        /// string tag = "first tag";
        ///
        /// Notification notification = new Notification
        /// {
        ///     Title = "title",
        ///     Content = "content",
        ///     Icon = "absolute icon path",
        ///     Tag = tag
        /// };
        ///
        /// Notification.AccessorySet accessory = new Notification.AccessorySet
        /// {
        ///     LedOption = AccessoryOption.On,
        ///     VibrationOption = AccessoryOption.Custom,
        ///     VibrationPath = "vibration absolute path"
        /// }
        /// notification.Accessory = accessory;
        ///
        /// NotificationManager.Post(notification);
        ///
        ///     // do something
        ///
        /// Notification loadNotification = NotificationManager.Load(tag);
        ///
        /// loadNotification.Progress = new ProgressType(ProgressCategory.Percent, 0.0. 100.0);
        ///
        /// Thread thread = new Thread(new ParameterizedThreadStart(UpdateProgress));
        /// thread.IsBackground = true;
        /// thread.Start(notification);
        ///
        ///   ...
        ///
        /// static void UpdateProgress(Object obj)
        /// {
        ///     Notification notification = (Notification)obj;
        ///
        ///     for (double current = 1.0; current &lt;= 100.0; current = current + 1.0)
        ///     {
        ///         notification.Progress.ProgressCurrent = current;
        ///         NotificationManager.Update(notification);
        ///         Thread.Sleep(300);
        ///     }
        /// }
        /// </code>
        /// </example>
        /// <privilege>http://tizen.org/privilege/notification</privilege>
        /// <pre>
        /// Post method should be called on the Notification object.
        /// </pre>
        public static void Update(Notification notification)
        {
            if (notification == null || notification.Handle == null || notification.Handle.IsInvalid)
            {
                throw NotificationErrorFactory.GetException(NotificationError.InvalidParameter, "invalid argument to post method");
            }

            notification.Make();
            NotificationError ret = Interop.Notification.Update(notification.Handle);
            if (ret != NotificationError.None)
            {
                throw NotificationErrorFactory.GetException(ret, "update notification failed");
            }
        }

        /// <summary>
        /// Deletes a posted Notification.
        /// </summary>
        /// <param name="notification">Notification to remove</param>
        /// <exception cref="ArgumentException">Thrown when argument is invalid</exception>
        /// <exception cref="UnauthorizedAccessException">Thrown in case of permission denied.</exception>
        /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
        /// <example>
        /// <code>
        /// Notification notification = new Notification
        /// {
        ///     Title = "title",
        ///     Content = "content",
        ///     Icon = "absolute icon path",
        ///     Tag = "first notification"
        /// };
        /// NotificationManager.Post(notification);
        ///
        ///     // do something
        ///
        /// NotificationManager.Delete(notification);
        /// </code>
        /// </example>
        /// <privilege>http://tizen.org/privilege/notification</privilege>
        /// <pre>
        /// Post method should be called on the Notification object.
        /// </pre>
        public static void Delete(Notification notification)
        {
            if (notification == null || notification.Handle == null || notification.Handle.IsInvalid)
            {
                throw NotificationErrorFactory.GetException(NotificationError.InvalidParameter, "invalid argument to post method");
            }

            NotificationError ret = Interop.Notification.Delete(notification.Handle);
            if (ret != NotificationError.None)
            {
                throw NotificationErrorFactory.GetException(ret, "delete notification failed");
            }
        }

        /// <summary>
        /// Removes all posted Notification of calling application.
        /// </summary>
        /// <exception cref="UnauthorizedAccessException">Thrown in case of permission denied.</exception>
        /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
        /// <example>
        /// <code>
        /// Notification firstNotification = new Notification
        /// {
        ///     Title = "title",
        ///     Content = "content",
        ///     Icon = "absolute icon path",
        ///     Tag = "first notification"
        /// };
        /// NotificationManager.Post(firstNotification);
        ///
        /// Notification secondNotification = new Notification
        /// {
        ///     Title = "title",
        ///     Content = "content",
        ///     Icon = "absolute icon path",
        ///     Tag = "second notification"
        /// };
        /// NotificationManager.Post(secondNotification);
        /// NotificationManager.DeleteAll();
        /// </code>
        /// </example>
        /// <privilege>http://tizen.org/privilege/notification</privilege>
        public static void DeleteAll()
        {
            NotificationError ret;

            ret = Interop.Notification.DeleteAll((int)NotificationType.Basic);
            if (ret != NotificationError.None)
            {
                throw NotificationErrorFactory.GetException(ret, "delete all notifications failed of Noti type");
            }

            ret = Interop.Notification.DeleteAll((int)NotificationType.Ongoing);
            if (ret != NotificationError.None)
            {
                throw NotificationErrorFactory.GetException(ret, "delete all notifications failed of Ongoing type");
            }
        }

        /// <summary>
        /// Searches for a posted notification which has the inputted tag and isn't deleted not yet.
        /// </summary>
        /// <remarks>
        /// Load method should be called only for notifications which have been posted using NotificationManager.Post method.
        /// If two or more notifications share the same tag, the notification posted most recently is returned.
        /// </remarks>
        /// <param name="tag">Tag used to query</param>
        /// <returns>Notification Object with inputted tag</returns>
        /// <exception cref="ArgumentException">Thrown when argument is invalid or when the tag does not exist</exception>
        /// <exception cref="UnauthorizedAccessException">Thrown in case of permission denied.</exception>
        /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
        /// <example>
        /// <code>
        /// Notification notification = new Notification
        /// {
        ///     Title = "title",
        ///     Content = "content",
        ///     Icon = "absolute icon path",
        ///     Tag = "first notification"
        /// };
        /// NotificationManager.Post(notification);
        ///
        ///     // do someting
        ///
        /// Notification loadNotification = NotificationManager.Load("first notification");
        /// </code>
        /// </example>
        /// <privilege>http://tizen.org/privilege/notification</privilege>
        public static Notification Load(string tag)
        {
            IntPtr ptr = IntPtr.Zero;

            if (string.IsNullOrEmpty(tag))
            {
                throw NotificationErrorFactory.GetException(NotificationError.InvalidParameter, "invalid parameter entered");
            }

            ptr = Interop.Notification.Load(tag);

            if (ptr == IntPtr.Zero)
            {
                NotificationError ret = (NotificationError)Tizen.Internals.Errors.ErrorFacts.GetLastResult();
                Log.Error(Notification.LogTag, "unable to load Notification : " + ret.ToString());
                if (ret == NotificationError.DbError)
                {
                    throw NotificationErrorFactory.GetException(NotificationError.InvalidParameter, "the tag does not exist");
                }
                else
                {
                    throw NotificationErrorFactory.GetException(ret, "unable to load Notification");
                }
            }

            Notification notification = new Notification
            {
                Handle = new NotificationSafeHandle(ptr, true)
            }.Build();

            return notification;
        }

        /// <summary>
        /// Saves a notification template to the notification database
        /// </summary>
        /// <param name="notification">Notification to save as template</param>
        /// <param name="name">Template name</param>
        /// <exception cref="ArgumentException">Thrown when argument is invalid</exception>
        /// <exception cref="UnauthorizedAccessException">Thrown in case of permission denied.</exception>
        /// <exception cref="InvalidOperationException">Thrown when can't save as template</exception>
        /// <example>
        /// <code>
        /// Notification notification = new Notification
        /// {
        ///     Title = "title",
        ///     Content = "content",
        ///     Icon = "absolute icon path",
        ///     Tag = "first notification"
        /// };
        ///
        /// Notification.Accessory accessory = new Notification.Accessory
        /// {
        ///     LedOption = AccessoryOption.On,
        ///     VibrationOption = AccessoryOption.Custom,
        ///     VibrationPath = "vibration absolute path"
        /// }
        /// notification.setAccessory(accessory);
        ///
        ///     // do something
        ///
        /// NotificationManager.Post(notification);
        ///
        /// Notification.LockStyle style = new Notification.LockStyle
        /// {
        ///     IconPath = "icon path",
        ///     ThumbnailPath = "Thumbnail path"
        /// }
        /// notification.AddStyle(style);
        /// NotificationManager.SaveTemplate(notification, "firstTemplate");
        /// </code>
        /// </example>
        /// <privilege>http://tizen.org/privilege/notification</privilege>
        public static void SaveTemplate(Notification notification, string name)
        {
            if (notification == null || string.IsNullOrEmpty(name))
            {
                throw NotificationErrorFactory.GetException(NotificationError.InvalidParameter, "invalid argument to save template");
            }

            notification.Make();

            NotificationError ret = Interop.Notification.SaveTemplate(notification.Handle, name);
            if (ret != NotificationError.None)
            {
                throw NotificationErrorFactory.GetException(ret, "save as template failed");
            }
        }

        /// <summary>
        /// Loads a notification template from the notification database
        /// </summary>
        /// <param name="name">Template name</param>
        /// <returns>Notification Object with inputted template name</returns>
        /// <exception cref="ArgumentException">Thrown when argument is invalid or when no template with input name exists</exception>
        /// <exception cref="UnauthorizedAccessException">Thrown in case of permission denied.</exception>
        /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
        /// <example>
        /// <code>
        /// Notification notification = new Notification
        /// {
        ///     Title = "title",
        ///     Content = "content",
        ///     Icon = "absolute icon path",
        ///     Tag = "first notification"
        /// };
        ///
        /// Notification.Accessory accessory = new Notification.Accessory
        /// {
        ///     LedOption = AccessoryOption.On,
        ///     VibrationOption = AccessoryOption.Custom,
        ///     VibrationPath = "vibration absolute path"
        /// }
        /// notification.setAccessory(accessory);
        ///
        ///     // do something
        ///
        /// NotificationManager.Post(notification);
        ///
        /// Notification.LockStyle style = new Notification.LockStyle
        /// {
        ///     IconPath = "icon path",
        ///     ThumbnailPath = "Thumbnail path"
        /// }
        /// notification.AddStyle(style);
        /// NotificationManager.SaveTemplate(notification, "firstTemplate");
        /// Notification notificationTemplate = NotificationManager.LoadTemplate("firstTemplate");
        /// </code>
        /// </example>
        /// <privilege>http://tizen.org/privilege/notification</privilege>
        public static Notification LoadTemplate(string name)
        {
            IntPtr handle = IntPtr.Zero;

            if (string.IsNullOrEmpty(name))
            {
                throw NotificationErrorFactory.GetException(NotificationError.InvalidParameter, "invalid argument to load template");
            }

            handle = Interop.Notification.LoadTemplate(name);
            if (handle == IntPtr.Zero)
            {
                NotificationError ret = (NotificationError)Tizen.Internals.Errors.ErrorFacts.GetLastResult();
                if (ret == NotificationError.DbError)
                {
                    throw NotificationErrorFactory.GetException(NotificationError.InvalidParameter, "the name does not exist");
                }
                else
                {
                    throw NotificationErrorFactory.GetException(ret, "unable to create Notification from template");
                }
            }

            Notification notification = new Notification
            {
                Handle = new NotificationSafeHandle(handle, true)
            }.Build();

            return notification;
        }

        /// <summary>
        /// Gets notification block state.
        /// </summary>
        /// <remarks>
        /// The user can set the notification block state in settings.
        /// The block state indicates whether or not notifications can be posted.
        /// Additionally only notifications to the notification panel are allowed in "Do not disturb mode".
        /// Sound, Vibrate and Active notifications are blocked.
        /// </remarks>
        /// <returns>NotificationBlockState is state if notification is posted</returns>
        /// <exception cref="UnauthorizedAccessException">Thrown in case of permission denied.</exception>
        /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
        /// <privilege>http://tizen.org/privilege/notification</privilege>
        public static NotificationBlockState GetBlockState()
        {
            NotificationBlockState state;
            NotificationError ret;

            ret = Interop.Notification.GetBlockState(out state);
            if (ret != NotificationError.None)
            {
                throw NotificationErrorFactory.GetException(ret, "GetBlockState failed");
            }

            Log.Info(Notification.LogTag, "Current block state is " + state.ToString());
            return state;
        }

        [EditorBrowsable(EditorBrowsableState.Never)]
        public static NotificationSafeHandle MakeNotificationSafeHandle(Notification notification)
        {
            if (notification == null)
            {
                throw NotificationErrorFactory.GetException(NotificationError.InvalidParameter, "invalid notification object");
            }

            notification.Make();

            return notification.Handle;
        }

        [EditorBrowsable(EditorBrowsableState.Never)]
        public static Notification MakeNotification(NotificationSafeHandle handle)
        {
            if (handle == null || handle.IsInvalid == true)
            {
                throw NotificationErrorFactory.GetException(NotificationError.InvalidParameter, "handle is invalid or null");
            }

            Notification notification = new Notification { Handle = handle }.Build();

            return notification;
        }
    }
}