Badging shortcuts with app icons
Change-Id: I3fa005ece20a54b31f823acb28c384ecdf1eafb1
This commit is contained in:
@@ -28,7 +28,6 @@ import android.text.TextUtils;
|
|||||||
|
|
||||||
import com.android.launcher3.LauncherSettings.Favorites;
|
import com.android.launcher3.LauncherSettings.Favorites;
|
||||||
import com.android.launcher3.compat.LauncherActivityInfoCompat;
|
import com.android.launcher3.compat.LauncherActivityInfoCompat;
|
||||||
import com.android.launcher3.compat.LauncherAppsCompat;
|
|
||||||
import com.android.launcher3.compat.UserHandleCompat;
|
import com.android.launcher3.compat.UserHandleCompat;
|
||||||
import com.android.launcher3.compat.UserManagerCompat;
|
import com.android.launcher3.compat.UserManagerCompat;
|
||||||
import com.android.launcher3.folder.FolderIcon;
|
import com.android.launcher3.folder.FolderIcon;
|
||||||
@@ -295,16 +294,33 @@ public class ShortcutInfo extends ItemInfo {
|
|||||||
contentDescription = UserManagerCompat.getInstance(context)
|
contentDescription = UserManagerCompat.getInstance(context)
|
||||||
.getBadgedLabelForUser(label, user);
|
.getBadgedLabelForUser(label, user);
|
||||||
|
|
||||||
|
// TODO: Use cache for this
|
||||||
LauncherAppState launcherAppState = LauncherAppState.getInstance();
|
LauncherAppState launcherAppState = LauncherAppState.getInstance();
|
||||||
Drawable unbadgedIcon = launcherAppState.getShortcutManager()
|
Drawable unbadgedDrawable = launcherAppState.getShortcutManager()
|
||||||
.getShortcutIconDrawable(shortcutInfo,
|
.getShortcutIconDrawable(shortcutInfo,
|
||||||
launcherAppState.getInvariantDeviceProfile().fillResIconDpi);
|
launcherAppState.getInvariantDeviceProfile().fillResIconDpi);
|
||||||
Bitmap icon = unbadgedIcon == null ? null : getBadgedIcon(unbadgedIcon, context);
|
|
||||||
setIcon(icon != null ? icon : launcherAppState.getIconCache().getDefaultIcon(user));
|
IconCache cache = launcherAppState.getIconCache();
|
||||||
|
Bitmap unbadgedBitmap = unbadgedDrawable == null
|
||||||
|
? cache.getDefaultIcon(UserHandleCompat.myUserHandle())
|
||||||
|
: Utilities.createScaledBitmapWithoutShadow(unbadgedDrawable, context);
|
||||||
|
setIcon(getBadgedIcon(unbadgedBitmap, shortcutInfo, cache, context));
|
||||||
}
|
}
|
||||||
|
|
||||||
protected Bitmap getBadgedIcon(Drawable unbadgedIcon, Context context) {
|
protected Bitmap getBadgedIcon(Bitmap unbadgedBitmap, ShortcutInfoCompat shortcutInfo,
|
||||||
return Utilities.createBadgedIconBitmapWithShadow(unbadgedIcon, user, context);
|
IconCache cache, Context context) {
|
||||||
|
unbadgedBitmap = Utilities.addShadowToIcon(unbadgedBitmap);
|
||||||
|
// Get the app info for the source activity.
|
||||||
|
AppInfo appInfo = new AppInfo();
|
||||||
|
appInfo.user = user;
|
||||||
|
appInfo.componentName = shortcutInfo.getActivity();
|
||||||
|
try {
|
||||||
|
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 Utilities.badgeWithBitmap(unbadgedBitmap, appInfo.iconBitmap, context);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Returns the ShortcutInfo id associated with the deep shortcut. */
|
/** Returns the ShortcutInfo id associated with the deep shortcut. */
|
||||||
|
|||||||
@@ -248,9 +248,16 @@ public final class Utilities {
|
|||||||
float scale = FeatureFlags.LAUNCHER3_DISABLE_ICON_NORMALIZATION ?
|
float scale = FeatureFlags.LAUNCHER3_DISABLE_ICON_NORMALIZATION ?
|
||||||
1 : IconNormalizer.getInstance().getScale(icon, null);
|
1 : IconNormalizer.getInstance().getScale(icon, null);
|
||||||
Bitmap bitmap = createIconBitmap(icon, context, scale);
|
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
|
if (Utilities.ATLEAST_LOLLIPOP && user != null
|
||||||
&& !UserHandleCompat.myUserHandle().equals(user)) {
|
&& !UserHandleCompat.myUserHandle().equals(user)) {
|
||||||
BitmapDrawable drawable = new FixedSizeBitmapDrawable(bitmap);
|
BitmapDrawable drawable = new FixedSizeBitmapDrawable(icon);
|
||||||
Drawable badged = context.getPackageManager().getUserBadgedIcon(
|
Drawable badged = context.getPackageManager().getUserBadgedIcon(
|
||||||
drawable, user.getUser());
|
drawable, user.getUser());
|
||||||
if (badged instanceof BitmapDrawable) {
|
if (badged instanceof BitmapDrawable) {
|
||||||
@@ -259,7 +266,7 @@ public final class Utilities {
|
|||||||
return createIconBitmap(badged, context);
|
return createIconBitmap(badged, context);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
return bitmap;
|
return icon;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -276,26 +283,28 @@ public final class Utilities {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Same as {@link #createBadgedIconBitmap} but adds a shadow before badging the icon
|
* 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)
|
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
|
||||||
public static Bitmap createBadgedIconBitmapWithShadow(
|
public static Bitmap badgeWithBitmap(Bitmap srcTgt, Bitmap badge, Context context) {
|
||||||
Drawable icon, UserHandleCompat user, Context context) {
|
int badgeSize = context.getResources().getDimensionPixelSize(R.dimen.profile_badge_size);
|
||||||
Bitmap bitmap = ShadowGenerator.getInstance().recreateIcon(
|
synchronized (sCanvas) {
|
||||||
createScaledBitmapWithoutShadow(icon, context));
|
sCanvas.setBitmap(srcTgt);
|
||||||
if (Utilities.ATLEAST_LOLLIPOP && user != null
|
sCanvas.drawBitmap(badge, new Rect(0, 0, badge.getWidth(), badge.getHeight()),
|
||||||
&& !UserHandleCompat.myUserHandle().equals(user)) {
|
new Rect(srcTgt.getWidth() - badgeSize,
|
||||||
BitmapDrawable drawable = new FixedSizeBitmapDrawable(bitmap);
|
srcTgt.getHeight() - badgeSize, srcTgt.getWidth(), srcTgt.getHeight()),
|
||||||
Drawable badged = context.getPackageManager().getUserBadgedIcon(
|
new Paint(Paint.FILTER_BITMAP_FLAG));
|
||||||
drawable, user.getUser());
|
sCanvas.setBitmap(null);
|
||||||
if (badged instanceof BitmapDrawable) {
|
|
||||||
return ((BitmapDrawable) badged).getBitmap();
|
|
||||||
} else {
|
|
||||||
return createIconBitmap(badged, context);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
return bitmap;
|
|
||||||
}
|
}
|
||||||
|
return srcTgt;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -0,0 +1,83 @@
|
|||||||
|
/*
|
||||||
|
* 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.compat;
|
||||||
|
|
||||||
|
import android.content.ComponentName;
|
||||||
|
import android.content.Context;
|
||||||
|
import android.content.Intent;
|
||||||
|
import android.content.pm.ApplicationInfo;
|
||||||
|
import android.graphics.drawable.Drawable;
|
||||||
|
import android.util.Log;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@link LauncherActivityInfoCompat} which loads its data only when needed.
|
||||||
|
*/
|
||||||
|
public class DeferredLauncherActivityInfo extends LauncherActivityInfoCompat {
|
||||||
|
|
||||||
|
private final ComponentName mComponent;
|
||||||
|
private final UserHandleCompat mUser;
|
||||||
|
private final Context mContext;
|
||||||
|
|
||||||
|
private LauncherActivityInfoCompat mActualInfo;
|
||||||
|
|
||||||
|
public DeferredLauncherActivityInfo(
|
||||||
|
ComponentName component, UserHandleCompat user, Context context) {
|
||||||
|
mComponent = component;
|
||||||
|
mUser = user;
|
||||||
|
mContext = context;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ComponentName getComponentName() {
|
||||||
|
return mComponent;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public UserHandleCompat getUser() {
|
||||||
|
return mUser;
|
||||||
|
}
|
||||||
|
|
||||||
|
private synchronized LauncherActivityInfoCompat getActualInfo() {
|
||||||
|
if (mActualInfo == null) {
|
||||||
|
Intent intent = new Intent(Intent.ACTION_MAIN)
|
||||||
|
.addCategory(Intent.CATEGORY_LAUNCHER)
|
||||||
|
.setComponent(mComponent);
|
||||||
|
mActualInfo = LauncherAppsCompat.getInstance(mContext).resolveActivity(intent, mUser);
|
||||||
|
}
|
||||||
|
return mActualInfo;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public CharSequence getLabel() {
|
||||||
|
return getActualInfo().getLabel();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Drawable getIcon(int density) {
|
||||||
|
return getActualInfo().getIcon(density);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ApplicationInfo getApplicationInfo() {
|
||||||
|
return getActualInfo().getApplicationInfo();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public long getFirstInstallTime() {
|
||||||
|
return getActualInfo().getFirstInstallTime();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -46,6 +46,7 @@ import android.widget.LinearLayout;
|
|||||||
import com.android.launcher3.BubbleTextView;
|
import com.android.launcher3.BubbleTextView;
|
||||||
import com.android.launcher3.DragSource;
|
import com.android.launcher3.DragSource;
|
||||||
import com.android.launcher3.DropTarget;
|
import com.android.launcher3.DropTarget;
|
||||||
|
import com.android.launcher3.IconCache;
|
||||||
import com.android.launcher3.ItemInfo;
|
import com.android.launcher3.ItemInfo;
|
||||||
import com.android.launcher3.Launcher;
|
import com.android.launcher3.Launcher;
|
||||||
import com.android.launcher3.LauncherAnimUtils;
|
import com.android.launcher3.LauncherAnimUtils;
|
||||||
@@ -761,8 +762,9 @@ public class DeepShortcutsContainer extends LinearLayout implements View.OnLongC
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected Bitmap getBadgedIcon(Drawable unbadgedIcon, Context context) {
|
protected Bitmap getBadgedIcon(Bitmap unbadgedBitmap, ShortcutInfoCompat shortcutInfo,
|
||||||
return Utilities.createScaledBitmapWithoutShadow(unbadgedIcon, context);
|
IconCache cache, Context context) {
|
||||||
|
return unbadgedBitmap;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,6 +24,8 @@ import android.content.pm.ShortcutInfo;
|
|||||||
import android.os.Build;
|
import android.os.Build;
|
||||||
|
|
||||||
import com.android.launcher3.ItemInfo;
|
import com.android.launcher3.ItemInfo;
|
||||||
|
import com.android.launcher3.compat.DeferredLauncherActivityInfo;
|
||||||
|
import com.android.launcher3.compat.LauncherActivityInfoCompat;
|
||||||
import com.android.launcher3.compat.UserHandleCompat;
|
import com.android.launcher3.compat.UserHandleCompat;
|
||||||
import com.android.launcher3.compat.UserManagerCompat;
|
import com.android.launcher3.compat.UserManagerCompat;
|
||||||
import com.android.launcher3.util.ComponentKey;
|
import com.android.launcher3.util.ComponentKey;
|
||||||
@@ -109,4 +111,8 @@ public class ShortcutInfoCompat {
|
|||||||
public String toString() {
|
public String toString() {
|
||||||
return mShortcutInfo.toString();
|
return mShortcutInfo.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public LauncherActivityInfoCompat getActivityInfo(Context context) {
|
||||||
|
return new DeferredLauncherActivityInfo(getActivity(), getUserHandle(), context);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user