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 public class WatchApplication : CoreApplication
29 /// Initialize the WatchApplication class
32 /// Default backend for Watch application will be used.
34 public WatchApplication() : base(new WatchCoreBackend())
39 /// Initialize the WatchApplication class
42 /// If want to change the backend , use this constructor
44 /// <param name="backend">The backend instance implementing ICoreBackend interface.</param>
45 public WatchApplication(ICoreBackend backend) : base(backend)
50 /// Instance for the window
52 protected Window Window;
55 /// Occurs whenever the application is resumed.
57 public event EventHandler Resumed;
60 /// Occurs whenever the application is paused.
62 public event EventHandler Paused;
65 /// Occurs whenever the time tick comes.
67 public event EventHandler<TimeEventArgs> TimeTick;
70 /// Occurs whenever the time tick comes in ambient mode.
72 public event EventHandler<TimeEventArgs> AmbientTick;
75 /// Occurs when the ambient mode is changed.
77 public event EventHandler<AmbientEventArgs> AmbientChanged;
80 /// Runs the UI applications' main loop.
82 /// <param name="args">Arguments from commandline.</param>
83 public override void Run(string[] args)
85 Backend.AddEventHandler(EventType.Resumed, OnResume);
86 Backend.AddEventHandler(EventType.Paused, OnPause);
88 Backend.AddEventHandler<TimeEventArgs>(WatchEventType.TimeTick, OnTick);
89 Backend.AddEventHandler<TimeEventArgs>(WatchEventType.AmbientTick, OnAmbientTick);
90 Backend.AddEventHandler<AmbientEventArgs>(WatchEventType.AmbientChanged, OnAmbientChanged);
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.
99 protected override void OnCreate()
104 Interop.Watch.GetWin(out win);
105 Window = new WatchWindow(win);
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.
113 protected virtual void OnResume()
115 Resumed?.Invoke(this, EventArgs.Empty);
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.
122 protected virtual void OnPause()
124 Paused?.Invoke(this, EventArgs.Empty);
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.
131 /// <param name="time">The received TimeEventArgs to get time information.</param>
132 protected virtual void OnTick(TimeEventArgs time)
134 TimeTick?.Invoke(this, time);
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.
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)
145 AmbientTick?.Invoke(this, time);
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.
152 /// <param name="mode">The received AmbientEventArgs</param>
153 protected virtual void OnAmbientChanged(AmbientEventArgs mode)
155 AmbientChanged?.Invoke(this, mode);
159 /// Gets the current time
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>
168 /// class MyApp : WatchApplication
171 /// public void TestMethod()
176 /// wt = GetCurrentTime();
185 protected WatchTime GetCurrentTime()
187 SafeWatchTimeHandle handle;
189 Interop.Watch.ErrorCode err = Interop.Watch.WatchTimeGetCurrentTime(out handle);
190 if (err != Interop.Watch.ErrorCode.None)
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);
199 return new WatchTime(handle);
203 /// Gets the type of periodic ambient tick.
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>
211 /// class MyApp : WatchApplication
214 /// public void TestMethod()
216 /// AmbientTickType atType;
219 /// atType = GetAmbientTickType();
228 protected AmbientTickType GetAmbientTickType()
230 AmbientTickType ambientTickType;
232 Interop.Watch.ErrorCode err = Interop.Watch.GetAmbientTickType(out ambientTickType);
234 if(err != Interop.Watch.ErrorCode.None)
236 if (err == Interop.Watch.ErrorCode.NotSupported)
237 throw new NotSupportedException("Failed to get ambient tick type. err : " + err);
239 throw new InvalidOperationException("Failed to get ambient tick type. err : " + err);
242 return ambientTickType;
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.
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>
256 /// class MyApp : WatchApplication
259 /// public void TestMethod()
263 /// SetAmbientTickType(AmbientTickType.EveryMinute);
272 protected void SetAmbientTickType(AmbientTickType ambientTickType)
274 Interop.Watch.ErrorCode err = Interop.Watch.SetAmbientTickType(ambientTickType);
276 if(err != Interop.Watch.ErrorCode.None)
278 if (err == Interop.Watch.ErrorCode.NotSupported)
279 throw new NotSupportedException("Failed to set ambient tick type. err : " + err);
281 throw new InvalidOperationException("Failed to set ambient tick type. err : " + err);
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.
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>
297 /// class MyApp : WatchApplication
300 /// public void TestMethod()
304 /// SetTimeTickFrequency(1, TimeTickResolution.TimeTicksPerMinute);
313 protected void SetTimeTickFrequency(int ticks, TimeTickResolution type)
315 Interop.Watch.ErrorCode err = Interop.Watch.SetTimeTickFrequency(ticks, type);
317 if (err != Interop.Watch.ErrorCode.None)
319 if (err == Interop.Watch.ErrorCode.NotSupported)
320 throw new NotSupportedException("Failed to set time tick frequency. err : " + err);
322 throw new InvalidOperationException("Failed to set time tick frequency. err : " + err);
327 /// Gets the frequency fo time tick.
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>
336 /// class MyApp : WatchApplication
339 /// public void TestMethod()
342 /// TimeTickResolution tType;
345 /// GetTimeTickFrequency(out tick, out tType);
354 protected void GetTimeTickFrequency(out int ticks, out TimeTickResolution type)
356 Interop.Watch.ErrorCode err = Interop.Watch.GetTimeTickFrequency(out ticks, out type);
358 if (err != Interop.Watch.ErrorCode.None)
360 if (err == Interop.Watch.ErrorCode.NotSupported)
361 throw new NotSupportedException("Failed to get time tick frequency. err : " + err);
363 throw new InvalidOperationException("Failed to get time tick frequency. err : " + err);