Refactor AppFW
[platform/core/csapi/tizenfx.git] / Tizen.Applications / Tizen.Applications.Manager / ApplicationManagerImpl.cs
1 /// Copyright 2016 by Samsung Electronics, Inc.,
2 ///
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.
8
9 using System;
10 using System.Collections.Generic;
11 using System.Threading.Tasks;
12 using System.Runtime.InteropServices;
13
14 namespace Tizen.Applications.Manager
15 {
16     internal class ApplicationManagerImpl : IDisposable
17     {
18         private static ApplicationManagerImpl _instance = new ApplicationManagerImpl();
19
20         private bool _disposed = false;
21         private Interop.ApplicationManager.AppManagerAppContextEventCallback _applicationChangedEventCallback;
22
23         private ApplicationManagerImpl()
24         {
25             Console.WriteLine("ApplicationManagerImpl()");
26             RegisterApplicationChangedEvent();
27         }
28
29
30         ~ApplicationManagerImpl()
31         {
32             Console.WriteLine("~ApplicationManagerImpl()");
33             UnRegisterApplicationChangedEvent();
34             Dispose(false);
35         }
36
37         internal event EventHandler<ApplicationChangedEventArgs> ApplicationLaunched;
38         internal event EventHandler<ApplicationChangedEventArgs> ApplicationTerminated;        
39
40         internal static ApplicationManagerImpl Instance
41         {
42             get
43             {
44                 return _instance;
45             }
46         }
47
48         public void Dispose()
49         {
50             Dispose(true);
51             GC.SuppressFinalize(this);
52         }
53
54         private void Dispose(bool disposing)
55         {
56             if (_disposed)
57                 return;
58
59             if (disposing)
60             {
61                 // Free managed objects.
62             }
63             //Free unmanaged objects
64             _disposed = true;
65         }
66
67         internal async Task<IEnumerable<InstalledApplication>> GetInstalledAppsAsync()
68         {
69             Console.WriteLine("GetInstalledAppsAsync()");
70             return await Task.Run(() =>
71             {
72                 List<InstalledApplication> Result = new List<InstalledApplication>();
73
74                 Interop.ApplicationManager.AppManagerAppInfoCallback cb = (IntPtr handle, IntPtr userData) =>
75                 {
76                     if (handle != IntPtr.Zero)
77                     {
78                         IntPtr clonedHandle;
79                         Interop.ApplicationManager.AppInfoClone(out clonedHandle, handle);
80                         InstalledApplication app = new InstalledApplication(clonedHandle);
81                         Result.Add(app);
82                         return true;
83                     }
84                     return false;
85                 };
86                 Interop.ApplicationManager.AppManagerForeachAppInfo(cb, IntPtr.Zero);
87                 return Result;
88             });
89         }
90
91         internal async Task<IEnumerable<InstalledApplication>> GetInstalledAppsAsync(InstalledApplicationFilter filter)
92         {
93             Console.WriteLine("GetInstalledAppsAsync(InstalledApplicationFilter filter)");
94             return await Task.Run(() =>
95             {
96                 List<InstalledApplication> Result = new List<InstalledApplication>();
97
98                 Interop.ApplicationManager.AppInfoFilterCallback cb = (IntPtr handle, IntPtr userData) =>
99                 {
100                     Console.WriteLine("AppInfoFilterCallback");
101                     if (handle != IntPtr.Zero)
102                     {
103                         IntPtr clonedHandle;
104                         Interop.ApplicationManager.AppInfoClone(out clonedHandle, handle);
105                         InstalledApplication app = new InstalledApplication(clonedHandle);
106                         Result.Add(app);
107                         return true;
108                     }
109                     return false;
110                 };
111
112                 Interop.ApplicationManager.AppInfoFilterForeachAppinfo(filter.Handle, cb, IntPtr.Zero);
113                 return Result;
114             });
115         }
116
117         internal async Task<IEnumerable<InstalledApplication>> GetInstalledAppsAsync(InstalledApplicationMetadataFilter filter)
118         {
119             Console.WriteLine("GetInstalledAppsAsync(InstalledApplicationMetadataFilter filter)");
120
121             return await Task.Run(() =>
122             {
123                 List<InstalledApplication> Result = new List<InstalledApplication>();
124
125                 Interop.ApplicationManager.AppInfoFilterCallback cb = (IntPtr handle, IntPtr userData) =>
126                 {
127                     Console.WriteLine("AppInfoFilterCallback");
128                     if (handle != IntPtr.Zero)
129                     {
130                         IntPtr clonedHandle;
131                         Interop.ApplicationManager.AppInfoClone(out clonedHandle, handle);
132                         InstalledApplication app = new InstalledApplication(clonedHandle);
133                         Result.Add(app);
134                         return true;
135                     }
136                     return false;
137                 };
138
139                 Interop.ApplicationManager.AppInfoMetadataFilterForeach(filter.Handle, cb, IntPtr.Zero);
140                 return Result;
141             });
142         }
143
144         internal async Task<IEnumerable<RunningApplication>> GetRunningAppsAsync()
145         {
146             Console.WriteLine("GetRunningAppsAsync()");
147
148             return await Task.Run(() =>
149             {
150                 List<RunningApplication> Result = new List<RunningApplication>();
151
152                 Interop.ApplicationManager.AppManagerAppContextCallback cb = (IntPtr handle, IntPtr userData) =>
153                 {
154                     Console.WriteLine("AppManagerAppContextCallback");
155                     if (handle != IntPtr.Zero)
156                     {
157                         IntPtr ptr = IntPtr.Zero;
158                         Interop.ApplicationManager.AppContextGetAppId(handle, out ptr);
159                         string appid = Marshal.PtrToStringAuto(ptr);
160                         int pid = 0;
161                         Interop.ApplicationManager.AppContextGetPid(handle, out pid);
162                         RunningApplication app = new RunningApplication(appid, pid);
163                         Result.Add(app);
164                         return true;
165                     }
166                     return false;
167                 };
168
169                 Interop.ApplicationManager.AppManagerForeachAppContext(cb, IntPtr.Zero);
170                 return Result;
171             });
172         }
173
174         internal InstalledApplication GetInstalledApp(string applicationId)
175         {
176             Console.WriteLine("GetInstalledApp(appid)");
177             IntPtr info = IntPtr.Zero;
178             Interop.ApplicationManager.AppManagerGetAppInfo(applicationId, out info);
179             if (info != IntPtr.Zero)
180             {
181                 InstalledApplication app = new InstalledApplication(info);
182                 return app;
183             }
184             return null;
185         }
186
187         internal RunningApplication GetRunningApp(string applicationId)
188         {
189             Console.WriteLine("GetRunningApp(appid)");
190             IntPtr context = IntPtr.Zero;
191             Interop.ApplicationManager.AppManagerGetAppContext(applicationId, out context);
192
193             if (context != IntPtr.Zero)
194             {
195                 int pid = 0;
196                 Interop.ApplicationManager.AppContextGetPid(context, out pid);
197                 Interop.ApplicationManager.AppContextDestroy(context);
198                 RunningApplication app = new RunningApplication(applicationId, pid);
199                 return app;
200             }
201             return null;
202         }
203
204         internal RunningApplication GetRunningApp(int processId)
205         {
206             Console.WriteLine("GetRunningApp(pid)");
207             string appid = "";
208             Interop.ApplicationManager.AppManagerGetAppId(processId, out appid);
209             RunningApplication app = new RunningApplication(appid, processId);
210             return app;
211         }
212
213         internal bool IsRunningApp(string applicationId)
214         {
215             Console.WriteLine("IsRunningApp(appid)");
216             bool running = false;
217             Interop.ApplicationManager.AppManagerIsRunning(applicationId, out running);
218             return running;
219         }
220
221         internal bool IsRunningApp(int processId)
222         {
223             Console.WriteLine("IsRunningApp(pid)");
224             string appid = "";
225             Interop.ApplicationManager.AppManagerGetAppId(processId, out appid);
226             bool running = false;
227             Interop.ApplicationManager.AppManagerIsRunning(appid, out running);
228             return running;
229         }
230
231         private void RegisterApplicationChangedEvent()
232         {
233             Console.WriteLine("RegisterApplicationChangedEvent()");
234             _applicationChangedEventCallback = (IntPtr context, int state, IntPtr userData) =>
235             {
236                 Console.WriteLine("ApplicationChangedEventCallback");
237                 if (context == IntPtr.Zero) return;
238
239                 IntPtr ptr = IntPtr.Zero;
240                 Interop.ApplicationManager.AppContextGetAppId(context, out ptr);
241                 string appid = Marshal.PtrToStringAuto(ptr);
242                 int pid = 0;
243                 Interop.ApplicationManager.AppContextGetPid(context, out pid);
244
245                 if (state == 0)
246                 {
247                     var launchedEventCache = ApplicationLaunched;
248                     if (launchedEventCache != null)
249                     {
250                         Console.WriteLine("Raise up ApplicationLaunched");
251                         ApplicationChangedEventArgs e = new ApplicationChangedEventArgs(appid, pid, state);
252                         launchedEventCache(null, e);
253                     }
254                 }
255                 else if (state == 1)
256                 {
257                     var terminatedEventCache = ApplicationTerminated;
258                     if (terminatedEventCache != null)
259                     {
260                         Console.WriteLine("Raise up ApplicationTerminated");
261                         ApplicationChangedEventArgs e = new ApplicationChangedEventArgs(appid, pid, state);
262                         terminatedEventCache(null, e);
263                     }
264                 }
265             };
266
267             Interop.ApplicationManager.AppManagerSetAppContextEvent(_applicationChangedEventCallback, IntPtr.Zero);
268         }
269
270         private void UnRegisterApplicationChangedEvent()
271         {
272             Console.WriteLine("UnRegisterApplicationChangedEvent()");
273             Interop.ApplicationManager.AppManagerUnSetAppContextEvent();
274         }
275     }
276 }