From 5d44f1e875deda0c1baac309a11b762f683b0d65 Mon Sep 17 00:00:00 2001 From: WonYoung Choi Date: Tue, 5 Jul 2016 16:14:50 +0900 Subject: [PATCH] Apply selective core backend model Change-Id: Ia8e64210234582a4757e5043f103b9dadbb2f31b --- .../DefaultCoreBackend.cs | 188 +++++++++++++++++ .../Tizen.Applications.CoreBackend/EventType.cs | 121 +++++++++++ .../Tizen.Applications.CoreBackend/ICoreBackend.cs | 44 ++++ .../ServiceCoreBackend.cs | 80 +++++++ .../UICoreBackend.cs | 106 ++++++++++ Tizen.Applications/Tizen.Applications.csproj | 11 +- .../Tizen.Applications/Application.cs | 229 +-------------------- .../Tizen.Applications/ApplicationInfo.cs | 1 - .../Tizen.Applications/CoreApplication.cs | 181 ++++++++++++++++ .../Tizen.Applications/CoreUIApplication.cs | 88 ++++++++ .../Tizen.Applications/LowBatteryEventArgs.cs | 4 +- .../Tizen.Applications/ServiceApplication.cs | 53 +---- .../Tizen.Applications/UIApplication.cs | 2 +- .../Tizen.Applications/UIApplicationBase.cs | 128 ------------ 14 files changed, 829 insertions(+), 407 deletions(-) create mode 100644 Tizen.Applications/Tizen.Applications.CoreBackend/DefaultCoreBackend.cs create mode 100644 Tizen.Applications/Tizen.Applications.CoreBackend/EventType.cs create mode 100644 Tizen.Applications/Tizen.Applications.CoreBackend/ICoreBackend.cs create mode 100644 Tizen.Applications/Tizen.Applications.CoreBackend/ServiceCoreBackend.cs create mode 100644 Tizen.Applications/Tizen.Applications.CoreBackend/UICoreBackend.cs mode change 100755 => 100644 Tizen.Applications/Tizen.Applications/ApplicationInfo.cs create mode 100644 Tizen.Applications/Tizen.Applications/CoreApplication.cs create mode 100644 Tizen.Applications/Tizen.Applications/CoreUIApplication.cs delete mode 100644 Tizen.Applications/Tizen.Applications/UIApplicationBase.cs diff --git a/Tizen.Applications/Tizen.Applications.CoreBackend/DefaultCoreBackend.cs b/Tizen.Applications/Tizen.Applications.CoreBackend/DefaultCoreBackend.cs new file mode 100644 index 0000000..6c8eb50 --- /dev/null +++ b/Tizen.Applications/Tizen.Applications.CoreBackend/DefaultCoreBackend.cs @@ -0,0 +1,188 @@ +// Copyright 2016 by Samsung Electronics, Inc., +// +// This software is the confidential and proprietary information +// of Samsung Electronics, Inc. ("Confidential Information"). You +// shall not disclose such Confidential Information and shall use +// it only in accordance with the terms of the license agreement +// you entered into with Samsung. + +using System; +using System.Collections.Generic; + +using Tizen.Internals.Errors; + +namespace Tizen.Applications.CoreBackend +{ + internal abstract class DefaultCoreBackend : ICoreBackend + { + protected static readonly string LogTag = typeof(DefaultCoreBackend).Namespace; + + protected Dictionary _handlers = new Dictionary(); + + private bool _disposedValue = false; + + private Interop.AppCommon.AppEventCallback _lowMemoryCallback; + private Interop.AppCommon.AppEventCallback _lowBatteryCallback; + private Interop.AppCommon.AppEventCallback _localeChangedCallback; + private Interop.AppCommon.AppEventCallback _regionChangedCallback; + + private IntPtr _lowMemoryEventHandle = IntPtr.Zero; + private IntPtr _lowBatteryEventHandle = IntPtr.Zero; + private IntPtr _localeChangedEventHandle = IntPtr.Zero; + private IntPtr _regionChangedEventHandle = IntPtr.Zero; + + public DefaultCoreBackend() + { + _lowMemoryCallback = new Interop.AppCommon.AppEventCallback(OnLowMemoryNative); + _lowBatteryCallback = new Interop.AppCommon.AppEventCallback(OnLowBatteryNative); + _localeChangedCallback = new Interop.AppCommon.AppEventCallback(OnLocaleChangedNative); + _regionChangedCallback = new Interop.AppCommon.AppEventCallback(OnRegionChangedNative); + } + + ~DefaultCoreBackend() + { + Dispose(false); + } + + public void AddEventHandler(EventType evType, Action handler) + { + _handlers.Add(evType, handler); + } + + public void AddEventHandler(EventType evType, Action handler) where TEventArgs : EventArgs + { + _handlers.Add(evType, handler); + } + + public virtual void Run(string[] args) + { + TizenSynchronizationContext.Initialize(); + + ErrorCode err = ErrorCode.None; + err = AddAppEventCallback(out _lowMemoryEventHandle, Interop.AppCommon.AppEventType.LowMemory, _lowMemoryCallback); + if (err != ErrorCode.None) + { + Log.Error(LogTag, "Failed to add event handler for LowMemory event. Err = " + err); + } + err = AddAppEventCallback(out _lowBatteryEventHandle, Interop.AppCommon.AppEventType.LowBattery, _lowBatteryCallback); + if (err != ErrorCode.None) + { + Log.Error(LogTag, "Failed to add event handler for LowBattery event. Err = " + err); + } + + err = AddAppEventCallback(out _localeChangedEventHandle, Interop.AppCommon.AppEventType.LanguageChanged, _localeChangedCallback); + if (err != ErrorCode.None) + { + Log.Error(LogTag, "Failed to add event handler for LocaleChanged event. Err = " + err); + } + + err = AddAppEventCallback(out _regionChangedEventHandle, Interop.AppCommon.AppEventType.RegionFormatChanged, _regionChangedCallback); + if (err != ErrorCode.None) + { + Log.Error(LogTag, "Failed to add event handler for RegionFormatChanged event. Err = " + err); + } + } + + public abstract void Exit(); + + public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(this); + } + + protected virtual void Dispose(bool disposing) + { + if (!_disposedValue) + { + if (disposing) + { + // Release disposable objects + } + + if (_lowMemoryEventHandle != IntPtr.Zero) + { + RemoveAppEventCallback(_lowMemoryEventHandle); + } + if (_lowBatteryEventHandle != IntPtr.Zero) + { + RemoveAppEventCallback(_lowBatteryEventHandle); + } + if (_localeChangedEventHandle != IntPtr.Zero) + { + RemoveAppEventCallback(_localeChangedEventHandle); + } + if (_regionChangedEventHandle != IntPtr.Zero) + { + RemoveAppEventCallback(_regionChangedEventHandle); + } + + _disposedValue = true; + } + } + + internal abstract ErrorCode AddAppEventCallback(out IntPtr handle, Interop.AppCommon.AppEventType type, Interop.AppCommon.AppEventCallback callback); + + internal abstract void RemoveAppEventCallback(IntPtr handle); + + private void OnLowMemoryNative(IntPtr infoHandle, IntPtr data) + { + LowMemoryStatus status = LowMemoryStatus.None; + ErrorCode err = Interop.AppCommon.AppEventGetLowMemoryStatus(infoHandle, out status); + if (err != ErrorCode.None) + { + Log.Error(LogTag, "Failed to get memory status. Err = " + err); + } + if (_handlers.ContainsKey(EventType.LowMemory)) + { + var handler = _handlers[EventType.LowMemory] as Action; + handler?.Invoke(new LowMemoryEventArgs(status)); + } + } + + private void OnLowBatteryNative(IntPtr infoHandle, IntPtr data) + { + LowBatteryStatus status = LowBatteryStatus.None; + ErrorCode err = Interop.AppCommon.AppEventGetLowBatteryStatus(infoHandle, out status); + if (err != ErrorCode.None) + { + Log.Error(LogTag, "Failed to get battery status. Err = " + err); + } + if (_handlers.ContainsKey(EventType.LowBattery)) + { + var handler = _handlers[EventType.LowBattery] as Action; + handler?.Invoke(new LowBatteryEventArgs(status)); + } + } + + private void OnLocaleChangedNative(IntPtr infoHandle, IntPtr data) + { + string lang; + ErrorCode err = Interop.AppCommon.AppEventGetLanguage(infoHandle, out lang); + if (err != ErrorCode.None) + { + Log.Error(LogTag, "Failed to get changed language. Err = " + err); + } + if (_handlers.ContainsKey(EventType.LocaleChanged)) + { + var handler = _handlers[EventType.LocaleChanged] as Action; + handler?.Invoke(new LocaleChangedEventArgs(lang)); + } + } + + private void OnRegionChangedNative(IntPtr infoHandle, IntPtr data) + { + string region; + ErrorCode err = Interop.AppCommon.AppEventGetRegionFormat(infoHandle, out region); + if (err != ErrorCode.None) + { + Log.Error(LogTag, "Failed to get changed region format. Err = " + err); + } + if (_handlers.ContainsKey(EventType.RegionFormatChanged)) + { + var handler = _handlers[EventType.RegionFormatChanged] as Action; + handler?.Invoke(new RegionFormatChangedEventArgs(region)); + } + } + } +} diff --git a/Tizen.Applications/Tizen.Applications.CoreBackend/EventType.cs b/Tizen.Applications/Tizen.Applications.CoreBackend/EventType.cs new file mode 100644 index 0000000..20ba02a --- /dev/null +++ b/Tizen.Applications/Tizen.Applications.CoreBackend/EventType.cs @@ -0,0 +1,121 @@ +// Copyright 2016 by Samsung Electronics, Inc., +// +// This software is the confidential and proprietary information +// of Samsung Electronics, Inc. ("Confidential Information"). You +// shall not disclose such Confidential Information and shall use +// it only in accordance with the terms of the license agreement +// you entered into with Samsung. + +namespace Tizen.Applications.CoreBackend +{ + /// + /// Class that represents the type of event for backends. This class can be converted from string type. + /// + public class EventType + { + /// + /// Pre-defined event type. "PreCreated" + /// + public static readonly EventType PreCreated = "PreCreated"; + + /// + /// Pre-defined event type. "Created" + /// + public static readonly EventType Created = "Created"; + + /// + /// Pre-defined event type. "Terminated" + /// + public static readonly EventType Terminated = "Terminated"; + + /// + /// Pre-defined event type. "AppControlReceived" + /// + public static readonly EventType AppControlReceived = "AppControlReceived"; + + /// + /// Pre-defined event type. "Resumed" + /// + public static readonly EventType Resumed = "Resumed"; + + /// + /// Pre-defined event type. "Paused" + /// + public static readonly EventType Paused = "Paused"; + + /// + /// Pre-defined event type. "LowMemory" + /// + public static readonly EventType LowMemory = "LowMemory"; + + /// + /// Pre-defined event type. "LowBattery" + /// + public static readonly EventType LowBattery = "LowBattery"; + + /// + /// Pre-defined event type. "LocaleChanged" + /// + public static readonly EventType LocaleChanged = "LocaleChanged"; + + /// + /// Pre-defined event type. "RegionFormatChanged" + /// + public static readonly EventType RegionFormatChanged = "RegionFormatChanged"; + + private string _typeName; + + /// + /// Initializes the EventType class. + /// + /// The name of event type. + public EventType(string name) + { + _typeName = name; + } + + /// + /// Returns the name of event type. + /// + public override string ToString() + { + return _typeName; + } + + /// + /// Returns the hash code for event type string. + /// + public override int GetHashCode() + { + return _typeName.GetHashCode(); + } + + /// + /// Determines whether this instance and a specified object. + /// + public override bool Equals(object obj) + { + return _typeName.Equals(obj); + } + + /// + /// Determines whether this instance and a specified object. + /// + public bool Equals(EventType obj) + { + if (obj == null) + { + return false; + } + return _typeName.Equals(obj._typeName); + } + + /// + /// Converts a string to EventType instance. + /// + public static implicit operator EventType(string value) + { + return new EventType(value); + } + } +} diff --git a/Tizen.Applications/Tizen.Applications.CoreBackend/ICoreBackend.cs b/Tizen.Applications/Tizen.Applications.CoreBackend/ICoreBackend.cs new file mode 100644 index 0000000..49974b7 --- /dev/null +++ b/Tizen.Applications/Tizen.Applications.CoreBackend/ICoreBackend.cs @@ -0,0 +1,44 @@ +// Copyright 2016 by Samsung Electronics, Inc., +// +// This software is the confidential and proprietary information +// of Samsung Electronics, Inc. ("Confidential Information"). You +// shall not disclose such Confidential Information and shall use +// it only in accordance with the terms of the license agreement +// you entered into with Samsung. + +using System; + +namespace Tizen.Applications.CoreBackend +{ + /// + /// Interface that represents the backend lifecycles. + /// + public interface ICoreBackend : IDisposable + { + /// + /// Adds an event handler. + /// + /// The type of event. + /// The handler method without arguments. + void AddEventHandler(EventType evType, Action handler); + + /// + /// Adds an event handler. + /// + /// The EventArgs type used in arguments of the handler method. + /// The type of event. + /// The handler method with a TEventArgs type argument. + void AddEventHandler(EventType evType, Action handler) where TEventArgs : EventArgs; + + /// + /// Runs the mainloop of backend. + /// + /// + void Run(string[] args); + + /// + /// Exits the mainloop of backend. + /// + void Exit(); + } +} diff --git a/Tizen.Applications/Tizen.Applications.CoreBackend/ServiceCoreBackend.cs b/Tizen.Applications/Tizen.Applications.CoreBackend/ServiceCoreBackend.cs new file mode 100644 index 0000000..e514652 --- /dev/null +++ b/Tizen.Applications/Tizen.Applications.CoreBackend/ServiceCoreBackend.cs @@ -0,0 +1,80 @@ +// Copyright 2016 by Samsung Electronics, Inc., +// +// This software is the confidential and proprietary information +// of Samsung Electronics, Inc. ("Confidential Information"). You +// shall not disclose such Confidential Information and shall use +// it only in accordance with the terms of the license agreement +// you entered into with Samsung. + +using System; + +using Tizen.Internals.Errors; + +namespace Tizen.Applications.CoreBackend +{ + internal class ServiceCoreBackend : DefaultCoreBackend + { + private Interop.Service.ServiceAppLifecycleCallbacks _callbacks; + + public ServiceCoreBackend() + { + _callbacks.OnCreate = new Interop.Service.ServiceAppCreateCallback(OnCreateNative); + _callbacks.OnTerminate = new Interop.Service.ServiceAppTerminateCallback(OnTerminateNative); + _callbacks.OnAppControl = new Interop.Service.ServiceAppControlCallback(OnAppControlNative); + } + + public override void Exit() + { + Interop.Service.Exit(); + } + + public override void Run(string[] args) + { + base.Run(args); + + ErrorCode err = Interop.Service.Main(args.Length, args, ref _callbacks, IntPtr.Zero); + if (err != ErrorCode.None) + { + Log.Error(LogTag, "Failed to run the application. Err = " + err); + } + } + + private bool OnCreateNative(IntPtr data) + { + if (_handlers.ContainsKey(EventType.Created)) + { + var handler = _handlers[EventType.Created] as Action; + handler?.Invoke(); + } + return true; + } + + private void OnTerminateNative(IntPtr data) + { + if (_handlers.ContainsKey(EventType.Terminated)) + { + var handler = _handlers[EventType.Terminated] as Action; + handler?.Invoke(); + } + } + + private void OnAppControlNative(IntPtr appControlHandle, IntPtr data) + { + if (_handlers.ContainsKey(EventType.AppControlReceived)) + { + var handler = _handlers[EventType.AppControlReceived] as Action; + handler.Invoke(new AppControlReceivedEventArgs { ReceivedAppControl = new ReceivedAppControl(appControlHandle) }); + } + } + + internal override ErrorCode AddAppEventCallback(out IntPtr handle, Interop.AppCommon.AppEventType type, Interop.AppCommon.AppEventCallback callback) + { + return Interop.Service.AddEventHandler(out handle, type, callback, IntPtr.Zero); + } + + internal override void RemoveAppEventCallback(IntPtr handle) + { + Interop.Service.RemoveEventHandler(handle); + } + } +} diff --git a/Tizen.Applications/Tizen.Applications.CoreBackend/UICoreBackend.cs b/Tizen.Applications/Tizen.Applications.CoreBackend/UICoreBackend.cs new file mode 100644 index 0000000..f6cf001 --- /dev/null +++ b/Tizen.Applications/Tizen.Applications.CoreBackend/UICoreBackend.cs @@ -0,0 +1,106 @@ +// Copyright 2016 by Samsung Electronics, Inc., +// +// This software is the confidential and proprietary information +// of Samsung Electronics, Inc. ("Confidential Information"). You +// shall not disclose such Confidential Information and shall use +// it only in accordance with the terms of the license agreement +// you entered into with Samsung. + +using System; + +using Tizen.Internals.Errors; + +namespace Tizen.Applications.CoreBackend +{ + internal class UICoreBackend : DefaultCoreBackend + { + private Interop.Application.UIAppLifecycleCallbacks _callbacks; + + public UICoreBackend() + { + _callbacks.OnCreate = new Interop.Application.AppCreateCallback(OnCreateNative); + _callbacks.OnTerminate = new Interop.Application.AppTerminateCallback(OnTerminateNative); + _callbacks.OnAppControl = new Interop.Application.AppControlCallback(OnAppControlNative); + _callbacks.OnResume = new Interop.Application.AppResumeCallback(OnResumeNative); + _callbacks.OnPause = new Interop.Application.AppPauseCallback(OnPauseNative); + } + + public override void Exit() + { + Interop.Application.Exit(); + } + + public override void Run(string[] args) + { + base.Run(args); + + ErrorCode err = Interop.Application.Main(args.Length, args, ref _callbacks, IntPtr.Zero); + if (err != ErrorCode.None) + { + Log.Error(LogTag, "Failed to run the application. Err = " + err); + } + } + + private bool OnCreateNative(IntPtr data) + { + if (_handlers.ContainsKey(EventType.PreCreated)) + { + var handler = _handlers[EventType.PreCreated] as Action; + handler?.Invoke(); + } + + if (_handlers.ContainsKey(EventType.Created)) + { + var handler = _handlers[EventType.Created] as Action; + handler?.Invoke(); + } + return true; + } + + private void OnTerminateNative(IntPtr data) + { + if (_handlers.ContainsKey(EventType.Terminated)) + { + var handler = _handlers[EventType.Terminated] as Action; + handler?.Invoke(); + } + } + + private void OnAppControlNative(IntPtr appControlHandle, IntPtr data) + { + if (_handlers.ContainsKey(EventType.AppControlReceived)) + { + var handler = _handlers[EventType.AppControlReceived] as Action; + handler.Invoke(new AppControlReceivedEventArgs { ReceivedAppControl = new ReceivedAppControl(appControlHandle) }); + } + } + + private void OnResumeNative(IntPtr data) + { + if (_handlers.ContainsKey(EventType.Resumed)) + { + var handler = _handlers[EventType.Resumed] as Action; + handler?.Invoke(); + } + } + + private void OnPauseNative(IntPtr data) + { + if (_handlers.ContainsKey(EventType.Paused)) + { + var handler = _handlers[EventType.Paused] as Action; + handler?.Invoke(); + } + } + + internal override ErrorCode AddAppEventCallback(out IntPtr handle, Interop.AppCommon.AppEventType type, Interop.AppCommon.AppEventCallback callback) + { + return Interop.Application.AddEventHandler(out handle, type, callback, IntPtr.Zero); + } + + internal override void RemoveAppEventCallback(IntPtr handle) + { + Interop.Application.RemoveEventHandler(handle); + } + } +} diff --git a/Tizen.Applications/Tizen.Applications.csproj b/Tizen.Applications/Tizen.Applications.csproj index bacae5c..9a2ed66 100644 --- a/Tizen.Applications/Tizen.Applications.csproj +++ b/Tizen.Applications/Tizen.Applications.csproj @@ -65,6 +65,11 @@ + + + + + @@ -75,6 +80,8 @@ + + @@ -113,7 +120,6 @@ - @@ -146,6 +152,9 @@ Tizen.UI + + +