From 8e8cc4b2dfe9c1b4c7ccf6c63e773e21a1439e53 Mon Sep 17 00:00:00 2001 From: Hwankyu Jhun Date: Mon, 13 Mar 2017 15:52:42 +0900 Subject: [PATCH] Add new APIs - Add ApplicationEnabled - Add ApplicationDisabled - The event handler is called when the application is enabled or disabled. Change-Id: I4da9a6d49660168fcca016a9504ad4be344bdac4 Signed-off-by: Hwankyu Jhun --- Tizen.Applications/Tizen.Applications.csproj | 3 + .../Interop/Interop.ApplicationManager.cs | 45 ++++++++++ .../Tizen.Applications.Common.csproj | 3 + .../ApplicationDisabledEventArgs.cs | 46 ++++++++++ .../ApplicationEnabledEventArgs.cs | 46 ++++++++++ .../Tizen.Applications/ApplicationEventState.cs | 40 +++++++++ .../Tizen.Applications/ApplicationInfo.cs | 0 .../Tizen.Applications/ApplicationManager.cs | 99 +++++++++++++++++++++- 8 files changed, 280 insertions(+), 2 deletions(-) mode change 100644 => 100755 src/Tizen.Applications.Common/Interop/Interop.ApplicationManager.cs mode change 100755 => 100644 src/Tizen.Applications.Common/Tizen.Applications.Common.csproj create mode 100644 src/Tizen.Applications.Common/Tizen.Applications/ApplicationDisabledEventArgs.cs create mode 100644 src/Tizen.Applications.Common/Tizen.Applications/ApplicationEnabledEventArgs.cs create mode 100644 src/Tizen.Applications.Common/Tizen.Applications/ApplicationEventState.cs mode change 100644 => 100755 src/Tizen.Applications.Common/Tizen.Applications/ApplicationInfo.cs diff --git a/Tizen.Applications/Tizen.Applications.csproj b/Tizen.Applications/Tizen.Applications.csproj index 99239ad..54d8a39 100644 --- a/Tizen.Applications/Tizen.Applications.csproj +++ b/Tizen.Applications/Tizen.Applications.csproj @@ -56,6 +56,9 @@ + + + diff --git a/src/Tizen.Applications.Common/Interop/Interop.ApplicationManager.cs b/src/Tizen.Applications.Common/Interop/Interop.ApplicationManager.cs old mode 100644 new mode 100755 index de4f638..4d45cdb --- a/src/Tizen.Applications.Common/Interop/Interop.ApplicationManager.cs +++ b/src/Tizen.Applications.Common/Interop/Interop.ApplicationManager.cs @@ -41,6 +41,31 @@ internal static partial class Interop Terminated = 1 } + internal enum AppManagerEventStatusType + { + All = 0x00, + Enable = 0x01, + Disable = 0x02 + } + + internal enum AppManagerEventType + { + Enable = 0, + Disable = 1 + } + + internal enum AppManagerEventState + { + Started = 0, + Completed = 1, + Failed = 2 + } + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void AppManagerEventCallback(string appType, string appId, AppManagerEventType eventType, AppManagerEventState eventState, IntPtr eventHandle, IntPtr userData); + //void(* app_manager_event_cb)(const char *type, const char *app_id, app_manager_event_type_e event_type, app_manager_event_state_e event_state, app_manager_event_h handle, void *user_data) + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] internal delegate void AppManagerAppContextEventCallback(IntPtr handle, AppContextEvent state, IntPtr userData); //void(* app_manager_app_context_event_cb)(app_context_h app_context, app_context_event_e event, void *user_data) @@ -121,6 +146,26 @@ internal static partial class Interop internal static extern ErrorCode AppManagerGetExternalSharedDataPath(string applicationId, out string path); //int app_manager_get_external_shared_data_path (const char *appid, char **path); + [DllImport(Libraries.AppManager, EntryPoint = "app_manager_event_create")] + internal static extern ErrorCode AppManagerEventCreate(out IntPtr handle); + //int app_manager_event_create (app_manager_event_h *handle); + + [DllImport(Libraries.AppManager, EntryPoint = "app_manager_event_set_status")] + internal static extern ErrorCode AppManagerEventSetStatus(IntPtr handle, AppManagerEventStatusType statusType); + //int app_manager_event_set_status (app_manager_event_h handle, int status_type); + + [DllImport(Libraries.AppManager, EntryPoint = "app_manager_set_event_cb")] + internal static extern ErrorCode AppManagerSetEventCallback(IntPtr handle, AppManagerEventCallback callback, IntPtr userData); + //int app_manager_set_event_cb (app_manager_event_h handle, app_manager_event_cb callback, void *user_data); + + [DllImport(Libraries.AppManager, EntryPoint = "app_manager_unset_event_cb")] + internal static extern ErrorCode AppManagerUnSetEventCallback(IntPtr handle); + //int app_manager_unset_event_cb (app_manager_event_h handle); + + [DllImport(Libraries.AppManager, EntryPoint = "app_manager_event_destroy")] + internal static extern ErrorCode AppManagerEventDestroy(IntPtr handle); + //int app_manager_event_destroy (app_manager_event_h handle); + [DllImport(Libraries.AppManager, EntryPoint = "app_context_destroy")] internal static extern ErrorCode AppContextDestroy(IntPtr handle); //int app_context_destroy(app_context_h app_context) diff --git a/src/Tizen.Applications.Common/Tizen.Applications.Common.csproj b/src/Tizen.Applications.Common/Tizen.Applications.Common.csproj old mode 100755 new mode 100644 index 2278de1..eca4af7 --- a/src/Tizen.Applications.Common/Tizen.Applications.Common.csproj +++ b/src/Tizen.Applications.Common/Tizen.Applications.Common.csproj @@ -56,6 +56,9 @@ + + + diff --git a/src/Tizen.Applications.Common/Tizen.Applications/ApplicationDisabledEventArgs.cs b/src/Tizen.Applications.Common/Tizen.Applications/ApplicationDisabledEventArgs.cs new file mode 100644 index 0000000..5b185d3 --- /dev/null +++ b/src/Tizen.Applications.Common/Tizen.Applications/ApplicationDisabledEventArgs.cs @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved + * + * 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; + +namespace Tizen.Applications +{ + /// + /// Arguments for the event that is raised when the application is disabled. + /// + public class ApplicationDisabledEventArgs : EventArgs + { + private readonly ApplicationEventState _eventState; + private readonly string _applicationId; + + internal ApplicationDisabledEventArgs(string appId, ApplicationEventState eventState) + { + _applicationId = appId; + _eventState = eventState; + } + + /// + /// The Id of the application + /// + public string ApplicationId { get { return _applicationId; } } + + /// + /// The Event state of the application + /// + public ApplicationEventState EventState { get { return _eventState; } } + } +} + diff --git a/src/Tizen.Applications.Common/Tizen.Applications/ApplicationEnabledEventArgs.cs b/src/Tizen.Applications.Common/Tizen.Applications/ApplicationEnabledEventArgs.cs new file mode 100644 index 0000000..fa72e8a --- /dev/null +++ b/src/Tizen.Applications.Common/Tizen.Applications/ApplicationEnabledEventArgs.cs @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved + * + * 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; + +namespace Tizen.Applications +{ + /// + /// Arguments for the event that is raised when the application is enabled. + /// + public class ApplicationEnabledEventArgs : EventArgs + { + private readonly ApplicationEventState _eventState; + private readonly string _applicationId; + + internal ApplicationEnabledEventArgs(string appId, ApplicationEventState eventState) + { + _applicationId = appId; + _eventState = eventState; + } + + /// + /// The Id of the application + /// + public string ApplicationId { get { return _applicationId; } } + + /// + /// The Event state of the application + /// + public ApplicationEventState EventState { get { return _eventState; } } + } +} + diff --git a/src/Tizen.Applications.Common/Tizen.Applications/ApplicationEventState.cs b/src/Tizen.Applications.Common/Tizen.Applications/ApplicationEventState.cs new file mode 100644 index 0000000..8ef9597 --- /dev/null +++ b/src/Tizen.Applications.Common/Tizen.Applications/ApplicationEventState.cs @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved + * + * 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; + +namespace Tizen.Applications +{ + /// + /// Enumeration of the application event state + /// + public enum ApplicationEventState + { + /// + /// Processing Started. + /// + Started = Interop.ApplicationManager.AppManagerEventState.Started, + /// + /// Processing Completed. + /// + Completed = Interop.ApplicationManager.AppManagerEventState.Completed, + /// + /// Processing Failed. + /// + Failed = Interop.ApplicationManager.AppManagerEventState.Failed + } +} + diff --git a/src/Tizen.Applications.Common/Tizen.Applications/ApplicationInfo.cs b/src/Tizen.Applications.Common/Tizen.Applications/ApplicationInfo.cs old mode 100644 new mode 100755 diff --git a/src/Tizen.Applications.Common/Tizen.Applications/ApplicationManager.cs b/src/Tizen.Applications.Common/Tizen.Applications/ApplicationManager.cs index e049a35..0c08a2f 100644 --- a/src/Tizen.Applications.Common/Tizen.Applications/ApplicationManager.cs +++ b/src/Tizen.Applications.Common/Tizen.Applications/ApplicationManager.cs @@ -26,11 +26,59 @@ namespace Tizen.Applications public static class ApplicationManager { private const string LogTag = "Tizen.Applications"; - private static EventHandler s_launchedHandler; private static EventHandler s_terminatedHandler; - private static Interop.ApplicationManager.AppManagerAppContextEventCallback s_applicationChangedEventCallback; + private static EventHandler _enabledHandler; + private static EventHandler _disabledHandler; + private static Interop.ApplicationManager.AppManagerEventCallback _eventCallback; + private static IntPtr _eventHandle = IntPtr.Zero; + + /// + /// Occurs whenever the installed application is enabled. + /// + public static event EventHandler ApplicationEnabled + { + add + { + if (_enabledHandler == null && _disabledHandler == null) + { + RegisterApplicationEvent(); + } + _enabledHandler += value; + } + remove + { + _enabledHandler -= value; + if (_enabledHandler == null && _disabledHandler == null) + { + UnRegisterApplicationEvent(); + } + } + } + + /// + /// Occurs whenever the installed application is disabled. + /// + public static event EventHandler ApplicationDisabled + { + add + { + if (_disabledHandler == null && _enabledHandler == null) + { + RegisterApplicationEvent(); + } + _disabledHandler += value; + } + remove + { + _disabledHandler -= value; + if (_disabledHandler == null && _enabledHandler == null) + { + UnRegisterApplicationEvent(); + } + } + } /// /// Occurs whenever the installed applications get launched. @@ -345,6 +393,53 @@ namespace Tizen.Applications { Interop.ApplicationManager.AppManagerUnSetAppContextEvent(); } + + private static void RegisterApplicationEvent() + { + Interop.ApplicationManager.ErrorCode err = Interop.ApplicationManager.ErrorCode.None; + err = Interop.ApplicationManager.AppManagerEventCreate(out _eventHandle); + if (err != Interop.ApplicationManager.ErrorCode.None) + { + throw ApplicationManagerErrorFactory.GetException(err, "Failed to create the application event handle"); + } + + err = Interop.ApplicationManager.AppManagerEventSetStatus(_eventHandle, Interop.ApplicationManager.AppManagerEventStatusType.All); + if (err != Interop.ApplicationManager.ErrorCode.None) + { + Interop.ApplicationManager.AppManagerEventDestroy(_eventHandle); + _eventHandle = IntPtr.Zero; + throw ApplicationManagerErrorFactory.GetException(err, "Failed to set the application event"); + } + + _eventCallback = (string appType, string appId, Interop.ApplicationManager.AppManagerEventType eventType, Interop.ApplicationManager.AppManagerEventState eventState, IntPtr eventHandle, IntPtr UserData) => + { + if (eventType == Interop.ApplicationManager.AppManagerEventType.Enable) + { + _enabledHandler?.Invoke(null, new ApplicationEnabledEventArgs(appId, (ApplicationEventState)eventState)); + } + else if (eventType == Interop.ApplicationManager.AppManagerEventType.Disable) + { + _disabledHandler?.Invoke(null, new ApplicationDisabledEventArgs(appId, (ApplicationEventState)eventState)); + } + }; + err = Interop.ApplicationManager.AppManagerSetEventCallback(_eventHandle, _eventCallback, IntPtr.Zero); + if (err != Interop.ApplicationManager.ErrorCode.None) + { + Interop.ApplicationManager.AppManagerEventDestroy(_eventHandle); + _eventHandle = IntPtr.Zero; + throw ApplicationManagerErrorFactory.GetException(err, "Failed to set the application event callback"); + } + } + + private static void UnRegisterApplicationEvent() + { + if (_eventHandle != IntPtr.Zero) + { + Interop.ApplicationManager.AppManagerUnSetEventCallback(_eventHandle); + Interop.ApplicationManager.AppManagerEventDestroy(_eventHandle); + _eventHandle = IntPtr.Zero; + } + } } internal static class FilterExtension -- 2.7.4