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 android.app.Activity; 20 import android.content.pm.ActivityInfo; 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.net.Uri; 29 import android.util.DisplayMetrics; 30 import android.view.View; 31 import android.widget.ImageView; 32 import android.widget.TextView; 33 34 import java.io.File; 35 import java.util.List; 36 37 /** 38 * This is a utility class for defining some utility methods and constants 39 * used in the package installer application. 40 */ 41 public class PackageUtil { 42 public static final String PREFIX="com.android.packageinstaller."; 43 public static final String INTENT_ATTR_INSTALL_STATUS = PREFIX+"installStatus"; 44 public static final String INTENT_ATTR_APPLICATION_INFO=PREFIX+"applicationInfo"; 45 public static final String INTENT_ATTR_PERMISSIONS_LIST=PREFIX+"PermissionsList"; 46 //intent attribute strings related to uninstall 47 public static final String INTENT_ATTR_PACKAGE_NAME=PREFIX+"PackageName"; 48 49 /* 50 * Utility method to get application information for a given packageURI 51 */ getApplicationInfo(Uri packageURI)52 public static ApplicationInfo getApplicationInfo(Uri packageURI) { 53 final String archiveFilePath = packageURI.getPath(); 54 PackageParser packageParser = new PackageParser(archiveFilePath); 55 File sourceFile = new File(archiveFilePath); 56 DisplayMetrics metrics = new DisplayMetrics(); 57 metrics.setToDefaults(); 58 PackageParser.Package pkg = packageParser.parsePackage(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 packageURI 67 */ getPackageInfo(Uri packageURI)68 public static PackageParser.Package getPackageInfo(Uri packageURI) { 69 final String archiveFilePath = packageURI.getPath(); 70 PackageParser packageParser = new PackageParser(archiveFilePath); 71 File sourceFile = new File(archiveFilePath); 72 DisplayMetrics metrics = new DisplayMetrics(); 73 metrics.setToDefaults(); 74 PackageParser.Package pkg = packageParser.parsePackage(sourceFile, 75 archiveFilePath, metrics, 0); 76 // Nuke the parser reference. 77 packageParser = null; 78 return pkg; 79 } 80 initSnippet(View snippetView, CharSequence label, Drawable icon)81 public static View initSnippet(View snippetView, CharSequence label, Drawable icon) { 82 ((ImageView)snippetView.findViewById(R.id.app_icon)).setImageDrawable(icon); 83 ((TextView)snippetView.findViewById(R.id.app_name)).setText(label); 84 return snippetView; 85 } 86 87 /* 88 * Utility method to display a snippet of an installed application. 89 * The content view should have been set on context before invoking this method. 90 * appSnippet view should include R.id.app_icon and R.id.app_name 91 * defined on it. 92 * 93 * @param pContext context of package that can load the resources 94 * @param componentInfo ComponentInfo object whose resources are to be loaded 95 * @param snippetView the snippet view 96 */ initSnippetForInstalledApp(Activity pContext, ApplicationInfo appInfo, View snippetView)97 public static View initSnippetForInstalledApp(Activity pContext, 98 ApplicationInfo appInfo, View snippetView) { 99 final PackageManager pm = pContext.getPackageManager(); 100 return initSnippet( 101 snippetView, 102 appInfo.loadLabel(pm), 103 appInfo.loadIcon(pm)); 104 } 105 106 /* 107 * Utility method to display application snippet of a new package. 108 * The content view should have been set on context before invoking this method. 109 * appSnippet view should include R.id.app_icon and R.id.app_name 110 * defined on it. 111 * 112 * @param pContext context of package that can load the resources 113 * @param appInfo ApplicationInfo object of package whose resources are to be loaded 114 * @param snippetId view id of app snippet view 115 */ initSnippetForNewApp(Activity pContext, AppSnippet as, int snippetId)116 public static View initSnippetForNewApp(Activity pContext, AppSnippet as, 117 int snippetId) { 118 View appSnippet = pContext.findViewById(snippetId); 119 ((ImageView)appSnippet.findViewById(R.id.app_icon)).setImageDrawable(as.icon); 120 ((TextView)appSnippet.findViewById(R.id.app_name)).setText(as.label); 121 return appSnippet; 122 } 123 isPackageAlreadyInstalled(Activity context, String pkgName)124 public static boolean isPackageAlreadyInstalled(Activity context, String pkgName) { 125 List<PackageInfo> installedList = context.getPackageManager().getInstalledPackages( 126 PackageManager.GET_UNINSTALLED_PACKAGES); 127 int installedListSize = installedList.size(); 128 for(int i = 0; i < installedListSize; i++) { 129 PackageInfo tmp = installedList.get(i); 130 if(pkgName.equalsIgnoreCase(tmp.packageName)) { 131 return true; 132 } 133 } 134 return false; 135 } 136 137 static public class AppSnippet { 138 CharSequence label; 139 Drawable icon; AppSnippet(CharSequence label, Drawable icon)140 public AppSnippet(CharSequence label, Drawable icon) { 141 this.label = label; 142 this.icon = icon; 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, Uri packageURI)152 public static AppSnippet getAppSnippet(Activity pContext, ApplicationInfo appInfo, 153 Uri packageURI) { 154 final String archiveFilePath = packageURI.getPath(); 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