From f9d9df7d3aada0a368f8ef7090fafc08d329ce77 Mon Sep 17 00:00:00 2001 From: Tony Wickham Date: Wed, 5 Sep 2018 12:48:13 -0700 Subject: [PATCH] Gracefully fallback to new ComponentName if one is renamed/removed Previously, if a developer renamed their launcher activity, we removed all instances of their icon from the home screen, since technically the activity they pointed to no longer exists. However, in the vast majority of cases, the developer simply renamed their activity and nothing should change from a user's perspective. So instead of removing icons that no longer point to a valid activity, we now redirect them to point to the first activity in the manifest (or remove them if there is none). Test: - Install app with Activity A and place on home screen - Rename A to B and reinstall - verify home screen icon remains - Add new launcher activity C - verify icons still go to B - Force stop launcher and rename B to A - verify icons go to A (same activity) - Remove activity A - verify icons go to C - Remove activity C - verify icons are removed Bug: 28907647 Change-Id: If9da251bd588908c4c425e7dd32e38fcbe54bab2 --- src/com/android/launcher3/AllAppsList.java | 2 +- src/com/android/launcher3/AppInfo.java | 1 - .../android/launcher3/model/LoaderTask.java | 26 ++++----- .../launcher3/model/PackageUpdatedTask.java | 53 ++++++++++--------- 4 files changed, 38 insertions(+), 44 deletions(-) diff --git a/src/com/android/launcher3/AllAppsList.java b/src/com/android/launcher3/AllAppsList.java index 45859ca07f..3d60605459 100644 --- a/src/com/android/launcher3/AllAppsList.java +++ b/src/com/android/launcher3/AllAppsList.java @@ -186,7 +186,7 @@ public class AllAppsList { if (user.equals(applicationInfo.user) && packageName.equals(applicationInfo.componentName.getPackageName())) { if (!findActivity(matches, applicationInfo.componentName)) { - Log.w(TAG, "Shortcut will be removed due to app component name change."); + Log.w(TAG, "Changing shortcut target due to app component name change."); removed.add(applicationInfo); data.remove(i); } diff --git a/src/com/android/launcher3/AppInfo.java b/src/com/android/launcher3/AppInfo.java index 4d1bedcf31..ed7991408f 100644 --- a/src/com/android/launcher3/AppInfo.java +++ b/src/com/android/launcher3/AppInfo.java @@ -26,7 +26,6 @@ import android.os.Process; import android.os.UserHandle; import com.android.launcher3.compat.UserManagerCompat; -import com.android.launcher3.config.FeatureFlags; import com.android.launcher3.util.ComponentKey; import com.android.launcher3.util.PackageManagerHelper; diff --git a/src/com/android/launcher3/model/LoaderTask.java b/src/com/android/launcher3/model/LoaderTask.java index 85faf4fbc2..8f8bc09f3c 100644 --- a/src/com/android/launcher3/model/LoaderTask.java +++ b/src/com/android/launcher3/model/LoaderTask.java @@ -381,24 +381,16 @@ public class LoaderTask implements Runnable { // no special handling necessary for this item c.markRestored(); } else { - if (c.hasRestoreFlag(ShortcutInfo.FLAG_AUTOINSTALL_ICON)) { - // We allow auto install apps to have their intent - // updated after an install. - intent = pmHelper.getAppLaunchIntent(targetPkg, c.user); - if (intent != null) { - c.restoreFlag = 0; - c.updater().put( - LauncherSettings.Favorites.INTENT, - intent.toUri(0)).commit(); - cn = intent.getComponent(); - } else { - c.markDeleted("Unable to find a launch target"); - continue; - } + // Gracefully try to find a fallback activity. + intent = pmHelper.getAppLaunchIntent(targetPkg, c.user); + if (intent != null) { + c.restoreFlag = 0; + c.updater().put( + LauncherSettings.Favorites.INTENT, + intent.toUri(0)).commit(); + cn = intent.getComponent(); } else { - // The app is installed but the component is no - // longer available. - c.markDeleted("Invalid component removed: " + cn); + c.markDeleted("Unable to find a launch target"); continue; } } diff --git a/src/com/android/launcher3/model/PackageUpdatedTask.java b/src/com/android/launcher3/model/PackageUpdatedTask.java index ccb1f0959e..1c0732e3f9 100644 --- a/src/com/android/launcher3/model/PackageUpdatedTask.java +++ b/src/com/android/launcher3/model/PackageUpdatedTask.java @@ -20,7 +20,6 @@ import android.content.Context; import android.content.Intent; import android.os.Process; import android.os.UserHandle; -import android.util.ArrayMap; import android.util.Log; import com.android.launcher3.AllAppsList; @@ -159,15 +158,16 @@ public class PackageUpdatedTask extends BaseModelUpdateTask { appsList.added.clear(); addedOrModified.addAll(appsList.modified); appsList.modified.clear(); + if (!addedOrModified.isEmpty()) { + scheduleCallbackTask((callbacks) -> callbacks.bindAppsAddedOrUpdated(addedOrModified)); + } final ArrayList removedApps = new ArrayList<>(appsList.removed); appsList.removed.clear(); - - final ArrayMap addedOrUpdatedApps = new ArrayMap<>(); - if (!addedOrModified.isEmpty()) { - scheduleCallbackTask((callbacks) -> callbacks.bindAppsAddedOrUpdated(addedOrModified)); - for (AppInfo ai : addedOrModified) { - addedOrUpdatedApps.put(ai.componentName, ai); + final HashSet removedComponents = new HashSet<>(); + if (mOp == OP_UPDATE) { + for (AppInfo ai : removedApps) { + removedComponents.add(ai.componentName); } } @@ -201,7 +201,7 @@ public class PackageUpdatedTask extends BaseModelUpdateTask { ComponentName cn = si.getTargetComponent(); if (cn != null && matcher.matches(si, cn)) { - AppInfo appInfo = addedOrUpdatedApps.get(cn); + String packageName = cn.getPackageName(); if (si.hasStatusFlag(ShortcutInfo.FLAG_SUPPORTS_WEB_UI)) { removedShortcuts.put(si.id, false); @@ -231,17 +231,7 @@ public class PackageUpdatedTask extends BaseModelUpdateTask { if (si.hasStatusFlag(ShortcutInfo.FLAG_AUTOINSTALL_ICON)) { // Auto install icon if (!isTargetValid) { - // Try to find the best match activity. - Intent intent = new PackageManagerHelper(context) - .getAppLaunchIntent(cn.getPackageName(), mUser); - if (intent != null) { - cn = intent.getComponent(); - appInfo = addedOrUpdatedApps.get(cn); - } - - if (intent != null && appInfo != null) { - si.intent = intent; - si.status = ShortcutInfo.DEFAULT; + if (updateShortcutIntent(context, si, packageName)) { infoUpdated = true; } else if (si.hasPromiseIconUi()) { removedShortcuts.put(si.id, true); @@ -257,6 +247,10 @@ public class PackageUpdatedTask extends BaseModelUpdateTask { si.status = ShortcutInfo.DEFAULT; infoUpdated = true; } + } else if (isNewApkAvailable && removedComponents.contains(cn)) { + if (updateShortcutIntent(context, si, packageName)) { + infoUpdated = true; + } } if (isNewApkAvailable && @@ -315,7 +309,6 @@ public class PackageUpdatedTask extends BaseModelUpdateTask { } final HashSet removedPackages = new HashSet<>(); - final HashSet removedComponents = new HashSet<>(); if (mOp == OP_REMOVE) { // Mark all packages in the broadcast to be removed Collections.addAll(removedPackages, packages); @@ -330,11 +323,6 @@ public class PackageUpdatedTask extends BaseModelUpdateTask { removedPackages.add(packages[i]); } } - - // Update removedComponents as some components can get removed during package update - for (AppInfo info : removedApps) { - removedComponents.add(info.componentName); - } } if (!removedPackages.isEmpty() || !removedComponents.isEmpty()) { @@ -366,4 +354,19 @@ public class PackageUpdatedTask extends BaseModelUpdateTask { bindUpdatedWidgets(dataModel); } } + + /** + * Updates {@param si}'s intent to point to a new ComponentName. + * @return Whether the shortcut intent was changed. + */ + private boolean updateShortcutIntent(Context context, ShortcutInfo si, String packageName) { + // Try to find the best match activity. + Intent intent = new PackageManagerHelper(context).getAppLaunchIntent(packageName, mUser); + if (intent != null) { + si.intent = intent; + si.status = ShortcutInfo.DEFAULT; + return true; + } + return false; + } }