Example usage for android.app PackageInstallObserver PackageInstallObserver

List of usage examples for android.app PackageInstallObserver PackageInstallObserver

Introduction

In this page you can find the example usage for android.app PackageInstallObserver PackageInstallObserver.

Prototype

PackageInstallObserver

Source Link

Usage

From source file:org.wso2.iot.system.service.utils.AppUtils.java

/**
 * Silently installs the app resides in the provided URI.
 * @param context - Application context.
 * @param  packageUri - App package URI.
 *//*w w  w.ja  v  a2  s.  c om*/
public static void silentInstallApp(final Context context, Uri packageUri) {
    PackageManager pm = context.getPackageManager();
    PackageInstallObserver observer = new PackageInstallObserver() {
        @Override
        public void onPackageInstalled(String basePackageName, int returnCode, String msg, Bundle extras) {
            if (INSTALL_SUCCEEDED == returnCode) {
                Log.d(TAG, "Installation succeeded!");
                publishAppInstallStatus(context, INSTALL_SUCCESS_STATUS, null);
            } else {
                if (msg == null || msg.isEmpty()) {
                    switch (returnCode) {
                    case INSTALL_FAILED_ALREADY_EXISTS:
                        msg = "INSTALL_FAILED_ALREADY_EXISTS";
                        break;
                    case INSTALL_FAILED_INVALID_APK:
                        msg = "INSTALL_FAILED_INVALID_APK";
                        break;
                    case INSTALL_FAILED_INVALID_URI:
                        msg = "INSTALL_FAILED_INVALID_URI";
                        break;
                    case INSTALL_FAILED_INSUFFICIENT_STORAGE:
                        msg = "INSTALL_FAILED_INSUFFICIENT_STORAGE";
                        break;
                    case INSTALL_FAILED_DUPLICATE_PACKAGE:
                        msg = "INSTALL_FAILED_DUPLICATE_PACKAGE";
                        break;
                    case INSTALL_FAILED_NO_SHARED_USER:
                        msg = "INSTALL_FAILED_NO_SHARED_USER";
                        break;
                    case INSTALL_FAILED_UPDATE_INCOMPATIBLE:
                        msg = "INSTALL_FAILED_UPDATE_INCOMPATIBLE";
                        break;
                    case INSTALL_FAILED_SHARED_USER_INCOMPATIBLE:
                        msg = "INSTALL_FAILED_SHARED_USER_INCOMPATIBLE";
                        break;
                    case INSTALL_FAILED_MISSING_SHARED_LIBRARY:
                        msg = "INSTALL_FAILED_MISSING_SHARED_LIBRARY";
                        break;
                    case INSTALL_FAILED_REPLACE_COULDNT_DELETE:
                        msg = "INSTALL_FAILED_REPLACE_COULDNT_DELETE";
                        break;
                    case INSTALL_FAILED_DEXOPT:
                        msg = "INSTALL_FAILED_DEXOPT";
                        break;
                    case INSTALL_FAILED_OLDER_SDK:
                        msg = "INSTALL_FAILED_OLDER_SDK";
                        break;
                    case INSTALL_FAILED_CONFLICTING_PROVIDER:
                        msg = "INSTALL_FAILED_CONFLICTING_PROVIDER";
                        break;
                    case INSTALL_FAILED_NEWER_SDK:
                        msg = "INSTALL_FAILED_NEWER_SDK";
                        break;
                    case INSTALL_FAILED_TEST_ONLY:
                        msg = "INSTALL_FAILED_TEST_ONLY";
                        break;
                    case INSTALL_FAILED_CPU_ABI_INCOMPATIBLE:
                        msg = "INSTALL_FAILED_CPU_ABI_INCOMPATIBLE";
                        break;
                    case INSTALL_FAILED_MISSING_FEATURE:
                        msg = "INSTALL_FAILED_MISSING_FEATURE";
                        break;
                    case INSTALL_FAILED_CONTAINER_ERROR:
                        msg = "INSTALL_FAILED_CONTAINER_ERROR";
                        break;
                    case INSTALL_FAILED_INVALID_INSTALL_LOCATION:
                        msg = "INSTALL_FAILED_INVALID_INSTALL_LOCATION";
                        break;
                    case INSTALL_FAILED_MEDIA_UNAVAILABLE:
                        msg = "INSTALL_FAILED_MEDIA_UNAVAILABLE";
                        break;
                    case INSTALL_FAILED_VERIFICATION_TIMEOUT:
                        msg = "INSTALL_FAILED_VERIFICATION_TIMEOUT";
                        break;
                    case INSTALL_FAILED_VERIFICATION_FAILURE:
                        msg = "INSTALL_FAILED_VERIFICATION_FAILURE";
                        break;
                    case INSTALL_FAILED_PACKAGE_CHANGED:
                        msg = "INSTALL_FAILED_PACKAGE_CHANGED";
                        break;
                    case INSTALL_FAILED_UID_CHANGED:
                        msg = "INSTALL_FAILED_UID_CHANGED";
                        break;
                    case INSTALL_FAILED_VERSION_DOWNGRADE:
                        msg = "INSTALL_FAILED_VERSION_DOWNGRADE";
                        break;
                    case INSTALL_PARSE_FAILED_NOT_APK:
                        msg = "INSTALL_PARSE_FAILED_NOT_APK";
                        break;
                    case INSTALL_PARSE_FAILED_BAD_MANIFEST:
                        msg = "INSTALL_PARSE_FAILED_BAD_MANIFEST";
                        break;
                    case INSTALL_PARSE_FAILED_UNEXPECTED_EXCEPTION:
                        msg = "INSTALL_PARSE_FAILED_UNEXPECTED_EXCEPTION";
                        break;
                    case INSTALL_PARSE_FAILED_NO_CERTIFICATES:
                        msg = "INSTALL_PARSE_FAILED_NO_CERTIFICATES";
                        break;
                    case INSTALL_PARSE_FAILED_INCONSISTENT_CERTIFICATES:
                        msg = "INSTALL_PARSE_FAILED_INCONSISTENT_CERTIFICATES";
                        break;
                    case INSTALL_PARSE_FAILED_CERTIFICATE_ENCODING:
                        msg = "INSTALL_PARSE_FAILED_CERTIFICATE_ENCODING";
                        break;
                    case INSTALL_PARSE_FAILED_BAD_PACKAGE_NAME:
                        msg = "INSTALL_PARSE_FAILED_BAD_PACKAGE_NAME";
                        break;
                    case INSTALL_PARSE_FAILED_BAD_SHARED_USER_ID:
                        msg = "INSTALL_PARSE_FAILED_BAD_SHARED_USER_ID";
                        break;
                    case INSTALL_PARSE_FAILED_MANIFEST_MALFORMED:
                        msg = "INSTALL_PARSE_FAILED_MANIFEST_MALFORMED";
                        break;
                    case INSTALL_PARSE_FAILED_MANIFEST_EMPTY:
                        msg = "INSTALL_PARSE_FAILED_MANIFEST_EMPTY";
                        break;
                    case INSTALL_FAILED_INTERNAL_ERROR:
                        msg = "INSTALL_FAILED_INTERNAL_ERROR";
                        break;
                    case INSTALL_FAILED_USER_RESTRICTED:
                        msg = "INSTALL_FAILED_USER_RESTRICTED";
                        break;
                    case INSTALL_FAILED_DUPLICATE_PERMISSION:
                        msg = "INSTALL_FAILED_DUPLICATE_PERMISSION";
                        break;
                    case INSTALL_FAILED_NO_MATCHING_ABIS:
                        msg = "INSTALL_FAILED_NO_MATCHING_ABIS";
                        break;
                    case NO_NATIVE_LIBRARIES:
                        msg = "NO_NATIVE_LIBRARIES";
                        break;
                    case INSTALL_FAILED_ABORTED:
                        msg = "INSTALL_FAILED_ABORTED";
                        break;
                    default:
                        msg = "UNKNOWN";
                    }
                }
                String error = "Package installation failed due to an internal error with code: " + returnCode
                        + " and message: " + msg;
                Log.e(TAG, error);
                publishAppInstallStatus(context, INSTALL_FAILED_STATUS, error);
            }
        }
    };
    pm.installPackage(packageUri, observer,
            INSTALL_ALL_USERS | INSTALL_ALLOW_DOWNGRADE | INSTALL_REPLACE_EXISTING, null);
}

From source file:org.wso2.emm.system.service.utils.AppUtils.java

/**
 * Silently installs the app resides in the provided URI.
 * @param context - Application context.
 * @param  packageUri - App package URI.
 *//*  ww  w.  j a v a 2s. c o  m*/
public static void silentInstallApp(final Context context, Uri packageUri) {
    PackageManager pm = context.getPackageManager();
    PackageInstallObserver observer = new PackageInstallObserver() {
        @Override
        public void onPackageInstalled(String basePackageName, int returnCode, String msg, Bundle extras) {
            if (INSTALL_SUCCEEDED == returnCode) {
                Log.d(TAG, "Installation succeeded!");
                publishAppInstallStatus(context, INSTALL_SUCCESS_STATUS, null);
            } else {
                if (msg == null || msg.isEmpty()) {
                    switch (returnCode) {
                    case INSTALL_FAILED_ALREADY_EXISTS:
                        msg = "INSTALL_FAILED_ALREADY_EXISTS";
                        break;
                    case INSTALL_FAILED_INVALID_APK:
                        msg = "INSTALL_FAILED_INVALID_APK";
                        break;
                    case INSTALL_FAILED_INVALID_URI:
                        msg = "INSTALL_FAILED_INVALID_URI";
                        break;
                    case INSTALL_FAILED_INSUFFICIENT_STORAGE:
                        msg = "INSTALL_FAILED_INSUFFICIENT_STORAGE";
                        break;
                    case INSTALL_FAILED_DUPLICATE_PACKAGE:
                        msg = "INSTALL_FAILED_DUPLICATE_PACKAGE";
                        break;
                    case INSTALL_FAILED_NO_SHARED_USER:
                        msg = "INSTALL_FAILED_NO_SHARED_USER";
                        break;
                    case INSTALL_FAILED_UPDATE_INCOMPATIBLE:
                        msg = "INSTALL_FAILED_UPDATE_INCOMPATIBLE";
                        break;
                    case INSTALL_FAILED_SHARED_USER_INCOMPATIBLE:
                        msg = "INSTALL_FAILED_SHARED_USER_INCOMPATIBLE";
                        break;
                    case INSTALL_FAILED_MISSING_SHARED_LIBRARY:
                        msg = "INSTALL_FAILED_MISSING_SHARED_LIBRARY";
                        break;
                    case INSTALL_FAILED_REPLACE_COULDNT_DELETE:
                        msg = "INSTALL_FAILED_REPLACE_COULDNT_DELETE";
                        break;
                    case INSTALL_FAILED_DEXOPT:
                        msg = "INSTALL_FAILED_DEXOPT";
                        break;
                    case INSTALL_FAILED_OLDER_SDK:
                        msg = "INSTALL_FAILED_OLDER_SDK";
                        break;
                    case INSTALL_FAILED_CONFLICTING_PROVIDER:
                        msg = "INSTALL_FAILED_CONFLICTING_PROVIDER";
                        break;
                    case INSTALL_FAILED_NEWER_SDK:
                        msg = "INSTALL_FAILED_NEWER_SDK";
                        break;
                    case INSTALL_FAILED_TEST_ONLY:
                        msg = "INSTALL_FAILED_TEST_ONLY";
                        break;
                    case INSTALL_FAILED_CPU_ABI_INCOMPATIBLE:
                        msg = "INSTALL_FAILED_CPU_ABI_INCOMPATIBLE";
                        break;
                    case INSTALL_FAILED_MISSING_FEATURE:
                        msg = "INSTALL_FAILED_MISSING_FEATURE";
                        break;
                    case INSTALL_FAILED_CONTAINER_ERROR:
                        msg = "INSTALL_FAILED_CONTAINER_ERROR";
                        break;
                    case INSTALL_FAILED_INVALID_INSTALL_LOCATION:
                        msg = "INSTALL_FAILED_INVALID_INSTALL_LOCATION";
                        break;
                    case INSTALL_FAILED_MEDIA_UNAVAILABLE:
                        msg = "INSTALL_FAILED_MEDIA_UNAVAILABLE";
                        break;
                    case INSTALL_FAILED_VERIFICATION_TIMEOUT:
                        msg = "INSTALL_FAILED_VERIFICATION_TIMEOUT";
                        break;
                    case INSTALL_FAILED_VERIFICATION_FAILURE:
                        msg = "INSTALL_FAILED_VERIFICATION_FAILURE";
                        break;
                    case INSTALL_FAILED_PACKAGE_CHANGED:
                        msg = "INSTALL_FAILED_PACKAGE_CHANGED";
                        break;
                    case INSTALL_FAILED_UID_CHANGED:
                        msg = "INSTALL_FAILED_UID_CHANGED";
                        break;
                    case INSTALL_FAILED_VERSION_DOWNGRADE:
                        msg = "INSTALL_FAILED_VERSION_DOWNGRADE";
                        break;
                    case INSTALL_PARSE_FAILED_NOT_APK:
                        msg = "INSTALL_PARSE_FAILED_NOT_APK";
                        break;
                    case INSTALL_PARSE_FAILED_BAD_MANIFEST:
                        msg = "INSTALL_PARSE_FAILED_BAD_MANIFEST";
                        break;
                    case INSTALL_PARSE_FAILED_UNEXPECTED_EXCEPTION:
                        msg = "INSTALL_PARSE_FAILED_UNEXPECTED_EXCEPTION";
                        break;
                    case INSTALL_PARSE_FAILED_NO_CERTIFICATES:
                        msg = "INSTALL_PARSE_FAILED_NO_CERTIFICATES";
                        break;
                    case INSTALL_PARSE_FAILED_INCONSISTENT_CERTIFICATES:
                        msg = "INSTALL_PARSE_FAILED_INCONSISTENT_CERTIFICATES";
                        break;
                    case INSTALL_PARSE_FAILED_CERTIFICATE_ENCODING:
                        msg = "INSTALL_PARSE_FAILED_CERTIFICATE_ENCODING";
                        break;
                    case INSTALL_PARSE_FAILED_BAD_PACKAGE_NAME:
                        msg = "INSTALL_PARSE_FAILED_BAD_PACKAGE_NAME";
                        break;
                    case INSTALL_PARSE_FAILED_BAD_SHARED_USER_ID:
                        msg = "INSTALL_PARSE_FAILED_BAD_SHARED_USER_ID";
                        break;
                    case INSTALL_PARSE_FAILED_MANIFEST_MALFORMED:
                        msg = "INSTALL_PARSE_FAILED_MANIFEST_MALFORMED";
                        break;
                    case INSTALL_PARSE_FAILED_MANIFEST_EMPTY:
                        msg = "INSTALL_PARSE_FAILED_MANIFEST_EMPTY";
                        break;
                    case INSTALL_FAILED_INTERNAL_ERROR:
                        msg = "INSTALL_FAILED_INTERNAL_ERROR";
                        break;
                    case INSTALL_FAILED_USER_RESTRICTED:
                        msg = "INSTALL_FAILED_USER_RESTRICTED";
                        break;
                    case INSTALL_FAILED_DUPLICATE_PERMISSION:
                        msg = "INSTALL_FAILED_DUPLICATE_PERMISSION";
                        break;
                    case INSTALL_FAILED_NO_MATCHING_ABIS:
                        msg = "INSTALL_FAILED_NO_MATCHING_ABIS";
                        break;
                    case NO_NATIVE_LIBRARIES:
                        msg = "NO_NATIVE_LIBRARIES";
                        break;
                    case INSTALL_FAILED_ABORTED:
                        msg = "INSTALL_FAILED_ABORTED";
                        break;
                    case INSTALL_FAILED_THEME_AAPT_ERROR:
                        msg = "INSTALL_FAILED_THEME_AAPT_ERROR";
                        break;
                    case INSTALL_FAILED_THEME_IDMAP_ERROR:
                        msg = "INSTALL_FAILED_THEME_IDMAP_ERROR";
                        break;
                    case INSTALL_FAILED_THEME_UNKNOWN_ERROR:
                        msg = "INSTALL_FAILED_THEME_UNKNOWN_ERROR";
                        break;
                    }
                }
                String error = "Package installation failed due to an internal error with code: " + returnCode
                        + " and message: " + msg;
                Log.e(TAG, error);
                publishAppInstallStatus(context, INSTALL_FAILED_STATUS, error);
            }
        }
    };
    pm.installPackage(packageUri, observer,
            INSTALL_ALL_USERS | INSTALL_ALLOW_DOWNGRADE | INSTALL_REPLACE_EXISTING, null);
}