From 583ed3ba08644912ec0f3ec8ceb939f9eb0b8e86 Mon Sep 17 00:00:00 2001 From: Mitsuru Oshima Date: Tue, 12 May 2009 19:19:10 -0700 Subject: [PATCH] * re-scale icons that are loaded using different denstiy metrics. * fix potential npe --- src/com/android/launcher/LauncherModel.java | 34 ++++++++++++++++------------ src/com/android/launcher/LiveFolderIcon.java | 4 ++-- src/com/android/launcher/Utilities.java | 24 +++++++++++++++----- 3 files changed, 39 insertions(+), 23 deletions(-) diff --git a/src/com/android/launcher/LauncherModel.java b/src/com/android/launcher/LauncherModel.java index da41f14..3a7fac6 100644 --- a/src/com/android/launcher/LauncherModel.java +++ b/src/com/android/launcher/LauncherModel.java @@ -174,7 +174,7 @@ public class LauncherModel { for (ResolveInfo info : matches) { adapter.setNotifyOnChange(false); - adapter.add(makeAndCacheApplicationInfo(packageManager, cache, info)); + adapter.add(makeAndCacheApplicationInfo(packageManager, cache, info, launcher)); } adapter.sort(new ApplicationInfoComparator()); @@ -239,7 +239,7 @@ public class LauncherModel { final ApplicationInfo applicationInfo = findIntent(adapter, info.activityInfo.applicationInfo.packageName, info.activityInfo.name); if (applicationInfo != null) { - updateAndCacheApplicationInfo(packageManager, info, applicationInfo); + updateAndCacheApplicationInfo(packageManager, info, applicationInfo, launcher); changed = true; } } @@ -252,9 +252,9 @@ public class LauncherModel { } private void updateAndCacheApplicationInfo(PackageManager packageManager, ResolveInfo info, - ApplicationInfo applicationInfo) { + ApplicationInfo applicationInfo, Context context) { - updateApplicationInfoTitleAndIcon(packageManager, info, applicationInfo); + updateApplicationInfoTitleAndIcon(packageManager, info, applicationInfo, context); ComponentName componentName = new ComponentName( info.activityInfo.applicationInfo.packageName, info.activityInfo.name); @@ -326,10 +326,11 @@ public class LauncherModel { info.activityInfo.applicationInfo.packageName, info.activityInfo.name); if (applicationInfo == null) { toAdd.add(makeAndCacheApplicationInfo(launcher.getPackageManager(), - mAppInfoCache, info)); + mAppInfoCache, info, launcher)); changed = true; } else { - updateAndCacheApplicationInfo(launcher.getPackageManager(), info, applicationInfo); + updateAndCacheApplicationInfo( + launcher.getPackageManager(), info, applicationInfo, launcher); changed = true; } } @@ -419,7 +420,8 @@ public class LauncherModel { } private static ApplicationInfo makeAndCacheApplicationInfo(PackageManager manager, - HashMap appInfoCache, ResolveInfo info) { + HashMap appInfoCache, ResolveInfo info, + Context context) { ComponentName componentName = new ComponentName( info.activityInfo.applicationInfo.packageName, @@ -430,7 +432,7 @@ public class LauncherModel { application = new ApplicationInfo(); application.container = ItemInfo.NO_ID; - updateApplicationInfoTitleAndIcon(manager, info, application); + updateApplicationInfoTitleAndIcon(manager, info, application, context); application.setActivity(componentName, Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); @@ -442,14 +444,15 @@ public class LauncherModel { } private static void updateApplicationInfoTitleAndIcon(PackageManager manager, ResolveInfo info, - ApplicationInfo application) { + ApplicationInfo application, Context context) { application.title = info.loadLabel(manager); if (application.title == null) { application.title = info.activityInfo.name; } - application.icon = info.activityInfo.loadIcon(manager); + application.icon = + Utilities.createIconThumbnail(info.activityInfo.loadIcon(manager), context); application.filtered = false; } @@ -495,7 +498,7 @@ public class LauncherModel { for (int i = 0; i < count && !mStopped; i++) { ResolveInfo info = apps.get(i); ApplicationInfo application = - makeAndCacheApplicationInfo(manager, appInfoCache, info); + makeAndCacheApplicationInfo(manager, appInfoCache, info, launcher); if (action.add(application) && !mStopped) { launcher.runOnUiThread(action); @@ -765,7 +768,7 @@ public class LauncherModel { } if (itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) { - info = getApplicationInfo(manager, intent); + info = getApplicationInfo(manager, intent, launcher); } else { info = getApplicationInfoShortcut(c, launcher, iconTypeIndex, iconPackageIndex, iconResourceIndex, iconIndex); @@ -1116,7 +1119,8 @@ public class LauncherModel { /** * Make an ApplicationInfo object for an application */ - private static ApplicationInfo getApplicationInfo(PackageManager manager, Intent intent) { + private static ApplicationInfo getApplicationInfo(PackageManager manager, Intent intent, + Context context) { final ResolveInfo resolveInfo = manager.resolveActivity(intent, 0); if (resolveInfo == null) { @@ -1125,7 +1129,7 @@ public class LauncherModel { final ApplicationInfo info = new ApplicationInfo(); final ActivityInfo activityInfo = resolveInfo.activityInfo; - info.icon = activityInfo.loadIcon(manager); + info.icon = Utilities.createIconThumbnail(activityInfo.loadIcon(manager), context); if (info.title == null || info.title.length() == 0) { info.title = activityInfo.loadLabel(manager); } @@ -1154,7 +1158,7 @@ public class LauncherModel { try { Resources resources = packageManager.getResourcesForApplication(packageName); final int id = resources.getIdentifier(resourceName, null, null); - info.icon = resources.getDrawable(id); + info.icon = Utilities.createIconThumbnail(resources.getDrawable(id), launcher); } catch (Exception e) { info.icon = packageManager.getDefaultActivityIcon(); } diff --git a/src/com/android/launcher/LiveFolderIcon.java b/src/com/android/launcher/LiveFolderIcon.java index 33cb0b7..14a4ee6 100644 --- a/src/com/android/launcher/LiveFolderIcon.java +++ b/src/com/android/launcher/LiveFolderIcon.java @@ -41,8 +41,8 @@ public class LiveFolderIcon extends FolderIcon { final Resources resources = launcher.getResources(); Drawable d = folderInfo.icon; if (d == null) { - resources.getDrawable(R.drawable.ic_launcher_folder); - d = Utilities.createIconThumbnail(d, launcher); + d = Utilities.createIconThumbnail( + resources.getDrawable(R.drawable.ic_launcher_folder), launcher); folderInfo.filtered = true; } icon.setCompoundDrawablesWithIntrinsicBounds(null, d, null, null); diff --git a/src/com/android/launcher/Utilities.java b/src/com/android/launcher/Utilities.java index cb8976c..33b084b 100644 --- a/src/com/android/launcher/Utilities.java +++ b/src/com/android/launcher/Utilities.java @@ -16,6 +16,7 @@ package com.android.launcher; +import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; import android.graphics.drawable.PaintDrawable; import android.graphics.Bitmap; @@ -80,24 +81,35 @@ final class Utilities { static Drawable createIconThumbnail(Drawable icon, Context context) { if (sIconWidth == -1) { final Resources resources = context.getResources(); - sIconWidth = sIconHeight = (int) resources.getDimension( - android.R.dimen.app_icon_size); + sIconWidth = sIconHeight = (int) resources.getDimension(android.R.dimen.app_icon_size); } int width = sIconWidth; int height = sIconHeight; - final int iconWidth = icon.getIntrinsicWidth(); - final int iconHeight = icon.getIntrinsicHeight(); - + float scale = 1.0f; if (icon instanceof PaintDrawable) { PaintDrawable painter = (PaintDrawable) icon; painter.setIntrinsicWidth(width); painter.setIntrinsicHeight(height); + } else if (icon instanceof BitmapDrawable) { + float displayDensity = context.getResources().getDisplayMetrics().density; + BitmapDrawable bitmapDrawable = (BitmapDrawable) icon; + Bitmap bitmap = bitmapDrawable.getBitmap(); + float iconDensity = bitmap.getDensityScale(); + scale = displayDensity / iconDensity; + + // Scale the bitmap to the screen density size if it's not loaded at the same density. + if (scale != 1.0f) { + icon = bitmapDrawable = new BitmapDrawable(bitmap); + bitmapDrawable.setDensityScale(scale); + } } + int iconWidth = icon.getIntrinsicWidth(); + int iconHeight = icon.getIntrinsicHeight(); if (width > 0 && height > 0) { - if (width < iconWidth || height < iconHeight) { + if (width < iconWidth || height < iconHeight || scale != 1.0f) { final float ratio = (float) iconWidth / iconHeight; if (iconWidth > iconHeight) { -- 2.11.0