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 public class ApplicationRunningContext : IDisposable
28 private const string LogTag = "Tizen.Applications";
29 private bool _disposed = false;
30 private IntPtr _contextHandle = IntPtr.Zero;
31 private Interop.ApplicationManager.ErrorCode err = Interop.ApplicationManager.ErrorCode.None;
33 internal ApplicationRunningContext(IntPtr contextHandle)
35 _contextHandle = contextHandle;
39 /// A constructor of ApplicationRunningContext that takes the application id.
41 /// <param name="applicationId">application id.</param>
42 /// <exception cref="ArgumentException">Thrown when failed of invalid argument.</exception>
43 /// <exception cref="InvalidOperationException">Thrown when failed because of application not exist error or system error.</exception>
44 /// <exception cref="OutOfMemoryException">Thrown when failed because of out of memory.</exception>
45 public ApplicationRunningContext(string applicationId)
47 IntPtr contextHandle = IntPtr.Zero;
48 err = Interop.ApplicationManager.AppManagerGetAppContext(applicationId, out contextHandle);
49 if (err != Interop.ApplicationManager.ErrorCode.None)
51 Log.Warn(LogTag, "Failed to get the handle of the ApplicationRunningContext. err = " + err);
54 case Interop.ApplicationManager.ErrorCode.InvalidParameter:
55 throw new ArgumentException("Invalid Parameter.");
56 case Interop.ApplicationManager.ErrorCode.NoSuchApp:
57 throw new InvalidOperationException("No such application.");
58 case Interop.ApplicationManager.ErrorCode.OutOfMemory:
59 throw new OutOfMemoryException("Out of memory");
61 throw new InvalidOperationException("Invalid Operation.");
64 _contextHandle = contextHandle;
68 /// A constructor of ApplicationRunningContext that takes the application id.
70 /// <param name="applicationId">application id.</param>
71 /// <param name="instanceId">instance id.</param>
72 /// <exception cref="ArgumentException">Thrown when failed of invalid argument.</exception>
73 /// <exception cref="InvalidOperationException">Thrown when failed because of application not exist error or system error.</exception>
74 /// <exception cref="OutOfMemoryException">Thrown when failed because of out of memory.</exception>
75 [EditorBrowsable(EditorBrowsableState.Never)]
76 public ApplicationRunningContext(string applicationId, string instanceId)
78 IntPtr contextHandle = IntPtr.Zero;
79 err = Interop.ApplicationManager.AppManagerGetAppContextByInstanceId(applicationId, instanceId, out contextHandle);
80 if (err != Interop.ApplicationManager.ErrorCode.None)
82 Log.Warn(LogTag, "Failed to get the handle of the ApplicationRunningContext. err = " + err);
85 case Interop.ApplicationManager.ErrorCode.InvalidParameter:
86 throw new ArgumentException("Invalid Parameter.");
87 case Interop.ApplicationManager.ErrorCode.NoSuchApp:
88 throw new InvalidOperationException("No such application.");
89 case Interop.ApplicationManager.ErrorCode.OutOfMemory:
90 throw new OutOfMemoryException("Out of memory");
92 throw new InvalidOperationException("Invalid Operation.");
95 _contextHandle = contextHandle;
99 /// Destructor of the class
101 ~ApplicationRunningContext()
107 /// Enumeration for the Application State.
112 /// The undefined state
117 /// The UI application is running in the foreground.
122 /// The UI application is running in the background.
127 /// The Service application is running.
132 /// The application is terminated.
138 /// Gets the application id.
140 public string ApplicationId
144 string appid = string.Empty;
145 err = Interop.ApplicationManager.AppContextGetAppId(_contextHandle, out appid);
146 if (err != Interop.ApplicationManager.ErrorCode.None)
148 Log.Warn(LogTag, "Failed to get the application id. err = " + err);
155 /// Gets the package id of the application.
157 public string PackageId
161 string packageid = string.Empty;
162 err = Interop.ApplicationManager.AppContextGetPackageId(_contextHandle, out packageid);
163 if (err != Interop.ApplicationManager.ErrorCode.None)
165 Log.Warn(LogTag, "Failed to get the package id. err = " + err);
172 /// Gets the application's process id.
179 err = Interop.ApplicationManager.AppContextGetPid(_contextHandle, out pid);
180 if (err != Interop.ApplicationManager.ErrorCode.None)
182 Log.Warn(LogTag, "Failed to get the process id. err = " + err);
189 /// Gets the state of the application.
191 public AppState State
197 err = Interop.ApplicationManager.AppContextGetAppState(_contextHandle, out state);
198 if (err != Interop.ApplicationManager.ErrorCode.None)
200 Log.Warn(LogTag, "Failed to get the application state. err = " + err);
202 return (AppState)state;
207 /// Gets whether the application is sub application of the application group.
214 err = Interop.ApplicationManager.AppContextIsSubApp(_contextHandle, out subapp);
215 if (err != Interop.ApplicationManager.ErrorCode.None)
217 Log.Warn(LogTag, "Failed to get the IsSubApp value. err = " + err);
224 /// Terminates the application.
226 /// <exception cref="ArgumentException">Thrown when failed of invalid argument.</exception>
227 /// <exception cref="InvalidOperationException">Thrown when failed because of permission denied or system error.</exception>
228 /// <privilege>http://tizen.org/privilege/appmanager.kill</privilege>
229 [EditorBrowsable(EditorBrowsableState.Never)]
230 public void Terminate()
232 err = Interop.ApplicationManager.AppManagerTerminateApp(_contextHandle);
233 if (err != Interop.ApplicationManager.ErrorCode.None)
237 case Interop.ApplicationManager.ErrorCode.InvalidParameter:
238 throw new ArgumentException("Invalid argument.");
239 case Interop.ApplicationManager.ErrorCode.PermissionDenied:
240 throw new InvalidOperationException("Permission denied.");
242 throw new InvalidOperationException("Invalid Operation.");
248 /// Releases all resources used by the ApplicationRunningContext class.
250 public void Dispose()
253 GC.SuppressFinalize(this);
256 private void Dispose(bool disposing)
261 if (_contextHandle != IntPtr.Zero)
263 Interop.ApplicationManager.AppContextDestroy(_contextHandle);
264 _contextHandle = IntPtr.Zero;