<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" />
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)
[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);
<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" />
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;
_infoHandle = infoHandle;
}
- internal ApplicationInfo(IntPtr infoHandle, IntPtr contextHandle)
- {
- _infoHandle = infoHandle;
- _contextHandle = contextHandle;
- }
-
/// <summary>
/// A constructor of ApplicationInfo that takes the application id.
/// </summary>
}
/// <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
}
/// <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
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)
Interop.ApplicationManager.AppInfoDestroy(_infoHandle);
_infoHandle = IntPtr.Zero;
}
- if (_contextHandle != IntPtr.Zero)
- {
- Interop.ApplicationManager.AppContextDestroy(_contextHandle);
- _contextHandle = IntPtr.Zero;
- }
_disposed = true;
}
}
/// <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
/// <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;
};
}
/// <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;
};
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);
--- /dev/null
+/*
+ * 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
/// <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