summaryrefslogtreecommitdiffstats
path: root/src/com/android/packageinstaller/PackageUtil.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/com/android/packageinstaller/PackageUtil.java')
-rw-r--r--src/com/android/packageinstaller/PackageUtil.java66
1 files changed, 32 insertions, 34 deletions
diff --git a/src/com/android/packageinstaller/PackageUtil.java b/src/com/android/packageinstaller/PackageUtil.java
index 330cbbce..0db09e88 100644
--- a/src/com/android/packageinstaller/PackageUtil.java
+++ b/src/com/android/packageinstaller/PackageUtil.java
@@ -18,27 +18,31 @@
package com.android.packageinstaller;
import android.app.Activity;
+import android.content.Context;
import android.content.pm.ApplicationInfo;
-import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageParser;
import android.content.pm.PackageParser.PackageParserException;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
+import android.os.UserHandle;
+import android.support.annotation.NonNull;
+import android.support.annotation.Nullable;
+import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
-import android.os.UserHandle;
import java.io.File;
-import java.util.List;
/**
* This is a utility class for defining some utility methods and constants
* used in the package installer application.
*/
public class PackageUtil {
+ private static final String LOG_TAG = PackageUtil.class.getSimpleName();
+
public static final String PREFIX="com.android.packageinstaller.";
public static final String INTENT_ATTR_INSTALL_STATUS = PREFIX+"installStatus";
public static final String INTENT_ATTR_APPLICATION_INFO=PREFIX+"applicationInfo";
@@ -49,8 +53,9 @@ public class PackageUtil {
/**
* Utility method to get package information for a given {@link File}
*/
- public static PackageParser.Package getPackageInfo(File sourceFile) {
+ public static PackageParser.Package getPackageInfo(Context context, File sourceFile) {
final PackageParser parser = new PackageParser();
+ parser.setCallback(new PackageParser.CallbackImpl(context.getPackageManager()));
try {
return parser.parsePackage(sourceFile, 0);
} catch (PackageParserException e) {
@@ -74,7 +79,7 @@ public class PackageUtil {
* @param componentInfo ComponentInfo object whose resources are to be loaded
* @param snippetView the snippet view
*/
- public static View initSnippetForInstalledApp(Activity pContext,
+ public static View initSnippetForInstalledApp(Context pContext,
ApplicationInfo appInfo, View snippetView) {
return initSnippetForInstalledApp(pContext, appInfo, snippetView, null);
}
@@ -90,7 +95,7 @@ public class PackageUtil {
* @param snippetView the snippet view
* @param UserHandle user that the app si installed for.
*/
- public static View initSnippetForInstalledApp(Activity pContext,
+ public static View initSnippetForInstalledApp(Context pContext,
ApplicationInfo appInfo, View snippetView, UserHandle user) {
final PackageManager pm = pContext.getPackageManager();
Drawable icon = appInfo.loadIcon(pm);
@@ -110,34 +115,23 @@ public class PackageUtil {
* defined on it.
*
* @param pContext context of package that can load the resources
- * @param appInfo ApplicationInfo object of package whose resources are to be loaded
+ * @param as The resources to be loaded
* @param snippetId view id of app snippet view
*/
- public static View initSnippetForNewApp(Activity pContext, AppSnippet as,
- int snippetId) {
+ @NonNull public static View initSnippetForNewApp(@NonNull Activity pContext,
+ @NonNull AppSnippet as, int snippetId) {
View appSnippet = pContext.findViewById(snippetId);
- ((ImageView)appSnippet.findViewById(R.id.app_icon)).setImageDrawable(as.icon);
+ if (as.icon != null) {
+ ((ImageView) appSnippet.findViewById(R.id.app_icon)).setImageDrawable(as.icon);
+ }
((TextView)appSnippet.findViewById(R.id.app_name)).setText(as.label);
return appSnippet;
}
- public static boolean isPackageAlreadyInstalled(Activity context, String pkgName) {
- List<PackageInfo> installedList = context.getPackageManager().getInstalledPackages(
- PackageManager.GET_UNINSTALLED_PACKAGES);
- int installedListSize = installedList.size();
- for(int i = 0; i < installedListSize; i++) {
- PackageInfo tmp = installedList.get(i);
- if(pkgName.equalsIgnoreCase(tmp.packageName)) {
- return true;
- }
- }
- return false;
- }
-
static public class AppSnippet {
- CharSequence label;
- Drawable icon;
- public AppSnippet(CharSequence label, Drawable icon) {
+ @NonNull public CharSequence label;
+ @Nullable public Drawable icon;
+ public AppSnippet(@NonNull CharSequence label, @Nullable Drawable icon) {
this.label = label;
this.icon = icon;
}
@@ -148,7 +142,7 @@ public class PackageUtil {
*
* @param pContext context of package that can load the resources
* @param appInfo ApplicationInfo object of package whose resources are to be loaded
- * @param snippetId view id of app snippet view
+ * @param sourceFile File the package is in
*/
public static AppSnippet getAppSnippet(
Activity pContext, ApplicationInfo appInfo, File sourceFile) {
@@ -173,14 +167,18 @@ public class PackageUtil {
Drawable icon = null;
// Try to load the icon from the package's resources. If an app has not explicitly
// specified any resource, just use the default icon for now.
- if (appInfo.icon != 0) {
- try {
- icon = res.getDrawable(appInfo.icon);
- } catch (Resources.NotFoundException e) {
+ try {
+ if (appInfo.icon != 0) {
+ try {
+ icon = res.getDrawable(appInfo.icon);
+ } catch (Resources.NotFoundException e) {
+ }
}
- }
- if (icon == null) {
- icon = pContext.getPackageManager().getDefaultActivityIcon();
+ if (icon == null) {
+ icon = pContext.getPackageManager().getDefaultActivityIcon();
+ }
+ } catch (OutOfMemoryError e) {
+ Log.i(LOG_TAG, "Could not load app icon", e);
}
return new PackageUtil.AppSnippet(label, icon);
}