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
diff --git a/src/com/android/launcher3/AllAppsList.java b/src/com/android/launcher3/AllAppsList.java
index 45859ca..3d60605 100644
--- a/src/com/android/launcher3/AllAppsList.java
+++ b/src/com/android/launcher3/AllAppsList.java
@@ -186,7 +186,7 @@
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 4d1bedc..ed79914 100644
--- a/src/com/android/launcher3/AppInfo.java
+++ b/src/com/android/launcher3/AppInfo.java
@@ -26,7 +26,6 @@
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 85faf4f..8f8bc09 100644
--- a/src/com/android/launcher3/model/LoaderTask.java
+++ b/src/com/android/launcher3/model/LoaderTask.java
@@ -381,24 +381,16 @@
// 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 ccb1f09..1c0732e 100644
--- a/src/com/android/launcher3/model/PackageUpdatedTask.java
+++ b/src/com/android/launcher3/model/PackageUpdatedTask.java
@@ -20,7 +20,6 @@
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 @@
appsList.added.clear();
addedOrModified.addAll(appsList.modified);
appsList.modified.clear();
+ if (!addedOrModified.isEmpty()) {
+ scheduleCallbackTask((callbacks) -> callbacks.bindAppsAddedOrUpdated(addedOrModified));
+ }
final ArrayList<AppInfo> removedApps = new ArrayList<>(appsList.removed);
appsList.removed.clear();
-
- final ArrayMap<ComponentName, AppInfo> addedOrUpdatedApps = new ArrayMap<>();
- if (!addedOrModified.isEmpty()) {
- scheduleCallbackTask((callbacks) -> callbacks.bindAppsAddedOrUpdated(addedOrModified));
- for (AppInfo ai : addedOrModified) {
- addedOrUpdatedApps.put(ai.componentName, ai);
+ final HashSet<ComponentName> removedComponents = new HashSet<>();
+ if (mOp == OP_UPDATE) {
+ for (AppInfo ai : removedApps) {
+ removedComponents.add(ai.componentName);
}
}
@@ -201,7 +201,7 @@
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 @@
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 @@
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 @@
}
final HashSet<String> removedPackages = new HashSet<>();
- final HashSet<ComponentName> removedComponents = new HashSet<>();
if (mOp == OP_REMOVE) {
// Mark all packages in the broadcast to be removed
Collections.addAll(removedPackages, packages);
@@ -330,11 +323,6 @@
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 @@
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;
+ }
}