|
| 1 | +/* |
| 2 | + * Copyright (C) 2014 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 | + |
| 17 | +package com.android.systemui.qs; |
| 18 | + |
| 19 | +import com.android.systemui.R; |
| 20 | + |
| 21 | +import android.content.Context; |
| 22 | +import android.content.res.TypedArray; |
| 23 | +import android.database.DataSetObserver; |
| 24 | +import android.util.AttributeSet; |
| 25 | +import android.view.View; |
| 26 | +import android.view.ViewGroup; |
| 27 | +import android.widget.BaseAdapter; |
| 28 | + |
| 29 | +import java.lang.ref.WeakReference; |
| 30 | + |
| 31 | +/** |
| 32 | + * A view that arranges it's children in a grid with a fixed number of evenly spaced columns. |
| 33 | + * |
| 34 | + * {@see android.widget.GridView} |
| 35 | + */ |
| 36 | +public class PseudoGridView extends ViewGroup { |
| 37 | + |
| 38 | + private int mNumColumns = 3; |
| 39 | + private int mVerticalSpacing; |
| 40 | + private int mHorizontalSpacing; |
| 41 | + |
| 42 | + public PseudoGridView(Context context, AttributeSet attrs) { |
| 43 | + super(context, attrs); |
| 44 | + |
| 45 | + final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.PseudoGridView); |
| 46 | + |
| 47 | + final int N = a.getIndexCount(); |
| 48 | + for (int i = 0; i < N; i++) { |
| 49 | + int attr = a.getIndex(i); |
| 50 | + switch (attr) { |
| 51 | + case R.styleable.PseudoGridView_numColumns: |
| 52 | + mNumColumns = a.getInt(attr, 3); |
| 53 | + break; |
| 54 | + case R.styleable.PseudoGridView_verticalSpacing: |
| 55 | + mVerticalSpacing = a.getDimensionPixelSize(attr, 0); |
| 56 | + break; |
| 57 | + case R.styleable.PseudoGridView_horizontalSpacing: |
| 58 | + mHorizontalSpacing = a.getDimensionPixelSize(attr, 0); |
| 59 | + break; |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + a.recycle(); |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 68 | + int widthMode = MeasureSpec.getMode(widthMeasureSpec); |
| 69 | + if (widthMode == MeasureSpec.UNSPECIFIED) { |
| 70 | + throw new UnsupportedOperationException("Needs a maximum width"); |
| 71 | + } |
| 72 | + int width = MeasureSpec.getSize(widthMeasureSpec); |
| 73 | + |
| 74 | + int childWidth = (width - (mNumColumns - 1) * mHorizontalSpacing) / mNumColumns; |
| 75 | + int childWidthSpec = MeasureSpec.makeMeasureSpec(childWidth, MeasureSpec.EXACTLY); |
| 76 | + int childHeightSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED); |
| 77 | + int totalHeight = 0; |
| 78 | + int children = getChildCount(); |
| 79 | + int rows = (children + mNumColumns - 1) / mNumColumns; |
| 80 | + for (int row = 0; row < rows; row++) { |
| 81 | + int startOfRow = row * mNumColumns; |
| 82 | + int endOfRow = Math.min(startOfRow + mNumColumns, children); |
| 83 | + int maxHeight = 0; |
| 84 | + for (int i = startOfRow; i < endOfRow; i++) { |
| 85 | + View child = getChildAt(i); |
| 86 | + child.measure(childWidthSpec, childHeightSpec); |
| 87 | + maxHeight = Math.max(maxHeight, child.getMeasuredHeight()); |
| 88 | + } |
| 89 | + int maxHeightSpec = MeasureSpec.makeMeasureSpec(maxHeight, MeasureSpec.EXACTLY); |
| 90 | + for (int i = startOfRow; i < endOfRow; i++) { |
| 91 | + View child = getChildAt(i); |
| 92 | + child.measure(childWidthSpec, maxHeightSpec); |
| 93 | + } |
| 94 | + totalHeight += maxHeight; |
| 95 | + if (row > 0) { |
| 96 | + totalHeight += mVerticalSpacing; |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + setMeasuredDimension(width, getDefaultSize(totalHeight, heightMeasureSpec)); |
| 101 | + } |
| 102 | + |
| 103 | + @Override |
| 104 | + protected void onLayout(boolean changed, int l, int t, int r, int b) { |
| 105 | + int children = getChildCount(); |
| 106 | + int rows = (children + mNumColumns - 1) / mNumColumns; |
| 107 | + int y = 0; |
| 108 | + for (int row = 0; row < rows; row++) { |
| 109 | + int x = 0; |
| 110 | + int maxHeight = 0; |
| 111 | + int startOfRow = row * mNumColumns; |
| 112 | + int endOfRow = Math.min(startOfRow + mNumColumns, children); |
| 113 | + for (int i = startOfRow; i < endOfRow; i++) { |
| 114 | + View child = getChildAt(i); |
| 115 | + int width = child.getMeasuredWidth(); |
| 116 | + int height = child.getMeasuredHeight(); |
| 117 | + child.layout(x, y, x + width, y + height); |
| 118 | + maxHeight = Math.max(maxHeight, height); |
| 119 | + x += width + mHorizontalSpacing; |
| 120 | + } |
| 121 | + y += maxHeight; |
| 122 | + if (row > 0) { |
| 123 | + y += mVerticalSpacing; |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * Bridges between a ViewGroup and a BaseAdapter. |
| 130 | + * <p> |
| 131 | + * Usage: {@code ViewGroupAdapterBridge.link(viewGroup, adapter)} |
| 132 | + * <br /> |
| 133 | + * After this call, the ViewGroup's children will be provided by the adapter. |
| 134 | + */ |
| 135 | + public static class ViewGroupAdapterBridge extends DataSetObserver { |
| 136 | + |
| 137 | + private final WeakReference<ViewGroup> mViewGroup; |
| 138 | + private final BaseAdapter mAdapter; |
| 139 | + private boolean mReleased; |
| 140 | + |
| 141 | + public static void link(ViewGroup viewGroup, BaseAdapter adapter) { |
| 142 | + new ViewGroupAdapterBridge(viewGroup, adapter); |
| 143 | + } |
| 144 | + |
| 145 | + private ViewGroupAdapterBridge(ViewGroup viewGroup, BaseAdapter adapter) { |
| 146 | + mViewGroup = new WeakReference<>(viewGroup); |
| 147 | + mAdapter = adapter; |
| 148 | + mReleased = false; |
| 149 | + mAdapter.registerDataSetObserver(this); |
| 150 | + refresh(); |
| 151 | + } |
| 152 | + |
| 153 | + private void refresh() { |
| 154 | + if (mReleased) { |
| 155 | + return; |
| 156 | + } |
| 157 | + ViewGroup viewGroup = mViewGroup.get(); |
| 158 | + if (viewGroup == null) { |
| 159 | + release(); |
| 160 | + return; |
| 161 | + } |
| 162 | + final int childCount = viewGroup.getChildCount(); |
| 163 | + final int adapterCount = mAdapter.getCount(); |
| 164 | + final int N = Math.max(childCount, adapterCount); |
| 165 | + for (int i = 0; i < N; i++) { |
| 166 | + if (i < adapterCount) { |
| 167 | + View oldView = null; |
| 168 | + if (i < childCount) { |
| 169 | + oldView = viewGroup.getChildAt(i); |
| 170 | + } |
| 171 | + View newView = mAdapter.getView(i, oldView, viewGroup); |
| 172 | + if (oldView == null) { |
| 173 | + // We ran out of existing views. Add it at the end. |
| 174 | + viewGroup.addView(newView); |
| 175 | + } else if (oldView != newView) { |
| 176 | + // We couldn't rebind the view. Replace it. |
| 177 | + viewGroup.removeViewAt(i); |
| 178 | + viewGroup.addView(newView, i); |
| 179 | + } |
| 180 | + } else { |
| 181 | + int lastIndex = viewGroup.getChildCount() - 1; |
| 182 | + viewGroup.removeViewAt(lastIndex); |
| 183 | + } |
| 184 | + } |
| 185 | + } |
| 186 | + |
| 187 | + @Override |
| 188 | + public void onChanged() { |
| 189 | + refresh(); |
| 190 | + } |
| 191 | + |
| 192 | + @Override |
| 193 | + public void onInvalidated() { |
| 194 | + release(); |
| 195 | + } |
| 196 | + |
| 197 | + private void release() { |
| 198 | + if (!mReleased) { |
| 199 | + mReleased = true; |
| 200 | + mAdapter.unregisterDataSetObserver(this); |
| 201 | + } |
| 202 | + } |
| 203 | + } |
| 204 | +} |
0 commit comments