Modify exception description in Notification
[platform/core/csapi/tizenfx.git] / src / Tizen.Applications.Notification / Tizen.Applications.Notifications / NotificationActiveStyle.cs
1 /*
2  * Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 namespace Tizen.Applications.Notifications
18 {
19     using System.Collections.Generic;
20
21     /// <summary>
22     /// Class containing common properties and methods of Notifications
23     /// </summary>
24     /// <remarks>
25     /// A notification is a message that is displayed on the notification area.
26     /// It is created to notify information to the user through the application.
27     /// This class helps you to provide method and property for creating notification object.
28     /// </remarks>
29     public sealed partial class Notification
30     {
31         /// <summary>
32         ///  Class for generating Active style notification
33         /// </summary>
34         public sealed class ActiveStyle : StyleBase
35         {
36             private IDictionary<ButtonIndex, ButtonAction> buttonDictionary;
37             private int hideTimeout = 0;
38             private int deleteTimeout = 0;
39
40             /// <summary>
41             /// Initializes a new instance of the <see cref="ActiveStyle"/> class.
42             /// </summary>
43             public ActiveStyle()
44             {
45                 buttonDictionary = new Dictionary<ButtonIndex, ButtonAction>();
46             }
47
48             /// <summary>
49             /// Gets or sets an absolute path for an image file to display on the background of active notification
50             /// </summary>
51             public string BackgroundImage { get; set; }
52
53             /// <summary>
54             /// Gets or sets a value indicating whether the active notification is removed automatically. Default value is true.
55             /// </summary>
56             /// <remarks>
57             /// IsAutoRemove option lets the active notification be removed several seconds after it shows.
58             /// When 'IsAutoRemove' is set as false, the active notification will not be removed as long as the user removes
59             /// the active notification or the app which posted the active notification removes the active notification.
60             /// </remarks>>
61             public bool IsAutoRemove { get; set; } = true;
62
63             /// <summary>
64             /// Gets or sets a ReplyAction to this active notification style.
65             /// </summary>
66             /// <remarks>
67             /// When you add a ReplyAction to the ActiveStyle, the notification UI will show a ReplyAction with button.
68             /// If you set null parameter, ReplyAction is disappeared.
69             /// </remarks>
70             /// <example>
71             /// <code>
72             ///
73             /// ButtonAction button = new ButtonAction
74             /// {
75             ///     Index = ButtonIndex.First,
76             ///     Text = "Yes"
77             ///     Action = new AppControl{ ApplicationId = "org.tizen.app" };
78             /// };
79             ///
80             /// ReplyAction reply = new ReplyAction
81             /// {
82             ///     ParentIndex = ButtonIndex.First;
83             ///     PlaceHolderText = "Please write your reply."
84             ///     ReplyMax = 160,
85             ///     Button = new ButtonAction
86             ///     {
87             ///         Text = "Yes",
88             ///         ImagePath = "image path"
89             ///         Action = new AppControl{ ApplicationId = "org.tizen.app" };
90             ///     };
91             /// };
92             ///
93             /// ActiveStyle active = new ActiveStyle
94             /// {
95             ///     AutoRemove = true,
96             ///     BackgroundImage = "image path",
97             ///     ReplyAction = reply
98             /// };
99             ///
100             /// active.AddButtonAction(button);
101             /// </code>
102             /// </example>
103             public ReplyAction ReplyAction { get; set; }
104
105             /// <summary>
106             /// Gets the key of ActiveStyle
107             /// </summary>
108             internal override string Key
109             {
110                 get
111                 {
112                     return "Active";
113                 }
114             }
115
116             /// <summary>
117             /// Method to set times to hide or delete notification.
118             /// </summary>
119             /// <remarks>
120             /// The time settings for hiding and deleting are only reflected on Tizen TV.
121             /// If you use this API on other profile, this time settings have no effect
122             /// </remarks>
123             /// <param name="hideTime">The value in second when the notification can be hidden from the notification viewer after notification is posted</param>
124             /// <param name="deleteTime">The value in second when the notification can be deleted from the notification list in setting application after notification is posted</param>
125             /// <exception cref="ArgumentException">Thrown when argument is invalid</exception>
126             public void SetRemoveTime(int hideTime, int deleteTime)
127             {
128                 if (hideTime < 0 || deleteTime < 0)
129                 {
130                     throw NotificationErrorFactory.GetException(NotificationError.InvalidParameter, "invalid argument");
131                 }
132
133                 hideTimeout = hideTime;
134                 deleteTimeout = deleteTime;
135             }
136
137             /// <summary>
138             /// Method to get times to hide or delete notification.
139             /// </summary>
140             /// <param name="hideTime">The value in second when the notification can be hidden from the notification viewer after notification is posted</param>
141             /// <param name="deleteTime">The value in second when the notification can be deleted from the notification list in setting application after notification is posted</param>
142             public void GetRemoveTime(out int hideTime, out int deleteTime)
143             {
144                 hideTime = hideTimeout;
145                 deleteTime = deleteTimeout;
146             }
147
148             /// <summary>
149             /// Method to add a button to the active notification style.
150             /// Buttons are displayed on the notification.
151             /// </summary>
152             /// <remarks>
153             /// If you add button that has same index, the button is replaced to latest adding button.
154             /// If you don't set an index on ButtonAction, the index is set sequentially from zero.
155             /// </remarks>
156             /// <param name="button">An ButtonAction for appear to the notification</param>
157             /// <exception cref="ArgumentException">Thrown when argument is invalid</exception>
158             /// <example>
159             /// <code>
160             ///
161             /// ButtonAction button = new ButtonAction
162             /// {
163             ///     Index = 0,
164             ///     Text = "Yes"
165             ///     Action = new AppControl{ ApplicationId = "org.tizen.app" };
166             /// };
167             ///
168             /// ActiveStyle active = new ActiveStyle
169             /// {
170             ///     IsAutoRemove = true,
171             ///     BackgroundImage = "image path",
172             /// };
173             ///
174             /// active.AddButtonAction(button);
175             ///
176             /// </code>
177             /// </example>
178             public void AddButtonAction(ButtonAction button)
179             {
180                 if (button == null)
181                 {
182                     throw NotificationErrorFactory.GetException(NotificationError.InvalidParameter, "invalid ButtonAction object");
183                 }
184
185                 if (button.Index == ButtonIndex.None)
186                 {
187                     button.Index = (ButtonIndex)buttonDictionary.Count;
188                     buttonDictionary.Add(button.Index, button);
189                 }
190                 else if (button.Index >= ButtonIndex.First)
191                 {
192                     if (buttonDictionary.ContainsKey(button.Index))
193                     {
194                         buttonDictionary.Remove(button.Index);
195                     }
196
197                     buttonDictionary.Add(button.Index, button);
198                 }
199             }
200
201             /// <summary>
202             /// Remove the ButtonAction you already add.
203             /// </summary>
204             /// <param name="index">The index to remove a button</param>
205             /// <returns>true if the element is successfully found and removed; otherwise, false</returns>
206             public bool RemoveButtonAction(ButtonIndex index)
207             {
208                 bool ret = buttonDictionary.Remove(index);
209
210                 if (ret == false)
211                 {
212                     Log.Debug(Notification.LogTag, "Invalid key, there is no button matched input index");
213                 }
214                 else
215                 {
216                     Log.Debug(Notification.LogTag, "The button was removed.");
217                 }
218
219                 return ret;
220             }
221
222             /// <summary>
223             /// Gets the ButtonAction of the active notification.
224             /// </summary>
225             /// <param name="index">The index to get a button you already add</param>
226             /// <returns>The ButtonAction object which is you already add</returns>
227             /// <exception cref="ArgumentException">Thrown when argument is invalid</exception>
228             public ButtonAction GetButtonAction(ButtonIndex index)
229             {
230                 ButtonAction button = null;
231
232                 if (buttonDictionary.ContainsKey(index) == true)
233                 {
234                     buttonDictionary.TryGetValue(index, out button);
235                 }
236                 else
237                 {
238                     throw NotificationErrorFactory.GetException(NotificationError.InvalidParameter, "The value is not existed.");
239                 }
240
241                 return button;
242             }
243
244             internal ICollection<ButtonAction> GetButtonAction()
245             {
246                 return buttonDictionary.Values;
247             }
248
249             internal override void Make(Notification notification)
250             {
251                 ActiveBinder.BindObject(notification);
252             }
253         }
254     }
255 }