From 239d6e9b8dc6c8e3df9f9ea36fc06353e9f43e42 Mon Sep 17 00:00:00 2001 From: Sunny Goyal Date: Tue, 7 Feb 2023 13:40:11 -0800 Subject: [PATCH] Adding support for customizing shortcut pinning logic > Allowing support for overring shortcut badge > Updating pendingShortcutInfo so that the handler can be easily overridden Bug: 268253894 Test: Verified on device Change-Id: I3184cb6494bc98c7eed33798b35b4e6e6a1e7ddc --- quickstep/res/drawable/close_icon.xml | 10 --------- src/com/android/launcher3/Launcher.java | 2 +- .../android/launcher3/PendingAddItemInfo.java | 18 +++++++++++++--- src/com/android/launcher3/Utilities.java | 12 +++++------ src/com/android/launcher3/Workspace.java | 2 +- .../android/launcher3/dragndrop/DragView.java | 7 ------- .../PinShortcutRequestActivityInfo.java | 2 +- src/com/android/launcher3/folder/Folder.java | 2 +- .../android/launcher3/icons/IconCache.java | 17 +++++++++++++-- .../launcher3/pm/InstallSessionHelper.java | 13 +++++++++--- .../pm/ShortcutConfigActivityInfo.java | 21 ------------------- .../widget/PendingAddShortcutInfo.java | 21 +++++++++++++++++-- .../widget/PendingItemDragHelper.java | 3 ++- 13 files changed, 71 insertions(+), 59 deletions(-) delete mode 100644 quickstep/res/drawable/close_icon.xml diff --git a/quickstep/res/drawable/close_icon.xml b/quickstep/res/drawable/close_icon.xml deleted file mode 100644 index 07f4336882..0000000000 --- a/quickstep/res/drawable/close_icon.xml +++ /dev/null @@ -1,10 +0,0 @@ - - - diff --git a/src/com/android/launcher3/Launcher.java b/src/com/android/launcher3/Launcher.java index 8097fd7cef..8e5310172c 100644 --- a/src/com/android/launcher3/Launcher.java +++ b/src/com/android/launcher3/Launcher.java @@ -1947,7 +1947,7 @@ public class Launcher extends StatefulActivity Intent intent = new Intent(Intent.ACTION_CREATE_SHORTCUT).setComponent(info.componentName); setWaitingForResult(PendingRequestArgs.forIntent(REQUEST_CREATE_SHORTCUT, intent, info)); TestLogging.recordEvent(TestProtocol.SEQUENCE_MAIN, "start: processShortcutFromDrop"); - if (!info.activityInfo.startConfigActivity(this, REQUEST_CREATE_SHORTCUT)) { + if (!info.getActivityInfo(this).startConfigActivity(this, REQUEST_CREATE_SHORTCUT)) { handleActivityResult(REQUEST_CREATE_SHORTCUT, RESULT_CANCELED, null); } } diff --git a/src/com/android/launcher3/PendingAddItemInfo.java b/src/com/android/launcher3/PendingAddItemInfo.java index b7a22fc9b9..000ddd8f79 100644 --- a/src/com/android/launcher3/PendingAddItemInfo.java +++ b/src/com/android/launcher3/PendingAddItemInfo.java @@ -21,7 +21,7 @@ import android.content.ComponentName; import androidx.annotation.NonNull; import androidx.annotation.Nullable; -import com.android.launcher3.model.data.ItemInfo; +import com.android.launcher3.model.data.ItemInfoWithIcon; import java.util.Optional; @@ -29,13 +29,20 @@ import java.util.Optional; * Meta data that is used for deferred binding. e.g., this object is used to pass information on * draggable targets when they are dropped onto the workspace from another container. */ -public class PendingAddItemInfo extends ItemInfo { +public class PendingAddItemInfo extends ItemInfoWithIcon { /** * The component that will be created. */ public ComponentName componentName; + public PendingAddItemInfo() { } + + public PendingAddItemInfo(PendingAddItemInfo info) { + super(info); + componentName = info.componentName; + } + @Override protected String dumpProperties() { return super.dumpProperties() + " componentName=" + componentName; @@ -46,13 +53,18 @@ public class PendingAddItemInfo extends ItemInfo { */ @NonNull @Override - public ItemInfo makeShallowCopy() { + public PendingAddItemInfo makeShallowCopy() { PendingAddItemInfo itemInfo = new PendingAddItemInfo(); itemInfo.copyFrom(this); itemInfo.componentName = this.componentName; return itemInfo; } + @Override + public PendingAddItemInfo clone() { + return makeShallowCopy(); + } + @Nullable @Override public ComponentName getTargetComponent() { diff --git a/src/com/android/launcher3/Utilities.java b/src/com/android/launcher3/Utilities.java index bd9493b271..b32ff3c09d 100644 --- a/src/com/android/launcher3/Utilities.java +++ b/src/com/android/launcher3/Utilities.java @@ -557,6 +557,12 @@ public final class Utilities { int width, int height, Object[] outObj) { ActivityContext activity = ActivityContext.lookupContext(context); LauncherAppState appState = LauncherAppState.getInstance(context); + if (info instanceof PendingAddShortcutInfo) { + ShortcutConfigActivityInfo activityInfo = + ((PendingAddShortcutInfo) info).getActivityInfo(context); + outObj[0] = activityInfo; + return activityInfo.getFullResIcon(appState.getIconCache()); + } if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) { LauncherActivityInfo activityInfo = context.getSystemService(LauncherApps.class) .resolveActivity(info.getIntent(), info.user); @@ -565,12 +571,6 @@ public final class Utilities { .getIconProvider().getIcon( activityInfo, activity.getDeviceProfile().inv.fillResIconDpi); } else if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) { - if (info instanceof PendingAddShortcutInfo) { - ShortcutConfigActivityInfo activityInfo = - ((PendingAddShortcutInfo) info).activityInfo; - outObj[0] = activityInfo; - return activityInfo.getFullResIcon(appState.getIconCache()); - } List si = ShortcutKey.fromItemInfo(info) .buildRequest(context) .query(ShortcutRequest.ALL); diff --git a/src/com/android/launcher3/Workspace.java b/src/com/android/launcher3/Workspace.java index cfb8ca4df5..ba492d57e2 100644 --- a/src/com/android/launcher3/Workspace.java +++ b/src/com/android/launcher3/Workspace.java @@ -2690,7 +2690,7 @@ public class Workspace extends PagedView private void onDropExternal(final int[] touchXY, final CellLayout cellLayout, DragObject d) { if (d.dragInfo instanceof PendingAddShortcutInfo) { WorkspaceItemInfo si = ((PendingAddShortcutInfo) d.dragInfo) - .activityInfo.createWorkspaceItemInfo(); + .getActivityInfo(mLauncher).createWorkspaceItemInfo(); if (si != null) { d.dragInfo = si; } diff --git a/src/com/android/launcher3/dragndrop/DragView.java b/src/com/android/launcher3/dragndrop/DragView.java index f54d05dbdc..e10fdf53d8 100644 --- a/src/com/android/launcher3/dragndrop/DragView.java +++ b/src/com/android/launcher3/dragndrop/DragView.java @@ -56,7 +56,6 @@ import androidx.dynamicanimation.animation.FloatPropertyCompat; import androidx.dynamicanimation.animation.SpringAnimation; import androidx.dynamicanimation.animation.SpringForce; -import com.android.launcher3.LauncherSettings; import com.android.launcher3.R; import com.android.launcher3.Utilities; import com.android.launcher3.anim.Interpolators; @@ -218,12 +217,6 @@ public abstract class DragView extends Fram */ @TargetApi(Build.VERSION_CODES.O) public void setItemInfo(final ItemInfo info) { - if (info.itemType != LauncherSettings.Favorites.ITEM_TYPE_APPLICATION - && info.itemType != LauncherSettings.Favorites.ITEM_TYPE_SEARCH_ACTION - && info.itemType != LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT - && info.itemType != LauncherSettings.Favorites.ITEM_TYPE_FOLDER) { - return; - } // Load the adaptive icon on a background thread and add the view in ui thread. MODEL_EXECUTOR.getHandler().postAtFrontOfQueue(() -> { Object[] outObj = new Object[1]; diff --git a/src/com/android/launcher3/dragndrop/PinShortcutRequestActivityInfo.java b/src/com/android/launcher3/dragndrop/PinShortcutRequestActivityInfo.java index f9916d0b3b..6b215229fd 100644 --- a/src/com/android/launcher3/dragndrop/PinShortcutRequestActivityInfo.java +++ b/src/com/android/launcher3/dragndrop/PinShortcutRequestActivityInfo.java @@ -44,7 +44,7 @@ import com.android.launcher3.pm.ShortcutConfigActivityInfo; * request. */ @TargetApi(Build.VERSION_CODES.O) -class PinShortcutRequestActivityInfo extends ShortcutConfigActivityInfo { +public class PinShortcutRequestActivityInfo extends ShortcutConfigActivityInfo { // Class name used in the target component, such that it will never represent an // actual existing class. diff --git a/src/com/android/launcher3/folder/Folder.java b/src/com/android/launcher3/folder/Folder.java index 9a5d77e3a2..c9fe745ea1 100644 --- a/src/com/android/launcher3/folder/Folder.java +++ b/src/com/android/launcher3/folder/Folder.java @@ -1263,7 +1263,7 @@ public class Folder extends AbstractFloatingView implements ClipPathView, DragSo PendingAddShortcutInfo pasi = d.dragInfo instanceof PendingAddShortcutInfo ? (PendingAddShortcutInfo) d.dragInfo : null; WorkspaceItemInfo pasiSi = - pasi != null ? pasi.activityInfo.createWorkspaceItemInfo() : null; + pasi != null ? pasi.getActivityInfo(launcher).createWorkspaceItemInfo() : null; if (pasi != null && pasiSi == null) { // There is no WorkspaceItemInfo, so we have to go through a configuration activity. pasi.container = mInfo.id; diff --git a/src/com/android/launcher3/icons/IconCache.java b/src/com/android/launcher3/icons/IconCache.java index 0b4a4a53d4..c8ccdcafab 100644 --- a/src/com/android/launcher3/icons/IconCache.java +++ b/src/com/android/launcher3/icons/IconCache.java @@ -60,6 +60,7 @@ import com.android.launcher3.model.data.IconRequestInfo; import com.android.launcher3.model.data.ItemInfoWithIcon; import com.android.launcher3.model.data.PackageItemInfo; import com.android.launcher3.model.data.WorkspaceItemInfo; +import com.android.launcher3.pm.InstallSessionHelper; import com.android.launcher3.pm.UserCache; import com.android.launcher3.shortcuts.ShortcutKey; import com.android.launcher3.util.InstantAppResolver; @@ -81,6 +82,11 @@ import java.util.stream.Stream; */ public class IconCache extends BaseIconCache { + // Shortcut extra which can point to a packageName and can be used to indicate an alternate + // badge info. Launcher only reads this if the shortcut comes from a system app. + public static final String EXTRA_SHORTCUT_BADGE_OVERRIDE_PACKAGE = + "extra_shortcut_badge_override_package"; + private static final String TAG = "Launcher.IconCache"; private final Predicate mIsUsingFallbackOrNonDefaultIconCheck = w -> @@ -260,8 +266,15 @@ public class IconCache extends BaseIconCache { getTitleAndIcon(appInfo, false); return appInfo.bitmap; } else { - PackageItemInfo pkgInfo = new PackageItemInfo(shortcutInfo.getPackage(), - shortcutInfo.getUserHandle()); + String pkg = shortcutInfo.getPackage(); + String override = shortcutInfo.getExtras() == null ? null + : shortcutInfo.getExtras().getString(EXTRA_SHORTCUT_BADGE_OVERRIDE_PACKAGE); + if (!TextUtils.isEmpty(override) + && InstallSessionHelper.INSTANCE.get(mContext) + .isTrustedPackage(pkg, shortcutInfo.getUserHandle())) { + pkg = override; + } + PackageItemInfo pkgInfo = new PackageItemInfo(pkg, shortcutInfo.getUserHandle()); getTitleAndIconForApp(pkgInfo, false); return pkgInfo.bitmap; } diff --git a/src/com/android/launcher3/pm/InstallSessionHelper.java b/src/com/android/launcher3/pm/InstallSessionHelper.java index db235668db..7ca3b11001 100644 --- a/src/com/android/launcher3/pm/InstallSessionHelper.java +++ b/src/com/android/launcher3/pm/InstallSessionHelper.java @@ -171,15 +171,22 @@ public class InstallSessionHelper { } return null; } - String pkg = sessionInfo.getInstallerPackageName(); + return isTrustedPackage(sessionInfo.getInstallerPackageName(), getUserHandle(sessionInfo)) + ? sessionInfo : null; + } + + /** + * Returns true if the provided packageName can be trusted for user configurations + */ + public boolean isTrustedPackage(String pkg, UserHandle user) { synchronized (mSessionVerifiedMap) { if (!mSessionVerifiedMap.containsKey(pkg)) { boolean hasSystemFlag = new PackageManagerHelper(mAppContext).getApplicationInfo( - pkg, getUserHandle(sessionInfo), ApplicationInfo.FLAG_SYSTEM) != null; + pkg, user, ApplicationInfo.FLAG_SYSTEM) != null; mSessionVerifiedMap.put(pkg, DEBUG || hasSystemFlag); } } - return mSessionVerifiedMap.get(pkg) ? sessionInfo : null; + return mSessionVerifiedMap.get(pkg); } @NonNull diff --git a/src/com/android/launcher3/pm/ShortcutConfigActivityInfo.java b/src/com/android/launcher3/pm/ShortcutConfigActivityInfo.java index 7af14c64ab..14e67b2fff 100644 --- a/src/com/android/launcher3/pm/ShortcutConfigActivityInfo.java +++ b/src/com/android/launcher3/pm/ShortcutConfigActivityInfo.java @@ -23,7 +23,6 @@ import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.IntentSender; -import android.content.pm.ActivityInfo; import android.content.pm.LauncherActivityInfo; import android.content.pm.LauncherApps; import android.content.pm.PackageManager; @@ -112,26 +111,6 @@ public abstract class ShortcutConfigActivityInfo implements ComponentWithLabelAn return true; } - static class ShortcutConfigActivityInfoVL extends ShortcutConfigActivityInfo { - - private final ActivityInfo mInfo; - - ShortcutConfigActivityInfoVL(ActivityInfo info) { - super(new ComponentName(info.packageName, info.name), Process.myUserHandle()); - mInfo = info; - } - - @Override - public CharSequence getLabel(PackageManager pm) { - return mInfo.loadLabel(pm); - } - - @Override - public Drawable getFullResIcon(IconCache cache) { - return cache.getFullResIcon(mInfo); - } - } - @TargetApi(26) public static class ShortcutConfigActivityInfoVO extends ShortcutConfigActivityInfo { diff --git a/src/com/android/launcher3/widget/PendingAddShortcutInfo.java b/src/com/android/launcher3/widget/PendingAddShortcutInfo.java index 96016526a7..3935be5e15 100644 --- a/src/com/android/launcher3/widget/PendingAddShortcutInfo.java +++ b/src/com/android/launcher3/widget/PendingAddShortcutInfo.java @@ -17,6 +17,8 @@ package com.android.launcher3.widget; import static com.android.launcher3.LauncherSettings.Favorites.CONTAINER_WIDGETS_TRAY; +import android.content.Context; + import com.android.launcher3.PendingAddItemInfo; import com.android.launcher3.pm.ShortcutConfigActivityInfo; @@ -27,13 +29,28 @@ import com.android.launcher3.pm.ShortcutConfigActivityInfo; */ public class PendingAddShortcutInfo extends PendingAddItemInfo { - public ShortcutConfigActivityInfo activityInfo; + // TODO: Make it @NonNull + protected ShortcutConfigActivityInfo mActivityInfo; public PendingAddShortcutInfo(ShortcutConfigActivityInfo activityInfo) { - this.activityInfo = activityInfo; + this.mActivityInfo = activityInfo; componentName = activityInfo.getComponent(); user = activityInfo.getUser(); itemType = activityInfo.getItemType(); this.container = CONTAINER_WIDGETS_TRAY; } + + public PendingAddShortcutInfo(PendingAddShortcutInfo info) { + super(info); + mActivityInfo = info.mActivityInfo; + } + + public PendingAddShortcutInfo() { } + + /** + * Returns the info used for creating the shortcut + */ + public ShortcutConfigActivityInfo getActivityInfo(Context context) { + return mActivityInfo; + } } diff --git a/src/com/android/launcher3/widget/PendingItemDragHelper.java b/src/com/android/launcher3/widget/PendingItemDragHelper.java index bbbc329c9c..2dedd12450 100644 --- a/src/com/android/launcher3/widget/PendingItemDragHelper.java +++ b/src/com/android/launcher3/widget/PendingItemDragHelper.java @@ -180,7 +180,8 @@ public class PendingItemDragHelper extends DragPreviewProvider { draggableView = DraggableView.ofType(DraggableView.DRAGGABLE_WIDGET); } else { PendingAddShortcutInfo createShortcutInfo = (PendingAddShortcutInfo) mAddInfo; - Drawable icon = createShortcutInfo.activityInfo.getFullResIcon(app.getIconCache()); + Drawable icon = createShortcutInfo.getActivityInfo(launcher) + .getFullResIcon(app.getIconCache()); LauncherIcons li = LauncherIcons.obtain(launcher); preview = new FastBitmapDrawable( li.createScaledBitmap(icon, BaseIconFactory.MODE_DEFAULT));