[NUI] TCSACR-226 code change (#1032)
[platform/core/csapi/tizenfx.git] / src / Tizen.Applications.WatchApplication / Tizen.Applications / WatchApplication.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 System;
18 using Tizen.Applications.CoreBackend;
19 using ElmSharp;
20
21 namespace Tizen.Applications
22 {
23     /// <summary>
24     /// The class that represents the Tizen watch application.
25     /// </summary>
26     /// <since_tizen> 4 </since_tizen>
27     public class WatchApplication : CoreApplication
28     {
29         /// <summary>
30         /// Initializes the WatchApplication class.
31         /// </summary>
32         /// <remarks>
33         /// Default backend for the watch application will be used.
34         /// </remarks>
35         /// <since_tizen> 4 </since_tizen>
36         public WatchApplication() : base(new WatchCoreBackend())
37         {
38         }
39
40         /// <summary>
41         /// Initializes the WatchApplication class.
42         /// </summary>
43         /// <remarks>
44         /// If you want to change the backend, use this constructor.
45         /// </remarks>
46         /// <param name="backend">The backend instance implementing the ICoreBackend interface.</param>
47         /// <since_tizen> 4 </since_tizen>
48         public WatchApplication(ICoreBackend backend) : base(backend)
49         {
50         }
51
52         /// <summary>
53         /// Instances for the window.
54         /// </summary>
55         /// <since_tizen> 4 </since_tizen>
56         protected Window Window;
57
58         /// <summary>
59         /// Occurs whenever the application is resumed.
60         /// </summary>
61         /// <since_tizen> 4 </since_tizen>
62         public event EventHandler Resumed;
63
64         /// <summary>
65         /// Occurs whenever the application is paused.
66         /// </summary>
67         /// <since_tizen> 4 </since_tizen>
68         public event EventHandler Paused;
69
70         /// <summary>
71         /// Occurs whenever the time tick comes.
72         /// </summary>
73         /// <since_tizen> 4 </since_tizen>
74         public event EventHandler<TimeEventArgs> TimeTick;
75
76         /// <summary>
77         /// Occurs whenever the time tick comes in the ambient mode.
78         /// </summary>
79         /// <since_tizen> 4 </since_tizen>
80         public event EventHandler<TimeEventArgs> AmbientTick;
81
82         /// <summary>
83         /// Occurs when the ambient mode is changed.
84         /// </summary>
85         /// <since_tizen> 4 </since_tizen>
86         public event EventHandler<AmbientEventArgs> AmbientChanged;
87
88         /// <summary>
89         /// Runs the UI applications' main loop.
90         /// </summary>
91         /// <param name="args">Arguments from the commandline.</param>
92         /// <since_tizen> 4 </since_tizen>
93         public override void Run(string[] args)
94         {
95             Backend.AddEventHandler(EventType.Resumed, OnResume);
96             Backend.AddEventHandler(EventType.Paused, OnPause);
97
98             Backend.AddEventHandler<TimeEventArgs>(WatchEventType.TimeTick, OnTick);
99             Backend.AddEventHandler<TimeEventArgs>(WatchEventType.AmbientTick, OnAmbientTick);
100             Backend.AddEventHandler<AmbientEventArgs>(WatchEventType.AmbientChanged, OnAmbientChanged);
101
102             base.Run(args);
103         }
104
105         /// <summary>
106         /// Overrides this method to handle the behavior when the application is launched.
107         /// If base.OnCreate() is not called, the event 'Created' will not be emitted.
108         /// </summary>
109         /// <since_tizen> 4 </since_tizen>
110         protected override void OnCreate()
111         {
112             base.OnCreate();
113             IntPtr win;
114
115             Interop.Watch.GetWin(out win);
116             Window = new WatchWindow(win);
117             Window.Show();
118         }
119
120         /// <summary>
121         /// Overrides this method to handle the behavior when the application is resumed.
122         /// If base.OnResume() is not called, the event 'Resumed' will not be emitted.
123         /// </summary>
124         /// <since_tizen> 4 </since_tizen>
125         protected virtual void OnResume()
126         {
127             Resumed?.Invoke(this, EventArgs.Empty);
128         }
129
130         /// <summary>
131         /// Overrides this method to handle the behavior when the application is paused.
132         /// If base.OnPause() is not called, the event 'Paused' will not be emitted.
133         /// </summary>
134         /// <since_tizen> 4 </since_tizen>
135         protected virtual void OnPause()
136         {
137             Paused?.Invoke(this, EventArgs.Empty);
138         }
139
140         /// <summary>
141         /// Overrides this method to handle the behavior when the time tick event comes.
142         /// If base.OnTick() is not called, the event 'TimeTick' will not be emitted.
143         /// </summary>
144         /// <param name="time">The received TimeEventArgs to get the time information.</param>
145         /// <since_tizen> 4 </since_tizen>
146         protected virtual void OnTick(TimeEventArgs time)
147         {
148             TimeTick?.Invoke(this, time);
149         }
150
151         /// <summary>
152         /// Overrides this method to handle the behavior when the time tick event comes in ambient mode.
153         /// If base.OnAmbientTick() is not called, the event 'AmbientTick' will not be emitted.
154         /// </summary>
155         /// <param name="time">The received TimeEventArgs to get time information.</param>
156         /// <privilege>http://tizen.org/privilege/alarm.set</privilege>
157         /// <since_tizen> 4 </since_tizen>
158         protected virtual void OnAmbientTick(TimeEventArgs time)
159         {
160             AmbientTick?.Invoke(this, time);
161         }
162
163         /// <summary>
164         /// Overrides this method to handle the behavior when the ambient mode is changed.
165         /// If base.OnAmbientChanged() is not called, the event 'AmbientChanged' will not be emitted.
166         /// </summary>
167         /// <param name="mode">The received AmbientEventArgs.</param>
168         /// <since_tizen> 4 </since_tizen>
169         protected virtual void OnAmbientChanged(AmbientEventArgs mode)
170         {
171             AmbientChanged?.Invoke(this, mode);
172         }
173
174         /// <summary>
175         /// Gets the current time.
176         /// </summary>
177         /// <returns>WatchTime</returns>
178         /// <feature>http://tizen.org/feature/watch_app</feature>
179         /// <exception cref="InvalidOperationException">Thrown when failed to get the current time because of an invalid parameter.</exception>
180         /// <exception cref="OutOfMemoryException">Thrown when failed to get the current time because the memory is not enough.</exception>
181         /// <exception cref="NotSupportedException">Thrown when the method is not supported.</exception>
182         /// <example>
183         /// <code>
184         /// class MyApp : WatchApplication
185         /// {
186         ///     ...
187         ///     public void TestMethod()
188         ///     {
189         ///         WatchTime wt;
190         ///         try
191         ///         {
192         ///             wt = GetCurrentTime();
193         ///         }
194         ///         catch
195         ///         {
196         ///         }
197         ///     }
198         /// }
199         /// </code>
200         /// </example>
201         /// <since_tizen> 4 </since_tizen>
202         protected WatchTime GetCurrentTime()
203         {
204             SafeWatchTimeHandle handle;
205
206             Interop.Watch.ErrorCode err = Interop.Watch.WatchTimeGetCurrentTime(out handle);
207             if (err != Interop.Watch.ErrorCode.None)
208             {
209                 if (err == Interop.Watch.ErrorCode.InvalidParameter)
210                     throw new InvalidOperationException("Failed to get current time. err : " + err);
211                 else if (err == Interop.Watch.ErrorCode.OutOfMemory)
212                     throw new OutOfMemoryException("Failed to get current time. err : " + err);
213                 else if (err == Interop.Watch.ErrorCode.NotSupported)
214                     throw new NotSupportedException("Failed to get current time. err : " + err);
215             }
216             return new WatchTime(handle);
217         }
218
219         /// <summary>
220         /// Gets the type of the periodic ambient tick.
221         /// </summary>
222         /// <returns>AmbientTickType</returns>
223         /// <feature>http://tizen.org/feature/watch_app</feature>
224         /// <exception cref="InvalidOperationException">Thrown when failed to get the ambient tick type.</exception>
225         /// <exception cref="NotSupportedException">Thrown when the method is not supported.</exception>
226         /// <example>
227         /// <code>
228         /// class MyApp : WatchApplication
229         /// {
230         ///     ...
231         ///     public void TestMethod()
232         ///     {
233         ///         AmbientTickType atType;
234         ///         try
235         ///         {
236         ///             atType = GetAmbientTickType();
237         ///         }
238         ///         catch
239         ///         {
240         ///         }
241         ///     }
242         /// }
243         /// </code>
244         /// </example>
245         /// <since_tizen> 4 </since_tizen>
246         protected AmbientTickType GetAmbientTickType()
247         {
248             AmbientTickType ambientTickType;
249
250             Interop.Watch.ErrorCode err = Interop.Watch.GetAmbientTickType(out ambientTickType);
251
252             if(err != Interop.Watch.ErrorCode.None)
253             {
254                 if (err == Interop.Watch.ErrorCode.NotSupported)
255                     throw new NotSupportedException("Failed to get ambient tick type. err : " + err);
256                 else
257                     throw new InvalidOperationException("Failed to get ambient tick type. err : " + err);
258             }
259
260             return ambientTickType;
261         }
262
263         /// <summary>
264         /// Sets the type of the periodic ambient tick.
265         /// OnAmbientTick will be called for the following settings.
266         /// If the SetAmbientTickType is not called, the OnAmbientTick will be called every minute.
267         /// </summary>
268         /// <param name="ambientTickType">The type of the ambient tick.</param>
269         /// <feature>http://tizen.org/feature/watch_app</feature>
270         /// <exception cref="InvalidOperationException">Thrown when failed to set the ambient tick type.</exception>
271         /// <exception cref="NotSupportedException">Thrown when the method is not supported.</exception>
272         /// <example>
273         /// <code>
274         /// class MyApp : WatchApplication
275         /// {
276         ///     ...
277         ///     public void TestMethod()
278         ///     {
279         ///         try
280         ///         {
281         ///             SetAmbientTickType(AmbientTickType.EveryMinute);
282         ///         }
283         ///         catch
284         ///         {
285         ///         }
286         ///     }
287         /// }
288         /// </code>
289         /// </example>
290         /// <since_tizen> 4 </since_tizen>
291         protected void SetAmbientTickType(AmbientTickType ambientTickType)
292         {
293             Interop.Watch.ErrorCode err = Interop.Watch.SetAmbientTickType(ambientTickType);
294
295             if(err != Interop.Watch.ErrorCode.None)
296             {
297                 if (err == Interop.Watch.ErrorCode.NotSupported)
298                     throw new NotSupportedException("Failed to set ambient tick type. err : " + err);
299                 else
300                     throw new InvalidOperationException("Failed to set ambient tick type. err : " + err);
301             }
302         }
303
304         /// <summary>
305         /// Sets the frequency of the time tick.
306         /// OnTick will be called for the following settings.
307         /// If SetTimeTickFrequency is not called, OnTick will be called every second.
308         /// </summary>
309         /// <param name="ticks">Ticks the number of ticks per given resolution type.</param>
310         /// <param name="type">Type of the resolution type.</param>
311         /// <feature>http://tizen.org/feature/watch_app</feature>
312         /// <exception cref="InvalidOperationException">Thrown when failed to set the time tick frequency.</exception>
313         /// <exception cref="NotSupportedException">Thrown when the method is not supported.</exception>
314         /// <example>
315         /// <code>
316         /// class MyApp : WatchApplication
317         /// {
318         ///     ...
319         ///     public void TestMethod()
320         ///     {
321         ///         try
322         ///         {
323         ///             SetTimeTickFrequency(1, TimeTickResolution.TimeTicksPerMinute);
324         ///         }
325         ///         catch
326         ///         {
327         ///         }
328         ///     }
329         /// }
330         /// </code>
331         /// </example>
332         /// <since_tizen> 4 </since_tizen>
333         protected void SetTimeTickFrequency(int ticks, TimeTickResolution type)
334         {
335             Interop.Watch.ErrorCode err = Interop.Watch.SetTimeTickFrequency(ticks, type);
336
337             if (err != Interop.Watch.ErrorCode.None)
338             {
339                 if (err == Interop.Watch.ErrorCode.NotSupported)
340                     throw new NotSupportedException("Failed to set time tick frequency. err : " + err);
341                 else
342                     throw new InvalidOperationException("Failed to set time tick frequency. err : " + err);
343             }
344         }
345
346         /// <summary>
347         /// Gets the frequency of the time tick.
348         /// </summary>
349         /// <param name="ticks">Ticks the number of ticks per given resolution type.</param>
350         /// <param name="type">Type of the resolution type.</param>
351         /// <feature>http://tizen.org/feature/watch_app</feature>
352         /// <exception cref="InvalidOperationException">Thrown when failed to get the time tick frequency.</exception>
353         /// <exception cref="NotSupportedException">Thrown when the method is not supported.</exception>
354         /// <example>
355         /// <code>
356         /// class MyApp : WatchApplication
357         /// {
358         ///     ...
359         ///     public void TestMethod()
360         ///     {
361         ///         int tick;
362         ///         TimeTickResolution tType;
363         ///         try
364         ///         {
365         ///             GetTimeTickFrequency(out tick, out tType);
366         ///         }
367         ///         catch
368         ///         {
369         ///         }
370         ///     }
371         /// }
372         /// </code>
373         /// </example>
374         /// <since_tizen> 4 </since_tizen>
375         protected void GetTimeTickFrequency(out int ticks, out TimeTickResolution type)
376         {
377             Interop.Watch.ErrorCode err = Interop.Watch.GetTimeTickFrequency(out ticks, out type);
378
379             if (err != Interop.Watch.ErrorCode.None)
380             {
381                 if (err == Interop.Watch.ErrorCode.NotSupported)
382                     throw new NotSupportedException("Failed to get time tick frequency. err : " + err);
383                 else
384                     throw new InvalidOperationException("Failed to get time tick frequency. err : " + err);
385             }
386         }
387     }
388 }