Updating PendingAppWidgetHostView to load icon on the background thread

Bug: 21325319
Change-Id: I0886eec5a3f36f5e1ac18020c54891a56bdb5ed2
This commit is contained in:
Sunny Goyal 2017-01-03 16:52:43 -08:00
parent a167a6e04b
commit 2d7cca1e05
4 changed files with 34 additions and 43 deletions

View File

@ -38,6 +38,7 @@ import android.view.ViewParent;
import android.widget.TextView; import android.widget.TextView;
import com.android.launcher3.IconCache.IconLoadRequest; import com.android.launcher3.IconCache.IconLoadRequest;
import com.android.launcher3.IconCache.ItemInfoUpdateReceiver;
import com.android.launcher3.folder.FolderIcon; import com.android.launcher3.folder.FolderIcon;
import com.android.launcher3.graphics.DrawableFactory; import com.android.launcher3.graphics.DrawableFactory;
import com.android.launcher3.graphics.HolographicOutlineHelper; import com.android.launcher3.graphics.HolographicOutlineHelper;
@ -51,7 +52,7 @@ import java.text.NumberFormat;
* too aggressive. * too aggressive.
*/ */
public class BubbleTextView extends TextView public class BubbleTextView extends TextView
implements BaseRecyclerViewFastScrollBar.FastScrollFocusableView { implements BaseRecyclerViewFastScrollBar.FastScrollFocusableView, ItemInfoUpdateReceiver {
private static SparseArray<Theme> sPreloaderThemes = new SparseArray<Theme>(2); private static SparseArray<Theme> sPreloaderThemes = new SparseArray<Theme>(2);
@ -540,7 +541,8 @@ public class BubbleTextView extends TextView
/** /**
* Applies the item info if it is same as what the view is pointing to currently. * Applies the item info if it is same as what the view is pointing to currently.
*/ */
public void reapplyItemInfo(final ItemInfo info) { @Override
public void reapplyItemInfo(ItemInfoWithIcon info) {
if (getTag() == info) { if (getTag() == info) {
FastBitmapDrawable.State prevState = FastBitmapDrawable.State.NORMAL; FastBitmapDrawable.State prevState = FastBitmapDrawable.State.NORMAL;
if (mIcon instanceof FastBitmapDrawable) { if (mIcon instanceof FastBitmapDrawable) {
@ -582,20 +584,8 @@ public class BubbleTextView extends TextView
mIconLoadRequest.cancel(); mIconLoadRequest.cancel();
mIconLoadRequest = null; mIconLoadRequest = null;
} }
if (getTag() instanceof AppInfo) { if (getTag() instanceof ItemInfoWithIcon) {
AppInfo info = (AppInfo) getTag(); ItemInfoWithIcon info = (ItemInfoWithIcon) getTag();
if (info.usingLowResIcon) {
mIconLoadRequest = LauncherAppState.getInstance().getIconCache()
.updateIconInBackground(BubbleTextView.this, info);
}
} else if (getTag() instanceof ShortcutInfo) {
ShortcutInfo info = (ShortcutInfo) getTag();
if (info.usingLowResIcon) {
mIconLoadRequest = LauncherAppState.getInstance().getIconCache()
.updateIconInBackground(BubbleTextView.this, info);
}
} else if (getTag() instanceof PackageItemInfo) {
PackageItemInfo info = (PackageItemInfo) getTag();
if (info.usingLowResIcon) { if (info.usingLowResIcon) {
mIconLoadRequest = LauncherAppState.getInstance().getIconCache() mIconLoadRequest = LauncherAppState.getInstance().getIconCache()
.updateIconInBackground(BubbleTextView.this, info); .updateIconInBackground(BubbleTextView.this, info);

View File

@ -406,13 +406,14 @@ public class IconCache {
* Fetches high-res icon for the provided ItemInfo and updates the caller when done. * Fetches high-res icon for the provided ItemInfo and updates the caller when done.
* @return a request ID that can be used to cancel the request. * @return a request ID that can be used to cancel the request.
*/ */
public IconLoadRequest updateIconInBackground(final BubbleTextView caller, final ItemInfo info) { public IconLoadRequest updateIconInBackground(final ItemInfoUpdateReceiver caller,
final ItemInfoWithIcon info) {
Runnable request = new Runnable() { Runnable request = new Runnable() {
@Override @Override
public void run() { public void run() {
if (info instanceof AppInfo || info instanceof ShortcutInfo) { if (info instanceof AppInfo || info instanceof ShortcutInfo) {
getTitleAndIcon((ItemInfoWithIcon) info, false); getTitleAndIcon(info, false);
} else if (info instanceof PackageItemInfo) { } else if (info instanceof PackageItemInfo) {
getTitleAndIconForApp((PackageItemInfo) info, false); getTitleAndIconForApp((PackageItemInfo) info, false);
} }
@ -429,20 +430,6 @@ public class IconCache {
return new IconLoadRequest(request, mWorkerHandler); return new IconLoadRequest(request, mWorkerHandler);
} }
/**
* Returns a high res icon for the given intent and user
*/
public synchronized Bitmap getIcon(Intent intent, UserHandle user) {
ComponentName component = intent.getComponent();
// null info means not installed, but if we have a component from the intent then
// we should still look in the cache for restored app icons.
if (component == null) {
return getDefaultIcon(user);
}
return cacheLocked(component, new ActivityInfoProvider(intent, user),
user, true, false /* useLowRes */).icon;
}
/** /**
* Updates {@param application} only if a valid entry is found. * Updates {@param application} only if a valid entry is found.
*/ */
@ -528,7 +515,7 @@ public class IconCache {
*/ */
protected CacheEntry cacheLocked( protected CacheEntry cacheLocked(
@NonNull ComponentName componentName, @NonNull ComponentName componentName,
@NonNull Provider<LauncherActivityInfoCompat> infoProfider, @NonNull Provider<LauncherActivityInfoCompat> infoProvider,
UserHandle user, boolean usePackageIcon, boolean useLowResIcon) { UserHandle user, boolean usePackageIcon, boolean useLowResIcon) {
ComponentKey cacheKey = new ComponentKey(componentName, user); ComponentKey cacheKey = new ComponentKey(componentName, user);
CacheEntry entry = mCache.get(cacheKey); CacheEntry entry = mCache.get(cacheKey);
@ -541,7 +528,7 @@ public class IconCache {
boolean providerFetchedOnce = false; boolean providerFetchedOnce = false;
if (!getEntryFromDB(cacheKey, entry, useLowResIcon) || DEBUG_IGNORE_CACHE) { if (!getEntryFromDB(cacheKey, entry, useLowResIcon) || DEBUG_IGNORE_CACHE) {
info = infoProfider.get(); info = infoProvider.get();
providerFetchedOnce = true; providerFetchedOnce = true;
if (info != null) { if (info != null) {
@ -570,7 +557,7 @@ public class IconCache {
if (TextUtils.isEmpty(entry.title)) { if (TextUtils.isEmpty(entry.title)) {
if (info == null && !providerFetchedOnce) { if (info == null && !providerFetchedOnce) {
info = infoProfider.get(); info = infoProvider.get();
providerFetchedOnce = true; providerFetchedOnce = true;
} }
if (info != null) { if (info != null) {
@ -868,4 +855,12 @@ public class IconCache {
return mLauncherApps.resolveActivity(mIntent, mUser); return mLauncherApps.resolveActivity(mIntent, mUser);
} }
} }
/**
* Interface for receiving itemInfo with high-res icon.
*/
public interface ItemInfoUpdateReceiver {
void reapplyItemInfo(ItemInfoWithIcon info);
}
} }

View File

@ -17,7 +17,6 @@
package com.android.launcher3; package com.android.launcher3;
import android.content.Context; import android.content.Context;
import android.content.Intent;
import android.content.res.Resources.Theme; import android.content.res.Resources.Theme;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.graphics.Canvas; import android.graphics.Canvas;
@ -35,8 +34,11 @@ import android.view.View;
import android.view.View.OnClickListener; import android.view.View.OnClickListener;
import com.android.launcher3.graphics.DrawableFactory; import com.android.launcher3.graphics.DrawableFactory;
import com.android.launcher3.IconCache.ItemInfoUpdateReceiver;
import com.android.launcher3.model.PackageItemInfo;
public class PendingAppWidgetHostView extends LauncherAppWidgetHostView implements OnClickListener { public class PendingAppWidgetHostView extends LauncherAppWidgetHostView
implements OnClickListener, ItemInfoUpdateReceiver {
private static final float SETUP_ICON_SIZE_FACTOR = 2f / 5; private static final float SETUP_ICON_SIZE_FACTOR = 2f / 5;
private static final float MIN_SATUNATION = 0.7f; private static final float MIN_SATUNATION = 0.7f;
@ -47,7 +49,6 @@ public class PendingAppWidgetHostView extends LauncherAppWidgetHostView implemen
private OnClickListener mClickListener; private OnClickListener mClickListener;
private final LauncherAppWidgetInfo mInfo; private final LauncherAppWidgetInfo mInfo;
private final int mStartState; private final int mStartState;
private final Intent mIconLookupIntent;
private final boolean mDisabledForSafeMode; private final boolean mDisabledForSafeMode;
private Launcher mLauncher; private Launcher mLauncher;
@ -68,7 +69,6 @@ public class PendingAppWidgetHostView extends LauncherAppWidgetHostView implemen
mLauncher = Launcher.getLauncher(context); mLauncher = Launcher.getLauncher(context);
mInfo = info; mInfo = info;
mStartState = info.restoreStatus; mStartState = info.restoreStatus;
mIconLookupIntent = new Intent().setComponent(info.providerName);
mDisabledForSafeMode = disabledForSafeMode; mDisabledForSafeMode = disabledForSafeMode;
mPaint = new TextPaint(); mPaint = new TextPaint();
@ -79,9 +79,13 @@ public class PendingAppWidgetHostView extends LauncherAppWidgetHostView implemen
setWillNotDraw(false); setWillNotDraw(false);
setElevation(getResources().getDimension(R.dimen.pending_widget_elevation)); setElevation(getResources().getDimension(R.dimen.pending_widget_elevation));
updateIcon(cache);
updateAppWidget(null); updateAppWidget(null);
setOnClickListener(mLauncher); setOnClickListener(mLauncher);
// Load icon
PackageItemInfo item = new PackageItemInfo(info.providerName.getPackageName());
item.user = info.user;
cache.updateIconInBackground(this, item);
} }
@Override @Override
@ -117,8 +121,9 @@ public class PendingAppWidgetHostView extends LauncherAppWidgetHostView implemen
mDrawableSizeChanged = true; mDrawableSizeChanged = true;
} }
private void updateIcon(IconCache cache) { @Override
Bitmap icon = cache.getIcon(mIconLookupIntent, mInfo.user); public void reapplyItemInfo(ItemInfoWithIcon info) {
Bitmap icon = info.iconBitmap;
if (mIcon == icon) { if (mIcon == icon) {
return; return;
} }
@ -157,6 +162,7 @@ public class PendingAppWidgetHostView extends LauncherAppWidgetHostView implemen
} }
mDrawableSizeChanged = true; mDrawableSizeChanged = true;
} }
invalidate();
} }
private void updateSettingColor() { private void updateSettingColor() {

View File

@ -28,7 +28,7 @@ public class PackageItemInfo extends ItemInfoWithIcon {
*/ */
public String packageName; public String packageName;
PackageItemInfo(String packageName) { public PackageItemInfo(String packageName) {
this.packageName = packageName; this.packageName = packageName;
} }