diff options
author | Michael W <baddaemon87@gmail.com> | 2016-12-09 01:19:14 +0100 |
---|---|---|
committer | Zhao Wei Liew <zhaoweiliew@gmail.com> | 2017-02-19 01:58:59 +0000 |
commit | b1be9e045727a16b1f95d7c3c14d5ca896825c50 (patch) | |
tree | 53cb4de9b53fc5a75a1bfff1d4500bac7ba213ab | |
parent | 595640e86ab51a121c5a473cb7e1989e44fd0177 (diff) | |
download | vendor_cmsdk-b1be9e045727a16b1f95d7c3c14d5ca896825c50.zip vendor_cmsdk-b1be9e045727a16b1f95d7c3c14d5ca896825c50.tar.gz vendor_cmsdk-b1be9e045727a16b1f95d7c3c14d5ca896825c50.tar.bz2 |
cmsdk: utils: Introduce PackageManagerUtils class
Partially based off the PackageManagerHelper class in Launcher3.
Change-Id: I0ed2ea210cb52ac4bd94c7bc5320c24e6d423279
-rw-r--r-- | sdk/src/java/org/cyanogenmod/internal/util/PackageManagerUtils.java | 117 |
1 files changed, 117 insertions, 0 deletions
diff --git a/sdk/src/java/org/cyanogenmod/internal/util/PackageManagerUtils.java b/sdk/src/java/org/cyanogenmod/internal/util/PackageManagerUtils.java new file mode 100644 index 0000000..690fd48 --- /dev/null +++ b/sdk/src/java/org/cyanogenmod/internal/util/PackageManagerUtils.java @@ -0,0 +1,117 @@ +/** + * Copyright (C) 2016 The CyanogenMod Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.cyanogenmod.internal.util; + +import android.content.Context; +import android.content.pm.ApplicationInfo; +import android.content.pm.PackageManager; + +public final class PackageManagerUtils { + private static final int FLAG_SUSPENDED = 1 << 30; + + private PackageManagerUtils() { + // This class is not supposed to be instantiated + } + + /** + * Checks whether a given package exists + * + * @param context + * @param packageName + * @return true if the package exists + */ + public static boolean isAppInstalled(final Context context, final String packageName) { + return getApplicationInfo(context, packageName, 0) != null; + } + + /** + * Check whether a package with specific flags is enabled + * + * @param context + * @param packageName + * @param flags + * @return true if the package is enabled + */ + public static boolean isAppEnabled(final Context context, + final String packageName, final int flags) { + final ApplicationInfo info = getApplicationInfo(context, packageName, flags); + return info != null && info.enabled; + } + + /** + * Check whether a package is enabled + * + * @param context + * @param packageName + * @return true if the package is enabled + */ + public static boolean isAppEnabled(final Context context, final String packageName) { + return isAppEnabled(context, packageName, 0); + } + + /** + * Check if a package can possibly be on the SDCard + * This is just a workaround and doesn't guarantee that the app is on SD card + * + * @param context + * @param packageName + * @return true if the package is on the SDCard + */ + public static boolean isAppOnSdcard(final Context context, final String packageName) { + return isAppEnabled(context, packageName, PackageManager.GET_UNINSTALLED_PACKAGES); + } + + /** + * Check if a package is suspended + * + * @param context + * @param packageName + * @return true if the package is suspended + */ + public static boolean isAppSuspended(final Context context, final String packageName) { + return isAppSuspended(getApplicationInfo(context, packageName, 0)); + } + + /** + * Check if a package is suspended + * + * @param info + * @return true if the package is suspended + */ + public static boolean isAppSuspended(final ApplicationInfo info) { + return info != null && (info.flags & FLAG_SUSPENDED) != 0; + } + + /** + * Get the ApplicationInfo of a package + * + * @param context + * @param packageName + * @param flags + * @return null if the package cannot be found or the ApplicationInfo is null + */ + public static ApplicationInfo getApplicationInfo(final Context context, + final String packageName, final int flags) { + final PackageManager packageManager = context.getPackageManager(); + ApplicationInfo info; + try { + info = packageManager.getApplicationInfo(packageName, flags); + } catch (PackageManager.NameNotFoundException e) { + info = null; + } + return info; + } +} |