c7f8dbf32fea94285fd687771975ddcb0002404f
[platform/core/csapi/tizenfx.git] / src / Tizen.Applications.WidgetApplication / Tizen.Applications / WidgetBase.cs
1 /*
2  * Copyright (c) 2016 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 using ElmSharp;
18 using System;
19
20 namespace Tizen.Applications
21 {
22     /// <summary>
23     /// The abstract class for widget instances.
24     /// </summary>
25     /// <since_tizen> 3 </since_tizen>
26     public abstract class WidgetBase
27     {
28         internal IntPtr Handle;
29         private const string LogTag = "Tizen.Applications.WidgetBase";
30
31         /// <summary>
32         /// Window object for this widget instance.
33         /// It will be created after OnCreate method is invoked.
34         /// </summary>
35         /// <since_tizen> 3 </since_tizen>
36         protected Window Window;
37
38         /// <summary>
39         /// Delete type.
40         /// </summary>
41         /// <since_tizen> 3 </since_tizen>
42         public enum WidgetDestroyType
43         {
44             /// <summary>
45             /// User deleted this widget from the viewer.
46             /// </summary>
47             Permanent = 0,
48
49             /// <summary>
50             /// Widget is deleted because of other reasons. (For example, widget process is terminated temporarily by the system)
51             /// </summary>
52             Temporary
53         }
54
55         /// <summary>
56         /// Constructor.
57         /// </summary>
58         /// <since_tizen> 3 </since_tizen>
59         public WidgetBase()
60         {
61         }
62
63         /// <summary>
64         /// ID for this widget instance.
65         /// It will be created after OnCreate method is invoked.
66         /// </summary>
67         /// <since_tizen> 6 </since_tizen>
68         public string Id { get; private set; }
69
70         /// <summary>
71         /// Sets the content information to the widget.
72         /// </summary>
73         /// <param name="info">The data set to save.</param>
74         /// <feature>http://tizen.org/feature/shell.appwidget</feature>
75         /// <exception cref="ArgumentException">Thrown when failed because of an invalid argument.</exception>
76         /// <exception cref="NotSupportedException">Thrown when the API is not supported in this device.</exception>
77         /// <exception cref="InvalidOperationException">Thrown in case of an unrecoverable error.</exception>
78         /// <since_tizen> 3 </since_tizen>
79         public void SetContent(Bundle info)
80         {
81             Interop.Widget.ErrorCode err = Interop.Widget.SetContent(Handle, info.SafeBundleHandle);
82
83             if (err != Interop.Widget.ErrorCode.None)
84             {
85                 Log.Error(LogTag, "Failed to set content. Err = " + err);
86
87                 switch(err)
88                 {
89                     case Interop.Widget.ErrorCode.InvalidParameter:
90                         throw new ArgumentException("Invalid parameter of SetContent()");
91
92                     case Interop.Widget.ErrorCode.NotSupported:
93                         throw new NotSupportedException();
94
95                     case Interop.Widget.ErrorCode.OutOfMemory:
96                     case Interop.Widget.ErrorCode.Fault:
97                         throw new InvalidOperationException();
98                 }
99
100             }
101         }
102
103         /// <summary>
104         /// Sends the title to the widget.
105         /// </summary>
106         /// <param name="title">When an accessibility mode is turned on, this string will be read.</param>
107         /// <feature>http://tizen.org/feature/shell.appwidget</feature>
108         /// <exception cref="ArgumentException">Thrown when failed because of an invalid argument.</exception>
109         /// <exception cref="NotSupportedException">Thrown when the API is not supported in this device.</exception>
110         /// <exception cref="InvalidOperationException">Thrown in case of an unrecoverable error.</exception>
111         /// <since_tizen> 3 </since_tizen>
112         public void SetTitle(string title)
113         {
114             Interop.Widget.ErrorCode err = Interop.Widget.SetTitle(Handle, title);
115
116             if (err != Interop.Widget.ErrorCode.None)
117             {
118                 Log.Error(LogTag, "Failed to set title. Err = " + err);
119
120                 switch (err)
121                 {
122                     case Interop.Widget.ErrorCode.InvalidParameter:
123                         throw new ArgumentException("Invalid parameter of SetContent()");
124
125                     case Interop.Widget.ErrorCode.NotSupported:
126                         throw new NotSupportedException();
127
128                     case Interop.Widget.ErrorCode.OutOfMemory:
129                     case Interop.Widget.ErrorCode.Fault:
130                         throw new InvalidOperationException();
131                 }
132             }
133         }
134
135         /// <summary>
136         /// Finishes the context for the widget instance.
137         /// </summary>
138         /// <feature>http://tizen.org/feature/shell.appwidget</feature>
139         /// <exception cref="NotSupportedException">Thrown when the API is not supported in this device.</exception>
140         /// <exception cref="InvalidOperationException">Thrown in case of an unrecoverable error.</exception>
141         /// <since_tizen> 3 </since_tizen>
142         public void Exit()
143         {
144             Interop.Widget.ErrorCode err = Interop.Widget.TerminateContext(Handle);
145
146             if (err != Interop.Widget.ErrorCode.None)
147             {
148                 Log.Error(LogTag, "Failed to terminate context. Err = " + err);
149
150                 switch (err)
151                 {
152                     case Interop.Widget.ErrorCode.NotSupported:
153                         throw new NotSupportedException();
154
155                     case Interop.Widget.ErrorCode.InvalidParameter:
156                     case Interop.Widget.ErrorCode.Fault:
157                         throw new InvalidOperationException();
158                 }
159             }
160         }
161
162         internal void Bind(IntPtr handle, string id)
163         {
164             Handle = handle;
165             Id = id;
166         }
167
168         /// <summary>
169         /// Overrides this method if want to handle the behavior when the widget instance is started.
170         /// </summary>
171         /// <param name="content">The data set for the previous status.</param>
172         /// <param name="w">The pixel value for the widget width.</param>
173         /// <param name="h">The pixel value for the widget height.</param>
174         /// <since_tizen> 3 </since_tizen>
175         public virtual void OnCreate(Bundle content, int w, int h)
176         {
177             IntPtr win;
178
179             Interop.Widget.GetWin(Handle, out win);
180             Window = new WidgetWindow(win);
181             Window.Resize(w, h);
182             Window.Show();
183         }
184
185         /// <summary>
186         /// Overrides this method if want to handle the behavior when the widget instance is destroyed.
187         /// </summary>
188         /// <param name="reason">The reason for destruction.</param>
189         /// <param name="content">The data set to save.</param>
190         /// <since_tizen> 3 </since_tizen>
191         public virtual void OnDestroy(WidgetDestroyType reason, Bundle content)
192         {
193         }
194
195         /// <summary>
196         /// Overrides this method if want to handle the behavior when the widget instance is paused.
197         /// </summary>
198         /// <since_tizen> 3 </since_tizen>
199         public virtual void OnPause()
200         {
201         }
202
203         /// <summary>
204         /// Overrides this method if want to handle the behavior when the widget instance is resumed.
205         /// </summary>
206         /// <since_tizen> 3 </since_tizen>
207         public virtual void OnResume()
208         {
209         }
210
211         /// <summary>
212         /// Overrides this method if want to handle the behavior when the widget instance is resized.
213         /// </summary>
214         /// <param name="w">Widget width.</param>
215         /// <param name="h">Widget height.</param>
216         /// <since_tizen> 3 </since_tizen>
217         public virtual void OnResize(int w, int h)
218         {
219         }
220
221         /// <summary>
222         /// Overrides this method if want to handle the behavior when the widget instance is updated.
223         /// </summary>
224         /// <param name="content">The data set for updating this widget will be provided by the requester.</param>
225         /// <param name="isForce">Although the widget is paused, if it is true, the widget can be updated.</param>
226         /// <since_tizen> 3 </since_tizen>
227         public virtual void OnUpdate(Bundle content, bool isForce)
228         {
229         }
230
231     }
232 }