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; |
Jason Monk | caf3762 | 2015-08-18 12:33:50 -0400 | [diff] [blame] | 5 | import android.util.AttributeSet; |
Jason Monk | 520ea06 | 2015-08-18 14:53:06 -0400 | [diff] [blame] | 6 | import android.view.View; |
| 7 | import android.view.ViewGroup; |
Winson | c0d7058 | 2016-01-29 10:24:39 -0800 | [diff] [blame] | 8 | |
Jason Monk | 520ea06 | 2015-08-18 14:53:06 -0400 | [diff] [blame] | 9 | import com.android.systemui.R; |
Jason Monk | caf3762 | 2015-08-18 12:33:50 -0400 | [diff] [blame] | 10 | import com.android.systemui.qs.QSPanel.QSTileLayout; |
Jason Monk | 520ea06 | 2015-08-18 14:53:06 -0400 | [diff] [blame] | 11 | import com.android.systemui.qs.QSPanel.TileRecord; |
| 12 | |
| 13 | import java.util.ArrayList; |
| 14 | |
Jason Monk | caf3762 | 2015-08-18 12:33:50 -0400 | [diff] [blame] | 15 | public class TileLayout extends ViewGroup implements QSTileLayout { |
Jason Monk | 520ea06 | 2015-08-18 14:53:06 -0400 | [diff] [blame] | 16 | |
| 17 | private static final float TILE_ASPECT = 1.2f; |
| 18 | |
| 19 | private static final String TAG = "TileLayout"; |
| 20 | |
Jason Monk | caf3762 | 2015-08-18 12:33:50 -0400 | [diff] [blame] | 21 | protected int mColumns; |
Xiaohui Chen | 2f3551b | 2016-04-07 10:37:25 -0700 | [diff] [blame] | 22 | protected int mCellWidth; |
| 23 | protected int mCellHeight; |
| 24 | protected int mCellMargin; |
Jason Monk | 520ea06 | 2015-08-18 14:53:06 -0400 | [diff] [blame] | 25 | |
Jason Monk | caf3762 | 2015-08-18 12:33:50 -0400 | [diff] [blame] | 26 | protected final ArrayList<TileRecord> mRecords = new ArrayList<>(); |
Jason Monk | 6573ef2 | 2016-04-06 12:37:18 -0400 | [diff] [blame] | 27 | private int mCellMarginTop; |
Jason Monk | 1bec6af | 2016-05-31 15:40:58 -0400 | [diff] [blame] | 28 | private boolean mListening; |
Jason Monk | caf3762 | 2015-08-18 12:33:50 -0400 | [diff] [blame] | 29 | |
| 30 | public TileLayout(Context context) { |
| 31 | this(context, null); |
| 32 | } |
| 33 | |
| 34 | public TileLayout(Context context, AttributeSet attrs) { |
| 35 | super(context, attrs); |
Julia Reynolds | 20aef8a | 2016-05-04 16:44:08 -0400 | [diff] [blame] | 36 | setFocusableInTouchMode(true); |
Jason Monk | 520ea06 | 2015-08-18 14:53:06 -0400 | [diff] [blame] | 37 | updateResources(); |
| 38 | } |
| 39 | |
Jason Monk | caf3762 | 2015-08-18 12:33:50 -0400 | [diff] [blame] | 40 | @Override |
| 41 | public int getOffsetTop(TileRecord tile) { |
| 42 | return getTop(); |
| 43 | } |
| 44 | |
Jason Monk | 1bec6af | 2016-05-31 15:40:58 -0400 | [diff] [blame] | 45 | @Override |
| 46 | public void setListening(boolean listening) { |
| 47 | if (mListening == listening) return; |
| 48 | mListening = listening; |
| 49 | for (TileRecord record : mRecords) { |
| 50 | record.tile.setListening(this, mListening); |
| 51 | } |
| 52 | } |
| 53 | |
Jason Monk | caf3762 | 2015-08-18 12:33:50 -0400 | [diff] [blame] | 54 | public void addTile(TileRecord tile) { |
| 55 | mRecords.add(tile); |
Jason Monk | 1bec6af | 2016-05-31 15:40:58 -0400 | [diff] [blame] | 56 | tile.tile.setListening(this, mListening); |
Jason Monk | caf3762 | 2015-08-18 12:33:50 -0400 | [diff] [blame] | 57 | addView(tile.tileView); |
| 58 | } |
| 59 | |
| 60 | @Override |
| 61 | public void removeTile(TileRecord tile) { |
| 62 | mRecords.remove(tile); |
Jason Monk | 1bec6af | 2016-05-31 15:40:58 -0400 | [diff] [blame] | 63 | tile.tile.setListening(this, false); |
Jason Monk | caf3762 | 2015-08-18 12:33:50 -0400 | [diff] [blame] | 64 | removeView(tile.tileView); |
| 65 | } |
| 66 | |
Jason Monk | bd6dbb0 | 2015-09-03 15:46:25 -0400 | [diff] [blame] | 67 | public void removeAllViews() { |
Jason Monk | 1bec6af | 2016-05-31 15:40:58 -0400 | [diff] [blame] | 68 | for (TileRecord record : mRecords) { |
| 69 | record.tile.setListening(this, false); |
| 70 | } |
Jason Monk | bd6dbb0 | 2015-09-03 15:46:25 -0400 | [diff] [blame] | 71 | mRecords.clear(); |
| 72 | super.removeAllViews(); |
| 73 | } |
| 74 | |
Jason Monk | 9d02a43 | 2016-01-20 16:33:46 -0500 | [diff] [blame] | 75 | public boolean updateResources() { |
Jason Monk | 520ea06 | 2015-08-18 14:53:06 -0400 | [diff] [blame] | 76 | final Resources res = mContext.getResources(); |
| 77 | 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] | 78 | mCellHeight = mContext.getResources().getDimensionPixelSize(R.dimen.qs_tile_height); |
| 79 | mCellMargin = res.getDimensionPixelSize(R.dimen.qs_tile_margin); |
Jason Monk | 6573ef2 | 2016-04-06 12:37:18 -0400 | [diff] [blame] | 80 | mCellMarginTop = res.getDimensionPixelSize(R.dimen.qs_tile_margin_top); |
Jason Monk | 520ea06 | 2015-08-18 14:53:06 -0400 | [diff] [blame] | 81 | if (mColumns != columns) { |
| 82 | mColumns = columns; |
Jason Monk | 6573ef2 | 2016-04-06 12:37:18 -0400 | [diff] [blame] | 83 | requestLayout(); |
Jason Monk | 9d02a43 | 2016-01-20 16:33:46 -0500 | [diff] [blame] | 84 | return true; |
Jason Monk | 520ea06 | 2015-08-18 14:53:06 -0400 | [diff] [blame] | 85 | } |
Jason Monk | 9d02a43 | 2016-01-20 16:33:46 -0500 | [diff] [blame] | 86 | return false; |
Jason Monk | 94a1bf6 | 2015-10-20 08:43:36 -0700 | [diff] [blame] | 87 | } |
| 88 | |
Jason Monk | 520ea06 | 2015-08-18 14:53:06 -0400 | [diff] [blame] | 89 | @Override |
| 90 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
Jason Monk | 9d02a43 | 2016-01-20 16:33:46 -0500 | [diff] [blame] | 91 | final int numTiles = mRecords.size(); |
Jason Monk | 520ea06 | 2015-08-18 14:53:06 -0400 | [diff] [blame] | 92 | final int width = MeasureSpec.getSize(widthMeasureSpec); |
Jason Monk | 9d02a43 | 2016-01-20 16:33:46 -0500 | [diff] [blame] | 93 | final int rows = (numTiles + mColumns - 1) / mColumns; |
| 94 | mCellWidth = (width - (mCellMargin * (mColumns + 1))) / mColumns; |
Jason Monk | 520ea06 | 2015-08-18 14:53:06 -0400 | [diff] [blame] | 95 | |
| 96 | View previousView = this; |
| 97 | for (TileRecord record : mRecords) { |
Jason Monk | 520ea06 | 2015-08-18 14:53:06 -0400 | [diff] [blame] | 98 | if (record.tileView.getVisibility() == GONE) continue; |
Jason Monk | 9d02a43 | 2016-01-20 16:33:46 -0500 | [diff] [blame] | 99 | record.tileView.measure(exactly(mCellWidth), exactly(mCellHeight)); |
Jason Monk | 520ea06 | 2015-08-18 14:53:06 -0400 | [diff] [blame] | 100 | previousView = record.tileView.updateAccessibilityOrder(previousView); |
| 101 | } |
Rohan Shah | db2cfa3 | 2018-02-20 11:27:22 -0800 | [diff] [blame] | 102 | |
| 103 | // Only include the top margin in our measurement if we have more than 1 row to show. |
| 104 | // Otherwise, don't add the extra margin buffer at top. |
| 105 | int height = (mCellHeight + mCellMargin) * rows + |
| 106 | (rows != 0 ? (mCellMarginTop - mCellMargin) : 0); |
Jason Monk | cedcb94 | 2017-02-06 14:37:29 -0800 | [diff] [blame] | 107 | if (height < 0) height = 0; |
| 108 | setMeasuredDimension(width, height); |
Jason Monk | 520ea06 | 2015-08-18 14:53:06 -0400 | [diff] [blame] | 109 | } |
| 110 | |
Amin Shaikh | 7e6e64c | 2018-02-07 16:07:24 -0500 | [diff] [blame] | 111 | @Override |
| 112 | public boolean hasOverlappingRendering() { |
| 113 | return false; |
| 114 | } |
| 115 | |
Jason Monk | 520ea06 | 2015-08-18 14:53:06 -0400 | [diff] [blame] | 116 | private static int exactly(int size) { |
| 117 | return MeasureSpec.makeMeasureSpec(size, MeasureSpec.EXACTLY); |
| 118 | } |
| 119 | |
| 120 | @Override |
| 121 | protected void onLayout(boolean changed, int l, int t, int r, int b) { |
| 122 | final int w = getWidth(); |
| 123 | boolean isRtl = getLayoutDirection() == LAYOUT_DIRECTION_RTL; |
Jason Monk | 9d02a43 | 2016-01-20 16:33:46 -0500 | [diff] [blame] | 124 | int row = 0; |
| 125 | int column = 0; |
| 126 | for (int i = 0; i < mRecords.size(); i++, column++) { |
| 127 | if (column == mColumns) { |
| 128 | row++; |
| 129 | column -= mColumns; |
| 130 | } |
| 131 | TileRecord record = mRecords.get(i); |
| 132 | int left = getColumnStart(column); |
| 133 | final int top = getRowTop(row); |
Jason Monk | 520ea06 | 2015-08-18 14:53:06 -0400 | [diff] [blame] | 134 | int right; |
Jason Monk | 520ea06 | 2015-08-18 14:53:06 -0400 | [diff] [blame] | 135 | if (isRtl) { |
| 136 | right = w - left; |
Jason Monk | 9d02a43 | 2016-01-20 16:33:46 -0500 | [diff] [blame] | 137 | left = right - mCellWidth; |
Jason Monk | 520ea06 | 2015-08-18 14:53:06 -0400 | [diff] [blame] | 138 | } else { |
Jason Monk | 9d02a43 | 2016-01-20 16:33:46 -0500 | [diff] [blame] | 139 | right = left + mCellWidth; |
Jason Monk | 520ea06 | 2015-08-18 14:53:06 -0400 | [diff] [blame] | 140 | } |
| 141 | record.tileView.layout(left, top, right, top + record.tileView.getMeasuredHeight()); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | private int getRowTop(int row) { |
Jason Monk | 6573ef2 | 2016-04-06 12:37:18 -0400 | [diff] [blame] | 146 | return row * (mCellHeight + mCellMargin) + mCellMarginTop; |
Jason Monk | 520ea06 | 2015-08-18 14:53:06 -0400 | [diff] [blame] | 147 | } |
| 148 | |
Jason Monk | 9d02a43 | 2016-01-20 16:33:46 -0500 | [diff] [blame] | 149 | private int getColumnStart(int column) { |
| 150 | return column * (mCellWidth + mCellMargin) + mCellMargin; |
Jason Monk | 520ea06 | 2015-08-18 14:53:06 -0400 | [diff] [blame] | 151 | } |
Jason Monk | 520ea06 | 2015-08-18 14:53:06 -0400 | [diff] [blame] | 152 | } |