diff --git a/src/com/android/launcher3/widget/picker/WidgetRecommendationsView.java b/src/com/android/launcher3/widget/picker/WidgetRecommendationsView.java index 255a6d2504..4f73e660d0 100644 --- a/src/com/android/launcher3/widget/picker/WidgetRecommendationsView.java +++ b/src/com/android/launcher3/widget/picker/WidgetRecommendationsView.java @@ -18,7 +18,9 @@ package com.android.launcher3.widget.picker; import static com.android.launcher3.widget.util.WidgetsTableUtils.groupWidgetItemsUsingRowPxWithoutReordering; +import android.content.ComponentName; import android.content.Context; +import android.os.Bundle; import android.util.AttributeSet; import android.view.LayoutInflater; import android.view.View; @@ -31,14 +33,19 @@ import androidx.annotation.Px; import com.android.launcher3.DeviceProfile; import com.android.launcher3.PagedView; import com.android.launcher3.R; +import com.android.launcher3.Utilities; import com.android.launcher3.model.WidgetItem; import com.android.launcher3.pageindicators.PageIndicatorDots; import java.util.ArrayList; +import java.util.Collections; +import java.util.HashSet; import java.util.List; import java.util.Map; +import java.util.Set; import java.util.TreeMap; import java.util.function.Consumer; +import java.util.stream.Collectors; /** * A {@link PagedView} that displays widget recommendations in categories with dots as paged @@ -46,6 +53,8 @@ import java.util.function.Consumer; */ public final class WidgetRecommendationsView extends PagedView { private @Px float mAvailableHeight = Float.MAX_VALUE; + private static final String INITIALLY_DISPLAYED_WIDGETS_STATE_KEY = + "widgetRecommendationsView:mDisplayedWidgets"; private static final int MAX_CATEGORIES = 3; private TextView mRecommendationPageTitle; private final List mCategoryTitles = new ArrayList<>(); @@ -57,6 +66,7 @@ public final class WidgetRecommendationsView extends PagedView mDisplayedWidgets = Collections.emptySet(); public WidgetRecommendationsView(Context context) { this(context, /* attrs= */ null); @@ -76,6 +86,38 @@ public final class WidgetRecommendationsView extends PagedView(mDisplayedWidgets)); + } + + /** + * Restores the state that was saved by the saveState method during orientation / other config + * changes. + */ + public void restoreState(Bundle bundle) { + ArrayList componentList; + if (Utilities.ATLEAST_T) { + componentList = bundle.getParcelableArrayList( + INITIALLY_DISPLAYED_WIDGETS_STATE_KEY, ComponentName.class); + } else { + componentList = bundle.getParcelableArrayList( + INITIALLY_DISPLAYED_WIDGETS_STATE_KEY); + } + + // Restore the "initial" set of widgets that were displayed, so that, on rotation / fold / + // unfold, we can maintain the set of widgets that user first saw (if they fit). + if (componentList != null) { + mDisplayedWidgets = new HashSet<>(componentList); + } + } + /** Sets a {@link android.view.View.OnLongClickListener} for all widget cells in this table. */ public void setWidgetCellLongClickListener(OnLongClickListener onLongClickListener) { mWidgetCellOnLongClickListener = onLongClickListener; @@ -112,10 +154,18 @@ public final class WidgetRecommendationsView extends PagedView displayedWidgets = maybeDisplayInTable(recommendedWidgets, + deviceProfile, availableWidth, cellPadding); + + if (mDisplayedWidgets.isEmpty()) { + // Save the widgets shown for the first time user opened the picker; so that, they can + // be maintained across orientation changes. + mDisplayedWidgets = displayedWidgets; + } + updateTitleAndIndicator(/* requestedPage= */ 0); - return displayedWidgets; + return displayedWidgets.size(); } /** @@ -144,20 +194,21 @@ public final class WidgetRecommendationsView extends PagedView allDisplayedWidgets = new HashSet<>(); // Render top MAX_CATEGORIES in separate tables. Each table becomes a page. for (Map.Entry> entry : new TreeMap<>(recommendations).entrySet()) { // If none of the recommendations for the category could fit in the mAvailableHeight, we // don't want to add that category; and we look for the next one. - int displayedCount = maybeDisplayInTable(entry.getValue(), deviceProfile, + Set displayedWidgetsForCategory = maybeDisplayInTable(entry.getValue(), + deviceProfile, availableWidth, cellPadding); - if (displayedCount > 0) { + if (!displayedWidgetsForCategory.isEmpty()) { mCategoryTitles.add( context.getResources().getString(entry.getKey().categoryTitleRes)); displayedCategories++; - totalDisplayedWidgets += displayedCount; + allDisplayedWidgets.addAll(displayedWidgetsForCategory); } if (displayedCategories == MAX_CATEGORIES) { @@ -165,11 +216,17 @@ public final class WidgetRecommendationsView extends PagedViewReturns false if none of the recommendations could fit.

+ * Groups the provided recommendations into rows and displays ones that fit in a table. + *

Returns the set of widgets that could fit.

*/ - private int maybeDisplayInTable(List recommendedWidgets, + private Set maybeDisplayInTable(List recommendedWidgets, DeviceProfile deviceProfile, final @Px int availableWidth, final @Px int cellPadding) { + List filteredRecommendedWidgets = recommendedWidgets; + // Show only those widgets that were displayed when user first opened the picker. + if (!mDisplayedWidgets.isEmpty()) { + filteredRecommendedWidgets = recommendedWidgets.stream().filter( + w -> mDisplayedWidgets.contains(w.componentName)).toList(); + } Context context = getContext(); LayoutInflater inflater = LayoutInflater.from(context); // Since we are limited by space, we don't sort recommendations - to show most relevant // (if possible). List> rows = groupWidgetItemsUsingRowPxWithoutReordering( - recommendedWidgets, + filteredRecommendedWidgets, context, deviceProfile, availableWidth, @@ -268,13 +330,17 @@ public final class WidgetRecommendationsView extends PagedView> displayedItems = recommendationsTable.setRecommendedWidgets( + rows, deviceProfile, mAvailableHeight); - if (displayedCount > 0) { + + if (!displayedItems.isEmpty()) { addView(recommendationsTable); } - return displayedCount; + return displayedItems.stream().flatMap( + items -> items.stream().map(w -> w.componentName)) + .collect(Collectors.toSet()); } /** Returns location of a widget cell for displaying the "touch and hold" education tip. */ diff --git a/src/com/android/launcher3/widget/picker/WidgetsFullSheet.java b/src/com/android/launcher3/widget/picker/WidgetsFullSheet.java index 85375eec9d..c3067a5583 100644 --- a/src/com/android/launcher3/widget/picker/WidgetsFullSheet.java +++ b/src/com/android/launcher3/widget/picker/WidgetsFullSheet.java @@ -633,15 +633,15 @@ public class WidgetsFullSheet extends BaseWidgetSheet } @Px - private float getMaxAvailableHeightForRecommendations() { + protected float getMaxAvailableHeightForRecommendations() { // There isn't enough space to show recommendations in landscape orientation on phones with // a full sheet design. Tablets use a two pane picker. - if (!isTwoPane() && mDeviceProfile.isLandscape) { + if (mDeviceProfile.isLandscape) { return 0f; } return (mDeviceProfile.heightPx - mDeviceProfile.bottomSheetTopPadding) - * getRecommendationSectionHeightRatio(); + * RECOMMENDATION_TABLE_HEIGHT_RATIO; } /** b/209579563: "Widgets" header should be focused first. */ @@ -650,14 +650,6 @@ public class WidgetsFullSheet extends BaseWidgetSheet return mHeaderTitle; } - /** - * Ratio of recommendations section with respect to bottom sheet's height on scale of 0 to 1. - */ - @Px - protected float getRecommendationSectionHeightRatio() { - return RECOMMENDATION_TABLE_HEIGHT_RATIO; - } - private void open(boolean animate) { if (animate) { if (getPopupContainer().getInsets().bottom > 0) { @@ -733,6 +725,7 @@ public class WidgetsFullSheet extends BaseWidgetSheet // picker and calls save/restore hierarchy state. We save the state of recommendations // across those updates. bundle.putInt(RECOMMENDATIONS_SAVED_STATE_KEY, mRecommendationsCurrentPage); + mWidgetRecommendationsView.saveState(bundle); SparseArray superState = new SparseArray<>(); super.saveHierarchyState(superState); bundle.putSparseParcelableArray(SUPER_SAVED_STATE_KEY, superState); @@ -744,6 +737,7 @@ public class WidgetsFullSheet extends BaseWidgetSheet Bundle state = (Bundle) sparseArray.get(0); mRecommendationsCurrentPage = state.getInt( RECOMMENDATIONS_SAVED_STATE_KEY, /*defaultValue=*/0); + mWidgetRecommendationsView.restoreState(state); super.restoreHierarchyState(state.getSparseParcelableArray(SUPER_SAVED_STATE_KEY)); } diff --git a/src/com/android/launcher3/widget/picker/WidgetsRecommendationTableLayout.java b/src/com/android/launcher3/widget/picker/WidgetsRecommendationTableLayout.java index 563894d804..a565780ddf 100644 --- a/src/com/android/launcher3/widget/picker/WidgetsRecommendationTableLayout.java +++ b/src/com/android/launcher3/widget/picker/WidgetsRecommendationTableLayout.java @@ -83,14 +83,15 @@ public final class WidgetsRecommendationTableLayout extends TableLayout { *

If the content can't fit {@code recommendationTableMaxHeight}, this view will remove a * last row from the {@code recommendedWidgets} until it fits or only one row left. * - *

Returns {@code false} if none of the widgets could fit

+ *

Returns the list of widgets that could fit

*/ - public int setRecommendedWidgets(List> recommendedWidgets, + public List> setRecommendedWidgets( + List> recommendedWidgets, DeviceProfile deviceProfile, float recommendationTableMaxHeight) { List> rows = selectRowsThatFitInAvailableHeight(recommendedWidgets, recommendationTableMaxHeight, deviceProfile); bindData(rows); - return rows.stream().mapToInt(ArrayList::size).sum(); + return rows; } private void bindData(List> recommendationTable) { diff --git a/src/com/android/launcher3/widget/picker/WidgetsTwoPaneSheet.java b/src/com/android/launcher3/widget/picker/WidgetsTwoPaneSheet.java index 1bf813c89c..14985bf148 100644 --- a/src/com/android/launcher3/widget/picker/WidgetsTwoPaneSheet.java +++ b/src/com/android/launcher3/widget/picker/WidgetsTwoPaneSheet.java @@ -276,8 +276,15 @@ public class WidgetsTwoPaneSheet extends WidgetsFullSheet { @Override @Px - protected float getRecommendationSectionHeightRatio() { - return RECOMMENDATION_SECTION_HEIGHT_RATIO_TWO_PANE; + protected float getMaxAvailableHeightForRecommendations() { + if (mRecommendedWidgetsCount > 0) { + // If widgets were already selected for display, we show them all on orientation change + // in a two pane picker + return Float.MAX_VALUE; + } + + return (mDeviceProfile.heightPx - mDeviceProfile.bottomSheetTopPadding) + * RECOMMENDATION_SECTION_HEIGHT_RATIO_TWO_PANE; } @Override