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
This commit is contained in:
Tony Wickham
2018-09-05 12:48:13 -07:00
parent b75f5366f9
commit f9d9df7d3a
4 changed files with 38 additions and 44 deletions

View File

@@ -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<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 @@ 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<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 @@ 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;
}
}