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

Commit 81fc2787 authored by Shamali P's avatar Shamali P Committed by Shamali Patwa
Browse files

Show full page view if categorized view is low density in large screen

UX prefers full page view over low density categorized view in tablets.

Bug: 347288847
Test: Screenshot (see the tablet screenshots vs foldable)
Flag: EXEMPT BUGFIX
Change-Id: I6c8ad36dc6169c65aea4e655af0542958e555669
parent b5522f05
Loading
Loading
Loading
Loading
+41 −2
Original line number Original line Diff line number Diff line
@@ -16,7 +16,7 @@


package com.android.launcher3.widget.picker;
package com.android.launcher3.widget.picker;


import static com.android.launcher3.widget.util.WidgetsTableUtils.groupWidgetItemsUsingRowPxWithoutReordering;
import static com.android.launcher3.widget.util.WidgetsTableUtils.groupWidgetItemsUsingRowPxWithReordering;


import android.content.ComponentName;
import android.content.ComponentName;
import android.content.Context;
import android.content.Context;
@@ -38,6 +38,7 @@ import com.android.launcher3.model.WidgetItem;
import com.android.launcher3.pageindicators.PageIndicatorDots;
import com.android.launcher3.pageindicators.PageIndicatorDots;


import java.util.ArrayList;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Collections;
import java.util.HashSet;
import java.util.HashSet;
import java.util.List;
import java.util.List;
@@ -57,6 +58,12 @@ public final class WidgetRecommendationsView extends PagedView<PageIndicatorDots
    private static final String INITIALLY_DISPLAYED_WIDGETS_STATE_KEY =
    private static final String INITIALLY_DISPLAYED_WIDGETS_STATE_KEY =
            "widgetRecommendationsView:mDisplayedWidgets";
            "widgetRecommendationsView:mDisplayedWidgets";
    private static final int MAX_CATEGORIES = 3;
    private static final int MAX_CATEGORIES = 3;

    // Whether to show all widgets in a full page without any limitation on height
    private boolean mShowFullPageViewIfLowDensity = false;
    // Number of items below which a category is considered low density.
    private static final int IDEAL_ITEMS_PER_CATEGORY = 2;

    private TextView mRecommendationPageTitle;
    private TextView mRecommendationPageTitle;
    private final List<String> mCategoryTitles = new ArrayList<>();
    private final List<String> mCategoryTitles = new ArrayList<>();


@@ -87,6 +94,14 @@ public final class WidgetRecommendationsView extends PagedView<PageIndicatorDots
        mRecommendationPageTitle = parent.findViewById(R.id.recommendations_page_title);
        mRecommendationPageTitle = parent.findViewById(R.id.recommendations_page_title);
    }
    }


    /**
     * When there are less than 3 categories or when at least one category has less than 2 widgets,
     * all widgets will be shown in a single page without being limited by the available height.
     */
    public void enableFullPageViewIfLowDensity() {
        mShowFullPageViewIfLowDensity = true;
    }

    /**
    /**
     * Saves the necessary state in the provided bundle. To be called in case of orientation /
     * Saves the necessary state in the provided bundle. To be called in case of orientation /
     * other config changes.
     * other config changes.
@@ -170,6 +185,22 @@ public final class WidgetRecommendationsView extends PagedView<PageIndicatorDots
        return displayedWidgets.size();
        return displayedWidgets.size();
    }
    }


    private boolean shouldShowFullPageView(
            Map<WidgetRecommendationCategory, List<WidgetItem>> recommendations) {
        if (mShowFullPageViewIfLowDensity) {
            boolean hasLessCategories = recommendations.size() < MAX_CATEGORIES;
            long lowDensityCategoriesCount = recommendations.values()
                    .stream()
                    .limit(MAX_CATEGORIES)
                    .filter(items -> items.size() < IDEAL_ITEMS_PER_CATEGORY).count();

            // If there less number of categories or if there are at least 2 categorizes with less
            // widgets, prefer showing single page view.
            return hasLessCategories || lowDensityCategoriesCount > 1;
        }
        return false;
    }

    /**
    /**
     * Displays the recommendations grouped by categories as pages.
     * Displays the recommendations grouped by categories as pages.
     * <p>In case of a single category, no title is displayed for it.</p>
     * <p>In case of a single category, no title is displayed for it.</p>
@@ -188,6 +219,14 @@ public final class WidgetRecommendationsView extends PagedView<PageIndicatorDots
            Map<WidgetRecommendationCategory, List<WidgetItem>> recommendations,
            Map<WidgetRecommendationCategory, List<WidgetItem>> recommendations,
            DeviceProfile deviceProfile, final @Px float availableHeight,
            DeviceProfile deviceProfile, final @Px float availableHeight,
            final @Px int availableWidth, final @Px int cellPadding, final int requestedPage) {
            final @Px int availableWidth, final @Px int cellPadding, final int requestedPage) {
        if (shouldShowFullPageView(recommendations)) {
            // Show all widgets in single page with unlimited available height.
            return setRecommendations(
                    recommendations.values().stream().flatMap(Collection::stream).toList(),
                    deviceProfile, /*availableHeight=*/ Float.MAX_VALUE, availableWidth,
                    cellPadding);

        }
        this.mAvailableHeight = availableHeight;
        this.mAvailableHeight = availableHeight;
        this.mAvailableWidth = availableWidth;
        this.mAvailableWidth = availableWidth;
        Context context = getContext();
        Context context = getContext();
@@ -325,7 +364,7 @@ public final class WidgetRecommendationsView extends PagedView<PageIndicatorDots


        // Since we are limited by space, we don't sort recommendations - to show most relevant
        // Since we are limited by space, we don't sort recommendations - to show most relevant
        // (if possible).
        // (if possible).
        List<ArrayList<WidgetItem>> rows = groupWidgetItemsUsingRowPxWithoutReordering(
        List<ArrayList<WidgetItem>> rows = groupWidgetItemsUsingRowPxWithReordering(
                filteredRecommendedWidgets,
                filteredRecommendedWidgets,
                context,
                context,
                deviceProfile,
                deviceProfile,
+2 −2
Original line number Original line Diff line number Diff line
@@ -18,7 +18,7 @@ package com.android.launcher3.widget.picker;
import static com.android.launcher3.Flags.enableCategorizedWidgetSuggestions;
import static com.android.launcher3.Flags.enableCategorizedWidgetSuggestions;
import static com.android.launcher3.LauncherSettings.Favorites.CONTAINER_WIDGETS_PREDICTION;
import static com.android.launcher3.LauncherSettings.Favorites.CONTAINER_WIDGETS_PREDICTION;
import static com.android.launcher3.widget.util.WidgetSizes.getWidgetSizePx;
import static com.android.launcher3.widget.util.WidgetSizes.getWidgetSizePx;
import static com.android.launcher3.widget.util.WidgetsTableUtils.WIDGETS_TABLE_ROW_SIZE_COMPARATOR;
import static com.android.launcher3.widget.util.WidgetsTableUtils.WIDGETS_TABLE_ROW_COUNT_COMPARATOR;


import static java.lang.Math.max;
import static java.lang.Math.max;


@@ -163,6 +163,6 @@ public final class WidgetsRecommendationTableLayout extends TableLayout {
        }
        }


        // Perform re-ordering once we have filtered out recommendations that fit.
        // Perform re-ordering once we have filtered out recommendations that fit.
        return filteredRows.stream().sorted(WIDGETS_TABLE_ROW_SIZE_COMPARATOR).toList();
        return filteredRows.stream().sorted(WIDGETS_TABLE_ROW_COUNT_COMPARATOR).toList();
    }
    }
}
}
+3 −0
Original line number Original line Diff line number Diff line
@@ -119,6 +119,9 @@ public class WidgetsTwoPaneSheet extends WidgetsFullSheet {
        mWidgetRecommendationsView.initParentViews(mWidgetRecommendationsContainer);
        mWidgetRecommendationsView.initParentViews(mWidgetRecommendationsContainer);
        mWidgetRecommendationsView.setWidgetCellLongClickListener(this);
        mWidgetRecommendationsView.setWidgetCellLongClickListener(this);
        mWidgetRecommendationsView.setWidgetCellOnClickListener(this);
        mWidgetRecommendationsView.setWidgetCellOnClickListener(this);
        if (!mDeviceProfile.isTwoPanels) {
            mWidgetRecommendationsView.enableFullPageViewIfLowDensity();
        }
        // To save the currently displayed page, so that, it can be requested when rebinding
        // To save the currently displayed page, so that, it can be requested when rebinding
        // recommendations with different size constraints.
        // recommendations with different size constraints.
        mWidgetRecommendationsView.addPageSwitchListener(
        mWidgetRecommendationsView.addPageSwitchListener(
+15 −0
Original line number Original line Diff line number Diff line
@@ -68,6 +68,21 @@ public final class WidgetsTableUtils {
                }
                }
            });
            });


    /**
     * Comparator that enables displaying rows with more number of items at the top, and then
     * rest of widgets shown in increasing order of their size (totalW * H).
     */
    public static final Comparator<ArrayList<WidgetItem>> WIDGETS_TABLE_ROW_COUNT_COMPARATOR =
            Comparator.comparingInt(row -> {
                if (row.size() > 1) {
                    return -row.size();
                } else {
                    int rowWidth = row.stream().mapToInt(w -> w.spanX).sum();
                    int rowHeight = row.get(0).spanY;
                    return (rowWidth * rowHeight);
                }
            });

    /**
    /**
     * Groups {@code widgetItems} items into a 2D array which matches their appearance in a UI
     * Groups {@code widgetItems} items into a 2D array which matches their appearance in a UI
     * table. This takes liberty to rearrange widgets to make the table visually appealing.
     * table. This takes liberty to rearrange widgets to make the table visually appealing.