Release 4.0.0-preview1-00051
[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     public class ApplicationRunningContext : IDisposable
27     {
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;
32
33         internal ApplicationRunningContext(IntPtr contextHandle)
34         {
35             _contextHandle = contextHandle;
36         }
37
38         /// <summary>
39         /// A constructor of ApplicationRunningContext that takes the application id.
40         /// </summary>
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)
46         {
47             IntPtr contextHandle = IntPtr.Zero;
48             err = Interop.ApplicationManager.AppManagerGetAppContext(applicationId, out contextHandle);
49             if (err != Interop.ApplicationManager.ErrorCode.None)
50             {
51                 Log.Warn(LogTag, "Failed to get the handle of the ApplicationRunningContext. err = " + err);
52                 switch (err)
53                 {
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");
60                     default:
61                         throw new InvalidOperationException("Invalid Operation.");
62                 }
63             }
64             _contextHandle = contextHandle;
65         }
66
67         /// <summary>
68         /// A constructor of ApplicationRunningContext that takes the application id.
69         /// </summary>
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)
77         {
78             IntPtr contextHandle = IntPtr.Zero;
79             err = Interop.ApplicationManager.AppManagerGetAppContextByInstanceId(applicationId, instanceId, out contextHandle);
80             if (err != Interop.ApplicationManager.ErrorCode.None)
81             {
82                 Log.Warn(LogTag, "Failed to get the handle of the ApplicationRunningContext. err = " + err);
83                 switch (err)
84                 {
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");
91                     default:
92                         throw new InvalidOperationException("Invalid Operation.");
93                 }
94             }
95             _contextHandle = contextHandle;
96         }
97
98         /// <summary>
99         /// Destructor of the class
100         /// </summary>
101         ~ApplicationRunningContext()
102         {
103             Dispose(false);
104         }
105
106         /// <summary>
107         /// Enumeration for the Application State.
108         /// </summary>
109         public enum AppState
110         {
111             /// <summary>
112             /// The undefined state
113             /// </summary>
114             Undefined = 0,
115
116             /// <summary>
117             /// The UI application is running in the foreground.
118             /// </summary>
119             Foreground,
120
121             /// <summary>
122             /// The UI application is running in the background.
123             /// </summary>
124             Background,
125
126             /// <summary>
127             /// The Service application is running.
128             /// </summary>
129             Service,
130
131             /// <summary>
132             /// The application is terminated.
133             /// </summary>
134             Terminated,
135         }
136
137         /// <summary>
138         /// Gets the application id.
139         /// </summary>
140         public string ApplicationId
141         {
142             get
143             {
144                 string appid = string.Empty;
145                 err = Interop.ApplicationManager.AppContextGetAppId(_contextHandle, out appid);
146                 if (err != Interop.ApplicationManager.ErrorCode.None)
147                 {
148                     Log.Warn(LogTag, "Failed to get the application id. err = " + err);
149                 }
150                 return appid;
151             }
152         }
153
154         /// <summary>
155         /// Gets the package id of the application.
156         /// </summary>
157         public string PackageId
158         {
159             get
160             {
161                 string packageid = string.Empty;
162                 err = Interop.ApplicationManager.AppContextGetPackageId(_contextHandle, out packageid);
163                 if (err != Interop.ApplicationManager.ErrorCode.None)
164                 {
165                     Log.Warn(LogTag, "Failed to get the package id. err = " + err);
166                 }
167                 return packageid;
168             }
169         }
170
171         /// <summary>
172         /// Gets the application's process id.
173         /// </summary>
174         public int ProcessId
175         {
176             get
177             {
178                 int pid = 0;
179                 err = Interop.ApplicationManager.AppContextGetPid(_contextHandle, out pid);
180                 if (err != Interop.ApplicationManager.ErrorCode.None)
181                 {
182                     Log.Warn(LogTag, "Failed to get the process id. err = " + err);
183                 }
184                 return pid;
185             }
186         }
187
188         /// <summary>
189         /// Gets the state of the application.
190         /// </summary>
191         public AppState State
192         {
193             get
194             {
195                 int state = 0;
196
197                 err = Interop.ApplicationManager.AppContextGetAppState(_contextHandle, out state);
198                 if (err != Interop.ApplicationManager.ErrorCode.None)
199                 {
200                     Log.Warn(LogTag, "Failed to get the application state. err = " + err);
201                 }
202                 return (AppState)state;
203             }
204         }
205
206         /// <summary>
207         /// Gets whether the application is sub application of the application group.
208         /// </summary>
209         public bool IsSubApp
210         {
211             get
212             {
213                 bool subapp = false;
214                 err = Interop.ApplicationManager.AppContextIsSubApp(_contextHandle, out subapp);
215                 if (err != Interop.ApplicationManager.ErrorCode.None)
216                 {
217                     Log.Warn(LogTag, "Failed to get the IsSubApp value. err = " + err);
218                 }
219                 return subapp;
220             }
221         }
222
223         /// <summary>
224         /// Terminates the application.
225         /// </summary>
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()
231         {
232             err = Interop.ApplicationManager.AppManagerTerminateApp(_contextHandle);
233             if (err != Interop.ApplicationManager.ErrorCode.None)
234             {
235                 switch (err)
236                 {
237                     case Interop.ApplicationManager.ErrorCode.InvalidParameter:
238                         throw new ArgumentException("Invalid argument.");
239                     case Interop.ApplicationManager.ErrorCode.PermissionDenied:
240                         throw new InvalidOperationException("Permission denied.");
241                     default:
242                         throw new InvalidOperationException("Invalid Operation.");
243                 }
244             }
245         }
246
247         /// <summary>
248         /// Releases all resources used by the ApplicationRunningContext class.
249         /// </summary>
250         public void Dispose()
251         {
252             Dispose(true);
253             GC.SuppressFinalize(this);
254         }
255
256         private void Dispose(bool disposing)
257         {
258             if (_disposed)
259                 return;
260
261             if (_contextHandle != IntPtr.Zero)
262             {
263                 Interop.ApplicationManager.AppContextDestroy(_contextHandle);
264                 _contextHandle = IntPtr.Zero;
265             }
266             _disposed = true;
267         }
268     }
269 }