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

Commit ecf35935 authored by Shamali P's avatar Shamali P
Browse files

Delete the unused code from popup provider

Test: Existing tests
Flag: EXEMPT minor refactoring
Bug: 353347512
Change-Id: Iff81e5dc46542da2d59dfa4f54bd8fc06851a9c1
parent 5a1b3725
Loading
Loading
Loading
Loading
+0 −136
Original line number Diff line number Diff line
@@ -24,28 +24,20 @@ import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.android.launcher3.dot.DotInfo;
import com.android.launcher3.model.WidgetItem;
import com.android.launcher3.model.data.ItemInfo;
import com.android.launcher3.notification.NotificationKeyData;
import com.android.launcher3.notification.NotificationListener;
import com.android.launcher3.util.ComponentKey;
import com.android.launcher3.util.PackageUserKey;
import com.android.launcher3.util.ShortcutUtil;
import com.android.launcher3.widget.PendingAddWidgetInfo;
import com.android.launcher3.widget.model.WidgetsListBaseEntry;
import com.android.launcher3.widget.model.WidgetsListContentEntry;
import com.android.launcher3.widget.picker.WidgetRecommendationCategory;

import java.io.PrintWriter;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;

/**
 * Provides data for the popup menu that appears after long-clicking on apps.
@@ -62,18 +54,6 @@ public class PopupDataProvider implements NotificationListener.NotificationsChan
    /** Maps packages to their DotInfo's . */
    private Map<PackageUserKey, DotInfo> mPackageUserToDotInfos = new HashMap<>();

    /** All installed widgets. */
    private List<WidgetsListBaseEntry> mAllWidgets = List.of();
    /**
     * Selectively chosen installed widgets which may be preferred for default display over the list
     * of all widgets.
     */
    private List<WidgetsListBaseEntry> mDefaultWidgets = List.of();
    /** Widgets that can be recommended to the users. */
    private List<ItemInfo> mRecommendedWidgets = List.of();

    private PopupDataChangeListener mChangeListener = PopupDataChangeListener.INSTANCE;

    public PopupDataProvider(Consumer<Predicate<PackageUserKey>> notificationDotsChangeListener) {
        mNotificationDotsChangeListener = notificationDotsChangeListener;
    }
@@ -188,124 +168,8 @@ public class PopupDataProvider implements NotificationListener.NotificationsChan
        })) ? dotInfo : null;
    }

    /**
     * Sets a list of recommended widgets ordered by their order of appearance in the widgets
     * recommendation UI.
     */
    public void setRecommendedWidgets(List<ItemInfo> recommendedWidgets) {
        mRecommendedWidgets = recommendedWidgets;
        mChangeListener.onRecommendedWidgetsBound();
    }

    public void setAllWidgets(List<WidgetsListBaseEntry> allWidgets) {
        mAllWidgets = allWidgets;
        mDefaultWidgets = List.of();
        mChangeListener.onWidgetsBound();
    }

    /**
     * Sets the list of widgets to be displayed by default and a complete list that can be displayed
     * when user chooses to show all widgets.
     */
    public void setAllWidgets(List<WidgetsListBaseEntry> allWidgets,
            List<WidgetsListBaseEntry> defaultWidgets) {
        mAllWidgets = allWidgets;
        mDefaultWidgets = defaultWidgets;
        mChangeListener.onWidgetsBound();
    }

    public void setChangeListener(PopupDataChangeListener listener) {
        mChangeListener = listener == null ? PopupDataChangeListener.INSTANCE : listener;
    }

    public List<WidgetsListBaseEntry> getAllWidgets() {
        return mAllWidgets;
    }

    /**
     * Returns a "selectively" chosen list of widgets that may be preferred to be shown by default
     * instead of a complete list.
     */
    public List<WidgetsListBaseEntry> getDefaultWidgets() {
        return mDefaultWidgets;
    }

    /** Returns a list of recommended widgets. */
    public List<WidgetItem> getRecommendedWidgets() {
        HashMap<ComponentKey, WidgetItem> allWidgetItems = new HashMap<>();
        mAllWidgets.stream()
                .filter(entry -> entry instanceof WidgetsListContentEntry)
                .forEach(entry -> ((WidgetsListContentEntry) entry).mWidgets
                        .forEach(widget -> allWidgetItems.put(
                                new ComponentKey(widget.componentName, widget.user), widget)));
        return mRecommendedWidgets.stream()
                .map(recommendedWidget -> allWidgetItems.get(
                        new ComponentKey(recommendedWidget.getTargetComponent(),
                                recommendedWidget.user)))
                .filter(Objects::nonNull)
                .collect(Collectors.toList());
    }

    /** Returns the recommended widgets mapped by their category. */
    @NonNull
    public Map<WidgetRecommendationCategory, List<WidgetItem>> getCategorizedRecommendedWidgets() {
        Map<ComponentKey, WidgetItem> allWidgetItems = mAllWidgets.stream()
                .filter(entry -> entry instanceof WidgetsListContentEntry)
                .flatMap(entry -> entry.mWidgets.stream())
                .distinct()
                .collect(Collectors.toMap(
                        widget -> new ComponentKey(widget.componentName, widget.user),
                        Function.identity()
                ));
        return mRecommendedWidgets.stream()
                .filter(itemInfo -> itemInfo instanceof PendingAddWidgetInfo
                        && ((PendingAddWidgetInfo) itemInfo).recommendationCategory != null)
                .collect(Collectors.groupingBy(
                        it -> ((PendingAddWidgetInfo) it).recommendationCategory,
                        Collectors.collectingAndThen(
                                Collectors.toList(),
                                list -> list.stream()
                                        .map(it -> allWidgetItems.get(
                                                new ComponentKey(it.getTargetComponent(),
                                                        it.user)))
                                        .filter(Objects::nonNull)
                                        .collect(Collectors.toList())
                        )
                ));
    }

    public List<WidgetItem> getWidgetsForPackageUser(PackageUserKey packageUserKey) {
        return mAllWidgets.stream()
                .filter(row -> row instanceof WidgetsListContentEntry
                        && row.mPkgItem.packageName.equals(packageUserKey.mPackageName))
                .flatMap(row -> ((WidgetsListContentEntry) row).mWidgets.stream())
                .filter(widget -> packageUserKey.mUser.equals(widget.user))
                .collect(Collectors.toList());
    }

    /** Gets the WidgetsListContentEntry for the currently selected header. */
    public WidgetsListContentEntry getSelectedAppWidgets(PackageUserKey packageUserKey,
            boolean useDefault) {
        List<WidgetsListBaseEntry> widgets = useDefault ? mDefaultWidgets : mAllWidgets;
        return (WidgetsListContentEntry) widgets.stream()
                .filter(row -> row instanceof WidgetsListContentEntry
                        && PackageUserKey.fromPackageItemInfo(row.mPkgItem).equals(packageUserKey))
                .findAny()
                .orElse(null);
    }

    public void dump(String prefix, PrintWriter writer) {
        writer.println(prefix + "PopupDataProvider:");
        writer.println(prefix + "\tmPackageUserToDotInfos:" + mPackageUserToDotInfos);
    }

    public interface PopupDataChangeListener {

        PopupDataChangeListener INSTANCE = new PopupDataChangeListener() { };

        default void onWidgetsBound() { }

        /** A callback to get notified when recommended widgets are bound. */
        default void onRecommendedWidgetsBound() { }
    }
}