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 /// Class containing 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> extenderDictionary;
40 private int count = 0;
43 /// Initializes a new instance of the <see cref="Notification"/> class.
47 styleDictionary = new Dictionary<string, StyleBase>();
48 extenderDictionary = new Dictionary<string, Bundle>();
52 /// Gets or sets Tag of Notification.
54 public string Tag { get; set; } = string.Empty;
57 /// Gets or sets Title of Notification.
59 public string Title { get; set; } = string.Empty;
62 /// Gets or sets icon of Notification.
64 public string Icon { get; set; } = string.Empty;
67 /// Gets or sets sub icon of Notification.
68 /// This SubIcon is displayed in Icon you set.
70 public string SubIcon { get; set; } = string.Empty;
73 /// Gets or sets content of Notification.
75 public string Content { get; set; } = string.Empty;
78 /// Gets or sets a value indicating whether TimeStamp of Notification is Visible or not.
81 public bool IsTimeStampVisible { get; set; } = true;
84 /// Gets or sets TimeStamp of Notification.
87 /// If you don't set TimeStamp, It will be set value that time when the notification is posted.
88 /// TimeStamp requires NotificationManager.Post() to be called.
89 /// If you set IsVisibleTimeStamp property is false, TimeStamp is not Visible in Notification.
91 public DateTime TimeStamp { get; set; }
94 /// Gets or sets Action which is invoked when notification is clicked
97 /// If you set it to null, the already set AppControl will be removed and nothing will happen when you click on notification.
99 /// <seealso cref="Tizen.Applications.AppControl"></seealso>
100 public AppControl Action { get; set; }
103 /// Gets or sets Count which is displayed at the right side of notification.
106 /// You must set only positive number.
107 /// If you set count to negative number, This property throw exception.
109 /// <exception cref="ArgumentException">Thrown when argument is invalid</exception>
121 Log.Error(LogTag, "Count value is negative");
122 throw NotificationErrorFactory.GetException(NotificationError.InvalidParameter, "The Count must be a positive integer.");
129 [EditorBrowsable(EditorBrowsableState.Never)]
130 public bool IsOngoing { get; set; } = false;
133 /// Gets or sets property
135 /// <seealso cref="Tizen.Applications.Notifications.NotificationProperty"></seealso>
136 public NotificationProperty Property { get; set; } = NotificationProperty.None;
139 /// Gets or sets <see cref="Notification.ProgressType"/> object for display at notification
141 /// <seealso cref="Tizen.Applications.Notifications.Notification.ProgressType"></seealso>
142 public ProgressType Progress { get; set; }
145 /// Gets or sets <see cref="Notification.AccessorySet"/> which is included vibration, led and sound option to be applied at Notification.
148 /// If you set it to null, the already set AccessorySet will be initialized.
152 /// Notification notification = new Notification
154 /// Title = "Notification",
155 /// Content = "Hello Tizen",
156 /// Icon = "Icon path",
160 /// Notification.AccessorySet accessory = new Notification.AccessorySet
162 /// SoundOption = AccessoryOption.Custom,
163 /// SoundPath = "Sound File Path",
164 /// IsVibration = true,
165 /// LedOption = AccessoryOption.Custom,
168 /// LedColor = Color.Lime
171 /// notification.Accessory = accessory;
173 /// NotificationManager.Post(notification);
176 public AccessorySet Accessory { get; set; }
179 /// Gets or sets a value indicating whether notification is displayed on default viewer.
180 /// If you set false and add style, you can see only style notification.
182 public bool IsDisplay { get; set; } = true;
185 /// Gets or sets NotificationSafeHandle
187 internal NotificationSafeHandle Handle
198 Log.Error(LogTag, "Invalid argument NotificationSafeHandle");
199 throw NotificationErrorFactory.GetException(NotificationError.InvalidParameter, "invalid argument to set NotificationSafeHandle");
207 /// Gets or sets Private ID
209 internal int PrivID { get; set; } = -1;
212 /// Method to add various style to be applied to notification.
215 /// The user always see about valid notification style. If you add style which is not supported in platform,
216 /// this method has no effect.
218 /// <param name="style">The style to be applied to notification</param>
219 /// <exception cref="ArgumentException">Thrown when argument is invalid</exception>
222 /// Notification notification = new Notification
224 /// Title = "Notification",
225 /// Content = "Hello Tizen",
226 /// Icon = "Icon path",
230 /// Notification.LockStyle lockStyle = new Notification.LockStyle
232 /// IconPath = "Icon path",
233 /// ThumbnailPath = "Thumbnail Path"
236 /// notification.AddStyle(lockStyle);
238 /// NotificationManager.Post(notification);
241 public void AddStyle(StyleBase style)
245 throw NotificationErrorFactory.GetException(NotificationError.InvalidParameter, "invalid parameter entered");
248 if (styleDictionary.ContainsKey(style.Key) == true)
250 Log.Info(LogTag, "The Style is existed, so extender data is replaced");
251 styleDictionary.Remove(style.Key);
252 styleDictionary.Add(style.Key, style);
256 styleDictionary.Add(style.Key, style);
261 /// Method to remove style you already added.
263 /// <typeparam name="T">Type of notification style to be queried</typeparam>
264 /// <exception cref="ArgumentException">Thrown when argument is invalid</exception>
265 public void RemoveStyle<T>() where T : Notification.StyleBase, new()
269 if (styleDictionary.ContainsKey(type.Key))
271 styleDictionary.Remove(type.Key);
275 Log.Error(LogTag, "Sytle Can't be removed, there is no style matched input key");
276 throw NotificationErrorFactory.GetException(NotificationError.InvalidParameter, "invalid parameter entered");
281 /// Method to get style you already added.
283 /// <typeparam name="T">Type of notification style to be queried</typeparam>
285 /// The Notification.Style object associated with the given style
287 /// <exception cref="ArgumentException">Thrown when argument is invalid</exception>
288 public T GetStyle<T>() where T : Notification.StyleBase, new()
291 StyleBase style = null;
293 styleDictionary.TryGetValue(type.Key, out style);
297 Log.Error(LogTag, "Invalid Style");
298 throw NotificationErrorFactory.GetException(NotificationError.InvalidParameter, "invalid parameter entered");
307 /// Method to set extender data to add extra data
310 /// The type of extra data is Bundle.
312 /// <param name="key">The key of the extra data you want to add.</param>
313 /// <param name="value">The value you want to add.</param>
314 /// <exception cref="ArgumentException">Thrown when argument is invalid</exception>
317 /// Notification notification = new Notification
319 /// Title = "Notification",
320 /// Content = "Hello Tizen",
321 /// Icon = "Icon path",
324 /// Bundle bundle = new Bundle();
325 /// bundle.AddItem("key", "value");
327 /// notification.SetExtender("firstKey", bundle);
330 public void SetExtender(string key, Bundle value)
332 if (value == null || value.SafeBundleHandle.IsInvalid || string.IsNullOrEmpty(key))
334 throw NotificationErrorFactory.GetException(NotificationError.InvalidParameter, "invalid parameter entered");
337 if (extenderDictionary.ContainsKey(key) == true)
339 Log.Info(LogTag, "The key is existed, so extender data is replaced");
340 extenderDictionary.Remove(key);
341 extenderDictionary.Add(key, value);
345 extenderDictionary.Add(key, value);
350 /// Method to remove extender you already added.
353 /// The type of extra data is Bundle.
355 /// <param name="key">The key of the extra data to add.</param>
356 /// <exception cref="ArgumentException">Thrown when argument is invalid</exception>
357 public void RemoveExtender(string key)
359 if (string.IsNullOrEmpty(key))
361 throw NotificationErrorFactory.GetException(NotificationError.InvalidParameter, "invalid parameter entered");
364 if (extenderDictionary.ContainsKey(key))
366 extenderDictionary.Remove(key);
370 throw NotificationErrorFactory.GetException(NotificationError.InvalidParameter, "invalid parameter entered");
375 /// Method to get extender data you already set
377 /// <param name="key">The key of the extra data to get.</param>
378 /// <returns>Bundle Object that include extender data</returns>
379 /// <exception cref="ArgumentException">Thrown when argument is invalid</exception>
380 public Bundle GetExtender(string key)
382 if (string.IsNullOrEmpty(key))
384 throw NotificationErrorFactory.GetException(NotificationError.InvalidParameter, "invalid parameter entered");
388 if (extenderDictionary.TryGetValue(key, out bundle) == false)
390 throw NotificationErrorFactory.GetException(NotificationError.InvalidParameter, "invalid parameter entered : " + key);
397 /// Releases any unmanaged resources used by this object.
399 public void Dispose()
402 GC.SuppressFinalize(this);
405 internal void Dispose(bool disposing)
420 internal IDictionary<string, StyleBase> GetStyleDictionary()
422 return styleDictionary;
425 internal IDictionary<string, Bundle> GetExtenderDictionary()
427 return extenderDictionary;
430 internal StyleBase GetStyle(string key)
432 if (string.IsNullOrEmpty(key))
434 throw NotificationErrorFactory.GetException(NotificationError.InvalidParameter, "Key is null or empty");
437 StyleBase style = null;
438 bool ret = styleDictionary.TryGetValue(key, out style);
441 throw NotificationErrorFactory.GetException(NotificationError.InvalidParameter, "The Style object matched input key is not existed");
449 NotificationBinder.BindObject(this);
451 foreach (string key in GetExtenderDictionary().Keys)
453 Log.Info(LogTag, "Start to bind Notification.ExtenderData to SafeHandle");
454 Interop.Notification.SetExtentionData(Handle, key, extenderDictionary[key].SafeBundleHandle);
457 foreach (Notification.StyleBase style in styleDictionary.Values)
459 Log.Info(LogTag, "Start to bind Notification.Style to SafeHandle [" + style.Key + "]");
463 if (Accessory != null)
465 Log.Info(LogTag, "Start to bind Notification.AccessetSet to SafeHandle");
466 Accessory.Make(this);
469 if (Progress != null)
471 Log.Info(LogTag, "Start to bind Notification.Progress to SafeHandle");
476 internal Notification Build()
478 IntPtr extention = IntPtr.Zero;
479 IntPtr extentionBundlePtr = IntPtr.Zero;
481 NotificationBinder.BindSafeHandle(this);
483 Interop.Notification.GetExtentionBundle(Handle, out extention, out extentionBundlePtr);
485 if (extention != IntPtr.Zero)
487 Bundle bundle = new Bundle(new SafeBundleHandle(extention, false));
488 foreach (string key in bundle.Keys)
490 SafeBundleHandle sbh;
491 Interop.Notification.GetExtentionData(Handle, key, out sbh);
492 extenderDictionary.Add(key, new Bundle(sbh));
496 ProgressBinder.BindSafeHandle(this);
497 AccessorySetBinder.BindSafeHandle(this);
498 IndicatorBinder.BindSafeHandle(this);
499 ActiveBinder.BindSafeHandle(this);
500 LockBinder.BindSafehandle(this);