2 * Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 using System.Collections.Generic;
19 using System.ComponentModel;
21 namespace Tizen.Applications
24 /// This class provides methods and properties to get information of the application.
26 /// <since_tizen> 3 </since_tizen>
27 public class ApplicationRunningContext : IDisposable
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;
34 internal ApplicationRunningContext(IntPtr contextHandle)
36 _contextHandle = contextHandle;
40 /// A constructor of ApplicationRunningContext that takes the application ID.
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)
49 IntPtr contextHandle = IntPtr.Zero;
50 err = Interop.ApplicationManager.AppManagerGetAppContext(applicationId, out contextHandle);
51 if (err != Interop.ApplicationManager.ErrorCode.None)
53 Log.Warn(LogTag, "Failed to get the handle of the ApplicationRunningContext. err = " + err);
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");
63 throw new InvalidOperationException("Invalid Operation.");
66 _contextHandle = contextHandle;
70 /// A constructor of ApplicationRunningContext that takes the application id.
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)
81 IntPtr contextHandle = IntPtr.Zero;
82 err = Interop.ApplicationManager.AppManagerGetAppContextByInstanceId(applicationId, instanceId, out contextHandle);
83 if (err != Interop.ApplicationManager.ErrorCode.None)
85 Log.Warn(LogTag, "Failed to get the handle of the ApplicationRunningContext. err = " + err);
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");
95 throw new InvalidOperationException("Invalid Operation.");
98 _contextHandle = contextHandle;
102 /// Destructor of the class.
104 ~ApplicationRunningContext()
110 /// Enumeration for the application state.
112 /// <since_tizen> 3 </since_tizen>
116 /// The undefined state.
121 /// The UI application is running in the foreground.
126 /// The UI application is running in the background.
131 /// The service application is running.
136 /// The application is terminated.
142 /// Gets the application ID.
144 /// <since_tizen> 3 </since_tizen>
145 public string ApplicationId
149 string appid = string.Empty;
150 err = Interop.ApplicationManager.AppContextGetAppId(_contextHandle, out appid);
151 if (err != Interop.ApplicationManager.ErrorCode.None)
153 Log.Warn(LogTag, "Failed to get the application id. err = " + err);
160 /// Gets whether the application is terminated.
162 /// <since_tizen> 6 </since_tizen>
163 public bool IsTerminated
167 bool isRunning = false;
168 string appid = string.Empty;
169 err = Interop.ApplicationManager.AppContextGetAppId(_contextHandle, out appid);
170 if (err != Interop.ApplicationManager.ErrorCode.None)
172 Log.Warn(LogTag, "Failed to get the application id. err = " + err);
176 Interop.ApplicationManager.AppManagerIsRunning(appid, out isRunning);
177 err = Interop.ApplicationManager.AppContextGetAppId(_contextHandle, out appid);
178 if (err != Interop.ApplicationManager.ErrorCode.None)
180 Log.Warn(LogTag, "Failed to get is running. err = " + err);
188 /// Gets the package ID of the application.
190 /// <since_tizen> 3 </since_tizen>
191 public string PackageId
195 string packageid = string.Empty;
196 err = Interop.ApplicationManager.AppContextGetPackageId(_contextHandle, out packageid);
197 if (err != Interop.ApplicationManager.ErrorCode.None)
199 Log.Warn(LogTag, "Failed to get the package id. err = " + err);
206 /// Gets the application's process ID.
208 /// <since_tizen> 3 </since_tizen>
214 err = Interop.ApplicationManager.AppContextGetPid(_contextHandle, out pid);
215 if (err != Interop.ApplicationManager.ErrorCode.None)
217 Log.Warn(LogTag, "Failed to get the process id. err = " + err);
224 /// Gets the state of the application.
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.
229 /// <since_tizen> 3 </since_tizen>
230 public AppState State
236 err = Interop.ApplicationManager.AppContextGetAppState(_contextHandle, out state);
237 if (err != Interop.ApplicationManager.ErrorCode.None)
239 Log.Warn(LogTag, "Failed to get the application state. err = " + err);
241 return (AppState)state;
246 /// Gets whether the application is sub application of the application group.
248 /// <since_tizen> 3 </since_tizen>
254 err = Interop.ApplicationManager.AppContextIsSubApp(_contextHandle, out subapp);
255 if (err != Interop.ApplicationManager.ErrorCode.None)
257 Log.Warn(LogTag, "Failed to get the IsSubApp value. err = " + err);
264 /// Terminates the application.
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()
274 err = Interop.ApplicationManager.AppManagerTerminateApp(_contextHandle);
275 if (err != Interop.ApplicationManager.ErrorCode.None)
279 case Interop.ApplicationManager.ErrorCode.InvalidParameter:
280 throw new ArgumentException("Invalid argument.");
281 case Interop.ApplicationManager.ErrorCode.PermissionDenied:
282 throw new UnauthorizedAccessException("Permission denied.");
284 throw new InvalidOperationException("Invalid Operation.");
290 /// Resumes the running application.
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>
299 err = Interop.ApplicationManager.AppManagerResumeApp(_contextHandle);
300 if (err != Interop.ApplicationManager.ErrorCode.None)
304 case Interop.ApplicationManager.ErrorCode.InvalidParameter:
305 throw new ArgumentException("Invalid argument.");
306 case Interop.ApplicationManager.ErrorCode.PermissionDenied:
307 throw new UnauthorizedAccessException("Permission denied.");
309 throw new InvalidOperationException("Invalid Operation.");
315 /// Releases all resources used by the ApplicationRunningContext class.
317 /// <since_tizen> 3 </since_tizen>
318 public void Dispose()
321 GC.SuppressFinalize(this);
324 private void Dispose(bool disposing)
329 if (_contextHandle != IntPtr.Zero)
331 Interop.ApplicationManager.AppContextDestroy(_contextHandle);
332 _contextHandle = IntPtr.Zero;