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 18 package com.android.packageinstaller; 19 20 import android.app.Activity; 21 import android.content.pm.ApplicationInfo; 22 import android.content.pm.PackageInfo; 23 import android.content.pm.PackageManager; 24 import android.content.pm.PackageParser; 25 import android.content.res.AssetManager; 26 import android.content.res.Resources; 27 import android.graphics.drawable.Drawable; 28 import android.util.DisplayMetrics; 29 import android.view.View; 30 import android.widget.ImageView; 31 import android.widget.TextView; 32 33 import java.io.File; 34 import java.util.List; 35 36 /** 37 * This is a utility class for defining some utility methods and constants 38 * used in the package installer application. 39 */ 40 public class PackageUtil { 41 public static final String PREFIX="com.android.packageinstaller."; 42 public static final String INTENT_ATTR_INSTALL_STATUS = PREFIX+"installStatus"; 43 public static final String INTENT_ATTR_APPLICATION_INFO=PREFIX+"applicationInfo"; 44 public static final String INTENT_ATTR_PERMISSIONS_LIST=PREFIX+"PermissionsList"; 45 //intent attribute strings related to uninstall 46 public static final String INTENT_ATTR_PACKAGE_NAME=PREFIX+"PackageName"; 47 48 /** 49 * Utility method to get application information for a given {@link File} 50 */ getApplicationInfo(File sourcePath)51 public static ApplicationInfo getApplicationInfo(File sourcePath) { 52 final String archiveFilePath = sourcePath.getAbsolutePath(); 53 PackageParser packageParser = new PackageParser(archiveFilePath); 54 File sourceFile = new File(archiveFilePath); 55 DisplayMetrics metrics = new DisplayMetrics(); 56 metrics.setToDefaults(); 57 PackageParser.Package pkg = packageParser.parsePackage( 58 sourceFile, archiveFilePath, metrics, 0); 59 if (pkg == null) { 60 return null; 61 } 62 return pkg.applicationInfo; 63 } 64 65 /** 66 * Utility method to get package information for a given {@link File} 67 */ getPackageInfo(File sourceFile)68 public static PackageParser.Package getPackageInfo(File sourceFile) { 69 final String archiveFilePath = sourceFile.getAbsolutePath(); 70 PackageParser packageParser = new PackageParser(archiveFilePath); 71 DisplayMetrics metrics = new DisplayMetrics(); 72 metrics.setToDefaults(); 73 PackageParser.Package pkg = packageParser.parsePackage(sourceFile, 74 archiveFilePath, metrics, 0); 75 // Nuke the parser reference. 76 packageParser = null; 77 return pkg; 78 } 79 initSnippet(View snippetView, CharSequence label, Drawable icon)80 public static View initSnippet(View snippetView, CharSequence label, Drawable icon) { 81 ((ImageView)snippetView.findViewById(R.id.app_icon)).setImageDrawable(icon); 82 ((TextView)snippetView.findViewById(R.id.app_name)).setText(label); 83 return snippetView; 84 } 85 86 /** 87 * Utility method to display a snippet of an installed application. 88 * The content view should have been set on context before invoking this method. 89 * appSnippet view should include R.id.app_icon and R.id.app_name 90 * defined on it. 91 * 92 * @param pContext context of package that can load the resources 93 * @param componentInfo ComponentInfo object whose resources are to be loaded 94 * @param snippetView the snippet view 95 */ initSnippetForInstalledApp(Activity pContext, ApplicationInfo appInfo, View snippetView)96 public static View initSnippetForInstalledApp(Activity pContext, 97 ApplicationInfo appInfo, View snippetView) { 98 final PackageManager pm = pContext.getPackageManager(); 99 return initSnippet( 100 snippetView, 101 appInfo.loadLabel(pm), 102 appInfo.loadIcon(pm)); 103 } 104 105 /** 106 * Utility method to display application snippet of a new package. 107 * The content view should have been set on context before invoking this method. 108 * appSnippet view should include R.id.app_icon and R.id.app_name 109 * defined on it. 110 * 111 * @param pContext context of package that can load the resources 112 * @param appInfo ApplicationInfo object of package whose resources are to be loaded 113 * @param snippetId view id of app snippet view 114 */ initSnippetForNewApp(Activity pContext, AppSnippet as, int snippetId)115 public static View initSnippetForNewApp(Activity pContext, AppSnippet as, 116 int snippetId) { 117 View appSnippet = pContext.findViewById(snippetId); 118 ((ImageView)appSnippet.findViewById(R.id.app_icon)).setImageDrawable(as.icon); 119 ((TextView)appSnippet.findViewById(R.id.app_name)).setText(as.label); 120 return appSnippet; 121 } 122 isPackageAlreadyInstalled(Activity context, String pkgName)123 public static boolean isPackageAlreadyInstalled(Activity context, String pkgName) { 124 List<PackageInfo> installedList = context.getPackageManager().getInstalledPackages( 125 PackageManager.GET_UNINSTALLED_PACKAGES); 126 int installedListSize = installedList.size(); 127 for(int i = 0; i < installedListSize; i++) { 128 PackageInfo tmp = installedList.get(i); 129 if(pkgName.equalsIgnoreCase(tmp.packageName)) { 130 return true; 131 } 132 } 133 return false; 134 } 135 136 static public class AppSnippet { 137 CharSequence label; 138 Drawable icon; AppSnippet(CharSequence label, Drawable icon)139 public AppSnippet(CharSequence label, Drawable icon) { 140 this.label = label; 141 this.icon = icon; 142 } 143 } 144 145 /** 146 * Utility method to load application label 147 * 148 * @param pContext context of package that can load the resources 149 * @param appInfo ApplicationInfo object of package whose resources are to be loaded 150 * @param snippetId view id of app snippet view 151 */ getAppSnippet( Activity pContext, ApplicationInfo appInfo, File sourceFile)152 public static AppSnippet getAppSnippet( 153 Activity pContext, ApplicationInfo appInfo, File sourceFile) { 154 final String archiveFilePath = sourceFile.getAbsolutePath(); 155 Resources pRes = pContext.getResources(); 156 AssetManager assmgr = new AssetManager(); 157 assmgr.addAssetPath(archiveFilePath); 158 Resources res = new Resources(assmgr, pRes.getDisplayMetrics(), pRes.getConfiguration()); 159 CharSequence label = null; 160 // Try to load the label from the package's resources. If an app has not explicitly 161 // specified any label, just use the package name. 162 if (appInfo.labelRes != 0) { 163 try { 164 label = res.getText(appInfo.labelRes); 165 } catch (Resources.NotFoundException e) { 166 } 167 } 168 if (label == null) { 169 label = (appInfo.nonLocalizedLabel != null) ? 170 appInfo.nonLocalizedLabel : appInfo.packageName; 171 } 172 Drawable icon = null; 173 // Try to load the icon from the package's resources. If an app has not explicitly 174 // specified any resource, just use the default icon for now. 175 if (appInfo.icon != 0) { 176 try { 177 icon = res.getDrawable(appInfo.icon); 178 } catch (Resources.NotFoundException e) { 179 } 180 } 181 if (icon == null) { 182 icon = pContext.getPackageManager().getDefaultActivityIcon(); 183 } 184 return new PackageUtil.AppSnippet(label, icon); 185 } 186 } 187