/*
* Copyright (c) 2021 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 = "";
private 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)
{
this.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()
{
application?.Dispose();
}
///
/// Exit Application.
///
public void Exit()
{
application?.Quit();
}
public void RegisterWidgetInfo(Dictionary widgetInfo)
{
this.widgetInfo = widgetInfo;
}
public void AddWidgetInfo(Dictionary widgetInfo)
{
application?.AddWidgetInfo(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.DeviceOrientationChanged += OnDeviceOrientationChanged;
application.Initialized += OnInitialized;
application.Terminating += OnTerminated;
application.MainLoop();
application.Dispose();
}
///
/// 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;
}
}
}
///
/// The Device Orientation changed event callback function.
///
/// The application instance.
/// The event argument for DeviceOrientationChanged.
private void OnDeviceOrientationChanged(object source, NUIApplicationDeviceOrientationChangedEventArgs e)
{
var handler = handlers[EventType.DeviceOrientationChanged] as Action;
switch (e.DeviceOrientationStatus)
{
case Application.DeviceOrientationStatus.Orientation_0:
{
handler?.Invoke(new DeviceOrientationEventArgs(DeviceOrientation.Orientation_0));
break;
}
case Application.DeviceOrientationStatus.Orientation_90:
{
handler?.Invoke(new DeviceOrientationEventArgs(DeviceOrientation.Orientation_90));
break;
}
case Application.DeviceOrientationStatus.Orientation_180:
{
handler?.Invoke(new DeviceOrientationEventArgs(DeviceOrientation.Orientation_180));
break;
}
case Application.DeviceOrientationStatus.Orientation_270:
{
handler?.Invoke(new DeviceOrientationEventArgs(DeviceOrientation.Orientation_270));
break;
}
}
}
internal WidgetApplication WidgetApplicationHandle
{
get
{
return application;
}
}
}
}