QuickSettingsContainerView.java revision d63c59786509aadd6a8d0c5cb45ed696339f16b7
1/* 2 * Copyright (C) 2012 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package com.android.systemui.statusbar.phone; 18 19import android.animation.LayoutTransition; 20import android.content.Context; 21import android.content.res.Resources; 22import android.util.AttributeSet; 23import android.view.ViewGroup; 24import android.widget.FrameLayout; 25 26import com.android.systemui.R; 27 28/** 29 * 30 */ 31class QuickSettingsContainerView extends FrameLayout { 32 33 // The number of columns in the QuickSettings grid 34 private int mNumColumns; 35 36 // The gap between tiles in the QuickSettings grid 37 private float mCellGap; 38 39 public QuickSettingsContainerView(Context context, AttributeSet attrs) { 40 super(context, attrs); 41 42 updateResources(); 43 } 44 45 @Override 46 protected void onFinishInflate() { 47 super.onFinishInflate(); 48 49 // TODO: Setup the layout transitions 50 LayoutTransition transitions = getLayoutTransition(); 51 } 52 53 void updateResources() { 54 Resources r = getContext().getResources(); 55 mCellGap = r.getDimension(R.dimen.quick_settings_cell_gap); 56 mNumColumns = r.getInteger(R.integer.quick_settings_num_columns); 57 requestLayout(); 58 } 59 60 @Override 61 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 62 // Calculate the cell width dynamically 63 int width = MeasureSpec.getSize(widthMeasureSpec); 64 int height = MeasureSpec.getSize(heightMeasureSpec); 65 int availableWidth = (int) (width - getPaddingLeft() - getPaddingRight() - 66 (mNumColumns - 1) * mCellGap); 67 float cellWidth = availableWidth / mNumColumns; 68 69 // Update each of the children's widths accordingly to the cell width 70 int N = getChildCount(); 71 int cellHeight = 0; 72 int cursor = 0; 73 for (int i = 0; i < N; ++i) { 74 // Update the child's width 75 QuickSettingsTileView v = (QuickSettingsTileView) getChildAt(i); 76 ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) v.getLayoutParams(); 77 int colSpan = v.getColumnSpan(); 78 lp.width = (int) ((colSpan * cellWidth) + (colSpan - 1) * mCellGap); 79 80 // Measure the child 81 v.setMinimumWidth(lp.width); 82 v.setMinimumHeight(lp.height); 83 int newWidthSpec = MeasureSpec.makeMeasureSpec(lp.width, MeasureSpec.AT_MOST); 84 int newHeightSpec = MeasureSpec.makeMeasureSpec(lp.height, MeasureSpec.AT_MOST); 85 v.measure(newWidthSpec, newHeightSpec); 86 87 // Save the cell height 88 if (cellHeight <= 0) { 89 cellHeight = v.getMeasuredHeight(); 90 } 91 cursor += colSpan; 92 } 93 94 // Set the measured dimensions. We always fill the tray width, but wrap to the height of 95 // all the tiles. 96 int numRows = (int) Math.ceil((float) cursor / mNumColumns); 97 int newHeight = (int) ((numRows * cellHeight) + ((numRows - 1) * mCellGap)) + 98 getPaddingTop() + getPaddingBottom(); 99 setMeasuredDimension(width, newHeight); 100 } 101 102 @Override 103 protected void onLayout(boolean changed, int left, int top, int right, int bottom) { 104 int N = getChildCount(); 105 int x = getPaddingLeft(); 106 int y = getPaddingTop(); 107 int cursor = 0; 108 for (int i = 0; i < N; ++i) { 109 QuickSettingsTileView v = (QuickSettingsTileView) getChildAt(i); 110 ViewGroup.LayoutParams lp = (ViewGroup.LayoutParams) v.getLayoutParams(); 111 if (v.getVisibility() != GONE) { 112 int col = cursor % mNumColumns; 113 int colSpan = v.getColumnSpan(); 114 int row = (int) (cursor / mNumColumns); 115 116 // Push the item to the next row if it can't fit on this one 117 if ((col + colSpan) > mNumColumns) { 118 x = getPaddingLeft(); 119 y += lp.height + mCellGap; 120 row++; 121 } 122 123 // Layout the container 124 v.layout(x, y, x + lp.width, y + lp.height); 125 126 // Offset the position by the cell gap or reset the position and cursor when we 127 // reach the end of the row 128 cursor += v.getColumnSpan(); 129 if (cursor < (((row + 1) * mNumColumns))) { 130 x += lp.width + mCellGap; 131 } else { 132 x = getPaddingLeft(); 133 y += lp.height + mCellGap; 134 } 135 } 136 } 137 } 138}