Add ApplicationRunningContext class
authorSangyoon Jang <s89.jang@samsung.com>
Fri, 10 Mar 2017 09:18:03 +0000 (18:18 +0900)
committerSangyoon Jang <s89.jang@samsung.com>
Fri, 17 Mar 2017 01:03:48 +0000 (18:03 -0700)
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 <s89.jang@samsung.com>
Tizen.Applications/Tizen.Applications.csproj
src/Tizen.Applications.Common/Interop/Interop.ApplicationManager.cs
src/Tizen.Applications.Common/Tizen.Applications.Common.csproj
src/Tizen.Applications.Common/Tizen.Applications/ApplicationInfo.cs
src/Tizen.Applications.Common/Tizen.Applications/ApplicationLaunchedEventArgs.cs
src/Tizen.Applications.Common/Tizen.Applications/ApplicationManager.cs
src/Tizen.Applications.Common/Tizen.Applications/ApplicationRunningContext.cs [new file with mode: 0644]
src/Tizen.Applications.Common/Tizen.Applications/ApplicationTerminatedEventArgs.cs

index 48adeff..2600bae 100755 (executable)
@@ -64,6 +64,7 @@
     <Compile Include="../Tizen.Applications.Common/Tizen.Applications/ApplicationInfoMetadataFilter.cs" />
     <Compile Include="../Tizen.Applications.Common/Tizen.Applications/ApplicationLaunchedEventArgs.cs" />
     <Compile Include="../Tizen.Applications.Common/Tizen.Applications/ApplicationManager.cs" />
+    <Compile Include="../Tizen.Applications.Common/Tizen.Applications/ApplicationRunningContext.cs" />
     <Compile Include="../Tizen.Applications.Common/Tizen.Applications/ApplicationTerminatedEventArgs.cs" />
     <Compile Include="../Tizen.Applications.Common/Tizen.Applications/ApplicationType.cs" />
     <Compile Include="../Tizen.Applications.Common/Tizen.Applications/Bundle.cs" />
index 4d45cdb..d93378a 100755 (executable)
@@ -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);
index eca4af7..2a69bc6 100644 (file)
@@ -59,6 +59,7 @@
     <Compile Include="Tizen.Applications\ApplicationEnabledEventArgs.cs" />
     <Compile Include="Tizen.Applications\ApplicationDisabledEventArgs.cs" />
     <Compile Include="Tizen.Applications\ApplicationEventState.cs" />
+    <Compile Include="Tizen.Applications\ApplicationRunningContext.cs" />
     <Compile Include="Tizen.Applications\ApplicationInfo.cs" />
     <Compile Include="Tizen.Applications\ApplicationManager.cs" />
     <Compile Include="Tizen.Applications\ApplicationInfoFilter.cs" />
index f9230fc..eb5e48e 100755 (executable)
@@ -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;
-        }
-
         /// <summary>
         /// A constructor of ApplicationInfo that takes the application id.
         /// </summary>
@@ -60,37 +53,6 @@ namespace Tizen.Applications
         }
 
         /// <summary>
-        /// Enumeration for the Application State.
-        /// </summary>
-        public enum AppState
-        {
-            /// <summary>
-            /// The undefined state
-            /// </summary>
-            Undefined = 0,
-
-            /// <summary>
-            /// The UI application is running in the foreground.
-            /// </summary>
-            Foreground,
-
-            /// <summary>
-            /// The UI application is running in the background.
-            /// </summary>
-            Background,
-
-            /// <summary>
-            /// The Service application is running.
-            /// </summary>
-            Service,
-
-            /// <summary>
-            /// The application is terminated.
-            /// </summary>
-            Terminated,
-        }
-
-        /// <summary>
         /// Gets the application id.
         /// </summary>
         public string ApplicationId
@@ -314,41 +276,6 @@ namespace Tizen.Applications
         }
 
         /// <summary>
-        /// Gets the application's process id. If the application is not running, the value will be zero (0).
-        /// </summary>
-        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;
-            }
-        }
-
-        /// <summary>
-        /// Checks whether the application is running. It returns the installed application running state.
-        /// </summary>
-        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;
-            }
-        }
-
-        /// <summary>
         /// Gets the shared data path.
         /// </summary>
         public string SharedDataPath
@@ -432,57 +359,6 @@ namespace Tizen.Applications
             return label;
         }
 
-        /// <summary>
-        /// Gets the state of the application.
-        /// </summary>
-        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;
-            }
-        }
-
-        /// <summary>
-        /// Checks whether the application is running as a sub application of the application group.
-        /// </summary>
-        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;
         }
     }
index d45a451..7f9c4b7 100755 (executable)
@@ -26,7 +26,6 @@ namespace Tizen.Applications
         /// <summary>
         /// The information of the application.
         /// </summary>
-        public ApplicationInfo ApplicationInfo { get; internal set; }
+        public ApplicationRunningContext ApplicationRunningContext { get; internal set; }
     }
-}
-
+}
\ No newline at end of file
index 0c08a2f..9a86c18 100644 (file)
@@ -229,45 +229,27 @@ namespace Tizen.Applications
         /// <summary>
         /// Gets the information of the running applications asynchronously.
         /// </summary>
-        public static async Task<IEnumerable<ApplicationInfo>> GetRunningApplicationsAsync()
+        public static async Task<IEnumerable<ApplicationRunningContext>> GetRunningApplicationsAsync()
         {
             return await Task.Run(() =>
             {
                 Interop.ApplicationManager.ErrorCode err = Interop.ApplicationManager.ErrorCode.None;
-                List<ApplicationInfo> result = new List<ApplicationInfo>();
+                List<ApplicationRunningContext> result = new List<ApplicationRunningContext>();
 
                 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
         }
 
         /// <summary>
-        /// Gets the information of all running applications asynchronously.
+        /// Gets the information of the running applications including subapp asynchronously.
         /// </summary>
-        public static async Task<IEnumerable<ApplicationInfo>> GetAllRunningApplicationsAsync()
+        public static async Task<IEnumerable<ApplicationRunningContext>> GetAllRunningApplicationsAsync()
         {
             return await Task.Run(() =>
             {
                 Interop.ApplicationManager.ErrorCode err = Interop.ApplicationManager.ErrorCode.None;
-                List<ApplicationInfo> result = new List<ApplicationInfo>();
+                List<ApplicationRunningContext> result = new List<ApplicationRunningContext>();
 
                 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 (file)
index 0000000..35d9223
--- /dev/null
@@ -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
+{
+    /// <summary>
+    /// This class provides methods and properties to get information of the application.
+    /// </summary>
+    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;
+        }
+
+        /// <summary>
+        /// A constructor of ApplicationRunningContext that takes the application id.
+        /// </summary>
+        /// <param name="applicationId">application id.</param>
+        /// <exception cref="ArgumentException">Thrown when failed of invalid argument.</exception>
+        /// <exception cref="InvalidOperationException">Thrown when failed because of application not exist error or system error.</exception>
+        /// <exception cref="OutOfMemoryException">Thrown when failed because of out of memory.</exception>
+        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;
+        }
+
+        /// <summary>
+        /// Destructor of the class
+        /// </summary>
+        ~ApplicationRunningContext()
+        {
+            Dispose(false);
+        }
+
+        /// <summary>
+        /// Enumeration for the Application State.
+        /// </summary>
+        public enum AppState
+        {
+            /// <summary>
+            /// The undefined state
+            /// </summary>
+            Undefined = 0,
+
+            /// <summary>
+            /// The UI application is running in the foreground.
+            /// </summary>
+            Foreground,
+
+            /// <summary>
+            /// The UI application is running in the background.
+            /// </summary>
+            Background,
+
+            /// <summary>
+            /// The Service application is running.
+            /// </summary>
+            Service,
+
+            /// <summary>
+            /// The application is terminated.
+            /// </summary>
+            Terminated,
+        }
+
+        /// <summary>
+        /// Gets the application id.
+        /// </summary>
+        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;
+            }
+        }
+
+        /// <summary>
+        /// Gets the package id of the application.
+        /// </summary>
+        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;
+            }
+        }
+
+        /// <summary>
+        /// Gets the application's process id.
+        /// </summary>
+        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;
+            }
+        }
+
+        /// <summary>
+        /// Gets the state of the application.
+        /// </summary>
+        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;
+            }
+        }
+
+        /// <summary>
+        /// Gets whether the application is sub application of the application group.
+        /// </summary>
+        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;
+            }
+        }
+
+        /// <summary>
+        /// Releases all resources used by the ApplicationRunningContext class.
+        /// </summary>
+        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
index befdbbd..622dc91 100755 (executable)
@@ -26,7 +26,6 @@ namespace Tizen.Applications
         /// <summary>
         /// The information of the application.
         /// </summary>
-        public ApplicationInfo ApplicationInfo { get; internal set; }
+        public ApplicationRunningContext ApplicationRunningContext { get; internal set; }
     }
-}
-
+}
\ No newline at end of file