From 09e9a3be0b91a4ce70144b04d4ef66bf5bee575a Mon Sep 17 00:00:00 2001 From: Fabian Kozynski Date: Fri, 4 Jun 2021 11:00:04 -0400 Subject: [PATCH] Fix margins around tiles in QSCustomizer When tiles are moved, only those that are marked as modified will be forced to update the item offsets, with the others maybe never getting the chance. Given that moving may make tiles change columns and therefore their margin, this is not good. Instead, have the LayoutManager always poll the MarginItemDecoration for the correct margins for elements that are not TextView (tiles), then set that as the margin. Also, fix margins for RTL. Test: manual Bug: 188640649 Change-Id: I4aaef4887a6f50e5070c764048b67792dec3a35c --- .../qs/customize/QSCustomizerController.java | 16 +++++++++ .../systemui/qs/customize/TileAdapter.java | 34 +++++++++++++------ 2 files changed, 40 insertions(+), 10 deletions(-) diff --git a/packages/SystemUI/src/com/android/systemui/qs/customize/QSCustomizerController.java b/packages/SystemUI/src/com/android/systemui/qs/customize/QSCustomizerController.java index f56a2bbefaf77..49d18e62346ae 100644 --- a/packages/SystemUI/src/com/android/systemui/qs/customize/QSCustomizerController.java +++ b/packages/SystemUI/src/com/android/systemui/qs/customize/QSCustomizerController.java @@ -20,9 +20,11 @@ import static com.android.systemui.qs.customize.QSCustomizer.EXTRA_QS_CUSTOMIZIN import static com.android.systemui.qs.customize.QSCustomizer.MENU_RESET; import android.content.res.Configuration; +import android.graphics.Rect; import android.os.Bundle; import android.view.MenuItem; import android.view.View; +import android.widget.TextView; import android.widget.Toolbar; import android.widget.Toolbar.OnMenuItemClickListener; @@ -139,6 +141,20 @@ public class QSCustomizerController extends ViewController { RecyclerView.State state, View host, AccessibilityNodeInfoCompat info) { // Do not read row and column every time it changes. } + + public void calculateItemDecorationsForChild(View child, Rect outRect) { + // There's only a single item decoration that cares about the itemOffsets, so + // we just call it manually so they are never cached. This way, it's updated as the + // tiles are moved around. + // It only sets the left and right margin and only cares about tiles (not TextView). + if (!(child instanceof TextView)) { + outRect.setEmpty(); + mTileAdapter.getMarginItemDecoration().getItemOffsets(outRect, child, + recyclerView, new RecyclerView.State()); + ((LayoutParams) child.getLayoutParams()).leftMargin = outRect.left; + ((LayoutParams) child.getLayoutParams()).rightMargin = outRect.right; + } + } }; layout.setSpanSizeLookup(mTileAdapter.getSizeLookup()); recyclerView.setLayoutManager(layout); diff --git a/packages/SystemUI/src/com/android/systemui/qs/customize/TileAdapter.java b/packages/SystemUI/src/com/android/systemui/qs/customize/TileAdapter.java index ba65d5163881d..5a60d2624a436 100644 --- a/packages/SystemUI/src/com/android/systemui/qs/customize/TileAdapter.java +++ b/packages/SystemUI/src/com/android/systemui/qs/customize/TileAdapter.java @@ -690,9 +690,7 @@ public class TileAdapter extends RecyclerView.Adapter implements TileSta if (parent.getLayoutManager() == null) return; GridLayoutManager lm = ((GridLayoutManager) parent.getLayoutManager()); - SpanSizeLookup span = lm.getSpanSizeLookup(); - ViewHolder holder = parent.getChildViewHolder(view); - int column = span.getSpanIndex(holder.getBindingAdapterPosition(), lm.getSpanCount()); + int column = ((GridLayoutManager.LayoutParams) view.getLayoutParams()).getSpanIndex(); if (view instanceof TextView) { super.getItemOffsets(outRect, view, parent, state); @@ -702,14 +700,30 @@ public class TileAdapter extends RecyclerView.Adapter implements TileSta // columns). outRect.left = mHalfMargin; outRect.right = mHalfMargin; - } else if (column == 0) { - // Leftmost column when not using side margins. Should only have margin on the - // right. - outRect.right = mHalfMargin; } else { - // Rightmost column when not using side margins. Should only have margin on the - // left. - outRect.left = mHalfMargin; + // Leftmost or rightmost column + if (parent.isLayoutRtl()) { + if (column == 0) { + // Rightmost column + outRect.left = mHalfMargin; + outRect.right = 0; + } else { + // Leftmost column + outRect.left = 0; + outRect.right = mHalfMargin; + } + } else { + // Non RTL + if (column == 0) { + // Leftmost column + outRect.left = 0; + outRect.right = mHalfMargin; + } else { + // Rightmost column + outRect.left = mHalfMargin; + outRect.right = 0; + } + } } } }