Jason Monk | 520ea06 | 2015-08-18 14:53:06 -0400 | [diff] [blame] | 1 | package com.android.systemui.qs; |
| 2 | |
| 3 | import android.content.Context; |
| 4 | import android.content.res.Resources; |
Fabian Kozynski | f92b881 | 2019-10-10 14:50:59 -0400 | [diff] [blame] | 5 | import android.provider.Settings; |
Jason Monk | caf3762 | 2015-08-18 12:33:50 -0400 | [diff] [blame] | 6 | import android.util.AttributeSet; |
Jason Monk | 520ea06 | 2015-08-18 14:53:06 -0400 | [diff] [blame] | 7 | import android.view.View; |
| 8 | import android.view.ViewGroup; |
Winson | c0d7058 | 2016-01-29 10:24:39 -0800 | [diff] [blame] | 9 | |
Jason Monk | 520ea06 | 2015-08-18 14:53:06 -0400 | [diff] [blame] | 10 | import com.android.systemui.R; |
Jason Monk | caf3762 | 2015-08-18 12:33:50 -0400 | [diff] [blame] | 11 | import com.android.systemui.qs.QSPanel.QSTileLayout; |
Jason Monk | 520ea06 | 2015-08-18 14:53:06 -0400 | [diff] [blame] | 12 | import com.android.systemui.qs.QSPanel.TileRecord; |
| 13 | |
| 14 | import java.util.ArrayList; |
| 15 | |
Jason Monk | caf3762 | 2015-08-18 12:33:50 -0400 | [diff] [blame] | 16 | public class TileLayout extends ViewGroup implements QSTileLayout { |
Jason Monk | 520ea06 | 2015-08-18 14:53:06 -0400 | [diff] [blame] | 17 | |
| 18 | private static final float TILE_ASPECT = 1.2f; |
| 19 | |
| 20 | private static final String TAG = "TileLayout"; |
| 21 | |
Jason Monk | caf3762 | 2015-08-18 12:33:50 -0400 | [diff] [blame] | 22 | protected int mColumns; |
Xiaohui Chen | 2f3551b | 2016-04-07 10:37:25 -0700 | [diff] [blame] | 23 | protected int mCellWidth; |
| 24 | protected int mCellHeight; |
Amin Shaikh | d620def | 2018-02-27 16:52:53 -0500 | [diff] [blame] | 25 | protected int mCellMarginHorizontal; |
| 26 | protected int mCellMarginVertical; |
Amin Shaikh | 9a6fa08 | 2018-03-29 17:27:26 -0400 | [diff] [blame] | 27 | protected int mSidePadding; |
Fabian Kozynski | 712ae39 | 2018-09-12 09:11:16 -0400 | [diff] [blame] | 28 | protected int mRows = 1; |
Jason Monk | 520ea06 | 2015-08-18 14:53:06 -0400 | [diff] [blame] | 29 | |
Jason Monk | caf3762 | 2015-08-18 12:33:50 -0400 | [diff] [blame] | 30 | protected final ArrayList<TileRecord> mRecords = new ArrayList<>(); |
Jason Monk | 6573ef2 | 2016-04-06 12:37:18 -0400 | [diff] [blame] | 31 | private int mCellMarginTop; |
Jason Monk | 1bec6af | 2016-05-31 15:40:58 -0400 | [diff] [blame] | 32 | private boolean mListening; |
Fabian Kozynski | 712ae39 | 2018-09-12 09:11:16 -0400 | [diff] [blame] | 33 | protected int mMaxAllowedRows = 3; |
Jason Monk | caf3762 | 2015-08-18 12:33:50 -0400 | [diff] [blame] | 34 | |
Fabian Kozynski | f92b881 | 2019-10-10 14:50:59 -0400 | [diff] [blame] | 35 | // Prototyping with less rows |
| 36 | private final boolean mLessRows; |
| 37 | |
Jason Monk | caf3762 | 2015-08-18 12:33:50 -0400 | [diff] [blame] | 38 | public TileLayout(Context context) { |
| 39 | this(context, null); |
| 40 | } |
| 41 | |
| 42 | public TileLayout(Context context, AttributeSet attrs) { |
| 43 | super(context, attrs); |
Julia Reynolds | 20aef8a | 2016-05-04 16:44:08 -0400 | [diff] [blame] | 44 | setFocusableInTouchMode(true); |
Fabian Kozynski | f92b881 | 2019-10-10 14:50:59 -0400 | [diff] [blame] | 45 | mLessRows = Settings.System.getInt(context.getContentResolver(), "qs_less_rows", 0) != 0; |
Jason Monk | 520ea06 | 2015-08-18 14:53:06 -0400 | [diff] [blame] | 46 | updateResources(); |
Fabian Kozynski | f92b881 | 2019-10-10 14:50:59 -0400 | [diff] [blame] | 47 | |
Jason Monk | 520ea06 | 2015-08-18 14:53:06 -0400 | [diff] [blame] | 48 | } |
| 49 | |
Jason Monk | caf3762 | 2015-08-18 12:33:50 -0400 | [diff] [blame] | 50 | @Override |
| 51 | public int getOffsetTop(TileRecord tile) { |
| 52 | return getTop(); |
| 53 | } |
| 54 | |
Jason Monk | 1bec6af | 2016-05-31 15:40:58 -0400 | [diff] [blame] | 55 | @Override |
| 56 | public void setListening(boolean listening) { |
| 57 | if (mListening == listening) return; |
| 58 | mListening = listening; |
| 59 | for (TileRecord record : mRecords) { |
| 60 | record.tile.setListening(this, mListening); |
| 61 | } |
| 62 | } |
| 63 | |
Jason Monk | caf3762 | 2015-08-18 12:33:50 -0400 | [diff] [blame] | 64 | public void addTile(TileRecord tile) { |
| 65 | mRecords.add(tile); |
Jason Monk | 1bec6af | 2016-05-31 15:40:58 -0400 | [diff] [blame] | 66 | tile.tile.setListening(this, mListening); |
Fabian Kozynski | 802279f | 2018-09-07 13:44:54 -0400 | [diff] [blame] | 67 | addTileView(tile); |
| 68 | } |
| 69 | |
| 70 | protected void addTileView(TileRecord tile) { |
Jason Monk | caf3762 | 2015-08-18 12:33:50 -0400 | [diff] [blame] | 71 | addView(tile.tileView); |
| 72 | } |
| 73 | |
| 74 | @Override |
| 75 | public void removeTile(TileRecord tile) { |
| 76 | mRecords.remove(tile); |
Jason Monk | 1bec6af | 2016-05-31 15:40:58 -0400 | [diff] [blame] | 77 | tile.tile.setListening(this, false); |
Jason Monk | caf3762 | 2015-08-18 12:33:50 -0400 | [diff] [blame] | 78 | removeView(tile.tileView); |
| 79 | } |
| 80 | |
Jason Monk | bd6dbb0 | 2015-09-03 15:46:25 -0400 | [diff] [blame] | 81 | public void removeAllViews() { |
Jason Monk | 1bec6af | 2016-05-31 15:40:58 -0400 | [diff] [blame] | 82 | for (TileRecord record : mRecords) { |
| 83 | record.tile.setListening(this, false); |
| 84 | } |
Jason Monk | bd6dbb0 | 2015-09-03 15:46:25 -0400 | [diff] [blame] | 85 | mRecords.clear(); |
| 86 | super.removeAllViews(); |
| 87 | } |
| 88 | |
Jason Monk | 9d02a43 | 2016-01-20 16:33:46 -0500 | [diff] [blame] | 89 | public boolean updateResources() { |
Jason Monk | 520ea06 | 2015-08-18 14:53:06 -0400 | [diff] [blame] | 90 | final Resources res = mContext.getResources(); |
| 91 | final int columns = Math.max(1, res.getInteger(R.integer.quick_settings_num_columns)); |
Jason Monk | 9d02a43 | 2016-01-20 16:33:46 -0500 | [diff] [blame] | 92 | mCellHeight = mContext.getResources().getDimensionPixelSize(R.dimen.qs_tile_height); |
Amin Shaikh | d620def | 2018-02-27 16:52:53 -0500 | [diff] [blame] | 93 | mCellMarginHorizontal = res.getDimensionPixelSize(R.dimen.qs_tile_margin_horizontal); |
| 94 | mCellMarginVertical= res.getDimensionPixelSize(R.dimen.qs_tile_margin_vertical); |
Jason Monk | 6573ef2 | 2016-04-06 12:37:18 -0400 | [diff] [blame] | 95 | mCellMarginTop = res.getDimensionPixelSize(R.dimen.qs_tile_margin_top); |
Amin Shaikh | 9a6fa08 | 2018-03-29 17:27:26 -0400 | [diff] [blame] | 96 | mSidePadding = res.getDimensionPixelOffset(R.dimen.qs_tile_layout_margin_side); |
Fabian Kozynski | 712ae39 | 2018-09-12 09:11:16 -0400 | [diff] [blame] | 97 | mMaxAllowedRows = Math.max(1, getResources().getInteger(R.integer.quick_settings_max_rows)); |
Fabian Kozynski | f92b881 | 2019-10-10 14:50:59 -0400 | [diff] [blame] | 98 | if (mLessRows) mMaxAllowedRows = Math.max(1, mMaxAllowedRows - 1); |
Jason Monk | 520ea06 | 2015-08-18 14:53:06 -0400 | [diff] [blame] | 99 | if (mColumns != columns) { |
| 100 | mColumns = columns; |
Jason Monk | 6573ef2 | 2016-04-06 12:37:18 -0400 | [diff] [blame] | 101 | requestLayout(); |
Jason Monk | 9d02a43 | 2016-01-20 16:33:46 -0500 | [diff] [blame] | 102 | return true; |
Jason Monk | 520ea06 | 2015-08-18 14:53:06 -0400 | [diff] [blame] | 103 | } |
Jason Monk | 9d02a43 | 2016-01-20 16:33:46 -0500 | [diff] [blame] | 104 | return false; |
Jason Monk | 94a1bf6 | 2015-10-20 08:43:36 -0700 | [diff] [blame] | 105 | } |
| 106 | |
Jason Monk | 520ea06 | 2015-08-18 14:53:06 -0400 | [diff] [blame] | 107 | @Override |
| 108 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
Fabian Kozynski | 712ae39 | 2018-09-12 09:11:16 -0400 | [diff] [blame] | 109 | // If called with AT_MOST, it will limit the number of rows. If called with UNSPECIFIED |
| 110 | // it will show all its tiles. In this case, the tiles have to be entered before the |
| 111 | // container is measured. Any change in the tiles, should trigger a remeasure. |
Jason Monk | 9d02a43 | 2016-01-20 16:33:46 -0500 | [diff] [blame] | 112 | final int numTiles = mRecords.size(); |
Fabian Kozynski | 6eaf3ac | 2018-10-01 15:59:05 -0400 | [diff] [blame] | 113 | final int width = MeasureSpec.getSize(widthMeasureSpec); |
Fabian Kozynski | 52bd8c2 | 2018-10-08 12:52:51 -0400 | [diff] [blame] | 114 | final int availableWidth = width - getPaddingStart() - getPaddingEnd(); |
Fabian Kozynski | 712ae39 | 2018-09-12 09:11:16 -0400 | [diff] [blame] | 115 | final int heightMode = MeasureSpec.getMode(heightMeasureSpec); |
| 116 | if (heightMode == MeasureSpec.UNSPECIFIED) { |
| 117 | mRows = (numTiles + mColumns - 1) / mColumns; |
| 118 | } |
Fabian Kozynski | 52bd8c2 | 2018-10-08 12:52:51 -0400 | [diff] [blame] | 119 | mCellWidth = |
| 120 | (availableWidth - mSidePadding * 2 - (mCellMarginHorizontal * mColumns)) / mColumns; |
Jason Monk | 520ea06 | 2015-08-18 14:53:06 -0400 | [diff] [blame] | 121 | |
Rohan Shah | 853cd8f6 | 2018-03-09 15:14:59 -0800 | [diff] [blame] | 122 | // Measure each QS tile. |
Jason Monk | 520ea06 | 2015-08-18 14:53:06 -0400 | [diff] [blame] | 123 | View previousView = this; |
| 124 | for (TileRecord record : mRecords) { |
Jason Monk | 520ea06 | 2015-08-18 14:53:06 -0400 | [diff] [blame] | 125 | if (record.tileView.getVisibility() == GONE) continue; |
Jason Monk | 9d02a43 | 2016-01-20 16:33:46 -0500 | [diff] [blame] | 126 | record.tileView.measure(exactly(mCellWidth), exactly(mCellHeight)); |
Jason Monk | 520ea06 | 2015-08-18 14:53:06 -0400 | [diff] [blame] | 127 | previousView = record.tileView.updateAccessibilityOrder(previousView); |
| 128 | } |
Rohan Shah | db2cfa3 | 2018-02-20 11:27:22 -0800 | [diff] [blame] | 129 | |
| 130 | // Only include the top margin in our measurement if we have more than 1 row to show. |
| 131 | // Otherwise, don't add the extra margin buffer at top. |
Fabian Kozynski | 712ae39 | 2018-09-12 09:11:16 -0400 | [diff] [blame] | 132 | int height = (mCellHeight + mCellMarginVertical) * mRows + |
| 133 | (mRows != 0 ? (mCellMarginTop - mCellMarginVertical) : 0); |
Jason Monk | cedcb94 | 2017-02-06 14:37:29 -0800 | [diff] [blame] | 134 | if (height < 0) height = 0; |
Rohan Shah | 853cd8f6 | 2018-03-09 15:14:59 -0800 | [diff] [blame] | 135 | |
Fabian Kozynski | 30b4d18 | 2019-05-08 09:19:52 -0400 | [diff] [blame] | 136 | setMeasuredDimension(width, height); |
Jason Monk | 520ea06 | 2015-08-18 14:53:06 -0400 | [diff] [blame] | 137 | } |
| 138 | |
Fabian Kozynski | 712ae39 | 2018-09-12 09:11:16 -0400 | [diff] [blame] | 139 | /** |
| 140 | * Determines the maximum number of rows that can be shown based on height. Clips at a minimum |
| 141 | * of 1 and a maximum of mMaxAllowedRows. |
| 142 | * |
| 143 | * @param heightMeasureSpec Available height. |
| 144 | * @param tilesCount Upper limit on the number of tiles to show. to prevent empty rows. |
| 145 | */ |
| 146 | public boolean updateMaxRows(int heightMeasureSpec, int tilesCount) { |
Fabian Kozynski | 72cd84a9 | 2019-04-15 14:09:43 -0400 | [diff] [blame] | 147 | final int availableHeight = MeasureSpec.getSize(heightMeasureSpec) - mCellMarginTop |
| 148 | + mCellMarginVertical; |
Fabian Kozynski | 712ae39 | 2018-09-12 09:11:16 -0400 | [diff] [blame] | 149 | final int previousRows = mRows; |
| 150 | mRows = availableHeight / (mCellHeight + mCellMarginVertical); |
| 151 | if (mRows >= mMaxAllowedRows) { |
| 152 | mRows = mMaxAllowedRows; |
| 153 | } else if (mRows <= 1) { |
| 154 | mRows = 1; |
| 155 | } |
| 156 | if (mRows > (tilesCount + mColumns - 1) / mColumns) { |
| 157 | mRows = (tilesCount + mColumns - 1) / mColumns; |
| 158 | } |
| 159 | return previousRows != mRows; |
| 160 | } |
| 161 | |
Amin Shaikh | 7e6e64c | 2018-02-07 16:07:24 -0500 | [diff] [blame] | 162 | @Override |
| 163 | public boolean hasOverlappingRendering() { |
| 164 | return false; |
| 165 | } |
| 166 | |
Fabian Kozynski | 802279f | 2018-09-07 13:44:54 -0400 | [diff] [blame] | 167 | protected static int exactly(int size) { |
Jason Monk | 520ea06 | 2015-08-18 14:53:06 -0400 | [diff] [blame] | 168 | return MeasureSpec.makeMeasureSpec(size, MeasureSpec.EXACTLY); |
| 169 | } |
| 170 | |
Fabian Kozynski | 802279f | 2018-09-07 13:44:54 -0400 | [diff] [blame] | 171 | |
| 172 | protected void layoutTileRecords(int numRecords) { |
Rohan Shah | 853cd8f6 | 2018-03-09 15:14:59 -0800 | [diff] [blame] | 173 | final boolean isRtl = getLayoutDirection() == LAYOUT_DIRECTION_RTL; |
Jason Monk | 9d02a43 | 2016-01-20 16:33:46 -0500 | [diff] [blame] | 174 | int row = 0; |
| 175 | int column = 0; |
Rohan Shah | 853cd8f6 | 2018-03-09 15:14:59 -0800 | [diff] [blame] | 176 | |
| 177 | // Layout each QS tile. |
Fabian Kozynski | 712ae39 | 2018-09-12 09:11:16 -0400 | [diff] [blame] | 178 | final int tilesToLayout = Math.min(numRecords, mRows * mColumns); |
| 179 | for (int i = 0; i < tilesToLayout; i++, column++) { |
Rohan Shah | 853cd8f6 | 2018-03-09 15:14:59 -0800 | [diff] [blame] | 180 | // If we reached the last column available to layout a tile, wrap back to the next row. |
Jason Monk | 9d02a43 | 2016-01-20 16:33:46 -0500 | [diff] [blame] | 181 | if (column == mColumns) { |
Rohan Shah | 853cd8f6 | 2018-03-09 15:14:59 -0800 | [diff] [blame] | 182 | column = 0; |
Jason Monk | 9d02a43 | 2016-01-20 16:33:46 -0500 | [diff] [blame] | 183 | row++; |
Jason Monk | 9d02a43 | 2016-01-20 16:33:46 -0500 | [diff] [blame] | 184 | } |
Rohan Shah | 853cd8f6 | 2018-03-09 15:14:59 -0800 | [diff] [blame] | 185 | |
| 186 | final TileRecord record = mRecords.get(i); |
Jason Monk | 9d02a43 | 2016-01-20 16:33:46 -0500 | [diff] [blame] | 187 | final int top = getRowTop(row); |
Amin Shaikh | a64bb52 | 2018-05-01 17:39:47 -0400 | [diff] [blame] | 188 | final int left = getColumnStart(isRtl ? mColumns - column - 1 : column); |
| 189 | final int right = left + mCellWidth; |
Jason Monk | 520ea06 | 2015-08-18 14:53:06 -0400 | [diff] [blame] | 190 | record.tileView.layout(left, top, right, top + record.tileView.getMeasuredHeight()); |
| 191 | } |
| 192 | } |
| 193 | |
Fabian Kozynski | 802279f | 2018-09-07 13:44:54 -0400 | [diff] [blame] | 194 | @Override |
| 195 | protected void onLayout(boolean changed, int l, int t, int r, int b) { |
| 196 | layoutTileRecords(mRecords.size()); |
| 197 | } |
| 198 | |
Jason Monk | 520ea06 | 2015-08-18 14:53:06 -0400 | [diff] [blame] | 199 | private int getRowTop(int row) { |
Amin Shaikh | d620def | 2018-02-27 16:52:53 -0500 | [diff] [blame] | 200 | return row * (mCellHeight + mCellMarginVertical) + mCellMarginTop; |
Jason Monk | 520ea06 | 2015-08-18 14:53:06 -0400 | [diff] [blame] | 201 | } |
| 202 | |
Fabian Kozynski | 802279f | 2018-09-07 13:44:54 -0400 | [diff] [blame] | 203 | protected int getColumnStart(int column) { |
Amin Shaikh | a64bb52 | 2018-05-01 17:39:47 -0400 | [diff] [blame] | 204 | return getPaddingStart() + mSidePadding + mCellMarginHorizontal / 2 + |
| 205 | column * (mCellWidth + mCellMarginHorizontal); |
Jason Monk | 520ea06 | 2015-08-18 14:53:06 -0400 | [diff] [blame] | 206 | } |
Fabian Kozynski | 802279f | 2018-09-07 13:44:54 -0400 | [diff] [blame] | 207 | |
| 208 | @Override |
| 209 | public int getNumVisibleTiles() { |
| 210 | return mRecords.size(); |
| 211 | } |
Jason Monk | 520ea06 | 2015-08-18 14:53:06 -0400 | [diff] [blame] | 212 | } |