Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit f9d9df7d authored by Tony Wickham's avatar Tony Wickham
Browse files

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
parent b75f5366
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -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);
                    }
+0 −1
Original line number Diff line number Diff line
@@ -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;

+9 −17
Original line number Diff line number Diff line
@@ -381,9 +381,7 @@ 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.
                                    // Gracefully try to find a fallback activity.
                                    intent = pmHelper.getAppLaunchIntent(targetPkg, c.user);
                                    if (intent != null) {
                                        c.restoreFlag = 0;
@@ -395,12 +393,6 @@ public class LoaderTask implements Runnable {
                                        c.markDeleted("Unable to find a launch target");
                                        continue;
                                    }
                                    } else {
                                        // The app is installed but the component is no
                                        // longer available.
                                        c.markDeleted("Invalid component removed: " + cn);
                                        continue;
                                    }
                                }
                            }
                            // else if cn == null => can't infer much, leave it
+28 −25
Original line number Diff line number Diff line
@@ -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;
    }
}