From ed8c3d67356e49cacec3243d03ff55e123f75887 Mon Sep 17 00:00:00 2001 From: hjhun <36876573+hjhun@users.noreply.github.com> Date: Wed, 21 Aug 2019 09:28:37 +0900 Subject: [PATCH] =?utf8?q?[ComponentManager]=20Add=20new=20APIs=20to=20han?= =?utf8?q?dle=20components=20of=20the=20component=E2=80=A6=20(#973)?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit * [ComponentManager] Add new APIs to handle components of the component-based application Signed-off-by: Hwankyu Jhun * Change property name Type is changed to ComponentType. Signed-off-by: Hwankyu Jhun * Fix typo Signed-off-by: Hwankyu Jhun * Update ComponentState Signed-off-by: Hwankyu Jhun --- .../Interop/Interop.ComponentManager.cs | 164 +++++++++++ .../Interop/Interop.Libraries.cs | 23 ++ .../Tizen.Applications.ComponentManager.csproj | 24 ++ .../Tizen.Applications.ComponentManager.sln | 25 ++ .../Tizen.Applications/ComponentInfo.cs | 260 +++++++++++++++++ .../Tizen.Applications/ComponentManager.cs | 186 ++++++++++++ .../Tizen.Applications/ComponentRunningContext.cs | 314 +++++++++++++++++++++ .../Tizen.Applications/ComponentType.cs | 35 +++ 8 files changed, 1031 insertions(+) create mode 100755 src/Tizen.Applications.ComponentManager/Interop/Interop.ComponentManager.cs create mode 100755 src/Tizen.Applications.ComponentManager/Interop/Interop.Libraries.cs create mode 100755 src/Tizen.Applications.ComponentManager/Tizen.Applications.ComponentManager.csproj create mode 100755 src/Tizen.Applications.ComponentManager/Tizen.Applications.ComponentManager.sln create mode 100755 src/Tizen.Applications.ComponentManager/Tizen.Applications/ComponentInfo.cs create mode 100755 src/Tizen.Applications.ComponentManager/Tizen.Applications/ComponentManager.cs create mode 100755 src/Tizen.Applications.ComponentManager/Tizen.Applications/ComponentRunningContext.cs create mode 100755 src/Tizen.Applications.ComponentManager/Tizen.Applications/ComponentType.cs diff --git a/src/Tizen.Applications.ComponentManager/Interop/Interop.ComponentManager.cs b/src/Tizen.Applications.ComponentManager/Interop/Interop.ComponentManager.cs new file mode 100755 index 0000000..0ca3c22 --- /dev/null +++ b/src/Tizen.Applications.ComponentManager/Interop/Interop.ComponentManager.cs @@ -0,0 +1,164 @@ +/* + * Copyright (c) 2019 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; +using System.Runtime.InteropServices; + +internal static partial class Interop +{ + internal static partial class ComponentManager + { + internal enum ErrorCode + { + None = Tizen.Internals.Errors.ErrorCode.None, + InvalidParameter = Tizen.Internals.Errors.ErrorCode.InvalidParameter, + OutOfMemory = Tizen.Internals.Errors.ErrorCode.OutOfMemory, + IoError = Tizen.Internals.Errors.ErrorCode.IoError, + NoSuchComponent = -0x03040000 | 0x01, + DbFailed = -0x03040000 | 0x03, + InvalidApplication = -0x03040000 | 0x04, + NotRunning = -0x03040000 | 0x05, + LabelNotFound = -0x03040000 | 0x06, + PermissionDenied = Tizen.Internals.Errors.ErrorCode.PermissionDenied + } + + internal enum ComponentInfoComponentType + { + Frame = 0, + Service = 1 + } + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate bool ComponentManagerComponentContextCallback(IntPtr handle, IntPtr userData); + // bool (*component_manager_component_context_cb)(component_context_h handle, void *user_data); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate bool ComponentManagerComponentInfoCallback(IntPtr handle, IntPtr userData); + // bool (*component_manager_component_info_cb)(component_info_h handle, void *user_data); + + [DllImport(Libraries.ComponentManager, EntryPoint = "component_manager_foreach_component_context")] + internal static extern ErrorCode ComponentManagerForeachComponentContext(ComponentManagerComponentContextCallback callback, IntPtr userData); + // int component_manager_foreach_component_context(component_manager_component_context_cb callback, void *user_data) + + [DllImport(Libraries.ComponentManager, EntryPoint = "component_manager_foreach_component_info")] + internal static extern ErrorCode ComponentManagerForeachComponentInfo(ComponentManagerComponentInfoCallback callback, IntPtr userData); + // int component_manager_foreach_component_info(component_manager_component_info_cb callback, void *user_data) + + [DllImport(Libraries.ComponentManager, EntryPoint = "component_manager_get_component_context")] + internal static extern ErrorCode ComponentManagerGetComponentContext(string componentId, out IntPtr handle); + // int component_manager_get_component_context(const char *comp_id, component_context_h *handle); + + [DllImport(Libraries.ComponentManager, EntryPoint = "component_manager_get_component_info")] + internal static extern ErrorCode ComponentManagerGetComponentInfo(string componentId, out IntPtr handle); + // int component_manager_get_component_info(const char *comp_id, component_info_h *handle); + + [DllImport(Libraries.ComponentManager, EntryPoint = "component_manager_is_running")] + internal static extern ErrorCode ComponentManagerIsRunning(string componentId, out bool running); + // int component_manager_is_running(const char *appid, bool *running); + + [DllImport(Libraries.ComponentManager, EntryPoint = "component_manager_resume_component")] + internal static extern ErrorCode ComponentManagerResumeComponent(IntPtr handle); + // int component_manager_resume_component(component_context_h handle); + + [DllImport(Libraries.ComponentManager, EntryPoint = "component_manager_terminate_bg_component")] + internal static extern ErrorCode ComponentManagerTerminateBgComponent(IntPtr handle); + // int component_manager_terminate_bg_component (component_context_h handle); + + [DllImport(Libraries.ComponentManager, EntryPoint = "component_manager_pause_component")] + internal static extern ErrorCode ComponentManagerPauseComponent(IntPtr handle); + // int component_manager_pause_component(component_context_h handle); + + [DllImport(Libraries.ComponentManager, EntryPoint = "component_manager_terminate_component")] + internal static extern ErrorCode ComponentManagerTerminateComponent(IntPtr handle); + // int component_manager_terminate_component(component_context_h handle); + + [DllImport(Libraries.ComponentManager, EntryPoint = "component_context_destroy")] + internal static extern ErrorCode ComponentContextDestroy(IntPtr handle); + // int component_context_destroy(component_context_h handle) + + [DllImport(Libraries.ComponentManager, EntryPoint = "component_context_get_app_id")] + internal static extern ErrorCode ComponentContextGetAppId(IntPtr handle, out string applicationId); + // int component_context_get_app_id(component_context_h handle, char **app_id) + + [DllImport(Libraries.ComponentManager, EntryPoint = "component_context_get_component_id")] + internal static extern ErrorCode ComponentContextGetComponentId(IntPtr handle, out string componentId); + // int component_context_get_component_id(component_context_h handle, char **component_id) + + [DllImport(Libraries.ComponentManager, EntryPoint = "component_context_get_instance_id")] + internal static extern ErrorCode ComponentContextGetInstanceId(IntPtr handle, out string instanceId); + // int component_context_get_instance_id(component_context_h handle, char **instance_id) + + [DllImport(Libraries.ComponentManager, EntryPoint = "component_context_get_component_state")] + internal static extern ErrorCode ComponentContextGetComponentState(IntPtr handle, out int state); + // int component_context_get_component_state(component_context_h handle, component_state_e *state) + + [DllImport(Libraries.ComponentManager, EntryPoint = "component_context_is_terminated")] + internal static extern ErrorCode ComponentContextIsTerminated(IntPtr handle, out bool terminated); + // int component_context_is_terminated(component_context_h handle, bool *terminated); + + [DllImport(Libraries.ComponentManager, EntryPoint = "component_context_is_subcomponent")] + internal static extern ErrorCode ComponentContextIsSubComponent(IntPtr handle, out bool is_subcomponent); + // int component_context_is_subcomponent(component_context_h handle, bool *is_subcomponent); + + [DllImport(Libraries.ComponentManager, EntryPoint = "component_context_clone")] + internal static extern ErrorCode ComponentContextClone(out IntPtr destination, IntPtr source); + // int component_context_clone(component_context_h *clone, component_context_h handle); + + [DllImport(Libraries.ComponentManager, EntryPoint = "component_info_create")] + internal static extern ErrorCode ComponentInfoCreate(string componentId, out IntPtr handle); + // int component_info_create(const char *component_id, component_info_h *handle); + + [DllImport(Libraries.ComponentManager, EntryPoint = "component_info_destroy")] + internal static extern ErrorCode ComponentInfoDestroy(IntPtr handle); + // int component_info_destroy(component_info_h handle); + + [DllImport(Libraries.ComponentManager, EntryPoint = "component_info_get_app_id")] + internal static extern ErrorCode ComponentInfoGetAppId(IntPtr handle, out string applicationId); + // int component_info_get_app_id(component_info_h handle, char **app_id); + + [DllImport(Libraries.ComponentManager, EntryPoint = "component_info_get_component_id")] + internal static extern ErrorCode ComponentInfoGetComponentId(IntPtr handle, out string componentId); + // int component_info_get_component_id(component_info_h handle, char **component_id); + + [DllImport(Libraries.ComponentManager, EntryPoint = "component_info_get_component_type")] + internal static extern ErrorCode ComponentInfoGetComponentType(IntPtr handle, out ComponentInfoComponentType type); + // int component_info_get_component_type(component_info_h handle, component_info_component_type_e *type); + + [DllImport(Libraries.ComponentManager, EntryPoint = "component_info_is_icon_display")] + internal static extern ErrorCode ComponentInfoIsIconDisplay(IntPtr handle, out bool iconDisplay); + // int component_info_is_icon_display(component_info_h handle, bool *icon_display); + + [DllImport(Libraries.ComponentManager, EntryPoint = "component_info_is_managed_by_task_manager")] + internal static extern ErrorCode ComponentInfoIsManagedByTaskManager(IntPtr handle, out bool managed); + // int component_info_is_managed_by_task_manager(component_info_h handle, bool *managed); + + [DllImport(Libraries.ComponentManager, EntryPoint = "component_info_get_icon")] + internal static extern ErrorCode ComponentInfoGetIcon(IntPtr handle, out string icon); + // int component_info_get_icon(component_info_h handle, char **icon); + + [DllImport(Libraries.ComponentManager, EntryPoint = "component_info_get_label")] + internal static extern ErrorCode ComponentInfoGetLabel(IntPtr handle, out string label); + // int component_info_get_label(component_info_h handle, char **label); + + [DllImport(Libraries.ComponentManager, EntryPoint = "component_info_get_localized_label")] + internal static extern ErrorCode ComponentInfoGetLocalizedLabel(IntPtr handle, string locale, out string label); + // int component_info_get_localized_label(component_info_h handle, const char *locale, char **label); + + [DllImport(Libraries.ComponentManager, EntryPoint = "component_info_clone")] + internal static extern ErrorCode ComponentInfoClone(out IntPtr destination, IntPtr source); + // int component_info_clone(component_info_h *clone, component_info_h handle); + } +} diff --git a/src/Tizen.Applications.ComponentManager/Interop/Interop.Libraries.cs b/src/Tizen.Applications.ComponentManager/Interop/Interop.Libraries.cs new file mode 100755 index 0000000..a793dd3 --- /dev/null +++ b/src/Tizen.Applications.ComponentManager/Interop/Interop.Libraries.cs @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2019 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. + */ + +internal static partial class Interop +{ + internal static partial class Libraries + { + public const string ComponentManager = "libcapi-appfw-component-manager.so.1"; + } +} \ No newline at end of file diff --git a/src/Tizen.Applications.ComponentManager/Tizen.Applications.ComponentManager.csproj b/src/Tizen.Applications.ComponentManager/Tizen.Applications.ComponentManager.csproj new file mode 100755 index 0000000..aeb7371 --- /dev/null +++ b/src/Tizen.Applications.ComponentManager/Tizen.Applications.ComponentManager.csproj @@ -0,0 +1,24 @@ + + + + + netstandard2.0 + false + + + + portable + + + None + + + + + + Runtime + + + + + \ No newline at end of file diff --git a/src/Tizen.Applications.ComponentManager/Tizen.Applications.ComponentManager.sln b/src/Tizen.Applications.ComponentManager/Tizen.Applications.ComponentManager.sln new file mode 100755 index 0000000..ca87df5 --- /dev/null +++ b/src/Tizen.Applications.ComponentManager/Tizen.Applications.ComponentManager.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.28307.271 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tizen.Applications.ComponentManager", "Tizen.Applications.ComponentManager.csproj", "{0C733CDC-EE90-42CE-9083-87FC55EF91DA}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {0C733CDC-EE90-42CE-9083-87FC55EF91DA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {0C733CDC-EE90-42CE-9083-87FC55EF91DA}.Debug|Any CPU.Build.0 = Debug|Any CPU + {0C733CDC-EE90-42CE-9083-87FC55EF91DA}.Release|Any CPU.ActiveCfg = Release|Any CPU + {0C733CDC-EE90-42CE-9083-87FC55EF91DA}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {7166CBA3-05BB-413B-9DA4-5F0A408AFA36} + EndGlobalSection +EndGlobal diff --git a/src/Tizen.Applications.ComponentManager/Tizen.Applications/ComponentInfo.cs b/src/Tizen.Applications.ComponentManager/Tizen.Applications/ComponentInfo.cs new file mode 100755 index 0000000..31add27 --- /dev/null +++ b/src/Tizen.Applications.ComponentManager/Tizen.Applications/ComponentInfo.cs @@ -0,0 +1,260 @@ +/* + * Copyright (c) 2019 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; +using System.Collections.Generic; + +namespace Tizen.Applications +{ + /// + /// This class provides methods and properties to get information of the component. + /// + /// 6 + public class ComponentInfo : IDisposable + { + private const string LogTag = "Tizen.Applications"; + private bool _disposed = false; + private IntPtr _infoHandle = IntPtr.Zero; + private string _componentId = string.Empty; + + internal ComponentInfo(IntPtr infoHandle) + { + Interop.ComponentManager.ErrorCode err = Interop.ComponentManager.ComponentInfoGetComponentId(infoHandle, out _componentId); + if (err != Interop.ComponentManager.ErrorCode.None) + { + throw ComponentManager.ComponentManagerErrorFactory.GetException(err, "Invalid native handle."); + } + _infoHandle = infoHandle; + } + + /// + /// A constructor of ComponentInfo that takes the component ID. + /// + /// Component ID. + /// Thrown when failed because of an invalid argument. + /// Thrown when failed because of the system error. + /// Thrown when failed because of out of memory. + /// Thrown when failed because of permission denied.> + /// http://tizen.org/privilege/packagemanager.info + /// 6 + public ComponentInfo(string componentId) + { + _componentId = componentId; + IntPtr infoHandle = IntPtr.Zero; + Interop.ComponentManager.ErrorCode err = Interop.ComponentManager.ComponentInfoCreate(_componentId, out infoHandle); + if (err != Interop.ComponentManager.ErrorCode.None) + { + throw ComponentManager.ComponentManagerErrorFactory.GetException(err, "Failed to create the ComponentInfo."); + } + _infoHandle = infoHandle; + } + + /// + /// Destructor of the class. + /// + ~ComponentInfo() + { + Dispose(false); + } + + /// + /// Gets the component ID. + /// + /// 6 + public string ComponentId + { + get + { + if (!string.IsNullOrEmpty(_componentId)) + return _componentId; + + string compId = string.Empty; + Interop.ComponentManager.ErrorCode err = Interop.ComponentManager.ComponentInfoGetComponentId(_infoHandle, out compId); + if (err != Interop.ComponentManager.ErrorCode.None) + { + Log.Warn(LogTag, "Failed to get the ComponentId. err = " + err); + } + _componentId = compId; + + return _componentId; + } + } + + /// + /// Gets the application ID of the component. + /// + /// 6 + public string ApplicationId + { + get + { + string appId = string.Empty; + Interop.ComponentManager.ErrorCode err = Interop.ComponentManager.ComponentInfoGetAppId(_infoHandle, out appId); + if (err != Interop.ComponentManager.ErrorCode.None) + { + Log.Warn(LogTag, "Failed to get the ApplicationId of " + _componentId + ". err = " + err); + } + + return appId; + } + } + + /// + /// Gets the type of the component. + /// + /// 6 + public ComponentType ComponentType + { + get + { + Interop.ComponentManager.ComponentInfoComponentType type = 0; + Interop.ComponentManager.ErrorCode err = Interop.ComponentManager.ComponentInfoGetComponentType(_infoHandle, out type); + if (err != Interop.ComponentManager.ErrorCode.None) + { + Log.Warn(LogTag, "Failed to get the Type of " + _componentId + ". err = " + err); + } + + return (ComponentType)type; + } + } + + /// + /// Checks whether the icon of the component should be displayed or not. + /// + /// 6 + public bool IsIconDisplayed + { + get + { + bool iconDisplay = false; + Interop.ComponentManager.ErrorCode err = Interop.ComponentManager.ComponentInfoIsIconDisplay(_infoHandle, out iconDisplay); + if (err != Interop.ComponentManager.ErrorCode.None) + { + Log.Warn(LogTag, "Failed to get the IsIconDisplay of " + _componentId + ". err = " + err); + } + + return iconDisplay; + } + } + + /// + /// Checks whether the component should be managed by task-manager or not. + /// + /// 6 + public bool IsManagedByTaskManager + { + get + { + bool managed = false; + Interop.ComponentManager.ErrorCode err = Interop.ComponentManager.ComponentInfoIsManagedByTaskManager(_infoHandle, out managed); + if (err != Interop.ComponentManager.ErrorCode.None) + { + Log.Warn(LogTag, "Failed to get the IsManagedByTaskManager of " + _componentId + ". err = " + err); + } + + return managed; + } + } + + /// + /// Gets the absolute path of the icon image. + /// + /// 6 + public string IconPath + { + get + { + string path = string.Empty; + Interop.ComponentManager.ErrorCode err = Interop.ComponentManager.ComponentInfoGetIcon(_infoHandle, out path); + if (err != Interop.ComponentManager.ErrorCode.None) + { + Log.Warn(LogTag, "Failed to get the IconPath of " + _componentId + ". err = " + err); + } + + return path; + } + } + + /// + /// Gets the label of the component. + /// + public string Label + { + get + { + string label = string.Empty; + Interop.ComponentManager.ErrorCode err = Interop.ComponentManager.ComponentInfoGetLabel(_infoHandle, out label); + if (err != Interop.ComponentManager.ErrorCode.None) + { + Log.Warn(LogTag, "Failed to get the Label of " + _componentId + ". err = " + err); + } + + return label; + } + } + + /// + /// Gets the localized label of the component for the given locale. + /// + /// Locale. + /// The format of locale is language and country code. (available value: "[2-letter lowercase language code (ISO 639-1)]-[2-letter lowercase country code (ISO 3166-alpha-2)]") + /// The localized label. + /// 6 + public string GetLocalizedLabel(string locale) + { + string label = string.Empty; + Interop.ComponentManager.ErrorCode err = Interop.ComponentManager.ComponentInfoGetLocalizedLabel(_infoHandle, locale, out label); + if (err != Interop.ComponentManager.ErrorCode.None) + { + Log.Warn(LogTag, "Failed to get the GetLocalizedLabel of " + _componentId + ". err = " + err); + label = Label; + } + return label; + } + + /// + /// Releases all resources used by the ComponentInfo class. + /// + /// 6 + public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(this); + } + + /// + /// Releases all resources used by the ComponentInfo class. + /// + /// Disposing + /// 6 + private void Dispose(bool disposing) + { + if (_disposed) + return; + + if (disposing) + { + } + + if (_infoHandle != IntPtr.Zero) + { + Interop.ComponentManager.ComponentInfoDestroy(_infoHandle); + _infoHandle = IntPtr.Zero; + } + _disposed = true; + } + } +} diff --git a/src/Tizen.Applications.ComponentManager/Tizen.Applications/ComponentManager.cs b/src/Tizen.Applications.ComponentManager/Tizen.Applications/ComponentManager.cs new file mode 100755 index 0000000..f37a90d --- /dev/null +++ b/src/Tizen.Applications.ComponentManager/Tizen.Applications/ComponentManager.cs @@ -0,0 +1,186 @@ +/* + * Copyright (c) 2019 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; +using System.Collections.Generic; +using System.ComponentModel; +using System.Runtime.InteropServices; +using System.Threading.Tasks; + +namespace Tizen.Applications +{ + /// + /// This class has the methods and events of the ComponentManager. + /// + /// 6 + public static class ComponentManager + { + private const string LogTag = "Tizen.Applications"; + + /// + /// Gets the information of the installed components asynchronously. + /// + /// The installed component info list. + /// Thrown when failed because of an invalid argument. + /// Thrown when failed because of the "component not exist" error or the system error. + /// Thrown when failed because of out of memory. + /// Thrown when failed because of permission denied. + /// http://tizen.org/privilege/packagemanager.info + /// 6 + public static async Task> GetInstalledComponentsAsync() + { + return await Task.Run(() => + { + Interop.ComponentManager.ErrorCode err = Interop.ComponentManager.ErrorCode.None; + List result = new List(); + + Interop.ComponentManager.ComponentManagerComponentInfoCallback cb = (IntPtr infoHandle, IntPtr userData) => + { + if (infoHandle != IntPtr.Zero) + { + IntPtr clonedHandle = IntPtr.Zero; + err = Interop.ComponentManager.ComponentInfoClone(out clonedHandle, infoHandle); + if (err != Interop.ComponentManager.ErrorCode.None) + { + Log.Warn(LogTag, "Failed to clone the ComponentInfo. err = " + err); + return false; + } + ComponentInfo info = new ComponentInfo(clonedHandle); + result.Add(info); + return true; + } + return false; + }; + err = Interop.ComponentManager.ComponentManagerForeachComponentInfo(cb, IntPtr.Zero); + if (err != Interop.ComponentManager.ErrorCode.None) + { + throw ComponentManagerErrorFactory.GetException(err, "Failed to retrieve the component info."); + } + return result; + }).ConfigureAwait(false); + } + + /// + /// Gets the information of the running components asynchronously. + /// + /// The component running context list. + /// Thrown when failed because of an invalid argument. + /// Thrown when failed because of the "component not exist" error or the system error. + /// Thrown when failed because of out of memory. + /// Thrown when failed because of permission denied. + /// http://tizen.org/privilege/packagemanager.info + /// 6 + public static async Task> GetRunningComponentsAsync() + { + return await Task.Run(() => + { + Interop.ComponentManager.ErrorCode err = Interop.ComponentManager.ErrorCode.None; + List result = new List(); + + Interop.ComponentManager.ComponentManagerComponentContextCallback cb = (IntPtr contextHandle, IntPtr userData) => + { + if (contextHandle != IntPtr.Zero) + { + IntPtr clonedHandle = IntPtr.Zero; + err = Interop.ComponentManager.ComponentContextClone(out clonedHandle, contextHandle); + if (err != Interop.ComponentManager.ErrorCode.None) + { + Log.Warn(LogTag, "Failed to clone the ComponentInfo. err = " + err); + return false; + } + ComponentRunningContext context = new ComponentRunningContext(clonedHandle); + result.Add(context); + return true; + } + return false; + }; + err = Interop.ComponentManager.ComponentManagerForeachComponentContext(cb, IntPtr.Zero); + if (err != Interop.ComponentManager.ErrorCode.None) + { + throw ComponentManagerErrorFactory.GetException(err, "Failed to retrieve the running component context."); + } + return result; + }).ConfigureAwait(false); + } + + /// + /// Checks whether the component is running or not. + /// + /// Component ID. + /// Returns true if the component is running, otherwise false. + /// Thrown when failed because of an invalid argument. + /// Thrown when failed because of the "component not exist" error or the system error. + /// Thrown when failed because of out of memory. + /// Thrown when failed because of permission denied. + /// http://tizen.org/privilege/packagemanager.info + /// 6 + public static bool IsRunning(string componentId) + { + bool isRunning = false; + Interop.ComponentManager.ErrorCode err = Interop.ComponentManager.ComponentManagerIsRunning(componentId, out isRunning); + if (err != Interop.ComponentManager.ErrorCode.None) + { + throw ComponentManagerErrorFactory.GetException(err, "Failed to check the IsRunning of " + componentId + "."); + } + return isRunning; + } + + /// + /// Terminates the component if it is running in the background. + /// + /// Component ID + /// Thrown when failed because of an invalid argument. + /// Thrown when failed because of the "component not exist" error or the system error. + /// Thrown when failed because of out of memory. + /// Thrown when failed because of permission denied. + /// http://tizen.org/privilege/appmanager.kill.bgapp + /// + /// This function returns after it just sends a request for terminating a background component. + /// Platform will decide if the target component could be terminated or not according to the state of the target component. + /// + /// 6 + public static void TerminateBackgroundComponent(ComponentRunningContext context) + { + Interop.ComponentManager.ErrorCode err = Interop.ComponentManager.ComponentManagerTerminateBgComponent(context._contextHandle); + if (err != Interop.ComponentManager.ErrorCode.None) + { + throw ComponentManagerErrorFactory.GetException(err, "Failed to send the terminate request."); + } + } + + internal static class ComponentManagerErrorFactory + { + internal static Exception GetException(Interop.ComponentManager.ErrorCode err, string message) + { + string errMessage = string.Format("{0} err = {1}", message, err); + switch (err) + { + case Interop.ComponentManager.ErrorCode.InvalidParameter: + case Interop.ComponentManager.ErrorCode.NoSuchComponent: + return new ArgumentException(errMessage); + case Interop.ComponentManager.ErrorCode.PermissionDenied: + return new UnauthorizedAccessException(errMessage); + case Interop.ComponentManager.ErrorCode.IoError: + return new global::System.IO.IOException(errMessage); + case Interop.ComponentManager.ErrorCode.OutOfMemory: + return new OutOfMemoryException(errMessage); + default: + return new InvalidOperationException(errMessage); + } + } + } + } +} diff --git a/src/Tizen.Applications.ComponentManager/Tizen.Applications/ComponentRunningContext.cs b/src/Tizen.Applications.ComponentManager/Tizen.Applications/ComponentRunningContext.cs new file mode 100755 index 0000000..5735948 --- /dev/null +++ b/src/Tizen.Applications.ComponentManager/Tizen.Applications/ComponentRunningContext.cs @@ -0,0 +1,314 @@ +/* + * Copyright (c) 2019 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; +using System.Collections.Generic; +using System.ComponentModel; +using System.Runtime.InteropServices; +using System.Threading.Tasks; + +namespace Tizen.Applications +{ + /// + /// This class provides methods and properties to get information of the running component. + /// + /// 6 + public class ComponentRunningContext : IDisposable + { + private const string LogTag = "Tizen.Applications"; + private bool _disposed = false; + internal IntPtr _contextHandle = IntPtr.Zero; + private string _componentId = string.Empty; + + internal ComponentRunningContext(IntPtr contextHandle) + { + _contextHandle = contextHandle; + } + + /// + /// A constructor of ComponentRunningContext that takes the component ID. + /// + /// Component ID. + /// Thrown when failed because of an invalid argument. + /// Thrown when failed because of the "component not exist" error or the system error. + /// Thrown when failed because of out of memory. + /// Thrown when failed because of permission denied. + /// http://tizen.org/privilege/packagemanager.info + /// 6 + public ComponentRunningContext(string componentId) + { + _componentId = componentId; + IntPtr contextHandle = IntPtr.Zero; + Interop.ComponentManager.ErrorCode err = Interop.ComponentManager.ComponentManagerGetComponentContext(_componentId, out contextHandle); + if (err != Interop.ComponentManager.ErrorCode.None) + { + throw ComponentManager.ComponentManagerErrorFactory.GetException(err, "Failed to create the ComponentRunningContext of " + _componentId + "."); + } + _contextHandle = contextHandle; + } + + /// + /// Destructor of the class. + /// + ~ComponentRunningContext() + { + Dispose(false); + } + + /// + /// Enumeration for the component state. + /// + /// 6 + public enum ComponentState + { + /// + /// The Initialized state. This is the state when the component is constructed but OnCreate() is not called yet. + /// + Initialized = 0, + + /// + /// The created state. This state is reached after OnCreate() is called. + /// + Created, + + /// + /// The started state. This state is reached after OnStart() or OnStartCommand() is called. + /// + Started, + + /// + /// The resumed state. This state is reached after OnResume() is called. + /// + Resumed, + + /// + /// The paused state. This state is reached after OnPause() is called. + /// + Paused, + + /// + /// The destroyed state. This state is reached right before OnDestroy() call. + /// + Destroyed + } + + /// + /// Gets the ID of the component. + /// + /// 6 + public string ComponentId + { + get + { + if (!string.IsNullOrEmpty(_componentId)) + return _componentId; + + string componentId = string.Empty; + Interop.ComponentManager.ErrorCode err = Interop.ComponentManager.ComponentContextGetComponentId(_contextHandle, out componentId); + if (err != Interop.ComponentManager.ErrorCode.None) + { + Log.Warn(LogTag, "Failed to get the ComponentId. err = " + err); + } + _componentId = componentId; + + return _componentId; + } + } + + /// + /// Gets the application ID of the component. + /// + /// 6 + public string ApplicationId + { + get + { + string appId = string.Empty; + Interop.ComponentManager.ErrorCode err = Interop.ComponentManager.ComponentContextGetAppId(_contextHandle, out appId); + if (err != Interop.ComponentManager.ErrorCode.None) + { + Log.Warn(LogTag, "Failed to get the ApplicationId of " + _componentId + ". err = " + err); + } + + return appId; + } + } + + /// + /// Gets the instance ID of the component. + /// + /// 6 + public string InstanceId + { + get + { + string instanceId = string.Empty; + Interop.ComponentManager.ErrorCode err = Interop.ComponentManager.ComponentContextGetInstanceId(_contextHandle, out instanceId); + if (err != Interop.ComponentManager.ErrorCode.None) + { + Log.Warn(LogTag, "Failed to get the InstanceId of " + _componentId + ". err = " + err); + } + + return instanceId; + } + } + + /// + /// Gets the state of the component. + /// + /// 6 + public ComponentState State + { + get + { + int state = 0; + Interop.ComponentManager.ErrorCode err = Interop.ComponentManager.ComponentContextGetComponentState(_contextHandle, out state); + if (err != Interop.ComponentManager.ErrorCode.None) + { + Log.Warn(LogTag, "Failed to get the State of " + _componentId + ". err = " + err); + } + + return (ComponentState)state; + } + } + + /// + /// Checks whether the component is terminated or not. + /// + /// 6 + public bool IsTerminated + { + get + { + bool isTerminated = false; + Interop.ComponentManager.ErrorCode err = Interop.ComponentManager.ComponentContextIsTerminated(_contextHandle, out isTerminated); + if (err != Interop.ComponentManager.ErrorCode.None) + { + Log.Warn(LogTag, "Failed to get the IsTerminated of " + _componentId + ". err = " + err); + } + + return isTerminated; + } + } + + /// + /// Checks whether the component is running as sub component of the group. + /// + /// 6 + public bool IsSubComponent + { + get + { + bool isSubComponent = false; + Interop.ComponentManager.ErrorCode err = Interop.ComponentManager.ComponentContextIsSubComponent(_contextHandle, out isSubComponent); + if (err != Interop.ComponentManager.ErrorCode.None) + { + Log.Warn(LogTag, "Failed to get the IsSubComponent of " + _componentId + ". err = " + err); + } + + return isSubComponent; + } + } + + /// + /// Resumes the running component. + /// + /// Thrown when failed because of an invalid argument. + /// Thrown when failed because of the system error. + /// Thrown when failed because of out of memory. + /// Thrown when failed because of permission denied. + /// http://tizen.org/privilege/appmanager.launch + /// 6 + public void Resume() + { + Interop.ComponentManager.ErrorCode err = Interop.ComponentManager.ComponentManagerResumeComponent(_contextHandle); + if (err != Interop.ComponentManager.ErrorCode.None) + { + throw ComponentManager.ComponentManagerErrorFactory.GetException(err, "Failed to Send the resume request."); + } + } + + /// + /// Pauses the running component. + /// + /// Thrown when failed because of an invalid argument. + /// Thrown when failed because of the system error. + /// Thrown when failed because of out of memory. + /// Thrown when failed because of permission denied. + /// http://tizen.org/privilege/appmanager.launch + /// 6 + [EditorBrowsable(EditorBrowsableState.Never)] + public void Pause() + { + Interop.ComponentManager.ErrorCode err = Interop.ComponentManager.ComponentManagerPauseComponent(_contextHandle); + if (err != Interop.ComponentManager.ErrorCode.None) + { + throw ComponentManager.ComponentManagerErrorFactory.GetException(err, "Failed to Send the pause request."); + } + } + + /// + /// Terminates the running component. + /// + /// Thrown when failed because of an invalid argument. + /// Thrown when failed because of the system error. + /// Thrown when failed because of out of memory. + /// Thrown when failed because of permission denied. + /// http://tizen.org/privilege/appmanager.launch + /// 6 + [EditorBrowsable(EditorBrowsableState.Never)] + public void Terminate() + { + Interop.ComponentManager.ErrorCode err = Interop.ComponentManager.ComponentManagerTerminateComponent(_contextHandle); + if (err != Interop.ComponentManager.ErrorCode.None) + { + throw ComponentManager.ComponentManagerErrorFactory.GetException(err, "Failed to Send the terminate request."); + } + } + + /// + /// Releases all resources used by the ComponentInfo class. + /// + /// 6 + public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(this); + } + + /// + /// Releases all resources used by the ComponentInfo class. + /// + /// Disposing + /// 6 + private void Dispose(bool disposing) + { + if (_disposed) + return; + + if (disposing) + { + } + + if (_contextHandle != IntPtr.Zero) + { + Interop.ComponentManager.ComponentContextDestroy(_contextHandle); + _contextHandle = IntPtr.Zero; + } + _disposed = true; + } + } +} diff --git a/src/Tizen.Applications.ComponentManager/Tizen.Applications/ComponentType.cs b/src/Tizen.Applications.ComponentManager/Tizen.Applications/ComponentType.cs new file mode 100755 index 0000000..7306f09 --- /dev/null +++ b/src/Tizen.Applications.ComponentManager/Tizen.Applications/ComponentType.cs @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2019 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. + */ + +namespace Tizen.Applications +{ + /// + /// Enumeration for component type. + /// + /// 6 + public enum ComponentType + { + /// + /// Frame component + /// + Frame = 0, + + /// + /// Service component + /// + Service = 1 + } +} -- 2.7.4