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 | } |
Evan Laird | 92fbcb2 | 2018-02-16 15:25:53 +0000 | [diff] [blame] | 102 | int height = (mCellHeight + mCellMargin) * rows + (mCellMarginTop - mCellMargin); |
Jason Monk | cedcb94 | 2017-02-06 14:37:29 -0800 | [diff] [blame] | 103 | if (height < 0) height = 0; |
| 104 | setMeasuredDimension(width, height); |
Jason Monk | 520ea06 | 2015-08-18 14:53:06 -0400 | [diff] [blame] | 105 | } |
| 106 | |
Amin Shaikh | 7e6e64c | 2018-02-07 16:07:24 -0500 | [diff] [blame] | 107 | @Override |
| 108 | public boolean hasOverlappingRendering() { |
| 109 | return false; |
| 110 | } |
| 111 | |
Jason Monk | 520ea06 | 2015-08-18 14:53:06 -0400 | [diff] [blame] | 112 | private static int exactly(int size) { |
| 113 | return MeasureSpec.makeMeasureSpec(size, MeasureSpec.EXACTLY); |
| 114 | } |
| 115 | |
| 116 | @Override |
| 117 | protected void onLayout(boolean changed, int l, int t, int r, int b) { |
| 118 | final int w = getWidth(); |
| 119 | boolean isRtl = getLayoutDirection() == LAYOUT_DIRECTION_RTL; |
Jason Monk | 9d02a43 | 2016-01-20 16:33:46 -0500 | [diff] [blame] | 120 | int row = 0; |
| 121 | int column = 0; |
| 122 | for (int i = 0; i < mRecords.size(); i++, column++) { |
| 123 | if (column == mColumns) { |
| 124 | row++; |
| 125 | column -= mColumns; |
| 126 | } |
| 127 | TileRecord record = mRecords.get(i); |
| 128 | int left = getColumnStart(column); |
| 129 | final int top = getRowTop(row); |
Jason Monk | 520ea06 | 2015-08-18 14:53:06 -0400 | [diff] [blame] | 130 | int right; |
Jason Monk | 520ea06 | 2015-08-18 14:53:06 -0400 | [diff] [blame] | 131 | if (isRtl) { |
| 132 | right = w - left; |
Jason Monk | 9d02a43 | 2016-01-20 16:33:46 -0500 | [diff] [blame] | 133 | left = right - mCellWidth; |
Jason Monk | 520ea06 | 2015-08-18 14:53:06 -0400 | [diff] [blame] | 134 | } else { |
Jason Monk | 9d02a43 | 2016-01-20 16:33:46 -0500 | [diff] [blame] | 135 | right = left + mCellWidth; |
Jason Monk | 520ea06 | 2015-08-18 14:53:06 -0400 | [diff] [blame] | 136 | } |
| 137 | record.tileView.layout(left, top, right, top + record.tileView.getMeasuredHeight()); |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | private int getRowTop(int row) { |
Jason Monk | 6573ef2 | 2016-04-06 12:37:18 -0400 | [diff] [blame] | 142 | return row * (mCellHeight + mCellMargin) + mCellMarginTop; |
Jason Monk | 520ea06 | 2015-08-18 14:53:06 -0400 | [diff] [blame] | 143 | } |
| 144 | |
Jason Monk | 9d02a43 | 2016-01-20 16:33:46 -0500 | [diff] [blame] | 145 | private int getColumnStart(int column) { |
| 146 | return column * (mCellWidth + mCellMargin) + mCellMargin; |
Jason Monk | 520ea06 | 2015-08-18 14:53:06 -0400 | [diff] [blame] | 147 | } |
Jason Monk | 520ea06 | 2015-08-18 14:53:06 -0400 | [diff] [blame] | 148 | } |