/*
* Copyright (c) 2017 Samsung Electronics Co., Ltd.
*
* 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 System.Collections.Generic;
using Tizen.Applications.CoreBackend;
using Tizen.Applications;
namespace Tizen.NUI
{
class NUIWidgetCoreBackend : ICoreBackend
{
///
/// Application instance to connect event.
///
protected WidgetApplication _application;
private string _stylesheet = "";
Dictionary _widgetInfo;
///
/// Dictionary to contain each type of event callback.
///
protected IDictionary Handlers = new Dictionary();
///
/// The default Constructor.
///
public NUIWidgetCoreBackend()
{
}
///
/// The constructor with stylesheet.
///
public NUIWidgetCoreBackend(string stylesheet)
{
_stylesheet = stylesheet;
}
///
/// Add NUIWidgetApplication event to Application.
/// Put each type of event callback in Dictionary.
///
/// Type of event
/// Event callback
public void AddEventHandler(EventType evType, Action handler)
{
Handlers.Add(evType, handler);
}
///
/// Add NUIWidgetApplication event to Application.
/// Put each type of event callback in Dictionary.
///
/// Argument type for the event
/// Type of event
/// Event callback
public void AddEventHandler(EventType evType, Action handler) where TEventArgs : EventArgs
{
Handlers.Add(evType, handler);
}
///
/// Dispose function.
///
public void Dispose()
{
if (_application != null)
{
_application.Dispose();
}
}
///
/// Exit Application.
///
public void Exit()
{
if (_application != null)
{
_application.Quit();
}
}
public void RegisterWidgetInfo(Dictionary widgetInfo)
{
_widgetInfo = widgetInfo;
}
///
/// Run Application.
///
/// Arguments from commandline.
public void Run(string[] args)
{
TizenSynchronizationContext.Initialize();
args[0] = Tizen.Applications.Application.Current.ApplicationInfo.ExecutablePath;
_application = WidgetApplication.NewWidgetApplication(args, _stylesheet);
_application.RegisterWidgetInfo(_widgetInfo);
_application.BatteryLow += OnBatteryLow;
_application.LanguageChanged += OnLanguageChanged;
_application.MemoryLow += OnMemoryLow;
_application.RegionChanged += OnRegionChanged; ;
_application.Initialized += OnInitialized;
_application.Terminating += OnTerminated;
_application.MainLoop();
}
///
/// The Initialized event callback function.
///
/// The application instance.
/// The event argument for Initialized.
private void OnInitialized(object source, NUIApplicationInitEventArgs e)
{
var preCreateHandler = Handlers[EventType.PreCreated] as Action;
preCreateHandler?.Invoke();
var createHandler = Handlers[EventType.Created] as Action;
createHandler?.Invoke();
_application.RegisterWidgetCreatingFunction();
}
///
/// The Terminated event callback function.
///
/// The application instance.
/// The event argument for Terminated.
private void OnTerminated(object source, NUIApplicationTerminatingEventArgs e)
{
var handler = Handlers[EventType.Terminated] as Action;
handler?.Invoke();
}
///
/// The Region changed event callback function.
///
/// The application instance.
/// The event argument for RegionChanged.
private void OnRegionChanged(object source, NUIApplicationRegionChangedEventArgs e)
{
var handler = Handlers[EventType.RegionFormatChanged] as Action;
handler?.Invoke(new RegionFormatChangedEventArgs(e.Application.GetRegion()));
}
///
/// The Language changed event callback function.
///
/// The application instance.
/// The event argument for LanguageChanged.
private void OnLanguageChanged(object source, NUIApplicationLanguageChangedEventArgs e)
{
var handler = Handlers[EventType.LocaleChanged] as Action;
handler?.Invoke(new LocaleChangedEventArgs(e.Application.GetLanguage()));
}
///
/// The Memory Low event callback function.
///
/// The application instance.
/// The event argument for MemoryLow.
private void OnMemoryLow(object source, NUIApplicationMemoryLowEventArgs e)
{
var handler = Handlers[EventType.LowMemory] as Action;
switch (e.MemoryStatus)
{
case Application.MemoryStatus.Normal:
{
handler?.Invoke(new LowMemoryEventArgs(LowMemoryStatus.None));
break;
}
case Application.MemoryStatus.Low:
{
handler?.Invoke(new LowMemoryEventArgs(LowMemoryStatus.SoftWarning));
break;
}
case Application.MemoryStatus.CriticallyLow:
{
handler?.Invoke(new LowMemoryEventArgs(LowMemoryStatus.HardWarning));
break;
}
}
}
///
/// The Battery Low event callback function.
///
/// The application instance.
/// The event argument for BatteryLow.
private void OnBatteryLow(object source, NUIApplicationBatteryLowEventArgs e)
{
var handler = Handlers[EventType.LowBattery] as Action;
switch (e.BatteryStatus)
{
case Application.BatteryStatus.Normal:
{
handler?.Invoke(new LowBatteryEventArgs(LowBatteryStatus.None));
break;
}
case Application.BatteryStatus.CriticallyLow:
{
handler?.Invoke(new LowBatteryEventArgs(LowBatteryStatus.CriticalLow));
break;
}
case Application.BatteryStatus.PowerOff:
{
handler?.Invoke(new LowBatteryEventArgs(LowBatteryStatus.PowerOff));
break;
}
}
}
internal WidgetApplication WidgetApplicationHandle
{
get
{
return _application;
}
}
}
}