/* * Copyright (c) 2018 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 NUICoreBackend : ICoreBackend { /// /// The Application instance to connect event. /// protected Application _application; private string _stylesheet = ""; private NUIApplication.WindowMode _windowMode = NUIApplication.WindowMode.Opaque; /// /// The Dictionary to contain each type of event callback. /// protected IDictionary Handlers = new Dictionary(); /// /// The default Constructor. /// public NUICoreBackend() { } /// /// The constructor with stylesheet. /// public NUICoreBackend(string stylesheet) { _stylesheet = stylesheet; } /// /// The constructor with stylesheet and window mode. /// public NUICoreBackend(string stylesheet, NUIApplication.WindowMode windowMode) { _stylesheet = stylesheet; _windowMode = windowMode; } /// /// Adds NUIApplication event to Application. /// Puts each type of event callback in Dictionary. /// /// The type of event. /// The event callback. public void AddEventHandler(EventType evType, Action handler) { Handlers.Add(evType, handler); } /// /// Adds NUIApplication event to Application. /// Puts each type of event callback in Dictionary. /// /// The argument type for the event. /// The type of event. /// The event callback. public void AddEventHandler(EventType evType, Action handler) where TEventArgs : EventArgs { Handlers.Add(evType, handler); } /// /// The Dispose function. /// public void Dispose() { if(_application != null) { _application.Dispose(); } } /// /// The Exit application. /// public void Exit() { if(_application != null) { _application.Quit(); } } /// /// Ensures that the function passed in is called from the main loop when it is idle. /// /// The function to call /// true if added successfully, false otherwise public bool AddIdle(System.Delegate func) { return _application.AddIdle(func); } /// /// The Run application. /// /// The arguments from commandline. public void Run(string[] args) { TizenSynchronizationContext.Initialize(); NDalicPINVOKE.SWIGStringHelper.RegistCallback(); args[0] = Tizen.Applications.Application.Current.ApplicationInfo.ExecutablePath; if (args.Length == 1) { _application = Application.NewApplication(); } else if (args.Length > 1) { _application = Application.NewApplication(args, _stylesheet, (Application.WindowMode)_windowMode); } _application.BatteryLow += OnBatteryLow; _application.LanguageChanged += OnLanguageChanged; _application.MemoryLow += OnMemoryLow; _application.RegionChanged += OnRegionChanged; _application.Initialized += OnInitialized; _application.Resumed += OnResumed; _application.Terminating += OnTerminated; _application.Paused += OnPaused; _application.AppControl += OnAppControl; _application.MainLoop(); _application.Dispose(); } /// /// The Region changed event callback function. /// /// The application instance. /// The event argument for RegionChanged. private void OnRegionChanged(object source, NUIApplicationRegionChangedEventArgs e) { Log.Info("NUI", "NUICorebackend OnRegionChanged Called"); var handler = Handlers[EventType.RegionFormatChanged] as Action; handler?.Invoke( new RegionFormatChangedEventArgs((source as Application)?.GetRegion())); } /// /// The Memory Low event callback function. /// /// The application instance. /// The event argument for MemoryLow. private void OnMemoryLow(object source, NUIApplicationMemoryLowEventArgs e) { Log.Info("NUI", "NUICorebackend OnMemoryLow Called"); 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 Language changed event callback function. /// /// The application instance. /// The event argument for LanguageChanged. private void OnLanguageChanged(object source, NUIApplicationLanguageChangedEventArgs e) { Log.Info("NUI", "NUICorebackend OnLanguageChanged Called"); var handler = Handlers[EventType.LocaleChanged] as Action; handler?.Invoke( new LocaleChangedEventArgs((source as Application)?.GetLanguage())); } /// /// The Battery Low event callback function. /// /// The application instance. /// The event argument for BatteryLow. private void OnBatteryLow(object source, NUIApplicationBatteryLowEventArgs e) { Log.Info("NUI", "NUICorebackend OnBatteryLow Called"); 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 Initialized event callback function. /// /// The application instance. /// The event argument for Initialized. private void OnInitialized(object source, NUIApplicationInitEventArgs e) { Log.Info("NUI", "NUICorebackend OnPreCreated Called"); var preCreateHandler = Handlers[EventType.PreCreated] as Action; preCreateHandler?.Invoke(); Log.Info("NUI", "NUICorebackend OnCreate Called"); var createHandler = Handlers[EventType.Created] as Action; createHandler?.Invoke(); } /// /// The Terminated event callback function. /// /// The application instance. /// The event argument for Terminated. private void OnTerminated(object source, NUIApplicationTerminatingEventArgs e) { Log.Info("NUI", "NUICorebackend OnTerminated Called"); var handler = Handlers[EventType.Terminated] as Action; handler?.Invoke(); } /// /// The Resumed event callback function. /// /// The application instance. /// The event argument for Resumed. private void OnResumed(object source, NUIApplicationResumedEventArgs e) { Log.Info("NUI", "NUICorebackend OnResumed Called"); var handler = Handlers[EventType.Resumed] as Action; handler?.Invoke(); } /// /// The App control event callback function. /// /// The application instance. /// The event argument for AppControl. private void OnAppControl(object source, NUIApplicationAppControlEventArgs e) { Log.Info("NUI", "NUICorebackend OnAppControl Called"); var handler = Handlers[EventType.AppControlReceived] as Action; SafeAppControlHandle handle = new SafeAppControlHandle(e.VoidP,false); handler?.Invoke( new AppControlReceivedEventArgs(new ReceivedAppControl(handle)) ); } /// /// The Paused event callback function. /// /// The application instance. /// The event argument for Paused. private void OnPaused(object source, NUIApplicationPausedEventArgs e) { Log.Info("NUI", "NUICorebackend OnPaused Called"); var handler = Handlers[EventType.Paused] as Action; handler?.Invoke(); } internal Application ApplicationHandle { get { return _application; } } } }