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 /// <since_tizen> 3 </since_tizen>
32 public static class PackageManager
34 private const string LogTag = "Tizen.Applications.PackageManager";
36 private static SafePackageManagerHandle s_handle = new SafePackageManagerHandle();
37 private static Interop.PackageManager.EventStatus s_eventStatus = Interop.PackageManager.EventStatus.All;
38 private static event EventHandler<PackageManagerEventArgs> s_installEventHandler;
39 private static event EventHandler<PackageManagerEventArgs> s_uninstallEventHandler;
40 private static event EventHandler<PackageManagerEventArgs> s_updateEventHandler;
41 private static event EventHandler<PackageManagerEventArgs> s_moveEventHandler;
42 private static event EventHandler<PackageManagerEventArgs> s_clearDataEventHandler;
44 private static Interop.PackageManager.PackageManagerEventCallback s_packageManagerEventCallback;
47 /// Event callback method for the request.
49 /// <param name="type">Type of the package which was requested.</param>
50 /// <param name="packageId">ID of the package which was requested.</param>
51 /// <param name="eventType">Event type of the request.</param>
52 /// <param name="eventState">Current event state of the request.</param>
53 /// <param name="progress">Progress for the request being processed by the package manager (in percent).</param>
54 /// <since_tizen> 3 </since_tizen>
55 public delegate void RequestEventCallback(string type, string packageId, PackageEventType eventType, PackageEventState eventState, int progress);
57 private static Dictionary<int, RequestEventCallback> RequestCallbacks = new Dictionary<int, RequestEventCallback>();
58 private static Dictionary<int, SafePackageManagerRequestHandle> RequestHandles = new Dictionary<int, SafePackageManagerRequestHandle>();
60 private delegate Interop.PackageManager.ErrorCode InstallMethodWithCallback(SafePackageManagerRequestHandle requestHandle, string pkgPath, Interop.PackageManager.PackageManagerRequestEventCallback requestCallback, IntPtr userData, out int requestID);
61 private delegate Interop.PackageManager.ErrorCode InstallMethod(SafePackageManagerRequestHandle requestHandle, string pkgPath, out int requestID);
64 /// InstallProgressChanged event. This event occurs when a package is getting installed and the progress of the request to the package manager is changed.
66 /// <since_tizen> 3 </since_tizen>
67 public static event EventHandler<PackageManagerEventArgs> InstallProgressChanged
71 SetPackageManagerEventStatus(Interop.PackageManager.EventStatus.Install);
72 RegisterPackageManagerEventIfNeeded();
73 s_installEventHandler += value;
77 s_installEventHandler -= value;
78 UnregisterPackageManagerEventIfNeeded();
79 UnsetPackageManagerEventStatus();
84 /// UninstallProgressChanged event. This event occurs when a package is getting uninstalled and the progress of the request to the package manager is changed.
86 /// <since_tizen> 3 </since_tizen>
87 public static event EventHandler<PackageManagerEventArgs> UninstallProgressChanged
91 SetPackageManagerEventStatus(Interop.PackageManager.EventStatus.Uninstall);
92 RegisterPackageManagerEventIfNeeded();
93 s_uninstallEventHandler += value;
97 s_uninstallEventHandler -= value;
98 UnregisterPackageManagerEventIfNeeded();
99 UnsetPackageManagerEventStatus();
104 /// UpdateProgressChanged event. This event occurs when a package is getting updated and the progress of the request to the package manager is changed.
106 /// <since_tizen> 3 </since_tizen>
107 public static event EventHandler<PackageManagerEventArgs> UpdateProgressChanged
111 SetPackageManagerEventStatus(Interop.PackageManager.EventStatus.Upgrade);
112 RegisterPackageManagerEventIfNeeded();
113 s_updateEventHandler += value;
117 s_updateEventHandler -= value;
118 UnregisterPackageManagerEventIfNeeded();
119 UnsetPackageManagerEventStatus();
124 /// MoveProgressChanged event. This event occurs when a package is getting moved and the progress of the request to the package manager is changed.
126 /// <since_tizen> 3 </since_tizen>
127 public static event EventHandler<PackageManagerEventArgs> MoveProgressChanged
131 SetPackageManagerEventStatus(Interop.PackageManager.EventStatus.Move);
132 RegisterPackageManagerEventIfNeeded();
133 s_moveEventHandler += value;
137 s_moveEventHandler -= value;
138 UnregisterPackageManagerEventIfNeeded();
139 UnsetPackageManagerEventStatus();
144 /// ClearDataProgressChanged event. This event occurs when data directories are cleared in the given package.
146 /// <since_tizen> 3 </since_tizen>
147 public static event EventHandler<PackageManagerEventArgs> ClearDataProgressChanged
151 SetPackageManagerEventStatus(Interop.PackageManager.EventStatus.ClearData);
152 RegisterPackageManagerEventIfNeeded();
153 s_clearDataEventHandler += value;
157 s_clearDataEventHandler -= value;
158 UnregisterPackageManagerEventIfNeeded();
159 UnsetPackageManagerEventStatus();
163 private static SafePackageManagerHandle Handle
167 if (s_handle.IsInvalid)
169 var err = Interop.PackageManager.PackageManagerCreate(out s_handle);
170 if (err != Interop.PackageManager.ErrorCode.None)
172 Log.Warn(LogTag, string.Format("Failed to create package manager handle. err = {0}", err));
179 private static Interop.PackageManager.PackageManagerRequestEventCallback internalRequestEventCallback = (id, packageType, packageId, eventType, eventState, progress, error, userData) =>
181 if (RequestCallbacks.ContainsKey(id))
185 RequestCallbacks[id](packageType, packageId, (PackageEventType)eventType, (PackageEventState)eventState, progress);
186 if (eventState == Interop.PackageManager.PackageEventState.Completed || eventState == Interop.PackageManager.PackageEventState.Failed)
188 Log.Debug(LogTag, string.Format("release request handle for id : {0}", id));
189 RequestHandles[id].Dispose();
190 RequestHandles.Remove(id);
191 RequestCallbacks.Remove(id);
196 Log.Warn(LogTag, e.Message);
197 RequestHandles[id].Dispose();
198 RequestHandles.Remove(id);
199 RequestCallbacks.Remove(id);
205 /// Gets the package ID for the given application ID.
207 /// <param name="applicationId">The ID of the application.</param>
208 /// <returns>Returns the ID of the package. Empty string if the application ID does not exist.</returns>
209 /// <exception cref="OutOfMemoryException">Thrown when there is not enough memory to continue the execution of the method.</exception>
210 /// <exception cref="UnauthorizedAccessException">Thrown when an application does not have the privilege to access this method.</exception>
211 /// <privilege>http://tizen.org/privilege/packagemanager.info</privilege>
212 /// <since_tizen> 3 </since_tizen>
213 public static string GetPackageIdByApplicationId(string applicationId)
216 var err = Interop.PackageManager.PackageManageGetPackageIdByAppId(applicationId, out packageId);
217 if (err != Interop.PackageManager.ErrorCode.None)
219 Log.Warn(LogTag, string.Format("Failed to get package Id of {0}. err = {1}", applicationId, err));
220 if (err != Interop.PackageManager.ErrorCode.InvalidParameter)
222 throw PackageManagerErrorFactory.GetException(err, "Failed to get package Id");
229 /// Gets the package information for the given package.
231 /// <param name="packageId">The ID of the package.</param>
232 /// <returns>Returns the package information for the given package ID.</returns>
233 /// <exception cref="ArgumentException">Thrown when the failed input package ID is invalid.</exception>
234 /// <exception cref="OutOfMemoryException">Thrown when there is not enough memory to continue the execution of the method.</exception>
235 /// <exception cref="System.IO.IOException">Thrown when the method fails due to an internal I/O error.</exception>
236 /// <exception cref="UnauthorizedAccessException">Thrown when an application does not have the privilege to access this method.</exception>
237 /// <privilege>http://tizen.org/privilege/packagemanager.info</privilege>
238 /// <since_tizen> 3 </since_tizen>
239 public static Package GetPackage(string packageId)
241 return Package.GetPackage(packageId);
245 /// Clears the application's internal and external cache directories.
247 /// <param name="packageId">ID of the package.</param>
248 /// <exception cref="OutOfMemoryException">Thrown when there is not enough memory to continue the execution of the method.</exception>
249 /// <exception cref="System.IO.IOException">Thrown when the method fails due to an internal I/O error.</exception>
250 /// <exception cref="UnauthorizedAccessException">Thrown when an application does not have the privilege to access this method.</exception>
251 /// <exception cref="SystemException">Thrown when the method failed due to an internal system error.</exception>
252 /// <privilege>http://tizen.org/privilege/packagemanager.clearcache</privilege>
253 /// <since_tizen> 3 </since_tizen>
254 public static void ClearCacheDirectory(string packageId)
256 Interop.PackageManager.ErrorCode err = Interop.PackageManager.PackageManagerClearCacheDir(packageId);
257 if (err != Interop.PackageManager.ErrorCode.None)
259 Log.Warn(LogTag, string.Format("Failed to clear cache directory for {0}. err = {1}", packageId, err));
260 throw PackageManagerErrorFactory.GetException(err, "Failed to clear cache directory");
265 /// Clears all the application's internal and external cache directories.
267 /// <exception cref="OutOfMemoryException">Thrown when there is not enough memory to continue the execution of the method.</exception>
268 /// <exception cref="System.IO.IOException">Thrown when the method fails due to an internal IO error.</exception>
269 /// <exception cref="UnauthorizedAccessException">Thrown when an application does not have the privilege to access this method.</exception>
270 /// <exception cref="SystemException">Thrown when the method failed due to an internal system error.</exception>
271 /// <privilege>http://tizen.org/privilege/packagemanager.admin</privilege>
272 /// <privlevel>platform</privlevel>
273 /// <since_tizen> 3 </since_tizen>
274 public static void ClearAllCacheDirectory()
276 var err = Interop.PackageManager.PackageManagerClearAllCacheDir();
277 if (err != Interop.PackageManager.ErrorCode.None)
279 Log.Warn(LogTag, string.Format("Failed to clear all cache directories. err = {0}", err));
280 throw PackageManagerErrorFactory.GetException(err, "Failed to clear all cache directories");
285 /// Clears the application's internal and external data directories.
288 /// All files under data, shared/data, and shared/trusted in the internal storage are removed.
289 /// And, if the external storage exists, then all files under data and shared/trusted in the external storage are removed.
291 /// <param name="packageId">ID of the package.</param>
292 /// <exception cref="OutOfMemoryException">Thrown when there is not enough memory to continue the execution of the method.</exception>
293 /// <exception cref="System.IO.IOException">Thrown when the method failed due to an internal IO error.</exception>
294 /// <exception cref="UnauthorizedAccessException">Thrown when an application does not have the privilege to access this method.</exception>
295 /// <exception cref="SystemException">Thrown when the method failed due to an internal system error.</exception>
296 /// <privilege>http://tizen.org/privilege/packagemanager.admin</privilege>
297 /// <privlevel>platform</privlevel>
298 /// <since_tizen> 3 </since_tizen>
299 public static void ClearDataDirectory(string packageId)
301 Interop.PackageManager.ErrorCode err = Interop.PackageManager.PackageManagerClearDataDir(packageId);
302 if (err != Interop.PackageManager.ErrorCode.None)
304 Log.Warn(LogTag, string.Format("Failed to clear data directory for {0}. err = {1}", packageId, err));
305 throw PackageManagerErrorFactory.GetException(err, "Failed to clear data directory");
310 /// Retrieves the package information of all installed packages.
312 /// <returns>Returns the list of packages.</returns>
313 /// <privilege>http://tizen.org/privilege/packagemanager.info</privilege>
314 /// <since_tizen> 3 </since_tizen>
315 public static IEnumerable<Package> GetPackages()
317 return GetPackages(null);
321 /// Retrieves the package information of all the installed packages satisfying the filter conditions.
323 /// <param name="filter">Optional - package filters.</param>
324 /// <returns>Returns the list of packages.</returns>
325 /// <privilege>http://tizen.org/privilege/packagemanager.info</privilege>
326 /// <since_tizen> 3 </since_tizen>
327 public static IEnumerable<Package> GetPackages(PackageFilter filter)
329 List<Package> packageList = new List<Package>();
332 var err = Interop.PackageManager.PackageManagerFilterCreate(out filterHandle);
333 if (err != Interop.PackageManager.ErrorCode.None)
335 Log.Warn(LogTag, string.Format("Failed to create package filter handle. err = {0}", err));
339 if (filter != null && filter.Filters != null)
341 foreach (KeyValuePair<string, bool> entry in filter?.Filters)
343 err = Interop.PackageManager.PackageManagerFilterAdd(filterHandle, entry.Key, entry.Value);
344 if (err != Interop.PackageManager.ErrorCode.None)
346 Log.Warn(LogTag, string.Format("Failed to configure package filter. err = {0}", err));
352 if (err == Interop.PackageManager.ErrorCode.None)
354 Interop.PackageManager.PackageManagerPackageInfoCallback cb = (handle, userData) =>
356 packageList.Add(Package.GetPackage(handle));
360 err = Interop.PackageManager.PackageManagerFilterForeachPackageInfo(filterHandle, cb, IntPtr.Zero);
361 if (err != Interop.PackageManager.ErrorCode.None)
363 Log.Warn(LogTag, string.Format("Failed to get package Informations. err = {0}", err));
367 err = Interop.PackageManager.PackageManagerFilterDestroy(filterHandle);
368 if (err != Interop.PackageManager.ErrorCode.None)
370 Log.Warn(LogTag, string.Format("Failed to destroy package filter handle. err = {0}", err));
376 /// Gets the total package size information.
378 /// <returns>Returns the total package size information asynchronously.</returns>
379 /// <privilege>http://tizen.org/privilege/packagemanager.info</privilege>
380 /// <since_tizen> 3 </since_tizen>
381 public static async Task<PackageSizeInformation> GetTotalSizeInformationAsync()
383 TaskCompletionSource<PackageSizeInformation> tcs = new TaskCompletionSource<PackageSizeInformation>();
384 Interop.PackageManager.PackageManagerTotalSizeInfoCallback cb = (handle, userData) =>
386 if (handle != IntPtr.Zero)
388 tcs.TrySetResult(PackageSizeInformation.GetPackageSizeInformation(handle));
392 var err = Interop.PackageManager.PackageManagerGetTotalSizeInfo(cb, IntPtr.Zero);
393 if (err != Interop.PackageManager.ErrorCode.None)
395 tcs.TrySetException(PackageManagerErrorFactory.GetException(err, "Failed to get total package size info"));
397 return await tcs.Task.ConfigureAwait(false);
401 /// Installs the package located at the given path.
403 /// <param name="packagePath">Absolute path for the package to be installed.</param>
404 /// <param name="installMode">Optional parameter to indicate special installation mode.</param>
405 /// <returns>Returns true if the installation request is successful, otherwise false.</returns>
407 /// The 'true' means that the request for installation is successful.
408 /// To check the result of the installation, the caller should check the progress using the InstallProgressChanged event.
410 /// <privilege>http://tizen.org/privilege/packagemanager.admin</privilege>
411 /// <privlevel>platform</privlevel>
412 /// <since_tizen> 3 </since_tizen>
413 public static bool Install(string packagePath, InstallationMode installMode = InstallationMode.Normal)
415 return Install(packagePath, null, PackageType.UNKNOWN, null, installMode);
419 /// Installs the package located at the given path.
421 /// <param name="packagePath">Absolute path for the package to be installed.</param>
422 /// <param name="eventCallback">The event callback will be invoked only for the current request.</param>
423 /// <param name="installMode">Optional parameter to indicate special installation mode.</param>
424 /// <returns>Returns true if installtion request is successful, false otherwise.</returns>
426 /// The 'true' means that the request for installation is successful.
427 /// To check the result of installation, the caller should check the progress using the InstallProgressChanged event or eventCallback.
429 /// <privilege>http://tizen.org/privilege/packagemanager.admin</privilege>
430 /// <privlevel>platform</privlevel>
431 /// <since_tizen> 3 </since_tizen>
432 public static bool Install(string packagePath, RequestEventCallback eventCallback, InstallationMode installMode = InstallationMode.Normal)
434 return Install(packagePath, null, PackageType.UNKNOWN, eventCallback, installMode);
438 /// Installs the package located at the given path.
440 /// <param name="packagePath">Absolute path for the package to be installed.</param>
441 /// <param name="type">Package type for the 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 the request for installation is successful.
446 /// To check the result of installation, the caller should check the progress using the InstallProgressChanged event.
448 /// <privilege>http://tizen.org/privilege/packagemanager.admin</privilege>
449 /// <privlevel>platform</privlevel>
450 /// <since_tizen> 3 </since_tizen>
451 public static bool Install(string packagePath, PackageType type, InstallationMode installMode = InstallationMode.Normal)
453 return Install(packagePath, null, type, 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="expansionPackagePath">Absolute path for the expansion package to be installed.</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 the request for installation is successful.
465 /// To check the result of installation, the caller should check the progress using the InstallProgressChanged event.
467 /// <privilege>http://tizen.org/privilege/packagemanager.admin</privilege>
468 /// <privlevel>platform</privlevel>
469 /// <since_tizen> 3 </since_tizen>
470 public static bool Install(string packagePath, string expansionPackagePath, InstallationMode installMode = InstallationMode.Normal)
472 return Install(packagePath, expansionPackagePath, PackageType.UNKNOWN, null, 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="type">Package type for the 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 /// <since_tizen> 3 </since_tizen>
490 public static bool Install(string packagePath, PackageType type, RequestEventCallback eventCallback, InstallationMode installMode = InstallationMode.Normal)
492 return Install(packagePath, null, type, eventCallback, installMode);
496 /// Installs the package located at the given path.
498 /// <param name="packagePath">Absolute path for the package to be installed.</param>
499 /// <param name="expansionPackagePath">Absolute path for the expansion package to be installed.</param>
500 /// <param name="eventCallback">The event callback will be invoked only for the current request.</param>
501 /// <param name="installMode">Optional parameter to indicate special installation mode.</param>
502 /// <returns>Returns true if installtion request is successful, false otherwise.</returns>
504 /// The 'true' means that the request for installation is successful.
505 /// To check the result of installation, the caller should check the progress using the InstallProgressChanged event or eventCallback.
507 /// <privilege>http://tizen.org/privilege/packagemanager.admin</privilege>
508 /// <privlevel>platform</privlevel>
509 /// <since_tizen> 3 </since_tizen>
510 public static bool Install(string packagePath, string expansionPackagePath, RequestEventCallback eventCallback, InstallationMode installMode = InstallationMode.Normal)
512 return Install(packagePath, expansionPackagePath, PackageType.UNKNOWN, eventCallback, installMode);
516 /// Installs the package located at the given path.
518 /// <param name="packagePath">Absolute path for the package to be installed.</param>
519 /// <param name="expansionPackagePath">Absolute path for the expansion package to be installed.</param>
520 /// <param name="type">Package type for the package to be installed.</param>
521 /// <param name="installMode">Optional parameter to indicate special installation mode.</param>
522 /// <returns>Returns true if installtion request is successful, false otherwise.</returns>
524 /// The 'true' means that the request for installation is successful.
525 /// To check the result of installation, the caller should check the progress using the InstallProgressChanged event.
527 /// <privilege>http://tizen.org/privilege/packagemanager.admin</privilege>
528 /// <privlevel>platform</privlevel>
529 /// <since_tizen> 3 </since_tizen>
530 public static bool Install(string packagePath, string expansionPackagePath, PackageType type, InstallationMode installMode = InstallationMode.Normal)
532 return Install(packagePath, expansionPackagePath, type, null, installMode);
536 /// Installs the package located at the given path.
538 /// <param name="packagePath">Absolute path for the package to be installed.</param>
539 /// <param name="expansionPackagePath">Absolute path for the expansion package to be installed.</param>
540 /// <param name="type">Package type for the package to be installed.</param>
541 /// <param name="eventCallback">The event callback will be invoked only for the current request.</param>
542 /// <param name="installMode">Optional parameter to indicate special installation mode.</param>
543 /// <returns>Returns true if installtion request is successful, false otherwise.</returns>
545 /// The 'true' means that the request for installation is successful.
546 /// To check the result of installation, the caller should check the progress using the InstallProgressChanged event or eventCallback.
548 /// <privilege>http://tizen.org/privilege/packagemanager.admin</privilege>
549 /// <privlevel>platform</privlevel>
550 /// <since_tizen> 3 </since_tizen>
551 public static bool Install(string packagePath, string expansionPackagePath, PackageType type, RequestEventCallback eventCallback, InstallationMode installMode = InstallationMode.Normal)
553 SafePackageManagerRequestHandle RequestHandle;
554 var err = Interop.PackageManager.PackageManagerRequestCreate(out RequestHandle);
555 if (err != Interop.PackageManager.ErrorCode.None)
557 Log.Warn(LogTag, string.Format("Failed to install package {0}. Error in creating package manager request handle. err = {1}", packagePath, err));
563 if (type != PackageType.UNKNOWN)
565 err = Interop.PackageManager.PackageManagerRequestSetType(RequestHandle, type.ToString().ToLower());
566 if (err != Interop.PackageManager.ErrorCode.None)
568 Log.Warn(LogTag, string.Format("Failed to install package {0}. Error in setting request package type. err = {1}", packagePath, err));
569 RequestHandle.Dispose();
574 if (!string.IsNullOrEmpty(expansionPackagePath))
576 err = Interop.PackageManager.PackageManagerRequestSetTepPath(RequestHandle, expansionPackagePath);
577 if (err != Interop.PackageManager.ErrorCode.None)
579 Log.Warn(LogTag, string.Format("Failed to install package {0}. Error in setting request package mode. err = {1}", packagePath, err));
580 RequestHandle.Dispose();
586 if (eventCallback != null)
588 InstallMethodWithCallback install;
589 if (installMode == InstallationMode.Mount)
591 install = Interop.PackageManager.PackageManagerRequestMountInstallWithCB;
595 install = Interop.PackageManager.PackageManagerRequestInstallWithCB;
597 err = install(RequestHandle, packagePath, internalRequestEventCallback, IntPtr.Zero, out requestId);
598 if (err == Interop.PackageManager.ErrorCode.None)
600 RequestCallbacks.Add(requestId, eventCallback);
601 RequestHandles.Add(requestId, RequestHandle);
605 Log.Warn(LogTag, string.Format("Failed to install package {0}. err = {1}", packagePath, err));
606 RequestHandle.Dispose();
612 InstallMethod install;
613 if (installMode == InstallationMode.Mount)
615 install = Interop.PackageManager.PackageManagerRequestMountInstall;
619 install = Interop.PackageManager.PackageManagerRequestInstall;
621 err = install(RequestHandle, packagePath, out requestId);
622 if (err != Interop.PackageManager.ErrorCode.None)
624 Log.Warn(LogTag, string.Format("Failed to install package {0}. err = {1}", packagePath, err));
625 RequestHandle.Dispose();
628 // RequestHandle isn't necessary when this method is called without 'eventCallback' parameter.
629 RequestHandle.Dispose();
635 Log.Warn(LogTag, e.Message);
636 RequestHandle.Dispose();
642 /// Uninstalls the package with the given name.
644 /// <param name="packageId">ID of the package to be uninstalled.</param>
645 /// <returns>Returns true if the uninstallation request is successful, false otherwise.</returns>
647 /// The 'true' means that the request for uninstallation is successful.
648 /// To check the result of uninstallation, the caller should check the progress using the UninstallProgressChanged event.
650 /// <privilege>http://tizen.org/privilege/packagemanager.admin</privilege>
651 /// <privlevel>platform</privlevel>
652 /// <since_tizen> 3 </since_tizen>
653 public static bool Uninstall(string packageId)
655 return Uninstall(packageId, PackageType.UNKNOWN, null);
659 /// Uninstalls package with the given names.
661 /// <param name="packageId">ID of the package to be uninstalled.</param>
662 /// <param name="type">Optional - Package type for the package to be uninstalled.</param>
663 /// <returns>Returns true if the uninstalltion request is successful, false otherwise.</returns>
665 /// The 'true' means that the request for uninstallation is successful.
666 /// To check the result of uninstallation, the caller should check the progress using the UninstallProgressChanged event.
668 /// <privilege>http://tizen.org/privilege/packagemanager.admin</privilege>
669 /// <privlevel>platform</privlevel>
670 /// <since_tizen> 3 </since_tizen>
671 public static bool Uninstall(string packageId, PackageType type)
673 return Uninstall(packageId, type, null);
677 /// Uninstalls the package with the given name.
679 /// <param name="packageId">ID of the package to be uninstalled.</param>
680 /// <param name="eventCallback">Optional - The event callback will be invoked only for the current request.</param>
681 /// <returns>Returns true if the uninstallation request is successful, false otherwise.</returns>
683 /// The 'true' means that the request for uninstallation is successful.
684 /// To check the result of uninstallation, the caller should check the progress using the UninstallProgressChanged event or eventCallback.
686 /// <privilege>http://tizen.org/privilege/packagemanager.admin</privilege>
687 /// <privlevel>platform</privlevel>
688 /// <since_tizen> 3 </since_tizen>
689 public static bool Uninstall(string packageId, RequestEventCallback eventCallback)
691 return Uninstall(packageId, PackageType.UNKNOWN, eventCallback);
695 /// Uninstalls the package with the given name.
697 /// <param name="packageId">ID of the package to be uninstalled</param>
698 /// <param name="type">Optional - Package type for the package to be uninstalled.</param>
699 /// <param name="eventCallback">Optional - The event callback will be invoked only for the current request.</param>
700 /// <returns>Returns true if the uninstallation request is successful, false otherwise.</returns>
702 /// The 'true' means that the request for uninstallation is successful.
703 /// To check the result of uninstallation, the caller should check the progress using the UninstallProgressChanged event or eventCallback.
705 /// <privilege>http://tizen.org/privilege/packagemanager.admin</privilege>
706 /// <privlevel>platform</privlevel>
707 /// <since_tizen> 3 </since_tizen>
708 public static bool Uninstall(string packageId, PackageType type, RequestEventCallback eventCallback)
710 SafePackageManagerRequestHandle RequestHandle;
711 var err = Interop.PackageManager.PackageManagerRequestCreate(out RequestHandle);
712 if (err != Interop.PackageManager.ErrorCode.None)
714 Log.Warn(LogTag, string.Format("Failed to uninstall package {0}. Error in creating package manager request handle. err = {1}", packageId, err));
720 err = Interop.PackageManager.PackageManagerRequestSetType(RequestHandle, type.ToString().ToLower());
721 if (err != Interop.PackageManager.ErrorCode.None)
723 Log.Warn(LogTag, string.Format("Failed to uninstall package {0}. Error in setting request package type. err = {1}", packageId, err));
724 RequestHandle.Dispose();
729 if (eventCallback != null)
731 err = Interop.PackageManager.PackageManagerRequestUninstallWithCB(RequestHandle, packageId, internalRequestEventCallback, IntPtr.Zero, out requestId);
732 if (err == Interop.PackageManager.ErrorCode.None)
734 RequestCallbacks.Add(requestId, eventCallback);
735 RequestHandles.Add(requestId, RequestHandle);
739 Log.Warn(LogTag, string.Format("Failed to uninstall package {0}. err = {1}", packageId, err));
740 RequestHandle.Dispose();
746 err = Interop.PackageManager.PackageManagerRequestUninstall(RequestHandle, packageId, out requestId);
747 if (err != Interop.PackageManager.ErrorCode.None)
749 Log.Warn(LogTag, string.Format("Failed to uninstall package. err = {0}", err));
750 RequestHandle.Dispose();
753 // RequestHandle isn't necessary when this method is called without 'eventCallback' parameter.
754 RequestHandle.Dispose();
760 Log.Warn(LogTag, e.Message);
761 RequestHandle.Dispose();
767 /// Moves the package to the given storage.
769 /// <param name="packageId">ID of the package to be moved.</param>
770 /// <param name="newStorage">Storage package should be moved to.</param>
771 /// <returns>Returns true if the move request is successful, false otherwise.</returns>
773 /// The 'true' means that the request for move is successful.
774 /// To check the result of move, the caller should check the progress using the MoveProgressChanged event.
776 /// <privilege>http://tizen.org/privilege/packagemanager.admin</privilege>
777 /// <privlevel>platform</privlevel>
778 /// <since_tizen> 3 </since_tizen>
779 public static bool Move(string packageId, StorageType newStorage)
781 return Move(packageId, PackageType.UNKNOWN, newStorage, null);
785 /// Moves the package to the given storage.
787 /// <param name="packageId">ID of the package to be moved.</param>
788 /// <param name="type">Optional - Package type for the package to be moved.</param>
789 /// <param name="newStorage">Storage package should be moved to.</param>
790 /// <returns>Returns true if the move request is successful, false otherwise.</returns>
792 /// The 'true' means that the request for move is successful.
793 /// To check the result of move, the caller should check the progress using the MoveProgressChanged event.
795 /// <privilege>http://tizen.org/privilege/packagemanager.admin</privilege>
796 /// <privlevel>platform</privlevel>
797 /// <since_tizen> 3 </since_tizen>
798 public static bool Move(string packageId, PackageType type, StorageType newStorage)
800 return Move(packageId, type, newStorage, null);
804 /// Moves the package to the given storage.
806 /// <param name="packageId">ID of the package to be moved.</param>
807 /// <param name="newStorage">Storage package should be moved to.</param>
808 /// <param name="eventCallback">Optional - The event callback will be invoked only for the current request.</param>
809 /// <returns>Returns true if move request is successful, false otherwise.</returns>
811 /// The 'true' means that the request for move is successful.
812 /// To check the result of move, the caller should check the progress using the MoveProgressChanged event.
814 /// <privilege>http://tizen.org/privilege/packagemanager.admin</privilege>
815 /// <privlevel>platform</privlevel>
816 /// <since_tizen> 3 </since_tizen>
817 public static bool Move(string packageId, StorageType newStorage, RequestEventCallback eventCallback)
819 return Move(packageId, PackageType.UNKNOWN, newStorage, eventCallback);
823 /// Moves the package to the given storage.
825 /// <param name="packageId">ID of the package to be moved.</param>
826 /// <param name="type">Optional - Package type for the package to be moved.</param>
827 /// <param name="newStorage">Storage, package should be moved to.</param>
828 /// <param name="eventCallback">Optional - The event callback will be invoked only for the current request.</param>
829 /// <returns>Returns true if move request is successful, false otherwise.</returns>
831 /// The 'true' means that the request for move is successful.
832 /// To check the result of move, the caller should check the progress using the MoveProgressChanged event.
834 /// <privilege>http://tizen.org/privilege/packagemanager.admin</privilege>
835 /// <privlevel>platform</privlevel>
836 /// <since_tizen> 3 </since_tizen>
837 public static bool Move(string packageId, PackageType type, StorageType newStorage, RequestEventCallback eventCallback)
839 SafePackageManagerRequestHandle RequestHandle;
840 var err = Interop.PackageManager.PackageManagerRequestCreate(out RequestHandle);
841 if (err != Interop.PackageManager.ErrorCode.None)
843 Log.Warn(LogTag, string.Format("Failed to create package manager request handle. err = {0}", err));
850 err = Interop.PackageManager.PackageManagerRequestSetType(RequestHandle, type.ToString().ToLower());
851 if (err != Interop.PackageManager.ErrorCode.None)
853 Log.Warn(LogTag, string.Format("Failed to move package. Error in setting request package type. err = {0}", err));
854 RequestHandle.Dispose();
858 if (eventCallback != null)
861 err = Interop.PackageManager.PackageManagerRequestMoveWithCB(RequestHandle, packageId, (Interop.PackageManager.StorageType)newStorage, internalRequestEventCallback, IntPtr.Zero, out requestId);
862 if (err == Interop.PackageManager.ErrorCode.None)
864 RequestCallbacks.Add(requestId, eventCallback);
865 RequestHandles.Add(requestId, RequestHandle);
869 Log.Warn(LogTag, string.Format("Failed to move package to requested location. err = {0}", err));
870 RequestHandle.Dispose();
876 err = Interop.PackageManager.PackageManagerRequestMove(RequestHandle, packageId, (Interop.PackageManager.StorageType)newStorage);
877 if (err != Interop.PackageManager.ErrorCode.None)
879 Log.Warn(LogTag, string.Format("Failed to move package to requested location. err = {0}", err));
880 RequestHandle.Dispose();
883 // RequestHandle isn't necessary when this method is called without 'eventCallback' parameter.
884 RequestHandle.Dispose();
890 Log.Warn(LogTag, e.Message);
891 RequestHandle.Dispose();
897 /// Gets the permission type of the package which has a given application ID.
899 /// <param name="applicationId">ID of the application.</param>
900 /// <returns>Returns the permission type.</returns>
901 /// <privilege>http://tizen.org/privilege/packagemanager.info</privilege>
902 /// <exception cref="ArgumentException">Thrown when the failed input package ID is invalid.</exception>
903 /// <exception cref="UnauthorizedAccessException">Thrown when an application does not have the privilege to access this method.</exception>
904 /// <since_tizen> 3 </since_tizen>
905 public static PermissionType GetPermissionTypeByApplicationId(string applicationId)
907 Interop.PackageManager.PackageManagerPermissionType permissionType;
908 Interop.PackageManager.ErrorCode err = Interop.PackageManager.PackageManagerGetPermissionType(applicationId, out permissionType);
909 if (err != Interop.PackageManager.ErrorCode.None)
911 throw PackageManagerErrorFactory.GetException(err, "Failed to get permission type.");
914 return (PermissionType)permissionType;
918 /// Gets the package's preload attribute which contains a given application ID.
920 /// <param name="applicationId">ID of the application.</param>
921 /// <returns>Returns true if the package is preloaded, otherwise false.</returns>
922 /// <privilege>http://tizen.org/privilege/packagemanager.info</privilege>
923 /// <exception cref="ArgumentException">Thrown when the failed input package ID is invalid.</exception>
924 /// <exception cref="UnauthorizedAccessException">Thrown when an application does not have the privilege to access this method.</exception>
925 /// <since_tizen> 3 </since_tizen>
926 public static bool IsPreloadPackageByApplicationId(string applicationId)
928 bool isPreloadPackage;
929 Interop.PackageManager.ErrorCode err = Interop.PackageManager.PackageManagerIsPreloadPackageByApplicationId(applicationId, out isPreloadPackage);
930 if (err != Interop.PackageManager.ErrorCode.None)
932 throw PackageManagerErrorFactory.GetException(err, "Failed to get preload info");
935 return isPreloadPackage;
939 /// Compares the certificate of the two packages.
941 /// <param name="lhsPackageId">Package ID to compare.</param>
942 /// <param name="rhsPackageId">Package ID to be compared.</param>
943 /// <returns>Returns certificate comparison result.</returns>
944 /// <exception cref="ArgumentException">Thrown when the failed input package ID is invalid.</exception>
945 /// <exception cref="System.IO.IOException">Thrown when the method failed due to an internal I/O error.</exception>
946 /// <since_tizen> 3 </since_tizen>
947 public static CertCompareResultType CompareCertInfo(string lhsPackageId, string rhsPackageId)
949 Interop.PackageManager.CertCompareResultType compareResult;
950 Interop.PackageManager.ErrorCode err = Interop.PackageManager.PackageManagerCompareCertInfo(lhsPackageId, rhsPackageId, out compareResult);
951 if (err != Interop.PackageManager.ErrorCode.None)
953 throw PackageManagerErrorFactory.GetException(err, "Failed to compare cert info");
956 return (CertCompareResultType)compareResult;
960 /// Compares the certificate of the two packages which contain each given application ID.
962 /// <param name="lhsApplicationId">Application ID to compare.</param>
963 /// <param name="rhsApplicationId">Application ID to be compared.</param>
964 /// <returns>Returns certificate comparison result.</returns>
965 /// <exception cref="ArgumentException">Thrown when the failed input package ID is invalid.</exception>
966 /// <exception cref="System.IO.IOException">Thrown when the method failed due to an internal I/O error.</exception>
967 /// <since_tizen> 3 </since_tizen>
968 public static CertCompareResultType CompareCertInfoByApplicationId(string lhsApplicationId, string rhsApplicationId)
970 Interop.PackageManager.CertCompareResultType compareResult;
971 Interop.PackageManager.ErrorCode err = Interop.PackageManager.PackageManagerCompareCertInfoByApplicationId(lhsApplicationId, rhsApplicationId, out compareResult);
972 if (err != Interop.PackageManager.ErrorCode.None)
974 throw PackageManagerErrorFactory.GetException(err, "Failed to compare cert info by application id");
977 return (CertCompareResultType)compareResult;
981 /// Drm nested class. This class has the PackageManager's drm related methods.
983 /// <since_tizen> 3 </since_tizen>
984 public static class Drm
987 /// Generates a request for getting the license.
989 /// <param name="responseData">Response data string of the purchase request.</param>
990 /// <returns>Returns the package DRM information of a given response data which contains the required data and license URL.</returns>
991 /// <privilege>http://tizen.org/privilege/packagemanager.admin</privilege>
992 /// <privlevel>platform</privlevel>
993 /// <exception cref="ArgumentException">Thrown when failed when input package ID is invalid.</exception>
994 /// <exception cref="OutOfMemoryException">Thrown when there is not enough memory to continue the execution of the method.</exception>
995 /// <exception cref="UnauthorizedAccessException">Thrown when an application does not have the privilege to access this method.</exception>
996 /// <exception cref="SystemException">Thrown when the method failed due to an internal system error.</exception>
997 /// <since_tizen> 3 </since_tizen>
998 public static PackageDrm GenerateLicenseRequest(string responseData)
1000 return PackageDrm.GenerateLicenseRequest(responseData);
1005 /// Registers the encrypted license.
1007 /// <param name="responseData">The response data string of the rights request.</param>
1008 /// <returns>Returns true if succeeds, otherwise false.</returns>
1009 /// <privilege>http://tizen.org/privilege/packagemanager.admin</privilege>
1010 /// <privlevel>platform</privlevel>
1011 /// <exception cref="ArgumentException">Thrown when failed when input package ID is invalid.</exception>
1012 /// <exception cref="OutOfMemoryException">Thrown when there is not enough memory to continue the execution of the method.</exception>
1013 /// <exception cref="UnauthorizedAccessException">Thrown when an application does not have the privilege to access this method.</exception>
1014 /// <exception cref="SystemException">Thrown when the method failed due to internal system error.</exception>
1015 /// <since_tizen> 3 </since_tizen>
1016 public static bool RegisterLicense(string responseData)
1018 Interop.PackageManager.ErrorCode err = Interop.PackageManager.PackageManagerDrmRegisterLicense(responseData);
1019 if (err != Interop.PackageManager.ErrorCode.None)
1021 throw PackageManagerErrorFactory.GetException(err, "Failed to register drm license");
1028 /// Decrypts the contents which are encrypted.
1030 /// <param name="drmFilePath">Drm file path.</param>
1031 /// <param name="decryptedFilePath">Decrypted file path.</param>
1032 /// <returns>Returns true if succeeds, otherwise false.</returns>
1033 /// <privilege>http://tizen.org/privilege/packagemanager.admin</privilege>
1034 /// <privlevel>platform</privlevel>
1035 /// <exception cref="ArgumentException">Thrown when failed when input package ID is invalid.</exception>
1036 /// <exception cref="OutOfMemoryException">Thrown when there is not enough memory to continue the execution of the method.</exception>
1037 /// <exception cref="UnauthorizedAccessException">Thrown when an application does not have the privilege to access this method.</exception>
1038 /// <exception cref="SystemException">Thrown when the method failed due to an internal system error.</exception>
1039 /// <since_tizen> 3 </since_tizen>
1040 public static bool DecryptPackage(string drmFilePath, string decryptedFilePath)
1042 Interop.PackageManager.ErrorCode err = Interop.PackageManager.PackageManagerDrmDecryptPackage(drmFilePath, decryptedFilePath);
1043 if (err != Interop.PackageManager.ErrorCode.None)
1045 throw PackageManagerErrorFactory.GetException(err, "Failed to decrypt drm package");
1052 private static void SetPackageManagerEventStatus(Interop.PackageManager.EventStatus status)
1054 if (Handle.IsInvalid) return;
1056 Interop.PackageManager.EventStatus eventStatus = s_eventStatus;
1057 eventStatus |= status;
1058 if (eventStatus != Interop.PackageManager.EventStatus.All)
1059 eventStatus |= Interop.PackageManager.EventStatus.Progress;
1061 var err = Interop.PackageManager.ErrorCode.None;
1062 if (s_eventStatus != eventStatus)
1064 err = Interop.PackageManager.PackageManagerSetEvenStatus(Handle, eventStatus);
1065 if (err == Interop.PackageManager.ErrorCode.None)
1067 s_eventStatus = eventStatus;
1068 Log.Debug(LogTag, string.Format("New Event Status flag: {0}", s_eventStatus));
1071 Log.Debug(LogTag, string.Format("Failed to set flag for {0} event. err = {1}", eventStatus, err));
1075 private static void UnsetPackageManagerEventStatus()
1077 if (Handle.IsInvalid) return;
1079 Interop.PackageManager.EventStatus eventStatus = Interop.PackageManager.EventStatus.All;
1080 if (s_installEventHandler != null) eventStatus |= Interop.PackageManager.EventStatus.Install;
1081 if (s_uninstallEventHandler != null) eventStatus |= Interop.PackageManager.EventStatus.Uninstall;
1082 if (s_updateEventHandler != null) eventStatus |= Interop.PackageManager.EventStatus.Upgrade;
1083 if (s_moveEventHandler != null) eventStatus |= Interop.PackageManager.EventStatus.Move;
1084 if (s_clearDataEventHandler != null) eventStatus |= Interop.PackageManager.EventStatus.ClearData;
1085 if (eventStatus != Interop.PackageManager.EventStatus.All)
1086 eventStatus |= Interop.PackageManager.EventStatus.Progress;
1088 var err = Interop.PackageManager.ErrorCode.None;
1089 if (s_eventStatus != eventStatus)
1091 err = Interop.PackageManager.PackageManagerSetEvenStatus(Handle, eventStatus);
1092 if (err == Interop.PackageManager.ErrorCode.None)
1094 s_eventStatus = eventStatus;
1095 Log.Debug(LogTag, string.Format("New Event Status flag: {0}", s_eventStatus));
1098 Log.Debug(LogTag, string.Format("Failed to set flag for {0} event. err = {1}", eventStatus, err));
1102 private static void RegisterPackageManagerEventIfNeeded()
1104 if (s_installEventHandler != null && s_uninstallEventHandler != null && s_updateEventHandler != null && s_moveEventHandler != null && s_clearDataEventHandler != null)
1107 var err = Interop.PackageManager.ErrorCode.None;
1108 s_packageManagerEventCallback = new Interop.PackageManager.PackageManagerEventCallback(InternalEventCallback);
1110 if (!Handle.IsInvalid)
1112 Log.Debug(LogTag, "Reset Package Event");
1113 err = Interop.PackageManager.PackageManagerUnsetEvent(Handle);
1114 if (err != Interop.PackageManager.ErrorCode.None)
1116 throw PackageManagerErrorFactory.GetException(err, "Failed to unregister package manager event event.");
1119 err = Interop.PackageManager.PackageManagerSetEvent(Handle, s_packageManagerEventCallback, IntPtr.Zero);
1121 if (err != Interop.PackageManager.ErrorCode.None)
1123 Log.Warn(LogTag, string.Format("Failed to register callback for package manager event. err = {0}", err));
1127 private static void InternalEventCallback(string packageType, string packageId, Interop.PackageManager.EventType eventType, Interop.PackageManager.PackageEventState eventState, int progress, Interop.PackageManager.ErrorCode error, IntPtr user_data)
1131 if (eventType == Interop.PackageManager.EventType.Install)
1133 s_installEventHandler?.Invoke(null, new PackageManagerEventArgs(packageType, packageId, (PackageEventState)eventState, progress));
1135 else if (eventType == Interop.PackageManager.EventType.Uninstall)
1137 s_uninstallEventHandler?.Invoke(null, new PackageManagerEventArgs(packageType, packageId, (PackageEventState)eventState, progress));
1139 else if (eventType == Interop.PackageManager.EventType.Update)
1141 s_updateEventHandler?.Invoke(null, new PackageManagerEventArgs(packageType, packageId, (PackageEventState)eventState, progress));
1143 else if (eventType == Interop.PackageManager.EventType.Move)
1145 s_moveEventHandler?.Invoke(null, new PackageManagerEventArgs(packageType, packageId, (PackageEventState)eventState, progress));
1147 else if (eventType == Interop.PackageManager.EventType.ClearData)
1149 s_clearDataEventHandler?.Invoke(null, new PackageManagerEventArgs(packageType, packageId, (PackageEventState)eventState, progress));
1154 Log.Warn(LogTag, e.Message);
1158 private static void UnregisterPackageManagerEventIfNeeded()
1160 if (s_packageManagerEventCallback == null || s_installEventHandler != null || s_uninstallEventHandler != null || s_updateEventHandler != null || s_moveEventHandler != null || s_clearDataEventHandler != null)
1165 s_packageManagerEventCallback = null;
1167 var err = Interop.PackageManager.PackageManagerUnsetEvent(Handle);
1168 if (err != Interop.PackageManager.ErrorCode.None)
1170 throw PackageManagerErrorFactory.GetException(err, "Failed to unregister package manager event event.");
1175 internal static class PackageManagerErrorFactory
1177 internal static Exception GetException(Interop.PackageManager.ErrorCode err, string message)
1179 string errMessage = string.Format("{0} err = {1}", message, err);
1182 case Interop.PackageManager.ErrorCode.InvalidParameter:
1183 case Interop.PackageManager.ErrorCode.NoSuchPackage:
1184 return new ArgumentException(errMessage);
1185 case Interop.PackageManager.ErrorCode.PermissionDenied:
1186 return new UnauthorizedAccessException(errMessage);
1187 case Interop.PackageManager.ErrorCode.IoError:
1188 return new global::System.IO.IOException(errMessage);
1190 return new InvalidOperationException(errMessage);