modify WatchApplication window type
[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 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         /// <exception cref="InvalidOperationException">Thrown when failed to get current time because of invalid parameter.</exception>
163         /// <exception cref="OutOfMemoryException">Thrown when failed to get current time because memory is not enough.</exception>
164         /// <example>
165         /// <code>
166         /// class MyApp : WatchApplication
167         /// {
168         ///     ...
169         ///     public void TestMethod()
170         ///     {
171         ///         WatchTime wt;
172         ///         try
173         ///         {
174         ///             wt = GetCurrentTime();
175         ///         }
176         ///         catch
177         ///         {
178         ///         }
179         ///     }
180         /// }
181         /// </code>
182         /// </example>
183         protected WatchTime GetCurrentTime()
184         {
185             SafeWatchTimeHandle handle;
186
187             Interop.Watch.ErrorCode err = Interop.Watch.WatchTimeGetCurrentTime(out handle);
188             if (err != Interop.Watch.ErrorCode.None)
189             {
190                 if (err == Interop.Watch.ErrorCode.InvalidParameter)
191                     throw new InvalidOperationException("Failed to get current time. err : " + err);
192                 else if (err == Interop.Watch.ErrorCode.OutOfMemory)
193                     throw new OutOfMemoryException("Failed to get current time. err : " + err);
194             }
195             return new WatchTime(handle);
196         }
197
198         /// <summary>
199         /// Gets the type of periodic ambient tick.
200         /// </summary>
201         /// <returns>AmbientTickType</returns>
202         /// <exception cref="InvalidOperationException">Thrown when failed to get ambient tick type.</exception>
203         /// <example>
204         /// <code>
205         /// class MyApp : WatchApplication
206         /// {
207         ///     ...
208         ///     public void TestMethod()
209         ///     {
210         ///         AmbientTickType atType;
211         ///         try
212         ///         {
213         ///             atType = GetAmbientTickType();
214         ///         }
215         ///         catch
216         ///         {
217         ///         }
218         ///     }
219         /// }
220         /// </code>
221         /// </example>
222         protected AmbientTickType GetAmbientTickType()
223         {
224             AmbientTickType ambientTickType;
225
226             Interop.Watch.ErrorCode err = Interop.Watch.GetAmbientTickType(out ambientTickType);
227
228             if(err != Interop.Watch.ErrorCode.None)
229             {
230                 throw new InvalidOperationException("Failed to get ambient tick type. err : " + err);
231             }
232
233             return ambientTickType;
234         }
235
236         /// <summary>
237         /// Sets the type of periodic ambient tick.
238         /// OnAmbientTick will be called following settings.
239         /// If SetAmbientTickType is not called, OnAmbientTick will be called every minute.
240         /// </summary>
241         /// <param name="ambientTickType">the type of ambient tick</param>
242         /// <exception cref="InvalidOperationException">Thrown when failed to set ambient tick type.</exception>
243         /// <example>
244         /// <code>
245         /// class MyApp : WatchApplication
246         /// {
247         ///     ...
248         ///     public void TestMethod()
249         ///     {
250         ///         try
251         ///         {
252         ///             SetAmbientTickType(AmbientTickType.EveryMinute);
253         ///         }
254         ///         catch
255         ///         {
256         ///         }
257         ///     }
258         /// }
259         /// </code>
260         /// </example>
261         protected void SetAmbientTickType(AmbientTickType ambientTickType)
262         {
263             Interop.Watch.ErrorCode err = Interop.Watch.SetAmbientTickType(ambientTickType);
264
265             if(err != Interop.Watch.ErrorCode.None)
266             {
267                 throw new InvalidOperationException("Failed to set ambient tick type. err : " + err);
268             }
269         }
270
271         /// <summary>
272         /// Sets the frequency of time tick.
273         /// OnTick will be called following settings.
274         /// If SetTimeTickFrequency is not called, OnTick will be called every second.
275         /// </summary>
276         /// <param name="ticks">Ticks the number of ticks per given resolution type</param>
277         /// <param name="type">Type of the resolution type</param>
278         /// <exception cref="InvalidOperationException">Thrown when failed to set time tick frequency.</exception>
279         /// <example>
280         /// <code>
281         /// class MyApp : WatchApplication
282         /// {
283         ///     ...
284         ///     public void TestMethod()
285         ///     {
286         ///         try
287         ///         {
288         ///             SetTimeTickFrequency(1, TimeTickResolution.TimeTicksPerMinute);
289         ///         }
290         ///         catch
291         ///         {
292         ///         }
293         ///     }
294         /// }
295         /// </code>
296         /// </example>
297         protected void SetTimeTickFrequency(int ticks, TimeTickResolution type)
298         {
299             Interop.Watch.ErrorCode err = Interop.Watch.SetTimeTickFrequency(ticks, type);
300
301             if (err != Interop.Watch.ErrorCode.None)
302             {
303                 throw new InvalidOperationException("Failed to set time tick frequency. err : " + err);
304             }
305         }
306
307         /// <summary>
308         /// Gets the frequency fo time tick.
309         /// </summary>
310         /// <param name="ticks">Ticks the number of ticks per given resolution type</param>
311         /// <param name="type">Type of the resolution type</param>
312         /// <exception cref="InvalidOperationException">Thrown when failed to get time tick frequency.</exception>
313         /// <example>
314         /// <code>
315         /// class MyApp : WatchApplication
316         /// {
317         ///     ...
318         ///     public void TestMethod()
319         ///     {
320         ///         int tick;
321         ///         TimeTickResolution tType;
322         ///         try
323         ///         {
324         ///             GetTimeTickFrequency(out tick, out tType);
325         ///         }
326         ///         catch
327         ///         {
328         ///         }
329         ///     }
330         /// }
331         /// </code>
332         /// </example>
333         protected void GetTimeTickFrequency(out int ticks, out TimeTickResolution type)
334         {
335             Interop.Watch.ErrorCode err = Interop.Watch.GetTimeTickFrequency(out ticks, out type);
336
337             if (err != Interop.Watch.ErrorCode.None)
338             {
339                 throw new InvalidOperationException("Failed to get time tick frequency. err : " + err);
340             }
341         }
342     }
343 }