[Applications][TCSACR-441] Add new APIs for resource control (#3414)
[platform/core/csapi/tizenfx.git] / src / Tizen.Applications.Common / Tizen.Applications / ApplicationInfo.cs
1 /*
2  * Copyright (c) 2016 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 application.
24     /// </summary>
25     /// <since_tizen> 3 </since_tizen>
26     public class ApplicationInfo : IDisposable
27     {
28         private const string LogTag = "Tizen.Applications";
29         private bool _disposed = false;
30         private IntPtr _infoHandle = IntPtr.Zero;
31         private string _applicationId = string.Empty;
32         private Interop.ApplicationManager.ErrorCode err = Interop.ApplicationManager.ErrorCode.None;
33
34         internal ApplicationInfo(IntPtr infoHandle)
35         {
36             err = Interop.ApplicationManager.AppInfoGetAppId(infoHandle, out _applicationId);
37             if (err != Interop.ApplicationManager.ErrorCode.None)
38             {
39                 throw new ArgumentException("Invalid native handle.");
40             }
41             _infoHandle = infoHandle;
42         }
43
44         /// <summary>
45         /// A constructor of ApplicationInfo that takes the application ID.
46         /// </summary>
47         /// <param name="applicationId">Application ID.</param>
48         /// <since_tizen> 3 </since_tizen>
49         public ApplicationInfo(string applicationId)
50         {
51             _applicationId = applicationId;
52         }
53
54         /// <summary>
55         /// Destructor of the class.
56         /// </summary>
57         ~ApplicationInfo()
58         {
59             Dispose(false);
60         }
61
62         /// <summary>
63         /// Gets the application ID.
64         /// </summary>
65         /// <since_tizen> 3 </since_tizen>
66         public string ApplicationId
67         {
68             get
69             {
70                 if (!string.IsNullOrEmpty(_applicationId))
71                     return _applicationId;
72                 IntPtr infoHandle = GetInfoHandle();
73                 string appid = string.Empty;
74                 if (infoHandle != IntPtr.Zero)
75                 {
76                     err = Interop.ApplicationManager.AppInfoGetAppId(infoHandle, out appid);
77                     if (err != Interop.ApplicationManager.ErrorCode.None)
78                     {
79                         Log.Warn(LogTag, "Failed to get the application id. err = " + err);
80                     }
81                 }
82                 return appid;
83             }
84         }
85
86         /// <summary>
87         /// Gets the package ID of the application.
88         /// </summary>
89         /// <since_tizen> 3 </since_tizen>
90         public string PackageId
91         {
92             get
93             {
94                 IntPtr infoHandle = GetInfoHandle();
95                 string packageid = string.Empty;
96                 if (infoHandle != IntPtr.Zero)
97                 {
98                     err = Interop.ApplicationManager.AppInfoGetPackage(infoHandle, out packageid);
99                     if (err != Interop.ApplicationManager.ErrorCode.None)
100                     {
101                         Log.Warn(LogTag, "Failed to get the package id of " + _applicationId + ". err = " + err);
102                     }
103                 }
104                 return packageid;
105             }
106         }
107
108         /// <summary>
109         /// Gets the label of the application.
110         /// </summary>
111         /// <since_tizen> 3 </since_tizen>
112         public string Label
113         {
114             get
115             {
116                 IntPtr infoHandle = GetInfoHandle();
117                 string label = string.Empty;
118                 if (infoHandle != IntPtr.Zero)
119                 {
120                     err = Interop.ApplicationManager.AppInfoGetLabel(infoHandle, out label);
121                     if (err != Interop.ApplicationManager.ErrorCode.None)
122                     {
123                         Log.Warn(LogTag, "Failed to get the app label of " + _applicationId + ". err = " + err);
124                     }
125                 }
126                 return label;
127             }
128         }
129
130         /// <summary>
131         /// Gets the executable path of the application.
132         /// </summary>
133         /// <since_tizen> 3 </since_tizen>
134         public string ExecutablePath
135         {
136             get
137             {
138                 IntPtr infoHandle = GetInfoHandle();
139                 string exec = string.Empty;
140                 if (infoHandle != IntPtr.Zero)
141                 {
142                     err = Interop.ApplicationManager.AppInfoGetExec(infoHandle, out exec);
143                     if (err != Interop.ApplicationManager.ErrorCode.None)
144                     {
145                         Log.Warn(LogTag, "Failed to get the executable file path of " + _applicationId + ". err = " + err);
146                     }
147                 }
148                 return exec;
149             }
150         }
151
152         /// <summary>
153         /// Gets the absolute path to the icon image.
154         /// </summary>
155         /// <since_tizen> 3 </since_tizen>
156         public string IconPath
157         {
158             get
159             {
160                 IntPtr infoHandle = GetInfoHandle();
161                 string path = string.Empty;
162                 if (infoHandle != IntPtr.Zero)
163                 {
164                     err = Interop.ApplicationManager.AppInfoGetIcon(infoHandle, out path);
165                     if (err != Interop.ApplicationManager.ErrorCode.None)
166                     {
167                         Log.Warn(LogTag, "Failed to get the app icon path of " + _applicationId + ". err = " + err);
168                     }
169                 }
170                 return path;
171             }
172         }
173
174         /// <summary>
175         /// Gets the application type name.
176         /// </summary>
177         /// <since_tizen> 3 </since_tizen>
178         public string ApplicationType
179         {
180             get
181             {
182                 IntPtr infoHandle = GetInfoHandle();
183                 string type = string.Empty;
184                 if (infoHandle != IntPtr.Zero)
185                 {
186                     err = Interop.ApplicationManager.AppInfoGetType(infoHandle, out type);
187                     if (err != Interop.ApplicationManager.ErrorCode.None)
188                     {
189                         Log.Warn(LogTag, "Failed to get the application type of " + _applicationId + ". err = " + err);
190                     }
191                 }
192                 return type;
193             }
194         }
195
196         /// <summary>
197         /// Gets the application component type.
198         /// </summary>
199         /// <since_tizen> 6 </since_tizen>
200         public ApplicationComponentType ComponentType
201         {
202             get
203             {
204                 IntPtr infoHandle = GetInfoHandle();
205                 Interop.ApplicationManager.AppInfoAppComponentType componentType = 0;
206                 if (infoHandle != IntPtr.Zero)
207                 {
208                     err = Interop.ApplicationManager.AppInfoGetAppComponentType(infoHandle, out componentType);
209                     if (err != Interop.ApplicationManager.ErrorCode.None)
210                     {
211                         Log.Warn(LogTag, "Failed to get the application component type of " + _applicationId + ". err = " + err);
212                     }
213                 }
214                 return (ApplicationComponentType)componentType;
215             }
216         }
217
218         /// <summary>
219         /// Gets the application's metadata.
220         /// </summary>
221         /// <since_tizen> 3 </since_tizen>
222         public IDictionary<String, String> Metadata
223         {
224             get
225             {
226                 IDictionary<string, string> metadata = new Dictionary<String, String>();
227
228                 Interop.ApplicationManager.AppInfoMetadataCallback cb = (string key, string value, IntPtr userData) =>
229                 {
230                     if (key.Length != 0)
231                     {
232                         if (!metadata.ContainsKey(key))
233                             metadata.Add(key, value);
234                     }
235                     return true;
236                 };
237
238                 IntPtr infoHandle = GetInfoHandle();
239                 if (infoHandle != IntPtr.Zero)
240                 {
241                     err = Interop.ApplicationManager.AppInfoForeachMetadata(infoHandle, cb, IntPtr.Zero);
242                     if (err != Interop.ApplicationManager.ErrorCode.None)
243                     {
244                         Log.Warn(LogTag, "Failed to get application metadata of " + _applicationId + ". err = " + err);
245                     }
246                 }
247                 return metadata;
248             }
249         }
250
251         /// <summary>
252         /// Checks whether the application information is nodisplay. If the application icon is not displayed on the menu screen, true; otherwise, false.
253         /// </summary>
254         /// <since_tizen> 3 </since_tizen>
255         public bool IsNoDisplay
256         {
257             get
258             {
259                 IntPtr infoHandle = GetInfoHandle();
260                 bool nodisplay = false;
261                 if (infoHandle != IntPtr.Zero)
262                 {
263                     err = Interop.ApplicationManager.AppInfoIsNodisplay(infoHandle, out nodisplay);
264                     if (err != Interop.ApplicationManager.ErrorCode.None)
265                     {
266                         Log.Warn(LogTag, "Failed to get the IsNoDisplay value of " + _applicationId + ". err = " + err);
267
268                     }
269                 }
270                 return nodisplay;
271             }
272         }
273
274         /// <summary>
275         /// Checks whether the application is launched on booting time. If the application automatically starts on boot, true; otherwise, false.
276         /// </summary>
277         /// <since_tizen> 3 </since_tizen>
278         public bool IsOnBoot
279         {
280             get
281             {
282                 IntPtr infoHandle = GetInfoHandle();
283                 bool onboot = false;
284                 if (infoHandle != IntPtr.Zero)
285                 {
286                     err = Interop.ApplicationManager.AppInfoIsOnBoot(infoHandle, out onboot);
287                     if (err != Interop.ApplicationManager.ErrorCode.None)
288                     {
289                         Log.Warn(LogTag, "Failed to get the IsOnBoot value of " + _applicationId + ". err = " + err);
290                     }
291                 }
292                 return onboot;
293             }
294         }
295
296         /// <summary>
297         /// Checks whether the application is preloaded. If the application is preloaded, true; otherwise, false.
298         /// </summary>
299         /// <since_tizen> 3 </since_tizen>
300         public bool IsPreload
301         {
302             get
303             {
304                 IntPtr infoHandle = GetInfoHandle();
305                 bool preloaded = false;
306                 if (infoHandle != IntPtr.Zero)
307                 {
308                     err = Interop.ApplicationManager.AppInfoIsPreLoad(infoHandle, out preloaded);
309                     if (err != Interop.ApplicationManager.ErrorCode.None)
310                     {
311                         Log.Warn(LogTag, "Failed to get the IsPreload value of " + _applicationId + ". err = " + err);
312                     }
313                 }
314                 return preloaded;
315             }
316         }
317
318         /// <summary>
319         /// Gets the application's category values specified in the tizen-manifest
320         /// </summary>
321         /// <privilege>http://tizen.org/privilege/packagemanager.admin</privilege>
322         /// <privlevel>platform</privlevel>
323         /// <since_tizen> 6 </since_tizen>
324         public IEnumerable<string> Categories
325         {
326             get
327             {
328                 List<string> categories = new List<string>();
329
330                 Interop.ApplicationManager.AppInfoCategoryCallback cb = (string category, IntPtr userData) =>
331                 {
332                     categories.Add(category);
333                     return true;
334                 };
335
336                 IntPtr infoHandle = GetInfoHandle();
337                 if (infoHandle != IntPtr.Zero)
338                 {
339                     err = Interop.ApplicationManager.AppInfoForeachCategory(infoHandle, cb, IntPtr.Zero);
340                     if (err != Interop.ApplicationManager.ErrorCode.None)
341                     {
342                         Log.Warn(LogTag, "Failed to get application category of " + _applicationId + ". err = " + err);
343                     }
344                 }
345                 return categories;
346             }
347         }
348
349         /// <summary>
350         /// Gets the shared data path.
351         /// </summary>
352         /// <remarks>
353         /// An application that wants to use shared/data directory must declare http://tizen.org/privilege/appdir.shareddata privilege. If the application doesn't declare the privilege, the framework will not create shared/data directory for the application. This property will return empty string when the application doesn't have shared/data directory.
354         /// </remarks>
355         /// <since_tizen> 3 </since_tizen>
356         public string SharedDataPath
357         {
358             get
359             {
360                 string path = string.Empty;
361                 err = Interop.ApplicationManager.AppManagerGetSharedDataPath(ApplicationId, out path);
362                 if (err != Interop.ApplicationManager.ErrorCode.None)
363                 {
364                     Log.Warn(LogTag, "Failed to get the SharedDataPath of " + _applicationId + ". err = " + err);
365                 }
366                 return path;
367             }
368         }
369
370         /// <summary>
371         /// Gets the shared resource path.
372         /// </summary>
373         /// <since_tizen> 3 </since_tizen>
374         public string SharedResourcePath
375         {
376             get
377             {
378                 string path = string.Empty;
379                 err = Interop.ApplicationManager.AppManagerGetSharedResourcePath(ApplicationId, out path);
380                 if (err != Interop.ApplicationManager.ErrorCode.None)
381                 {
382                     Log.Warn(LogTag, "Failed to get the SharedResourcePath of " + _applicationId + ". err = " + err);
383                 }
384                 return path;
385             }
386         }
387
388         /// <summary>
389         /// Gets the shared trust path.
390         /// </summary>
391         /// <since_tizen> 3 </since_tizen>
392         public string SharedTrustedPath
393         {
394             get
395             {
396                 string path = string.Empty;
397                 err = Interop.ApplicationManager.AppManagerGetSharedTrustedPath(ApplicationId, out path);
398                 if (err != Interop.ApplicationManager.ErrorCode.None)
399                 {
400                     Log.Warn(LogTag, "Failed to get the SharedTrustedPath of " + _applicationId + ". err = " + err);
401                 }
402                 return path;
403             }
404         }
405
406         /// <summary>
407         /// Gets the external shared data path.
408         /// </summary>
409         /// <since_tizen> 3 </since_tizen>
410         public string ExternalSharedDataPath
411         {
412             get
413             {
414                 string path = string.Empty;
415                 err = Interop.ApplicationManager.AppManagerGetExternalSharedDataPath(ApplicationId, out path);
416                 if (err != Interop.ApplicationManager.ErrorCode.None)
417                 {
418                     Log.Warn(LogTag, "Failed to get the ExternalSharedDataPath of " + _applicationId + ". err = " + err);
419                 }
420                 return path;
421             }
422         }
423
424         /// <summary>
425         /// Gets the resource controls.
426         /// </summary>
427         /// <since_tizen> 9 </since_tizen>
428         public IEnumerable<ResourceControl> ResourceControls
429         {
430             get
431             {
432                 List<ResourceControl> resourceControls = new List<ResourceControl>();
433                 Interop.ApplicationManager.AppInfoResControlCallback cb = (string resType, string minResourceVersion, string maxResourceVersion, string isAutoClose, IntPtr userData) =>
434                 {
435                     resourceControls.Add(new ResourceControl(resType, minResourceVersion, maxResourceVersion, isAutoClose == "true"));
436                     return true;
437                 };
438
439                 IntPtr infoHandle = GetInfoHandle();
440                 if (infoHandle != null)
441                 {
442                     err = Interop.ApplicationManager.AppInfoForeachResControl(infoHandle, cb, IntPtr.Zero);
443                     if (err != Interop.ApplicationManager.ErrorCode.None)
444                     {
445                         Log.Warn(LogTag, "Failed to get the resource controls of " + _applicationId + ". err = " + err);
446                     }
447                 }
448
449                 return resourceControls;
450             }
451         }
452
453         /// <summary>
454         /// Gets the localized label of the application for the given locale.
455         /// </summary>
456         /// <param name="locale">Locale.</param>
457         /// <returns>The localized label.</returns>
458         /// <since_tizen> 3 </since_tizen>
459         public string GetLocalizedLabel(string locale)
460         {
461             string label = string.Empty;
462             err = Interop.ApplicationManager.AppInfoGetLocaledLabel(ApplicationId, locale, out label);
463             if (err != Interop.ApplicationManager.ErrorCode.None)
464             {
465                 Log.Warn(LogTag, "Failed to get the GetLocalizedLabel of " + _applicationId + ". err = " + err);
466                 label = Label;
467             }
468             return label;
469         }
470
471         private IntPtr GetInfoHandle()
472         {
473             if (_infoHandle == IntPtr.Zero)
474             {
475                 IntPtr infoHandle = IntPtr.Zero;
476                 err = Interop.ApplicationManager.AppManagerGetAppInfo(_applicationId, out infoHandle);
477                 if (err != Interop.ApplicationManager.ErrorCode.None)
478                 {
479                     Log.Warn(LogTag, "Failed to get the handle of the ApplicationInfo. err = " + err);
480                 }
481                 _infoHandle = infoHandle;
482             }
483             return _infoHandle;
484         }
485
486         /// <summary>
487         /// Releases all resources used by the ApplicationInfo class.
488         /// </summary>
489         /// <since_tizen> 3 </since_tizen>
490         public void Dispose()
491         {
492             Dispose(true);
493             GC.SuppressFinalize(this);
494         }
495
496         private void Dispose(bool disposing)
497         {
498             if (_disposed)
499                 return;
500             if (disposing)
501             {
502             }
503             if (_infoHandle != IntPtr.Zero)
504             {
505                 Interop.ApplicationManager.AppInfoDestroy(_infoHandle);
506                 _infoHandle = IntPtr.Zero;
507             }
508             _disposed = true;
509         }
510     }
511 }