/*
* 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;
namespace Tizen.Applications
{
///
/// Class that represents an application controlled lifecycles by the backend system.
///
public class CoreApplication : Application
{
private readonly ICoreBackend _backend;
private bool _disposedValue = false;
///
/// Initializes the CoreApplication class.
///
/// The backend instance implementing ICoreBacked interface.
public CoreApplication(ICoreBackend backend)
{
_backend = backend;
}
///
/// Occurs when the application is launched.
///
public event EventHandler Created;
///
/// Occurs when the application is about to shutdown.
///
public event EventHandler Terminated;
///
/// Occurs whenever the application receives the appcontrol message.
///
public event EventHandler AppControlReceived;
///
/// Occurs when the system memory is low.
///
public event EventHandler LowMemory;
///
/// Occurs when the system battery is low.
///
public event EventHandler LowBattery;
///
/// Occurs when the system language is chagned.
///
public event EventHandler LocaleChanged;
///
/// Occurs when the region format is changed.
///
public event EventHandler RegionFormatChanged;
///
/// Occurs when the device orientation is changed.
///
public event EventHandler DeviceOrientationChanged;
///
/// The backend instance.
///
protected ICoreBackend Backend { get { return _backend; } }
///
/// Runs the application's main loop.
///
/// Arguments from commandline.
public override void Run(string[] args)
{
base.Run(args);
_backend.AddEventHandler(EventType.Created, OnCreate);
_backend.AddEventHandler(EventType.Terminated, OnTerminate);
_backend.AddEventHandler(EventType.AppControlReceived, OnAppControlReceived);
_backend.AddEventHandler(EventType.LowMemory, OnLowMemory);
_backend.AddEventHandler(EventType.LowBattery, OnLowBattery);
_backend.AddEventHandler(EventType.LocaleChanged, OnLocaleChanged);
_backend.AddEventHandler(EventType.RegionFormatChanged, OnRegionFormatChanged);
_backend.AddEventHandler(EventType.DeviceOrientationChanged, OnDeviceOrientationChanged);
string[] argsClone = null;
if (args == null)
{
argsClone = new string[1];
}
else
{
argsClone = new string[args.Length + 1];
args.CopyTo(argsClone, 1);
}
argsClone[0] = string.Empty;
_backend.Run(argsClone);
}
///
/// Exits the main loop of the application.
///
public override void Exit()
{
_backend.Exit();
}
///
/// Overrides this method if want to handle behavior when the application is launched.
/// If base.OnCreated() is not called, the event 'Created' will not be emitted.
///
protected virtual void OnCreate()
{
Created?.Invoke(this, EventArgs.Empty);
}
///
/// Overrides this method if want to handle behavior when the application is terminated.
/// If base.OnTerminate() is not called, the event 'Terminated' will not be emitted.
///
protected virtual void OnTerminate()
{
Terminated?.Invoke(this, EventArgs.Empty);
}
///
/// Overrides this method if want to handle behavior when the application receives the appcontrol message.
/// If base.OnAppControlReceived() is not called, the event 'AppControlReceived' will not be emitted.
///
///
protected virtual void OnAppControlReceived(AppControlReceivedEventArgs e)
{
AppControlReceived?.Invoke(this, e);
}
///
/// Overrides this method if want to handle behavior when the system memory is low.
/// If base.OnLowMemory() is not called, the event 'LowMemory' will not be emitted.
///
protected virtual void OnLowMemory(LowMemoryEventArgs e)
{
LowMemory?.Invoke(this, e);
System.GC.Collect();
}
///
/// Overrides this method if want to handle behavior when the system battery is low.
/// If base.OnLowBattery() is not called, the event 'LowBattery' will not be emitted.
///
protected virtual void OnLowBattery(LowBatteryEventArgs e)
{
LowBattery?.Invoke(this, e);
}
///
/// Overrides this method if want to handle behavior when the system language is changed.
/// If base.OnLocaleChanged() is not called, the event 'LocaleChanged' will not be emitted.
///
protected virtual void OnLocaleChanged(LocaleChangedEventArgs e)
{
LocaleChanged?.Invoke(this, e);
}
///
/// Overrides this method if want to handle behavior when the region format is changed.
/// If base.OnRegionFormatChanged() is not called, the event 'RegionFormatChanged' will not be emitted.
///
protected virtual void OnRegionFormatChanged(RegionFormatChangedEventArgs e)
{
RegionFormatChanged?.Invoke(this, e);
}
///
/// Overrides this method if want to handle behavior when the device orientation is changed.
/// If base.OnRegionFormatChanged() is not called, the event 'RegionFormatChanged' will not be emitted.
///
protected virtual void OnDeviceOrientationChanged(DeviceOrientationEventArgs e)
{
DeviceOrientationChanged?.Invoke(this, e);
}
///
/// Releases any unmanaged resources used by this object. Can also dispose any other disposable objects.
///
/// If true, disposes any disposable objects. If false, does not dispose disposable objects.
protected override void Dispose(bool disposing)
{
if (!_disposedValue)
{
if (disposing)
{
_backend.Dispose();
}
_disposedValue = true;
}
base.Dispose(disposing);
}
}
}