summaryrefslogtreecommitdiff
path: root/Tizen.Applications.Common/Tizen.Applications/ApplicationInfo.cs
blob: f9230fc5e48502bbceefded26c5b8ff4fafb3646 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
/*
 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
 *
 * Licensed under the Apache License, Version 2.0 (the License);
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an AS IS BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

using System;
using System.Collections.Generic;

namespace Tizen.Applications
{
    /// <summary>
    /// This class provides methods and properties to get information of the application.
    /// </summary>
    public class ApplicationInfo : IDisposable
    {
        private const string LogTag = "Tizen.Applications";
        private bool _disposed = false;
        private IntPtr _infoHandle = IntPtr.Zero;
        private IntPtr _contextHandle = IntPtr.Zero;
        private string _applicationId = string.Empty;
        private Interop.ApplicationManager.ErrorCode err = Interop.ApplicationManager.ErrorCode.None;

        internal ApplicationInfo(IntPtr infoHandle)
        {
            _infoHandle = infoHandle;
        }

        internal ApplicationInfo(IntPtr infoHandle, IntPtr contextHandle)
        {
            _infoHandle = infoHandle;
            _contextHandle = contextHandle;
        }

        /// <summary>
        /// A constructor of ApplicationInfo that takes the application id.
        /// </summary>
        /// <param name="applicationId">application id.</param>
        public ApplicationInfo(string applicationId)
        {
            _applicationId = applicationId;
        }

        /// <summary>
        /// Destructor of the class
        /// </summary>
        ~ApplicationInfo()
        {
            Dispose(false);
        }

        /// <summary>
        /// Enumeration for the Application State.
        /// </summary>
        public enum AppState
        {
            /// <summary>
            /// The undefined state
            /// </summary>
            Undefined = 0,

            /// <summary>
            /// The UI application is running in the foreground.
            /// </summary>
            Foreground,

            /// <summary>
            /// The UI application is running in the background.
            /// </summary>
            Background,

            /// <summary>
            /// The Service application is running.
            /// </summary>
            Service,

            /// <summary>
            /// The application is terminated.
            /// </summary>
            Terminated,
        }

        /// <summary>
        /// Gets the application id.
        /// </summary>
        public string ApplicationId
        {
            get
            {
                if (!string.IsNullOrEmpty(_applicationId))
                    return _applicationId;
                IntPtr infoHandle = GetInfoHandle();
                string appid = string.Empty;
                if (infoHandle != IntPtr.Zero)
                {
                    err = Interop.ApplicationManager.AppInfoGetAppId(infoHandle, out appid);
                    if (err != Interop.ApplicationManager.ErrorCode.None)
                    {
                        Log.Warn(LogTag, "Failed to get the application id. err = " + err);
                    }
                }
                return appid;
            }
        }

        /// <summary>
        /// Gets the package id of the application.
        /// </summary>
        public string PackageId
        {
            get
            {
                IntPtr infoHandle = GetInfoHandle();
                string packageid = string.Empty;
                if (infoHandle != IntPtr.Zero)
                {
                    err = Interop.ApplicationManager.AppInfoGetPackage(infoHandle, out packageid);
                    if (err != Interop.ApplicationManager.ErrorCode.None)
                    {
                        Log.Warn(LogTag, "Failed to get the package id. err = " + err);
                    }
                }
                return packageid;
            }
        }

        /// <summary>
        /// Gets the label of the application.
        /// </summary>
        public string Label
        {
            get
            {
                IntPtr infoHandle = GetInfoHandle();
                string label = string.Empty;
                if (infoHandle != IntPtr.Zero)
                {
                    err = Interop.ApplicationManager.AppInfoGetLabel(infoHandle, out label);
                    if (err != Interop.ApplicationManager.ErrorCode.None)
                    {
                        Log.Warn(LogTag, "Failed to get the label. err = " + err);
                    }
                }
                return label;
            }
        }

        /// <summary>
        /// Gets the executable path of the application.
        /// </summary>
        public string ExecutablePath
        {
            get
            {
                IntPtr infoHandle = GetInfoHandle();
                string exec = string.Empty;
                if (infoHandle != IntPtr.Zero)
                {
                    err = Interop.ApplicationManager.AppInfoGetExec(infoHandle, out exec);
                    if (err != Interop.ApplicationManager.ErrorCode.None)
                    {
                        Log.Warn(LogTag, "Failed to get the executable file path. err = " + err);
                    }
                }
                return exec;
            }
        }

        /// <summary>
        /// Gets the absolute path to the icon image.
        /// </summary>
        public string IconPath
        {
            get
            {
                IntPtr infoHandle = GetInfoHandle();
                string path = string.Empty;
                if (infoHandle != IntPtr.Zero)
                {
                    err = Interop.ApplicationManager.AppInfoGetIcon(infoHandle, out path);
                    if (err != Interop.ApplicationManager.ErrorCode.None)
                    {
                        Log.Warn(LogTag, "Failed to get the icon path. err = " + err);
                    }
                }
                return path;
            }
        }

        /// <summary>
        /// Gets the application type name.
        /// </summary>
        public string ApplicationType
        {
            get
            {
                IntPtr infoHandle = GetInfoHandle();
                string type = string.Empty;
                if (infoHandle != IntPtr.Zero)
                {
                    err = Interop.ApplicationManager.AppInfoGetType(infoHandle, out type);
                    if (err != Interop.ApplicationManager.ErrorCode.None)
                    {
                        Log.Warn(LogTag, "Failed to get the application type. err = " + err);
                    }
                }
                return type;
            }
        }

        /// <summary>
        /// Gets the application's metadata.
        /// </summary>
        public IDictionary<String, String> Metadata
        {
            get
            {
                IDictionary<string, string> metadata = new Dictionary<String, String>();

                Interop.ApplicationManager.AppInfoMetadataCallback cb = (string key, string value, IntPtr userData) =>
                {
                    if (key.Length != 0)
                    {
                        metadata.Add(key, value);
                    }
                    return true;
                };

                IntPtr infoHandle = GetInfoHandle();
                if (infoHandle != IntPtr.Zero)
                {
                    err = Interop.ApplicationManager.AppInfoForeachMetadata(infoHandle, cb, IntPtr.Zero);
                    if (err != Interop.ApplicationManager.ErrorCode.None)
                    {
                        Log.Warn(LogTag, "Failed to get metadata of the application. err = " + err);
                    }
                }
                return metadata;
            }
        }

        /// <summary>
        /// Checks whether application information is nodisplay. If the application icon is not displayed on the menu screen, true; otherwise, false.
        /// </summary>
        public bool IsNoDisplay
        {
            get
            {
                IntPtr infoHandle = GetInfoHandle();
                bool nodisplay = false;
                if (infoHandle != IntPtr.Zero)
                {
                    err = Interop.ApplicationManager.AppInfoIsNodisplay(infoHandle, out nodisplay);
                    if (err != Interop.ApplicationManager.ErrorCode.None)
                    {
                        Log.Warn(LogTag, "Failed to get the IsNoDisplay value. err = " + err);

                    }
                }
                return nodisplay;
            }
        }

        /// <summary>
        /// Checks whether application is launched on booting time. If the application will be automatically start on boot, true; otherwise, false.
        /// </summary>
        public bool IsOnBoot
        {
            get
            {
                IntPtr infoHandle = GetInfoHandle();
                bool onboot = false;
                if (infoHandle != IntPtr.Zero)
                {
                    err = Interop.ApplicationManager.AppInfoIsOnBoot(infoHandle, out onboot);
                    if (err != Interop.ApplicationManager.ErrorCode.None)
                    {
                        Log.Warn(LogTag, "Failed to get the IsOnBoot value. err = " + err);
                    }
                }
                return onboot;
            }
        }

        /// <summary>
        /// Checks whether application is preloaded. If the application is preloaded, true; otherwise, false.
        /// </summary>
        public bool IsPreload
        {
            get
            {
                IntPtr infoHandle = GetInfoHandle();
                bool preloaded = false;
                if (infoHandle != IntPtr.Zero)
                {
                    err = Interop.ApplicationManager.AppInfoIsPreLoad(infoHandle, out preloaded);
                    if (err != Interop.ApplicationManager.ErrorCode.None)
                    {
                        Log.Warn(LogTag, "Failed to get the IsPreload value. err = " + err);
                    }
                }
                return preloaded;
            }
        }

        /// <summary>
        /// Gets the application's process id. If the application is not running, the value will be zero (0).
        /// </summary>
        public int ProcessId
        {
            get
            {
                int pid = 0;
                IntPtr contextHandle = GetContextHandle();
                err = Interop.ApplicationManager.AppContextGetPid(contextHandle, out pid);
                if (err != Interop.ApplicationManager.ErrorCode.None)
                {
                    Log.Warn(LogTag, "Failed to get the process id. err = " + err);
                }
                return pid;
            }
        }

        /// <summary>
        /// Checks whether the application is running. It returns the installed application running state.
        /// </summary>
        public bool IsRunning
        {
            get
            {
                bool running = false;
                err = Interop.ApplicationManager.AppManagerIsRunning(ApplicationId, out running);
                if (err != Interop.ApplicationManager.ErrorCode.None)
                {
                    Log.Warn(LogTag, "Failed to get the IsRunning value. err = " + err);
                }
                return running;
            }
        }

        /// <summary>
        /// Gets the shared data path.
        /// </summary>
        public string SharedDataPath
        {
            get
            {
                string path = string.Empty;
                err = Interop.ApplicationManager.AppManagerGetSharedDataPath(ApplicationId, out path);
                if (err != Interop.ApplicationManager.ErrorCode.None)
                {
                    Log.Warn(LogTag, "Failed to get the SharedDataPath. err = " + err);
                }
                return path;
            }
        }

        /// <summary>
        /// Gets the shared resource path.
        /// </summary>
        public string SharedResourcePath
        {
            get
            {
                string path = string.Empty;
                err = Interop.ApplicationManager.AppManagerGetSharedResourcePath(ApplicationId, out path);
                if (err != Interop.ApplicationManager.ErrorCode.None)
                {
                    Log.Warn(LogTag, "Failed to get the SharedResourcePath. err = " + err);
                }
                return path;
            }
        }

        /// <summary>
        /// Gets the shared trust path.
        /// </summary>
        public string SharedTrustedPath
        {
            get
            {
                string path = string.Empty;
                err = Interop.ApplicationManager.AppManagerGetSharedTrustedPath(ApplicationId, out path);
                if (err != Interop.ApplicationManager.ErrorCode.None)
                {
                    Log.Warn(LogTag, "Failed to get the SharedTrustedPath. err = " + err);
                }
                return path;
            }
        }

        /// <summary>
        /// Gets the external shared data path.
        /// </summary>
        public string ExternalSharedDataPath
        {
            get
            {
                string path = string.Empty;
                err = Interop.ApplicationManager.AppManagerGetExternalSharedDataPath(ApplicationId, out path);
                if (err != Interop.ApplicationManager.ErrorCode.None)
                {
                    Log.Warn(LogTag, "Failed to get the ExternalSharedDataPath. err = " + err);
                }
                return path;
            }
        }

        /// <summary>
        /// Gets the localized label of application for the given locale.
        /// </summary>
        /// <param name="locale">locale.</param>
        public string GetLocalizedLabel(string locale)
        {
            string label = string.Empty;
            err = Interop.ApplicationManager.AppInfoGetLocaledLabel(ApplicationId, locale, out label);
            if (err != Interop.ApplicationManager.ErrorCode.None)
            {
                Log.Warn(LogTag, "Failed to get the GetLocalizedLabel. err = " + err);
                label = Label;
            }
            return label;
        }

        /// <summary>
        /// Gets the state of the application.
        /// </summary>
        public AppState State
        {
            get
            {
                int value = 0;
                IntPtr contextHandle = GetContextHandle();
                err = Interop.ApplicationManager.AppContextGetAppState(contextHandle, out value);
                if (err != Interop.ApplicationManager.ErrorCode.None)
                {
                    Log.Warn(LogTag, "Failed to get the app state. err = " + err);
                }
                return (AppState)value;
            }
        }

        /// <summary>
        /// Checks whether the application is running as a sub application of the application group.
        /// </summary>
        public bool IsSubApp
        {
            get
            {
                bool value = false;
                IntPtr contextHandle = GetContextHandle();
                err = Interop.ApplicationManager.AppContextIsSubApp(contextHandle, out value);
                if (err != Interop.ApplicationManager.ErrorCode.None)
                {
                    Log.Warn(LogTag, "Failed to get IsSubApp. err = " + err);
                }
                return value;
            }
        }

        private IntPtr GetContextHandle()
        {
            if (_contextHandle == IntPtr.Zero)
            {
                IntPtr contextHandle = IntPtr.Zero;
                err = Interop.ApplicationManager.AppManagerGetAppContext(_applicationId, out contextHandle);
                if (err != Interop.ApplicationManager.ErrorCode.None)
                {
                    Log.Warn(LogTag, "Failed to get the handle of the ApplicationContext. err = " + err);
                }
                _contextHandle = contextHandle;
            }
            return _contextHandle;
        }

        private IntPtr GetInfoHandle()
        {
            if (_infoHandle == IntPtr.Zero)
            {
                IntPtr infoHandle = IntPtr.Zero;
                err = Interop.ApplicationManager.AppManagerGetAppInfo(_applicationId, out infoHandle);
                if (err != Interop.ApplicationManager.ErrorCode.None)
                {
                    Log.Warn(LogTag, "Failed to get the handle of the ApplicationInfo. err = " + err);
                }
                _infoHandle = infoHandle;
            }
            return _infoHandle;
        }

        /// <summary>
        /// Releases all resources used by the ApplicationInfo class.
        /// </summary>
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        private void Dispose(bool disposing)
        {
            if (_disposed)
                return;
            if (disposing)
            {
            }
            if (_infoHandle != IntPtr.Zero)
            {
                Interop.ApplicationManager.AppInfoDestroy(_infoHandle);
                _infoHandle = IntPtr.Zero;
            }
            if (_contextHandle != IntPtr.Zero)
            {
                Interop.ApplicationManager.AppContextDestroy(_contextHandle);
                _contextHandle = IntPtr.Zero;
            }
            _disposed = true;
        }
    }
}