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

Commit 69af278f authored by Stevie Kideckel's avatar Stevie Kideckel
Browse files

Refresh widgets in the model when packages change

Fix: 191163748
Test: local sample app
Change-Id: Ic09ad11cabf6efa471fb6e685cb255c4a17493ee
parent 358411a9
Loading
Loading
Loading
Loading
+9 −0
Original line number Diff line number Diff line
@@ -29,6 +29,7 @@ import android.os.UserHandle;
import android.os.UserManager;
import android.util.Log;

import com.android.launcher3.Launcher;
import com.android.launcher3.LauncherAppState;
import com.android.launcher3.LauncherSettings;
import com.android.launcher3.LauncherSettings.Favorites;
@@ -123,6 +124,14 @@ public class PackageUpdatedTask extends BaseModelUpdateTask {
                        activitiesLists.put(
                                packages[i], appsList.updatePackage(context, packages[i], mUser));
                        app.getWidgetCache().removePackage(packages[i], mUser);

                        // The update may have changed which shortcuts/widgets are available.
                        // Refresh the widgets for the package if we have an activity running.
                        Launcher launcher = Launcher.ACTIVITY_TRACKER.getCreatedActivity();
                        if (launcher != null) {
                            launcher.refreshAndBindWidgetsForPackageUser(
                                    new PackageUserKey(packages[i], mUser));
                        }
                    }
                }
                // Since package was just updated, the target must be available now.
+36 −17
Original line number Diff line number Diff line
@@ -15,6 +15,7 @@ import android.os.UserHandle;
import android.util.Log;

import androidx.annotation.Nullable;
import androidx.collection.ArrayMap;

import com.android.launcher3.AppFilter;
import com.android.launcher3.InvariantDeviceProfile;
@@ -37,6 +38,7 @@ import com.android.launcher3.widget.picker.WidgetsDiffReporter;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -158,34 +160,29 @@ public class WidgetsModel {
            Log.d(TAG, "addWidgetsAndShortcuts, widgetsShortcuts#=" + rawWidgetsShortcuts.size());
        }

        // Temporary list for {@link PackageItemInfos} to avoid having to go through
        // Temporary cache for {@link PackageItemInfos} to avoid having to go through
        // {@link mPackageItemInfos} to locate the key to be used for {@link #mWidgetsList}
        HashMap<WidgetPackageOrCategoryKey, PackageItemInfo> tmpPackageItemInfos = new HashMap<>();
        PackageItemInfoCache packageItemInfoCache = new PackageItemInfoCache();

        // Clear the lists only if this is an update on all widgets and shortcuts. If packageUser
        // isn't null, only updates the shortcuts and widgets for the app represented in
        // packageUser.
        if (packageUser == null) {
            // Clear the list if this is an update on all widgets and shortcuts.
            mWidgetsList.clear();
        } else {
            // Otherwise, only clear the widgets and shortcuts for the changed package.
            mWidgetsList.remove(
                    packageItemInfoCache.getOrCreate(new WidgetPackageOrCategoryKey(packageUser)));
        }

        // add and update.
        mWidgetsList.putAll(rawWidgetsShortcuts.stream()
                .filter(new WidgetValidityCheck(app))
                .collect(Collectors.groupingBy(item -> {
                    WidgetPackageOrCategoryKey packageUserKey = getWidgetPackageOrCategoryKey(item);
                    PackageItemInfo pInfo = tmpPackageItemInfos.get(packageUserKey);
                    if (pInfo == null) {
                        pInfo = new PackageItemInfo(item.componentName.getPackageName(),
                                packageUserKey.mCategory);
                        pInfo.user = item.user;
                        tmpPackageItemInfos.put(packageUserKey,  pInfo);
                    }
                    return pInfo;
                })));
                .collect(Collectors.groupingBy(item ->
                        packageItemInfoCache.getOrCreate(getWidgetPackageOrCategoryKey(item))
                )));

        // Update each package entry
        IconCache iconCache = app.getIconCache();
        for (PackageItemInfo p : tmpPackageItemInfos.values()) {
        for (PackageItemInfo p : packageItemInfoCache.values()) {
            iconCache.getTitleAndIconForApp(p, true /* userLowResIcon */);
        }
    }
@@ -289,6 +286,10 @@ public class WidgetsModel {
        public final UserHandle mUser;
        private final int mHashCode;

        WidgetPackageOrCategoryKey(PackageUserKey key) {
            this(key.mPackageName, key.mUser);
        }

        WidgetPackageOrCategoryKey(String packageName, UserHandle user) {
            this(packageName,  PackageItemInfo.NO_CATEGORY, user);
        }
@@ -310,4 +311,22 @@ public class WidgetsModel {
            return mHashCode;
        }
    }

    private static final class PackageItemInfoCache {
        private final Map<WidgetPackageOrCategoryKey, PackageItemInfo> mMap = new ArrayMap<>();

        PackageItemInfo getOrCreate(WidgetPackageOrCategoryKey key) {
            PackageItemInfo pInfo = mMap.get(key);
            if (pInfo == null) {
                pInfo = new PackageItemInfo(key.mPackage, key.mCategory);
                pInfo.user = key.mUser;
                mMap.put(key,  pInfo);
            }
            return pInfo;
        }

        Collection<PackageItemInfo> values() {
            return mMap.values();
        }
    }
}
 No newline at end of file