Add watch-application initial Code
[platform/core/csapi/watch-application.git] / 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 Widget 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         }
107
108         /// <summary>
109         /// Overrides this method if want to handle behavior when the application is resumed.
110         /// If base.OnResume() is not called, the event 'Resumed' will not be emitted.
111         /// </summary>
112         protected virtual void OnResume()
113         {
114             Resumed?.Invoke(this, EventArgs.Empty);
115         }
116
117         /// <summary>
118         /// Overrides this method if want to handle behavior when the application is paused.
119         /// If base.OnPause() is not called, the event 'Paused' will not be emitted.
120         /// </summary>
121         protected virtual void OnPause()
122         {
123             Paused?.Invoke(this, EventArgs.Empty);
124         }
125
126         /// <summary>
127         /// Overrides this method if want to handle behavior when the time tick event comes.
128         /// If base.OnTick() is not called, the event 'TimeTick' will not be emitted.
129         /// </summary>
130         /// <param name="time">The received TimeEventArgs to get time information.</param>
131         protected virtual void OnTick(TimeEventArgs time)
132         {
133             TimeTick?.Invoke(this, time);
134         }\r
135 \r
136         /// <summary>
137         /// Overrides this method if want to handle behavior when the time tick event comes in ambient mode.
138         /// If base.OnAmbientTick() is not called, the event 'AmbientTick' will not be emitted.
139         /// </summary>
140         /// <param name="time">The received TimeEventArgs to get time information.</param>
141         /// <privilege>http://tizen.org/privilege/alarm.set</privilege>
142         protected virtual void OnAmbientTick(TimeEventArgs time)
143         {
144             AmbientTick?.Invoke(this, time);
145         }
146
147         /// <summary>
148         /// Overrides this method if want to handle behavior when the ambient mode is changed.
149         /// If base.OnAmbientChanged() is not called, the event 'AmbientChanged' will not be emitted.
150         /// </summary>
151         /// <param name="mode">The received AmbientEventArgs</param>
152         protected virtual void OnAmbientChanged(AmbientEventArgs mode)
153         {
154             AmbientChanged?.Invoke(this, mode);
155         }
156
157         /// <summary>
158         /// Gets the current time
159         /// </summary>
160         /// <returns>WatchTime</returns>
161         /// <exception cref="InvalidOperationException">Thrown when failed to get current time because of invalid parameter.</exception>
162         /// <exception cref="OutOfMemoryException">Thrown when failed to get current time because memory is not enough.</exception>
163         /// <example>
164         /// <code>
165         /// class MyApp : WatchApplication
166         /// {
167         ///     ...
168         ///     public void TestMethod()
169         ///     {
170         ///         WatchTime wt;
171         ///         try
172         ///         {
173         ///             wt = GetCurrentTime();
174         ///         }
175         ///         catch
176         ///         {
177         ///         }
178         ///     }
179         /// }
180         /// </code>
181         /// </example>
182         protected WatchTime GetCurrentTime()
183         {
184             SafeWatchTimeHandle handle;
185
186             Interop.Watch.ErrorCode err = Interop.Watch.WatchTimeGetCurrentTime(out handle);
187             if (err != Interop.Watch.ErrorCode.None)
188             {
189                 if (err == Interop.Watch.ErrorCode.InvalidParameter)
190                     throw new InvalidOperationException("Failed to get current time. err : " + err);
191                 else if (err == Interop.Watch.ErrorCode.OutOfMemory)
192                     throw new OutOfMemoryException("Failed to get current time. err : " + err);
193             }
194             return new WatchTime(handle);
195         }
196
197         /// <summary>
198         /// Gets the type of periodic ambient tick.
199         /// </summary>
200         /// <returns>AmbientTickType</returns>
201         /// <exception cref="InvalidOperationException">Thrown when failed to get ambient tick type.</exception>
202         /// <example>
203         /// <code>
204         /// class MyApp : WatchApplication
205         /// {
206         ///     ...
207         ///     public void TestMethod()
208         ///     {
209         ///         AmbientTickType atType;
210         ///         try
211         ///         {
212         ///             atType = GetAmbientTickType();
213         ///         }
214         ///         catch
215         ///         {
216         ///         }
217         ///     }
218         /// }
219         /// </code>
220         /// </example>
221         protected AmbientTickType GetAmbientTickType()
222         {
223             AmbientTickType ambientTickType;
224
225             Interop.Watch.ErrorCode err = Interop.Watch.GetAmbientTickType(out ambientTickType);
226
227             if(err != Interop.Watch.ErrorCode.None)
228             {
229                 throw new InvalidOperationException("Failed to get ambient tick type. err : " + err);
230             }
231
232             return ambientTickType;
233         }
234
235         /// <summary>
236         /// Sets the type of periodic ambient tick.
237         /// OnAmbientTick will be called following settings.
238         /// If SetAmbientTickType is not called, OnAmbientTick will be called every minute.
239         /// </summary>
240         /// <param name="ambientTickType">the type of ambient tick</param>
241         /// <exception cref="InvalidOperationException">Thrown when failed to set ambient tick type.</exception>
242         /// <example>
243         /// <code>
244         /// class MyApp : WatchApplication
245         /// {
246         ///     ...
247         ///     public void TestMethod()
248         ///     {
249         ///         try
250         ///         {
251         ///             SetAmbientTickType(AmbientTickType.EveryMinute);
252         ///         }
253         ///         catch
254         ///         {
255         ///         }
256         ///     }
257         /// }
258         /// </code>
259         /// </example>
260         protected void SetAmbientTickType(AmbientTickType ambientTickType)
261         {
262             Interop.Watch.ErrorCode err = Interop.Watch.SetAmbientTickType(ambientTickType);
263
264             if(err != Interop.Watch.ErrorCode.None)
265             {
266                 throw new InvalidOperationException("Failed to set ambient tick type. err : " + err);
267             }
268         }
269
270         /// <summary>
271         /// Sets the frequency of time tick.
272         /// OnTick will be called following settings.
273         /// If SetTimeTickFrequency is not called, OnTick will be called every second.
274         /// </summary>
275         /// <param name="ticks">Ticks the number of ticks per given resolution type</param>
276         /// <param name="type">Type of the resolution type</param>
277         /// <exception cref="InvalidOperationException">Thrown when failed to set time tick frequency.</exception>
278         /// <example>
279         /// <code>
280         /// class MyApp : WatchApplication
281         /// {
282         ///     ...
283         ///     public void TestMethod()
284         ///     {
285         ///         try
286         ///         {
287         ///             SetTimeTickFrequency(1, TimeTickResolution.TimeTicksPerMinute);
288         ///         }
289         ///         catch
290         ///         {
291         ///         }
292         ///     }
293         /// }
294         /// </code>
295         /// </example>
296         protected void SetTimeTickFrequency(int ticks, TimeTickResolution type)
297         {
298             Interop.Watch.ErrorCode err = Interop.Watch.SetTimeTickFrequency(ticks, type);
299
300             if (err != Interop.Watch.ErrorCode.None)
301             {
302                 throw new InvalidOperationException("Failed to set time tick frequency. err : " + err);
303             }
304         }
305
306         /// <summary>
307         /// Gets the frequency fo time tick.
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         /// <exception cref="InvalidOperationException">Thrown when failed to get time tick frequency.</exception>
312         /// <example>
313         /// <code>
314         /// class MyApp : WatchApplication
315         /// {
316         ///     ...
317         ///     public void TestMethod()
318         ///     {
319         ///         int tick;
320         ///         TimeTickResolution tType;
321         ///         try
322         ///         {
323         ///             GetTimeTickFrequency(out tick, out tType);
324         ///         }
325         ///         catch
326         ///         {
327         ///         }
328         ///     }
329         /// }
330         /// </code>
331         /// </example>
332         protected void GetTimeTickFrequency(out int ticks, out TimeTickResolution type)
333         {
334             Interop.Watch.ErrorCode err = Interop.Watch.GetTimeTickFrequency(out ticks, out type);
335
336             if (err != Interop.Watch.ErrorCode.None)
337             {
338                 throw new InvalidOperationException("Failed to get time tick frequency. err : " + err);
339             }
340         }
341     }
342 }