// it only in accordance with the terms of the license agreement
// you entered into with Samsung.
+using System;
using System.Runtime.InteropServices;
using Tizen.Internals.Errors;
+using Tizen.Applications;
internal static partial class Interop
{
internal static partial class AppCommon
{
+ internal enum AppEventType
+ {
+ LowMemory = 0,
+ LowBattery,
+ LanguageChanged,
+ DeviceOrientationChanged,
+ RegionFormatChanged,
+ SuspendedStateChanged
+ }
+
+ internal delegate void AppEventCallback(IntPtr handle, IntPtr data);
+
[DllImport(Libraries.AppCommon, EntryPoint = "app_get_id")]
internal static extern ErrorCode AppGetId(out string appId);
[DllImport(Libraries.AppCommon, EntryPoint = "app_get_version")]
internal static extern ErrorCode AppGetVersion(out string version);
+
+ [DllImport(Libraries.AppCommon, EntryPoint = "app_event_get_low_memory_status")]
+ internal static extern ErrorCode AppEventGetLowMemoryStatus(IntPtr handle, out LowMemoryStatus status);
+
+ [DllImport(Libraries.AppCommon, EntryPoint = "app_event_get_low_battery_status")]
+ internal static extern ErrorCode AppEventGetLowBatteryStatus(IntPtr handle, out LowBatteryStatus status);
+
+ [DllImport(Libraries.AppCommon, EntryPoint = "app_event_get_language")]
+ internal static extern ErrorCode AppEventGetLanguage(IntPtr handle, out string lang);
+
+ [DllImport(Libraries.AppCommon, EntryPoint = "app_event_get_region_format")]
+ internal static extern ErrorCode AppEventGetRegionFormat(IntPtr handle, out string region);
+
}
}
+++ /dev/null
-// 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.Runtime.InteropServices;
-
-using Tizen.Internals.Errors;
-
-internal static partial class Interop
-{
- internal static partial class AppEvent
- {
- internal delegate void AppEventCallback(string eventName, IntPtr bundle, IntPtr data);
-
- [DllImport(Libraries.AppEvent, EntryPoint = "event_add_event_handler")]
- internal static extern ErrorCode AddEventHandler(string eventName, AppEventCallback callback, IntPtr data, out SafeAppEventHandle eventHandler);
-
- [DllImport(Libraries.AppEvent, EntryPoint = "event_remove_event_handler")]
- internal static extern ErrorCode DangerousRemoveEventHandler(IntPtr eventHandler);
-
- [DllImport(Libraries.AppEvent, EntryPoint = "event_publish_app_event")]
- internal static extern ErrorCode Publish(string eventName, IntPtr bundle);
-
- [DllImport(Libraries.AppEvent, EntryPoint = "event_publish_trusted_app_event")]
- internal static extern ErrorCode PublishTrusted(string eventName, IntPtr bundle);
-
- internal sealed class SafeAppEventHandle : SafeHandle
- {
- public SafeAppEventHandle() : base(IntPtr.Zero, true)
- {
- }
-
- public override bool IsInvalid
- {
- get { return handle == IntPtr.Zero; }
- }
-
- protected override bool ReleaseHandle()
- {
- DangerousRemoveEventHandler(handle);
- SetHandle(IntPtr.Zero);
- return true;
- }
- }
-
- internal static class EventNames
- {
- public const string LowMemory = "tizen.system.event.low_memory";
- public const string LanguageSet = "tizen.system.event.language_set";
- }
-
- internal static class EventKeys
- {
- public const string LowMemory = "low_memory";
- public const string LanguageSet = "language_set";
- }
-
- internal static class EventValues
- {
- public const string MemoryNormal = "normal";
- public const string MemorySoftWarning = "soft_warning";
- public const string MemoryHardWarning = "hard_warning";
- }
- }
-}
-
using System;
using System.Runtime.InteropServices;
+using Tizen.Internals.Errors;
+
internal static partial class Interop
{
internal static partial class Application
internal delegate void AppControlCallback(IntPtr appControl, IntPtr userData);
[DllImport(Libraries.Application, EntryPoint = "ui_app_main")]
- internal static extern int Main(int argc, string[] argv, ref UIAppLifecycleCallbacks callback, IntPtr userData);
+ internal static extern ErrorCode Main(int argc, string[] argv, ref UIAppLifecycleCallbacks callback, IntPtr userData);
[DllImport(Libraries.Application, EntryPoint = "ui_app_exit")]
internal static extern void Exit();
- [StructLayoutAttribute(LayoutKind.Sequential)]
+ [DllImport(Libraries.Application, EntryPoint = "ui_app_add_event_handler")]
+ internal static extern ErrorCode AddEventHandler(out IntPtr handle, AppCommon.AppEventType eventType, AppCommon.AppEventCallback callback, IntPtr data);
+
+ [DllImport(Libraries.Application, EntryPoint = "ui_app_remove_event_handler")]
+ internal static extern ErrorCode RemoveEventHandler(IntPtr handle);
+
+ [StructLayout(LayoutKind.Sequential)]
internal struct UIAppLifecycleCallbacks
{
public AppCreateCallback OnCreate;
using System;
using System.Runtime.InteropServices;
+using Tizen.Internals.Errors;
+
internal static partial class Interop
{
internal static partial class Service
internal delegate void ServiceAppControlCallback(IntPtr appControl, IntPtr userData);
[DllImport(Libraries.AppcoreAgent, EntryPoint = "service_app_main")]
- internal static extern int Main(int argc, string[] argv, ref ServiceAppLifecycleCallbacks callback, IntPtr userData);
+ internal static extern ErrorCode Main(int argc, string[] argv, ref ServiceAppLifecycleCallbacks callback, IntPtr userData);
[DllImport(Libraries.AppcoreAgent, EntryPoint = "service_app_exit")]
internal static extern void Exit();
+ [DllImport(Libraries.AppcoreAgent, EntryPoint = "service_app_add_event_handler")]
+ internal static extern ErrorCode AddEventHandler(out IntPtr handle, AppCommon.AppEventType eventType, AppCommon.AppEventCallback callback, IntPtr data);
+
+ [DllImport(Libraries.AppcoreAgent, EntryPoint = "service_app_remove_event_handler")]
+ internal static extern ErrorCode RemoveEventHandler(IntPtr handle);
+
[StructLayoutAttribute(LayoutKind.Sequential)]
internal struct ServiceAppLifecycleCallbacks
{
<Compile Include="GlobalSuppressions.cs" />
<Compile Include="Interop\Interop.AppCommon.cs" />
<Compile Include="Interop\Interop.AppControl.cs" />
- <Compile Include="Interop\Interop.AppEvent.cs" />
<Compile Include="Interop\Interop.Application.cs" />
<Compile Include="Interop\Interop.ApplicationManager.cs" />
<Compile Include="Interop\Interop.Bundle.cs" />
<Compile Include="Tizen.Applications\ApplicationTerminatedEventArgs.cs" />
<Compile Include="Tizen.Applications\DirectoryInfo.cs" />
<Compile Include="Tizen.Applications\LocaleChangedEventArgs.cs" />
+ <Compile Include="Tizen.Applications\LowBatteryEventArgs.cs" />
+ <Compile Include="Tizen.Applications\LowBatteryStatus.cs" />
<Compile Include="Tizen.Applications\LowMemoryEventArgs.cs" />
<Compile Include="Tizen.Applications\LowMemoryStatus.cs" />
<Compile Include="Tizen.Applications\ReceivedAppControl.cs" />
+ <Compile Include="Tizen.Applications\RegionFormatChangedEventArgs.cs" />
<Compile Include="Tizen.Applications\SafeAppControlHandle.cs" />
<Compile Include="Tizen.Applications\ServiceApplication.cs" />
<Compile Include="Tizen.Applications\UIApplication.cs" />
<Target Name="AfterBuild">
</Target>
-->
-</Project>
+</Project>
\ No newline at end of file
/// </summary>
public abstract class Application : IDisposable
{
- private const string LogTag = "Tizen.Applications";
+ internal const string LogTag = "Tizen.Applications";
private static Application s_CurrentApplication = null;
- private Interop.AppEvent.SafeAppEventHandle _lowMemoryNativeHandle;
- private Interop.AppEvent.SafeAppEventHandle _localeChangedNativeHandle;
+ private object _lock = new object();
- private Interop.AppEvent.AppEventCallback _appEventCallback;
+ private Interop.AppCommon.AppEventCallback _lowMemoryCallback;
+ private Interop.AppCommon.AppEventCallback _lowBatteryCallback;
+ private Interop.AppCommon.AppEventCallback _localeChangedCallback;
+ private Interop.AppCommon.AppEventCallback _regionChangedCallback;
- private object _lock = new object();
+ private IntPtr _lowMemoryEventHandle = IntPtr.Zero;
+ private IntPtr _lowBatteryEventHandle = IntPtr.Zero;
+ private IntPtr _localeChangedEventHandle = IntPtr.Zero;
+ private IntPtr _regionChangedEventHandle = IntPtr.Zero;
private DirectoryInfo _directoryInfo;
private ApplicationInfo _applicationInfo;
/// <summary>
- /// Initializes Application instance.
+ /// Initializes the Application class.
/// </summary>
public Application()
{
- _appEventCallback = new Interop.AppEvent.AppEventCallback(HandleAppEvent);
+ _lowMemoryCallback = new Interop.AppCommon.AppEventCallback(OnLowMemoryNative);
+ _lowBatteryCallback = new Interop.AppCommon.AppEventCallback(OnLowBatteryNative);
+ _localeChangedCallback = new Interop.AppCommon.AppEventCallback(OnLocaleChangedNative);
+ _regionChangedCallback = new Interop.AppCommon.AppEventCallback(OnRegionChangedNative);
}
/// <summary>
public event EventHandler<LowMemoryEventArgs> LowMemory;
/// <summary>
+ /// Occurs when the system battery is low.
+ /// </summary>
+ public event EventHandler<LowBatteryEventArgs> LowBattery;
+
+ /// <summary>
/// Occurs when the system language is chagned.
/// </summary>
public event EventHandler<LocaleChangedEventArgs> LocaleChanged;
/// <summary>
+ /// Occurs when the region format is changed.
+ /// </summary>
+ public event EventHandler<RegionFormatChangedEventArgs> RegionFormatChanged;
+
+ /// <summary>
/// Gets the instance of current application.
/// </summary>
public static Application Current { get { return s_CurrentApplication; } }
throw new ArgumentNullException("args");
}
+ TizenSynchronizationContext.Initialize();
+
s_CurrentApplication = this;
- Interop.AppEvent.AddEventHandler(Interop.AppEvent.EventNames.LowMemory, _appEventCallback, IntPtr.Zero, out _lowMemoryNativeHandle);
- Interop.AppEvent.AddEventHandler(Interop.AppEvent.EventNames.LanguageSet, _appEventCallback, IntPtr.Zero, out _localeChangedNativeHandle);
+ ErrorCode err = ErrorCode.None;
+ err = AddEventHandler(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 = AddEventHandler(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 = AddEventHandler(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 = AddEventHandler(out _regionChangedEventHandle, Interop.AppCommon.AppEventType.RegionFormatChanged, _regionChangedCallback);
+ if (err != ErrorCode.None)
+ {
+ Log.Error(LogTag, "Failed to add event handler for RegionFormatChanged event. Err = " + err);
+ }
}
/// <summary>
}
/// <summary>
+ /// Overrides this method if want to handle behavior when the system battery is low.
+ /// If base.OnLowBattery() is not called, the event 'LowBattery' will not be emitted.
+ /// </summary>
+ protected virtual void OnLowBattery(LowBatteryEventArgs e)
+ {
+ LowBattery?.Invoke(this, e);
+ }
+
+ /// <summary>
/// Overrides this method if want to handle behavior when the system language is changed.
/// If base.OnLocaleChanged() is not called, the event 'LocaleChanged' will not be emitted.
/// </summary>
LocaleChanged?.Invoke(this, e);
}
- private void HandleAppEvent(string eventName, IntPtr eventData, IntPtr data)
+ /// <summary>
+ /// Overrides this method if want to handle behavior when the region format is changed.
+ /// If base.OnRegionFormatChanged() is not called, the event 'RegionFormatChanged' will not be emitted.
+ /// </summary>
+ protected virtual void OnRegionFormatChanged(RegionFormatChangedEventArgs e)
+ {
+ RegionFormatChanged?.Invoke(this, e);
+ }
+
+ internal virtual ErrorCode AddEventHandler(out IntPtr handle, Interop.AppCommon.AppEventType type, Interop.AppCommon.AppEventCallback callback)
+ {
+ handle = IntPtr.Zero;
+ return ErrorCode.None;
+ }
+
+ internal virtual void RemoveEventHandler(IntPtr handle)
{
- Bundle b = new Bundle(eventData);
- if (eventName == Interop.AppEvent.EventNames.LowMemory)
+ }
+
+ private void OnLowMemoryNative(IntPtr infoHandle, IntPtr data)
+ {
+ LowMemoryStatus status = LowMemoryStatus.None;
+ ErrorCode err = Interop.AppCommon.AppEventGetLowMemoryStatus(infoHandle, out status);
+ if (err != ErrorCode.None)
{
- string value = b.GetItem<string>(Interop.AppEvent.EventKeys.LowMemory);
- LowMemoryStatus status = LowMemoryStatus.Normal;
- if (value == Interop.AppEvent.EventValues.MemorySoftWarning)
- {
- status = LowMemoryStatus.SoftWarning;
- }
- else if (value == Interop.AppEvent.EventValues.MemoryHardWarning)
- {
- status = LowMemoryStatus.HardWarning;
- }
- OnLowMemory(new LowMemoryEventArgs { LowMemoryStatus = status });
+ Log.Error(LogTag, "Failed to get memory status. Err = " + err);
}
- else if (eventName == Interop.AppEvent.EventNames.LanguageSet)
+ OnLowMemory(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);
+ }
+ OnLowBattery(new LowBatteryEventArgs(status));
+ }
+
+ private void OnLocaleChangedNative(IntPtr infoHandle, IntPtr data)
+ {
+ string lang;
+ ErrorCode err = Interop.AppCommon.AppEventGetLanguage(infoHandle, out lang);
+ if (err != ErrorCode.None)
{
- string value = b.GetItem<string>(Interop.AppEvent.EventKeys.LanguageSet);
- OnLocaleChanged(new LocaleChangedEventArgs { Locale = value });
+ Log.Error(LogTag, "Failed to get changed language. Err = " + err);
}
- b.Dispose();
+ OnLocaleChanged(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);
+ }
+ OnRegionFormatChanged(new RegionFormatChangedEventArgs(region));
}
#region IDisposable Support
{
_applicationInfo.Dispose();
}
- if (_lowMemoryNativeHandle != null && !_lowMemoryNativeHandle.IsInvalid)
- {
- _lowMemoryNativeHandle.Dispose();
- }
- if (_localeChangedNativeHandle != null && !_localeChangedNativeHandle.IsInvalid)
- {
- _localeChangedNativeHandle.Dispose();
- }
+ }
+
+ if (_lowMemoryEventHandle != IntPtr.Zero)
+ {
+ RemoveEventHandler(_lowMemoryEventHandle);
+ }
+ if (_lowBatteryEventHandle != IntPtr.Zero)
+ {
+ RemoveEventHandler(_lowBatteryEventHandle);
+ }
+ if (_localeChangedEventHandle != IntPtr.Zero)
+ {
+ RemoveEventHandler(_localeChangedEventHandle);
+ }
+ if (_regionChangedEventHandle != IntPtr.Zero)
+ {
+ RemoveEventHandler(_regionChangedEventHandle);
}
disposedValue = true;
/// </summary>
public class LocaleChangedEventArgs : EventArgs
{
+
+ /// <summary>
+ ///
+ /// </summary>
+ /// <param name="locale"></param>
+ public LocaleChangedEventArgs(string locale)
+ {
+ Locale = locale;
+ }
+
+ /// <summary>
+ ///
+ /// </summary>
+ public string Locale { get; private set; }
+
/// <summary>
///
/// </summary>
- public string Locale { get; internal set; }
+ public string Region { get; internal set; }
}
}
--- /dev/null
+// 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
+{
+ /// <summary>
+ ///
+ /// </summary>
+ public class LowBatteryEventArgs
+ {
+ /// <summary>
+ ///
+ /// </summary>
+ /// <param name="status"></param>
+ public LowBatteryEventArgs(LowBatteryStatus status)
+ {
+ LowBatteryStatus = status;
+ }
+
+ /// <summary>
+ ///
+ /// </summary>
+ public LowBatteryStatus LowBatteryStatus { get; private set; }
+ }
+}
--- /dev/null
+// 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
+{
+ /// <summary>
+ /// Enumeration for low battery status.
+ /// </summary>
+ public enum LowBatteryStatus
+ {
+ /// <summary>
+ ///
+ /// </summary>
+ None = 0,
+
+ /// <summary>
+ /// The battery status is under 1%
+ /// </summary>
+ PowerOff = 1,
+
+ /// <summary>
+ /// The battery status is under 5%
+ /// </summary>
+ CriticalLow
+ }
+}
/// <summary>
///
/// </summary>
- public LowMemoryStatus LowMemoryStatus { get; internal set; }
+ /// <param name="status"></param>
+ public LowMemoryEventArgs(LowMemoryStatus status)
+ {
+ LowMemoryStatus = status;
+ }
+
+ /// <summary>
+ ///
+ /// </summary>
+ public LowMemoryStatus LowMemoryStatus { get; private set; }
}
}
--- /dev/null
+// 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
+{
+ /// <summary>
+ ///
+ /// </summary>
+ public class RegionFormatChangedEventArgs : EventArgs
+ {
+
+ /// <summary>
+ ///
+ /// </summary>
+ /// <param name="region"></param>
+ public RegionFormatChangedEventArgs(string region)
+ {
+ Region = region;
+ }
+
+ /// <summary>
+ ///
+ /// </summary>
+ public string Region { get; private set; }
+ }
+}
using System;
+using Tizen.Internals.Errors;
+
namespace Tizen.Applications
{
/// <summary>
{
base.Run(args);
- TizenSynchronizationContext.Initialize();
- Interop.Service.Main(args.Length, args, ref _callbacks, IntPtr.Zero);
+ ErrorCode err = Interop.Service.Main(args.Length, args, ref _callbacks, IntPtr.Zero);
+ if (err != ErrorCode.None)
+ {
+ Log.Error(LogTag, "Failed to run the service. Err = " + err);
+ }
}
/// <summary>
Interop.Service.Exit();
}
+ internal override ErrorCode AddEventHandler(out IntPtr handle, Interop.AppCommon.AppEventType type, Interop.AppCommon.AppEventCallback callback)
+ {
+ return Interop.Service.AddEventHandler(out handle, type, callback, IntPtr.Zero);
+ }
+
+ internal override void RemoveEventHandler(IntPtr handle)
+ {
+ Interop.Service.RemoveEventHandler(handle);
+ }
+
private bool OnCreateNative(IntPtr data)
{
OnCreate();
// you entered into with Samsung.
using System;
+using Tizen.Internals.Errors;
using Tizen.UI;
namespace Tizen.Applications
{
base.Run(args);
- TizenSynchronizationContext.Initialize();
- Interop.Application.Main(args.Length, args, ref _callbacks, IntPtr.Zero);
+ 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);
+ }
}
/// <summary>
Paused?.Invoke(this, EventArgs.Empty);
}
+ internal override ErrorCode AddEventHandler(out IntPtr handle, Interop.AppCommon.AppEventType type, Interop.AppCommon.AppEventCallback callback)
+ {
+ return Interop.Application.AddEventHandler(out handle, type, callback, IntPtr.Zero);
+ }
+
+ internal override void RemoveEventHandler(IntPtr handle)
+ {
+ Interop.Application.RemoveEventHandler(handle);
+ }
+
private bool OnCreateNative(IntPtr data)
{
Window = new Window("C# UI Application");