2 * Copyright (c) 2016 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 provide methods and properties to get information about packages.
29 private const string LogTag = "Tizen.Applications";
31 private string _id = string.Empty;
32 private string _label = string.Empty;
33 private string _iconPath = string.Empty;
34 private string _version = string.Empty;
35 private PackageType _type;
36 private Interop.PackageManager.StorageType _installedStorageType;
37 private string _rootPath = string.Empty;
38 private string _expansionPackageName = string.Empty;
39 private bool _isSystemPackage;
40 private bool _isRemovable;
41 private bool _isPreloaded;
42 private bool _isAccessible;
43 private IReadOnlyDictionary<CertificateType, PackageCertificate> _certificates;
44 private List<string> _privileges;
45 private int _installedTime;
47 private Package(string pkgId)
55 public string Id { get { return _id; } }
58 /// Label of the package.
60 public string Label { get { return _label; } }
63 /// Absolute path to the icon image.
65 public string IconPath { get { return _iconPath; } }
68 /// Version of the package.
70 public string Version { get { return _version; } }
73 /// Type of the package.
75 public PackageType PackageType { get { return _type; } }
78 /// Installed storage type for the package.
80 public StorageType InstalledStorageType { get { return (StorageType)_installedStorageType; } }
83 /// Root path for the package.
85 public string RootPath { get { return _rootPath; } }
88 /// Expansion package name for the package.
90 /// <privilege>http://tizen.org/privilege/packagemanager.admin</privilege>
91 /// <privlevel>platform</privlevel>
92 public string TizenExpansionPackageName { get { return _expansionPackageName; } }
95 /// Checks whether the package is system package.
97 public bool IsSystemPackage { get { return _isSystemPackage; } }
100 /// Checks whether the package is removable.
102 public bool IsRemovable { get { return _isRemovable; } }
105 /// Checks whether the package is preloaded.
107 public bool IsPreloaded { get { return _isPreloaded; } }
110 /// Checks whether the current package is accessible.
112 public bool IsAccessible { get { return _isAccessible; } }
115 /// Certificate information for the package
117 public IReadOnlyDictionary<CertificateType, PackageCertificate> Certificates { get { return _certificates; } }
120 /// Requested privilege for the package
122 public IEnumerable<string> Privileges { get { return _privileges; } }
125 /// Installed time of the package.
127 public int InstalledTime { get { return _installedTime; } }
130 /// Retrieves all application IDs of this package.
132 /// <returns>Returns a dictionary containing all application info for given application type.</returns>
133 public IEnumerable<ApplicationInfo> GetApplications()
135 return GetApplications(ApplicationType.All);
139 /// Retrieves all application IDs of this package.
141 /// <param name="type">Optional: AppType enum value</param>
142 /// <returns>Returns a dictionary containing all application info for given application type.</returns>
143 public IEnumerable<ApplicationInfo> GetApplications(ApplicationType type)
145 List<ApplicationInfo> appInfoList = new List<ApplicationInfo>();
146 Interop.Package.PackageInfoAppInfoCallback cb = (Interop.Package.AppType appType, string appId, IntPtr userData) =>
148 appInfoList.Add(new ApplicationInfo(appId));
152 IntPtr packageInfoHandle;
153 Interop.PackageManager.ErrorCode err = Interop.Package.PackageInfoCreate(Id, out packageInfoHandle);
154 if (err != Interop.PackageManager.ErrorCode.None)
156 Log.Warn(LogTag, string.Format("Failed to create native handle for package info of {0}. err = {1}", Id, err));
159 err = Interop.Package.PackageInfoForeachAppInfo(packageInfoHandle, (Interop.Package.AppType)type, cb, IntPtr.Zero);
160 if (err != Interop.PackageManager.ErrorCode.None)
162 Log.Warn(LogTag, string.Format("Failed to application info of {0}. err = {1}", Id, err));
165 err = Interop.Package.PackageInfoDestroy(packageInfoHandle);
166 if (err != Interop.PackageManager.ErrorCode.None)
168 Log.Warn(LogTag, string.Format("Failed to destroy native handle for package info of {0}. err = {1}", Id, err));
174 /// Gets size information for this package.
176 /// <returns>package size information</returns>
177 /// <privilege>http://tizen.org/privilege/packagemanager.info</privilege>
178 public async Task<PackageSizeInformation> GetSizeInformationAsync()
180 TaskCompletionSource<PackageSizeInformation> tcs = new TaskCompletionSource<PackageSizeInformation>();
181 Interop.PackageManager.PackageManagerSizeInfoCallback sizeInfoCb = (pkgId, sizeInfoHandle, userData) =>
183 if (sizeInfoHandle != IntPtr.Zero && Id == pkgId)
185 var pkgSizeInfo = PackageSizeInformation.GetPackageSizeInformation(sizeInfoHandle);
186 tcs.TrySetResult(pkgSizeInfo);
190 Interop.PackageManager.ErrorCode err = Interop.PackageManager.PackageManagerGetSizeInfo(Id, sizeInfoCb, IntPtr.Zero);
191 if (err != Interop.PackageManager.ErrorCode.None)
193 tcs.TrySetException(PackageManagerErrorFactory.GetException(err, "Failed to get total package size info of " + Id));
195 return await tcs.Task.ConfigureAwait(false);
199 /// Compare certificate information with given package id.
201 /// <param name="packageId">Id of the package</param>
202 /// <returns>Certificate comparison result</returns>
203 /// <exception cref="ArgumentException">Thrown when failed when input package ID is invalid</exception>
204 /// <exception cref="System.IO.IOException">Thrown when method failed due to internal IO error</exception>
205 public CertCompareResultType CompareCertInfo(string packageId)
207 Interop.PackageManager.CertCompareResultType compareResult;
208 Interop.PackageManager.ErrorCode err = Interop.PackageManager.PackageManagerCompareCertInfo(Id, packageId, out compareResult);
209 if (err != Interop.PackageManager.ErrorCode.None)
211 throw PackageManagerErrorFactory.GetException(err, "Failed to compare package cert info");
214 return (CertCompareResultType)compareResult;
217 // This method assumes that given arguments are already validated and have valid values.
218 internal static Package CreatePackage(IntPtr handle, string pkgId)
220 Package package = new Package(pkgId);
222 var err = Interop.PackageManager.ErrorCode.None;
223 err = Interop.Package.PackageInfoGetLabel(handle, out package._label);
224 if (err != Interop.PackageManager.ErrorCode.None)
226 Log.Warn(LogTag, "Failed to get package label of " + pkgId);
228 err = Interop.Package.PackageInfoGetIconPath(handle, out package._iconPath);
229 if (err != Interop.PackageManager.ErrorCode.None)
231 Log.Warn(LogTag, "Failed to get package icon path of " + pkgId);
233 err = Interop.Package.PackageInfoGetVersion(handle, out package._version);
234 if (err != Interop.PackageManager.ErrorCode.None)
236 Log.Warn(LogTag, "Failed to get package version of " + pkgId);
240 Interop.Package.PackageInfoGetType(handle, out type);
241 if (Enum.TryParse(type, true, out package._type) == false)
243 Log.Warn(LogTag, "Failed to get package type of " + pkgId);
245 err = Interop.Package.PackageInfoGetRootPath(handle, out package._rootPath);
246 if (err != Interop.PackageManager.ErrorCode.None)
248 Log.Warn(LogTag, "Failed to get package root directory of " + pkgId);
250 err = Interop.Package.PackageInfoGetTepName(handle, out package._expansionPackageName);
251 if (err != Interop.PackageManager.ErrorCode.None)
253 Log.Warn(LogTag, "Failed to get expansion package name of " + pkgId);
254 package._expansionPackageName = string.Empty;
257 err = Interop.Package.PackageInfoGetInstalledStorage(handle, out package._installedStorageType);
258 if (err != Interop.PackageManager.ErrorCode.None)
260 Log.Warn(LogTag, "Failed to get installed storage type of " + pkgId);
262 Interop.Package.PackageInfoIsSystemPackage(handle, out package._isSystemPackage);
263 if (err != Interop.PackageManager.ErrorCode.None)
265 Log.Warn(LogTag, "Failed to get whether package " + pkgId + " is system package or not");
267 Interop.Package.PackageInfoIsRemovablePackage(handle, out package._isRemovable);
268 if (err != Interop.PackageManager.ErrorCode.None)
270 Log.Warn(LogTag, "Failed to get whether package " + pkgId + " is removable or not");
272 Interop.Package.PackageInfoIsPreloadPackage(handle, out package._isPreloaded);
273 if (err != Interop.PackageManager.ErrorCode.None)
275 Log.Warn(LogTag, "Failed to get whether package " + pkgId + " is preloaded or not");
277 Interop.Package.PackageInfoIsAccessible(handle, out package._isAccessible);
278 if (err != Interop.PackageManager.ErrorCode.None)
280 Log.Warn(LogTag, "Failed to get whether package " + pkgId + " is accessible or not");
284 Interop.Package.PackageInfoGetInstalledTime(handle, out package._installedTime);
285 if (err != Interop.PackageManager.ErrorCode.None)
287 Log.Warn(LogTag, "Failed to get installed time of " + pkgId);
290 catch (TypeLoadException)
292 // To support in API vesion 3.0
293 package._installedTime = 0;
296 package._certificates = PackageCertificate.GetPackageCertificates(handle);
297 package._privileges = GetPackagePrivilegeInformation(handle);
301 internal static Package GetPackage(string packageId)
303 IntPtr packageInfoHandle;
304 Interop.PackageManager.ErrorCode err = Interop.Package.PackageInfoCreate(packageId, out packageInfoHandle);
305 if (err != Interop.PackageManager.ErrorCode.None)
307 throw PackageManagerErrorFactory.GetException(err, string.Format("Failed to create native handle for package info of {0}", packageId));
310 Package package = CreatePackage(packageInfoHandle, packageId);
312 err = Interop.Package.PackageInfoDestroy(packageInfoHandle);
313 if (err != Interop.PackageManager.ErrorCode.None)
315 Log.Warn(LogTag, string.Format("Failed to destroy native handle for package info of {0}. err = {1}", packageId, err));
320 internal static Package GetPackage(IntPtr packageInfoHandle)
323 Interop.PackageManager.ErrorCode err = Interop.Package.PackageInfoGetPackage(packageInfoHandle, out packageId);
324 if (err != Interop.PackageManager.ErrorCode.None)
326 throw PackageManagerErrorFactory.GetException(err, "Failed to get package id for given package handle.");
328 return CreatePackage(packageInfoHandle, packageId);
331 private static List<string> GetPackagePrivilegeInformation(IntPtr packageInfoHandle)
333 List<string> privileges = new List<string>();
334 Interop.Package.PackageInfoPrivilegeInfoCallback privilegeInfoCb = (privilege, userData) =>
336 privileges.Add(privilege);
340 Interop.PackageManager.ErrorCode err = Interop.Package.PackageInfoForeachPrivilegeInfo(packageInfoHandle, privilegeInfoCb, IntPtr.Zero);
341 if (err != Interop.PackageManager.ErrorCode.None)
343 Log.Warn(LogTag, string.Format("Failed to get privilage info. err = {0}", err));