diff options
author | The Android Open Source Project <initial-contribution@android.com> | 2009-03-03 19:32:31 -0800 |
---|---|---|
committer | The Android Open Source Project <initial-contribution@android.com> | 2009-03-03 19:32:31 -0800 |
commit | 91ec61c3dca241befdf4a3803c45b051a3e3124f (patch) | |
tree | 59fc988de9bdfbca4bcf0b946d29bceda657fa7c /src/com/android/packageinstaller/PackageUtil.java | |
parent | 8106e501baad76e7192c7ddbb9f5e179c7ff6f9b (diff) | |
download | android_packages_apps_PackageInstaller-91ec61c3dca241befdf4a3803c45b051a3e3124f.tar.gz android_packages_apps_PackageInstaller-91ec61c3dca241befdf4a3803c45b051a3e3124f.tar.bz2 android_packages_apps_PackageInstaller-91ec61c3dca241befdf4a3803c45b051a3e3124f.zip |
auto import from //depot/cupcake/@135843
Diffstat (limited to 'src/com/android/packageinstaller/PackageUtil.java')
-rw-r--r-- | src/com/android/packageinstaller/PackageUtil.java | 121 |
1 files changed, 121 insertions, 0 deletions
diff --git a/src/com/android/packageinstaller/PackageUtil.java b/src/com/android/packageinstaller/PackageUtil.java new file mode 100644 index 00000000..b36bcf2b --- /dev/null +++ b/src/com/android/packageinstaller/PackageUtil.java @@ -0,0 +1,121 @@ +/* +** +** Copyright 2007, The Android Open Source 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 com.android.packageinstaller; + +import java.io.File; +import java.util.List; + +import android.app.Activity; +import android.content.Context; +import android.content.Intent; +import android.content.pm.ApplicationInfo; +import android.content.pm.PackageInfo; +import android.content.pm.PackageManager; +import android.content.pm.PackageParser; +import android.content.pm.ResolveInfo; +import android.graphics.drawable.Drawable; +import android.net.Uri; +import android.util.DisplayMetrics; +import android.view.View; +import android.widget.ImageView; +import android.widget.TextView; + +/** + * This is a utility class for defining some utility methods and constants + * used in the package installer application. + */ +public class PackageUtil { + public static final String PREFIX="com.android.packageinstaller."; + public static final String INTENT_ATTR_INSTALL_STATUS = PREFIX+"installStatus"; + public static final String INTENT_ATTR_APPLICATION_INFO=PREFIX+"applicationInfo"; + public static final String INTENT_ATTR_PERMISSIONS_LIST=PREFIX+"PermissionsList"; + //intent attribute strings related to uninstall + public static final String INTENT_ATTR_PACKAGE_NAME=PREFIX+"PackageName"; + + /* + * Utility method to get application information for a given packageURI + */ + public static ApplicationInfo getApplicationInfo(Uri packageURI) { + final String archiveFilePath = packageURI.getPath(); + PackageParser packageParser = new PackageParser(archiveFilePath); + File sourceFile = new File(archiveFilePath); + DisplayMetrics metrics = new DisplayMetrics(); + metrics.setToDefaults(); + PackageParser.Package pkg = packageParser.parsePackage(sourceFile, archiveFilePath, metrics, 0); + if (pkg == null) { + return null; + } + return pkg.applicationInfo; + } + + /* + * Utility method to get package information for a given packageURI + */ + public static PackageParser.Package getPackageInfo(Uri packageURI) { + final String archiveFilePath = packageURI.getPath(); + PackageParser packageParser = new PackageParser(archiveFilePath); + File sourceFile = new File(archiveFilePath); + DisplayMetrics metrics = new DisplayMetrics(); + metrics.setToDefaults(); + return packageParser.parsePackage(sourceFile, archiveFilePath, metrics, 0); + } + + /* + * Utility method to get application label from package manager for a given context + */ + public static CharSequence getApplicationLabel(Context context, ApplicationInfo appInfo) { + CharSequence appName = appInfo.loadLabel(context.getPackageManager()); + if(appName == null) { + appName = context.getString(R.string.unknown); + } + return appName; + } + + /* + * Utility method to getApplicationIcon from package manager for a given context + */ + public static Drawable getApplicationIcon(Context context, ApplicationInfo appInfo) { + return appInfo.loadIcon(context.getPackageManager()); + } + + /* + * Utility method to display application snippet. make sure to setContentView on context + * before invoking this method + */ + public static View initAppSnippet(Activity context, ApplicationInfo appInfo, int snippetId) { + View appSnippet = context.findViewById(snippetId); + ((ImageView)appSnippet.findViewById(R.id.app_icon)).setImageDrawable( + getApplicationIcon(context, appInfo)); + ((TextView)appSnippet.findViewById(R.id.app_name)).setText( + getApplicationLabel(context, appInfo)); + return appSnippet; + } + + public static boolean isPackageAlreadyInstalled(Activity context, String pkgName) { + List<PackageInfo> installedList = context.getPackageManager().getInstalledPackages( + PackageManager.GET_UNINSTALLED_PACKAGES); + int installedListSize = installedList.size(); + for(int i = 0; i < installedListSize; i++) { + PackageInfo tmp = installedList.get(i); + if(pkgName.equalsIgnoreCase(tmp.packageName)) { + return true; + } + + } + return false; + } +} |