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