2 * Copyright (c) 2019 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;
20 using System.Runtime.InteropServices;
21 using System.Threading.Tasks;
23 namespace Tizen.Applications.ComponentBased
26 /// This class provides methods and properties to get information of the running component.
28 /// <since_tizen> 6 </since_tizen>
29 public class ComponentRunningContext : IDisposable
31 private const string LogTag = "Tizen.Applications";
32 private bool _disposed = false;
33 internal IntPtr _contextHandle = IntPtr.Zero;
34 private string _componentId = string.Empty;
36 internal ComponentRunningContext(IntPtr contextHandle)
38 _contextHandle = contextHandle;
42 /// A constructor of ComponentRunningContext that takes the component ID.
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)
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)
58 throw ComponentManager.ComponentManagerErrorFactory.GetException(err, "Failed to create the ComponentRunningContext of " + _componentId + ".");
60 _contextHandle = contextHandle;
64 /// Destructor of the class.
66 ~ComponentRunningContext()
72 /// Enumeration for the component state.
74 /// <since_tizen> 6 </since_tizen>
75 public enum ComponentState
78 /// The Initialized state. This is the state when the component is constructed but OnCreate() is not called yet.
83 /// The created state. This state is reached after OnCreate() is called.
88 /// The started state. This state is reached after OnStart() or OnStartCommand() is called.
93 /// The resumed state. This state is reached after OnResume() is called.
98 /// The paused state. This state is reached after OnPause() is called.
103 /// The destroyed state. This state is reached right before OnDestroy() call.
109 /// Gets the ID of the component.
111 /// <since_tizen> 6 </since_tizen>
112 public string ComponentId
116 if (!string.IsNullOrEmpty(_componentId))
119 string componentId = string.Empty;
120 Interop.ComponentManager.ErrorCode err = Interop.ComponentManager.ComponentContextGetComponentId(_contextHandle, out componentId);
121 if (err != Interop.ComponentManager.ErrorCode.None)
123 Log.Warn(LogTag, "Failed to get the ComponentId. err = " + err);
125 _componentId = componentId;
132 /// Gets the application ID of the component.
134 /// <since_tizen> 6 </since_tizen>
135 public string ApplicationId
139 string appId = string.Empty;
140 Interop.ComponentManager.ErrorCode err = Interop.ComponentManager.ComponentContextGetAppId(_contextHandle, out appId);
141 if (err != Interop.ComponentManager.ErrorCode.None)
143 Log.Warn(LogTag, "Failed to get the ApplicationId of " + _componentId + ". err = " + err);
151 /// Gets the instance ID of the component.
153 /// <since_tizen> 6 </since_tizen>
154 public string InstanceId
158 string instanceId = string.Empty;
159 Interop.ComponentManager.ErrorCode err = Interop.ComponentManager.ComponentContextGetInstanceId(_contextHandle, out instanceId);
160 if (err != Interop.ComponentManager.ErrorCode.None)
162 Log.Warn(LogTag, "Failed to get the InstanceId of " + _componentId + ". err = " + err);
170 /// Gets the state of the component.
172 /// <since_tizen> 6 </since_tizen>
173 public ComponentState State
178 Interop.ComponentManager.ErrorCode err = Interop.ComponentManager.ComponentContextGetComponentState(_contextHandle, out state);
179 if (err != Interop.ComponentManager.ErrorCode.None)
181 Log.Warn(LogTag, "Failed to get the State of " + _componentId + ". err = " + err);
184 return (ComponentState)state;
189 /// Checks whether the component is terminated or not.
191 /// <since_tizen> 6 </since_tizen>
192 public bool IsTerminated
196 bool isTerminated = false;
197 Interop.ComponentManager.ErrorCode err = Interop.ComponentManager.ComponentContextIsTerminated(_contextHandle, out isTerminated);
198 if (err != Interop.ComponentManager.ErrorCode.None)
200 Log.Warn(LogTag, "Failed to get the IsTerminated of " + _componentId + ". err = " + err);
208 /// Checks whether the component is running as sub component of the group.
210 /// <since_tizen> 6 </since_tizen>
211 public bool IsSubComponent
215 bool isSubComponent = false;
216 Interop.ComponentManager.ErrorCode err = Interop.ComponentManager.ComponentContextIsSubComponent(_contextHandle, out isSubComponent);
217 if (err != Interop.ComponentManager.ErrorCode.None)
219 Log.Warn(LogTag, "Failed to get the IsSubComponent of " + _componentId + ". err = " + err);
222 return isSubComponent;
227 /// Resumes the running component.
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>
237 Interop.ComponentManager.ErrorCode err = Interop.ComponentManager.ComponentManagerResumeComponent(_contextHandle);
238 if (err != Interop.ComponentManager.ErrorCode.None)
240 throw ComponentManager.ComponentManagerErrorFactory.GetException(err, "Failed to Send the resume request.");
245 /// Pauses the running component.
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)]
256 Interop.ComponentManager.ErrorCode err = Interop.ComponentManager.ComponentManagerPauseComponent(_contextHandle);
257 if (err != Interop.ComponentManager.ErrorCode.None)
259 throw ComponentManager.ComponentManagerErrorFactory.GetException(err, "Failed to Send the pause request.");
264 /// Terminates the running component.
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()
275 Interop.ComponentManager.ErrorCode err = Interop.ComponentManager.ComponentManagerTerminateComponent(_contextHandle);
276 if (err != Interop.ComponentManager.ErrorCode.None)
278 throw ComponentManager.ComponentManagerErrorFactory.GetException(err, "Failed to Send the terminate request.");
283 /// Releases all resources used by the ComponentInfo class.
285 /// <since_tizen> 6 </since_tizen>
286 public void Dispose()
289 GC.SuppressFinalize(this);
293 /// Releases all resources used by the ComponentInfo class.
295 /// <param name="disposing">Disposing</param>
296 /// <since_tizen> 6 </since_tizen>
297 private void Dispose(bool disposing)
306 if (_contextHandle != IntPtr.Zero)
308 Interop.ComponentManager.ComponentContextDestroy(_contextHandle);
309 _contextHandle = IntPtr.Zero;