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