fix: Reimplement legacy shortcut path (#7027)
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> Signed-off-by: Pun Butrach <pun.butrach@gmail.com>
This commit is contained in:
1 parent
16d35d03ba
commit
5cb2e99626
8 files changed
+196
-6
No files matched your search
@@ -128,6 +128,7 @@ import android.os.Build.VERSION;
|
||||
import android.os.Build.VERSION_CODES;
|
||||
import android.os.Bundle;
|
||||
import android.os.Parcelable;
|
||||
import android.os.Process;
|
||||
import android.os.SystemClock;
|
||||
import android.os.Trace;
|
||||
import android.os.UserHandle;
|
||||
@@ -201,6 +202,7 @@ import com.android.launcher3.logging.StartupLatencyLogger;
|
||||
import com.android.launcher3.logging.StatsLogManager;
|
||||
import com.android.launcher3.model.BgDataModel.Callbacks;
|
||||
import com.android.launcher3.model.ItemInstallQueue;
|
||||
import com.android.launcher3.model.ModelUtils;
|
||||
import com.android.launcher3.model.ModelWriter;
|
||||
import com.android.launcher3.model.StringCache;
|
||||
import com.android.launcher3.model.data.AppInfo;
|
||||
@@ -235,6 +237,7 @@ import com.android.launcher3.util.ItemInflater;
|
||||
import com.android.launcher3.util.KeyboardShortcutsDelegate;
|
||||
import com.android.launcher3.util.LauncherBindableItemsContainer;
|
||||
import com.android.launcher3.util.OnboardingPrefs;
|
||||
import com.android.launcher3.util.PackageManagerHelper;
|
||||
import com.android.launcher3.util.PackageUserKey;
|
||||
import com.android.launcher3.util.PendingRequestArgs;
|
||||
import com.android.launcher3.util.PluginManagerWrapper;
|
||||
@@ -1375,7 +1378,9 @@ public class Launcher extends StatefulActivity<LauncherState>
|
||||
*/
|
||||
protected void completeAddShortcut(Intent data, int container, int screenId, int cellX,
|
||||
int cellY, PendingRequestArgs args) {
|
||||
if (args.getRequestCode() != REQUEST_CREATE_SHORTCUT) {
|
||||
if (args.getRequestCode() != REQUEST_CREATE_SHORTCUT
|
||||
|| args.getPendingIntent() == null
|
||||
|| args.getPendingIntent().getComponent() == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -1385,8 +1390,20 @@ public class Launcher extends StatefulActivity<LauncherState>
|
||||
WorkspaceItemInfo info = PinRequestHelper.createWorkspaceItemFromPinItemRequest(
|
||||
this, PinRequestHelper.getPinItemRequest(data), 0);
|
||||
if (info == null) {
|
||||
Log.e(TAG, "Unable to parse a valid shortcut result");
|
||||
return;
|
||||
// Legacy shortcuts are only supported for the primary profile.
|
||||
info = Process.myUserHandle().equals(args.user)
|
||||
? ModelUtils.fromLegacyShortcutIntent(this, data) : null;
|
||||
|
||||
if (info == null) {
|
||||
Log.e(TAG, "Unable to parse a valid shortcut result");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!new PackageManagerHelper(this).hasPermissionForActivity(
|
||||
info.intent, args.getPendingIntent().getComponent().getPackageName())) {
|
||||
Log.e(TAG, "Ignoring shortcut intent failing activity permission validation");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (container < 0) {
|
||||
|
||||
@@ -99,6 +99,8 @@ public class LauncherSettings {
|
||||
/**
|
||||
* The gesture is an application created shortcut
|
||||
* @deprecated This is no longer supported. Use {@link #ITEM_TYPE_DEEP_SHORTCUT} instead
|
||||
*
|
||||
* LC-Note: We still use this to support legacy shortcut for non-root.
|
||||
*/
|
||||
@Deprecated
|
||||
public static final int ITEM_TYPE_SHORTCUT = 1;
|
||||
@@ -212,6 +214,7 @@ public class LauncherSettings {
|
||||
public static final String itemTypeToString(int type) {
|
||||
switch(type) {
|
||||
case ITEM_TYPE_APPLICATION: return "APP";
|
||||
case ITEM_TYPE_SHORTCUT: return "SHORTCUT";
|
||||
case ITEM_TYPE_FOLDER: return "FOLDER";
|
||||
case ITEM_TYPE_APPWIDGET: return "WIDGET";
|
||||
case ITEM_TYPE_CUSTOM_APPWIDGET: return "CUSTOMWIDGET";
|
||||
|
||||
@@ -245,7 +245,8 @@ public class LoaderCursor extends CursorWrapper {
|
||||
|
||||
public IconRequestInfo<WorkspaceItemInfo> createIconRequestInfo(
|
||||
WorkspaceItemInfo wai, boolean useLowResIcon) {
|
||||
byte[] iconBlob = itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT || restoreFlag != 0
|
||||
byte[] iconBlob = itemType == Favorites.ITEM_TYPE_SHORTCUT
|
||||
|| itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT || restoreFlag != 0
|
||||
|| (wai.isInactiveArchive() && Flags.restoreArchivedAppIconsFromDb())
|
||||
? getIconBlob() : null;
|
||||
return new IconRequestInfo<>(wai, mActivityInfo, iconBlob,
|
||||
@@ -582,7 +583,8 @@ public class LoaderCursor extends CursorWrapper {
|
||||
loadedItems.put(info.id, info);
|
||||
if ((info.itemType == ITEM_TYPE_APP_PAIR
|
||||
|| info.itemType == ITEM_TYPE_DEEP_SHORTCUT
|
||||
|| info.itemType == ITEM_TYPE_APPLICATION)
|
||||
|| info.itemType == ITEM_TYPE_APPLICATION
|
||||
|| info.itemType == Favorites.ITEM_TYPE_SHORTCUT)
|
||||
&& info.container != CONTAINER_DESKTOP
|
||||
&& info.container != CONTAINER_HOTSEAT) {
|
||||
findOrMakeFolder(info.container, loadedItems).add(info);
|
||||
|
||||
@@ -20,7 +20,19 @@ import static com.android.launcher3.LauncherSettings.Favorites.CONTAINER_HOTSEAT
|
||||
import static com.android.launcher3.LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET;
|
||||
import static com.android.launcher3.LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.graphics.Bitmap;
|
||||
import android.os.BadParcelableException;
|
||||
import android.os.Process;
|
||||
import android.util.Log;
|
||||
|
||||
import com.android.launcher3.LauncherSettings;
|
||||
import com.android.launcher3.Utilities;
|
||||
import com.android.launcher3.icons.BitmapInfo;
|
||||
import com.android.launcher3.icons.LauncherIcons;
|
||||
import com.android.launcher3.model.data.ItemInfo;
|
||||
import com.android.launcher3.model.data.WorkspaceItemInfo;
|
||||
import com.android.launcher3.util.IntSet;
|
||||
|
||||
import java.util.function.Predicate;
|
||||
@@ -30,6 +42,8 @@ import java.util.function.Predicate;
|
||||
*/
|
||||
public class ModelUtils {
|
||||
|
||||
private static final String TAG = "ModelUtils";
|
||||
|
||||
/**
|
||||
* Returns a filter for items on hotseat or current screens
|
||||
*/
|
||||
@@ -44,4 +58,70 @@ public class ModelUtils {
|
||||
*/
|
||||
public static final Predicate<ItemInfo> WIDGET_FILTER = item ->
|
||||
item.itemType == ITEM_TYPE_APPWIDGET || item.itemType == ITEM_TYPE_CUSTOM_APPWIDGET;
|
||||
|
||||
/**
|
||||
* Creates a workspace item from the result of an ACTION_CREATE_SHORTCUT activity.
|
||||
* <p>
|
||||
* LC-Note: We still use this to support legacy shortcut for non-root.
|
||||
*/
|
||||
@SuppressWarnings("deprecation")
|
||||
public static WorkspaceItemInfo fromLegacyShortcutIntent(Context context, Intent data) {
|
||||
if (data == null) {
|
||||
return null;
|
||||
}
|
||||
Object shortcutIntent;
|
||||
Object iconResource;
|
||||
Object icon;
|
||||
String label;
|
||||
try {
|
||||
shortcutIntent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
|
||||
iconResource = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
|
||||
icon = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
|
||||
label = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
|
||||
} catch (BadParcelableException | ClassCastException e) {
|
||||
Log.e(TAG, "Unable to read legacy shortcut intent", e);
|
||||
return null;
|
||||
}
|
||||
|
||||
if ((shortcutIntent != null && !(shortcutIntent instanceof Intent))
|
||||
|| (iconResource != null
|
||||
&& !(iconResource instanceof Intent.ShortcutIconResource))
|
||||
|| (icon != null && !(icon instanceof Bitmap))) {
|
||||
Log.e(TAG, "Invalid install shortcut intent");
|
||||
return null;
|
||||
}
|
||||
|
||||
Intent launchIntent = (Intent) shortcutIntent;
|
||||
if (launchIntent == null || label == null) {
|
||||
Log.e(TAG, "Invalid install shortcut intent");
|
||||
return null;
|
||||
}
|
||||
|
||||
BitmapInfo iconInfo = null;
|
||||
try (LauncherIcons li = LauncherIcons.obtain(context)) {
|
||||
Bitmap bitmap = (Bitmap) icon;
|
||||
if (bitmap != null) {
|
||||
iconInfo = li.createIconBitmap(bitmap);
|
||||
} else {
|
||||
Intent.ShortcutIconResource resource =
|
||||
(Intent.ShortcutIconResource) iconResource;
|
||||
if (resource != null) {
|
||||
iconInfo = li.createIconBitmap(resource);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (iconInfo == null) {
|
||||
Log.e(TAG, "Invalid icon by the app");
|
||||
return null;
|
||||
}
|
||||
|
||||
WorkspaceItemInfo info = new WorkspaceItemInfo();
|
||||
info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
|
||||
info.user = Process.myUserHandle();
|
||||
info.bitmap = iconInfo;
|
||||
info.contentDescription = info.title = Utilities.trim(label);
|
||||
info.intent = launchIntent;
|
||||
return info;
|
||||
}
|
||||
}
|
||||
@@ -114,6 +114,7 @@ class WorkspaceItemProcessor(
|
||||
when (c.itemType) {
|
||||
Favorites.ITEM_TYPE_APPLICATION,
|
||||
Favorites.ITEM_TYPE_DEEP_SHORTCUT -> processAppOrDeepShortcut()
|
||||
Favorites.ITEM_TYPE_SHORTCUT -> processLegacyShortcut()
|
||||
Favorites.ITEM_TYPE_FOLDER,
|
||||
Favorites.ITEM_TYPE_APP_PAIR -> processFolderOrAppPair()
|
||||
Favorites.ITEM_TYPE_APPWIDGET,
|
||||
@@ -124,6 +125,55 @@ class WorkspaceItemProcessor(
|
||||
}
|
||||
}
|
||||
|
||||
/** LC-Note: We still use this to support legacy shortcut for non-root. */
|
||||
private fun processLegacyShortcut() {
|
||||
val intent =
|
||||
c.parseIntent()
|
||||
?: run {
|
||||
c.markDeleted(
|
||||
"Null intent from db for legacy shortcut id=${c.id}",
|
||||
RestoreError.APP_NO_DB_INTENT,
|
||||
)
|
||||
return
|
||||
}
|
||||
var disabledState =
|
||||
if (userManagerState.isUserQuiet(c.serialNumber))
|
||||
WorkspaceItemInfo.FLAG_DISABLED_QUIET_USER
|
||||
else 0
|
||||
val appInfoWrapper = ApplicationInfoWrapper(context, intent)
|
||||
if (appInfoWrapper.isSuspended()) {
|
||||
disabledState = disabledState or ItemInfoWithIcon.FLAG_DISABLED_SUSPENDED
|
||||
}
|
||||
|
||||
val info = c.loadSimpleWorkspaceItem()
|
||||
info.options = c.options
|
||||
if (
|
||||
intent.action != null &&
|
||||
intent.categories != null &&
|
||||
intent.action == Intent.ACTION_MAIN &&
|
||||
intent.categories.contains(Intent.CATEGORY_LAUNCHER)
|
||||
) {
|
||||
intent.addFlags(
|
||||
Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
|
||||
)
|
||||
}
|
||||
|
||||
c.markRestored()
|
||||
val useLowResIcon = !c.isOnWorkspaceOrHotseat
|
||||
iconRequestInfos.add(c.createIconRequestInfo(info, useLowResIcon))
|
||||
c.applyCommonProperties(info)
|
||||
info.intent = intent
|
||||
info.rank = c.rank
|
||||
info.spanX = 1
|
||||
info.spanY = 1
|
||||
info.runtimeStatusFlags = info.runtimeStatusFlags or disabledState
|
||||
if (isSafeMode && !appInfoWrapper.isSystem()) {
|
||||
info.runtimeStatusFlags =
|
||||
info.runtimeStatusFlags or ItemInfoWithIcon.FLAG_DISABLED_SAFEMODE
|
||||
}
|
||||
c.checkAndAddItem(info, loadedItems, memoryLogger)
|
||||
}
|
||||
|
||||
/**
|
||||
* This method verifies that an app shortcut should be shown on the home screen, updates the
|
||||
* database accordingly, formats the data in such a way that it is ready to be added to the data
|
||||
|
||||
@@ -145,7 +145,7 @@ public class WorkspaceItemInfo extends ItemInfoWithIcon {
|
||||
.put(Favorites.OPTIONS, options)
|
||||
.put(Favorites.RESTORED, status);
|
||||
|
||||
if (!getMatchingLookupFlag().useLowRes()) {
|
||||
if (itemType == Favorites.ITEM_TYPE_SHORTCUT || !getMatchingLookupFlag().useLowRes()) {
|
||||
writer.putIcon(bitmap, user);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -62,6 +62,8 @@ class ItemInflater<T>(
|
||||
val parent = nullableParent ?: defaultParent
|
||||
when (item.itemType) {
|
||||
Favorites.ITEM_TYPE_APPLICATION,
|
||||
// LC-Note: Legacy ACTION_CREATE_SHORTCUT items use the standard shortcut view.
|
||||
Favorites.ITEM_TYPE_SHORTCUT,
|
||||
Favorites.ITEM_TYPE_DEEP_SHORTCUT,
|
||||
Favorites.ITEM_TYPE_SEARCH_ACTION -> {
|
||||
var info =
|
||||
|
||||
@@ -19,6 +19,7 @@ package com.android.launcher3.util;
|
||||
import static com.android.launcher3.Utilities.ATLEAST_R;
|
||||
import static com.android.launcher3.model.data.ItemInfoWithIcon.FLAG_INSTALL_SESSION_ACTIVE;
|
||||
|
||||
import android.app.AppOpsManager;
|
||||
import android.annotation.SuppressLint;
|
||||
import android.content.ActivityNotFoundException;
|
||||
import android.content.ComponentName;
|
||||
@@ -29,6 +30,7 @@ import android.content.pm.LauncherActivityInfo;
|
||||
import android.content.pm.LauncherApps;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.content.pm.PackageManager.NameNotFoundException;
|
||||
import android.content.pm.ResolveInfo;
|
||||
import android.graphics.Rect;
|
||||
import android.net.Uri;
|
||||
import android.os.Build.VERSION;
|
||||
@@ -220,6 +222,40 @@ public class PackageManagerHelper {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether the source package has the permission required to start the activity from the
|
||||
* supplied intent.
|
||||
*
|
||||
* LC-Note: We still use this to support legacy shortcut for non-root.
|
||||
*/
|
||||
public boolean hasPermissionForActivity(Intent intent, String srcPackage) {
|
||||
ResolveInfo target = mPm.resolveActivity(intent, 0);
|
||||
if (target == null) {
|
||||
return false;
|
||||
}
|
||||
if (TextUtils.isEmpty(target.activityInfo.permission)) {
|
||||
return true;
|
||||
}
|
||||
if (TextUtils.isEmpty(srcPackage)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (mPm.checkPermission(target.activityInfo.permission, srcPackage)
|
||||
!= PackageManager.PERMISSION_GRANTED) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (TextUtils.isEmpty(AppOpsManager.permissionToOp(target.activityInfo.permission))) {
|
||||
return true;
|
||||
}
|
||||
|
||||
try {
|
||||
return mPm.getApplicationInfo(srcPackage, 0).targetSdkVersion >= VERSION_CODES.M;
|
||||
} catch (NameNotFoundException e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/** Returns the incremental download progress for the given shortcut's app. */
|
||||
public static int getLoadingProgress(LauncherActivityInfo info) {
|
||||
if (Utilities.ATLEAST_S) {
|
||||
|
||||
Reference in new issue
Block a user