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.Threading.Tasks;
22 namespace Tizen.Applications
25 /// PackageManager class. This class has the methods and events of the PackageManager.
28 /// The package manager is one of the core modules of the Tizen application framework and responsible for getting their information.
29 /// You can also retrieve information related to the packages that are installed on the device.
31 public static class PackageManager
33 private const string LogTag = "Tizen.Applications.PackageManager";
35 private static SafePackageManagerHandle s_handle = new SafePackageManagerHandle();
36 private static Interop.PackageManager.EventStatus s_eventStatus = Interop.PackageManager.EventStatus.All;
37 private static event EventHandler<PackageManagerEventArgs> s_installEventHandler;
38 private static event EventHandler<PackageManagerEventArgs> s_uninstallEventHandler;
39 private static event EventHandler<PackageManagerEventArgs> s_updateEventHandler;
40 private static event EventHandler<PackageManagerEventArgs> s_moveEventHandler;
41 private static event EventHandler<PackageManagerEventArgs> s_clearDataEventHandler;
43 private static Interop.PackageManager.PackageManagerEventCallback s_packageManagerEventCallback;
46 /// Event callback method for the request.
48 /// <param name="type">Type of the package which was requested.</param>
49 /// <param name="packageId">ID of the package which was requested.</param>
50 /// <param name="eventType">Event type of the request.</param>
51 /// <param name="eventState">Current event state of the request.</param>
52 /// <param name="progress">Progress for the request being processed by the package manager (in percent).</param>
53 public delegate void RequestEventCallback(string type, string packageId, PackageEventType eventType, PackageEventState eventState, int progress);
55 private static Dictionary<int, RequestEventCallback> RequestCallbacks = new Dictionary<int, RequestEventCallback>();
56 private static Dictionary<int, SafePackageManagerRequestHandle> RequestHandles = new Dictionary<int, SafePackageManagerRequestHandle>();
58 private delegate Interop.PackageManager.ErrorCode InstallMethodWithCallback(SafePackageManagerRequestHandle requestHandle, string pkgPath, Interop.PackageManager.PackageManagerRequestEventCallback requestCallback, IntPtr userData, out int requestID);
59 private delegate Interop.PackageManager.ErrorCode InstallMethod(SafePackageManagerRequestHandle requestHandle, string pkgPath, out int requestID);
62 /// InstallProgressChanged event. This event occurs when a package is getting installed and the progress of the request to the package manager is changed.
64 public static event EventHandler<PackageManagerEventArgs> InstallProgressChanged
68 SetPackageManagerEventStatus(Interop.PackageManager.EventStatus.Install);
69 RegisterPackageManagerEventIfNeeded();
70 s_installEventHandler += value;
74 s_installEventHandler -= value;
75 UnregisterPackageManagerEventIfNeeded();
76 UnsetPackageManagerEventStatus();
81 /// UninstallProgressChanged event. This event occurs when a package is getting uninstalled and the progress of the request to the package manager is changed.
83 public static event EventHandler<PackageManagerEventArgs> UninstallProgressChanged
87 SetPackageManagerEventStatus(Interop.PackageManager.EventStatus.Uninstall);
88 RegisterPackageManagerEventIfNeeded();
89 s_uninstallEventHandler += value;
93 s_uninstallEventHandler -= value;
94 UnregisterPackageManagerEventIfNeeded();
95 UnsetPackageManagerEventStatus();
100 /// UpdateProgressChanged event. This event occurs when a package is getting updated and the progress of the request to the package manager is changed.
102 public static event EventHandler<PackageManagerEventArgs> UpdateProgressChanged
106 SetPackageManagerEventStatus(Interop.PackageManager.EventStatus.Upgrade);
107 RegisterPackageManagerEventIfNeeded();
108 s_updateEventHandler += value;
112 s_updateEventHandler -= value;
113 UnregisterPackageManagerEventIfNeeded();
114 UnsetPackageManagerEventStatus();
119 /// MoveProgressChanged event. This event occurs when a package is getting moved and the progress of the request to the package manager is changed.
121 public static event EventHandler<PackageManagerEventArgs> MoveProgressChanged
125 SetPackageManagerEventStatus(Interop.PackageManager.EventStatus.Move);
126 RegisterPackageManagerEventIfNeeded();
127 s_moveEventHandler += value;
131 s_moveEventHandler -= value;
132 UnregisterPackageManagerEventIfNeeded();
133 UnsetPackageManagerEventStatus();
138 /// ClearDataProgressChanged event. This event occurs when data directories are cleared in the given package.
140 public static event EventHandler<PackageManagerEventArgs> ClearDataProgressChanged
144 SetPackageManagerEventStatus(Interop.PackageManager.EventStatus.ClearData);
145 RegisterPackageManagerEventIfNeeded();
146 s_clearDataEventHandler += value;
150 s_clearDataEventHandler -= value;
151 UnregisterPackageManagerEventIfNeeded();
152 UnsetPackageManagerEventStatus();
156 private static SafePackageManagerHandle Handle
160 if (s_handle.IsInvalid)
162 var err = Interop.PackageManager.PackageManagerCreate(out s_handle);
163 if (err != Interop.PackageManager.ErrorCode.None)
165 Log.Warn(LogTag, string.Format("Failed to create package manager handle. err = {0}", err));
172 private static Interop.PackageManager.PackageManagerRequestEventCallback internalRequestEventCallback = (id, packageType, packageId, eventType, eventState, progress, error, userData) =>
174 if (RequestCallbacks.ContainsKey(id))
178 RequestCallbacks[id](packageType, packageId, (PackageEventType)eventType, (PackageEventState)eventState, progress);
179 if (eventState == Interop.PackageManager.PackageEventState.Completed || eventState == Interop.PackageManager.PackageEventState.Failed)
181 Log.Debug(LogTag, string.Format("release request handle for id : {0}", id));
182 RequestHandles[id].Dispose();
183 RequestHandles.Remove(id);
184 RequestCallbacks.Remove(id);
189 Log.Warn(LogTag, e.Message);
190 RequestHandles[id].Dispose();
191 RequestHandles.Remove(id);
192 RequestCallbacks.Remove(id);
198 /// Gets the package ID for the given application ID.
200 /// <param name="applicationId">The ID of the application.</param>
201 /// <returns>Returns the ID of the package. Empty string if the application ID does not exist.</returns>
202 /// <exception cref="OutOfMemoryException">Thrown when there is not enough memory to continue the execution of the method.</exception>
203 /// <exception cref="UnauthorizedAccessException">Thrown when an application does not have the privilege to access this method.</exception>
204 /// <privilege>http://tizen.org/privilege/packagemanager.info</privilege>
205 public static string GetPackageIdByApplicationId(string applicationId)
208 var err = Interop.PackageManager.PackageManageGetPackageIdByAppId(applicationId, out packageId);
209 if (err != Interop.PackageManager.ErrorCode.None)
211 Log.Warn(LogTag, string.Format("Failed to get package Id of {0}. err = {1}", applicationId, err));
212 if (err != Interop.PackageManager.ErrorCode.InvalidParameter)
214 throw PackageManagerErrorFactory.GetException(err, "Failed to get package Id");
221 /// Gets the package information for the given package.
223 /// <param name="packageId">The ID of the package.</param>
224 /// <returns>Returns the package information for the given package ID.</returns>
225 /// <exception cref="ArgumentException">Thrown when the failed input package ID is invalid.</exception>
226 /// <exception cref="OutOfMemoryException">Thrown when there is not enough memory to continue the execution of the method.</exception>
227 /// <exception cref="System.IO.IOException">Thrown when the method fails due to an internal I/O error.</exception>
228 /// <exception cref="UnauthorizedAccessException">Thrown when an application does not have the privilege to access this method.</exception>
229 /// <privilege>http://tizen.org/privilege/packagemanager.info</privilege>
230 public static Package GetPackage(string packageId)
232 return Package.GetPackage(packageId);
236 /// Clears the application's internal and external cache directories.
238 /// <param name="packageId">ID of the package.</param>
239 /// <exception cref="OutOfMemoryException">Thrown when there is not enough memory to continue the execution of the method.</exception>
240 /// <exception cref="System.IO.IOException">Thrown when the method fails due to an internal I/O error.</exception>
241 /// <exception cref="UnauthorizedAccessException">Thrown when an application does not have the privilege to access this method.</exception>
242 /// <exception cref="SystemException">Thrown when the method failed due to an internal system error.</exception>
243 /// <privilege>http://tizen.org/privilege/packagemanager.clearcache</privilege>
244 public static void ClearCacheDirectory(string packageId)
246 Interop.PackageManager.ErrorCode err = Interop.PackageManager.PackageManagerClearCacheDir(packageId);
247 if (err != Interop.PackageManager.ErrorCode.None)
249 Log.Warn(LogTag, string.Format("Failed to clear cache directory for {0}. err = {1}", packageId, err));
250 throw PackageManagerErrorFactory.GetException(err, "Failed to clear cache directory");
255 /// Clears all the application's internal and external cache directories.
257 /// <exception cref="OutOfMemoryException">Thrown when there is not enough memory to continue the execution of the method.</exception>
258 /// <exception cref="System.IO.IOException">Thrown when the method fails due to an internal IO error.</exception>
259 /// <exception cref="UnauthorizedAccessException">Thrown when an application does not have the privilege to access this method.</exception>
260 /// <exception cref="SystemException">Thrown when the method failed due to an internal system error.</exception>
261 /// <privilege>http://tizen.org/privilege/packagemanager.admin</privilege>
262 /// <privlevel>platform</privlevel>
263 public static void ClearAllCacheDirectory()
265 var err = Interop.PackageManager.PackageManagerClearAllCacheDir();
266 if (err != Interop.PackageManager.ErrorCode.None)
268 Log.Warn(LogTag, string.Format("Failed to clear all cache directories. err = {0}", err));
269 throw PackageManagerErrorFactory.GetException(err, "Failed to clear all cache directories");
274 /// Clears the application's internal and external data directories.
277 /// All files under data, shared/data, and shared/trusted in the internal storage are removed.
278 /// And, if the external storage exists, then all files under data and shared/trusted in the external storage are removed.
280 /// <param name="packageId">ID of the package.</param>
281 /// <exception cref="OutOfMemoryException">Thrown when there is not enough memory to continue the execution of the method.</exception>
282 /// <exception cref="System.IO.IOException">Thrown when the method failed due to an internal IO error.</exception>
283 /// <exception cref="UnauthorizedAccessException">Thrown when an application does not have the privilege to access this method.</exception>
284 /// <exception cref="SystemException">Thrown when the method failed due to an internal system error.</exception>
285 /// <privilege>http://tizen.org/privilege/packagemanager.admin</privilege>
286 /// <privlevel>platform</privlevel>
287 public static void ClearDataDirectory(string packageId)
289 Interop.PackageManager.ErrorCode err = Interop.PackageManager.PackageManagerClearDataDir(packageId);
290 if (err != Interop.PackageManager.ErrorCode.None)
292 Log.Warn(LogTag, string.Format("Failed to clear data directory for {0}. err = {1}", packageId, err));
293 throw PackageManagerErrorFactory.GetException(err, "Failed to clear data directory");
298 /// Retrieves the package information of all installed packages.
300 /// <returns>Returns the list of packages.</returns>
301 /// <privilege>http://tizen.org/privilege/packagemanager.info</privilege>
302 public static IEnumerable<Package> GetPackages()
304 return GetPackages(null);
308 /// Retrieves the package information of all the installed packages satisfying the filter conditions.
310 /// <param name="filter">Optional - package filters.</param>
311 /// <returns>Returns the list of packages.</returns>
312 /// <privilege>http://tizen.org/privilege/packagemanager.info</privilege>
313 public static IEnumerable<Package> GetPackages(PackageFilter filter)
315 List<Package> packageList = new List<Package>();
318 var err = Interop.PackageManager.PackageManagerFilterCreate(out filterHandle);
319 if (err != Interop.PackageManager.ErrorCode.None)
321 Log.Warn(LogTag, string.Format("Failed to create package filter handle. err = {0}", err));
325 if (filter != null && filter.Filters != null)
327 foreach (KeyValuePair<string, bool> entry in filter?.Filters)
329 err = Interop.PackageManager.PackageManagerFilterAdd(filterHandle, entry.Key, entry.Value);
330 if (err != Interop.PackageManager.ErrorCode.None)
332 Log.Warn(LogTag, string.Format("Failed to configure package filter. err = {0}", err));
338 if (err == Interop.PackageManager.ErrorCode.None)
340 Interop.PackageManager.PackageManagerPackageInfoCallback cb = (handle, userData) =>
342 packageList.Add(Package.GetPackage(handle));
346 err = Interop.PackageManager.PackageManagerFilterForeachPackageInfo(filterHandle, cb, IntPtr.Zero);
347 if (err != Interop.PackageManager.ErrorCode.None)
349 Log.Warn(LogTag, string.Format("Failed to get package Informations. err = {0}", err));
353 err = Interop.PackageManager.PackageManagerFilterDestroy(filterHandle);
354 if (err != Interop.PackageManager.ErrorCode.None)
356 Log.Warn(LogTag, string.Format("Failed to destroy package filter handle. err = {0}", err));
362 /// Gets the total package size information.
364 /// <returns>Returns the total package size information asynchronously.</returns>
365 /// <privilege>http://tizen.org/privilege/packagemanager.info</privilege>
366 public static async Task<PackageSizeInformation> GetTotalSizeInformationAsync()
368 TaskCompletionSource<PackageSizeInformation> tcs = new TaskCompletionSource<PackageSizeInformation>();
369 Interop.PackageManager.PackageManagerTotalSizeInfoCallback cb = (handle, userData) =>
371 if (handle != IntPtr.Zero)
373 tcs.TrySetResult(PackageSizeInformation.GetPackageSizeInformation(handle));
377 var err = Interop.PackageManager.PackageManagerGetTotalSizeInfo(cb, IntPtr.Zero);
378 if (err != Interop.PackageManager.ErrorCode.None)
380 tcs.TrySetException(PackageManagerErrorFactory.GetException(err, "Failed to get total package size info"));
382 return await tcs.Task.ConfigureAwait(false);
386 /// Installs the package located at the given path.
388 /// <param name="packagePath">Absolute path for the package to be installed.</param>
389 /// <param name="installMode">Optional parameter to indicate special installation mode.</param>
390 /// <returns>Returns true if the installation request is successful, otherwise false.</returns>
392 /// The 'true' means that the request for installation is successful.
393 /// To check the result of the installation, the caller should check the progress using the InstallProgressChanged event.
395 /// <privilege>http://tizen.org/privilege/packagemanager.admin</privilege>
396 /// <privlevel>platform</privlevel>
397 public static bool Install(string packagePath, InstallationMode installMode = InstallationMode.Normal)
399 return Install(packagePath, null, PackageType.UNKNOWN, null, installMode);
403 /// Installs the package located at the given path.
405 /// <param name="packagePath">Absolute path for the package to be installed.</param>
406 /// <param name="eventCallback">The event callback will be invoked only for the current request.</param>
407 /// <param name="installMode">Optional parameter to indicate special installation mode.</param>
408 /// <returns>Returns true if installtion request is successful, false otherwise.</returns>
410 /// The 'true' means that the request for installation is successful.
411 /// To check the result of installation, the caller should check the progress using the InstallProgressChanged event or eventCallback.
413 /// <privilege>http://tizen.org/privilege/packagemanager.admin</privilege>
414 /// <privlevel>platform</privlevel>
415 public static bool Install(string packagePath, RequestEventCallback eventCallback, InstallationMode installMode = InstallationMode.Normal)
417 return Install(packagePath, null, PackageType.UNKNOWN, eventCallback, installMode);
421 /// Installs the package located at the given path.
423 /// <param name="packagePath">Absolute path for the package to be installed.</param>
424 /// <param name="type">Package type for the package to be installed.</param>
425 /// <param name="installMode">Optional parameter to indicate special installation mode.</param>
426 /// <returns>Returns true if installtion request is successful, false otherwise.</returns>
428 /// The 'true' means that the request for installation is successful.
429 /// To check the result of installation, the caller should check the progress using the InstallProgressChanged event.
431 /// <privilege>http://tizen.org/privilege/packagemanager.admin</privilege>
432 /// <privlevel>platform</privlevel>
433 public static bool Install(string packagePath, PackageType type, InstallationMode installMode = InstallationMode.Normal)
435 return Install(packagePath, null, type, null, installMode);
439 /// Installs the package located at the given path.
441 /// <param name="packagePath">Absolute path for the package to be installed.</param>
442 /// <param name="expansionPackagePath">Absolute path for the expansion package to be installed.</param>
443 /// <param name="installMode">Optional parameter to indicate special installation mode.</param>
444 /// <returns>Returns true if installtion request is successful, false otherwise.</returns>
446 /// The 'true' means that the request for installation is successful.
447 /// To check the result of installation, the caller should check the progress using the InstallProgressChanged event.
449 /// <privilege>http://tizen.org/privilege/packagemanager.admin</privilege>
450 /// <privlevel>platform</privlevel>
451 public static bool Install(string packagePath, string expansionPackagePath, InstallationMode installMode = InstallationMode.Normal)
453 return Install(packagePath, expansionPackagePath, PackageType.UNKNOWN, null, installMode);
457 /// Installs the package located at the given path.
459 /// <param name="packagePath">Absolute path for the package to be installed.</param>
460 /// <param name="type">Package type for the package to be installed.</param>
461 /// <param name="eventCallback">The event callback will be invoked only for the current request.</param>
462 /// <param name="installMode">Optional parameter to indicate special installation mode.</param>
463 /// <returns>Returns true if installtion request is successful, false otherwise.</returns>
465 /// The 'true' means that the request for installation is successful.
466 /// To check the result of installation, the caller should check the progress using the InstallProgressChanged event or eventCallback.
468 /// <privilege>http://tizen.org/privilege/packagemanager.admin</privilege>
469 /// <privlevel>platform</privlevel>
470 public static bool Install(string packagePath, PackageType type, RequestEventCallback eventCallback, InstallationMode installMode = InstallationMode.Normal)
472 return Install(packagePath, null, type, eventCallback, installMode);
476 /// Installs the package located at the given path.
478 /// <param name="packagePath">Absolute path for the package to be installed.</param>
479 /// <param name="expansionPackagePath">Absolute path for the expansion package to be installed.</param>
480 /// <param name="eventCallback">The event callback will be invoked only for the current request.</param>
481 /// <param name="installMode">Optional parameter to indicate special installation mode.</param>
482 /// <returns>Returns true if installtion request is successful, false otherwise.</returns>
484 /// The 'true' means that the request for installation is successful.
485 /// To check the result of installation, the caller should check the progress using the InstallProgressChanged event or eventCallback.
487 /// <privilege>http://tizen.org/privilege/packagemanager.admin</privilege>
488 /// <privlevel>platform</privlevel>
489 public static bool Install(string packagePath, string expansionPackagePath, RequestEventCallback eventCallback, InstallationMode installMode = InstallationMode.Normal)
491 return Install(packagePath, expansionPackagePath, PackageType.UNKNOWN, eventCallback, installMode);
495 /// Installs the package located at the given path.
497 /// <param name="packagePath">Absolute path for the package to be installed.</param>
498 /// <param name="expansionPackagePath">Absolute path for the expansion package to be installed.</param>
499 /// <param name="type">Package type for the package to be installed.</param>
500 /// <param name="installMode">Optional parameter to indicate special installation mode.</param>
501 /// <returns>Returns true if installtion request is successful, false otherwise.</returns>
503 /// The 'true' means that the request for installation is successful.
504 /// To check the result of installation, the caller should check the progress using the InstallProgressChanged event.
506 /// <privilege>http://tizen.org/privilege/packagemanager.admin</privilege>
507 /// <privlevel>platform</privlevel>
508 public static bool Install(string packagePath, string expansionPackagePath, PackageType type, InstallationMode installMode = InstallationMode.Normal)
510 return Install(packagePath, expansionPackagePath, type, null, installMode);
514 /// Installs the package located at the given path.
516 /// <param name="packagePath">Absolute path for the package to be installed.</param>
517 /// <param name="expansionPackagePath">Absolute path for the expansion package to be installed.</param>
518 /// <param name="type">Package type for the package to be installed.</param>
519 /// <param name="eventCallback">The event callback will be invoked only for the current request.</param>
520 /// <param name="installMode">Optional parameter to indicate special installation mode.</param>
521 /// <returns>Returns true if installtion request is successful, false otherwise.</returns>
523 /// The 'true' means that the request for installation is successful.
524 /// To check the result of installation, the caller should check the progress using the InstallProgressChanged event or eventCallback.
526 /// <privilege>http://tizen.org/privilege/packagemanager.admin</privilege>
527 /// <privlevel>platform</privlevel>
528 public static bool Install(string packagePath, string expansionPackagePath, PackageType type, RequestEventCallback eventCallback, InstallationMode installMode = InstallationMode.Normal)
530 SafePackageManagerRequestHandle RequestHandle;
531 var err = Interop.PackageManager.PackageManagerRequestCreate(out RequestHandle);
532 if (err != Interop.PackageManager.ErrorCode.None)
534 Log.Warn(LogTag, string.Format("Failed to install package {0}. Error in creating package manager request handle. err = {1}", packagePath, err));
540 if (type != PackageType.UNKNOWN)
542 err = Interop.PackageManager.PackageManagerRequestSetType(RequestHandle, type.ToString().ToLower());
543 if (err != Interop.PackageManager.ErrorCode.None)
545 Log.Warn(LogTag, string.Format("Failed to install package {0}. Error in setting request package type. err = {1}", packagePath, err));
546 RequestHandle.Dispose();
551 if (!string.IsNullOrEmpty(expansionPackagePath))
553 err = Interop.PackageManager.PackageManagerRequestSetTepPath(RequestHandle, expansionPackagePath);
554 if (err != Interop.PackageManager.ErrorCode.None)
556 Log.Warn(LogTag, string.Format("Failed to install package {0}. Error in setting request package mode. err = {1}", packagePath, err));
557 RequestHandle.Dispose();
563 if (eventCallback != null)
565 InstallMethodWithCallback install;
566 if (installMode == InstallationMode.Mount)
568 install = Interop.PackageManager.PackageManagerRequestMountInstallWithCB;
572 install = Interop.PackageManager.PackageManagerRequestInstallWithCB;
574 err = install(RequestHandle, packagePath, internalRequestEventCallback, IntPtr.Zero, out requestId);
575 if (err == Interop.PackageManager.ErrorCode.None)
577 RequestCallbacks.Add(requestId, eventCallback);
578 RequestHandles.Add(requestId, RequestHandle);
582 Log.Warn(LogTag, string.Format("Failed to install package {0}. err = {1}", packagePath, err));
583 RequestHandle.Dispose();
589 InstallMethod install;
590 if (installMode == InstallationMode.Mount)
592 install = Interop.PackageManager.PackageManagerRequestMountInstall;
596 install = Interop.PackageManager.PackageManagerRequestInstall;
598 err = install(RequestHandle, packagePath, out requestId);
599 if (err != Interop.PackageManager.ErrorCode.None)
601 Log.Warn(LogTag, string.Format("Failed to install package {0}. err = {1}", packagePath, err));
602 RequestHandle.Dispose();
605 // RequestHandle isn't necessary when this method is called without 'eventCallback' parameter.
606 RequestHandle.Dispose();
612 Log.Warn(LogTag, e.Message);
613 RequestHandle.Dispose();
619 /// Uninstalls the package with the given name.
621 /// <param name="packageId">ID of the package to be uninstalled.</param>
622 /// <returns>Returns true if the uninstallation request is successful, false otherwise.</returns>
624 /// The 'true' means that the request for uninstallation is successful.
625 /// To check the result of uninstallation, the caller should check the progress using the UninstallProgressChanged event.
627 /// <privilege>http://tizen.org/privilege/packagemanager.admin</privilege>
628 /// <privlevel>platform</privlevel>
629 public static bool Uninstall(string packageId)
631 return Uninstall(packageId, PackageType.UNKNOWN, null);
635 /// Uninstalls package with the given names.
637 /// <param name="packageId">ID of the package to be uninstalled.</param>
638 /// <param name="type">Optional - Package type for the package to be uninstalled.</param>
639 /// <returns>Returns true if the uninstalltion request is successful, false otherwise.</returns>
641 /// The 'true' means that the request for uninstallation is successful.
642 /// To check the result of uninstallation, the caller should check the progress using the UninstallProgressChanged event.
644 /// <privilege>http://tizen.org/privilege/packagemanager.admin</privilege>
645 /// <privlevel>platform</privlevel>
646 public static bool Uninstall(string packageId, PackageType type)
648 return Uninstall(packageId, type, null);
652 /// Uninstalls the package with the given name.
654 /// <param name="packageId">ID of the package to be uninstalled.</param>
655 /// <param name="eventCallback">Optional - The event callback will be invoked only for the current request.</param>
656 /// <returns>Returns true if the uninstallation request is successful, false otherwise.</returns>
658 /// The 'true' means that the request for uninstallation is successful.
659 /// To check the result of uninstallation, the caller should check the progress using the UninstallProgressChanged event or eventCallback.
661 /// <privilege>http://tizen.org/privilege/packagemanager.admin</privilege>
662 /// <privlevel>platform</privlevel>
663 public static bool Uninstall(string packageId, RequestEventCallback eventCallback)
665 return Uninstall(packageId, PackageType.UNKNOWN, eventCallback);
669 /// Uninstalls the package with the given name.
671 /// <param name="packageId">ID of the package to be uninstalled</param>
672 /// <param name="type">Optional - Package type for the package to be uninstalled.</param>
673 /// <param name="eventCallback">Optional - The event callback will be invoked only for the current request.</param>
674 /// <returns>Returns true if the uninstallation request is successful, false otherwise.</returns>
676 /// The 'true' means that the request for uninstallation is successful.
677 /// To check the result of uninstallation, the caller should check the progress using the UninstallProgressChanged event or eventCallback.
679 /// <privilege>http://tizen.org/privilege/packagemanager.admin</privilege>
680 /// <privlevel>platform</privlevel>
681 public static bool Uninstall(string packageId, PackageType type, RequestEventCallback eventCallback)
683 SafePackageManagerRequestHandle RequestHandle;
684 var err = Interop.PackageManager.PackageManagerRequestCreate(out RequestHandle);
685 if (err != Interop.PackageManager.ErrorCode.None)
687 Log.Warn(LogTag, string.Format("Failed to uninstall package {0}. Error in creating package manager request handle. err = {1}", packageId, err));
693 err = Interop.PackageManager.PackageManagerRequestSetType(RequestHandle, type.ToString().ToLower());
694 if (err != Interop.PackageManager.ErrorCode.None)
696 Log.Warn(LogTag, string.Format("Failed to uninstall package {0}. Error in setting request package type. err = {1}", packageId, err));
697 RequestHandle.Dispose();
702 if (eventCallback != null)
704 err = Interop.PackageManager.PackageManagerRequestUninstallWithCB(RequestHandle, packageId, internalRequestEventCallback, IntPtr.Zero, out requestId);
705 if (err == Interop.PackageManager.ErrorCode.None)
707 RequestCallbacks.Add(requestId, eventCallback);
708 RequestHandles.Add(requestId, RequestHandle);
712 Log.Warn(LogTag, string.Format("Failed to uninstall package {0}. err = {1}", packageId, err));
713 RequestHandle.Dispose();
719 err = Interop.PackageManager.PackageManagerRequestUninstall(RequestHandle, packageId, out requestId);
720 if (err != Interop.PackageManager.ErrorCode.None)
722 Log.Warn(LogTag, string.Format("Failed to uninstall package. err = {0}", err));
723 RequestHandle.Dispose();
726 // RequestHandle isn't necessary when this method is called without 'eventCallback' parameter.
727 RequestHandle.Dispose();
733 Log.Warn(LogTag, e.Message);
734 RequestHandle.Dispose();
740 /// Moves the package to the given storage.
742 /// <param name="packageId">ID of the package to be moved.</param>
743 /// <param name="newStorage">Storage package should be moved to.</param>
744 /// <returns>Returns true if the move request is successful, false otherwise.</returns>
746 /// The 'true' means that the request for move is successful.
747 /// To check the result of move, the caller should check the progress using the MoveProgressChanged event.
749 /// <privilege>http://tizen.org/privilege/packagemanager.admin</privilege>
750 /// <privlevel>platform</privlevel>
751 public static bool Move(string packageId, StorageType newStorage)
753 return Move(packageId, PackageType.UNKNOWN, newStorage, null);
757 /// Moves the package to the given storage.
759 /// <param name="packageId">ID of the package to be moved.</param>
760 /// <param name="type">Optional - Package type for the package to be moved.</param>
761 /// <param name="newStorage">Storage package should be moved to.</param>
762 /// <returns>Returns true if the move request is successful, false otherwise.</returns>
764 /// The 'true' means that the request for move is successful.
765 /// To check the result of move, the caller should check the progress using the MoveProgressChanged event.
767 /// <privilege>http://tizen.org/privilege/packagemanager.admin</privilege>
768 /// <privlevel>platform</privlevel>
769 public static bool Move(string packageId, PackageType type, StorageType newStorage)
771 return Move(packageId, type, newStorage, null);
775 /// Moves the package to the given storage.
777 /// <param name="packageId">ID of the package to be moved.</param>
778 /// <param name="newStorage">Storage package should be moved to.</param>
779 /// <param name="eventCallback">Optional - The event callback will be invoked only for the current request.</param>
780 /// <returns>Returns true if move request is successful, false otherwise.</returns>
782 /// The 'true' means that the request for move is successful.
783 /// To check the result of move, the caller should check the progress using the MoveProgressChanged event.
785 /// <privilege>http://tizen.org/privilege/packagemanager.admin</privilege>
786 /// <privlevel>platform</privlevel>
787 public static bool Move(string packageId, StorageType newStorage, RequestEventCallback eventCallback)
789 return Move(packageId, PackageType.UNKNOWN, newStorage, eventCallback);
793 /// Moves the package to the given storage.
795 /// <param name="packageId">ID of the package to be moved.</param>
796 /// <param name="type">Optional - Package type for the package to be moved.</param>
797 /// <param name="newStorage">Storage, package should be moved to.</param>
798 /// <param name="eventCallback">Optional - The event callback will be invoked only for the current request.</param>
799 /// <returns>Returns true if move request is successful, false otherwise.</returns>
801 /// The 'true' means that the request for move is successful.
802 /// To check the result of move, the caller should check the progress using the MoveProgressChanged event.
804 /// <privilege>http://tizen.org/privilege/packagemanager.admin</privilege>
805 /// <privlevel>platform</privlevel>
806 public static bool Move(string packageId, PackageType type, StorageType newStorage, RequestEventCallback eventCallback)
808 SafePackageManagerRequestHandle RequestHandle;
809 var err = Interop.PackageManager.PackageManagerRequestCreate(out RequestHandle);
810 if (err != Interop.PackageManager.ErrorCode.None)
812 Log.Warn(LogTag, string.Format("Failed to create package manager request handle. err = {0}", err));
819 err = Interop.PackageManager.PackageManagerRequestSetType(RequestHandle, type.ToString().ToLower());
820 if (err != Interop.PackageManager.ErrorCode.None)
822 Log.Warn(LogTag, string.Format("Failed to move package. Error in setting request package type. err = {0}", err));
823 RequestHandle.Dispose();
827 if (eventCallback != null)
830 err = Interop.PackageManager.PackageManagerRequestMoveWithCB(RequestHandle, packageId, (Interop.PackageManager.StorageType)newStorage, internalRequestEventCallback, IntPtr.Zero, out requestId);
831 if (err == Interop.PackageManager.ErrorCode.None)
833 RequestCallbacks.Add(requestId, eventCallback);
834 RequestHandles.Add(requestId, RequestHandle);
838 Log.Warn(LogTag, string.Format("Failed to move package to requested location. err = {0}", err));
839 RequestHandle.Dispose();
845 err = Interop.PackageManager.PackageManagerRequestMove(RequestHandle, packageId, (Interop.PackageManager.StorageType)newStorage);
846 if (err != Interop.PackageManager.ErrorCode.None)
848 Log.Warn(LogTag, string.Format("Failed to move package to requested location. err = {0}", err));
849 RequestHandle.Dispose();
852 // RequestHandle isn't necessary when this method is called without 'eventCallback' parameter.
853 RequestHandle.Dispose();
859 Log.Warn(LogTag, e.Message);
860 RequestHandle.Dispose();
866 /// Gets the permission type of the package which has a given application ID.
868 /// <param name="applicationId">ID of the application.</param>
869 /// <returns>Returns the permission type.</returns>
870 /// <privilege>http://tizen.org/privilege/packagemanager.info</privilege>
871 /// <exception cref="ArgumentException">Thrown when the failed input package ID is invalid.</exception>
872 /// <exception cref="UnauthorizedAccessException">Thrown when an application does not have the privilege to access this method.</exception>
873 public static PermissionType GetPermissionTypeByApplicationId(string applicationId)
875 Interop.PackageManager.PackageManagerPermissionType permissionType;
876 Interop.PackageManager.ErrorCode err = Interop.PackageManager.PackageManagerGetPermissionType(applicationId, out permissionType);
877 if (err != Interop.PackageManager.ErrorCode.None)
879 throw PackageManagerErrorFactory.GetException(err, "Failed to get permission type.");
882 return (PermissionType)permissionType;
886 /// Gets the package's preload attribute which contains a given application ID.
888 /// <param name="applicationId">ID of the application.</param>
889 /// <returns>Returns true if the package is preloaded, otherwise false.</returns>
890 /// <privilege>http://tizen.org/privilege/packagemanager.info</privilege>
891 /// <exception cref="ArgumentException">Thrown when the failed input package ID is invalid.</exception>
892 /// <exception cref="UnauthorizedAccessException">Thrown when an application does not have the privilege to access this method.</exception>
893 public static bool IsPreloadPackageByApplicationId(string applicationId)
895 bool isPreloadPackage;
896 Interop.PackageManager.ErrorCode err = Interop.PackageManager.PackageManagerIsPreloadPackageByApplicationId(applicationId, out isPreloadPackage);
897 if (err != Interop.PackageManager.ErrorCode.None)
899 throw PackageManagerErrorFactory.GetException(err, "Failed to get preload info");
902 return isPreloadPackage;
906 /// Compares the certificate of the two packages.
908 /// <param name="lhsPackageId">Package ID to compare.</param>
909 /// <param name="rhsPackageId">Package ID to be compared.</param>
910 /// <returns>Returns certificate comparison result.</returns>
911 /// <exception cref="ArgumentException">Thrown when the failed input package ID is invalid.</exception>
912 /// <exception cref="System.IO.IOException">Thrown when the method failed due to an internal I/O error.</exception>
913 public static CertCompareResultType CompareCertInfo(string lhsPackageId, string rhsPackageId)
915 Interop.PackageManager.CertCompareResultType compareResult;
916 Interop.PackageManager.ErrorCode err = Interop.PackageManager.PackageManagerCompareCertInfo(lhsPackageId, rhsPackageId, out compareResult);
917 if (err != Interop.PackageManager.ErrorCode.None)
919 throw PackageManagerErrorFactory.GetException(err, "Failed to compare cert info");
922 return (CertCompareResultType)compareResult;
926 /// Compares the certificate of the two packages which contain each given application ID.
928 /// <param name="lhsApplicationId">Application ID to compare.</param>
929 /// <param name="rhsApplicationId">Application ID to be compared.</param>
930 /// <returns>Returns certificate comparison result.</returns>
931 /// <exception cref="ArgumentException">Thrown when the failed input package ID is invalid.</exception>
932 /// <exception cref="System.IO.IOException">Thrown when the method failed due to an internal I/O error.</exception>
933 public static CertCompareResultType CompareCertInfoByApplicationId(string lhsApplicationId, string rhsApplicationId)
935 Interop.PackageManager.CertCompareResultType compareResult;
936 Interop.PackageManager.ErrorCode err = Interop.PackageManager.PackageManagerCompareCertInfoByApplicationId(lhsApplicationId, rhsApplicationId, out compareResult);
937 if (err != Interop.PackageManager.ErrorCode.None)
939 throw PackageManagerErrorFactory.GetException(err, "Failed to compare cert info by application id");
942 return (CertCompareResultType)compareResult;
946 /// Drm nested class. This class has the PackageManager's drm related methods.
948 public static class Drm
951 /// Generates a request for getting the license.
953 /// <param name="responseData">Response data string of the purchase request.</param>
954 /// <returns>Returns the package DRM information of a given response data which contains the required data and license URL.</returns>
955 /// <privilege>http://tizen.org/privilege/packagemanager.admin</privilege>
956 /// <privlevel>platform</privlevel>
957 /// <exception cref="ArgumentException">Thrown when failed when input package ID is invalid.</exception>
958 /// <exception cref="OutOfMemoryException">Thrown when there is not enough memory to continue the execution of the method.</exception>
959 /// <exception cref="UnauthorizedAccessException">Thrown when an application does not have the privilege to access this method.</exception>
960 /// <exception cref="SystemException">Thrown when the method failed due to an internal system error.</exception>
961 public static PackageDrm GenerateLicenseRequest(string responseData)
963 return PackageDrm.GenerateLicenseRequest(responseData);
968 /// Registers the encrypted license.
970 /// <param name="responseData">The response data string of the rights request.</param>
971 /// <returns>Returns true if succeeds, otherwise false.</returns>
972 /// <privilege>http://tizen.org/privilege/packagemanager.admin</privilege>
973 /// <privlevel>platform</privlevel>
974 /// <exception cref="ArgumentException">Thrown when failed when input package ID is invalid.</exception>
975 /// <exception cref="OutOfMemoryException">Thrown when there is not enough memory to continue the execution of the method.</exception>
976 /// <exception cref="UnauthorizedAccessException">Thrown when an application does not have the privilege to access this method.</exception>
977 /// <exception cref="SystemException">Thrown when the method failed due to internal system error.</exception>
978 public static bool RegisterLicense(string responseData)
980 Interop.PackageManager.ErrorCode err = Interop.PackageManager.PackageManagerDrmRegisterLicense(responseData);
981 if (err != Interop.PackageManager.ErrorCode.None)
983 throw PackageManagerErrorFactory.GetException(err, "Failed to register drm license");
990 /// Decrypts the contents which are encrypted.
992 /// <param name="drmFilePath">Drm file path.</param>
993 /// <param name="decryptedFilePath">Decrypted file path.</param>
994 /// <returns>Returns true if succeeds, otherwise false.</returns>
995 /// <privilege>http://tizen.org/privilege/packagemanager.admin</privilege>
996 /// <privlevel>platform</privlevel>
997 /// <exception cref="ArgumentException">Thrown when failed when input package ID is invalid.</exception>
998 /// <exception cref="OutOfMemoryException">Thrown when there is not enough memory to continue the execution of the method.</exception>
999 /// <exception cref="UnauthorizedAccessException">Thrown when an application does not have the privilege to access this method.</exception>
1000 /// <exception cref="SystemException">Thrown when the method failed due to an internal system error.</exception>
1001 public static bool DecryptPackage(string drmFilePath, string decryptedFilePath)
1003 Interop.PackageManager.ErrorCode err = Interop.PackageManager.PackageManagerDrmDecryptPackage(drmFilePath, decryptedFilePath);
1004 if (err != Interop.PackageManager.ErrorCode.None)
1006 throw PackageManagerErrorFactory.GetException(err, "Failed to decrypt drm package");
1013 private static void SetPackageManagerEventStatus(Interop.PackageManager.EventStatus status)
1015 if (Handle.IsInvalid) return;
1017 Interop.PackageManager.EventStatus eventStatus = s_eventStatus;
1018 eventStatus |= status;
1019 if (eventStatus != Interop.PackageManager.EventStatus.All)
1020 eventStatus |= Interop.PackageManager.EventStatus.Progress;
1022 var err = Interop.PackageManager.ErrorCode.None;
1023 if (s_eventStatus != eventStatus)
1025 err = Interop.PackageManager.PackageManagerSetEvenStatus(Handle, eventStatus);
1026 if (err == Interop.PackageManager.ErrorCode.None)
1028 s_eventStatus = eventStatus;
1029 Log.Debug(LogTag, string.Format("New Event Status flag: {0}", s_eventStatus));
1032 Log.Debug(LogTag, string.Format("Failed to set flag for {0} event. err = {1}", eventStatus, err));
1036 private static void UnsetPackageManagerEventStatus()
1038 if (Handle.IsInvalid) return;
1040 Interop.PackageManager.EventStatus eventStatus = Interop.PackageManager.EventStatus.All;
1041 if (s_installEventHandler != null) eventStatus |= Interop.PackageManager.EventStatus.Install;
1042 if (s_uninstallEventHandler != null) eventStatus |= Interop.PackageManager.EventStatus.Uninstall;
1043 if (s_updateEventHandler != null) eventStatus |= Interop.PackageManager.EventStatus.Upgrade;
1044 if (s_moveEventHandler != null) eventStatus |= Interop.PackageManager.EventStatus.Move;
1045 if (s_clearDataEventHandler != null) eventStatus |= Interop.PackageManager.EventStatus.ClearData;
1046 if (eventStatus != Interop.PackageManager.EventStatus.All)
1047 eventStatus |= Interop.PackageManager.EventStatus.Progress;
1049 var err = Interop.PackageManager.ErrorCode.None;
1050 if (s_eventStatus != eventStatus)
1052 err = Interop.PackageManager.PackageManagerSetEvenStatus(Handle, eventStatus);
1053 if (err == Interop.PackageManager.ErrorCode.None)
1055 s_eventStatus = eventStatus;
1056 Log.Debug(LogTag, string.Format("New Event Status flag: {0}", s_eventStatus));
1059 Log.Debug(LogTag, string.Format("Failed to set flag for {0} event. err = {1}", eventStatus, err));
1063 private static void RegisterPackageManagerEventIfNeeded()
1065 if (s_installEventHandler != null && s_uninstallEventHandler != null && s_updateEventHandler != null && s_moveEventHandler != null && s_clearDataEventHandler != null)
1070 var err = Interop.PackageManager.ErrorCode.None;
1071 s_packageManagerEventCallback = (packageType, packageId, eventType, eventState, progress, error, user_data) =>
1075 if (eventType == Interop.PackageManager.EventType.Install)
1077 s_installEventHandler?.Invoke(null, new PackageManagerEventArgs(packageType, packageId, (PackageEventState)eventState, progress));
1079 else if (eventType == Interop.PackageManager.EventType.Uninstall)
1081 s_uninstallEventHandler?.Invoke(null, new PackageManagerEventArgs(packageType, packageId, (PackageEventState)eventState, progress));
1083 else if (eventType == Interop.PackageManager.EventType.Update)
1085 s_updateEventHandler?.Invoke(null, new PackageManagerEventArgs(packageType, packageId, (PackageEventState)eventState, progress));
1087 else if (eventType == Interop.PackageManager.EventType.Move)
1089 s_moveEventHandler?.Invoke(null, new PackageManagerEventArgs(packageType, packageId, (PackageEventState)eventState, progress));
1091 else if (eventType == Interop.PackageManager.EventType.ClearData)
1093 s_clearDataEventHandler?.Invoke(null, new PackageManagerEventArgs(packageType, packageId, (PackageEventState)eventState, progress));
1098 Log.Warn(LogTag, e.Message);
1102 if (!Handle.IsInvalid)
1104 Log.Debug(LogTag, "Reset Package Event");
1105 err = Interop.PackageManager.PackageManagerUnsetEvent(Handle);
1106 if (err != Interop.PackageManager.ErrorCode.None)
1108 throw PackageManagerErrorFactory.GetException(err, "Failed to unregister package manager event event.");
1111 err = Interop.PackageManager.PackageManagerSetEvent(Handle, s_packageManagerEventCallback, IntPtr.Zero);
1113 if (err != Interop.PackageManager.ErrorCode.None)
1115 Log.Warn(LogTag, string.Format("Failed to register callback for package manager event. err = {0}", err));
1119 private static void UnregisterPackageManagerEventIfNeeded()
1121 if (Handle.IsInvalid || s_installEventHandler != null || s_uninstallEventHandler != null || s_updateEventHandler != null || s_moveEventHandler != null || s_clearDataEventHandler != null)
1126 var err = Interop.PackageManager.PackageManagerUnsetEvent(Handle);
1127 if (err != Interop.PackageManager.ErrorCode.None)
1129 throw PackageManagerErrorFactory.GetException(err, "Failed to unregister package manager event event.");
1134 internal static class PackageManagerErrorFactory
1136 internal static Exception GetException(Interop.PackageManager.ErrorCode err, string message)
1138 string errMessage = string.Format("{0} err = {1}", message, err);
1141 case Interop.PackageManager.ErrorCode.InvalidParameter:
1142 case Interop.PackageManager.ErrorCode.NoSuchPackage:
1143 return new ArgumentException(errMessage);
1144 case Interop.PackageManager.ErrorCode.PermissionDenied:
1145 return new UnauthorizedAccessException(errMessage);
1146 case Interop.PackageManager.ErrorCode.IoError:
1147 return new global::System.IO.IOException(errMessage);
1149 return new InvalidOperationException(errMessage);