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 package information for a given {@link File}
51      */
getPackageInfo(File sourceFile)52     public static PackageParser.Package getPackageInfo(File sourceFile) {
53         final PackageParser parser = new PackageParser();
54         try {
55             return parser.parsePackage(sourceFile, 0);
56         } catch (PackageParserException e) {
57             return null;
58         }
59     }
60 
initSnippet(View snippetView, CharSequence label, Drawable icon)61     public static View initSnippet(View snippetView, CharSequence label, Drawable icon) {
62         ((ImageView)snippetView.findViewById(R.id.app_icon)).setImageDrawable(icon);
63         ((TextView)snippetView.findViewById(R.id.app_name)).setText(label);
64         return snippetView;
65     }
66 
67     /**
68      * Utility method to display a snippet of an installed application.
69      * The content view should have been set on context before invoking this method.
70      * appSnippet view should include R.id.app_icon and R.id.app_name
71      * defined on it.
72      *
73      * @param pContext context of package that can load the resources
74      * @param componentInfo ComponentInfo object whose resources are to be loaded
75      * @param snippetView the snippet view
76      */
initSnippetForInstalledApp(Activity pContext, ApplicationInfo appInfo, View snippetView)77     public static View initSnippetForInstalledApp(Activity pContext,
78             ApplicationInfo appInfo, View snippetView) {
79         return initSnippetForInstalledApp(pContext, appInfo, snippetView, null);
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      * @param UserHandle user that the app si installed for.
92      */
initSnippetForInstalledApp(Activity pContext, ApplicationInfo appInfo, View snippetView, UserHandle user)93     public static View initSnippetForInstalledApp(Activity pContext,
94             ApplicationInfo appInfo, View snippetView, UserHandle user) {
95         final PackageManager pm = pContext.getPackageManager();
96         Drawable icon = appInfo.loadIcon(pm);
97         if (user != null) {
98             icon = pContext.getPackageManager().getUserBadgedIcon(icon, user);
99         }
100         return initSnippet(
101                 snippetView,
102                 appInfo.loadLabel(pm),
103                 icon);
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     /**
147      * Utility method to load application label
148      *
149      * @param pContext context of package that can load the resources
150      * @param appInfo ApplicationInfo object of package whose resources are to be loaded
151      * @param snippetId view id of app snippet view
152      */
getAppSnippet( Activity pContext, ApplicationInfo appInfo, File sourceFile)153     public static AppSnippet getAppSnippet(
154             Activity pContext, ApplicationInfo appInfo, File sourceFile) {
155         final String archiveFilePath = sourceFile.getAbsolutePath();
156         Resources pRes = pContext.getResources();
157         AssetManager assmgr = new AssetManager();
158         assmgr.addAssetPath(archiveFilePath);
159         Resources res = new Resources(assmgr, pRes.getDisplayMetrics(), pRes.getConfiguration());
160         CharSequence label = null;
161         // Try to load the label from the package's resources. If an app has not explicitly
162         // specified any label, just use the package name.
163         if (appInfo.labelRes != 0) {
164             try {
165                 label = res.getText(appInfo.labelRes);
166             } catch (Resources.NotFoundException e) {
167             }
168         }
169         if (label == null) {
170             label = (appInfo.nonLocalizedLabel != null) ?
171                     appInfo.nonLocalizedLabel : appInfo.packageName;
172         }
173         Drawable icon = null;
174         // Try to load the icon from the package's resources. If an app has not explicitly
175         // specified any resource, just use the default icon for now.
176         if (appInfo.icon != 0) {
177             try {
178                 icon = res.getDrawable(appInfo.icon);
179             } catch (Resources.NotFoundException e) {
180             }
181         }
182         if (icon == null) {
183             icon = pContext.getPackageManager().getDefaultActivityIcon();
184         }
185         return new PackageUtil.AppSnippet(label, icon);
186     }
187 }
188