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; |
Jason Monk | 520ea06 | 2015-08-18 14:53:06 -0400 | [diff] [blame] | 8 | import com.android.systemui.R; |
Jason Monk | caf3762 | 2015-08-18 12:33:50 -0400 | [diff] [blame] | 9 | import com.android.systemui.qs.QSPanel.QSTileLayout; |
Jason Monk | 520ea06 | 2015-08-18 14:53:06 -0400 | [diff] [blame] | 10 | import com.android.systemui.qs.QSPanel.TileRecord; |
| 11 | |
| 12 | import java.util.ArrayList; |
| 13 | |
Jason Monk | caf3762 | 2015-08-18 12:33:50 -0400 | [diff] [blame] | 14 | public class TileLayout extends ViewGroup implements QSTileLayout { |
Jason Monk | 520ea06 | 2015-08-18 14:53:06 -0400 | [diff] [blame] | 15 | |
| 16 | private static final float TILE_ASPECT = 1.2f; |
| 17 | |
| 18 | private static final String TAG = "TileLayout"; |
| 19 | |
Jason Monk | caf3762 | 2015-08-18 12:33:50 -0400 | [diff] [blame] | 20 | protected int mColumns; |
Jason Monk | 520ea06 | 2015-08-18 14:53:06 -0400 | [diff] [blame] | 21 | private int mCellWidth; |
| 22 | private int mCellHeight; |
Jason Monk | 9d02a43 | 2016-01-20 16:33:46 -0500 | [diff] [blame] | 23 | private int mCellMargin; |
Jason Monk | 520ea06 | 2015-08-18 14:53:06 -0400 | [diff] [blame] | 24 | |
Jason Monk | caf3762 | 2015-08-18 12:33:50 -0400 | [diff] [blame] | 25 | protected final ArrayList<TileRecord> mRecords = new ArrayList<>(); |
| 26 | |
| 27 | public TileLayout(Context context) { |
| 28 | this(context, null); |
| 29 | } |
| 30 | |
| 31 | public TileLayout(Context context, AttributeSet attrs) { |
| 32 | super(context, attrs); |
Jason Monk | 520ea06 | 2015-08-18 14:53:06 -0400 | [diff] [blame] | 33 | updateResources(); |
| 34 | } |
| 35 | |
Jason Monk | caf3762 | 2015-08-18 12:33:50 -0400 | [diff] [blame] | 36 | @Override |
| 37 | public int getOffsetTop(TileRecord tile) { |
| 38 | return getTop(); |
| 39 | } |
| 40 | |
| 41 | public void addTile(TileRecord tile) { |
| 42 | mRecords.add(tile); |
| 43 | addView(tile.tileView); |
| 44 | } |
| 45 | |
| 46 | @Override |
| 47 | public void removeTile(TileRecord tile) { |
| 48 | mRecords.remove(tile); |
| 49 | removeView(tile.tileView); |
| 50 | } |
| 51 | |
Jason Monk | bd6dbb0 | 2015-09-03 15:46:25 -0400 | [diff] [blame] | 52 | public void removeAllViews() { |
| 53 | mRecords.clear(); |
| 54 | super.removeAllViews(); |
| 55 | } |
| 56 | |
Jason Monk | 9d02a43 | 2016-01-20 16:33:46 -0500 | [diff] [blame] | 57 | public boolean updateResources() { |
Jason Monk | 520ea06 | 2015-08-18 14:53:06 -0400 | [diff] [blame] | 58 | final Resources res = mContext.getResources(); |
| 59 | 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] | 60 | mCellHeight = mContext.getResources().getDimensionPixelSize(R.dimen.qs_tile_height); |
| 61 | mCellMargin = res.getDimensionPixelSize(R.dimen.qs_tile_margin); |
Jason Monk | 520ea06 | 2015-08-18 14:53:06 -0400 | [diff] [blame] | 62 | if (mColumns != columns) { |
| 63 | mColumns = columns; |
| 64 | postInvalidate(); |
Jason Monk | 9d02a43 | 2016-01-20 16:33:46 -0500 | [diff] [blame] | 65 | return true; |
Jason Monk | 520ea06 | 2015-08-18 14:53:06 -0400 | [diff] [blame] | 66 | } |
Jason Monk | 9d02a43 | 2016-01-20 16:33:46 -0500 | [diff] [blame] | 67 | return false; |
Jason Monk | 94a1bf6 | 2015-10-20 08:43:36 -0700 | [diff] [blame] | 68 | } |
| 69 | |
Jason Monk | 520ea06 | 2015-08-18 14:53:06 -0400 | [diff] [blame] | 70 | @Override |
| 71 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
Jason Monk | 9d02a43 | 2016-01-20 16:33:46 -0500 | [diff] [blame] | 72 | final int numTiles = mRecords.size(); |
Jason Monk | 520ea06 | 2015-08-18 14:53:06 -0400 | [diff] [blame] | 73 | final int width = MeasureSpec.getSize(widthMeasureSpec); |
Jason Monk | 9d02a43 | 2016-01-20 16:33:46 -0500 | [diff] [blame] | 74 | final int rows = (numTiles + mColumns - 1) / mColumns; |
| 75 | mCellWidth = (width - (mCellMargin * (mColumns + 1))) / mColumns; |
Jason Monk | 520ea06 | 2015-08-18 14:53:06 -0400 | [diff] [blame] | 76 | |
| 77 | View previousView = this; |
| 78 | for (TileRecord record : mRecords) { |
Jason Monk | 520ea06 | 2015-08-18 14:53:06 -0400 | [diff] [blame] | 79 | if (record.tileView.getVisibility() == GONE) continue; |
Jason Monk | 9d02a43 | 2016-01-20 16:33:46 -0500 | [diff] [blame] | 80 | record.tileView.measure(exactly(mCellWidth), exactly(mCellHeight)); |
Jason Monk | 520ea06 | 2015-08-18 14:53:06 -0400 | [diff] [blame] | 81 | previousView = record.tileView.updateAccessibilityOrder(previousView); |
| 82 | } |
Jason Monk | 9d02a43 | 2016-01-20 16:33:46 -0500 | [diff] [blame] | 83 | setMeasuredDimension(width, (mCellHeight + mCellMargin) * rows + mCellMargin); |
Jason Monk | 520ea06 | 2015-08-18 14:53:06 -0400 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | private static int exactly(int size) { |
| 87 | return MeasureSpec.makeMeasureSpec(size, MeasureSpec.EXACTLY); |
| 88 | } |
| 89 | |
| 90 | @Override |
| 91 | protected void onLayout(boolean changed, int l, int t, int r, int b) { |
| 92 | final int w = getWidth(); |
| 93 | boolean isRtl = getLayoutDirection() == LAYOUT_DIRECTION_RTL; |
Jason Monk | 9d02a43 | 2016-01-20 16:33:46 -0500 | [diff] [blame] | 94 | int row = 0; |
| 95 | int column = 0; |
| 96 | for (int i = 0; i < mRecords.size(); i++, column++) { |
| 97 | if (column == mColumns) { |
| 98 | row++; |
| 99 | column -= mColumns; |
| 100 | } |
| 101 | TileRecord record = mRecords.get(i); |
| 102 | int left = getColumnStart(column); |
| 103 | final int top = getRowTop(row); |
Jason Monk | 520ea06 | 2015-08-18 14:53:06 -0400 | [diff] [blame] | 104 | int right; |
Jason Monk | 520ea06 | 2015-08-18 14:53:06 -0400 | [diff] [blame] | 105 | if (isRtl) { |
| 106 | right = w - left; |
Jason Monk | 9d02a43 | 2016-01-20 16:33:46 -0500 | [diff] [blame] | 107 | left = right - mCellWidth; |
Jason Monk | 520ea06 | 2015-08-18 14:53:06 -0400 | [diff] [blame] | 108 | } else { |
Jason Monk | 9d02a43 | 2016-01-20 16:33:46 -0500 | [diff] [blame] | 109 | right = left + mCellWidth; |
Jason Monk | 520ea06 | 2015-08-18 14:53:06 -0400 | [diff] [blame] | 110 | } |
| 111 | record.tileView.layout(left, top, right, top + record.tileView.getMeasuredHeight()); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | private int getRowTop(int row) { |
Jason Monk | 9d02a43 | 2016-01-20 16:33:46 -0500 | [diff] [blame] | 116 | return row * (mCellHeight + mCellMargin) + mCellMargin; |
Jason Monk | 520ea06 | 2015-08-18 14:53:06 -0400 | [diff] [blame] | 117 | } |
| 118 | |
Jason Monk | 9d02a43 | 2016-01-20 16:33:46 -0500 | [diff] [blame] | 119 | private int getColumnStart(int column) { |
| 120 | return column * (mCellWidth + mCellMargin) + mCellMargin; |
Jason Monk | 520ea06 | 2015-08-18 14:53:06 -0400 | [diff] [blame] | 121 | } |
Jason Monk | 520ea06 | 2015-08-18 14:53:06 -0400 | [diff] [blame] | 122 | } |