2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 using Tizen.Applications.CoreBackend;
21 namespace Tizen.Applications
24 /// The class that represents a Tizen watch application.
26 /// <since_tizen> 4 </since_tizen>
27 public class WatchApplication : CoreApplication
30 /// Initialize the WatchApplication class
33 /// Default backend for Watch application will be used.
35 /// <since_tizen> 4 </since_tizen>
36 public WatchApplication() : base(new WatchCoreBackend())
41 /// Initialize the WatchApplication class
44 /// If want to change the backend , use this constructor
46 /// <param name="backend">The backend instance implementing ICoreBackend interface.</param>
47 /// <since_tizen> 4 </since_tizen>
48 public WatchApplication(ICoreBackend backend) : base(backend)
53 /// Instance for the window
55 /// <since_tizen> 4 </since_tizen>
56 protected Window Window;
59 /// Occurs whenever the application is resumed.
61 /// <since_tizen> 4 </since_tizen>
62 public event EventHandler Resumed;
65 /// Occurs whenever the application is paused.
67 /// <since_tizen> 4 </since_tizen>
68 public event EventHandler Paused;
71 /// Occurs whenever the time tick comes.
73 /// <since_tizen> 4 </since_tizen>
74 public event EventHandler<TimeEventArgs> TimeTick;
77 /// Occurs whenever the time tick comes in ambient mode.
79 /// <since_tizen> 4 </since_tizen>
80 public event EventHandler<TimeEventArgs> AmbientTick;
83 /// Occurs when the ambient mode is changed.
85 /// <since_tizen> 4 </since_tizen>
86 public event EventHandler<AmbientEventArgs> AmbientChanged;
89 /// Runs the UI applications' main loop.
91 /// <param name="args">Arguments from commandline.</param>
92 /// <since_tizen> 4 </since_tizen>
93 public override void Run(string[] args)
95 Backend.AddEventHandler(EventType.Resumed, OnResume);
96 Backend.AddEventHandler(EventType.Paused, OnPause);
98 Backend.AddEventHandler<TimeEventArgs>(WatchEventType.TimeTick, OnTick);
99 Backend.AddEventHandler<TimeEventArgs>(WatchEventType.AmbientTick, OnAmbientTick);
100 Backend.AddEventHandler<AmbientEventArgs>(WatchEventType.AmbientChanged, OnAmbientChanged);
106 /// Overrides this method if want to handle behavior when the application is launched.
107 /// If base.OnCreate() is not called, the event 'Created' will not be emitted.
109 /// <since_tizen> 4 </since_tizen>
110 protected override void OnCreate()
115 Interop.Watch.GetWin(out win);
116 Window = new WatchWindow(win);
121 /// Overrides this method if want to handle behavior when the application is resumed.
122 /// If base.OnResume() is not called, the event 'Resumed' will not be emitted.
124 /// <since_tizen> 4 </since_tizen>
125 protected virtual void OnResume()
127 Resumed?.Invoke(this, EventArgs.Empty);
131 /// Overrides this method if want to handle behavior when the application is paused.
132 /// If base.OnPause() is not called, the event 'Paused' will not be emitted.
134 /// <since_tizen> 4 </since_tizen>
135 protected virtual void OnPause()
137 Paused?.Invoke(this, EventArgs.Empty);
141 /// Overrides this method if want to handle behavior when the time tick event comes.
142 /// If base.OnTick() is not called, the event 'TimeTick' will not be emitted.
144 /// <param name="time">The received TimeEventArgs to get time information.</param>
145 /// <since_tizen> 4 </since_tizen>
146 protected virtual void OnTick(TimeEventArgs time)
148 TimeTick?.Invoke(this, time);
152 /// Overrides this method if want to handle behavior when the time tick event comes in ambient mode.
153 /// If base.OnAmbientTick() is not called, the event 'AmbientTick' will not be emitted.
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)
160 AmbientTick?.Invoke(this, time);
164 /// Overrides this method if want to handle behavior when the ambient mode is changed.
165 /// If base.OnAmbientChanged() is not called, the event 'AmbientChanged' will not be emitted.
167 /// <param name="mode">The received AmbientEventArgs</param>
168 /// <since_tizen> 4 </since_tizen>
169 protected virtual void OnAmbientChanged(AmbientEventArgs mode)
171 AmbientChanged?.Invoke(this, mode);
175 /// Gets the current time
177 /// <returns>WatchTime</returns>
178 /// <feature>http://tizen.org/feature/watch_app</feature>
179 /// <exception cref="InvalidOperationException">Thrown when failed to get current time because of invalid parameter.</exception>
180 /// <exception cref="OutOfMemoryException">Thrown when failed to get current time because memory is not enough.</exception>
181 /// <exception cref="NotSupportedException">Thrown when the method is not supported.</exception>
184 /// class MyApp : WatchApplication
187 /// public void TestMethod()
192 /// wt = GetCurrentTime();
201 /// <since_tizen> 4 </since_tizen>
202 protected WatchTime GetCurrentTime()
204 SafeWatchTimeHandle handle;
206 Interop.Watch.ErrorCode err = Interop.Watch.WatchTimeGetCurrentTime(out handle);
207 if (err != Interop.Watch.ErrorCode.None)
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);
216 return new WatchTime(handle);
220 /// Gets the type of periodic ambient tick.
222 /// <returns>AmbientTickType</returns>
223 /// <feature>http://tizen.org/feature/watch_app</feature>
224 /// <exception cref="InvalidOperationException">Thrown when failed to get ambient tick type.</exception>
225 /// <exception cref="NotSupportedException">Thrown when the method is not supported.</exception>
228 /// class MyApp : WatchApplication
231 /// public void TestMethod()
233 /// AmbientTickType atType;
236 /// atType = GetAmbientTickType();
245 /// <since_tizen> 4 </since_tizen>
246 protected AmbientTickType GetAmbientTickType()
248 AmbientTickType ambientTickType;
250 Interop.Watch.ErrorCode err = Interop.Watch.GetAmbientTickType(out ambientTickType);
252 if(err != Interop.Watch.ErrorCode.None)
254 if (err == Interop.Watch.ErrorCode.NotSupported)
255 throw new NotSupportedException("Failed to get ambient tick type. err : " + err);
257 throw new InvalidOperationException("Failed to get ambient tick type. err : " + err);
260 return ambientTickType;
264 /// Sets the type of periodic ambient tick.
265 /// OnAmbientTick will be called following settings.
266 /// If SetAmbientTickType is not called, OnAmbientTick will be called every minute.
268 /// <param name="ambientTickType">the type of ambient tick</param>
269 /// <feature>http://tizen.org/feature/watch_app</feature>
270 /// <exception cref="InvalidOperationException">Thrown when failed to set ambient tick type.</exception>
271 /// <exception cref="NotSupportedException">Thrown when the method is not supported.</exception>
274 /// class MyApp : WatchApplication
277 /// public void TestMethod()
281 /// SetAmbientTickType(AmbientTickType.EveryMinute);
290 /// <since_tizen> 4 </since_tizen>
291 protected void SetAmbientTickType(AmbientTickType ambientTickType)
293 Interop.Watch.ErrorCode err = Interop.Watch.SetAmbientTickType(ambientTickType);
295 if(err != Interop.Watch.ErrorCode.None)
297 if (err == Interop.Watch.ErrorCode.NotSupported)
298 throw new NotSupportedException("Failed to set ambient tick type. err : " + err);
300 throw new InvalidOperationException("Failed to set ambient tick type. err : " + err);
305 /// Sets the frequency of time tick.
306 /// OnTick will be called following settings.
307 /// If SetTimeTickFrequency is not called, OnTick will be called every second.
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 time tick frequency.</exception>
313 /// <exception cref="NotSupportedException">Thrown when the method is not supported.</exception>
316 /// class MyApp : WatchApplication
319 /// public void TestMethod()
323 /// SetTimeTickFrequency(1, TimeTickResolution.TimeTicksPerMinute);
332 /// <since_tizen> 4 </since_tizen>
333 protected void SetTimeTickFrequency(int ticks, TimeTickResolution type)
335 Interop.Watch.ErrorCode err = Interop.Watch.SetTimeTickFrequency(ticks, type);
337 if (err != Interop.Watch.ErrorCode.None)
339 if (err == Interop.Watch.ErrorCode.NotSupported)
340 throw new NotSupportedException("Failed to set time tick frequency. err : " + err);
342 throw new InvalidOperationException("Failed to set time tick frequency. err : " + err);
347 /// Gets the frequency fo time tick.
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 time tick frequency.</exception>
353 /// <exception cref="NotSupportedException">Thrown when the method is not supported.</exception>
356 /// class MyApp : WatchApplication
359 /// public void TestMethod()
362 /// TimeTickResolution tType;
365 /// GetTimeTickFrequency(out tick, out tType);
374 /// <since_tizen> 4 </since_tizen>
375 protected void GetTimeTickFrequency(out int ticks, out TimeTickResolution type)
377 Interop.Watch.ErrorCode err = Interop.Watch.GetTimeTickFrequency(out ticks, out type);
379 if (err != Interop.Watch.ErrorCode.None)
381 if (err == Interop.Watch.ErrorCode.NotSupported)
382 throw new NotSupportedException("Failed to get time tick frequency. err : " + err);
384 throw new InvalidOperationException("Failed to get time tick frequency. err : " + err);