Home | History | Annotate | Download | only in packageinstaller
      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      */
     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      */
     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         if (pkg == null) {
     76             return null;
     77         }
     78         if (!packageParser.collectManifestDigest(pkg)) {
     79             return null;
     80         }
     81         return pkg;
     82     }
     83 
     84     public static View initSnippet(View snippetView, CharSequence label, Drawable icon) {
     85         ((ImageView)snippetView.findViewById(R.id.app_icon)).setImageDrawable(icon);
     86         ((TextView)snippetView.findViewById(R.id.app_name)).setText(label);
     87         return snippetView;
     88     }
     89 
     90     /**
     91      * Utility method to display a snippet of an installed application.
     92      * The content view should have been set on context before invoking this method.
     93      * appSnippet view should include R.id.app_icon and R.id.app_name
     94      * defined on it.
     95      *
     96      * @param pContext context of package that can load the resources
     97      * @param componentInfo ComponentInfo object whose resources are to be loaded
     98      * @param snippetView the snippet view
     99      */
    100     public static View initSnippetForInstalledApp(Activity pContext,
    101             ApplicationInfo appInfo, View snippetView) {
    102         final PackageManager pm = pContext.getPackageManager();
    103         return initSnippet(
    104                 snippetView,
    105                 appInfo.loadLabel(pm),
    106                 appInfo.loadIcon(pm));
    107     }
    108 
    109     /**
    110      * Utility method to display application snippet of a new package.
    111      * The content view should have been set on context before invoking this method.
    112      * appSnippet view should include R.id.app_icon and R.id.app_name
    113      * defined on it.
    114      *
    115      * @param pContext context of package that can load the resources
    116      * @param appInfo ApplicationInfo object of package whose resources are to be loaded
    117      * @param snippetId view id of app snippet view
    118      */
    119     public static View initSnippetForNewApp(Activity pContext, AppSnippet as,
    120             int snippetId) {
    121         View appSnippet = pContext.findViewById(snippetId);
    122         ((ImageView)appSnippet.findViewById(R.id.app_icon)).setImageDrawable(as.icon);
    123         ((TextView)appSnippet.findViewById(R.id.app_name)).setText(as.label);
    124         return appSnippet;
    125     }
    126 
    127     public static boolean isPackageAlreadyInstalled(Activity context, String pkgName) {
    128         List<PackageInfo> installedList = context.getPackageManager().getInstalledPackages(
    129                 PackageManager.GET_UNINSTALLED_PACKAGES);
    130         int installedListSize = installedList.size();
    131         for(int i = 0; i < installedListSize; i++) {
    132             PackageInfo tmp = installedList.get(i);
    133             if(pkgName.equalsIgnoreCase(tmp.packageName)) {
    134                 return true;
    135             }
    136         }
    137         return false;
    138     }
    139 
    140     static public class AppSnippet {
    141         CharSequence label;
    142         Drawable icon;
    143         public AppSnippet(CharSequence label, Drawable icon) {
    144             this.label = label;
    145             this.icon = icon;
    146         }
    147     }
    148 
    149     /**
    150      * Utility method to load application label
    151      *
    152      * @param pContext context of package that can load the resources
    153      * @param appInfo ApplicationInfo object of package whose resources are to be loaded
    154      * @param snippetId view id of app snippet view
    155      */
    156     public static AppSnippet getAppSnippet(
    157             Activity pContext, ApplicationInfo appInfo, File sourceFile) {
    158         final String archiveFilePath = sourceFile.getAbsolutePath();
    159         Resources pRes = pContext.getResources();
    160         AssetManager assmgr = new AssetManager();
    161         assmgr.addAssetPath(archiveFilePath);
    162         Resources res = new Resources(assmgr, pRes.getDisplayMetrics(), pRes.getConfiguration());
    163         CharSequence label = null;
    164         // Try to load the label from the package's resources. If an app has not explicitly
    165         // specified any label, just use the package name.
    166         if (appInfo.labelRes != 0) {
    167             try {
    168                 label = res.getText(appInfo.labelRes);
    169             } catch (Resources.NotFoundException e) {
    170             }
    171         }
    172         if (label == null) {
    173             label = (appInfo.nonLocalizedLabel != null) ?
    174                     appInfo.nonLocalizedLabel : appInfo.packageName;
    175         }
    176         Drawable icon = null;
    177         // Try to load the icon from the package's resources. If an app has not explicitly
    178         // specified any resource, just use the default icon for now.
    179         if (appInfo.icon != 0) {
    180             try {
    181                 icon = res.getDrawable(appInfo.icon);
    182             } catch (Resources.NotFoundException e) {
    183             }
    184         }
    185         if (icon == null) {
    186             icon = pContext.getPackageManager().getDefaultActivityIcon();
    187         }
    188         return new PackageUtil.AppSnippet(label, icon);
    189     }
    190 }
    191