Moving some image handling classes to .graphics package

Change-Id: Id6d3d0b9c345a503ff2e09f073eb4b6449e21c7e
This commit is contained in:
Sunny Goyal 2016-09-01 12:50:11 -07:00
parent 6677eb7668
commit 10629b077c
20 changed files with 302 additions and 309 deletions

View File

@ -38,6 +38,7 @@ import android.util.Patterns;
import com.android.launcher3.LauncherProvider.SqlArguments;
import com.android.launcher3.LauncherSettings.Favorites;
import com.android.launcher3.config.FeatureFlags;
import com.android.launcher3.graphics.LauncherIcons;
import com.android.launcher3.util.Thunk;
import org.xmlpull.v1.XmlPullParser;
@ -436,7 +437,7 @@ public class AutoInstallsLayout {
return -1;
}
ItemInfo.writeBitmap(mValues, Utilities.createIconBitmap(icon, mContext));
ItemInfo.writeBitmap(mValues, LauncherIcons.createIconBitmap(icon, mContext));
mValues.put(Favorites.ICON_PACKAGE, mIconRes.getResourcePackageName(iconId));
mValues.put(Favorites.ICON_RESOURCE, mIconRes.getResourceName(iconId));

View File

@ -25,6 +25,8 @@ import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import com.android.launcher3.graphics.HolographicOutlineHelper;
/**
* The fast scroller popup that shows the section name the list will jump to.
*/
@ -116,7 +118,7 @@ public class BaseRecyclerViewFastScrollPopup {
mBgBounds.bottom = mBgBounds.top + bgHeight;
// Generate a bitmap for a shadow matching these bounds
mShadow = HolographicOutlineHelper.obtain(
mShadow = HolographicOutlineHelper.getInstance(
mRv.getContext()).createMediumDropShadow(mBg, false /* shouldCache */);
} else {
mShadow = null;

View File

@ -42,6 +42,7 @@ import android.widget.TextView;
import com.android.launcher3.IconCache.IconLoadRequest;
import com.android.launcher3.folder.FolderIcon;
import com.android.launcher3.graphics.HolographicOutlineHelper;
import com.android.launcher3.model.PackageItemInfo;
import java.text.NumberFormat;
@ -148,7 +149,7 @@ public class BubbleTextView extends TextView
mLongPressHelper = new CheckLongPressHelper(this);
mStylusEventHelper = new StylusEventHelper(new SimpleOnStylusPressListener(this), this);
mOutlineHelper = HolographicOutlineHelper.obtain(getContext());
mOutlineHelper = HolographicOutlineHelper.getInstance(getContext());
setAccessibilityDelegate(mLauncher.getAccessibilityDelegate());
}
@ -326,7 +327,7 @@ public class BubbleTextView extends TextView
void setStayPressed(boolean stayPressed) {
mStayPressed = stayPressed;
if (!stayPressed) {
HolographicOutlineHelper.obtain(getContext()).recycleShadowBitmap(mPressedBackground);
HolographicOutlineHelper.getInstance(getContext()).recycleShadowBitmap(mPressedBackground);
mPressedBackground = null;
} else {
if (mPressedBackground == null) {

View File

@ -47,6 +47,7 @@ import com.android.launcher3.compat.LauncherAppsCompat;
import com.android.launcher3.compat.UserHandleCompat;
import com.android.launcher3.compat.UserManagerCompat;
import com.android.launcher3.config.FeatureFlags;
import com.android.launcher3.graphics.LauncherIcons;
import com.android.launcher3.model.PackageItemInfo;
import com.android.launcher3.util.ComponentKey;
import com.android.launcher3.util.SQLiteCacheHelper;
@ -187,7 +188,7 @@ public class IconCache {
private Bitmap makeDefaultIcon(UserHandleCompat user) {
Drawable unbadged = getFullResDefaultActivityIcon();
return Utilities.createBadgedIconBitmap(unbadged, user, mContext);
return LauncherIcons.createBadgedIconBitmap(unbadged, user, mContext);
}
/**
@ -387,7 +388,7 @@ public class IconCache {
}
if (entry == null) {
entry = new CacheEntry();
entry.icon = Utilities.createBadgedIconBitmap(
entry.icon = LauncherIcons.createBadgedIconBitmap(
mIconProvider.getIcon(app, mIconDpi), app.getUser(),
mContext);
}
@ -555,7 +556,7 @@ public class IconCache {
// Check the DB first.
if (!getEntryFromDB(cacheKey, entry, useLowResIcon) || DEBUG_IGNORE_CACHE) {
if (info != null) {
entry.icon = Utilities.createBadgedIconBitmap(
entry.icon = LauncherIcons.createBadgedIconBitmap(
mIconProvider.getIcon(info, mIconDpi), info.getUser(),
mContext);
} else {
@ -606,7 +607,7 @@ public class IconCache {
entry.title = title;
}
if (icon != null) {
entry.icon = Utilities.createIconBitmap(icon, mContext);
entry.icon = LauncherIcons.createIconBitmap(icon, mContext);
}
}
@ -641,7 +642,7 @@ public class IconCache {
// Load the full res icon for the application, but if useLowResIcon is set, then
// only keep the low resolution icon instead of the larger full-sized icon
Bitmap icon = Utilities.createBadgedIconBitmap(
Bitmap icon = LauncherIcons.createBadgedIconBitmap(
appInfo.loadIcon(mPackageManager), user, mContext);
Bitmap lowResIcon = generateLowResIcon(icon, mPackageBgColor);
entry.title = appInfo.loadLabel(mPackageManager);

View File

@ -1,30 +0,0 @@
/*
* Copyright (C) 2008 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.launcher3;
import android.content.Context;
@Deprecated
public class LauncherClings {
private static final String WORKSPACE_CLING_DISMISSED_KEY = "cling_gel.workspace.dismissed";
public static void markFirstRunClingDismissed(Context ctx) {
Utilities.getPrefs(ctx).edit()
.putBoolean(WORKSPACE_CLING_DISMISSED_KEY, true)
.apply();
}
}

View File

@ -57,6 +57,7 @@ import com.android.launcher3.config.ProviderConfig;
import com.android.launcher3.dynamicui.ExtractionUtils;
import com.android.launcher3.folder.Folder;
import com.android.launcher3.folder.FolderIcon;
import com.android.launcher3.graphics.LauncherIcons;
import com.android.launcher3.logging.FileLog;
import com.android.launcher3.model.GridSizeMigrationTask;
import com.android.launcher3.model.WidgetsModel;
@ -3159,7 +3160,7 @@ public class LauncherModel extends BroadcastReceiver
// Update shortcuts which use iconResource.
if ((si.iconResource != null)
&& pkgFilter.matches(si.iconResource.packageName)) {
Bitmap icon = Utilities.createIconBitmap(
Bitmap icon = LauncherIcons.createIconBitmap(
si.iconResource.packageName,
si.iconResource.resourceName, context);
if (icon != null) {
@ -3753,17 +3754,15 @@ public class LauncherModel extends BroadcastReceiver
}
Bitmap icon = null;
boolean customIcon = false;
ShortcutIconResource iconResource = null;
if (bitmap instanceof Bitmap) {
icon = Utilities.createIconBitmap((Bitmap) bitmap, context);
customIcon = true;
icon = LauncherIcons.createIconBitmap((Bitmap) bitmap, context);
} else {
Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
if (extra instanceof ShortcutIconResource) {
iconResource = (ShortcutIconResource) extra;
icon = Utilities.createIconBitmap(iconResource.packageName,
icon = LauncherIcons.createIconBitmap(iconResource.packageName,
iconResource.resourceName, context);
}
}

View File

@ -764,12 +764,7 @@ public class LauncherProvider extends ContentProvider {
}
}
case 16: {
// We use the db version upgrade here to identify users who may not have seen
// clings yet (because they weren't available), but for whom the clings are now
// available (tablet users). Because one of the possible cling flows (migration)
// is very destructive (wipes out workspaces), we want to prevent this from showing
// until clear data. We do so by marking that the clings have been shown.
LauncherClings.markFirstRunClingDismissed(mContext);
// No-op
}
case 17: {
// No-op

View File

@ -31,6 +31,7 @@ import com.android.launcher3.compat.LauncherActivityInfoCompat;
import com.android.launcher3.compat.UserHandleCompat;
import com.android.launcher3.compat.UserManagerCompat;
import com.android.launcher3.folder.FolderIcon;
import com.android.launcher3.graphics.LauncherIcons;
import com.android.launcher3.shortcuts.ShortcutInfoCompat;
/**
@ -323,13 +324,13 @@ public class ShortcutInfo extends ItemInfo {
IconCache cache = launcherAppState.getIconCache();
Bitmap unbadgedBitmap = unbadgedDrawable == null
? cache.getDefaultIcon(UserHandleCompat.myUserHandle())
: Utilities.createScaledBitmapWithoutShadow(unbadgedDrawable, context);
: LauncherIcons.createScaledBitmapWithoutShadow(unbadgedDrawable, context);
setIcon(getBadgedIcon(unbadgedBitmap, shortcutInfo, cache, context));
}
protected Bitmap getBadgedIcon(Bitmap unbadgedBitmap, ShortcutInfoCompat shortcutInfo,
IconCache cache, Context context) {
unbadgedBitmap = Utilities.addShadowToIcon(unbadgedBitmap);
unbadgedBitmap = LauncherIcons.addShadowToIcon(unbadgedBitmap);
// Get the app info for the source activity.
AppInfo appInfo = new AppInfo();
appInfo.user = user;
@ -338,9 +339,9 @@ public class ShortcutInfo extends ItemInfo {
cache.getTitleAndIcon(appInfo, shortcutInfo.getActivityInfo(context), false);
} catch (NullPointerException e) {
// This may happen when we fail to load the activity info. Worst case ignore badging.
return Utilities.badgeIconForUser(unbadgedBitmap, user, context);
return LauncherIcons.badgeIconForUser(unbadgedBitmap, user, context);
}
return Utilities.badgeWithBitmap(unbadgedBitmap, appInfo.iconBitmap, context);
return LauncherIcons.badgeWithBitmap(unbadgedBitmap, appInfo.iconBitmap, context);
}
/** Returns the ShortcutInfo id associated with the deep shortcut. */

View File

@ -31,19 +31,11 @@ import android.content.pm.PackageManager.NameNotFoundException;
import android.content.pm.ResolveInfo;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PaintFlagsDrawFilter;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.PaintDrawable;
import android.os.Build;
import android.os.Bundle;
import android.os.PowerManager;
@ -62,11 +54,7 @@ import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityManager;
import android.widget.Toast;
import com.android.launcher3.compat.UserHandleCompat;
import com.android.launcher3.config.FeatureFlags;
import com.android.launcher3.config.ProviderConfig;
import com.android.launcher3.graphics.ShadowGenerator;
import com.android.launcher3.util.IconNormalizer;
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
@ -90,19 +78,9 @@ public final class Utilities {
private static final String TAG = "Launcher.Utilities";
private static final Rect sOldBounds = new Rect();
private static final Canvas sCanvas = new Canvas();
private static final Pattern sTrimPattern =
Pattern.compile("^[\\s|\\p{javaSpaceChar}]*(.*)[\\s|\\p{javaSpaceChar}]*$");
static {
sCanvas.setDrawFilter(new PaintFlagsDrawFilter(Paint.DITHER_FLAG,
Paint.FILTER_BITMAP_FLAG));
}
static int sColors[] = { 0xffff0000, 0xff00ff00, 0xff0000ff };
static int sColorIndex = 0;
private static final int[] sLoc0 = new int[2];
private static final int[] sLoc1 = new int[2];
@ -170,198 +148,6 @@ public final class Utilities {
return false;
}
public static Bitmap createIconBitmap(Cursor c, int iconIndex, Context context) {
byte[] data = c.getBlob(iconIndex);
try {
return createIconBitmap(BitmapFactory.decodeByteArray(data, 0, data.length), context);
} catch (Exception e) {
return null;
}
}
/**
* Returns a bitmap suitable for the all apps view. If the package or the resource do not
* exist, it returns null.
*/
public static Bitmap createIconBitmap(String packageName, String resourceName,
Context context) {
PackageManager packageManager = context.getPackageManager();
// the resource
try {
Resources resources = packageManager.getResourcesForApplication(packageName);
if (resources != null) {
final int id = resources.getIdentifier(resourceName, null, null);
return createIconBitmap(
resources.getDrawableForDensity(id, LauncherAppState.getInstance()
.getInvariantDeviceProfile().fillResIconDpi), context);
}
} catch (Exception e) {
// Icon not found.
}
return null;
}
private static int getIconBitmapSize() {
return LauncherAppState.getInstance().getInvariantDeviceProfile().iconBitmapSize;
}
/**
* Returns a bitmap which is of the appropriate size to be displayed as an icon
*/
public static Bitmap createIconBitmap(Bitmap icon, Context context) {
final int iconBitmapSize = getIconBitmapSize();
if (iconBitmapSize == icon.getWidth() && iconBitmapSize == icon.getHeight()) {
return icon;
}
return createIconBitmap(new BitmapDrawable(context.getResources(), icon), context);
}
/**
* Returns a bitmap suitable for the all apps view. The icon is badged for {@param user}.
* The bitmap is also visually normalized with other icons.
*/
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public static Bitmap createBadgedIconBitmap(
Drawable icon, UserHandleCompat user, Context context) {
float scale = FeatureFlags.LAUNCHER3_DISABLE_ICON_NORMALIZATION ?
1 : IconNormalizer.getInstance().getScale(icon, null);
Bitmap bitmap = createIconBitmap(icon, context, scale);
return badgeIconForUser(bitmap, user, context);
}
/**
* Badges the provided icon with the user badge if required.
*/
public static Bitmap badgeIconForUser(Bitmap icon, UserHandleCompat user, Context context) {
if (Utilities.ATLEAST_LOLLIPOP && user != null
&& !UserHandleCompat.myUserHandle().equals(user)) {
BitmapDrawable drawable = new FixedSizeBitmapDrawable(icon);
Drawable badged = context.getPackageManager().getUserBadgedIcon(
drawable, user.getUser());
if (badged instanceof BitmapDrawable) {
return ((BitmapDrawable) badged).getBitmap();
} else {
return createIconBitmap(badged, context);
}
} else {
return icon;
}
}
/**
* Creates a normalized bitmap suitable for the all apps view. The bitmap is also visually
* normalized with other icons and has enough spacing to add shadow.
*/
public static Bitmap createScaledBitmapWithoutShadow(Drawable icon, Context context) {
RectF iconBounds = new RectF();
float scale = FeatureFlags.LAUNCHER3_DISABLE_ICON_NORMALIZATION ?
1 : IconNormalizer.getInstance().getScale(icon, iconBounds);
scale = Math.min(scale, ShadowGenerator.getScaleForBounds(iconBounds));
return createIconBitmap(icon, context, scale);
}
/**
* Adds a shadow to the provided icon. It assumes that the icon has already been scaled using
* {@link #createScaledBitmapWithoutShadow(Drawable, Context)}
*/
public static Bitmap addShadowToIcon(Bitmap icon) {
return ShadowGenerator.getInstance().recreateIcon(icon);
}
/**
* Adds the {@param badge} on top of {@param srcTgt} using the badge dimensions.
*/
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public static Bitmap badgeWithBitmap(Bitmap srcTgt, Bitmap badge, Context context) {
int badgeSize = context.getResources().getDimensionPixelSize(R.dimen.profile_badge_size);
synchronized (sCanvas) {
sCanvas.setBitmap(srcTgt);
sCanvas.drawBitmap(badge, new Rect(0, 0, badge.getWidth(), badge.getHeight()),
new Rect(srcTgt.getWidth() - badgeSize,
srcTgt.getHeight() - badgeSize, srcTgt.getWidth(), srcTgt.getHeight()),
new Paint(Paint.FILTER_BITMAP_FLAG));
sCanvas.setBitmap(null);
}
return srcTgt;
}
/**
* Returns a bitmap suitable for the all apps view.
*/
public static Bitmap createIconBitmap(Drawable icon, Context context) {
return createIconBitmap(icon, context, 1.0f /* scale */);
}
/**
* @param scale the scale to apply before drawing {@param icon} on the canvas
*/
public static Bitmap createIconBitmap(Drawable icon, Context context, float scale) {
synchronized (sCanvas) {
final int iconBitmapSize = getIconBitmapSize();
int width = iconBitmapSize;
int height = iconBitmapSize;
if (icon instanceof PaintDrawable) {
PaintDrawable painter = (PaintDrawable) icon;
painter.setIntrinsicWidth(width);
painter.setIntrinsicHeight(height);
} else if (icon instanceof BitmapDrawable) {
// Ensure the bitmap has a density.
BitmapDrawable bitmapDrawable = (BitmapDrawable) icon;
Bitmap bitmap = bitmapDrawable.getBitmap();
if (bitmap != null && bitmap.getDensity() == Bitmap.DENSITY_NONE) {
bitmapDrawable.setTargetDensity(context.getResources().getDisplayMetrics());
}
}
int sourceWidth = icon.getIntrinsicWidth();
int sourceHeight = icon.getIntrinsicHeight();
if (sourceWidth > 0 && sourceHeight > 0) {
// Scale the icon proportionally to the icon dimensions
final float ratio = (float) sourceWidth / sourceHeight;
if (sourceWidth > sourceHeight) {
height = (int) (width / ratio);
} else if (sourceHeight > sourceWidth) {
width = (int) (height * ratio);
}
}
// no intrinsic size --> use default size
int textureWidth = iconBitmapSize;
int textureHeight = iconBitmapSize;
final Bitmap bitmap = Bitmap.createBitmap(textureWidth, textureHeight,
Bitmap.Config.ARGB_8888);
final Canvas canvas = sCanvas;
canvas.setBitmap(bitmap);
final int left = (textureWidth-width) / 2;
final int top = (textureHeight-height) / 2;
@SuppressWarnings("all") // suppress dead code warning
final boolean debug = false;
if (debug) {
// draw a big box for the icon for debugging
canvas.drawColor(sColors[sColorIndex]);
if (++sColorIndex >= sColors.length) sColorIndex = 0;
Paint debugPaint = new Paint();
debugPaint.setColor(0xffcccc00);
canvas.drawRect(left, top, left+width, top+height, debugPaint);
}
sOldBounds.set(icon.getBounds());
icon.setBounds(left, top, left+width, top+height);
canvas.save(Canvas.MATRIX_SAVE_FLAG);
canvas.scale(scale, scale, textureWidth / 2, textureHeight / 2);
icon.draw(canvas);
canvas.restore();
icon.setBounds(sOldBounds);
canvas.setBitmap(null);
return bitmap;
}
}
/**
* Given a coordinate relative to the descendant, find the coordinate in a parent view's
* coordinates.
@ -880,28 +666,6 @@ public final class Utilities {
return c == null || c.isEmpty();
}
/**
* An extension of {@link BitmapDrawable} which returns the bitmap pixel size as intrinsic size.
* This allows the badging to be done based on the action bitmap size rather than
* the scaled bitmap size.
*/
private static class FixedSizeBitmapDrawable extends BitmapDrawable {
public FixedSizeBitmapDrawable(Bitmap bitmap) {
super(null, bitmap);
}
@Override
public int getIntrinsicHeight() {
return getBitmap().getWidth();
}
@Override
public int getIntrinsicWidth() {
return getBitmap().getWidth();
}
}
public static int getColorAccent(Context context) {
TypedArray ta = context.obtainStyledAttributes(new int[]{android.R.attr.colorAccent});
int colorAccent = ta.getColor(0, 0);

View File

@ -24,7 +24,6 @@ import android.graphics.drawable.Drawable;
import android.view.View;
import android.widget.TextView;
import com.android.launcher3.HolographicOutlineHelper;
import com.android.launcher3.Launcher;
import com.android.launcher3.PreloadIconDrawable;
import com.android.launcher3.Workspace;
@ -137,7 +136,7 @@ public class DragPreviewProvider {
mView.getHeight() + DRAG_BITMAP_PADDING, Bitmap.Config.ALPHA_8);
canvas.setBitmap(b);
drawDragView(canvas);
HolographicOutlineHelper.obtain(mView.getContext())
HolographicOutlineHelper.getInstance(mView.getContext())
.applyExpensiveOutlineWithBlur(b, canvas);
canvas.setBitmap(null);
return b;

View File

@ -14,7 +14,7 @@
* limitations under the License.
*/
package com.android.launcher3;
package com.android.launcher3.graphics;
import android.content.Context;
import android.content.res.Resources;
@ -29,6 +29,8 @@ import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.util.SparseArray;
import com.android.launcher3.BubbleTextView;
import com.android.launcher3.R;
import com.android.launcher3.config.ProviderConfig;
import java.nio.ByteBuffer;
@ -72,9 +74,9 @@ public class HolographicOutlineHelper {
mErasePaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
}
public static HolographicOutlineHelper obtain(Context context) {
public static HolographicOutlineHelper getInstance(Context context) {
if (sInstance == null) {
sInstance = new HolographicOutlineHelper(context);
sInstance = new HolographicOutlineHelper(context.getApplicationContext());
}
return sInstance;
}
@ -155,11 +157,11 @@ public class HolographicOutlineHelper {
thickInnerBlur.recycle();
}
Bitmap createMediumDropShadow(BubbleTextView view) {
public Bitmap createMediumDropShadow(BubbleTextView view) {
return createMediumDropShadow(view.getIcon(), view.getScaleX(), view.getScaleY(), true);
}
Bitmap createMediumDropShadow(Drawable drawable, boolean shouldCache) {
public Bitmap createMediumDropShadow(Drawable drawable, boolean shouldCache) {
return createMediumDropShadow(drawable, 1f, 1f, shouldCache);
}

View File

@ -14,7 +14,7 @@
* limitations under the License.
*/
package com.android.launcher3.util;
package com.android.launcher3.graphics;
import android.graphics.Bitmap;
import android.graphics.Canvas;

View File

@ -0,0 +1,258 @@
/*
* Copyright (C) 2016 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.launcher3.graphics;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PaintFlagsDrawFilter;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.PaintDrawable;
import android.os.Build;
import com.android.launcher3.LauncherAppState;
import com.android.launcher3.R;
import com.android.launcher3.Utilities;
import com.android.launcher3.compat.UserHandleCompat;
import com.android.launcher3.config.FeatureFlags;
/**
* Helper methods for generating various launcher icons
*/
public class LauncherIcons {
private static final Rect sOldBounds = new Rect();
private static final Canvas sCanvas = new Canvas();
static {
sCanvas.setDrawFilter(new PaintFlagsDrawFilter(Paint.DITHER_FLAG,
Paint.FILTER_BITMAP_FLAG));
}
public static Bitmap createIconBitmap(Cursor c, int iconIndex, Context context) {
byte[] data = c.getBlob(iconIndex);
try {
return createIconBitmap(BitmapFactory.decodeByteArray(data, 0, data.length), context);
} catch (Exception e) {
return null;
}
}
/**
* Returns a bitmap suitable for the all apps view. If the package or the resource do not
* exist, it returns null.
*/
public static Bitmap createIconBitmap(String packageName, String resourceName,
Context context) {
PackageManager packageManager = context.getPackageManager();
// the resource
try {
Resources resources = packageManager.getResourcesForApplication(packageName);
if (resources != null) {
final int id = resources.getIdentifier(resourceName, null, null);
return createIconBitmap(
resources.getDrawableForDensity(id, LauncherAppState.getInstance()
.getInvariantDeviceProfile().fillResIconDpi), context);
}
} catch (Exception e) {
// Icon not found.
}
return null;
}
private static int getIconBitmapSize() {
return LauncherAppState.getInstance().getInvariantDeviceProfile().iconBitmapSize;
}
/**
* Returns a bitmap which is of the appropriate size to be displayed as an icon
*/
public static Bitmap createIconBitmap(Bitmap icon, Context context) {
final int iconBitmapSize = getIconBitmapSize();
if (iconBitmapSize == icon.getWidth() && iconBitmapSize == icon.getHeight()) {
return icon;
}
return createIconBitmap(new BitmapDrawable(context.getResources(), icon), context);
}
/**
* Returns a bitmap suitable for the all apps view. The icon is badged for {@param user}.
* The bitmap is also visually normalized with other icons.
*/
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public static Bitmap createBadgedIconBitmap(
Drawable icon, UserHandleCompat user, Context context) {
float scale = FeatureFlags.LAUNCHER3_DISABLE_ICON_NORMALIZATION ?
1 : IconNormalizer.getInstance().getScale(icon, null);
Bitmap bitmap = createIconBitmap(icon, context, scale);
return badgeIconForUser(bitmap, user, context);
}
/**
* Badges the provided icon with the user badge if required.
*/
public static Bitmap badgeIconForUser(Bitmap icon, UserHandleCompat user, Context context) {
if (Utilities.ATLEAST_LOLLIPOP && user != null
&& !UserHandleCompat.myUserHandle().equals(user)) {
BitmapDrawable drawable = new FixedSizeBitmapDrawable(icon);
Drawable badged = context.getPackageManager().getUserBadgedIcon(
drawable, user.getUser());
if (badged instanceof BitmapDrawable) {
return ((BitmapDrawable) badged).getBitmap();
} else {
return createIconBitmap(badged, context);
}
} else {
return icon;
}
}
/**
* Creates a normalized bitmap suitable for the all apps view. The bitmap is also visually
* normalized with other icons and has enough spacing to add shadow.
*/
public static Bitmap createScaledBitmapWithoutShadow(Drawable icon, Context context) {
RectF iconBounds = new RectF();
float scale = FeatureFlags.LAUNCHER3_DISABLE_ICON_NORMALIZATION ?
1 : IconNormalizer.getInstance().getScale(icon, iconBounds);
scale = Math.min(scale, ShadowGenerator.getScaleForBounds(iconBounds));
return createIconBitmap(icon, context, scale);
}
/**
* Adds a shadow to the provided icon. It assumes that the icon has already been scaled using
* {@link #createScaledBitmapWithoutShadow(Drawable, Context)}
*/
public static Bitmap addShadowToIcon(Bitmap icon) {
return ShadowGenerator.getInstance().recreateIcon(icon);
}
/**
* Adds the {@param badge} on top of {@param srcTgt} using the badge dimensions.
*/
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public static Bitmap badgeWithBitmap(Bitmap srcTgt, Bitmap badge, Context context) {
int badgeSize = context.getResources().getDimensionPixelSize(R.dimen.profile_badge_size);
synchronized (sCanvas) {
sCanvas.setBitmap(srcTgt);
sCanvas.drawBitmap(badge, new Rect(0, 0, badge.getWidth(), badge.getHeight()),
new Rect(srcTgt.getWidth() - badgeSize,
srcTgt.getHeight() - badgeSize, srcTgt.getWidth(), srcTgt.getHeight()),
new Paint(Paint.FILTER_BITMAP_FLAG));
sCanvas.setBitmap(null);
}
return srcTgt;
}
/**
* Returns a bitmap suitable for the all apps view.
*/
public static Bitmap createIconBitmap(Drawable icon, Context context) {
return createIconBitmap(icon, context, 1.0f /* scale */);
}
/**
* @param scale the scale to apply before drawing {@param icon} on the canvas
*/
public static Bitmap createIconBitmap(Drawable icon, Context context, float scale) {
synchronized (sCanvas) {
final int iconBitmapSize = getIconBitmapSize();
int width = iconBitmapSize;
int height = iconBitmapSize;
if (icon instanceof PaintDrawable) {
PaintDrawable painter = (PaintDrawable) icon;
painter.setIntrinsicWidth(width);
painter.setIntrinsicHeight(height);
} else if (icon instanceof BitmapDrawable) {
// Ensure the bitmap has a density.
BitmapDrawable bitmapDrawable = (BitmapDrawable) icon;
Bitmap bitmap = bitmapDrawable.getBitmap();
if (bitmap != null && bitmap.getDensity() == Bitmap.DENSITY_NONE) {
bitmapDrawable.setTargetDensity(context.getResources().getDisplayMetrics());
}
}
int sourceWidth = icon.getIntrinsicWidth();
int sourceHeight = icon.getIntrinsicHeight();
if (sourceWidth > 0 && sourceHeight > 0) {
// Scale the icon proportionally to the icon dimensions
final float ratio = (float) sourceWidth / sourceHeight;
if (sourceWidth > sourceHeight) {
height = (int) (width / ratio);
} else if (sourceHeight > sourceWidth) {
width = (int) (height * ratio);
}
}
// no intrinsic size --> use default size
int textureWidth = iconBitmapSize;
int textureHeight = iconBitmapSize;
final Bitmap bitmap = Bitmap.createBitmap(textureWidth, textureHeight,
Bitmap.Config.ARGB_8888);
final Canvas canvas = sCanvas;
canvas.setBitmap(bitmap);
final int left = (textureWidth-width) / 2;
final int top = (textureHeight-height) / 2;
sOldBounds.set(icon.getBounds());
icon.setBounds(left, top, left+width, top+height);
canvas.save(Canvas.MATRIX_SAVE_FLAG);
canvas.scale(scale, scale, textureWidth / 2, textureHeight / 2);
icon.draw(canvas);
canvas.restore();
icon.setBounds(sOldBounds);
canvas.setBitmap(null);
return bitmap;
}
}
/**
* An extension of {@link BitmapDrawable} which returns the bitmap pixel size as intrinsic size.
* This allows the badging to be done based on the action bitmap size rather than
* the scaled bitmap size.
*/
private static class FixedSizeBitmapDrawable extends BitmapDrawable {
public FixedSizeBitmapDrawable(Bitmap bitmap) {
super(null, bitmap);
}
@Override
public int getIntrinsicHeight() {
return getBitmap().getWidth();
}
@Override
public int getIntrinsicWidth() {
return getBitmap().getWidth();
}
}
}

View File

@ -63,6 +63,7 @@ import com.android.launcher3.dragndrop.DragController;
import com.android.launcher3.dragndrop.DragLayer;
import com.android.launcher3.dragndrop.DragOptions;
import com.android.launcher3.dragndrop.DragView;
import com.android.launcher3.graphics.LauncherIcons;
import com.android.launcher3.graphics.TriangleShape;
import com.android.launcher3.userevent.nano.LauncherLogProto;
import com.android.launcher3.userevent.nano.LauncherLogProto.Target;
@ -391,7 +392,7 @@ public class DeepShortcutsContainer extends LinearLayout implements View.OnLongC
private void showDragView(BubbleTextView originalIcon) {
// TODO: implement support for Drawable DragViews so we don't have to create a bitmap here.
Bitmap b = Utilities.createIconBitmap(originalIcon.getIcon(), mLauncher);
Bitmap b = LauncherIcons.createIconBitmap(originalIcon.getIcon(), mLauncher);
float scale = mLauncher.getDragLayer().getLocationInDragLayer(originalIcon, mTempXY);
int dragLayerX = Math.round(mTempXY[0] - (b.getWidth() - scale * originalIcon.getWidth()) / 2);
int dragLayerY = Math.round(mTempXY[1] - (b.getHeight() - scale * b.getHeight()) / 2

View File

@ -23,7 +23,7 @@ import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.view.View;
import com.android.launcher3.HolographicOutlineHelper;
import com.android.launcher3.graphics.HolographicOutlineHelper;
import com.android.launcher3.Launcher;
import com.android.launcher3.Utilities;
import com.android.launcher3.graphics.DragPreviewProvider;
@ -44,7 +44,7 @@ public class ShortcutDragPreviewProvider extends DragPreviewProvider {
public Bitmap createDragOutline(Canvas canvas) {
Bitmap b = drawScaledPreview(canvas, Bitmap.Config.ALPHA_8);
HolographicOutlineHelper.obtain(mView.getContext())
HolographicOutlineHelper.getInstance(mView.getContext())
.applyExpensiveOutlineWithBlur(b, canvas);
canvas.setBitmap(null);
return b;

View File

@ -25,6 +25,7 @@ import android.text.TextUtils;
import com.android.launcher3.LauncherSettings;
import com.android.launcher3.ShortcutInfo;
import com.android.launcher3.Utilities;
import com.android.launcher3.graphics.LauncherIcons;
/**
* Utility class to load icon from a cursor.
@ -59,7 +60,7 @@ public class CursorIconInfo {
info.iconResource = new ShortcutIconResource();
info.iconResource.packageName = packageName;
info.iconResource.resourceName = resourceName;
icon = Utilities.createIconBitmap(packageName, resourceName, mContext);
icon = LauncherIcons.createIconBitmap(packageName, resourceName, mContext);
}
if (icon == null) {
// Failed to load from resource, try loading from DB.
@ -72,7 +73,7 @@ public class CursorIconInfo {
* Loads the fixed bitmap from the icon if available.
*/
public Bitmap loadIcon(Cursor c) {
return Utilities.createIconBitmap(c, iconIndex, mContext);
return LauncherIcons.createIconBitmap(c, iconIndex, mContext);
}
/**

View File

@ -21,7 +21,7 @@ import android.graphics.Canvas;
import android.graphics.Rect;
import android.view.View;
import com.android.launcher3.HolographicOutlineHelper;
import com.android.launcher3.graphics.HolographicOutlineHelper;
import com.android.launcher3.Launcher;
import com.android.launcher3.PendingAddItemInfo;
import com.android.launcher3.Workspace;
@ -67,7 +67,7 @@ public class PendingItemPreviewProvider extends DragPreviewProvider {
// Don't clip alpha values for the drag outline if we're using the default widget preview
boolean clipAlpha = !(mAddInfo instanceof PendingAddWidgetInfo &&
(((PendingAddWidgetInfo) mAddInfo).previewImage == 0));
HolographicOutlineHelper.obtain(mView.getContext())
HolographicOutlineHelper.getInstance(mView.getContext())
.applyExpensiveOutlineWithBlur(b, canvas, clipAlpha);
canvas.setBitmap(null);

View File

@ -45,6 +45,7 @@ import com.android.launcher3.Utilities;
import com.android.launcher3.WidgetPreviewLoader;
import com.android.launcher3.Workspace;
import com.android.launcher3.dragndrop.DragController;
import com.android.launcher3.graphics.LauncherIcons;
import com.android.launcher3.model.WidgetsModel;
import com.android.launcher3.userevent.nano.LauncherLogProto;
import com.android.launcher3.userevent.nano.LauncherLogProto.Target;
@ -240,7 +241,7 @@ public class WidgetsContainerView extends BaseContainerView
} else {
PendingAddShortcutInfo createShortcutInfo = (PendingAddShortcutInfo) v.getTag();
Drawable icon = mIconCache.getFullResIcon(createShortcutInfo.activityInfo);
preview = Utilities.createIconBitmap(icon, mLauncher);
preview = LauncherIcons.createIconBitmap(icon, mLauncher);
createItemInfo.spanX = createItemInfo.spanY = 1;
scale = ((float) mLauncher.getDeviceProfile().iconSizePx) / preview.getWidth();
}

View File

@ -232,7 +232,6 @@ public class BindWidgetTest extends LauncherInstrumentationTestCase {
runTestOnUiThread(new Runnable() {
@Override
public void run() {
LauncherClings.markFirstRunClingDismissed(mTargetContext);
ManagedProfileHeuristic.markExistingUsersForNoFolderCreation(mTargetContext);
LauncherAppState.getInstance().getModel().resetLoadedState(true, true);
}

View File

@ -23,7 +23,6 @@ import com.android.launcher3.InvariantDeviceProfile;
import com.android.launcher3.Launcher;
import com.android.launcher3.LauncherAppState;
import com.android.launcher3.LauncherAppWidgetProviderInfo;
import com.android.launcher3.LauncherClings;
import com.android.launcher3.LauncherSettings;
import com.android.launcher3.R;
import com.android.launcher3.Utilities;
@ -189,7 +188,6 @@ public class LauncherInstrumentationTestCase extends InstrumentationTestCase {
LauncherSettings.Settings.METHOD_CREATE_EMPTY_DB);
LauncherSettings.Settings.call(mTargetContext.getContentResolver(),
LauncherSettings.Settings.METHOD_CLEAR_EMPTY_DB_FLAG);
LauncherClings.markFirstRunClingDismissed(mTargetContext);
ManagedProfileHeuristic.markExistingUsersForNoFolderCreation(mTargetContext);
runTestOnUiThread(new Runnable() {