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 Widget 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);
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.
112 protected virtual void OnResume()
114 Resumed?.Invoke(this, EventArgs.Empty);
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.
121 protected virtual void OnPause()
123 Paused?.Invoke(this, EventArgs.Empty);
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.
130 /// <param name="time">The received TimeEventArgs to get time information.</param>
131 protected virtual void OnTick(TimeEventArgs time)
133 TimeTick?.Invoke(this, time);
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.
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)
144 AmbientTick?.Invoke(this, time);
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.
151 /// <param name="mode">The received AmbientEventArgs</param>
152 protected virtual void OnAmbientChanged(AmbientEventArgs mode)
154 AmbientChanged?.Invoke(this, mode);
158 /// Gets the current time
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>
165 /// class MyApp : WatchApplication
168 /// public void TestMethod()
173 /// wt = GetCurrentTime();
182 protected WatchTime GetCurrentTime()
184 SafeWatchTimeHandle handle;
186 Interop.Watch.ErrorCode err = Interop.Watch.WatchTimeGetCurrentTime(out handle);
187 if (err != Interop.Watch.ErrorCode.None)
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);
194 return new WatchTime(handle);
198 /// Gets the type of periodic ambient tick.
200 /// <returns>AmbientTickType</returns>
201 /// <exception cref="InvalidOperationException">Thrown when failed to get ambient tick type.</exception>
204 /// class MyApp : WatchApplication
207 /// public void TestMethod()
209 /// AmbientTickType atType;
212 /// atType = GetAmbientTickType();
221 protected AmbientTickType GetAmbientTickType()
223 AmbientTickType ambientTickType;
225 Interop.Watch.ErrorCode err = Interop.Watch.GetAmbientTickType(out ambientTickType);
227 if(err != Interop.Watch.ErrorCode.None)
229 throw new InvalidOperationException("Failed to get ambient tick type. err : " + err);
232 return ambientTickType;
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.
240 /// <param name="ambientTickType">the type of ambient tick</param>
241 /// <exception cref="InvalidOperationException">Thrown when failed to set ambient tick type.</exception>
244 /// class MyApp : WatchApplication
247 /// public void TestMethod()
251 /// SetAmbientTickType(AmbientTickType.EveryMinute);
260 protected void SetAmbientTickType(AmbientTickType ambientTickType)
262 Interop.Watch.ErrorCode err = Interop.Watch.SetAmbientTickType(ambientTickType);
264 if(err != Interop.Watch.ErrorCode.None)
266 throw new InvalidOperationException("Failed to set ambient tick type. err : " + err);
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.
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>
280 /// class MyApp : WatchApplication
283 /// public void TestMethod()
287 /// SetTimeTickFrequency(1, TimeTickResolution.TimeTicksPerMinute);
296 protected void SetTimeTickFrequency(int ticks, TimeTickResolution type)
298 Interop.Watch.ErrorCode err = Interop.Watch.SetTimeTickFrequency(ticks, type);
300 if (err != Interop.Watch.ErrorCode.None)
302 throw new InvalidOperationException("Failed to set time tick frequency. err : " + err);
307 /// Gets the frequency fo time tick.
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>
314 /// class MyApp : WatchApplication
317 /// public void TestMethod()
320 /// TimeTickResolution tType;
323 /// GetTimeTickFrequency(out tick, out tType);
332 protected void GetTimeTickFrequency(out int ticks, out TimeTickResolution type)
334 Interop.Watch.ErrorCode err = Interop.Watch.GetTimeTickFrequency(out ticks, out type);
336 if (err != Interop.Watch.ErrorCode.None)
338 throw new InvalidOperationException("Failed to get time tick frequency. err : " + err);