c7196e7f4c4ddf2e15a13d801c3fb0b46da4040e
[platform/core/csapi/tizenfx.git] / src / Tizen.Applications.Common / Tizen.Applications / ApplicationRunningContext.cs
1 /*
2  * Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 using System;
18 using System.Collections.Generic;
19 using System.ComponentModel;
20
21 namespace Tizen.Applications
22 {
23     /// <summary>
24     /// This class provides methods and properties to get information of the application.
25     /// </summary>
26     /// <since_tizen> 3 </since_tizen>
27     public class ApplicationRunningContext : IDisposable
28     {
29         private const string LogTag = "Tizen.Applications";
30         private bool _disposed = false;
31         internal IntPtr _contextHandle = IntPtr.Zero;
32         private Interop.ApplicationManager.ErrorCode err = Interop.ApplicationManager.ErrorCode.None;
33
34         internal ApplicationRunningContext(IntPtr contextHandle)
35         {
36             _contextHandle = contextHandle;
37         }
38
39         /// <summary>
40         /// A constructor of ApplicationRunningContext that takes the application ID.
41         /// </summary>
42         /// <param name="applicationId">Application ID.</param>
43         /// <exception cref="ArgumentException">Thrown when failed because of an invalid argument.</exception>
44         /// <exception cref="InvalidOperationException">Thrown when failed because of the "application not exist" error or the system error.</exception>
45         /// <exception cref="OutOfMemoryException">Thrown when failed because of out of memory.</exception>
46         /// <since_tizen> 3 </since_tizen>
47         public ApplicationRunningContext(string applicationId)
48         {
49             IntPtr contextHandle = IntPtr.Zero;
50             err = Interop.ApplicationManager.AppManagerGetAppContext(applicationId, out contextHandle);
51             if (err != Interop.ApplicationManager.ErrorCode.None)
52             {
53                 Log.Warn(LogTag, "Failed to get the handle of the ApplicationRunningContext. err = " + err);
54                 switch (err)
55                 {
56                     case Interop.ApplicationManager.ErrorCode.InvalidParameter:
57                         throw new ArgumentException("Invalid Parameter.");
58                     case Interop.ApplicationManager.ErrorCode.NoSuchApp:
59                         throw new InvalidOperationException("No such application.");
60                     case Interop.ApplicationManager.ErrorCode.OutOfMemory:
61                         throw new OutOfMemoryException("Out of memory");
62                     default:
63                         throw new InvalidOperationException("Invalid Operation.");
64                 }
65             }
66             _contextHandle = contextHandle;
67         }
68
69         /// <summary>
70         /// A constructor of ApplicationRunningContext that takes the application id.
71         /// </summary>
72         /// <param name="applicationId">application id.</param>
73         /// <param name="instanceId">instance id.</param>
74         /// <exception cref="ArgumentException">Thrown when failed of invalid argument.</exception>
75         /// <exception cref="InvalidOperationException">Thrown when failed because of application not exist error or system error.</exception>
76         /// <exception cref="OutOfMemoryException">Thrown when failed because of out of memory.</exception>
77         /// <since_tizen> 4 </since_tizen>
78         [EditorBrowsable(EditorBrowsableState.Never)]
79         public ApplicationRunningContext(string applicationId, string instanceId)
80         {
81             IntPtr contextHandle = IntPtr.Zero;
82             err = Interop.ApplicationManager.AppManagerGetAppContextByInstanceId(applicationId, instanceId, out contextHandle);
83             if (err != Interop.ApplicationManager.ErrorCode.None)
84             {
85                 Log.Warn(LogTag, "Failed to get the handle of the ApplicationRunningContext. err = " + err);
86                 switch (err)
87                 {
88                     case Interop.ApplicationManager.ErrorCode.InvalidParameter:
89                         throw new ArgumentException("Invalid Parameter.");
90                     case Interop.ApplicationManager.ErrorCode.NoSuchApp:
91                         throw new InvalidOperationException("No such application.");
92                     case Interop.ApplicationManager.ErrorCode.OutOfMemory:
93                         throw new OutOfMemoryException("Out of memory");
94                     default:
95                         throw new InvalidOperationException("Invalid Operation.");
96                 }
97             }
98             _contextHandle = contextHandle;
99         }
100
101         /// <summary>
102         /// Destructor of the class.
103         /// </summary>
104         ~ApplicationRunningContext()
105         {
106             Dispose(false);
107         }
108
109         /// <summary>
110         /// Enumeration for the application state.
111         /// </summary>
112         /// <since_tizen> 3 </since_tizen>
113         public enum AppState
114         {
115             /// <summary>
116             /// The undefined state.
117             /// </summary>
118             Undefined = 0,
119
120             /// <summary>
121             /// The UI application is running in the foreground.
122             /// </summary>
123             Foreground,
124
125             /// <summary>
126             /// The UI application is running in the background.
127             /// </summary>
128             Background,
129
130             /// <summary>
131             /// The service application is running.
132             /// </summary>
133             Service,
134
135             /// <summary>
136             /// The application is terminated.
137             /// </summary>
138             Terminated,
139         }
140
141         /// <summary>
142         /// Gets the application ID.
143         /// </summary>
144         /// <since_tizen> 3 </since_tizen>
145         public string ApplicationId
146         {
147             get
148             {
149                 string appid = string.Empty;
150                 err = Interop.ApplicationManager.AppContextGetAppId(_contextHandle, out appid);
151                 if (err != Interop.ApplicationManager.ErrorCode.None)
152                 {
153                     Log.Warn(LogTag, "Failed to get the application id. err = " + err);
154                 }
155                 return appid;
156             }
157         }
158
159         /// <summary>
160         /// Gets whether the application is terminated.
161         /// </summary>
162         /// <since_tizen> 6 </since_tizen>
163         public bool IsTerminated
164         {
165             get
166             {
167                 bool isRunning = false;
168                 string appid = string.Empty;
169                 err = Interop.ApplicationManager.AppContextGetAppId(_contextHandle, out appid);
170                 if (err != Interop.ApplicationManager.ErrorCode.None)
171                 {
172                     Log.Warn(LogTag, "Failed to get the application id. err = " + err);
173                 }
174                 else
175                 {
176                     Interop.ApplicationManager.AppManagerIsRunning(appid, out isRunning);
177                     err = Interop.ApplicationManager.AppContextGetAppId(_contextHandle, out appid);
178                     if (err != Interop.ApplicationManager.ErrorCode.None)
179                     {
180                         Log.Warn(LogTag, "Failed to get is running. err = " + err);
181                     }
182                 }
183                 return !isRunning;
184             }
185         }
186
187         /// <summary>
188         /// Gets the package ID of the application.
189         /// </summary>
190         /// <since_tizen> 3 </since_tizen>
191         public string PackageId
192         {
193             get
194             {
195                 string packageid = string.Empty;
196                 err = Interop.ApplicationManager.AppContextGetPackageId(_contextHandle, out packageid);
197                 if (err != Interop.ApplicationManager.ErrorCode.None)
198                 {
199                     Log.Warn(LogTag, "Failed to get the package id. err = " + err);
200                 }
201                 return packageid;
202             }
203         }
204
205         /// <summary>
206         /// Gets the application's process ID.
207         /// </summary>
208         /// <since_tizen> 3 </since_tizen>
209         public int ProcessId
210         {
211             get
212             {
213                 int pid = 0;
214                 err = Interop.ApplicationManager.AppContextGetPid(_contextHandle, out pid);
215                 if (err != Interop.ApplicationManager.ErrorCode.None)
216                 {
217                     Log.Warn(LogTag, "Failed to get the process id. err = " + err);
218                 }
219                 return pid;
220             }
221         }
222
223         /// <summary>
224         /// Gets the state of the application.
225         /// </summary>
226         /// <remarks>
227         /// Note that application's state might be changed after you get app_context. This API just returns the state of application when you get the app_context.
228         /// </remarks>
229         /// <since_tizen> 3 </since_tizen>
230         public AppState State
231         {
232             get
233             {
234                 int state = 0;
235
236                 err = Interop.ApplicationManager.AppContextGetAppState(_contextHandle, out state);
237                 if (err != Interop.ApplicationManager.ErrorCode.None)
238                 {
239                     Log.Warn(LogTag, "Failed to get the application state. err = " + err);
240                 }
241                 return (AppState)state;
242             }
243         }
244
245         /// <summary>
246         /// Gets whether the application is sub application of the application group.
247         /// </summary>
248         /// <since_tizen> 3 </since_tizen>
249         public bool IsSubApp
250         {
251             get
252             {
253                 bool subapp = false;
254                 err = Interop.ApplicationManager.AppContextIsSubApp(_contextHandle, out subapp);
255                 if (err != Interop.ApplicationManager.ErrorCode.None)
256                 {
257                     Log.Warn(LogTag, "Failed to get the IsSubApp value. err = " + err);
258                 }
259                 return subapp;
260             }
261         }
262
263         /// <summary>
264         /// Terminates the application.
265         /// </summary>
266         /// <exception cref="ArgumentException">Thrown when failed of invalid argument.</exception>
267         /// <exception cref="UnauthorizedAccessException">Thrown when failed because of permission denied.</exception>
268         /// <exception cref="InvalidOperationException">Thrown when failed because of system error.</exception>
269         /// <privilege>http://tizen.org/privilege/appmanager.kill</privilege>
270         /// <since_tizen> 4 </since_tizen>
271         [EditorBrowsable(EditorBrowsableState.Never)]
272         public void Terminate()
273         {
274             err = Interop.ApplicationManager.AppManagerTerminateApp(_contextHandle);
275             if (err != Interop.ApplicationManager.ErrorCode.None)
276             {
277                 switch (err)
278                 {
279                     case Interop.ApplicationManager.ErrorCode.InvalidParameter:
280                         throw new ArgumentException("Invalid argument.");
281                     case Interop.ApplicationManager.ErrorCode.PermissionDenied:
282                         throw new UnauthorizedAccessException("Permission denied.");
283                     default:
284                         throw new InvalidOperationException("Invalid Operation.");
285                 }
286             }
287         }
288
289         /// <summary>
290         /// Resumes the running application.
291         /// </summary>
292         /// <exception cref="ArgumentException">Thrown when failed of invalid argument.</exception>
293         /// <exception cref="UnauthorizedAccessException">Thrown when failed because of permission denied.</exception>
294         /// <exception cref="InvalidOperationException">Thrown when failed because of system error.</exception>
295         /// <privilege>http://tizen.org/privilege/appmanager.launch</privilege>
296         /// <since_tizen> 4 </since_tizen>
297         public void Resume()
298         {
299             err = Interop.ApplicationManager.AppManagerResumeApp(_contextHandle);
300             if (err != Interop.ApplicationManager.ErrorCode.None)
301             {
302                 switch (err)
303                 {
304                     case Interop.ApplicationManager.ErrorCode.InvalidParameter:
305                         throw new ArgumentException("Invalid argument.");
306                     case Interop.ApplicationManager.ErrorCode.PermissionDenied:
307                         throw new UnauthorizedAccessException("Permission denied.");
308                     default:
309                         throw new InvalidOperationException("Invalid Operation.");
310                 }
311             }
312         }
313
314         /// <summary>
315         /// Releases all resources used by the ApplicationRunningContext class.
316         /// </summary>
317         /// <since_tizen> 3 </since_tizen>
318         public void Dispose()
319         {
320             Dispose(true);
321             GC.SuppressFinalize(this);
322         }
323
324         private void Dispose(bool disposing)
325         {
326             if (_disposed)
327                 return;
328
329             if (_contextHandle != IntPtr.Zero)
330             {
331                 Interop.ApplicationManager.AppContextDestroy(_contextHandle);
332                 _contextHandle = IntPtr.Zero;
333             }
334             _disposed = true;
335         }
336     }
337 }