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 /// <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>
166 /// class MyApp : WatchApplication
169 /// public void TestMethod()
174 /// wt = GetCurrentTime();
183 protected WatchTime GetCurrentTime()
185 SafeWatchTimeHandle handle;
187 Interop.Watch.ErrorCode err = Interop.Watch.WatchTimeGetCurrentTime(out handle);
188 if (err != Interop.Watch.ErrorCode.None)
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);
195 return new WatchTime(handle);
199 /// Gets the type of periodic ambient tick.
201 /// <returns>AmbientTickType</returns>
202 /// <exception cref="InvalidOperationException">Thrown when failed to get ambient tick type.</exception>
205 /// class MyApp : WatchApplication
208 /// public void TestMethod()
210 /// AmbientTickType atType;
213 /// atType = GetAmbientTickType();
222 protected AmbientTickType GetAmbientTickType()
224 AmbientTickType ambientTickType;
226 Interop.Watch.ErrorCode err = Interop.Watch.GetAmbientTickType(out ambientTickType);
228 if(err != Interop.Watch.ErrorCode.None)
230 throw new InvalidOperationException("Failed to get ambient tick type. err : " + err);
233 return ambientTickType;
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.
241 /// <param name="ambientTickType">the type of ambient tick</param>
242 /// <exception cref="InvalidOperationException">Thrown when failed to set ambient tick type.</exception>
245 /// class MyApp : WatchApplication
248 /// public void TestMethod()
252 /// SetAmbientTickType(AmbientTickType.EveryMinute);
261 protected void SetAmbientTickType(AmbientTickType ambientTickType)
263 Interop.Watch.ErrorCode err = Interop.Watch.SetAmbientTickType(ambientTickType);
265 if(err != Interop.Watch.ErrorCode.None)
267 throw new InvalidOperationException("Failed to set ambient tick type. err : " + err);
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.
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>
281 /// class MyApp : WatchApplication
284 /// public void TestMethod()
288 /// SetTimeTickFrequency(1, TimeTickResolution.TimeTicksPerMinute);
297 protected void SetTimeTickFrequency(int ticks, TimeTickResolution type)
299 Interop.Watch.ErrorCode err = Interop.Watch.SetTimeTickFrequency(ticks, type);
301 if (err != Interop.Watch.ErrorCode.None)
303 throw new InvalidOperationException("Failed to set time tick frequency. err : " + err);
308 /// Gets the frequency fo time tick.
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>
315 /// class MyApp : WatchApplication
318 /// public void TestMethod()
321 /// TimeTickResolution tType;
324 /// GetTimeTickFrequency(out tick, out tType);
333 protected void GetTimeTickFrequency(out int ticks, out TimeTickResolution type)
335 Interop.Watch.ErrorCode err = Interop.Watch.GetTimeTickFrequency(out ticks, out type);
337 if (err != Interop.Watch.ErrorCode.None)
339 throw new InvalidOperationException("Failed to get time tick frequency. err : " + err);