2 * Copyright(c) 2020 Samsung Electronics Co., Ltd.
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.
18 using System.Collections.Generic;
19 using System.ComponentModel;
20 using Tizen.NUI.BaseComponents;
22 namespace Tizen.NUI.Components
25 /// Notification helps to raise a notification window with a content View.
27 /// <since_tizen> 8 </since_tizen>
28 public class Notification : Disposable
30 private static HashSet<Notification> instanceSet;
32 private Window notificationWindow;
36 private NotificationLevel level = NotificationLevel.Base;
38 private Rectangle positionSize;
40 private bool dismissOnTouch = false;
42 private Animation onPostAnimation;
44 private Animation onDismissAnimation;
46 private NotificationState state = NotificationState.Ready;
49 /// Create a notification with a content View.
51 /// <param name="contentView">The content view instance to display in the notification window.</param>
52 /// <exception cref="ArgumentException">Thrown when a given contentView is invalid.</exception>
53 /// <since_tizen> 8 </since_tizen>
54 public Notification(View contentView) : base()
56 ContentView = contentView ?? throw new ArgumentException("Input contentView should not be null.");
59 private enum NotificationState
67 /// The content view received in a constructor.
69 /// <since_tizen> 8 </since_tizen>
70 public View ContentView { get; private set; }
72 private Window NotificationWindow
76 if (notificationWindow == null)
78 notificationWindow = new Window(null, true)
80 Type = WindowType.Notification,
82 notificationWindow.Show();
85 return notificationWindow;
87 set => notificationWindow = value;
97 timer.Tick += OnTimeOut;
107 timer.Tick -= OnTimeOut;
120 /// Post a notification window with the content view.
122 /// <param name="duration">Dismiss the notification window after given time. The value 0 won't dismiss the notification.</param>
123 /// <returns>The current Notification instance.</returns>
124 /// <privilege>http://tizen.org/privilege/window.priority.set</privilege>
125 /// <exception cref="UnauthorizedAccessException">Thrown when the application does not have proper privilege.</exception>
126 /// <since_tizen> 8 </since_tizen>
127 public void Post(uint duration = 0)
129 if (state != NotificationState.Ready)
134 if (!ApplyLevel(level))
136 throw new UnauthorizedAccessException("Cannot post a Notification: Permission Denied");
139 ApplyPositionSize(positionSize);
141 ApplyDismissOnTouch(dismissOnTouch);
143 NotificationWindow.Add(ContentView);
147 Timer.Interval = duration;
150 state = NotificationState.Post;
152 onPostAnimation?.Play();
154 RegisterInstance(this);
158 /// Sets a priority level for the specified notification window.
159 /// The default level is NotificationLevel.Base.
161 /// <param name="level">The notification window level.</param>
162 /// <returns>The current Notification instance.</returns>
163 /// <privilege>http://tizen.org/privilege/window.priority.set</privilege>
164 /// <exception cref="UnauthorizedAccessException">Thrown when the application does not have proper privilege.</exception>
165 /// <since_tizen> 8 </since_tizen>
166 public Notification SetLevel(NotificationLevel level)
170 if (state == NotificationState.Post && !ApplyLevel(level))
172 throw new UnauthorizedAccessException("Cannot set notification level: Permission Denied");
179 /// Sets position and size of the notification window.
181 /// <param name="positionSize">The position and size information in rectangle.</param>
182 /// <returns>The current Notification instance.</returns>
183 /// <exception cref="ArgumentException">Thrown when a given positionSize is invalid.</exception>
184 /// <since_tizen> 8 </since_tizen>
185 public Notification SetPositionSize(Rectangle positionSize)
187 this.positionSize = positionSize ?? throw (new ArgumentException("Input positionSize should not be null."));
189 if (state == NotificationState.Post || state == NotificationState.Dismiss)
191 ApplyPositionSize(positionSize);
198 /// Sets whether listen to touch event to dismiss notification window.
200 /// <param name="dismissOnTouch">Dismiss notification window on touch if the value is true.</param>
201 /// <returns>The current Notification instance.</returns>
202 [EditorBrowsable(EditorBrowsableState.Never)]
203 public Notification SetDismissOnTouch(bool dismissOnTouch)
205 if (this.dismissOnTouch == dismissOnTouch)
210 this.dismissOnTouch = dismissOnTouch;
212 if (state == NotificationState.Post)
214 ApplyDismissOnTouch(dismissOnTouch);
221 /// Sets a user-defined animation to play when posting the notification.
222 /// The Notification will play the given animation right after the notification window pops up.
224 /// <param name="animation">The animation to play.</param>
225 /// <since_tizen> 8 </since_tizen>
226 public Notification SetAnimationOnPost(Animation animation)
228 this.onPostAnimation = animation;
234 /// Sets a user-defined animation to play when dismiss the notification.
235 /// On dismiss, the given animation is played, and after the playback is completed the notification window is undisplayed.
237 /// <param name="animation">The animation to play.</param>
238 /// <since_tizen> 8 </since_tizen>
239 public Notification SetAnimationOnDismiss(Animation animation)
241 this.onDismissAnimation = animation;
247 /// Dismiss the notification window.
249 /// <since_tizen> 8 </since_tizen>
250 public void Dismiss()
252 if (state != NotificationState.Post)
257 state = NotificationState.Dismiss;
259 if (onDismissAnimation != null)
261 onDismissAnimation.Finished += OnAnimationEnd;
263 onDismissAnimation.Play();
267 ApplyDismissOnTouch(false);
276 /// Dismiss the notification window directly without waiting the onDismissAnimation finished.
278 /// <since_tizen> 8 </since_tizen>
279 public void ForceQuit()
281 if (state != NotificationState.Post && state != NotificationState.Dismiss)
290 /// <since_tizen> 8 </since_tizen>
291 protected override void Dispose(DisposeTypes type)
298 if (type == DisposeTypes.Explicit)
302 positionSize?.Dispose();
303 onPostAnimation?.Dispose();
304 onDismissAnimation?.Dispose();
310 private static void RegisterInstance(Notification instance)
312 if (instanceSet == null)
314 instanceSet = new HashSet<Notification>();
317 instanceSet.Add(instance);
320 private static void UnregisterInstance(Notification instance)
322 if (instanceSet == null)
327 instanceSet.Remove(instance);
329 if (instanceSet.Count == 0)
335 private void DestroyNotificationWindow()
337 notificationWindow.Hide();
339 notificationWindow.Dispose();
341 notificationWindow = null;
344 private bool ApplyLevel(NotificationLevel level)
346 return NotificationWindow.SetNotificationLevel(level);
349 private void ApplyPositionSize(Rectangle positionSize)
351 if (positionSize != null)
353 NotificationWindow.SetPositionSize(positionSize);
357 private void ApplyDismissOnTouch(bool dismissOnTouch)
361 NotificationWindow.TouchEvent += OnWindowTouch;
365 NotificationWindow.TouchEvent -= OnWindowTouch;
369 private void ClearAll()
371 if (onDismissAnimation != null)
373 onDismissAnimation.Finished -= OnAnimationEnd;
375 onDismissAnimation.Stop();
378 notificationWindow.Remove(ContentView);
380 notificationWindow.TouchEvent -= OnWindowTouch;
384 DestroyNotificationWindow();
386 state = NotificationState.Ready;
388 UnregisterInstance(this);
391 private void OnWindowTouch(object target, Window.TouchEventArgs args)
396 private bool OnTimeOut(object target, Timer.TickEventArgs args)
403 private void OnAnimationEnd(object target, EventArgs args)