internal static extern void AppManagerUnSetAppContextEvent();
//void app_manager_unset_app_context_event_cb (void);
+ [DllImport(Libraries.AppManager, EntryPoint = "app_manager_foreach_running_app_context")]
+ internal static extern ErrorCode AppManagerForeachRunningAppContext(AppManagerAppContextCallback callback, IntPtr userData);
+ //int app_manager_foreach_running_app_context(app_manager_app_context_cb callback, void *user_data)
+
[DllImport(Libraries.AppManager, EntryPoint = "app_manager_foreach_app_context")]
internal static extern ErrorCode AppManagerForeachAppContext(AppManagerAppContextCallback callback, IntPtr userData);
//int app_manager_foreach_app_context(app_manager_app_context_cb callback, void *user_data)
internal static extern ErrorCode AppContextIsEqual(IntPtr first, IntPtr second, out bool equal);
//int app_context_is_equal (app_context_h lhs, app_context_h rhs, bool *equal);
+ [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);
+
[DllImport(Libraries.AppManager, EntryPoint = "app_context_clone")]
internal static extern ErrorCode AppContextClone(out IntPtr destination, IntPtr source);
//int app_context_clone (app_context_h *clone, app_context_h app_context);
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>
get
{
int pid = 0;
- IntPtr contextHandle = IntPtr.Zero;
- try
- {
- err = Interop.ApplicationManager.AppManagerGetAppContext(ApplicationId, out contextHandle);
- if (err != Interop.ApplicationManager.ErrorCode.None)
- {
- Log.Warn(LogTag, "Failed to get the context handle. err = " + err);
- }
- err = Interop.ApplicationManager.AppContextGetPid(contextHandle, out pid);
- if (err != Interop.ApplicationManager.ErrorCode.None)
- {
- Log.Warn(LogTag, "Failed to get the process id. err = " + err);
- }
- }
- finally
+ IntPtr contextHandle = GetContextHandle();
+ err = Interop.ApplicationManager.AppContextGetPid(contextHandle, out pid);
+ if (err != Interop.ApplicationManager.ErrorCode.None)
{
- if (contextHandle != IntPtr.Zero)
- {
- Interop.ApplicationManager.AppContextDestroy(contextHandle);
- }
+ Log.Warn(LogTag, "Failed to get the process id. err = " + err);
}
return pid;
}
get
{
int value = 0;
- IntPtr contextHandle = IntPtr.Zero;
- try
+ IntPtr contextHandle = GetContextHandle();
+ err = Interop.ApplicationManager.AppContextGetAppState(contextHandle, out value);
+ if (err != Interop.ApplicationManager.ErrorCode.None)
{
- err = Interop.ApplicationManager.AppManagerGetAppContext(ApplicationId, out contextHandle);
- if (err != Interop.ApplicationManager.ErrorCode.None)
- {
- Log.Warn(LogTag, "Failed to get the context handle. err = " + err);
- }
- err = Interop.ApplicationManager.AppContextGetAppState(contextHandle, out value);
- if (err != Interop.ApplicationManager.ErrorCode.None)
- {
- Log.Warn(LogTag, "Failed to get the app state. err = " + err);
- }
+ Log.Warn(LogTag, "Failed to get the app state. err = " + err);
}
- finally
+ 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)
{
- if (contextHandle != IntPtr.Zero)
- {
- Interop.ApplicationManager.AppContextDestroy(contextHandle);
- }
+ Log.Warn(LogTag, "Failed to get IsSubApp. err = " + err);
}
- return (AppState)value;
+ 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()
Interop.ApplicationManager.AppInfoDestroy(_infoHandle);
_infoHandle = IntPtr.Zero;
}
+ if (_contextHandle != IntPtr.Zero)
+ {
+ Interop.ApplicationManager.AppContextDestroy(_contextHandle);
+ _contextHandle = IntPtr.Zero;
+ }
_disposed = true;
}
}
Log.Warn(LogTag, "Failed to get appid. err = " + err);
return false;
}
- ApplicationInfo app = GetInstalledApplication(appid);
+ 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);
+ if (err != Interop.ApplicationManager.ErrorCode.None)
+ {
+ Log.Warn(LogTag, "Failed to clone the application context handle");
+ Interop.ApplicationManager.AppInfoDestroy(infoHandle);
+ return false;
+ }
+ ApplicationInfo app = new ApplicationInfo(infoHandle, cloneContextHandle);
if (app != null)
{
result.Add(app);
}
/// <summary>
+ /// Gets the information of all running applications asynchronously.
+ /// </summary>
+ public static async Task<IEnumerable<ApplicationInfo>> GetAllRunningApplicationsAsync()
+ {
+ return await Task.Run(() =>
+ {
+ Interop.ApplicationManager.ErrorCode err = Interop.ApplicationManager.ErrorCode.None;
+ List<ApplicationInfo> result = new List<ApplicationInfo>();
+
+ 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);
+ if (err != Interop.ApplicationManager.ErrorCode.None)
+ {
+ Log.Warn(LogTag, "Failed to clone the application context handle");
+ Interop.ApplicationManager.AppInfoDestroy(infoHandle);
+ return false;
+ }
+ ApplicationInfo app = new ApplicationInfo(infoHandle, cloneContextHandle);
+ if (app != null)
+ {
+ result.Add(app);
+ return true;
+ }
+ }
+ return false;
+ };
+
+ err = Interop.ApplicationManager.AppManagerForeachRunningAppContext(cb, IntPtr.Zero);
+ if (err != Interop.ApplicationManager.ErrorCode.None)
+ {
+ throw ApplicationManagerErrorFactory.GetException(err, "Failed to foreach appcontext.");
+ }
+ return result;
+ });
+ }
+
+ /// <summary>
/// Gets the information of the specified application with the application id.
/// </summary>
/// <param name="applicationId">Application id.</param>