1 /// Copyright 2016 by Samsung Electronics, Inc.,
3 /// This software is the confidential and proprietary information
4 /// of Samsung Electronics, Inc. ("Confidential Information"). You
5 /// shall not disclose such Confidential Information and shall use
6 /// it only in accordance with the terms of the license agreement
7 /// you entered into with Samsung.
11 using System.Collections.Generic;
12 using System.Threading.Tasks;
13 using System.Runtime.InteropServices;
15 namespace Tizen.Applications.Managers
17 internal class ApplicationManagerImpl : IDisposable
19 private static ApplicationManagerImpl s_instance = new ApplicationManagerImpl();
21 private bool _disposed = false;
22 private Interop.ApplicationManager.AppManagerAppContextEventCallback _applicationChangedEventCallback;
24 private const string LogTag = "Tizen.Applications.Managers";
26 private ApplicationManagerImpl()
28 Log.Debug(LogTag, "ApplicationManagerImpl()");
29 RegisterApplicationChangedEvent();
33 ~ApplicationManagerImpl()
35 Log.Debug(LogTag, "~ApplicationManagerImpl()");
36 UnRegisterApplicationChangedEvent();
40 internal event EventHandler<ApplicationChangedEventArgs> ApplicationLaunched;
41 internal event EventHandler<ApplicationChangedEventArgs> ApplicationTerminated;
43 internal static ApplicationManagerImpl Instance
54 GC.SuppressFinalize(this);
57 private void Dispose(bool disposing)
64 // Free managed objects.
66 //Free unmanaged objects
70 internal async Task<IEnumerable<InstalledApplication>> GetInstalledAppsAsync()
72 Log.Debug(LogTag, "GetInstalledAppsAsync()");
73 return await Task.Run(() =>
75 List<InstalledApplication> Result = new List<InstalledApplication>();
77 Interop.ApplicationManager.AppManagerAppInfoCallback cb = (IntPtr handle, IntPtr userData) =>
79 if (handle != IntPtr.Zero)
81 IntPtr clonedHandle = IntPtr.Zero;
82 int result = Interop.ApplicationManager.AppInfoClone(out clonedHandle, handle);
87 InstalledApplication app = new InstalledApplication(clonedHandle);
93 int ret = Interop.ApplicationManager.AppManagerForeachAppInfo(cb, IntPtr.Zero);
96 ApplicationManagerErrorFactory.ExceptionChecker(ret, "GetInstalledAppsAsync() failed.");
102 internal async Task<IEnumerable<InstalledApplication>> GetInstalledAppsAsync(InstalledApplicationFilter filter)
104 Log.Debug(LogTag, "GetInstalledAppsAsync(InstalledApplicationFilter filter)");
105 return await Task.Run(() =>
107 List<InstalledApplication> Result = new List<InstalledApplication>();
109 Interop.ApplicationManager.AppInfoFilterCallback cb = (IntPtr handle, IntPtr userData) =>
111 Log.Debug(LogTag, "AppInfoFilterCallback");
112 if (handle != IntPtr.Zero)
114 IntPtr clonedHandle = IntPtr.Zero;
115 int ret = Interop.ApplicationManager.AppInfoClone(out clonedHandle, handle);
120 InstalledApplication app = new InstalledApplication(clonedHandle);
130 internal async Task<IEnumerable<RunningApplication>> GetRunningAppsAsync()
132 Log.Debug(LogTag, "GetRunningAppsAsync()");
134 return await Task.Run(() =>
136 List<RunningApplication> Result = new List<RunningApplication>();
138 Interop.ApplicationManager.AppManagerAppContextCallback cb = (IntPtr handle, IntPtr userData) =>
140 Log.Debug(LogTag, "AppManagerAppContextCallback");
141 if (handle != IntPtr.Zero)
143 IntPtr ptr = IntPtr.Zero;
144 int result = Interop.ApplicationManager.AppContextGetAppId(handle, out ptr);
149 string appid = Marshal.PtrToStringAuto(ptr);
151 result = Interop.ApplicationManager.AppContextGetPid(handle, out pid);
156 RunningApplication app = new RunningApplication(appid, pid);
163 int ret = Interop.ApplicationManager.AppManagerForeachAppContext(cb, IntPtr.Zero);
166 ApplicationManagerErrorFactory.ExceptionChecker(ret, "GetRunningAppsAsync() failed.");
172 internal InstalledApplication GetInstalledApp(string applicationId)
174 Log.Debug(LogTag, "GetInstalledApp(applicationId)");
175 IntPtr handle = IntPtr.Zero;
176 int ret = Interop.ApplicationManager.AppManagerGetAppInfo(applicationId, out handle);
179 ApplicationManagerErrorFactory.ExceptionChecker(ret, "GetInstalledApp(applicationId) failed.");
181 if (handle != IntPtr.Zero)
183 InstalledApplication app = new InstalledApplication(handle);
189 internal RunningApplication GetRunningApp(string applicationId)
191 Log.Debug(LogTag, "GetRunningApp(applicationId)");
192 IntPtr handle = IntPtr.Zero;
193 int ret = Interop.ApplicationManager.AppManagerGetAppContext(applicationId, out handle);
196 ApplicationManagerErrorFactory.ExceptionChecker(ret, "GetRunningApp(applicationId) failed.");
198 if (handle != IntPtr.Zero)
201 ret = Interop.ApplicationManager.AppContextGetPid(handle, out pid);
204 ApplicationManagerErrorFactory.ExceptionChecker(ret, "GetRunningApp(applicationId) failed.");
206 RunningApplication app = new RunningApplication(applicationId, pid);
212 internal RunningApplication GetRunningApp(int processId)
214 Log.Debug(LogTag, "GetRunningApp(processId)");
216 int ret = Interop.ApplicationManager.AppManagerGetAppId(processId, out appid);
219 ApplicationManagerErrorFactory.ExceptionChecker(ret, "GetRunningApp(processId) failed.");
221 RunningApplication app = new RunningApplication(appid, processId);
225 internal bool IsRunningApp(string applicationId)
227 Log.Debug(LogTag, "IsRunningApp(applicationId)");
228 bool running = false;
229 int ret = Interop.ApplicationManager.AppManagerIsRunning(applicationId, out running);
232 Log.Warn(LogTag, "IsRunningApp(applicationId) failed.");
237 internal bool IsRunningApp(int processId)
239 Log.Debug(LogTag, "IsRunningApp(processId)");
241 int ret = Interop.ApplicationManager.AppManagerGetAppId(processId, out appid);
244 ApplicationManagerErrorFactory.ExceptionChecker(ret, "IsRunningApp(processId) failed.");
246 bool running = false;
247 ret = Interop.ApplicationManager.AppManagerIsRunning(appid, out running);
250 Log.Warn(LogTag, "IsRunningApp(processId) failed.");
255 private void RegisterApplicationChangedEvent()
257 Log.Debug(LogTag, "RegisterApplicationChangedEvent()");
258 _applicationChangedEventCallback = (IntPtr handle, int state, IntPtr userData) =>
260 Log.Debug(LogTag, "ApplicationChangedEventCallback");
261 if (handle == IntPtr.Zero) return;
263 IntPtr ptr = IntPtr.Zero;
264 int result = Interop.ApplicationManager.AppContextGetAppId(handle, out ptr);
267 ApplicationManagerErrorFactory.ExceptionChecker(result, "RegisterApplicationChangedEvent() failed.");
269 string appid = Marshal.PtrToStringAuto(ptr);
271 result = Interop.ApplicationManager.AppContextGetPid(handle, out pid);
274 ApplicationManagerErrorFactory.ExceptionChecker(result, "RegisterApplicationChangedEvent() failed.");
278 var launchedEventCache = ApplicationLaunched;
279 if (launchedEventCache != null)
281 Log.Debug(LogTag, "Raise up ApplicationLaunched");
282 ApplicationChangedEventArgs e = new ApplicationChangedEventArgs(appid, pid, state);
283 launchedEventCache(null, e);
288 var terminatedEventCache = ApplicationTerminated;
289 if (terminatedEventCache != null)
291 Log.Debug(LogTag, "Raise up ApplicationTerminated");
292 ApplicationChangedEventArgs e = new ApplicationChangedEventArgs(appid, pid, state);
293 terminatedEventCache(null, e);
297 int ret = Interop.ApplicationManager.AppManagerSetAppContextEvent(_applicationChangedEventCallback, IntPtr.Zero);
300 ApplicationManagerErrorFactory.ExceptionChecker(ret, "RegisterApplicationChangedEvent() register failed.");
304 private void UnRegisterApplicationChangedEvent()
306 Log.Debug(LogTag, "UnRegisterApplicationChangedEvent()");
307 Interop.ApplicationManager.AppManagerUnSetAppContextEvent();
310 internal static class FilterExtension
312 private const string LogTag = "Tizen.Applications.Managers";
313 internal static void Fetch(this InstalledApplicationFilter filter, Interop.ApplicationManager.AppInfoFilterCallback callback)
315 if (filter is InstalledApplicationMetadataFilter)
317 InstalledApplicationMetadataFilter metaFilter = (InstalledApplicationMetadataFilter)filter;
318 metaFilter.Fetch(callback);
322 IntPtr nativeHandle = MakeNativeAppInfoFilter(filter.Filter);
323 int ret = Interop.ApplicationManager.AppInfoFilterForeachAppinfo(nativeHandle, callback, IntPtr.Zero);
324 if (nativeHandle != IntPtr.Zero)
325 Interop.ApplicationManager.AppInfoFilterDestroy(nativeHandle);
328 ApplicationManagerErrorFactory.ExceptionChecker(ret, "GetInstalledAppsAsync(InstalledApplicationFilter) failed.");
332 internal static void Fetch(this InstalledApplicationMetadataFilter filter, Interop.ApplicationManager.AppInfoFilterCallback callback)
334 IntPtr nativeHandle = MakeNativeAppMetadataFilter(filter.Filter);
335 int ret = Interop.ApplicationManager.AppInfoMetadataFilterForeach(nativeHandle, callback, IntPtr.Zero);
336 if (nativeHandle != IntPtr.Zero)
337 Interop.ApplicationManager.AppInfoMetadataFilterDestroy(nativeHandle);
340 ApplicationManagerErrorFactory.ExceptionChecker(ret, "GetInstalledAppsAsync(InstalledApplicationMetadataFilter) failed.");
345 private static IntPtr MakeNativeAppInfoFilter(IDictionary<string, string> filter)
348 throw new ArgumentException("Filter dose not added");
350 int ret = Interop.ApplicationManager.AppInfoFilterCreate(out handle);
353 ApplicationManagerErrorFactory.ExceptionChecker(ret, "AppInfoFilter creation failed.");
356 foreach (var item in filter)
358 if ((item.Key == InstalledApplicationFilter.Keys.Id) ||
359 (item.Key == InstalledApplicationFilter.Keys.Type) ||
360 (item.Key == InstalledApplicationFilter.Keys.Category))
362 ret = Interop.ApplicationManager.AppInfoFilterAddString(handle, item.Key, item.Value);
364 else if ((item.Key == InstalledApplicationFilter.Keys.NoDisplay) ||
365 (item.Key == InstalledApplicationFilter.Keys.TaskManage))
367 ret = Interop.ApplicationManager.AppInfoFilterAddBool(handle, item.Key, Convert.ToBoolean(item.Value));
371 Log.Warn(LogTag, "InstalledApplicationFilter is NOT supported " + item.Key + " key.");
375 Interop.ApplicationManager.AppInfoFilterDestroy(handle);
376 ApplicationManagerErrorFactory.ExceptionChecker(ret, "InstalledApplicationFilter item add failed.");
382 private static IntPtr MakeNativeAppMetadataFilter(IDictionary<string, string> filter)
385 throw new ArgumentException("Filter dose not added");
387 int ret = Interop.ApplicationManager.AppInfoMetadataFilterCreate(out handle);
390 ApplicationManagerErrorFactory.ExceptionChecker(ret, "InstalledApplicationMetadataFilter creation failed.");
392 foreach (var item in filter)
394 ret = Interop.ApplicationManager.AppInfoMetadataFilterAdd(handle, item.Key, item.Value);
397 Interop.ApplicationManager.AppInfoMetadataFilterDestroy(handle);
398 ApplicationManagerErrorFactory.ExceptionChecker(ret, "InstalledApplicationMetadataFilter item add failed.");