Release 4.0.0-preview1-00051
[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     public class ApplicationInfo : IDisposable
26     {
27         private const string LogTag = "Tizen.Applications";
28         private bool _disposed = false;
29         private IntPtr _infoHandle = IntPtr.Zero;
30         private string _applicationId = string.Empty;
31         private Interop.ApplicationManager.ErrorCode err = Interop.ApplicationManager.ErrorCode.None;
32
33         internal ApplicationInfo(IntPtr infoHandle)
34         {
35             err = Interop.ApplicationManager.AppInfoGetAppId(infoHandle, out _applicationId);
36             if (err != Interop.ApplicationManager.ErrorCode.None)
37             {
38                 throw new ArgumentException("Invalid native handle.");
39             }
40             _infoHandle = infoHandle;
41         }
42
43         /// <summary>
44         /// A constructor of ApplicationInfo that takes the application id.
45         /// </summary>
46         /// <param name="applicationId">application id.</param>
47         public ApplicationInfo(string applicationId)
48         {
49             _applicationId = applicationId;
50         }
51
52         /// <summary>
53         /// Destructor of the class
54         /// </summary>
55         ~ApplicationInfo()
56         {
57             Dispose(false);
58         }
59
60         /// <summary>
61         /// Gets the application id.
62         /// </summary>
63         public string ApplicationId
64         {
65             get
66             {
67                 if (!string.IsNullOrEmpty(_applicationId))
68                     return _applicationId;
69                 IntPtr infoHandle = GetInfoHandle();
70                 string appid = string.Empty;
71                 if (infoHandle != IntPtr.Zero)
72                 {
73                     err = Interop.ApplicationManager.AppInfoGetAppId(infoHandle, out appid);
74                     if (err != Interop.ApplicationManager.ErrorCode.None)
75                     {
76                         Log.Warn(LogTag, "Failed to get the application id. err = " + err);
77                     }
78                 }
79                 return appid;
80             }
81         }
82
83         /// <summary>
84         /// Gets the package id of the application.
85         /// </summary>
86         public string PackageId
87         {
88             get
89             {
90                 IntPtr infoHandle = GetInfoHandle();
91                 string packageid = string.Empty;
92                 if (infoHandle != IntPtr.Zero)
93                 {
94                     err = Interop.ApplicationManager.AppInfoGetPackage(infoHandle, out packageid);
95                     if (err != Interop.ApplicationManager.ErrorCode.None)
96                     {
97                         Log.Warn(LogTag, "Failed to get the package id of " + _applicationId + ". err = " + err);
98                     }
99                 }
100                 return packageid;
101             }
102         }
103
104         /// <summary>
105         /// Gets the label of the application.
106         /// </summary>
107         public string Label
108         {
109             get
110             {
111                 IntPtr infoHandle = GetInfoHandle();
112                 string label = string.Empty;
113                 if (infoHandle != IntPtr.Zero)
114                 {
115                     err = Interop.ApplicationManager.AppInfoGetLabel(infoHandle, out label);
116                     if (err != Interop.ApplicationManager.ErrorCode.None)
117                     {
118                         Log.Warn(LogTag, "Failed to get the app label of " + _applicationId + ". err = " + err);
119                     }
120                 }
121                 return label;
122             }
123         }
124
125         /// <summary>
126         /// Gets the executable path of the application.
127         /// </summary>
128         public string ExecutablePath
129         {
130             get
131             {
132                 IntPtr infoHandle = GetInfoHandle();
133                 string exec = string.Empty;
134                 if (infoHandle != IntPtr.Zero)
135                 {
136                     err = Interop.ApplicationManager.AppInfoGetExec(infoHandle, out exec);
137                     if (err != Interop.ApplicationManager.ErrorCode.None)
138                     {
139                         Log.Warn(LogTag, "Failed to get the executable file path of " + _applicationId + ". err = " + err);
140                     }
141                 }
142                 return exec;
143             }
144         }
145
146         /// <summary>
147         /// Gets the absolute path to the icon image.
148         /// </summary>
149         public string IconPath
150         {
151             get
152             {
153                 IntPtr infoHandle = GetInfoHandle();
154                 string path = string.Empty;
155                 if (infoHandle != IntPtr.Zero)
156                 {
157                     err = Interop.ApplicationManager.AppInfoGetIcon(infoHandle, out path);
158                     if (err != Interop.ApplicationManager.ErrorCode.None)
159                     {
160                         Log.Warn(LogTag, "Failed to get the app icon path of " + _applicationId + ". err = " + err);
161                     }
162                 }
163                 return path;
164             }
165         }
166
167         /// <summary>
168         /// Gets the application type name.
169         /// </summary>
170         public string ApplicationType
171         {
172             get
173             {
174                 IntPtr infoHandle = GetInfoHandle();
175                 string type = string.Empty;
176                 if (infoHandle != IntPtr.Zero)
177                 {
178                     err = Interop.ApplicationManager.AppInfoGetType(infoHandle, out type);
179                     if (err != Interop.ApplicationManager.ErrorCode.None)
180                     {
181                         Log.Warn(LogTag, "Failed to get the application type of " + _applicationId + ". err = " + err);
182                     }
183                 }
184                 return type;
185             }
186         }
187
188         /// <summary>
189         /// Gets the application's metadata.
190         /// </summary>
191         public IDictionary<String, String> Metadata
192         {
193             get
194             {
195                 IDictionary<string, string> metadata = new Dictionary<String, String>();
196
197                 Interop.ApplicationManager.AppInfoMetadataCallback cb = (string key, string value, IntPtr userData) =>
198                 {
199                     if (key.Length != 0)
200                     {
201                         metadata.Add(key, value);
202                     }
203                     return true;
204                 };
205
206                 IntPtr infoHandle = GetInfoHandle();
207                 if (infoHandle != IntPtr.Zero)
208                 {
209                     err = Interop.ApplicationManager.AppInfoForeachMetadata(infoHandle, cb, IntPtr.Zero);
210                     if (err != Interop.ApplicationManager.ErrorCode.None)
211                     {
212                         Log.Warn(LogTag, "Failed to get application metadata of " + _applicationId + ". err = " + err);
213                     }
214                 }
215                 return metadata;
216             }
217         }
218
219         /// <summary>
220         /// Checks whether application information is nodisplay. If the application icon is not displayed on the menu screen, true; otherwise, false.
221         /// </summary>
222         public bool IsNoDisplay
223         {
224             get
225             {
226                 IntPtr infoHandle = GetInfoHandle();
227                 bool nodisplay = false;
228                 if (infoHandle != IntPtr.Zero)
229                 {
230                     err = Interop.ApplicationManager.AppInfoIsNodisplay(infoHandle, out nodisplay);
231                     if (err != Interop.ApplicationManager.ErrorCode.None)
232                     {
233                         Log.Warn(LogTag, "Failed to get the IsNoDisplay value of " + _applicationId + ". err = " + err);
234
235                     }
236                 }
237                 return nodisplay;
238             }
239         }
240
241         /// <summary>
242         /// Checks whether application is launched on booting time. If the application will be automatically start on boot, true; otherwise, false.
243         /// </summary>
244         public bool IsOnBoot
245         {
246             get
247             {
248                 IntPtr infoHandle = GetInfoHandle();
249                 bool onboot = false;
250                 if (infoHandle != IntPtr.Zero)
251                 {
252                     err = Interop.ApplicationManager.AppInfoIsOnBoot(infoHandle, out onboot);
253                     if (err != Interop.ApplicationManager.ErrorCode.None)
254                     {
255                         Log.Warn(LogTag, "Failed to get the IsOnBoot value of " + _applicationId + ". err = " + err);
256                     }
257                 }
258                 return onboot;
259             }
260         }
261
262         /// <summary>
263         /// Checks whether application is preloaded. If the application is preloaded, true; otherwise, false.
264         /// </summary>
265         public bool IsPreload
266         {
267             get
268             {
269                 IntPtr infoHandle = GetInfoHandle();
270                 bool preloaded = false;
271                 if (infoHandle != IntPtr.Zero)
272                 {
273                     err = Interop.ApplicationManager.AppInfoIsPreLoad(infoHandle, out preloaded);
274                     if (err != Interop.ApplicationManager.ErrorCode.None)
275                     {
276                         Log.Warn(LogTag, "Failed to get the IsPreload value of " + _applicationId + ". err = " + err);
277                     }
278                 }
279                 return preloaded;
280             }
281         }
282
283         /// <summary>
284         /// Gets the shared data path.
285         /// </summary>
286         public string SharedDataPath
287         {
288             get
289             {
290                 string path = string.Empty;
291                 err = Interop.ApplicationManager.AppManagerGetSharedDataPath(ApplicationId, out path);
292                 if (err != Interop.ApplicationManager.ErrorCode.None)
293                 {
294                     Log.Warn(LogTag, "Failed to get the SharedDataPath of " + _applicationId + ". err = " + err);
295                 }
296                 return path;
297             }
298         }
299
300         /// <summary>
301         /// Gets the shared resource path.
302         /// </summary>
303         public string SharedResourcePath
304         {
305             get
306             {
307                 string path = string.Empty;
308                 err = Interop.ApplicationManager.AppManagerGetSharedResourcePath(ApplicationId, out path);
309                 if (err != Interop.ApplicationManager.ErrorCode.None)
310                 {
311                     Log.Warn(LogTag, "Failed to get the SharedResourcePath of " + _applicationId + ". err = " + err);
312                 }
313                 return path;
314             }
315         }
316
317         /// <summary>
318         /// Gets the shared trust path.
319         /// </summary>
320         public string SharedTrustedPath
321         {
322             get
323             {
324                 string path = string.Empty;
325                 err = Interop.ApplicationManager.AppManagerGetSharedTrustedPath(ApplicationId, out path);
326                 if (err != Interop.ApplicationManager.ErrorCode.None)
327                 {
328                     Log.Warn(LogTag, "Failed to get the SharedTrustedPath of " + _applicationId + ". err = " + err);
329                 }
330                 return path;
331             }
332         }
333
334         /// <summary>
335         /// Gets the external shared data path.
336         /// </summary>
337         public string ExternalSharedDataPath
338         {
339             get
340             {
341                 string path = string.Empty;
342                 err = Interop.ApplicationManager.AppManagerGetExternalSharedDataPath(ApplicationId, out path);
343                 if (err != Interop.ApplicationManager.ErrorCode.None)
344                 {
345                     Log.Warn(LogTag, "Failed to get the ExternalSharedDataPath of " + _applicationId + ". err = " + err);
346                 }
347                 return path;
348             }
349         }
350
351         /// <summary>
352         /// Gets the localized label of application for the given locale.
353         /// </summary>
354         /// <param name="locale">locale.</param>
355         public string GetLocalizedLabel(string locale)
356         {
357             string label = string.Empty;
358             err = Interop.ApplicationManager.AppInfoGetLocaledLabel(ApplicationId, locale, out label);
359             if (err != Interop.ApplicationManager.ErrorCode.None)
360             {
361                 Log.Warn(LogTag, "Failed to get the GetLocalizedLabel of " + _applicationId + ". err = " + err);
362                 label = Label;
363             }
364             return label;
365         }
366
367         private IntPtr GetInfoHandle()
368         {
369             if (_infoHandle == IntPtr.Zero)
370             {
371                 IntPtr infoHandle = IntPtr.Zero;
372                 err = Interop.ApplicationManager.AppManagerGetAppInfo(_applicationId, out infoHandle);
373                 if (err != Interop.ApplicationManager.ErrorCode.None)
374                 {
375                     Log.Warn(LogTag, "Failed to get the handle of the ApplicationInfo. err = " + err);
376                 }
377                 _infoHandle = infoHandle;
378             }
379             return _infoHandle;
380         }
381
382         /// <summary>
383         /// Releases all resources used by the ApplicationInfo class.
384         /// </summary>
385         public void Dispose()
386         {
387             Dispose(true);
388             GC.SuppressFinalize(this);
389         }
390
391         private void Dispose(bool disposing)
392         {
393             if (_disposed)
394                 return;
395             if (disposing)
396             {
397             }
398             if (_infoHandle != IntPtr.Zero)
399             {
400                 Interop.ApplicationManager.AppInfoDestroy(_infoHandle);
401                 _infoHandle = IntPtr.Zero;
402             }
403             _disposed = true;
404         }
405     }
406 }