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 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 is occurred when a package is getting installed and the progress of the request to the package manager changes.
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 is occurred when a package is getting uninstalled and the progress of the request to the package manager changes.
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 is occurred when a package is getting updated and the progress of the request to the package manager changes.
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 is occurred when a package is getting moved and the progress of the request to the package manager changes.
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 is occurred 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 app ID.
200 /// <param name="applicationId">The ID of the application</param>
201 /// <returns>Returns the ID of the package. Empty string if App 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 app does not have 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 failed when 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 method failed due to internal IO error</exception>
228 /// <exception cref="UnauthorizedAccessException">Thrown when app does not have 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 directory.
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 method failed due to internal IO error</exception>
241 /// <exception cref="UnauthorizedAccessException">Thrown when app does not have privilege to access this method</exception>
242 /// <exception cref="SystemException">Thrown when method failed due to 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 application's internal and external cache directory.
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 method failed due to internal IO error</exception>
259 /// <exception cref="UnauthorizedAccessException">Thrown when app does not have privilege to access this method</exception>
260 /// <exception cref="SystemException">Thrown when method failed due to 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 external storeage 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 method failed due to internal IO error</exception>
283 /// <exception cref="UnauthorizedAccessException">Thrown when app does not have privilege to access this method</exception>
284 /// <exception cref="SystemException">Thrown when method failed due to 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 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 package information of all installed packages satisfying 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 package located at the given path
388 /// <param name="packagePath">Absolute path for the package to be installed</param>
389 /// <returns>Returns true if installtion request is successful, false otherwise.</returns>
391 /// The 'true' means that just the request of installation is seccessful.
392 /// To check the result of installation, the caller should check the progress using InstallProgressChanged event.
394 /// <privilege>http://tizen.org/privilege/packagemanager.admin</privilege>
395 /// <privlevel>platform</privlevel>
396 public static bool Install(string packagePath, InstallationMode installMode = InstallationMode.Normal)
398 return Install(packagePath, null, PackageType.UNKNOWN, null, installMode);
402 /// Installs package located at the given path
404 /// <param name="packagePath">Absolute path for the package to be installed</param>
405 /// <param name="eventCallback">The event callback will be invoked only for the current request</param>
406 /// <param name="installMode">Optional parameter to indicate special installation mode</param>
407 /// <returns>Returns true if installtion request is successful, false otherwise.</returns>
409 /// The 'true' means that just the request of installation is seccessful.
410 /// To check the result of installation, the caller should check the progress using InstallProgressChanged event OR eventCallback.
412 /// <privilege>http://tizen.org/privilege/packagemanager.admin</privilege>
413 /// <privlevel>platform</privlevel>
414 public static bool Install(string packagePath, RequestEventCallback eventCallback, InstallationMode installMode = InstallationMode.Normal)
416 return Install(packagePath, null, PackageType.UNKNOWN, eventCallback, installMode);
420 /// Installs package located at the given path
422 /// <param name="packagePath">Absolute path for the package to be installed</param>
423 /// <param name="type">Package type for the package to be installed</param>
424 /// <param name="installMode">Optional parameter to indicate special installation mode</param>
425 /// <returns>Returns true if installtion request is successful, false otherwise.</returns>
427 /// The 'true' means that just the request of installation is seccessful.
428 /// To check the result of installation, the caller should check the progress using InstallProgressChanged event.
430 /// <privilege>http://tizen.org/privilege/packagemanager.admin</privilege>
431 /// <privlevel>platform</privlevel>
432 public static bool Install(string packagePath, PackageType type, InstallationMode installMode = InstallationMode.Normal)
434 return Install(packagePath, null, type, null, installMode);
438 /// Installs package located at the given path
440 /// <param name="packagePath">Absolute path for the package to be installed</param>
441 /// <param name="expansionPackagePath">Absolute path for the expansion package to be installed</param>
442 /// <param name="installMode">Optional parameter to indicate special installation mode</param>
443 /// <returns>Returns true if installtion request is successful, false otherwise.</returns>
445 /// The 'true' means that just the request of installation is seccessful.
446 /// To check the result of installation, the caller should check the progress using InstallProgressChanged event.
448 /// <privilege>http://tizen.org/privilege/packagemanager.admin</privilege>
449 /// <privlevel>platform</privlevel>
450 public static bool Install(string packagePath, string expansionPackagePath, InstallationMode installMode = InstallationMode.Normal)
452 return Install(packagePath, expansionPackagePath, PackageType.UNKNOWN, null, installMode);
456 /// Installs package located at the given path
458 /// <param name="packagePath">Absolute path for the package to be installed</param>
459 /// <param name="type">Package type for the package to be installed</param>
460 /// <param name="eventCallback">The event callback will be invoked only for the current request</param>
461 /// <param name="installMode">Optional parameter to indicate special installation mode</param>
462 /// <returns>Returns true if installtion request is successful, false otherwise.</returns>
464 /// The 'true' means that just the request of installation is seccessful.
465 /// To check the result of installation, the caller should check the progress using InstallProgressChanged event OR eventCallback.
467 /// <privilege>http://tizen.org/privilege/packagemanager.admin</privilege>
468 /// <privlevel>platform</privlevel>
469 public static bool Install(string packagePath, PackageType type, RequestEventCallback eventCallback, InstallationMode installMode = InstallationMode.Normal)
471 return Install(packagePath, null, type, eventCallback, installMode);
475 /// Installs package located at the given path
477 /// <param name="packagePath">Absolute path for the package to be installed</param>
478 /// <param name="expansionPackagePath">Absolute path for the expansion package to be installed</param>
479 /// <param name="eventCallback">The event callback will be invoked only for the current request</param>
480 /// <param name="installMode">Optional parameter to indicate special installation mode</param>
481 /// <returns>Returns true if installtion request is successful, false otherwise.</returns>
483 /// The 'true' means that just the request of installation is seccessful.
484 /// To check the result of installation, the caller should check the progress using InstallProgressChanged event OR eventCallback.
486 /// <privilege>http://tizen.org/privilege/packagemanager.admin</privilege>
487 /// <privlevel>platform</privlevel>
488 public static bool Install(string packagePath, string expansionPackagePath, RequestEventCallback eventCallback, InstallationMode installMode = InstallationMode.Normal)
490 return Install(packagePath, expansionPackagePath, PackageType.UNKNOWN, eventCallback, installMode);
494 /// Installs package located at the given path
496 /// <param name="packagePath">Absolute path for the package to be installed</param>
497 /// <param name="expansionPackagePath">Absolute path for the expansion package to be installed</param>
498 /// <param name="type">Package type for the package to be installed</param>
499 /// <param name="installMode">Optional parameter to indicate special installation mode</param>
500 /// <returns>Returns true if installtion request is successful, false otherwise.</returns>
502 /// The 'true' means that just the request of installation is seccessful.
503 /// To check the result of installation, the caller should check the progress using InstallProgressChanged event.
505 /// <privilege>http://tizen.org/privilege/packagemanager.admin</privilege>
506 /// <privlevel>platform</privlevel>
507 public static bool Install(string packagePath, string expansionPackagePath, PackageType type, InstallationMode installMode = InstallationMode.Normal)
509 return Install(packagePath, expansionPackagePath, type, null, installMode);
513 /// Installs package located at the given path
515 /// <param name="packagePath">Absolute path for the package to be installed</param>
516 /// <param name="expansionPackagePath">Absolute path for the expansion package to be installed</param>
517 /// <param name="type">Package type for the package to be installed</param>
518 /// <param name="eventCallback">The event callback will be invoked only for the current request</param>
519 /// <param name="installMode">Optional parameter to indicate special installation mode</param>
520 /// <returns>Returns true if installtion request is successful, false otherwise.</returns>
522 /// The 'true' means that just the request of installation is seccessful.
523 /// To check the result of installation, the caller should check the progress using InstallProgressChanged event OR eventCallback.
525 /// <privilege>http://tizen.org/privilege/packagemanager.admin</privilege>
526 /// <privlevel>platform</privlevel>
527 public static bool Install(string packagePath, string expansionPackagePath, PackageType type, RequestEventCallback eventCallback, InstallationMode installMode = InstallationMode.Normal)
529 SafePackageManagerRequestHandle RequestHandle;
530 var err = Interop.PackageManager.PackageManagerRequestCreate(out RequestHandle);
531 if (err != Interop.PackageManager.ErrorCode.None)
533 Log.Warn(LogTag, string.Format("Failed to install package {0}. Error in creating package manager request handle. err = {1}", packagePath, err));
539 if (type != PackageType.UNKNOWN)
541 err = Interop.PackageManager.PackageManagerRequestSetType(RequestHandle, type.ToString().ToLower());
542 if (err != Interop.PackageManager.ErrorCode.None)
544 Log.Warn(LogTag, string.Format("Failed to install package {0}. Error in setting request package type. err = {1}", packagePath, err));
545 RequestHandle.Dispose();
550 if (!string.IsNullOrEmpty(expansionPackagePath))
552 err = Interop.PackageManager.PackageManagerRequestSetTepPath(RequestHandle, expansionPackagePath);
553 if (err != Interop.PackageManager.ErrorCode.None)
555 Log.Warn(LogTag, string.Format("Failed to install package {0}. Error in setting request package mode. err = {1}", packagePath, err));
556 RequestHandle.Dispose();
562 if (eventCallback != null)
564 InstallMethodWithCallback install;
565 if (installMode == InstallationMode.Mount)
567 install = Interop.PackageManager.PackageManagerRequestMountInstallWithCB;
571 install = Interop.PackageManager.PackageManagerRequestInstallWithCB;
573 err = install(RequestHandle, packagePath, internalRequestEventCallback, IntPtr.Zero, out requestId);
574 if (err == Interop.PackageManager.ErrorCode.None)
576 RequestCallbacks.Add(requestId, eventCallback);
577 RequestHandles.Add(requestId, RequestHandle);
581 Log.Warn(LogTag, string.Format("Failed to install package {0}. err = {1}", packagePath, err));
582 RequestHandle.Dispose();
588 InstallMethod install;
589 if (installMode == InstallationMode.Mount)
591 install = Interop.PackageManager.PackageManagerRequestMountInstall;
595 install = Interop.PackageManager.PackageManagerRequestInstall;
597 err = install(RequestHandle, packagePath, out requestId);
598 if (err != Interop.PackageManager.ErrorCode.None)
600 Log.Warn(LogTag, string.Format("Failed to install package {0}. err = {1}", packagePath, err));
601 RequestHandle.Dispose();
604 // RequestHandle isn't necessary when this method is called without 'eventCallback' parameter.
605 RequestHandle.Dispose();
611 Log.Warn(LogTag, e.Message);
612 RequestHandle.Dispose();
618 /// Uninstalls package with the given name.
620 /// <param name="packageId">Id of the package to be uninstalled</param>
621 /// <returns>Returns true if uninstallation request is successful, false otherwise.</returns>
623 /// The 'true' means that just the request of uninstallation is seccessful.
624 /// To check the result of uninstallation, the caller should check the progress using UninstallProgressChanged event.
626 /// <privilege>http://tizen.org/privilege/packagemanager.admin</privilege>
627 /// <privlevel>platform</privlevel>
628 public static bool Uninstall(string packageId)
630 return Uninstall(packageId, PackageType.UNKNOWN, null);
634 /// Uninstalls package with the given name.
636 /// <param name="packageId">Id of the package to be uninstalled</param>
637 /// <param name="type">Optional - Package type for the package to be uninstalled</param>
638 /// <returns>Returns true if uninstalltion request is successful, false otherwise.</returns>
640 /// The 'true' means that just the request of uninstallation is seccessful.
641 /// To check the result of uninstallation, the caller should check the progress using UninstallProgressChanged event.
643 /// <privilege>http://tizen.org/privilege/packagemanager.admin</privilege>
644 /// <privlevel>platform</privlevel>
645 public static bool Uninstall(string packageId, PackageType type)
647 return Uninstall(packageId, type, null);
651 /// Uninstalls package with the given name.
653 /// <param name="packageId">Id of the package to be uninstalled</param>
654 /// <param name="eventCallback">Optional - The event callback will be invoked only for the current request</param>
655 /// <returns>Returns true if uninstalltion request is successful, false otherwise.</returns>
657 /// The 'true' means that just the request of uninstallation is seccessful.
658 /// To check the result of uninstallation, the caller should check the progress using UninstallProgressChanged event OR eventCallback.
660 /// <privilege>http://tizen.org/privilege/packagemanager.admin</privilege>
661 /// <privlevel>platform</privlevel>
662 public static bool Uninstall(string packageId, RequestEventCallback eventCallback)
664 return Uninstall(packageId, PackageType.UNKNOWN, eventCallback);
668 /// Uninstalls package with the given name.
670 /// <param name="packageId">Id of the package to be uninstalled</param>
671 /// <param name="type">Optional - Package type for the package to be uninstalled</param>
672 /// <param name="eventCallback">Optional - The event callback will be invoked only for the current request</param>
673 /// <returns>Returns true if uninstalltion request is successful, false otherwise.</returns>
675 /// The 'true' means that just the request of uninstallation is seccessful.
676 /// To check the result of uninstallation, the caller should check the progress using UninstallProgressChanged event OR eventCallback.
678 /// <privilege>http://tizen.org/privilege/packagemanager.admin</privilege>
679 /// <privlevel>platform</privlevel>
680 public static bool Uninstall(string packageId, PackageType type, RequestEventCallback eventCallback)
682 SafePackageManagerRequestHandle RequestHandle;
683 var err = Interop.PackageManager.PackageManagerRequestCreate(out RequestHandle);
684 if (err != Interop.PackageManager.ErrorCode.None)
686 Log.Warn(LogTag, string.Format("Failed to uninstall package {0}. Error in creating package manager request handle. err = {1}", packageId, err));
692 err = Interop.PackageManager.PackageManagerRequestSetType(RequestHandle, type.ToString().ToLower());
693 if (err != Interop.PackageManager.ErrorCode.None)
695 Log.Warn(LogTag, string.Format("Failed to uninstall package {0}. Error in setting request package type. err = {1}", packageId, err));
696 RequestHandle.Dispose();
701 if (eventCallback != null)
703 err = Interop.PackageManager.PackageManagerRequestUninstallWithCB(RequestHandle, packageId, internalRequestEventCallback, IntPtr.Zero, out requestId);
704 if (err == Interop.PackageManager.ErrorCode.None)
706 RequestCallbacks.Add(requestId, eventCallback);
707 RequestHandles.Add(requestId, RequestHandle);
711 Log.Warn(LogTag, string.Format("Failed to uninstall package {0}. err = {1}", packageId, err));
712 RequestHandle.Dispose();
718 err = Interop.PackageManager.PackageManagerRequestUninstall(RequestHandle, packageId, out requestId);
719 if (err != Interop.PackageManager.ErrorCode.None)
721 Log.Warn(LogTag, string.Format("Failed to uninstall package. err = {0}", err));
722 RequestHandle.Dispose();
725 // RequestHandle isn't necessary when this method is called without 'eventCallback' parameter.
726 RequestHandle.Dispose();
732 Log.Warn(LogTag, e.Message);
733 RequestHandle.Dispose();
739 /// Move package to given storage.
741 /// <param name="packageId">Id of the package to be moved</param>
742 /// <param name="newStorage">Storage, package should be moved to</param>
743 /// <returns>Returns true if move request is successful, false otherwise.</returns>
745 /// The 'true' means that just the request of move is seccessful.
746 /// To check the result of move, the caller should check the progress using MoveProgressChanged event.
748 /// <privilege>http://tizen.org/privilege/packagemanager.admin</privilege>
749 /// <privlevel>platform</privlevel>
750 public static bool Move(string packageId, StorageType newStorage)
752 return Move(packageId, PackageType.UNKNOWN, newStorage, null);
756 /// Move package to given storage.
758 /// <param name="packageId">Id of the package to be moved</param>
759 /// <param name="type">Optional - Package type for the package to be moved</param>
760 /// <param name="newStorage">Storage, package should be moved to</param>
761 /// <returns>Returns true if move request is successful, false otherwise.</returns>
763 /// The 'true' means that just the request of move is seccessful.
764 /// To check the result of move, the caller should check the progress using MoveProgressChanged event.
766 /// <privilege>http://tizen.org/privilege/packagemanager.admin</privilege>
767 /// <privlevel>platform</privlevel>
768 public static bool Move(string packageId, PackageType type, StorageType newStorage)
770 return Move(packageId, type, newStorage, null);
774 /// Move package to given storage.
776 /// <param name="packageId">Id of the package to be moved</param>
777 /// <param name="newStorage">Storage, package should be moved to</param>
778 /// <param name="eventCallback">Optional - The event callback will be invoked only for the current request</param>
779 /// <returns>Returns true if move request is successful, false otherwise.</returns>
781 /// The 'true' means that just the request of move is seccessful.
782 /// To check the result of move, the caller should check the progress using MoveProgressChanged event.
784 /// <privilege>http://tizen.org/privilege/packagemanager.admin</privilege>
785 /// <privlevel>platform</privlevel>
786 public static bool Move(string packageId, StorageType newStorage, RequestEventCallback eventCallback)
788 return Move(packageId, PackageType.UNKNOWN, newStorage, eventCallback);
792 /// Move package to given storage.
794 /// <param name="packageId">Id of the package to be moved</param>
795 /// <param name="type">Optional - Package type for the package to be moved</param>
796 /// <param name="newStorage">Storage, package should be moved to</param>
797 /// <param name="eventCallback">Optional - The event callback will be invoked only for the current request</param>
798 /// <returns>Returns true if move request is successful, false otherwise.</returns>
800 /// The 'true' means that just the request of move is seccessful.
801 /// To check the result of move, the caller should check the progress using MoveProgressChanged event.
803 /// <privilege>http://tizen.org/privilege/packagemanager.admin</privilege>
804 /// <privlevel>platform</privlevel>
805 public static bool Move(string packageId, PackageType type, StorageType newStorage, RequestEventCallback eventCallback)
807 SafePackageManagerRequestHandle RequestHandle;
808 var err = Interop.PackageManager.PackageManagerRequestCreate(out RequestHandle);
809 if (err != Interop.PackageManager.ErrorCode.None)
811 Log.Warn(LogTag, string.Format("Failed to create package manager request handle. err = {0}", err));
818 err = Interop.PackageManager.PackageManagerRequestSetType(RequestHandle, type.ToString().ToLower());
819 if (err != Interop.PackageManager.ErrorCode.None)
821 Log.Warn(LogTag, string.Format("Failed to move package. Error in setting request package type. err = {0}", err));
822 RequestHandle.Dispose();
826 if (eventCallback != null)
829 err = Interop.PackageManager.PackageManagerRequestMoveWithCB(RequestHandle, packageId, (Interop.PackageManager.StorageType)newStorage, internalRequestEventCallback, IntPtr.Zero, out requestId);
830 if (err == Interop.PackageManager.ErrorCode.None)
832 RequestCallbacks.Add(requestId, eventCallback);
833 RequestHandles.Add(requestId, RequestHandle);
837 Log.Warn(LogTag, string.Format("Failed to move package to requested location. err = {0}", err));
838 RequestHandle.Dispose();
844 err = Interop.PackageManager.PackageManagerRequestMove(RequestHandle, packageId, (Interop.PackageManager.StorageType)newStorage);
845 if (err != Interop.PackageManager.ErrorCode.None)
847 Log.Warn(LogTag, string.Format("Failed to move package to requested location. err = {0}", err));
848 RequestHandle.Dispose();
851 // RequestHandle isn't necessary when this method is called without 'eventCallback' parameter.
852 RequestHandle.Dispose();
858 Log.Warn(LogTag, e.Message);
859 RequestHandle.Dispose();
865 /// Gets permission type of package which has given application id
867 /// <param name="applicationId">Id of the application</param>
868 /// <returns>Returns permission type.</returns>
869 /// <privilege>http://tizen.org/privilege/packagemanager.info</privilege>
870 /// <exception cref="ArgumentException">Thrown when failed when input package ID is invalid</exception>
871 /// <exception cref="UnauthorizedAccessException">Thrown when app does not have privilege to access this method</exception>
872 public static PermissionType GetPermissionTypeByApplicationId(string applicationId)
874 Interop.PackageManager.PackageManagerPermissionType permissionType;
875 Interop.PackageManager.ErrorCode err = Interop.PackageManager.PackageManagerGetPermissionType(applicationId, out permissionType);
876 if (err != Interop.PackageManager.ErrorCode.None)
878 throw PackageManagerErrorFactory.GetException(err, "Failed to get permission type.");
881 return (PermissionType)permissionType;
885 /// Gets package's preload attribute which contain given applicion id
887 /// <param name="applicationId">Id of the application</param>
888 /// <returns>Returns true if package is preloaded. Otherwise return false.</returns>
889 /// <privilege>http://tizen.org/privilege/packagemanager.info</privilege>
890 /// <exception cref="ArgumentException">Thrown when failed when input package ID is invalid</exception>
891 /// <exception cref="UnauthorizedAccessException">Thrown when app does not have privilege to access this method</exception>
892 public static bool IsPreloadPackageByApplicationId(string applicationId)
894 bool isPreloadPackage;
895 Interop.PackageManager.ErrorCode err = Interop.PackageManager.PackageManagerIsPreloadPackageByApplicationId(applicationId, out isPreloadPackage);
896 if (err != Interop.PackageManager.ErrorCode.None)
898 throw PackageManagerErrorFactory.GetException(err, "Failed to get preload info");
901 return isPreloadPackage;
905 /// Compare certificate of two packages
907 /// <param name="lhsPackageId">package id to compare</param>
908 /// <param name="rhsPackageId">package id to be compared</param>
909 /// <returns>Returns certificate comparison result.</returns>
910 /// <exception cref="ArgumentException">Thrown when failed when input package ID is invalid</exception>
911 /// <exception cref="System.IO.IOException">Thrown when method failed due to internal IO error</exception>
912 public static CertCompareResultType CompareCertInfo(string lhsPackageId, string rhsPackageId)
914 Interop.PackageManager.CertCompareResultType compareResult;
915 Interop.PackageManager.ErrorCode err = Interop.PackageManager.PackageManagerCompareCertInfo(lhsPackageId, rhsPackageId, out compareResult);
916 if (err != Interop.PackageManager.ErrorCode.None)
918 throw PackageManagerErrorFactory.GetException(err, "Failed to compare cert info");
921 return (CertCompareResultType)compareResult;
925 /// Compare certificate of two packages which contain each given application id
927 /// <param name="lhsApplicationId">application id to compare</param>
928 /// <param name="rhsApplicationId">application id to be compared</param>
929 /// <returns>Returns certificate comparison result.</returns>
930 /// <exception cref="ArgumentException">Thrown when failed when input package ID is invalid</exception>
931 /// <exception cref="System.IO.IOException">Thrown when method failed due to internal IO error</exception>
932 public static CertCompareResultType CompareCertInfoByApplicationId(string lhsApplicationId, string rhsApplicationId)
934 Interop.PackageManager.CertCompareResultType compareResult;
935 Interop.PackageManager.ErrorCode err = Interop.PackageManager.PackageManagerCompareCertInfoByApplicationId(lhsApplicationId, rhsApplicationId, out compareResult);
936 if (err != Interop.PackageManager.ErrorCode.None)
938 throw PackageManagerErrorFactory.GetException(err, "Failed to compare cert info by application id");
941 return (CertCompareResultType)compareResult;
945 /// Drm nested class. This class has the PackageManager's drm related methods.
947 public static class Drm
950 /// Generates request for getting license
952 /// <param name="responseData">Response data string of the purchase request</param>
953 /// <returns>Returns package drm information of given response data which contains require data and license url</returns>
954 /// <privilege>http://tizen.org/privilege/packagemanager.admin</privilege>
955 /// <privlevel>platform</privlevel>
956 /// <exception cref="ArgumentException">Thrown when failed when input package ID is invalid</exception>
957 /// <exception cref="OutOfMemoryException">Thrown when there is not enough memory to continue the execution of the method</exception>
958 /// <exception cref="UnauthorizedAccessException">Thrown when app does not have privilege to access this method</exception>
959 /// <exception cref="SystemException">Thrown when method failed due to internal system error</exception>
960 public static PackageDrm GenerateLicenseRequest(string responseData)
962 return PackageDrm.GenerateLicenseRequest(responseData);
967 /// Registers encrypted license
969 /// <param name="responseData">The response data string of the rights request</param>
970 /// <returns>Returns true if succeed. Otherwise return false</returns>
971 /// <privilege>http://tizen.org/privilege/packagemanager.admin</privilege>
972 /// <privlevel>platform</privlevel>
973 /// <exception cref="ArgumentException">Thrown when failed when input package ID is invalid</exception>
974 /// <exception cref="OutOfMemoryException">Thrown when there is not enough memory to continue the execution of the method</exception>
975 /// <exception cref="UnauthorizedAccessException">Thrown when app does not have privilege to access this method</exception>
976 /// <exception cref="SystemException">Thrown when method failed due to internal system error</exception>
977 public static bool RegisterLicense(string responseData)
979 Interop.PackageManager.ErrorCode err = Interop.PackageManager.PackageManagerDrmRegisterLicense(responseData);
980 if (err != Interop.PackageManager.ErrorCode.None)
982 throw PackageManagerErrorFactory.GetException(err, "Failed to register drm license");
989 /// Decrypts contents which is encrypted
991 /// <param name="drmFilePath">Drm file path</param>
992 /// <param name="decryptedFilePath">Decrypted file path</param>
993 /// <returns>Returns true if succeed. Otherwise return false</returns>
994 /// <privilege>http://tizen.org/privilege/packagemanager.admin</privilege>
995 /// <privlevel>platform</privlevel>
996 /// <exception cref="ArgumentException">Thrown when failed when input package ID is invalid</exception>
997 /// <exception cref="OutOfMemoryException">Thrown when there is not enough memory to continue the execution of the method</exception>
998 /// <exception cref="UnauthorizedAccessException">Thrown when app does not have privilege to access this method</exception>
999 /// <exception cref="SystemException">Thrown when method failed due to internal system error</exception>
1000 public static bool DecryptPackage(string drmFilePath, string decryptedFilePath)
1002 Interop.PackageManager.ErrorCode err = Interop.PackageManager.PackageManagerDrmDecryptPackage(drmFilePath, decryptedFilePath);
1003 if (err != Interop.PackageManager.ErrorCode.None)
1005 throw PackageManagerErrorFactory.GetException(err, "Failed to decrypt drm package");
1012 private static void SetPackageManagerEventStatus(Interop.PackageManager.EventStatus status)
1014 if (Handle.IsInvalid) return;
1016 Interop.PackageManager.EventStatus eventStatus = s_eventStatus;
1017 eventStatus |= status;
1018 if (eventStatus != Interop.PackageManager.EventStatus.All)
1019 eventStatus |= Interop.PackageManager.EventStatus.Progress;
1021 var err = Interop.PackageManager.ErrorCode.None;
1022 if (s_eventStatus != eventStatus)
1024 err = Interop.PackageManager.PackageManagerSetEvenStatus(Handle, eventStatus);
1025 if (err == Interop.PackageManager.ErrorCode.None)
1027 s_eventStatus = eventStatus;
1028 Log.Debug(LogTag, string.Format("New Event Status flag: {0}", s_eventStatus));
1031 Log.Debug(LogTag, string.Format("Failed to set flag for {0} event. err = {1}", eventStatus, err));
1035 private static void UnsetPackageManagerEventStatus()
1037 if (Handle.IsInvalid) return;
1039 Interop.PackageManager.EventStatus eventStatus = Interop.PackageManager.EventStatus.All;
1040 if (s_installEventHandler != null) eventStatus |= Interop.PackageManager.EventStatus.Install;
1041 if (s_uninstallEventHandler != null) eventStatus |= Interop.PackageManager.EventStatus.Uninstall;
1042 if (s_updateEventHandler != null) eventStatus |= Interop.PackageManager.EventStatus.Upgrade;
1043 if (s_moveEventHandler != null) eventStatus |= Interop.PackageManager.EventStatus.Move;
1044 if (s_clearDataEventHandler != null) eventStatus |= Interop.PackageManager.EventStatus.ClearData;
1045 if (eventStatus != Interop.PackageManager.EventStatus.All)
1046 eventStatus |= Interop.PackageManager.EventStatus.Progress;
1048 var err = Interop.PackageManager.ErrorCode.None;
1049 if (s_eventStatus != eventStatus)
1051 err = Interop.PackageManager.PackageManagerSetEvenStatus(Handle, eventStatus);
1052 if (err == Interop.PackageManager.ErrorCode.None)
1054 s_eventStatus = eventStatus;
1055 Log.Debug(LogTag, string.Format("New Event Status flag: {0}", s_eventStatus));
1058 Log.Debug(LogTag, string.Format("Failed to set flag for {0} event. err = {1}", eventStatus, err));
1062 private static void RegisterPackageManagerEventIfNeeded()
1064 if (s_installEventHandler != null && s_uninstallEventHandler != null && s_updateEventHandler != null && s_moveEventHandler != null && s_clearDataEventHandler != null)
1069 var err = Interop.PackageManager.ErrorCode.None;
1070 s_packageManagerEventCallback = (packageType, packageId, eventType, eventState, progress, error, user_data) =>
1074 if (eventType == Interop.PackageManager.EventType.Install)
1076 s_installEventHandler?.Invoke(null, new PackageManagerEventArgs(packageType, packageId, (PackageEventState)eventState, progress));
1078 else if (eventType == Interop.PackageManager.EventType.Uninstall)
1080 s_uninstallEventHandler?.Invoke(null, new PackageManagerEventArgs(packageType, packageId, (PackageEventState)eventState, progress));
1082 else if (eventType == Interop.PackageManager.EventType.Update)
1084 s_updateEventHandler?.Invoke(null, new PackageManagerEventArgs(packageType, packageId, (PackageEventState)eventState, progress));
1086 else if (eventType == Interop.PackageManager.EventType.Move)
1088 s_moveEventHandler?.Invoke(null, new PackageManagerEventArgs(packageType, packageId, (PackageEventState)eventState, progress));
1090 else if (eventType == Interop.PackageManager.EventType.ClearData)
1092 s_clearDataEventHandler?.Invoke(null, new PackageManagerEventArgs(packageType, packageId, (PackageEventState)eventState, progress));
1097 Log.Warn(LogTag, e.Message);
1101 if (!Handle.IsInvalid)
1103 Log.Debug(LogTag, "Reset Package Event");
1104 err = Interop.PackageManager.PackageManagerUnsetEvent(Handle);
1105 if (err != Interop.PackageManager.ErrorCode.None)
1107 throw PackageManagerErrorFactory.GetException(err, "Failed to unregister package manager event event.");
1110 err = Interop.PackageManager.PackageManagerSetEvent(Handle, s_packageManagerEventCallback, IntPtr.Zero);
1112 if (err != Interop.PackageManager.ErrorCode.None)
1114 Log.Warn(LogTag, string.Format("Failed to register callback for package manager event. err = {0}", err));
1118 private static void UnregisterPackageManagerEventIfNeeded()
1120 if (Handle.IsInvalid || s_installEventHandler != null || s_uninstallEventHandler != null || s_updateEventHandler != null || s_moveEventHandler != null || s_clearDataEventHandler != null)
1125 var err = Interop.PackageManager.PackageManagerUnsetEvent(Handle);
1126 if (err != Interop.PackageManager.ErrorCode.None)
1128 throw PackageManagerErrorFactory.GetException(err, "Failed to unregister package manager event event.");
1133 internal static class PackageManagerErrorFactory
1135 internal static Exception GetException(Interop.PackageManager.ErrorCode err, string message)
1137 string errMessage = string.Format("{0} err = {1}", message, err);
1140 case Interop.PackageManager.ErrorCode.InvalidParameter:
1141 case Interop.PackageManager.ErrorCode.NoSuchPackage:
1142 return new ArgumentException(errMessage);
1143 case Interop.PackageManager.ErrorCode.PermissionDenied:
1144 return new UnauthorizedAccessException(errMessage);
1145 case Interop.PackageManager.ErrorCode.IoError:
1146 return new global::System.IO.IOException(errMessage);
1148 return new InvalidOperationException(errMessage);