/* ** ** 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.content.pm.PackageManager.NameNotFoundException; import android.content.res.AssetManager; import android.content.res.Configuration; import android.content.res.Resources; import android.graphics.drawable.Drawable; import android.net.Uri; import android.util.DisplayMetrics; import android.util.Log; 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 display application snippet of an installed application. * The content view should have been set on context before invoking this method. * appSnippet view should include R.id.app_icon and R.id.app_name * defined on it. * * @param pContext context of package that can load the resources * @param appInfo ApplicationInfo object of package whose resources are to be loaded * @param snippetId view id of app snippet view */ public static View initSnippetForInstalledApp(Activity pContext, ApplicationInfo appInfo, int snippetId) { View appSnippet = pContext.findViewById(snippetId); String pkgName = appInfo.packageName; PackageManager pm = pContext.getPackageManager(); CharSequence label = appInfo.loadLabel(pm); Drawable icon = appInfo.loadIcon(pm); ((ImageView)appSnippet.findViewById(R.id.app_icon)).setImageDrawable(icon); ((TextView)appSnippet.findViewById(R.id.app_name)).setText(label); return appSnippet; } /* * Utility method to display application snippet of a new package. * The content view should have been set on context before invoking this method. * appSnippet view should include R.id.app_icon and R.id.app_name * defined on it. * * @param pContext context of package that can load the resources * @param appInfo ApplicationInfo object of package whose resources are to be loaded * @param snippetId view id of app snippet view */ public static View initSnippetForNewApp(Activity pContext, ApplicationInfo appInfo, int snippetId, Uri packageURI) { View appSnippet = pContext.findViewById(snippetId); final String archiveFilePath = packageURI.getPath(); DisplayMetrics metrics = new DisplayMetrics(); metrics.setToDefaults(); AssetManager assmgr = new AssetManager(); assmgr.addAssetPath(archiveFilePath); Resources res = new Resources(assmgr, metrics, null); CharSequence label = null; // Try to load the label from the package's resources. If an app has not explicitly // specified any label, just use the package name. try { label = res.getText(appInfo.labelRes); } catch (Resources.NotFoundException e) { label = appInfo.packageName; } Drawable icon = null; // Try to load the icon from the package's resources. If an app has not explicitly // specified any resource, just use the default icon for now. try { icon = res.getDrawable(appInfo.icon); } catch (Resources.NotFoundException e) { icon = pContext.getPackageManager().getDefaultActivityIcon(); } ((ImageView)appSnippet.findViewById(R.id.app_icon)).setImageDrawable(icon); ((TextView)appSnippet.findViewById(R.id.app_name)).setText(label); return appSnippet; } public static boolean isPackageAlreadyInstalled(Activity context, String pkgName) { List 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; } }