From 9309524f66351dc0f7ee2c122f8e790bafe3372f Mon Sep 17 00:00:00 2001 From: Sangyoon Jang Date: Fri, 10 Mar 2017 18:18:03 +0900 Subject: [PATCH] Add ApplicationRunningContext class ApplicationRunningContext has dynamic informations of application such as app state, process id. Some properties of ApplicationInfo class is removed or moved into ApplicationRunningContext class. Now ApplicationManager.GetRunningApplicationsAsync returns a list of ApplicationRunningContext and ApplicationLaunched/Terminated event passes ApplicationRunningContext class. Change-Id: Iab37846028d0f04de3a4de419cd4b923c3f0644e Signed-off-by: Sangyoon Jang --- Tizen.Applications/Tizen.Applications.csproj | 1 + .../Interop/Interop.ApplicationManager.cs | 6 +- .../Tizen.Applications.Common.csproj | 1 + .../Tizen.Applications/ApplicationInfo.cs | 129 ------------- .../ApplicationLaunchedEventArgs.cs | 5 +- .../Tizen.Applications/ApplicationManager.cs | 95 +++------ .../ApplicationRunningContext.cs | 213 +++++++++++++++++++++ .../ApplicationTerminatedEventArgs.cs | 5 +- 8 files changed, 247 insertions(+), 208 deletions(-) create mode 100644 src/Tizen.Applications.Common/Tizen.Applications/ApplicationRunningContext.cs diff --git a/Tizen.Applications/Tizen.Applications.csproj b/Tizen.Applications/Tizen.Applications.csproj index 48adeff..2600bae 100755 --- a/Tizen.Applications/Tizen.Applications.csproj +++ b/Tizen.Applications/Tizen.Applications.csproj @@ -64,6 +64,7 @@ + diff --git a/src/Tizen.Applications.Common/Interop/Interop.ApplicationManager.cs b/src/Tizen.Applications.Common/Interop/Interop.ApplicationManager.cs index 4d45cdb..d93378a 100755 --- a/src/Tizen.Applications.Common/Interop/Interop.ApplicationManager.cs +++ b/src/Tizen.Applications.Common/Interop/Interop.ApplicationManager.cs @@ -174,6 +174,10 @@ internal static partial class Interop internal static extern ErrorCode AppContextGetAppId(IntPtr handle, out string applicationId); //int app_context_get_app_id(app_context_h app_context, char **app_id) + [DllImport(Libraries.AppManager, EntryPoint = "app_context_get_package_id")] + internal static extern ErrorCode AppContextGetPackageId(IntPtr handle, out string packageId); + //int app_context_get_package_id(app_context_h app_context, char **package_id) + [DllImport(Libraries.AppManager, EntryPoint = "app_context_get_pid")] internal static extern ErrorCode AppContextGetPid(IntPtr handle, out int processId); //int app_context_get_pid (app_context_h app_context, pid_t *pid) @@ -192,7 +196,7 @@ internal static partial class Interop [DllImport(Libraries.AppManager, EntryPoint = "app_context_is_sub_app")] internal static extern ErrorCode AppContextIsSubApp(IntPtr handle, out bool is_sub_app); - //int app_context_is_sub_app(app_context_h app_context, bool *is_sub_app); + //int app_context_is_sub_app (app_context_h app_context, bool *is_sub_app); [DllImport(Libraries.AppManager, EntryPoint = "app_context_clone")] internal static extern ErrorCode AppContextClone(out IntPtr destination, IntPtr source); diff --git a/src/Tizen.Applications.Common/Tizen.Applications.Common.csproj b/src/Tizen.Applications.Common/Tizen.Applications.Common.csproj index eca4af7..2a69bc6 100644 --- a/src/Tizen.Applications.Common/Tizen.Applications.Common.csproj +++ b/src/Tizen.Applications.Common/Tizen.Applications.Common.csproj @@ -59,6 +59,7 @@ + diff --git a/src/Tizen.Applications.Common/Tizen.Applications/ApplicationInfo.cs b/src/Tizen.Applications.Common/Tizen.Applications/ApplicationInfo.cs index f9230fc..eb5e48e 100755 --- a/src/Tizen.Applications.Common/Tizen.Applications/ApplicationInfo.cs +++ b/src/Tizen.Applications.Common/Tizen.Applications/ApplicationInfo.cs @@ -27,7 +27,6 @@ namespace Tizen.Applications private const string LogTag = "Tizen.Applications"; private bool _disposed = false; private IntPtr _infoHandle = IntPtr.Zero; - private IntPtr _contextHandle = IntPtr.Zero; private string _applicationId = string.Empty; private Interop.ApplicationManager.ErrorCode err = Interop.ApplicationManager.ErrorCode.None; @@ -36,12 +35,6 @@ namespace Tizen.Applications _infoHandle = infoHandle; } - internal ApplicationInfo(IntPtr infoHandle, IntPtr contextHandle) - { - _infoHandle = infoHandle; - _contextHandle = contextHandle; - } - /// /// A constructor of ApplicationInfo that takes the application id. /// @@ -60,37 +53,6 @@ namespace Tizen.Applications } /// - /// Enumeration for the Application State. - /// - public enum AppState - { - /// - /// The undefined state - /// - Undefined = 0, - - /// - /// The UI application is running in the foreground. - /// - Foreground, - - /// - /// The UI application is running in the background. - /// - Background, - - /// - /// The Service application is running. - /// - Service, - - /// - /// The application is terminated. - /// - Terminated, - } - - /// /// Gets the application id. /// public string ApplicationId @@ -314,41 +276,6 @@ namespace Tizen.Applications } /// - /// Gets the application's process id. If the application is not running, the value will be zero (0). - /// - public int ProcessId - { - get - { - int pid = 0; - IntPtr contextHandle = GetContextHandle(); - err = Interop.ApplicationManager.AppContextGetPid(contextHandle, out pid); - if (err != Interop.ApplicationManager.ErrorCode.None) - { - Log.Warn(LogTag, "Failed to get the process id. err = " + err); - } - return pid; - } - } - - /// - /// Checks whether the application is running. It returns the installed application running state. - /// - public bool IsRunning - { - get - { - bool running = false; - err = Interop.ApplicationManager.AppManagerIsRunning(ApplicationId, out running); - if (err != Interop.ApplicationManager.ErrorCode.None) - { - Log.Warn(LogTag, "Failed to get the IsRunning value. err = " + err); - } - return running; - } - } - - /// /// Gets the shared data path. /// public string SharedDataPath @@ -432,57 +359,6 @@ namespace Tizen.Applications return label; } - /// - /// Gets the state of the application. - /// - public AppState State - { - get - { - int value = 0; - IntPtr contextHandle = GetContextHandle(); - err = Interop.ApplicationManager.AppContextGetAppState(contextHandle, out value); - if (err != Interop.ApplicationManager.ErrorCode.None) - { - Log.Warn(LogTag, "Failed to get the app state. err = " + err); - } - return (AppState)value; - } - } - - /// - /// Checks whether the application is running as a sub application of the application group. - /// - public bool IsSubApp - { - get - { - bool value = false; - IntPtr contextHandle = GetContextHandle(); - err = Interop.ApplicationManager.AppContextIsSubApp(contextHandle, out value); - if (err != Interop.ApplicationManager.ErrorCode.None) - { - Log.Warn(LogTag, "Failed to get IsSubApp. err = " + err); - } - return value; - } - } - - private IntPtr GetContextHandle() - { - if (_contextHandle == IntPtr.Zero) - { - IntPtr contextHandle = IntPtr.Zero; - err = Interop.ApplicationManager.AppManagerGetAppContext(_applicationId, out contextHandle); - if (err != Interop.ApplicationManager.ErrorCode.None) - { - Log.Warn(LogTag, "Failed to get the handle of the ApplicationContext. err = " + err); - } - _contextHandle = contextHandle; - } - return _contextHandle; - } - private IntPtr GetInfoHandle() { if (_infoHandle == IntPtr.Zero) @@ -519,11 +395,6 @@ namespace Tizen.Applications Interop.ApplicationManager.AppInfoDestroy(_infoHandle); _infoHandle = IntPtr.Zero; } - if (_contextHandle != IntPtr.Zero) - { - Interop.ApplicationManager.AppContextDestroy(_contextHandle); - _contextHandle = IntPtr.Zero; - } _disposed = true; } } diff --git a/src/Tizen.Applications.Common/Tizen.Applications/ApplicationLaunchedEventArgs.cs b/src/Tizen.Applications.Common/Tizen.Applications/ApplicationLaunchedEventArgs.cs index d45a451..7f9c4b7 100755 --- a/src/Tizen.Applications.Common/Tizen.Applications/ApplicationLaunchedEventArgs.cs +++ b/src/Tizen.Applications.Common/Tizen.Applications/ApplicationLaunchedEventArgs.cs @@ -26,7 +26,6 @@ namespace Tizen.Applications /// /// The information of the application. /// - public ApplicationInfo ApplicationInfo { get; internal set; } + public ApplicationRunningContext ApplicationRunningContext { get; internal set; } } -} - +} \ No newline at end of file diff --git a/src/Tizen.Applications.Common/Tizen.Applications/ApplicationManager.cs b/src/Tizen.Applications.Common/Tizen.Applications/ApplicationManager.cs index 0c08a2f..9a86c18 100644 --- a/src/Tizen.Applications.Common/Tizen.Applications/ApplicationManager.cs +++ b/src/Tizen.Applications.Common/Tizen.Applications/ApplicationManager.cs @@ -229,45 +229,27 @@ namespace Tizen.Applications /// /// Gets the information of the running applications asynchronously. /// - public static async Task> GetRunningApplicationsAsync() + public static async Task> GetRunningApplicationsAsync() { return await Task.Run(() => { Interop.ApplicationManager.ErrorCode err = Interop.ApplicationManager.ErrorCode.None; - List result = new List(); + List result = new List(); Interop.ApplicationManager.AppManagerAppContextCallback cb = (IntPtr contextHandle, IntPtr userData) => { if (contextHandle != IntPtr.Zero) { - string appid = string.Empty; - err = Interop.ApplicationManager.AppContextGetAppId(contextHandle, out appid); - if (err != Interop.ApplicationManager.ErrorCode.None) - { - Log.Warn(LogTag, "Failed to get appid. err = " + err); - return false; - } - IntPtr infoHandle = IntPtr.Zero; - err = Interop.ApplicationManager.AppManagerGetAppInfo(appid, out infoHandle); - if (err != Interop.ApplicationManager.ErrorCode.None) - { - Log.Warn(LogTag, "Failed to get the application information."); - return false; - } - IntPtr cloneContextHandle = IntPtr.Zero; - err = Interop.ApplicationManager.AppContextClone(out cloneContextHandle, contextHandle); + IntPtr clonedHandle = IntPtr.Zero; + err = Interop.ApplicationManager.AppContextClone(out clonedHandle, contextHandle); if (err != Interop.ApplicationManager.ErrorCode.None) { - Log.Warn(LogTag, "Failed to clone the application context handle"); - Interop.ApplicationManager.AppInfoDestroy(infoHandle); + Log.Warn(LogTag, "Failed to clone the app context. err = " + err); return false; } - ApplicationInfo app = new ApplicationInfo(infoHandle, cloneContextHandle); - if (app != null) - { - result.Add(app); - return true; - } + ApplicationRunningContext context = new ApplicationRunningContext(clonedHandle); + result.Add(context); + return true; } return false; }; @@ -282,47 +264,29 @@ namespace Tizen.Applications } /// - /// Gets the information of all running applications asynchronously. + /// Gets the information of the running applications including subapp asynchronously. /// - public static async Task> GetAllRunningApplicationsAsync() + public static async Task> GetAllRunningApplicationsAsync() { return await Task.Run(() => { Interop.ApplicationManager.ErrorCode err = Interop.ApplicationManager.ErrorCode.None; - List result = new List(); + List result = new List(); Interop.ApplicationManager.AppManagerAppContextCallback cb = (IntPtr contextHandle, IntPtr userData) => { if (contextHandle != IntPtr.Zero) { - string appid = string.Empty; - err = Interop.ApplicationManager.AppContextGetAppId(contextHandle, out appid); - if (err != Interop.ApplicationManager.ErrorCode.None) - { - Log.Warn(LogTag, "Failed to get appid. err = " + err); - return false; - } - IntPtr infoHandle = IntPtr.Zero; - err = Interop.ApplicationManager.AppManagerGetAppInfo(appid, out infoHandle); - if (err != Interop.ApplicationManager.ErrorCode.None) - { - Log.Warn(LogTag, "Failed to get the application information."); - return false; - } - IntPtr cloneContextHandle = IntPtr.Zero; - err = Interop.ApplicationManager.AppContextClone(out cloneContextHandle, contextHandle); + IntPtr clonedHandle = IntPtr.Zero; + err = Interop.ApplicationManager.AppContextClone(out clonedHandle, contextHandle); if (err != Interop.ApplicationManager.ErrorCode.None) { - Log.Warn(LogTag, "Failed to clone the application context handle"); - Interop.ApplicationManager.AppInfoDestroy(infoHandle); + Log.Warn(LogTag, "Failed to clone the app context. err = " + err); return false; } - ApplicationInfo app = new ApplicationInfo(infoHandle, cloneContextHandle); - if (app != null) - { - result.Add(app); - return true; - } + ApplicationRunningContext context = new ApplicationRunningContext(clonedHandle); + result.Add(context); + return true; } return false; }; @@ -358,28 +322,15 @@ namespace Tizen.Applications s_applicationChangedEventCallback = (IntPtr contextHandle, Interop.ApplicationManager.AppContextEvent state, IntPtr userData) => { if (contextHandle == IntPtr.Zero) return; - try - { - string appid = string.Empty; - err = Interop.ApplicationManager.AppContextGetAppId(contextHandle, out appid); - if (err != Interop.ApplicationManager.ErrorCode.None) - { - throw ApplicationManagerErrorFactory.GetException(err, "Failed to get application id."); - } - ApplicationInfo appInfo = GetInstalledApplication(appid); - if (state == Interop.ApplicationManager.AppContextEvent.Launched) - { - s_launchedHandler?.Invoke(null, new ApplicationLaunchedEventArgs { ApplicationInfo = appInfo }); - } - else if (state == Interop.ApplicationManager.AppContextEvent.Terminated) - { - s_terminatedHandler?.Invoke(null, new ApplicationTerminatedEventArgs { ApplicationInfo = appInfo }); - } + ApplicationRunningContext context = new ApplicationRunningContext(contextHandle); + if (state == Interop.ApplicationManager.AppContextEvent.Launched) + { + s_launchedHandler?.Invoke(null, new ApplicationLaunchedEventArgs { ApplicationRunningContext = context }); } - catch (Exception e) + else if (state == Interop.ApplicationManager.AppContextEvent.Terminated) { - Log.Warn(LogTag, e.Message); + s_terminatedHandler?.Invoke(null, new ApplicationTerminatedEventArgs { ApplicationRunningContext = context }); } }; err = Interop.ApplicationManager.AppManagerSetAppContextEvent(s_applicationChangedEventCallback, IntPtr.Zero); diff --git a/src/Tizen.Applications.Common/Tizen.Applications/ApplicationRunningContext.cs b/src/Tizen.Applications.Common/Tizen.Applications/ApplicationRunningContext.cs new file mode 100644 index 0000000..35d9223 --- /dev/null +++ b/src/Tizen.Applications.Common/Tizen.Applications/ApplicationRunningContext.cs @@ -0,0 +1,213 @@ +/* + * 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; +using System.Collections.Generic; + +namespace Tizen.Applications +{ + /// + /// This class provides methods and properties to get information of the application. + /// + public class ApplicationRunningContext : IDisposable + { + private const string LogTag = "Tizen.Applications"; + private bool _disposed = false; + private IntPtr _contextHandle = IntPtr.Zero; + private Interop.ApplicationManager.ErrorCode err = Interop.ApplicationManager.ErrorCode.None; + + internal ApplicationRunningContext(IntPtr contextHandle) + { + _contextHandle = contextHandle; + } + + /// + /// A constructor of ApplicationRunningContext that takes the application id. + /// + /// application id. + /// Thrown when failed of invalid argument. + /// Thrown when failed because of application not exist error or system error. + /// Thrown when failed because of out of memory. + public ApplicationRunningContext(string applicationId) + { + IntPtr contextHandle = IntPtr.Zero; + err = Interop.ApplicationManager.AppManagerGetAppContext(applicationId, out contextHandle); + if (err != Interop.ApplicationManager.ErrorCode.None) + { + Log.Warn(LogTag, "Failed to get the handle of the ApplicationRunningContext. err = " + err); + switch (err) + { + case Interop.ApplicationManager.ErrorCode.InvalidParameter: + throw new ArgumentException("Invalid Parameter."); + case Interop.ApplicationManager.ErrorCode.NoSuchApp: + throw new InvalidOperationException("No such application."); + case Interop.ApplicationManager.ErrorCode.OutOfMemory: + throw new OutOfMemoryException("Out of memory"); + default: + throw new InvalidOperationException("Invalid Operation."); + } + } + _contextHandle = contextHandle; + } + + /// + /// Destructor of the class + /// + ~ApplicationRunningContext() + { + Dispose(false); + } + + /// + /// Enumeration for the Application State. + /// + public enum AppState + { + /// + /// The undefined state + /// + Undefined = 0, + + /// + /// The UI application is running in the foreground. + /// + Foreground, + + /// + /// The UI application is running in the background. + /// + Background, + + /// + /// The Service application is running. + /// + Service, + + /// + /// The application is terminated. + /// + Terminated, + } + + /// + /// Gets the application id. + /// + public string ApplicationId + { + get + { + string appid = string.Empty; + err = Interop.ApplicationManager.AppContextGetAppId(_contextHandle, out appid); + if (err != Interop.ApplicationManager.ErrorCode.None) + { + Log.Warn(LogTag, "Failed to get the application id. err = " + err); + } + return appid; + } + } + + /// + /// Gets the package id of the application. + /// + public string PackageId + { + get + { + string packageid = string.Empty; + err = Interop.ApplicationManager.AppContextGetPackageId(_contextHandle, out packageid); + if (err != Interop.ApplicationManager.ErrorCode.None) + { + Log.Warn(LogTag, "Failed to get the package id. err = " + err); + } + return packageid; + } + } + + /// + /// Gets the application's process id. + /// + public int ProcessId + { + get + { + int pid = 0; + err = Interop.ApplicationManager.AppContextGetPid(_contextHandle, out pid); + if (err != Interop.ApplicationManager.ErrorCode.None) + { + Log.Warn(LogTag, "Failed to get the process id. err = " + err); + } + return pid; + } + } + + /// + /// Gets the state of the application. + /// + public AppState State + { + get + { + int state = 0; + + err = Interop.ApplicationManager.AppContextGetAppState(_contextHandle, out state); + if (err != Interop.ApplicationManager.ErrorCode.None) + { + Log.Warn(LogTag, "Failed to get the application state. err = " + err); + } + return (AppState)state; + } + } + + /// + /// Gets whether the application is sub application of the application group. + /// + public bool IsSubApp + { + get + { + bool subapp = false; + err = Interop.ApplicationManager.AppContextIsSubApp(_contextHandle, out subapp); + if (err != Interop.ApplicationManager.ErrorCode.None) + { + Log.Warn(LogTag, "Failed to get the IsSubApp value. err = " + err); + } + return subapp; + } + } + + /// + /// Releases all resources used by the ApplicationRunningContext class. + /// + public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(this); + } + + private void Dispose(bool disposing) + { + if (_disposed) + return; + + if (_contextHandle != IntPtr.Zero) + { + Interop.ApplicationManager.AppContextDestroy(_contextHandle); + _contextHandle = IntPtr.Zero; + } + _disposed = true; + } + } +} \ No newline at end of file diff --git a/src/Tizen.Applications.Common/Tizen.Applications/ApplicationTerminatedEventArgs.cs b/src/Tizen.Applications.Common/Tizen.Applications/ApplicationTerminatedEventArgs.cs index befdbbd..622dc91 100755 --- a/src/Tizen.Applications.Common/Tizen.Applications/ApplicationTerminatedEventArgs.cs +++ b/src/Tizen.Applications.Common/Tizen.Applications/ApplicationTerminatedEventArgs.cs @@ -26,7 +26,6 @@ namespace Tizen.Applications /// /// The information of the application. /// - public ApplicationInfo ApplicationInfo { get; internal set; } + public ApplicationRunningContext ApplicationRunningContext { get; internal set; } } -} - +} \ No newline at end of file -- 2.7.4