From 9d02a431b78966a04a1c2e66b89a926fc54bac5f Mon Sep 17 00:00:00 2001 From: Jason Monk Date: Wed, 20 Jan 2016 16:33:46 -0500 Subject: [PATCH] Work on QS layouts Better layouts, less work, more columns... sometimes. Also fix some bugs. Bug: 26705136 Change-Id: Ia368791259a1a91522a3efb70729919da79e37ea --- .../res/layout/qs_customize_layout.xml | 8 -- packages/SystemUI/res/values-land/config.xml | 2 + .../res/values-sw600dp-land/config.xml | 2 + packages/SystemUI/res/values/dimens.xml | 4 +- .../android/systemui/qs/PagedTileLayout.java | 49 +++++++----- .../src/com/android/systemui/qs/QSPanel.java | 4 +- .../src/com/android/systemui/qs/QSTile.java | 14 +++- .../com/android/systemui/qs/QuickQSPanel.java | 3 +- .../com/android/systemui/qs/TileLayout.java | 76 +++++++------------ .../qs/customize/NonPagedTileLayout.java | 36 ++++----- .../systemui/qs/customize/QSCustomizer.java | 4 +- .../systemui/qs/customize/TileAdapter.java | 7 +- .../systemui/statusbar/phone/QSTileHost.java | 1 + .../systemui/tuner/QSPagingSwitch.java | 26 ------- 14 files changed, 99 insertions(+), 137 deletions(-) delete mode 100644 packages/SystemUI/src/com/android/systemui/tuner/QSPagingSwitch.java diff --git a/packages/SystemUI/res/layout/qs_customize_layout.xml b/packages/SystemUI/res/layout/qs_customize_layout.xml index 91cf89483c96e..0b8e02fb35643 100644 --- a/packages/SystemUI/res/layout/qs_customize_layout.xml +++ b/packages/SystemUI/res/layout/qs_customize_layout.xml @@ -21,14 +21,6 @@ android:layout_height="wrap_content" android:orientation="vertical"> - - -2 1.5 + + 4 diff --git a/packages/SystemUI/res/values-sw600dp-land/config.xml b/packages/SystemUI/res/values-sw600dp-land/config.xml index f9b01c84d6e30..7e8d802fc947d 100644 --- a/packages/SystemUI/res/values-sw600dp-land/config.xml +++ b/packages/SystemUI/res/values-sw600dp-land/config.xml @@ -18,4 +18,6 @@ 3 + + 3 diff --git a/packages/SystemUI/res/values/dimens.xml b/packages/SystemUI/res/values/dimens.xml index 3fb5f182c1b87..47eb05a5fb5e6 100644 --- a/packages/SystemUI/res/values/dimens.xml +++ b/packages/SystemUI/res/values/dimens.xml @@ -134,9 +134,7 @@ 25dp 88dp - 100dp - 88dp - 25dp + 16dp 48dp 12dp 44.5dp diff --git a/packages/SystemUI/src/com/android/systemui/qs/PagedTileLayout.java b/packages/SystemUI/src/com/android/systemui/qs/PagedTileLayout.java index 28ddf0600fe3e..6bc8b50911141 100644 --- a/packages/SystemUI/src/com/android/systemui/qs/PagedTileLayout.java +++ b/packages/SystemUI/src/com/android/systemui/qs/PagedTileLayout.java @@ -62,27 +62,32 @@ public class PagedTileLayout extends ViewPager implements QSTileLayout { @Override public int getOffsetTop(TileRecord tile) { - return ((ViewGroup) tile.tileView.getParent()).getTop(); + return ((ViewGroup) tile.tileView.getParent()).getTop() + getTop(); } @Override public void addTile(TileRecord tile) { mTiles.add(tile); - distributeTiles(); + postDistributeTiles(); } @Override public void removeTile(TileRecord tile) { if (mTiles.remove(tile)) { - distributeTiles(); + postDistributeTiles(); } } + private void postDistributeTiles() { + removeCallbacks(mDistribute); + post(mDistribute); + } + private void distributeTiles() { if (DEBUG) Log.d(TAG, "Distributing tiles"); final int NP = mPages.size(); for (int i = 0; i < NP; i++) { - mPages.get(i).clear(); + mPages.get(i).removeAllViews(); } int index = 0; final int NT = mTiles.size(); @@ -107,10 +112,15 @@ public class PagedTileLayout extends ViewPager implements QSTileLayout { } @Override - public void updateResources() { + public boolean updateResources() { + boolean changed = false; for (int i = 0; i < mPages.size(); i++) { - mPages.get(i).updateResources(); + changed |= mPages.get(i).updateResources(); } + if (changed) { + distributeTiles(); + } + return changed; } @Override @@ -129,6 +139,13 @@ public class PagedTileLayout extends ViewPager implements QSTileLayout { setMeasuredDimension(getMeasuredWidth(), maxHeight + mPageIndicator.getMeasuredHeight()); } + private final Runnable mDistribute = new Runnable() { + @Override + public void run() { + distributeTiles(); + } + }; + public static class TilePage extends TileLayout { private int mMaxRows = 3; @@ -137,21 +154,19 @@ public class PagedTileLayout extends ViewPager implements QSTileLayout { updateResources(); } + @Override + public boolean updateResources() { + if (super.updateResources()) { + mMaxRows = mColumns != 3 ? 2 : 3; + return true; + } + return false; + } + public void setMaxRows(int maxRows) { mMaxRows = maxRows; } - @Override - protected int getCellHeight() { - return mContext.getResources().getDimensionPixelSize(R.dimen.qs_new_tile_height); - } - - private void clear() { - if (DEBUG) Log.d(TAG, "Clearing page"); - removeAllViews(); - mRecords.clear(); - } - public boolean isFull() { return mRecords.size() >= mColumns * mMaxRows; } diff --git a/packages/SystemUI/src/com/android/systemui/qs/QSPanel.java b/packages/SystemUI/src/com/android/systemui/qs/QSPanel.java index fd07e50c18a62..b5f146b29fdf9 100644 --- a/packages/SystemUI/src/com/android/systemui/qs/QSPanel.java +++ b/packages/SystemUI/src/com/android/systemui/qs/QSPanel.java @@ -557,8 +557,6 @@ public class QSPanel extends FrameLayout implements Tunable { public static final class TileRecord extends Record { public QSTile tile; public QSTileBaseView tileView; - public int row; - public int col; public boolean scanState; public boolean openingDetail; } @@ -607,6 +605,6 @@ public class QSPanel extends FrameLayout implements Tunable { void addTile(TileRecord tile); void removeTile(TileRecord tile); int getOffsetTop(TileRecord tile); - void updateResources(); + boolean updateResources(); } } diff --git a/packages/SystemUI/src/com/android/systemui/qs/QSTile.java b/packages/SystemUI/src/com/android/systemui/qs/QSTile.java index 56364e9e52af7..72629a331500c 100644 --- a/packages/SystemUI/src/com/android/systemui/qs/QSTile.java +++ b/packages/SystemUI/src/com/android/systemui/qs/QSTile.java @@ -129,6 +129,10 @@ public abstract class QSTile implements Listenable { mHandler.obtainMessage(H.ADD_CALLBACK, callback).sendToTarget(); } + public void removeCallbacks() { + mHandler.sendEmptyMessage(H.REMOVE_CALLBACKS); + } + public void click() { mHandler.sendEmptyMessage(H.CLICK); } @@ -188,6 +192,10 @@ public abstract class QSTile implements Listenable { handleRefreshState(null); } + private void handleRemoveCallbacks() { + mCallbacks.clear(); + } + protected void handleSecondaryClick() { // Default to normal click. handleClick(); @@ -285,6 +293,7 @@ public abstract class QSTile implements Listenable { private static final int SCAN_STATE_CHANGED = 9; private static final int DESTROY = 10; private static final int CLEAR_STATE = 11; + private static final int REMOVE_CALLBACKS = 12; private H(Looper looper) { super(looper); @@ -296,7 +305,10 @@ public abstract class QSTile implements Listenable { try { if (msg.what == ADD_CALLBACK) { name = "handleAddCallback"; - handleAddCallback((QSTile.Callback)msg.obj); + handleAddCallback((QSTile.Callback) msg.obj); + } else if (msg.what == REMOVE_CALLBACKS) { + name = "handleRemoveCallbacks"; + handleRemoveCallbacks(); } else if (msg.what == CLICK) { name = "handleClick"; if (mState.disabledByPolicy) { diff --git a/packages/SystemUI/src/com/android/systemui/qs/QuickQSPanel.java b/packages/SystemUI/src/com/android/systemui/qs/QuickQSPanel.java index 5782800198306..b391c1e3b8758 100644 --- a/packages/SystemUI/src/com/android/systemui/qs/QuickQSPanel.java +++ b/packages/SystemUI/src/com/android/systemui/qs/QuickQSPanel.java @@ -145,8 +145,9 @@ public class QuickQSPanel extends QSPanel { } @Override - public void updateResources() { + public boolean updateResources() { // No resources here. + return false; } } } diff --git a/packages/SystemUI/src/com/android/systemui/qs/TileLayout.java b/packages/SystemUI/src/com/android/systemui/qs/TileLayout.java index ff111776ed853..59a394ff30f07 100644 --- a/packages/SystemUI/src/com/android/systemui/qs/TileLayout.java +++ b/packages/SystemUI/src/com/android/systemui/qs/TileLayout.java @@ -5,7 +5,6 @@ import android.content.res.Resources; import android.util.AttributeSet; import android.view.View; import android.view.ViewGroup; - import com.android.systemui.R; import com.android.systemui.qs.QSPanel.QSTileLayout; import com.android.systemui.qs.QSPanel.TileRecord; @@ -21,6 +20,7 @@ public class TileLayout extends ViewGroup implements QSTileLayout { protected int mColumns; private int mCellWidth; private int mCellHeight; + private int mCellMargin; protected final ArrayList mRecords = new ArrayList<>(); @@ -54,52 +54,33 @@ public class TileLayout extends ViewGroup implements QSTileLayout { super.removeAllViews(); } - public void updateResources() { + public boolean updateResources() { final Resources res = mContext.getResources(); final int columns = Math.max(1, res.getInteger(R.integer.quick_settings_num_columns)); - mCellHeight = getCellHeight(); - mCellWidth = (int) (mCellHeight * TILE_ASPECT); + mCellHeight = mContext.getResources().getDimensionPixelSize(R.dimen.qs_tile_height); + mCellMargin = res.getDimensionPixelSize(R.dimen.qs_tile_margin); if (mColumns != columns) { mColumns = columns; postInvalidate(); + return true; } - } - - protected int getCellHeight() { - return mContext.getResources().getDimensionPixelSize(R.dimen.qs_tile_height); + return false; } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { + final int numTiles = mRecords.size(); final int width = MeasureSpec.getSize(widthMeasureSpec); - int r = -1; - int c = -1; - int rows = 0; - for (TileRecord record : mRecords) { - if (record.tileView.getVisibility() == GONE) continue; - // wrap to next column if we've reached the max # of columns - // also don't allow dual + single tiles on the same row - if (r == -1 || c == (mColumns - 1)) { - r++; - c = 0; - } else { - c++; - } - record.row = r; - record.col = c; - rows = r + 1; - } + final int rows = (numTiles + mColumns - 1) / mColumns; + mCellWidth = (width - (mCellMargin * (mColumns + 1))) / mColumns; View previousView = this; for (TileRecord record : mRecords) { if (record.tileView.getVisibility() == GONE) continue; - final int cw = mCellWidth; - final int ch = mCellHeight; - record.tileView.measure(exactly(cw), exactly(ch)); + record.tileView.measure(exactly(mCellWidth), exactly(mCellHeight)); previousView = record.tileView.updateAccessibilityOrder(previousView); } - int h = rows == 0 ? 0 : getRowTop(rows); - setMeasuredDimension(width, h); + setMeasuredDimension(width, (mCellHeight + mCellMargin) * rows + mCellMargin); } private static int exactly(int size) { @@ -110,37 +91,32 @@ public class TileLayout extends ViewGroup implements QSTileLayout { protected void onLayout(boolean changed, int l, int t, int r, int b) { final int w = getWidth(); boolean isRtl = getLayoutDirection() == LAYOUT_DIRECTION_RTL; - for (TileRecord record : mRecords) { - if (record.tileView.getVisibility() == GONE) continue; - final int cols = getColumnCount(record.row); - final int cw = mCellWidth; - final int extra = (w - cw * cols) / (cols + 1); - int left = record.col * cw + (record.col + 1) * extra; - final int top = getRowTop(record.row); + int row = 0; + int column = 0; + for (int i = 0; i < mRecords.size(); i++, column++) { + if (column == mColumns) { + row++; + column -= mColumns; + } + TileRecord record = mRecords.get(i); + int left = getColumnStart(column); + final int top = getRowTop(row); int right; - int tileWith = record.tileView.getMeasuredWidth(); if (isRtl) { right = w - left; - left = right - tileWith; + left = right - mCellWidth; } else { - right = left + tileWith; + right = left + mCellWidth; } record.tileView.layout(left, top, right, top + record.tileView.getMeasuredHeight()); } } private int getRowTop(int row) { - if (row <= 0) return 0; - return row * mCellHeight; + return row * (mCellHeight + mCellMargin) + mCellMargin; } - private int getColumnCount(int row) { - int cols = 0; - for (TileRecord record : mRecords) { - if (record.tileView.getVisibility() == GONE) continue; - if (record.row == row) cols++; - } - return cols; + private int getColumnStart(int column) { + return column * (mCellWidth + mCellMargin) + mCellMargin; } - } diff --git a/packages/SystemUI/src/com/android/systemui/qs/customize/NonPagedTileLayout.java b/packages/SystemUI/src/com/android/systemui/qs/customize/NonPagedTileLayout.java index 3acbed865dc81..8f0d194732715 100644 --- a/packages/SystemUI/src/com/android/systemui/qs/customize/NonPagedTileLayout.java +++ b/packages/SystemUI/src/com/android/systemui/qs/customize/NonPagedTileLayout.java @@ -31,7 +31,6 @@ import com.android.systemui.qs.PagedTileLayout.TilePage; import com.android.systemui.qs.QSPanel.QSTileLayout; import com.android.systemui.qs.QSPanel.TileRecord; import com.android.systemui.qs.QSTile; -import com.android.systemui.qs.QuickTileLayout; import java.util.ArrayList; @@ -42,7 +41,6 @@ import java.util.ArrayList; */ public class NonPagedTileLayout extends LinearLayout implements QSTileLayout, OnTouchListener { - private QuickTileLayout mQuickTiles; private final ArrayList mPages = new ArrayList<>(); private final ArrayList mTiles = new ArrayList(); private CustomQSPanel mPanel; @@ -58,8 +56,6 @@ public class NonPagedTileLayout extends LinearLayout implements QSTileLayout, On @Override protected void onFinishInflate() { super.onFinishInflate(); - mQuickTiles = (QuickTileLayout) findViewById(R.id.quick_tile_layout); - mQuickTiles.setVisibility(View.GONE); TilePage page = (PagedTileLayout.TilePage) findViewById(R.id.tile_page); page.setMaxRows(3 /* First page only gets 3 */); mPages.add(page); @@ -95,7 +91,6 @@ public class NonPagedTileLayout extends LinearLayout implements QSTileLayout, On } private void distributeTiles() { - mQuickTiles.removeAllViews(); final int NP = mPages.size(); for (int i = 0; i < NP; i++) { mPages.get(i).removeAllViews(); @@ -124,7 +119,8 @@ public class NonPagedTileLayout extends LinearLayout implements QSTileLayout, On } @Override - public void updateResources() { + public boolean updateResources() { + return false; } @Override @@ -133,24 +129,20 @@ public class NonPagedTileLayout extends LinearLayout implements QSTileLayout, On case DragEvent.ACTION_DRAG_LOCATION: float x = event.getX(); float y = event.getY(); - if (contains(mQuickTiles, x, y)) { - // TODO: Reset to pre-drag state. - } else { - final int NP = mPages.size(); - for (int i = 0; i < NP; i++) { - TilePage page = mPages.get(i); - if (contains(page, x, y)) { - x -= page.getLeft(); - y -= page.getTop(); - final int NC = page.getChildCount(); - for (int j = 0; j < NC; j++) { - View child = page.getChildAt(j); - if (contains(child, x, y)) { - mPanel.tileSelected((QSTile) child.getTag(), mCurrentClip); - } + final int NP = mPages.size(); + for (int i = 0; i < NP; i++) { + TilePage page = mPages.get(i); + if (contains(page, x, y)) { + x -= page.getLeft(); + y -= page.getTop(); + final int NC = page.getChildCount(); + for (int j = 0; j < NC; j++) { + View child = page.getChildAt(j); + if (contains(child, x, y)) { + mPanel.tileSelected((QSTile) child.getTag(), mCurrentClip); } - break; } + break; } } break; diff --git a/packages/SystemUI/src/com/android/systemui/qs/customize/QSCustomizer.java b/packages/SystemUI/src/com/android/systemui/qs/customize/QSCustomizer.java index 4a7d67f9fe385..cc4ce706e6d61 100644 --- a/packages/SystemUI/src/com/android/systemui/qs/customize/QSCustomizer.java +++ b/packages/SystemUI/src/com/android/systemui/qs/customize/QSCustomizer.java @@ -45,7 +45,6 @@ import com.android.systemui.qs.customize.TileAdapter.TileSelectedListener; import com.android.systemui.statusbar.phone.PhoneStatusBar; import com.android.systemui.statusbar.phone.QSTileHost; import com.android.systemui.statusbar.phone.SystemUIDialog; -import com.android.systemui.tuner.QSPagingSwitch; import java.util.ArrayList; @@ -145,7 +144,8 @@ public class QSCustomizer extends LinearLayout implements OnMenuItemClickListene private void reset() { ArrayList tiles = new ArrayList<>(); - for (String tile : QSPagingSwitch.QS_PAGE_TILES.split(",")) { + String defTiles = mContext.getString(R.string.quick_settings_tiles_default); + for (String tile : defTiles.split(",")) { tiles.add(tile); } mQsPanel.setTiles(tiles); 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 a6a71439dbae2..6e22dde542d05 100644 --- a/packages/SystemUI/src/com/android/systemui/qs/customize/TileAdapter.java +++ b/packages/SystemUI/src/com/android/systemui/qs/customize/TileAdapter.java @@ -37,13 +37,11 @@ import android.widget.BaseAdapter; import android.widget.GridLayout; import android.widget.ImageView; import android.widget.TextView; - import com.android.systemui.R; import com.android.systemui.qs.QSTile; import com.android.systemui.qs.QSTile.Icon; import com.android.systemui.qs.external.CustomTile; import com.android.systemui.statusbar.phone.QSTileHost; -import com.android.systemui.tuner.QSPagingSwitch; import java.util.ArrayList; import java.util.Collection; @@ -74,8 +72,9 @@ public class TileAdapter extends BaseAdapter { } mCurrentTiles = tileSpecs; final TileGroup group = new TileGroup("com.android.settings", mContext); - // TODO: Pull this list from a more authoritative place. - String[] possibleTiles = QSPagingSwitch.QS_PAGE_TILES.split(","); + String possible = mContext.getString(R.string.quick_settings_tiles_default) + + ",user,hotspot,inversion"; + String[] possibleTiles = possible.split(","); for (int i = 0; i < possibleTiles.length; i++) { final String spec = possibleTiles[i]; if (spec.startsWith("q")) { diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/QSTileHost.java b/packages/SystemUI/src/com/android/systemui/statusbar/phone/QSTileHost.java index 29ad5d1cec3b9..7e278560ba55b 100644 --- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/QSTileHost.java +++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/QSTileHost.java @@ -295,6 +295,7 @@ public final class QSTileHost implements QSTile.Host, Tunable { if (mTiles.containsKey(tileSpec)) { QSTile tile = mTiles.get(tileSpec); if (DEBUG) Log.d(TAG, "Adding " + tile); + tile.removeCallbacks(); newTiles.put(tileSpec, tile); } else { if (DEBUG) Log.d(TAG, "Creating tile: " + tileSpec); diff --git a/packages/SystemUI/src/com/android/systemui/tuner/QSPagingSwitch.java b/packages/SystemUI/src/com/android/systemui/tuner/QSPagingSwitch.java deleted file mode 100644 index d19a825013edc..0000000000000 --- a/packages/SystemUI/src/com/android/systemui/tuner/QSPagingSwitch.java +++ /dev/null @@ -1,26 +0,0 @@ -package com.android.systemui.tuner; - -import android.content.Context; -import android.provider.Settings; -import android.util.AttributeSet; - -import com.android.systemui.statusbar.phone.QSTileHost; - -public class QSPagingSwitch extends TunerSwitch { - - public static final String QS_PAGE_TILES = - "dnd,cell,battery,user,rotation,flashlight,location," - + "hotspot,inversion,cast"; - - public QSPagingSwitch(Context context, AttributeSet attrs) { - super(context, attrs); - } - - @Override - protected boolean persistBoolean(boolean value) { - Settings.Secure.putString(getContext().getContentResolver(), QSTileHost.TILES_SETTING, - value ? QS_PAGE_TILES : "default"); - return super.persistBoolean(value); - } - -}