1 /* 2 ** 3 ** Copyright 2007, The Android Open Source Project 4 ** 5 ** Licensed under the Apache License, Version 2.0 (the "License"); 6 ** you may not use this file except in compliance with the License. 7 ** You may obtain a copy of the License at 8 ** 9 ** http://www.apache.org/licenses/LICENSE-2.0 10 ** 11 ** Unless required by applicable law or agreed to in writing, software 12 ** distributed under the License is distributed on an "AS IS" BASIS, 13 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 ** See the License for the specific language governing permissions and 15 ** limitations under the License. 16 */ 17 package com.android.packageinstaller; 18 19 import java.io.File; 20 import java.util.List; 21 22 import android.app.Activity; 23 import android.content.Context; 24 import android.content.Intent; 25 import android.content.pm.ApplicationInfo; 26 import android.content.pm.PackageInfo; 27 import android.content.pm.PackageManager; 28 import android.content.pm.PackageParser; 29 import android.content.pm.ResolveInfo; 30 import android.content.pm.PackageManager.NameNotFoundException; 31 import android.content.res.AssetManager; 32 import android.content.res.Configuration; 33 import android.content.res.Resources; 34 import android.graphics.drawable.Drawable; 35 import android.net.Uri; 36 import android.util.DisplayMetrics; 37 import android.util.Log; 38 import android.view.View; 39 import android.widget.ImageView; 40 import android.widget.TextView; 41 42 /** 43 * This is a utility class for defining some utility methods and constants 44 * used in the package installer application. 45 */ 46 public class PackageUtil { 47 public static final String PREFIX="com.android.packageinstaller."; 48 public static final String INTENT_ATTR_INSTALL_STATUS = PREFIX+"installStatus"; 49 public static final String INTENT_ATTR_APPLICATION_INFO=PREFIX+"applicationInfo"; 50 public static final String INTENT_ATTR_PERMISSIONS_LIST=PREFIX+"PermissionsList"; 51 //intent attribute strings related to uninstall 52 public static final String INTENT_ATTR_PACKAGE_NAME=PREFIX+"PackageName"; 53 54 /* 55 * Utility method to get application information for a given packageURI 56 */ getApplicationInfo(Uri packageURI)57 public static ApplicationInfo getApplicationInfo(Uri packageURI) { 58 final String archiveFilePath = packageURI.getPath(); 59 PackageParser packageParser = new PackageParser(archiveFilePath); 60 File sourceFile = new File(archiveFilePath); 61 DisplayMetrics metrics = new DisplayMetrics(); 62 metrics.setToDefaults(); 63 PackageParser.Package pkg = packageParser.parsePackage(sourceFile, archiveFilePath, metrics, 0); 64 if (pkg == null) { 65 return null; 66 } 67 return pkg.applicationInfo; 68 } 69 70 /* 71 * Utility method to get package information for a given packageURI 72 */ getPackageInfo(Uri packageURI)73 public static PackageParser.Package getPackageInfo(Uri packageURI) { 74 final String archiveFilePath = packageURI.getPath(); 75 PackageParser packageParser = new PackageParser(archiveFilePath); 76 File sourceFile = new File(archiveFilePath); 77 DisplayMetrics metrics = new DisplayMetrics(); 78 metrics.setToDefaults(); 79 return packageParser.parsePackage(sourceFile, archiveFilePath, metrics, 0); 80 } 81 82 /* 83 * Utility method to display application snippet of an installed application. 84 * The content view should have been set on context before invoking this method. 85 * appSnippet view should include R.id.app_icon and R.id.app_name 86 * defined on it. 87 * 88 * @param pContext context of package that can load the resources 89 * @param appInfo ApplicationInfo object of package whose resources are to be loaded 90 * @param snippetId view id of app snippet view 91 */ initSnippetForInstalledApp(Activity pContext, ApplicationInfo appInfo, int snippetId)92 public static View initSnippetForInstalledApp(Activity pContext, 93 ApplicationInfo appInfo, int snippetId) { 94 View appSnippet = pContext.findViewById(snippetId); 95 String pkgName = appInfo.packageName; 96 PackageManager pm = pContext.getPackageManager(); 97 CharSequence label = appInfo.loadLabel(pm); 98 Drawable icon = appInfo.loadIcon(pm); 99 ((ImageView)appSnippet.findViewById(R.id.app_icon)).setImageDrawable(icon); 100 ((TextView)appSnippet.findViewById(R.id.app_name)).setText(label); 101 return appSnippet; 102 } 103 104 /* 105 * Utility method to display application snippet of a new package. 106 * The content view should have been set on context before invoking this method. 107 * appSnippet view should include R.id.app_icon and R.id.app_name 108 * defined on it. 109 * 110 * @param pContext context of package that can load the resources 111 * @param appInfo ApplicationInfo object of package whose resources are to be loaded 112 * @param snippetId view id of app snippet view 113 */ initSnippetForNewApp(Activity pContext, ApplicationInfo appInfo, int snippetId, Uri packageURI)114 public static View initSnippetForNewApp(Activity pContext, ApplicationInfo appInfo, 115 int snippetId, Uri packageURI) { 116 View appSnippet = pContext.findViewById(snippetId); 117 final String archiveFilePath = packageURI.getPath(); 118 DisplayMetrics metrics = new DisplayMetrics(); 119 metrics.setToDefaults(); 120 AssetManager assmgr = new AssetManager(); 121 assmgr.addAssetPath(archiveFilePath); 122 Resources res = new Resources(assmgr, metrics, null); 123 CharSequence label = null; 124 // Try to load the label from the package's resources. If an app has not explicitly 125 // specified any label, just use the package name. 126 try { 127 label = res.getText(appInfo.labelRes); 128 } catch (Resources.NotFoundException e) { 129 label = appInfo.packageName; 130 } 131 Drawable icon = null; 132 // Try to load the icon from the package's resources. If an app has not explicitly 133 // specified any resource, just use the default icon for now. 134 try { 135 icon = res.getDrawable(appInfo.icon); 136 } catch (Resources.NotFoundException e) { 137 icon = pContext.getPackageManager().getDefaultActivityIcon(); 138 } 139 ((ImageView)appSnippet.findViewById(R.id.app_icon)).setImageDrawable(icon); 140 ((TextView)appSnippet.findViewById(R.id.app_name)).setText(label); 141 return appSnippet; 142 } 143 isPackageAlreadyInstalled(Activity context, String pkgName)144 public static boolean isPackageAlreadyInstalled(Activity context, String pkgName) { 145 List<PackageInfo> installedList = context.getPackageManager().getInstalledPackages( 146 PackageManager.GET_UNINSTALLED_PACKAGES); 147 int installedListSize = installedList.size(); 148 for(int i = 0; i < installedListSize; i++) { 149 PackageInfo tmp = installedList.get(i); 150 if(pkgName.equalsIgnoreCase(tmp.packageName)) { 151 return true; 152 } 153 154 } 155 return false; 156 } 157 } 158