[ComponentManager] Add new APIs to handle components of the component… (#973)
[platform/core/csapi/tizenfx.git] / src / Tizen.Applications.ComponentManager / Tizen.Applications / ComponentInfo.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
20 namespace Tizen.Applications
21 {
22     /// <summary>
23     /// This class provides methods and properties to get information of the component.
24     /// </summary>
25     /// <since_tizen> 6 </since_tizen>
26     public class ComponentInfo : IDisposable
27     {
28         private const string LogTag = "Tizen.Applications";
29         private bool _disposed = false;
30         private IntPtr _infoHandle = IntPtr.Zero;
31         private string _componentId = string.Empty;
32
33         internal ComponentInfo(IntPtr infoHandle)
34         {
35             Interop.ComponentManager.ErrorCode err = Interop.ComponentManager.ComponentInfoGetComponentId(infoHandle, out _componentId);
36             if (err != Interop.ComponentManager.ErrorCode.None)
37             {
38                 throw ComponentManager.ComponentManagerErrorFactory.GetException(err, "Invalid native handle.");
39             }
40             _infoHandle = infoHandle;
41         }
42
43         /// <summary>
44         /// A constructor of ComponentInfo that takes the component ID.
45         /// </summary>
46         /// <param name="componentId">Component ID.</param>
47         /// <exception cref="ArgumentException">Thrown when failed because of an invalid argument.</exception>
48         /// <exception cref="InvalidOperationException">Thrown when failed because of the system error.</exception>
49         /// <exception cref="OutOfMemoryException">Thrown when failed because of out of memory.</exception>
50         /// <exception cref="UnauthorizedAccessException">Thrown when failed because of permission denied.</exception>>
51         /// <privilege>http://tizen.org/privilege/packagemanager.info</privilege>
52         /// <since_tizen> 6 </since_tizen>
53         public ComponentInfo(string componentId)
54         {
55             _componentId = componentId;
56             IntPtr infoHandle = IntPtr.Zero;
57             Interop.ComponentManager.ErrorCode err = Interop.ComponentManager.ComponentInfoCreate(_componentId, out infoHandle);
58             if (err != Interop.ComponentManager.ErrorCode.None)
59             {
60                 throw ComponentManager.ComponentManagerErrorFactory.GetException(err, "Failed to create the ComponentInfo.");
61             }
62             _infoHandle = infoHandle;
63         }
64
65         /// <summary>
66         /// Destructor of the class.
67         /// </summary>
68         ~ComponentInfo()
69         {
70             Dispose(false);
71         }
72
73         /// <summary>
74         /// Gets the component ID.
75         /// </summary>
76         /// <since_tizen> 6 </since_tizen>
77         public string ComponentId
78         {
79             get
80             {
81                 if (!string.IsNullOrEmpty(_componentId))
82                     return _componentId;
83
84                 string compId = string.Empty;
85                 Interop.ComponentManager.ErrorCode err = Interop.ComponentManager.ComponentInfoGetComponentId(_infoHandle, out compId);
86                 if (err != Interop.ComponentManager.ErrorCode.None)
87                 {
88                     Log.Warn(LogTag, "Failed to get the ComponentId. err = " + err);
89                 }
90                 _componentId = compId;
91
92                 return _componentId;
93             }
94         }
95
96         /// <summary>
97         /// Gets the application ID of the component.
98         /// </summary>
99         /// <since_tizen> 6 </since_tizen>
100         public string ApplicationId
101         {
102             get
103             {
104                 string appId = string.Empty;
105                 Interop.ComponentManager.ErrorCode err = Interop.ComponentManager.ComponentInfoGetAppId(_infoHandle, out appId);
106                 if (err != Interop.ComponentManager.ErrorCode.None)
107                 {
108                     Log.Warn(LogTag, "Failed to get the ApplicationId of " + _componentId + ". err = " + err);
109                 }
110
111                 return appId;
112             }
113         }
114
115         /// <summary>
116         /// Gets the type of the component.
117         /// </summary>
118         /// <since_tizen> 6 </since_tizen>
119         public ComponentType ComponentType
120         {
121             get
122             {
123                 Interop.ComponentManager.ComponentInfoComponentType type = 0;
124                 Interop.ComponentManager.ErrorCode err = Interop.ComponentManager.ComponentInfoGetComponentType(_infoHandle, out type);
125                 if (err != Interop.ComponentManager.ErrorCode.None)
126                 {
127                     Log.Warn(LogTag, "Failed to get the Type of " + _componentId + ". err = " + err);
128                 }
129
130                 return (ComponentType)type;
131             }
132         }
133
134         /// <summary>
135         /// Checks whether the icon of the component should be displayed or not.
136         /// </summary>
137         /// <since_tizen> 6 </since_tizen>
138         public bool IsIconDisplayed
139         {
140             get
141             {
142                 bool iconDisplay = false;
143                 Interop.ComponentManager.ErrorCode err = Interop.ComponentManager.ComponentInfoIsIconDisplay(_infoHandle, out iconDisplay);
144                 if (err != Interop.ComponentManager.ErrorCode.None)
145                 {
146                     Log.Warn(LogTag, "Failed to get the IsIconDisplay of " + _componentId + ". err = " + err);
147                 }
148
149                 return iconDisplay;
150             }
151         }
152
153         /// <summary>
154         /// Checks whether the component should be managed by task-manager or not.
155         /// </summary>
156         /// <since_tizen> 6 </since_tizen>
157         public bool IsManagedByTaskManager
158         {
159             get
160             {
161                 bool managed = false;
162                 Interop.ComponentManager.ErrorCode err = Interop.ComponentManager.ComponentInfoIsManagedByTaskManager(_infoHandle, out managed);
163                 if (err != Interop.ComponentManager.ErrorCode.None)
164                 {
165                     Log.Warn(LogTag, "Failed to get the IsManagedByTaskManager of " + _componentId + ". err = " + err);
166                 }
167
168                 return managed;
169             }
170         }
171
172         /// <summary>
173         /// Gets the absolute path of the icon image.
174         /// </summary>
175         /// <since_tizen> 6 </since_tizen>
176         public string IconPath
177         {
178             get
179             {
180                 string path = string.Empty;
181                 Interop.ComponentManager.ErrorCode err = Interop.ComponentManager.ComponentInfoGetIcon(_infoHandle, out path);
182                 if (err != Interop.ComponentManager.ErrorCode.None)
183                 {
184                     Log.Warn(LogTag, "Failed to get the IconPath of " + _componentId + ". err = " + err);
185                 }
186
187                 return path;
188             }
189         }
190
191         /// <summary>
192         /// Gets the label of the component.
193         /// </summary>
194         public string Label
195         {
196             get
197             {
198                 string label = string.Empty;
199                 Interop.ComponentManager.ErrorCode err = Interop.ComponentManager.ComponentInfoGetLabel(_infoHandle, out label);
200                 if (err != Interop.ComponentManager.ErrorCode.None)
201                 {
202                     Log.Warn(LogTag, "Failed to get the Label of " + _componentId + ". err = " + err);
203                 }
204
205                 return label;
206             }
207         }
208
209         /// <summary>
210         /// Gets the localized label of the component for the given locale.
211         /// </summary>
212         /// <param name="locale">Locale.</param>
213         /// <remarks>The format of locale is language and country code. (available value: "[2-letter lowercase language code (ISO 639-1)]-[2-letter lowercase country code (ISO 3166-alpha-2)]")</remarks>
214         /// <returns>The localized label.</returns>
215         /// <since_tizen> 6 </since_tizen>
216         public string GetLocalizedLabel(string locale)
217         {
218             string label = string.Empty;
219             Interop.ComponentManager.ErrorCode err = Interop.ComponentManager.ComponentInfoGetLocalizedLabel(_infoHandle, locale, out label);
220             if (err != Interop.ComponentManager.ErrorCode.None)
221             {
222                 Log.Warn(LogTag, "Failed to get the GetLocalizedLabel of " + _componentId + ". err = " + err);
223                 label = Label;
224             }
225             return label;
226         }
227
228         /// <summary>
229         /// Releases all resources used by the ComponentInfo class.
230         /// </summary>
231         /// <since_tizen> 6 </since_tizen>
232         public void Dispose()
233         {
234             Dispose(true);
235             GC.SuppressFinalize(this);
236         }
237
238         /// <summary>
239         /// Releases all resources used by the ComponentInfo class.
240         /// </summary>
241         /// <param name="disposing">Disposing</param>
242         /// <since_tizen> 6 </since_tizen>
243         private void Dispose(bool disposing)
244         {
245             if (_disposed)
246                 return;
247
248             if (disposing)
249             {
250             }
251
252             if (_infoHandle != IntPtr.Zero)
253             {
254                 Interop.ComponentManager.ComponentInfoDestroy(_infoHandle);
255                 _infoHandle = IntPtr.Zero;
256             }
257             _disposed = true;
258         }
259     }
260 }