2 * Copyright (c) 2017 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.
17 namespace Tizen.Applications.Notifications
20 using System.Collections.Generic;
21 using System.ComponentModel;
24 /// This class contains common properties and methods of notifications.
27 /// A notification is a message that is displayed on the notification area.
28 /// It is created to notify information to the user through the application.
29 /// This class helps you to provide method and property for creating notification object.
31 public sealed partial class Notification : IDisposable
33 internal static readonly string LogTag = "Tizen.Applications.Notification";
35 private NotificationSafeHandle safeHandle;
36 private bool disposed = false;
38 private IDictionary<string, StyleBase> styleDictionary;
39 private IDictionary<string, Bundle> extraDataDictionary;
40 private int count = 0;
43 /// Initializes a new instance of the <see cref="Notification"/> class.
47 styleDictionary = new Dictionary<string, StyleBase>();
48 extraDataDictionary = new Dictionary<string, Bundle>();
52 /// Gets or sets the tag of notification.
54 public string Tag { get; set; } = string.Empty;
57 /// Gets or sets the title of notification.
59 public string Title { get; set; } = string.Empty;
62 /// Gets or sets the icon of notification.
63 /// You should set an absolute path for an image file.
65 public string Icon { get; set; } = string.Empty;
68 /// Gets or sets the sub icon of notification.
69 /// This SubIcon is displayed in Icon you set.
70 /// You should set an absolute path for an image file.
72 public string SubIcon { get; set; } = string.Empty;
75 /// Gets or sets the content of notification.
77 public string Content { get; set; } = string.Empty;
80 /// Gets or sets a value indicating whether TimeStamp of the notification is Visible or not.
83 public bool IsTimeStampVisible { get; set; } = true;
86 /// Gets or sets the TimeStamp of notification.
89 /// If you don't set TimeStamp, it will set the value when the notification is posted.
90 /// TimeStamp requires NotificationManager.Post() to be called.
91 /// If you set IsVisibleTimeStamp property to false, TimeStamp is not visible in notification.
93 public DateTime TimeStamp { get; set; }
96 /// Gets or sets action, which is invoked when the notification is clicked.
99 /// If you set it to null, the already set AppControl will be removed and nothing will happen when you click on notification.
101 /// <seealso cref="Tizen.Applications.AppControl"></seealso>
102 public AppControl Action { get; set; }
105 /// Gets or sets count, which is displayed at the right side of the notification.
108 /// You must set only positive number.
109 /// If you set count to negative number, this property throws exception.
111 /// <exception cref="ArgumentException">Thrown when argument is invalid.</exception>
123 Log.Error(LogTag, "Count value is negative");
124 throw NotificationErrorFactory.GetException(NotificationError.InvalidParameter, "The Count must be a positive integer.");
131 [EditorBrowsable(EditorBrowsableState.Never)]
132 public bool IsOngoing { get; set; } = false;
135 /// Gets or sets property.
137 /// <seealso cref="Tizen.Applications.Notifications.NotificationProperty"></seealso>
138 public NotificationProperty Property { get; set; } = NotificationProperty.None;
141 /// Gets or sets <see cref="Notification.ProgressType"/> object for display at notification.
143 /// <seealso cref="Tizen.Applications.Notifications.Notification.ProgressType"></seealso>
144 public ProgressType Progress { get; set; }
147 /// Gets or sets <see cref="Notification.AccessorySet"/> which is included vibration, LED and sound option to be applied at notification.
150 /// If you set it to null, the already set AccessorySet will be initialized.
154 /// Notification notification = new Notification
156 /// Title = "Notification",
157 /// Content = "Hello Tizen",
158 /// Icon = "Icon path",
162 /// Notification.AccessorySet accessory = new Notification.AccessorySet
164 /// SoundOption = AccessoryOption.Custom,
165 /// SoundPath = "Sound File Path",
166 /// IsVibration = true,
167 /// LedOption = AccessoryOption.Custom,
170 /// LedColor = Color.Lime
173 /// notification.Accessory = accessory;
175 /// NotificationManager.Post(notification);
178 public AccessorySet Accessory { get; set; }
181 /// Gets or sets a value indicating whether notification is displayed on the default viewer.
182 /// If you set false and add style, you can see only style notification.
184 public bool IsVisible { get; set; } = true;
187 /// Gets or sets NotificationSafeHandle.
189 internal NotificationSafeHandle Handle
200 Log.Error(LogTag, "Invalid argument NotificationSafeHandle");
201 throw NotificationErrorFactory.GetException(NotificationError.InvalidParameter, "invalid argument to set NotificationSafeHandle");
209 /// Gets or sets private ID.
211 internal int PrivID { get; set; } = -1;
214 /// Method for adding various styles to be applied to notification.
217 /// The user always see about valid notification style. If you add a style which is not supported in platform,
218 /// this method has no effect.
220 /// <param name="style">The style to be applied to notification.</param>
221 /// <exception cref="ArgumentException">Thrown when argument is invalid.</exception>
224 /// Notification notification = new Notification
226 /// Title = "Notification",
227 /// Content = "Hello Tizen",
228 /// Icon = "Icon path",
232 /// Notification.LockStyle lockStyle = new Notification.LockStyle
234 /// IconPath = "Icon path",
235 /// ThumbnailPath = "Thumbnail Path"
238 /// notification.AddStyle(lockStyle);
240 /// NotificationManager.Post(notification);
243 public void AddStyle(StyleBase style)
247 throw NotificationErrorFactory.GetException(NotificationError.InvalidParameter, "invalid parameter entered");
250 if (styleDictionary.ContainsKey(style.Key) == true)
252 Log.Info(LogTag, "The Style is existed, so extender data is replaced");
253 styleDictionary.Remove(style.Key);
254 styleDictionary.Add(style.Key, style);
258 styleDictionary.Add(style.Key, style);
263 /// Method to remove style you already added.
265 /// <typeparam name="T">Type of notification style to be queried.</typeparam>
266 /// <exception cref="ArgumentException">Thrown when argument is invalid.</exception>
267 public void RemoveStyle<T>() where T : Notification.StyleBase, new()
271 if (styleDictionary.ContainsKey(type.Key))
273 styleDictionary.Remove(type.Key);
277 Log.Error(LogTag, "Sytle Can't be removed, there is no style matched input key");
278 throw NotificationErrorFactory.GetException(NotificationError.InvalidParameter, "invalid parameter entered");
283 /// Method to get style you already added.
285 /// <typeparam name="T">Type of notification style to be queried.</typeparam>
287 /// The Notification.Style object associated with the given style.
289 /// <exception cref="ArgumentException">Thrown when argument is invalid.</exception>
290 public T GetStyle<T>() where T : Notification.StyleBase, new()
293 StyleBase style = null;
295 styleDictionary.TryGetValue(type.Key, out style);
299 Log.Error(LogTag, "Invalid Style");
300 throw NotificationErrorFactory.GetException(NotificationError.InvalidParameter, "invalid parameter entered");
309 /// Method to set extra data to add extra data.
312 /// The type of extra data is bundle.
314 /// <param name="key">The key of the extra data you want to add.</param>
315 /// <param name="value">The value you want to add.</param>
316 /// <exception cref="ArgumentException">Thrown when argument is invalid.</exception>
319 /// Notification notification = new Notification
321 /// Title = "Notification",
322 /// Content = "Hello Tizen",
323 /// Icon = "Icon path",
326 /// Bundle bundle = new Bundle();
327 /// bundle.AddItem("key", "value");
329 /// notification.SetExtraData("firstKey", bundle);
332 public void SetExtraData(string key, Bundle value)
334 if (value == null || value.SafeBundleHandle.IsInvalid || string.IsNullOrEmpty(key))
336 throw NotificationErrorFactory.GetException(NotificationError.InvalidParameter, "invalid parameter entered");
339 if (extraDataDictionary.ContainsKey(key) == true)
341 Log.Info(LogTag, "The key is existed, so extender data is replaced");
342 extraDataDictionary.Remove(key);
343 extraDataDictionary.Add(key, value);
347 extraDataDictionary.Add(key, value);
352 /// Method to remove extra you already added.
355 /// The type of extra data is bundle.
357 /// <param name="key">The key of the extra data to add.</param>
358 /// <exception cref="ArgumentException">Thrown when argument is invalid.</exception>
359 public void RemoveExtraData(string key)
361 if (string.IsNullOrEmpty(key))
363 throw NotificationErrorFactory.GetException(NotificationError.InvalidParameter, "invalid parameter entered");
366 if (extraDataDictionary.ContainsKey(key))
368 extraDataDictionary.Remove(key);
372 throw NotificationErrorFactory.GetException(NotificationError.InvalidParameter, "invalid parameter entered");
377 /// Method to get extra data you already set.
379 /// <param name="key">The key of the extra data to get.</param>
380 /// <returns>Bundle Object that include extra data</returns>
381 /// <exception cref="ArgumentException">Thrown when argument is invalid.</exception>
382 public Bundle GetExtraData(string key)
384 if (string.IsNullOrEmpty(key))
386 throw NotificationErrorFactory.GetException(NotificationError.InvalidParameter, "invalid parameter entered");
390 if (extraDataDictionary.TryGetValue(key, out bundle) == false)
392 throw NotificationErrorFactory.GetException(NotificationError.InvalidParameter, "invalid parameter entered : " + key);
399 /// Releases any unmanaged resources used by this object.
401 public void Dispose()
404 GC.SuppressFinalize(this);
407 internal void Dispose(bool disposing)
422 internal IDictionary<string, StyleBase> GetStyleDictionary()
424 return styleDictionary;
427 internal IDictionary<string, Bundle> GetextraDataDictionary()
429 return extraDataDictionary;
432 internal StyleBase GetStyle(string key)
434 if (string.IsNullOrEmpty(key))
436 throw NotificationErrorFactory.GetException(NotificationError.InvalidParameter, "Key is null or empty");
439 StyleBase style = null;
440 bool ret = styleDictionary.TryGetValue(key, out style);
443 throw NotificationErrorFactory.GetException(NotificationError.InvalidParameter, "The Style object matched input key is not existed");
451 NotificationBinder.BindObject(this);
453 foreach (string key in GetextraDataDictionary().Keys)
455 Log.Info(LogTag, "Start to bind Notification.ExtenderData to SafeHandle");
456 Interop.Notification.SetExtentionData(Handle, key, extraDataDictionary[key].SafeBundleHandle);
459 foreach (Notification.StyleBase style in styleDictionary.Values)
461 Log.Info(LogTag, "Start to bind Notification.Style to SafeHandle [" + style.Key + "]");
465 if (Accessory != null)
467 Log.Info(LogTag, "Start to bind Notification.AccessetSet to SafeHandle");
468 Accessory.Make(this);
471 if (Progress != null)
473 Log.Info(LogTag, "Start to bind Notification.Progress to SafeHandle");
478 internal Notification Build()
480 IntPtr extention = IntPtr.Zero;
481 IntPtr extentionBundlePtr = IntPtr.Zero;
483 NotificationBinder.BindSafeHandle(this);
485 Interop.Notification.GetExtentionBundle(Handle, out extention, out extentionBundlePtr);
487 if (extention != IntPtr.Zero)
489 Bundle bundle = new Bundle(new SafeBundleHandle(extention, false));
490 foreach (string key in bundle.Keys)
492 if (key.StartsWith("_NOTIFICATION_EXTENSION_EVENT_"))
495 SafeBundleHandle sbh;
496 Interop.Notification.GetExtentionData(Handle, key, out sbh);
497 extraDataDictionary.Add(key, new Bundle(sbh));
501 ProgressBinder.BindSafeHandle(this);
502 AccessorySetBinder.BindSafeHandle(this);
503 IndicatorBinder.BindSafeHandle(this);
504 ActiveBinder.BindSafeHandle(this);
505 LockBinder.BindSafehandle(this);