2 * Copyright (c) 2018 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 using System.Collections.Generic;
19 using System.Runtime.InteropServices;
20 using System.Threading.Tasks;
22 namespace Tizen.Applications
25 /// This class provides the methods and properties to get information about the packages.
27 /// <since_tizen> 3 </since_tizen>
30 private const string LogTag = "Tizen.Applications";
32 private string _id = string.Empty;
33 private string _label = string.Empty;
34 private string _iconPath = string.Empty;
35 private string _version = string.Empty;
36 private PackageType _type;
37 private Interop.PackageManager.StorageType _installedStorageType;
38 private string _rootPath = string.Empty;
39 private string _expansionPackageName = string.Empty;
40 private bool _isSystemPackage;
41 private bool _isRemovable;
42 private bool _isPreloaded;
43 private bool _isAccessible;
44 private IReadOnlyDictionary<CertificateType, PackageCertificate> _certificates;
45 private List<string> _privileges;
46 private int _installedTime;
48 private Dictionary<IntPtr, Interop.PackageManager.PackageManagerSizeInfoCallback> _packageManagerSizeInfoCallbackDict = new Dictionary<IntPtr, Interop.PackageManager.PackageManagerSizeInfoCallback>();
49 private int _callbackId = 0;
51 private Package(string pkgId)
59 /// <since_tizen> 3 </since_tizen>
60 public string Id { get { return _id; } }
63 /// Label of the package.
65 /// <since_tizen> 3 </since_tizen>
66 public string Label { get { return _label; } }
69 /// Absolute path to the icon image.
71 /// <since_tizen> 3 </since_tizen>
72 public string IconPath { get { return _iconPath; } }
75 /// Version of the package.
77 /// <since_tizen> 3 </since_tizen>
78 public string Version { get { return _version; } }
81 /// Type of the package.
83 /// <since_tizen> 3 </since_tizen>
84 public PackageType PackageType { get { return _type; } }
87 /// Installed storage type for the package.
89 /// <since_tizen> 3 </since_tizen>
90 public StorageType InstalledStorageType { get { return (StorageType)_installedStorageType; } }
93 /// Root path for the package.
95 /// <since_tizen> 3 </since_tizen>
96 public string RootPath { get { return _rootPath; } }
99 /// Expansion package name for the package.
101 /// <privilege>http://tizen.org/privilege/packagemanager.admin</privilege>
102 /// <privlevel>platform</privlevel>
103 /// <since_tizen> 3 </since_tizen>
104 public string TizenExpansionPackageName { get { return _expansionPackageName; } }
107 /// Checks whether the package is a system package.
109 /// <since_tizen> 3 </since_tizen>
110 public bool IsSystemPackage { get { return _isSystemPackage; } }
113 /// Checks whether the package is removable.
115 /// <since_tizen> 3 </since_tizen>
116 public bool IsRemovable { get { return _isRemovable; } }
119 /// Checks whether the package is preloaded.
121 /// <since_tizen> 3 </since_tizen>
122 public bool IsPreloaded { get { return _isPreloaded; } }
125 /// Checks whether the current package is accessible.
127 /// <since_tizen> 3 </since_tizen>
128 public bool IsAccessible { get { return _isAccessible; } }
131 /// Certificate information for the package.
133 /// <since_tizen> 3 </since_tizen>
134 public IReadOnlyDictionary<CertificateType, PackageCertificate> Certificates { get { return _certificates; } }
137 /// Requested privilege for the package.
139 /// <since_tizen> 3 </since_tizen>
140 public IEnumerable<string> Privileges { get { return _privileges; } }
143 /// Installed time of the package.
145 /// <since_tizen> 3 </since_tizen>
146 public int InstalledTime { get { return _installedTime; } }
149 /// Retrieves all the application IDs of this package.
151 /// <returns>Returns a dictionary containing all the application information for a given application type.</returns>
152 /// <since_tizen> 3 </since_tizen>
153 public IEnumerable<ApplicationInfo> GetApplications()
155 return GetApplications(ApplicationType.All);
159 /// Retrieves all the application IDs of this package.
161 /// <param name="type">Optional: AppType enumeration value.</param>
162 /// <returns>Returns a dictionary containing all the application information for a given application type.</returns>
163 /// <since_tizen> 3 </since_tizen>
164 public IEnumerable<ApplicationInfo> GetApplications(ApplicationType type)
166 List<ApplicationInfo> appInfoList = new List<ApplicationInfo>();
167 Interop.Package.PackageInfoAppInfoCallback cb = (Interop.Package.AppType appType, string appId, IntPtr userData) =>
169 appInfoList.Add(new ApplicationInfo(appId));
173 IntPtr packageInfoHandle;
174 Interop.PackageManager.ErrorCode err = Interop.Package.PackageInfoCreate(Id, out packageInfoHandle);
175 if (err != Interop.PackageManager.ErrorCode.None)
177 Log.Warn(LogTag, string.Format("Failed to create native handle for package info of {0}. err = {1}", Id, err));
180 err = Interop.Package.PackageInfoForeachAppInfo(packageInfoHandle, (Interop.Package.AppType)type, cb, IntPtr.Zero);
181 if (err != Interop.PackageManager.ErrorCode.None)
183 Log.Warn(LogTag, string.Format("Failed to application info of {0}. err = {1}", Id, err));
186 err = Interop.Package.PackageInfoDestroy(packageInfoHandle);
187 if (err != Interop.PackageManager.ErrorCode.None)
189 Log.Warn(LogTag, string.Format("Failed to destroy native handle for package info of {0}. err = {1}", Id, err));
195 /// Retrieves all the application IDs of this package.
197 /// <param name="componentType">Optional: AppType enumeration value.</param>
198 /// <returns>Returns a dictionary containing all the application information for a given application type.</returns>
199 /// <since_tizen> 6 </since_tizen>
200 public IEnumerable<ApplicationInfo> GetApplications(ApplicationComponentType componentType)
202 return GetApplications(ToApplicationType(componentType));
206 /// Gets the package size information.
208 /// <returns>Package size information.</returns>
209 /// <privilege>http://tizen.org/privilege/packagemanager.info</privilege>
210 /// <since_tizen> 3 </since_tizen>
211 public async Task<PackageSizeInformation> GetSizeInformationAsync()
213 TaskCompletionSource<PackageSizeInformation> tcs = new TaskCompletionSource<PackageSizeInformation>();
214 Interop.PackageManager.PackageManagerSizeInfoCallback sizeInfoCb = (pkgId, sizeInfoHandle, userData) =>
216 if (sizeInfoHandle != IntPtr.Zero && Id == pkgId)
218 var pkgSizeInfo = PackageSizeInformation.GetPackageSizeInformation(sizeInfoHandle);
219 tcs.TrySetResult(pkgSizeInfo);
222 lock (_packageManagerSizeInfoCallbackDict)
224 _packageManagerSizeInfoCallbackDict.Remove(userData);
229 lock (_packageManagerSizeInfoCallbackDict)
231 callbackId = (IntPtr)_callbackId++;
232 _packageManagerSizeInfoCallbackDict[callbackId] = sizeInfoCb;
235 Interop.PackageManager.ErrorCode err = Interop.PackageManager.PackageManagerGetSizeInfo(Id, sizeInfoCb, callbackId);
236 if (err != Interop.PackageManager.ErrorCode.None)
238 tcs.TrySetException(PackageManagerErrorFactory.GetException(err, "Failed to get total package size info of " + Id));
240 return await tcs.Task.ConfigureAwait(false);
244 /// Compares the certificate information with the given package ID.
246 /// <param name="packageId">ID of the package.</param>
247 /// <returns>Certificate comparison result.</returns>
248 /// <exception cref="ArgumentException">Thrown when a failed input package ID is invalid.</exception>
249 /// <exception cref="System.IO.IOException">Thrown when the method failed due to an internal I/O error.</exception>
250 /// <since_tizen> 3 </since_tizen>
251 public CertCompareResultType CompareCertInfo(string packageId)
253 Interop.PackageManager.CertCompareResultType compareResult;
254 Interop.PackageManager.ErrorCode err = Interop.PackageManager.PackageManagerCompareCertInfo(Id, packageId, out compareResult);
255 if (err != Interop.PackageManager.ErrorCode.None)
257 throw PackageManagerErrorFactory.GetException(err, "Failed to compare package cert info");
260 return (CertCompareResultType)compareResult;
263 // This method assumes that given arguments are already validated and have valid values.
264 internal static Package CreatePackage(IntPtr handle, string pkgId)
266 Package package = new Package(pkgId);
268 var err = Interop.PackageManager.ErrorCode.None;
269 err = Interop.Package.PackageInfoGetLabel(handle, out package._label);
270 if (err != Interop.PackageManager.ErrorCode.None)
272 Log.Warn(LogTag, "Failed to get package label of " + pkgId);
274 err = Interop.Package.PackageInfoGetIconPath(handle, out package._iconPath);
275 if (err != Interop.PackageManager.ErrorCode.None)
277 Log.Warn(LogTag, "Failed to get package icon path of " + pkgId);
279 err = Interop.Package.PackageInfoGetVersion(handle, out package._version);
280 if (err != Interop.PackageManager.ErrorCode.None)
282 Log.Warn(LogTag, "Failed to get package version of " + pkgId);
286 Interop.Package.PackageInfoGetType(handle, out type);
287 if (Enum.TryParse(type, true, out package._type) == false)
289 Log.Warn(LogTag, "Failed to get package type of " + pkgId);
291 err = Interop.Package.PackageInfoGetRootPath(handle, out package._rootPath);
292 if (err != Interop.PackageManager.ErrorCode.None)
294 Log.Warn(LogTag, "Failed to get package root directory of " + pkgId);
296 err = Interop.Package.PackageInfoGetTepName(handle, out package._expansionPackageName);
297 if (err != Interop.PackageManager.ErrorCode.None)
299 Log.Warn(LogTag, "Failed to get expansion package name of " + pkgId);
300 package._expansionPackageName = string.Empty;
303 err = Interop.Package.PackageInfoGetInstalledStorage(handle, out package._installedStorageType);
304 if (err != Interop.PackageManager.ErrorCode.None)
306 Log.Warn(LogTag, "Failed to get installed storage type of " + pkgId);
308 Interop.Package.PackageInfoIsSystemPackage(handle, out package._isSystemPackage);
309 if (err != Interop.PackageManager.ErrorCode.None)
311 Log.Warn(LogTag, "Failed to get whether package " + pkgId + " is system package or not");
313 Interop.Package.PackageInfoIsRemovablePackage(handle, out package._isRemovable);
314 if (err != Interop.PackageManager.ErrorCode.None)
316 Log.Warn(LogTag, "Failed to get whether package " + pkgId + " is removable or not");
318 Interop.Package.PackageInfoIsPreloadPackage(handle, out package._isPreloaded);
319 if (err != Interop.PackageManager.ErrorCode.None)
321 Log.Warn(LogTag, "Failed to get whether package " + pkgId + " is preloaded or not");
323 Interop.Package.PackageInfoIsAccessible(handle, out package._isAccessible);
324 if (err != Interop.PackageManager.ErrorCode.None)
326 Log.Warn(LogTag, "Failed to get whether package " + pkgId + " is accessible or not");
330 Interop.Package.PackageInfoGetInstalledTime(handle, out package._installedTime);
331 if (err != Interop.PackageManager.ErrorCode.None)
333 Log.Warn(LogTag, "Failed to get installed time of " + pkgId);
336 catch (TypeLoadException)
338 // To support in API vesion 3.0
339 package._installedTime = 0;
342 package._certificates = PackageCertificate.GetPackageCertificates(handle);
343 package._privileges = GetPackagePrivilegeInformation(handle);
347 internal static Package GetPackage(string packageId)
349 IntPtr packageInfoHandle;
350 Interop.PackageManager.ErrorCode err = Interop.Package.PackageInfoCreate(packageId, out packageInfoHandle);
351 if (err != Interop.PackageManager.ErrorCode.None)
353 throw PackageManagerErrorFactory.GetException(err, string.Format("Failed to create native handle for package info of {0}", packageId));
356 Package package = CreatePackage(packageInfoHandle, packageId);
358 err = Interop.Package.PackageInfoDestroy(packageInfoHandle);
359 if (err != Interop.PackageManager.ErrorCode.None)
361 Log.Warn(LogTag, string.Format("Failed to destroy native handle for package info of {0}. err = {1}", packageId, err));
366 internal static Package GetPackage(IntPtr packageInfoHandle)
369 Interop.PackageManager.ErrorCode err = Interop.Package.PackageInfoGetPackage(packageInfoHandle, out packageId);
370 if (err != Interop.PackageManager.ErrorCode.None)
372 throw PackageManagerErrorFactory.GetException(err, "Failed to get package id for given package handle.");
374 return CreatePackage(packageInfoHandle, packageId);
377 private static List<string> GetPackagePrivilegeInformation(IntPtr packageInfoHandle)
379 List<string> privileges = new List<string>();
380 Interop.Package.PackageInfoPrivilegeInfoCallback privilegeInfoCb = (privilege, userData) =>
382 privileges.Add(privilege);
386 Interop.PackageManager.ErrorCode err = Interop.Package.PackageInfoForeachPrivilegeInfo(packageInfoHandle, privilegeInfoCb, IntPtr.Zero);
387 if (err != Interop.PackageManager.ErrorCode.None)
389 Log.Warn(LogTag, string.Format("Failed to get privilage info. err = {0}", err));
394 private ApplicationType ToApplicationType(ApplicationComponentType componentType)
396 ApplicationType applicationType = 0;
397 if (componentType == Tizen.Applications.ApplicationComponentType.UIApplication)
398 applicationType = Tizen.Applications.ApplicationType.Ui;
399 else if (componentType == Tizen.Applications.ApplicationComponentType.ServiceApplication)
400 applicationType = Tizen.Applications.ApplicationType.Service;
401 else if (componentType == Tizen.Applications.ApplicationComponentType.WidgetApplication)
402 applicationType = Tizen.Applications.ApplicationType.Widget;
403 else if (componentType == Tizen.Applications.ApplicationComponentType.WatchApplication)
404 applicationType = Tizen.Applications.ApplicationType.Watch;
406 return applicationType;