Change how PagedTileLayout is measured

The change makes PagedTileLayout only use excess space from QSPanel
after other views have been measured and the padding has been accounted
for (see LinearLayout#measureVertical).

PagedTileLayout caches the last height to be measured so as to minimize
the number of times the number of rows is recalculated (and prevent
loops).

Also, fixed the calculation in TileLayout#updateMaxRows to match the
height calculation in TileLayout#onMeasure.

Test: manual, stress testing with multiple pages, adding and removing
tiles, starting and disconnecting VPN, changing display size
Bug:122714773

Change-Id: I5c85f03cfc79e341244d213fd92307821db80889
diff --git a/packages/SystemUI/src/com/android/systemui/qs/PagedTileLayout.java b/packages/SystemUI/src/com/android/systemui/qs/PagedTileLayout.java
index ebc3a6a..e22a21a 100644
--- a/packages/SystemUI/src/com/android/systemui/qs/PagedTileLayout.java
+++ b/packages/SystemUI/src/com/android/systemui/qs/PagedTileLayout.java
@@ -63,6 +63,7 @@
     private int mLayoutDirection;
     private int mHorizontalClipBound;
     private final Rect mClippingRect;
+    private int mLastMaxHeight = -1;
 
     public PagedTileLayout(Context context, AttributeSet attrs) {
         super(context, attrs);
@@ -303,8 +304,11 @@
     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
 
         final int nTiles = mTiles.size();
-        if (MeasureSpec.getMode(heightMeasureSpec) == MeasureSpec.AT_MOST) {
+        // If we have no reason to recalculate the number of rows, skip this step. In particular,
+        // if the height passed by its parent is the same as the last time, we try not to remeasure.
+        if (mDistributeTiles || mLastMaxHeight != MeasureSpec.getSize(heightMeasureSpec)) {
 
+            mLastMaxHeight = MeasureSpec.getSize(heightMeasureSpec);
             // Only change the pages if the number of rows or columns (from updateResources) has
             // changed or the tiles have changed
             if (mPages.get(0).updateMaxRows(heightMeasureSpec, nTiles) || mDistributeTiles) {