/*
* 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 to contain each type of event callback.
///
protected IDictionary Handlers = new Dictionary();
///
/// The default Constructor.
///
public NUIWidgetCoreBackend()
{
//Tizen.Log.Fatal("NUI", "### NUIWidgetCoreBackend called");
//_application = WidgetApplication.NewWidgetApplication();
}
///
/// The constructor with stylesheet.
///
public NUIWidgetCoreBackend(string stylesheet)
{
_stylesheet = stylesheet;
//_application = WidgetApplication.NewWidgetApplication(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()
{
Tizen.Log.Fatal("NUI", "### NUIWidgetCoreBackend Dispose called");
if (_application != null)
{
_application.Dispose();
}
}
///
/// Exit Application.
///
public void Exit()
{
Tizen.Log.Fatal("NUI", "### NUIWidgetCoreBackend Exit called");
if (_application != null)
{
_application.Quit();
}
}
///
/// Run Application.
///
/// Arguments from commandline.
public void Run(string[] args)
{
args[0] = Tizen.Applications.Application.Current.ApplicationInfo.ExecutablePath;
_application = WidgetApplication.NewWidgetApplication(args, _stylesheet);
TizenSynchronizationContext.Initialize();
_application.BatteryLow += OnBatteryLow;
_application.LanguageChanged += OnLanguageChanged;
_application.MemoryLow += OnMemoryLow;
_application.RegionChanged += OnRegionChanged; ;
_application.Init += OnInit;
_application.Terminate += OnTerminate;
_application.MainLoop();
}
private void OnInit(object sender, WidgetApplication.WidgetApplicationEventArgs e)
{
Log.Fatal("NUI", "NUIWidgetApplication OnPreCreated Called");
var preCreateHandler = Handlers[EventType.PreCreated] as Action;
preCreateHandler?.Invoke();
Log.Fatal("NUI", "NUIWidgetApplication OnCreate Called");
var createHandler = Handlers[EventType.Created] as Action;
createHandler?.Invoke();
}
private void OnTerminate(object sender, WidgetApplication.WidgetApplicationEventArgs e)
{
Log.Fatal("NUI", "NUIWidgetApplication OnTerminated Called");
var handler = Handlers[EventType.Terminated] as Action;
handler?.Invoke();
}
///
/// Region changed event callback function.
///
/// Application instance
/// Event argument for RegionChanged
private void OnRegionChanged(object sender, WidgetApplication.WidgetApplicationEventArgs e)
{
Log.Fatal("NUI", "NUIWidgetApplication OnRegionChanged Called");
var handler = Handlers[EventType.RegionFormatChanged] as Action;
// Need to make new signal return in native to return right value.
handler?.Invoke(new RegionFormatChangedEventArgs(""));
}
///
/// Memory Low event callback function.
///
/// Application instance
/// Event argument for MemoryLow
private void OnMemoryLow(object sender, WidgetApplication.WidgetApplicationEventArgs e)
{
Log.Fatal("NUI", "NUIWidgetApplication OnMemoryLow Called");
var handler = Handlers[EventType.LowMemory] as Action;
// Need to make new signal return in native to return right value.
handler?.Invoke(new LowMemoryEventArgs(LowMemoryStatus.None));
}
///
/// Language changed event callback function.
///
/// Application instance
/// Event argument for LanguageChanged
private void OnLanguageChanged(object sender, WidgetApplication.WidgetApplicationEventArgs e)
{
Log.Fatal("NUI", "NUIWidgetApplication OnLanguageChanged Called");
var handler = Handlers[EventType.LocaleChanged] as Action;
// Need to make new signal return in native to return right value.
handler?.Invoke(new LocaleChangedEventArgs(""));
}
///
/// Battery low event callback function.
///
/// Application instance
/// Event argument for BatteryLow
private void OnBatteryLow(object sender, WidgetApplication.WidgetApplicationEventArgs e)
{
Log.Fatal("NUI", "NUIWidgetApplication OnBatteryLow Called");
var handler = Handlers[EventType.LowBattery] as Action;
// Need to make new signal return in native to return right value.
handler?.Invoke(new LowBatteryEventArgs(LowBatteryStatus.None));
}
internal WidgetApplication WidgetApplicationHandle
{
get
{
return _application;
}
}
}
}