[ComponentManager] Change namespace (#989)
[platform/core/csapi/tizenfx.git] / src / Tizen.Applications.ComponentBased.ComponentManager / Tizen.Applications / ComponentRunningContext.cs
1 /*
2  * Copyright (c) 2019 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 using System.Runtime.InteropServices;
21 using System.Threading.Tasks;
22
23 namespace Tizen.Applications.ComponentBased
24 {
25     /// <summary>
26     /// This class provides methods and properties to get information of the running component.
27     /// </summary>
28     /// <since_tizen> 6 </since_tizen>
29     public class ComponentRunningContext : IDisposable
30     {
31         private const string LogTag = "Tizen.Applications";
32         private bool _disposed = false;
33         internal IntPtr _contextHandle = IntPtr.Zero;
34         private string _componentId = string.Empty;
35
36         internal ComponentRunningContext(IntPtr contextHandle)
37         {
38             _contextHandle = contextHandle;
39         }
40
41         /// <summary>
42         /// A constructor of ComponentRunningContext that takes the component ID.
43         /// </summary>
44         /// <param name="componentId">Component ID.</param>
45         /// <exception cref="ArgumentException">Thrown when failed because of an invalid argument.</exception>
46         /// <exception cref="InvalidOperationException">Thrown when failed because of the "component not exist" error or the system error.</exception>
47         /// <exception cref="OutOfMemoryException">Thrown when failed because of out of memory.</exception>
48         /// <exception cref="UnauthorizedAccessException">Thrown when failed because of permission denied.</exception>
49         /// <privilege>http://tizen.org/privilege/packagemanager.info</privilege>
50         /// <since_tizen> 6 </since_tizen>
51         public ComponentRunningContext(string componentId)
52         {
53             _componentId = componentId;
54             IntPtr contextHandle = IntPtr.Zero;
55             Interop.ComponentManager.ErrorCode err = Interop.ComponentManager.ComponentManagerGetComponentContext(_componentId, out contextHandle);
56             if (err != Interop.ComponentManager.ErrorCode.None)
57             {
58                 throw ComponentManager.ComponentManagerErrorFactory.GetException(err, "Failed to create the ComponentRunningContext of " + _componentId + ".");
59             }
60             _contextHandle = contextHandle;
61         }
62
63         /// <summary>
64         /// Destructor of the class.
65         /// </summary>
66         ~ComponentRunningContext()
67         {
68             Dispose(false);
69         }
70
71         /// <summary>
72         /// Enumeration for the component state.
73         /// </summary>
74         /// <since_tizen> 6 </since_tizen>
75         public enum ComponentState
76         {
77             /// <summary>
78             /// The Initialized state. This is the state when the component is constructed but OnCreate() is not called yet.
79             /// </summary>
80             Initialized = 0,
81
82             /// <summary>
83             /// The created state. This state is reached after OnCreate() is called.
84             /// </summary>
85             Created,
86
87             /// <summary>
88             /// The started state. This state is reached after OnStart() or OnStartCommand() is called.
89             /// </summary>
90             Started,
91
92             /// <summary>
93             /// The resumed state. This state is reached after OnResume() is called.
94             /// </summary>
95             Resumed,
96
97             /// <summary>
98             /// The paused state. This state is reached after OnPause() is called.
99             /// </summary>
100             Paused,
101
102             /// <summary>
103             /// The destroyed state. This state is reached right before OnDestroy() call.
104             /// </summary>
105             Destroyed
106         }
107
108         /// <summary>
109         /// Gets the ID of the component.
110         /// </summary>
111         /// <since_tizen> 6 </since_tizen>
112         public string ComponentId
113         {
114             get
115             {
116                 if (!string.IsNullOrEmpty(_componentId))
117                     return _componentId;
118
119                 string componentId = string.Empty;
120                 Interop.ComponentManager.ErrorCode err = Interop.ComponentManager.ComponentContextGetComponentId(_contextHandle, out componentId);
121                 if (err != Interop.ComponentManager.ErrorCode.None)
122                 {
123                     Log.Warn(LogTag, "Failed to get the ComponentId. err = " + err);
124                 }
125                 _componentId = componentId;
126
127                 return _componentId;
128             }
129         }
130
131         /// <summary>
132         /// Gets the application ID of the component.
133         /// </summary>
134         /// <since_tizen> 6 </since_tizen>
135         public string ApplicationId
136         {
137             get
138             {
139                 string appId = string.Empty;
140                 Interop.ComponentManager.ErrorCode err = Interop.ComponentManager.ComponentContextGetAppId(_contextHandle, out appId);
141                 if (err != Interop.ComponentManager.ErrorCode.None)
142                 {
143                     Log.Warn(LogTag, "Failed to get the ApplicationId of " + _componentId + ". err = " + err);
144                 }
145
146                 return appId;
147             }
148         }
149         
150         /// <summary>
151         /// Gets the instance ID of the component.
152         /// </summary>
153         /// <since_tizen> 6 </since_tizen>
154         public string InstanceId
155         {
156             get
157             {
158                 string instanceId = string.Empty;
159                 Interop.ComponentManager.ErrorCode err = Interop.ComponentManager.ComponentContextGetInstanceId(_contextHandle, out instanceId);
160                 if (err != Interop.ComponentManager.ErrorCode.None)
161                 {
162                     Log.Warn(LogTag, "Failed to get the InstanceId of " + _componentId + ". err = " + err);
163                 }
164
165                 return instanceId;
166             }
167         }
168
169         /// <summary>
170         /// Gets the state of the component.
171         /// </summary>
172         /// <since_tizen> 6 </since_tizen>
173         public ComponentState State
174         {
175             get
176             {
177                 int state = 0;
178                 Interop.ComponentManager.ErrorCode err = Interop.ComponentManager.ComponentContextGetComponentState(_contextHandle, out state);
179                 if (err != Interop.ComponentManager.ErrorCode.None)
180                 {
181                     Log.Warn(LogTag, "Failed to get the State of " + _componentId + ". err = " + err);
182                 }
183
184                 return (ComponentState)state;
185             }
186         }
187
188         /// <summary>
189         /// Checks whether the component is terminated or not.
190         /// </summary>
191         /// <since_tizen> 6 </since_tizen>
192         public bool IsTerminated
193         {
194             get
195             {
196                 bool isTerminated = false;
197                 Interop.ComponentManager.ErrorCode err = Interop.ComponentManager.ComponentContextIsTerminated(_contextHandle, out isTerminated);
198                 if (err != Interop.ComponentManager.ErrorCode.None)
199                 {
200                     Log.Warn(LogTag, "Failed to get the IsTerminated of " + _componentId + ". err = " + err);
201                 }
202
203                 return isTerminated;
204             }
205         }
206
207         /// <summary>
208         /// Checks whether the component is running as sub component of the group.
209         /// </summary>
210         /// <since_tizen> 6 </since_tizen>
211         public bool IsSubComponent
212         {
213             get
214             {
215                 bool isSubComponent = false;
216                 Interop.ComponentManager.ErrorCode err = Interop.ComponentManager.ComponentContextIsSubComponent(_contextHandle, out isSubComponent);
217                 if (err != Interop.ComponentManager.ErrorCode.None)
218                 {
219                     Log.Warn(LogTag, "Failed to get the IsSubComponent of " + _componentId + ". err = " + err);
220                 }
221
222                 return isSubComponent;
223             }
224         }
225
226         /// <summary>
227         /// Resumes the running component.
228         /// </summary>
229         /// <exception cref="ArgumentException">Thrown when failed because of an invalid argument.</exception>
230         /// <exception cref="InvalidOperationException">Thrown when failed because of the system error.</exception>
231         /// <exception cref="OutOfMemoryException">Thrown when failed because of out of memory.</exception>
232         /// <exception cref="UnauthorizedAccessException">Thrown when failed because of permission denied.</exception>
233         /// <privilege>http://tizen.org/privilege/appmanager.launch</privilege>
234         /// <since_tizen> 6 </since_tizen>
235         public void Resume()
236         {
237             Interop.ComponentManager.ErrorCode err = Interop.ComponentManager.ComponentManagerResumeComponent(_contextHandle);
238             if (err != Interop.ComponentManager.ErrorCode.None)
239             {
240                 throw ComponentManager.ComponentManagerErrorFactory.GetException(err, "Failed to Send the resume request.");
241             }
242         }
243
244         /// <summary>
245         /// Pauses the running component.
246         /// </summary>
247         /// <exception cref="ArgumentException">Thrown when failed because of an invalid argument.</exception>
248         /// <exception cref="InvalidOperationException">Thrown when failed because of the system error.</exception>
249         /// <exception cref="OutOfMemoryException">Thrown when failed because of out of memory.</exception>
250         /// <exception cref="UnauthorizedAccessException">Thrown when failed because of permission denied.</exception>
251         /// <privilege>http://tizen.org/privilege/appmanager.launch</privilege>
252         /// <since_tizen> 6 </since_tizen>
253         [EditorBrowsable(EditorBrowsableState.Never)]
254         public void Pause()
255         {
256             Interop.ComponentManager.ErrorCode err = Interop.ComponentManager.ComponentManagerPauseComponent(_contextHandle);
257             if (err != Interop.ComponentManager.ErrorCode.None)
258             {
259                 throw ComponentManager.ComponentManagerErrorFactory.GetException(err, "Failed to Send the pause request.");
260             }
261         }
262
263         /// <summary>
264         /// Terminates the running component.
265         /// </summary>
266         /// <exception cref="ArgumentException">Thrown when failed because of an invalid argument.</exception>
267         /// <exception cref="InvalidOperationException">Thrown when failed because of the system error.</exception>
268         /// <exception cref="OutOfMemoryException">Thrown when failed because of out of memory.</exception>
269         /// <exception cref="UnauthorizedAccessException">Thrown when failed because of permission denied.</exception>
270         /// <privilege>http://tizen.org/privilege/appmanager.launch</privilege>
271         /// <since_tizen> 6 </since_tizen>
272         [EditorBrowsable(EditorBrowsableState.Never)]
273         public void Terminate()
274         {
275             Interop.ComponentManager.ErrorCode err = Interop.ComponentManager.ComponentManagerTerminateComponent(_contextHandle);
276             if (err != Interop.ComponentManager.ErrorCode.None)
277             {
278                 throw ComponentManager.ComponentManagerErrorFactory.GetException(err, "Failed to Send the terminate request.");
279             }
280         }
281
282         /// <summary>
283         /// Releases all resources used by the ComponentInfo class.
284         /// </summary>
285         /// <since_tizen> 6 </since_tizen>
286         public void Dispose()
287         {
288             Dispose(true);
289             GC.SuppressFinalize(this);
290         }
291
292         /// <summary>
293         /// Releases all resources used by the ComponentInfo class.
294         /// </summary>
295         /// <param name="disposing">Disposing</param>
296         /// <since_tizen> 6 </since_tizen>
297         private void Dispose(bool disposing)
298         {
299             if (_disposed)
300                 return;
301
302             if (disposing)
303             {
304             }
305
306             if (_contextHandle != IntPtr.Zero)
307             {
308                 Interop.ComponentManager.ComponentContextDestroy(_contextHandle);
309                 _contextHandle = IntPtr.Zero;
310             }
311             _disposed = true;
312         }
313     }
314 }