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.");
132 /// Gets or sets a value indicating whether the notification is Onging or not.
133 /// Default value is false.
135 [EditorBrowsable(EditorBrowsableState.Never)]
136 public bool IsOngoing { get; set; } = false;
139 /// Gets or sets property.
141 /// <seealso cref="Tizen.Applications.Notifications.NotificationProperty"></seealso>
142 public NotificationProperty Property { get; set; } = NotificationProperty.None;
145 /// Gets or sets <see cref="Notification.ProgressType"/> object for display at notification.
147 /// <seealso cref="Tizen.Applications.Notifications.Notification.ProgressType"></seealso>
148 public ProgressType Progress { get; set; }
151 /// Gets or sets <see cref="Notification.AccessorySet"/> which is included vibration, LED and sound option to be applied at notification.
154 /// If you set it to null, the already set AccessorySet will be initialized.
158 /// Notification notification = new Notification
160 /// Title = "Notification",
161 /// Content = "Hello Tizen",
162 /// Icon = "Icon path",
166 /// Notification.AccessorySet accessory = new Notification.AccessorySet
168 /// SoundOption = AccessoryOption.Custom,
169 /// SoundPath = "Sound File Path",
170 /// IsVibration = true,
171 /// LedOption = AccessoryOption.Custom,
174 /// LedColor = Color.Lime
177 /// notification.Accessory = accessory;
179 /// NotificationManager.Post(notification);
182 public AccessorySet Accessory { get; set; }
185 /// Gets or sets a value indicating whether notification is displayed on the default viewer.
186 /// If you set false and add style, you can see only style notification.
188 public bool IsVisible { get; set; } = true;
191 /// Gets or sets NotificationSafeHandle.
193 internal NotificationSafeHandle Handle
204 Log.Error(LogTag, "Invalid argument NotificationSafeHandle");
205 throw NotificationErrorFactory.GetException(NotificationError.InvalidParameter, "invalid argument to set NotificationSafeHandle");
213 /// Gets or sets private ID.
215 internal int PrivID { get; set; } = -1;
218 /// Method for adding various styles to be applied to notification.
221 /// The user always see about valid notification style. If you add a style which is not supported in platform,
222 /// this method has no effect.
224 /// <param name="style">The style to be applied to notification.</param>
225 /// <exception cref="ArgumentException">Thrown when argument is invalid.</exception>
228 /// Notification notification = new Notification
230 /// Title = "Notification",
231 /// Content = "Hello Tizen",
232 /// Icon = "Icon path",
236 /// Notification.LockStyle lockStyle = new Notification.LockStyle
238 /// IconPath = "Icon path",
239 /// ThumbnailPath = "Thumbnail Path"
242 /// notification.AddStyle(lockStyle);
244 /// NotificationManager.Post(notification);
247 public void AddStyle(StyleBase style)
251 throw NotificationErrorFactory.GetException(NotificationError.InvalidParameter, "invalid parameter entered");
254 if (styleDictionary.ContainsKey(style.Key) == true)
256 Log.Info(LogTag, "The Style is existed, so extender data is replaced");
257 styleDictionary.Remove(style.Key);
258 styleDictionary.Add(style.Key, style);
262 styleDictionary.Add(style.Key, style);
267 /// Method to remove style you already added.
269 /// <typeparam name="T">Type of notification style to be queried.</typeparam>
270 /// <exception cref="ArgumentException">Thrown when argument is invalid.</exception>
271 public void RemoveStyle<T>() where T : Notification.StyleBase, new()
275 if (styleDictionary.ContainsKey(type.Key))
277 styleDictionary.Remove(type.Key);
281 Log.Error(LogTag, "Sytle Can't be removed, there is no style matched input key");
282 throw NotificationErrorFactory.GetException(NotificationError.InvalidParameter, "invalid parameter entered");
287 /// Method to get style you already added.
289 /// <typeparam name="T">Type of notification style to be queried.</typeparam>
291 /// The Notification.Style object associated with the given style.
293 /// <exception cref="ArgumentException">Thrown when argument is invalid.</exception>
294 public T GetStyle<T>() where T : Notification.StyleBase, new()
297 StyleBase style = null;
299 styleDictionary.TryGetValue(type.Key, out style);
303 Log.Error(LogTag, "Invalid Style");
304 throw NotificationErrorFactory.GetException(NotificationError.InvalidParameter, "invalid parameter entered");
313 /// Method to set extra data to add extra data.
316 /// The type of extra data is bundle.
318 /// <param name="key">The key of the extra data you want to add.</param>
319 /// <param name="value">The value you want to add.</param>
320 /// <exception cref="ArgumentException">Thrown when argument is invalid.</exception>
323 /// Notification notification = new Notification
325 /// Title = "Notification",
326 /// Content = "Hello Tizen",
327 /// Icon = "Icon path",
330 /// Bundle bundle = new Bundle();
331 /// bundle.AddItem("key", "value");
333 /// notification.SetExtraData("firstKey", bundle);
336 public void SetExtraData(string key, Bundle value)
338 if (value == null || value.SafeBundleHandle.IsInvalid || string.IsNullOrEmpty(key))
340 throw NotificationErrorFactory.GetException(NotificationError.InvalidParameter, "invalid parameter entered");
343 if (extraDataDictionary.ContainsKey(key) == true)
345 Log.Info(LogTag, "The key is existed, so extender data is replaced");
346 extraDataDictionary.Remove(key);
347 extraDataDictionary.Add(key, value);
351 extraDataDictionary.Add(key, value);
356 /// Method to remove extra you already added.
359 /// The type of extra data is bundle.
361 /// <param name="key">The key of the extra data to add.</param>
362 /// <exception cref="ArgumentException">Thrown when argument is invalid.</exception>
363 public void RemoveExtraData(string key)
365 if (string.IsNullOrEmpty(key))
367 throw NotificationErrorFactory.GetException(NotificationError.InvalidParameter, "invalid parameter entered");
370 if (extraDataDictionary.ContainsKey(key))
372 extraDataDictionary.Remove(key);
376 throw NotificationErrorFactory.GetException(NotificationError.InvalidParameter, "invalid parameter entered");
381 /// Method to get extra data you already set.
383 /// <param name="key">The key of the extra data to get.</param>
384 /// <returns>Bundle Object that include extra data</returns>
385 /// <exception cref="ArgumentException">Thrown when argument is invalid.</exception>
386 public Bundle GetExtraData(string key)
388 if (string.IsNullOrEmpty(key))
390 throw NotificationErrorFactory.GetException(NotificationError.InvalidParameter, "invalid parameter entered");
394 if (extraDataDictionary.TryGetValue(key, out bundle) == false)
396 throw NotificationErrorFactory.GetException(NotificationError.InvalidParameter, "invalid parameter entered : " + key);
403 /// Releases any unmanaged resources used by this object.
405 public void Dispose()
408 GC.SuppressFinalize(this);
411 internal void Dispose(bool disposing)
418 if (disposing && Handle != null && Handle.IsInvalid == false)
426 internal IDictionary<string, StyleBase> GetStyleDictionary()
428 return styleDictionary;
431 internal IDictionary<string, Bundle> GetextraDataDictionary()
433 return extraDataDictionary;
436 internal StyleBase GetStyle(string key)
438 if (string.IsNullOrEmpty(key))
440 throw NotificationErrorFactory.GetException(NotificationError.InvalidParameter, "Key is null or empty");
443 StyleBase style = null;
444 bool ret = styleDictionary.TryGetValue(key, out style);
447 throw NotificationErrorFactory.GetException(NotificationError.InvalidParameter, "The Style object matched input key is not existed");
455 NotificationBinder.BindObject(this);
457 foreach (string key in GetextraDataDictionary().Keys)
459 Log.Info(LogTag, "Start to bind Notification.ExtenderData to SafeHandle");
460 Interop.Notification.SetExtentionData(Handle, key, extraDataDictionary[key].SafeBundleHandle);
463 foreach (Notification.StyleBase style in styleDictionary.Values)
465 Log.Info(LogTag, "Start to bind Notification.Style to SafeHandle [" + style.Key + "]");
469 if (Accessory != null)
471 Log.Info(LogTag, "Start to bind Notification.AccessetSet to SafeHandle");
472 Accessory.Make(this);
475 if (Progress != null)
477 Log.Info(LogTag, "Start to bind Notification.Progress to SafeHandle");
482 internal Notification Build()
484 IntPtr extention = IntPtr.Zero;
485 IntPtr extentionBundlePtr = IntPtr.Zero;
487 NotificationBinder.BindSafeHandle(this);
489 Interop.Notification.GetExtentionBundle(Handle, out extention, out extentionBundlePtr);
491 if (extention != IntPtr.Zero)
493 Bundle bundle = new Bundle(new SafeBundleHandle(extention, false));
494 foreach (string key in bundle.Keys)
496 if (key.StartsWith("_NOTIFICATION_EXTENSION_EVENT_"))
499 SafeBundleHandle sbh;
500 Interop.Notification.GetExtentionData(Handle, key, out sbh);
501 extraDataDictionary.Add(key, new Bundle(sbh));
505 ProgressBinder.BindSafeHandle(this);
506 AccessorySetBinder.BindSafeHandle(this);
507 IndicatorBinder.BindSafeHandle(this);
508 ActiveBinder.BindSafeHandle(this);
509 LockBinder.BindSafehandle(this);