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.pm.PackageParser.PackageParserException;
26 import android.content.res.AssetManager;
27 import android.content.res.Resources;
28 import android.graphics.drawable.Drawable;
29 import android.view.View;
30 import android.widget.ImageView;
31 import android.widget.TextView;
32 import android.os.UserHandle;
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 {@link File}
51      */
getApplicationInfo(File sourcePath)52     public static ApplicationInfo getApplicationInfo(File sourcePath) {
53         final PackageParser parser = new PackageParser();
54         try {
55             PackageParser.Package pkg = parser.parseMonolithicPackage(sourcePath, 0);
56             return pkg.applicationInfo;
57         } catch (PackageParserException e) {
58             return null;
59         }
60     }
61 
62     /**
63      * Utility method to get package information for a given {@link File}
64      */
getPackageInfo(File sourceFile)65     public static PackageParser.Package getPackageInfo(File sourceFile) {
66         final PackageParser parser = new PackageParser();
67         try {
68             PackageParser.Package pkg = parser.parseMonolithicPackage(sourceFile, 0);
69             parser.collectManifestDigest(pkg);
70             return pkg;
71         } catch (PackageParserException e) {
72             return null;
73         }
74     }
75 
initSnippet(View snippetView, CharSequence label, Drawable icon)76     public static View initSnippet(View snippetView, CharSequence label, Drawable icon) {
77         ((ImageView)snippetView.findViewById(R.id.app_icon)).setImageDrawable(icon);
78         ((TextView)snippetView.findViewById(R.id.app_name)).setText(label);
79         return snippetView;
80     }
81 
82     /**
83      * Utility method to display a 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 componentInfo ComponentInfo object whose resources are to be loaded
90      * @param snippetView the snippet view
91      */
initSnippetForInstalledApp(Activity pContext, ApplicationInfo appInfo, View snippetView)92     public static View initSnippetForInstalledApp(Activity pContext,
93             ApplicationInfo appInfo, View snippetView) {
94         return initSnippetForInstalledApp(pContext, appInfo, snippetView, null);
95     }
96 
97     /**
98      * Utility method to display a snippet of an installed application.
99      * The content view should have been set on context before invoking this method.
100      * appSnippet view should include R.id.app_icon and R.id.app_name
101      * defined on it.
102      *
103      * @param pContext context of package that can load the resources
104      * @param componentInfo ComponentInfo object whose resources are to be loaded
105      * @param snippetView the snippet view
106      * @param UserHandle user that the app si installed for.
107      */
initSnippetForInstalledApp(Activity pContext, ApplicationInfo appInfo, View snippetView, UserHandle user)108     public static View initSnippetForInstalledApp(Activity pContext,
109             ApplicationInfo appInfo, View snippetView, UserHandle user) {
110         final PackageManager pm = pContext.getPackageManager();
111         Drawable icon = appInfo.loadIcon(pm);
112         if (user != null) {
113             icon = pContext.getPackageManager().getUserBadgedIcon(icon, user);
114         }
115         return initSnippet(
116                 snippetView,
117                 appInfo.loadLabel(pm),
118                 icon);
119     }
120 
121     /**
122      * Utility method to display application snippet of a new package.
123      * The content view should have been set on context before invoking this method.
124      * appSnippet view should include R.id.app_icon and R.id.app_name
125      * defined on it.
126      *
127      * @param pContext context of package that can load the resources
128      * @param appInfo ApplicationInfo object of package whose resources are to be loaded
129      * @param snippetId view id of app snippet view
130      */
initSnippetForNewApp(Activity pContext, AppSnippet as, int snippetId)131     public static View initSnippetForNewApp(Activity pContext, AppSnippet as,
132             int snippetId) {
133         View appSnippet = pContext.findViewById(snippetId);
134         ((ImageView)appSnippet.findViewById(R.id.app_icon)).setImageDrawable(as.icon);
135         ((TextView)appSnippet.findViewById(R.id.app_name)).setText(as.label);
136         return appSnippet;
137     }
138 
isPackageAlreadyInstalled(Activity context, String pkgName)139     public static boolean isPackageAlreadyInstalled(Activity context, String pkgName) {
140         List<PackageInfo> installedList = context.getPackageManager().getInstalledPackages(
141                 PackageManager.GET_UNINSTALLED_PACKAGES);
142         int installedListSize = installedList.size();
143         for(int i = 0; i < installedListSize; i++) {
144             PackageInfo tmp = installedList.get(i);
145             if(pkgName.equalsIgnoreCase(tmp.packageName)) {
146                 return true;
147             }
148         }
149         return false;
150     }
151 
152     static public class AppSnippet {
153         CharSequence label;
154         Drawable icon;
AppSnippet(CharSequence label, Drawable icon)155         public AppSnippet(CharSequence label, Drawable icon) {
156             this.label = label;
157             this.icon = icon;
158         }
159     }
160 
161     /**
162      * Utility method to load application label
163      *
164      * @param pContext context of package that can load the resources
165      * @param appInfo ApplicationInfo object of package whose resources are to be loaded
166      * @param snippetId view id of app snippet view
167      */
getAppSnippet( Activity pContext, ApplicationInfo appInfo, File sourceFile)168     public static AppSnippet getAppSnippet(
169             Activity pContext, ApplicationInfo appInfo, File sourceFile) {
170         final String archiveFilePath = sourceFile.getAbsolutePath();
171         Resources pRes = pContext.getResources();
172         AssetManager assmgr = new AssetManager();
173         assmgr.addAssetPath(archiveFilePath);
174         Resources res = new Resources(assmgr, pRes.getDisplayMetrics(), pRes.getConfiguration());
175         CharSequence label = null;
176         // Try to load the label from the package's resources. If an app has not explicitly
177         // specified any label, just use the package name.
178         if (appInfo.labelRes != 0) {
179             try {
180                 label = res.getText(appInfo.labelRes);
181             } catch (Resources.NotFoundException e) {
182             }
183         }
184         if (label == null) {
185             label = (appInfo.nonLocalizedLabel != null) ?
186                     appInfo.nonLocalizedLabel : appInfo.packageName;
187         }
188         Drawable icon = null;
189         // Try to load the icon from the package's resources. If an app has not explicitly
190         // specified any resource, just use the default icon for now.
191         if (appInfo.icon != 0) {
192             try {
193                 icon = res.getDrawable(appInfo.icon);
194             } catch (Resources.NotFoundException e) {
195             }
196         }
197         if (icon == null) {
198             icon = pContext.getPackageManager().getDefaultActivityIcon();
199         }
200         return new PackageUtil.AppSnippet(label, icon);
201     }
202 }
203