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