Jason Monk | 6573ef2 | 2016-04-06 12:37:18 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file |
| 5 | * except in compliance with the License. You may obtain a copy of the License at |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 9 | * Unless required by applicable law or agreed to in writing, software distributed under the |
| 10 | * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 11 | * KIND, either express or implied. See the License for the specific language governing |
| 12 | * permissions and limitations under the License. |
| 13 | */ |
| 14 | |
| 15 | package com.android.systemui.qs; |
| 16 | |
| 17 | import android.annotation.Nullable; |
| 18 | import android.content.Context; |
| 19 | import android.content.res.TypedArray; |
| 20 | import android.database.DataSetObserver; |
| 21 | import android.os.Handler; |
| 22 | import android.util.AttributeSet; |
| 23 | import android.view.View; |
| 24 | import android.widget.LinearLayout; |
| 25 | import android.widget.ListAdapter; |
Gus Prevas | ab33679 | 2018-11-14 13:52:20 -0500 | [diff] [blame] | 26 | |
Jason Monk | 6573ef2 | 2016-04-06 12:37:18 -0400 | [diff] [blame] | 27 | import com.android.systemui.R; |
| 28 | |
| 29 | /** |
| 30 | * Similar to a ListView, but it will show only as many items as fit on screen and |
| 31 | * bind those instead of scrolling. |
| 32 | */ |
| 33 | public class AutoSizingList extends LinearLayout { |
| 34 | |
| 35 | private static final String TAG = "AutoSizingList"; |
| 36 | private final int mItemSize; |
| 37 | private final Handler mHandler; |
| 38 | |
| 39 | private ListAdapter mAdapter; |
| 40 | private int mCount; |
Muyuan Li | 92c9141 | 2016-05-04 17:06:29 -0700 | [diff] [blame] | 41 | private boolean mEnableAutoSizing; |
Jason Monk | 6573ef2 | 2016-04-06 12:37:18 -0400 | [diff] [blame] | 42 | |
| 43 | public AutoSizingList(Context context, @Nullable AttributeSet attrs) { |
| 44 | super(context, attrs); |
| 45 | |
| 46 | mHandler = new Handler(); |
| 47 | TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.AutoSizingList); |
| 48 | mItemSize = a.getDimensionPixelSize(R.styleable.AutoSizingList_itemHeight, 0); |
Muyuan Li | 92c9141 | 2016-05-04 17:06:29 -0700 | [diff] [blame] | 49 | mEnableAutoSizing = a.getBoolean(R.styleable.AutoSizingList_enableAutoSizing, true); |
| 50 | a.recycle(); |
Jason Monk | 6573ef2 | 2016-04-06 12:37:18 -0400 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | public void setAdapter(ListAdapter adapter) { |
| 54 | if (mAdapter != null) { |
| 55 | mAdapter.unregisterDataSetObserver(mDataObserver); |
| 56 | } |
| 57 | mAdapter = adapter; |
| 58 | if (adapter != null) { |
| 59 | adapter.registerDataSetObserver(mDataObserver); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | @Override |
| 64 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 65 | int requestedHeight = MeasureSpec.getSize(heightMeasureSpec); |
| 66 | if (requestedHeight != 0) { |
Muyuan Li | 92c9141 | 2016-05-04 17:06:29 -0700 | [diff] [blame] | 67 | int count = getItemCount(requestedHeight); |
Jason Monk | 6573ef2 | 2016-04-06 12:37:18 -0400 | [diff] [blame] | 68 | if (mCount != count) { |
| 69 | postRebindChildren(); |
| 70 | mCount = count; |
| 71 | } |
| 72 | } |
| 73 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
| 74 | } |
| 75 | |
Muyuan Li | 92c9141 | 2016-05-04 17:06:29 -0700 | [diff] [blame] | 76 | private int getItemCount(int requestedHeight) { |
| 77 | int desiredCount = getDesiredCount(); |
| 78 | return mEnableAutoSizing ? Math.min(requestedHeight / mItemSize, desiredCount) |
| 79 | : desiredCount; |
| 80 | } |
| 81 | |
Jason Monk | 6573ef2 | 2016-04-06 12:37:18 -0400 | [diff] [blame] | 82 | private int getDesiredCount() { |
| 83 | return mAdapter != null ? mAdapter.getCount() : 0; |
| 84 | } |
| 85 | |
| 86 | private void postRebindChildren() { |
| 87 | mHandler.post(mBindChildren); |
| 88 | } |
| 89 | |
| 90 | private void rebindChildren() { |
| 91 | if (mAdapter == null) { |
| 92 | return; |
| 93 | } |
| 94 | for (int i = 0; i < mCount; i++) { |
| 95 | View v = i < getChildCount() ? getChildAt(i) : null; |
| 96 | View newView = mAdapter.getView(i, v, this); |
| 97 | if (newView != v) { |
| 98 | if (v != null) { |
| 99 | removeView(v); |
| 100 | } |
| 101 | addView(newView, i); |
| 102 | } |
| 103 | } |
| 104 | // Ditch extra views. |
| 105 | while (getChildCount() > mCount) { |
| 106 | removeViewAt(getChildCount() - 1); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | private final Runnable mBindChildren = new Runnable() { |
| 111 | @Override |
| 112 | public void run() { |
| 113 | rebindChildren(); |
| 114 | } |
| 115 | }; |
| 116 | |
| 117 | private final DataSetObserver mDataObserver = new DataSetObserver() { |
| 118 | @Override |
| 119 | public void onChanged() { |
| 120 | if (mCount > getDesiredCount()) { |
| 121 | mCount = getDesiredCount(); |
| 122 | } |
| 123 | postRebindChildren(); |
| 124 | } |
| 125 | |
| 126 | @Override |
| 127 | public void onInvalidated() { |
| 128 | postRebindChildren(); |
| 129 | } |
| 130 | }; |
| 131 | } |