/*
* Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the License);
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an AS IS BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using Tizen.Applications.CoreBackend;
using ElmSharp;
namespace Tizen.Applications
{
///
/// The class that represents the Tizen watch application.
///
/// 4
public class WatchApplication : CoreApplication
{
///
/// Initializes the WatchApplication class.
///
///
/// Default backend for the watch application will be used.
///
/// 4
public WatchApplication() : base(new WatchCoreBackend())
{
}
///
/// Initializes the WatchApplication class.
///
///
/// If you want to change the backend, use this constructor.
///
/// The backend instance implementing the ICoreBackend interface.
/// 4
public WatchApplication(ICoreBackend backend) : base(backend)
{
}
///
/// Instances for the window.
///
/// 4
protected Window Window;
///
/// Occurs whenever the application is resumed.
///
/// 4
public event EventHandler Resumed;
///
/// Occurs whenever the application is paused.
///
/// 4
public event EventHandler Paused;
///
/// Occurs whenever the time tick comes.
///
/// 4
public event EventHandler TimeTick;
///
/// Occurs whenever the time tick comes in the ambient mode.
///
/// 4
public event EventHandler AmbientTick;
///
/// Occurs when the ambient mode is changed.
///
/// 4
public event EventHandler AmbientChanged;
///
/// Runs the UI applications' main loop.
///
/// Arguments from the commandline.
/// 4
public override void Run(string[] args)
{
Backend.AddEventHandler(EventType.Resumed, OnResume);
Backend.AddEventHandler(EventType.Paused, OnPause);
Backend.AddEventHandler(WatchEventType.TimeTick, OnTick);
Backend.AddEventHandler(WatchEventType.AmbientTick, OnAmbientTick);
Backend.AddEventHandler(WatchEventType.AmbientChanged, OnAmbientChanged);
base.Run(args);
}
///
/// Overrides this method to handle the behavior when the application is launched.
/// If base.OnCreate() is not called, the event 'Created' will not be emitted.
///
/// 4
protected override void OnCreate()
{
base.OnCreate();
IntPtr win;
Interop.Watch.GetWin(out win);
Window = new WatchWindow(win);
Window.Show();
}
///
/// Overrides this method to handle the behavior when the application is resumed.
/// If base.OnResume() is not called, the event 'Resumed' will not be emitted.
///
/// 4
protected virtual void OnResume()
{
Resumed?.Invoke(this, EventArgs.Empty);
}
///
/// Overrides this method to handle the behavior when the application is paused.
/// If base.OnPause() is not called, the event 'Paused' will not be emitted.
///
/// 4
protected virtual void OnPause()
{
Paused?.Invoke(this, EventArgs.Empty);
}
///
/// Overrides this method to handle the behavior when the time tick event comes.
/// If base.OnTick() is not called, the event 'TimeTick' will not be emitted.
///
/// The received TimeEventArgs to get the time information.
/// 4
protected virtual void OnTick(TimeEventArgs time)
{
TimeTick?.Invoke(this, time);
}
///
/// Overrides this method to handle the behavior when the time tick event comes in ambient mode.
/// If base.OnAmbientTick() is not called, the event 'AmbientTick' will not be emitted.
///
/// The received TimeEventArgs to get time information.
/// http://tizen.org/privilege/alarm.set
/// 4
protected virtual void OnAmbientTick(TimeEventArgs time)
{
AmbientTick?.Invoke(this, time);
}
///
/// Overrides this method to handle the behavior when the ambient mode is changed.
/// If base.OnAmbientChanged() is not called, the event 'AmbientChanged' will not be emitted.
///
/// The received AmbientEventArgs.
/// 4
protected virtual void OnAmbientChanged(AmbientEventArgs mode)
{
AmbientChanged?.Invoke(this, mode);
}
///
/// Gets the current time.
///
/// WatchTime
/// http://tizen.org/feature/watch_app
/// Thrown when failed to get the current time because of an invalid parameter.
/// Thrown when failed to get the current time because the memory is not enough.
/// Thrown when the method is not supported.
///
///
/// class MyApp : WatchApplication
/// {
/// ...
/// public void TestMethod()
/// {
/// WatchTime wt;
/// try
/// {
/// wt = GetCurrentTime();
/// }
/// catch
/// {
/// }
/// }
/// }
///
///
/// 4
protected WatchTime GetCurrentTime()
{
SafeWatchTimeHandle handle;
Interop.Watch.ErrorCode err = Interop.Watch.WatchTimeGetCurrentTime(out handle);
if (err != Interop.Watch.ErrorCode.None)
{
if (err == Interop.Watch.ErrorCode.InvalidParameter)
throw new InvalidOperationException("Failed to get current time. err : " + err);
else if (err == Interop.Watch.ErrorCode.OutOfMemory)
throw new OutOfMemoryException("Failed to get current time. err : " + err);
else if (err == Interop.Watch.ErrorCode.NotSupported)
throw new NotSupportedException("Failed to get current time. err : " + err);
}
return new WatchTime(handle);
}
///
/// Gets the type of the periodic ambient tick.
///
/// AmbientTickType
/// http://tizen.org/feature/watch_app
/// Thrown when failed to get the ambient tick type.
/// Thrown when the method is not supported.
///
///
/// class MyApp : WatchApplication
/// {
/// ...
/// public void TestMethod()
/// {
/// AmbientTickType atType;
/// try
/// {
/// atType = GetAmbientTickType();
/// }
/// catch
/// {
/// }
/// }
/// }
///
///
/// 4
protected AmbientTickType GetAmbientTickType()
{
AmbientTickType ambientTickType;
Interop.Watch.ErrorCode err = Interop.Watch.GetAmbientTickType(out ambientTickType);
if(err != Interop.Watch.ErrorCode.None)
{
if (err == Interop.Watch.ErrorCode.NotSupported)
throw new NotSupportedException("Failed to get ambient tick type. err : " + err);
else
throw new InvalidOperationException("Failed to get ambient tick type. err : " + err);
}
return ambientTickType;
}
///
/// Sets the type of the periodic ambient tick.
/// OnAmbientTick will be called for the following settings.
/// If the SetAmbientTickType is not called, the OnAmbientTick will be called every minute.
///
/// The type of the ambient tick.
/// http://tizen.org/feature/watch_app
/// Thrown when failed to set the ambient tick type.
/// Thrown when the method is not supported.
///
///
/// class MyApp : WatchApplication
/// {
/// ...
/// public void TestMethod()
/// {
/// try
/// {
/// SetAmbientTickType(AmbientTickType.EveryMinute);
/// }
/// catch
/// {
/// }
/// }
/// }
///
///
/// 4
protected void SetAmbientTickType(AmbientTickType ambientTickType)
{
Interop.Watch.ErrorCode err = Interop.Watch.SetAmbientTickType(ambientTickType);
if(err != Interop.Watch.ErrorCode.None)
{
if (err == Interop.Watch.ErrorCode.NotSupported)
throw new NotSupportedException("Failed to set ambient tick type. err : " + err);
else
throw new InvalidOperationException("Failed to set ambient tick type. err : " + err);
}
}
///
/// Sets the frequency of the time tick.
/// OnTick will be called for the following settings.
/// If SetTimeTickFrequency is not called, OnTick will be called every second.
///
/// Ticks the number of ticks per given resolution type.
/// Type of the resolution type.
/// http://tizen.org/feature/watch_app
/// Thrown when failed to set the time tick frequency.
/// Thrown when the method is not supported.
///
///
/// class MyApp : WatchApplication
/// {
/// ...
/// public void TestMethod()
/// {
/// try
/// {
/// SetTimeTickFrequency(1, TimeTickResolution.TimeTicksPerMinute);
/// }
/// catch
/// {
/// }
/// }
/// }
///
///
/// 4
protected void SetTimeTickFrequency(int ticks, TimeTickResolution type)
{
Interop.Watch.ErrorCode err = Interop.Watch.SetTimeTickFrequency(ticks, type);
if (err != Interop.Watch.ErrorCode.None)
{
if (err == Interop.Watch.ErrorCode.NotSupported)
throw new NotSupportedException("Failed to set time tick frequency. err : " + err);
else
throw new InvalidOperationException("Failed to set time tick frequency. err : " + err);
}
}
///
/// Gets the frequency of the time tick.
///
/// Ticks the number of ticks per given resolution type.
/// Type of the resolution type.
/// http://tizen.org/feature/watch_app
/// Thrown when failed to get the time tick frequency.
/// Thrown when the method is not supported.
///
///
/// class MyApp : WatchApplication
/// {
/// ...
/// public void TestMethod()
/// {
/// int tick;
/// TimeTickResolution tType;
/// try
/// {
/// GetTimeTickFrequency(out tick, out tType);
/// }
/// catch
/// {
/// }
/// }
/// }
///
///
/// 4
protected void GetTimeTickFrequency(out int ticks, out TimeTickResolution type)
{
Interop.Watch.ErrorCode err = Interop.Watch.GetTimeTickFrequency(out ticks, out type);
if (err != Interop.Watch.ErrorCode.None)
{
if (err == Interop.Watch.ErrorCode.NotSupported)
throw new NotSupportedException("Failed to get time tick frequency. err : " + err);
else
throw new InvalidOperationException("Failed to get time tick frequency. err : " + err);
}
}
}
}