blob: 1195184050e3df6f54bca2823f67dc87d1a2e291 [file] [log] [blame]
Jason Monk6573ef22016-04-06 12:37:18 -04001/*
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
15package com.android.systemui.qs;
16
17import android.annotation.Nullable;
18import android.content.Context;
19import android.content.res.TypedArray;
20import android.database.DataSetObserver;
21import android.os.Handler;
22import android.util.AttributeSet;
23import android.view.View;
24import android.widget.LinearLayout;
25import android.widget.ListAdapter;
Gus Prevasab336792018-11-14 13:52:20 -050026
Jason Monk6573ef22016-04-06 12:37:18 -040027import 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 */
33public 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 Li92c91412016-05-04 17:06:29 -070041 private boolean mEnableAutoSizing;
Jason Monk6573ef22016-04-06 12:37:18 -040042
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 Li92c91412016-05-04 17:06:29 -070049 mEnableAutoSizing = a.getBoolean(R.styleable.AutoSizingList_enableAutoSizing, true);
50 a.recycle();
Jason Monk6573ef22016-04-06 12:37:18 -040051 }
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 Li92c91412016-05-04 17:06:29 -070067 int count = getItemCount(requestedHeight);
Jason Monk6573ef22016-04-06 12:37:18 -040068 if (mCount != count) {
69 postRebindChildren();
70 mCount = count;
71 }
72 }
73 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
74 }
75
Muyuan Li92c91412016-05-04 17:06:29 -070076 private int getItemCount(int requestedHeight) {
77 int desiredCount = getDesiredCount();
78 return mEnableAutoSizing ? Math.min(requestedHeight / mItemSize, desiredCount)
79 : desiredCount;
80 }
81
Jason Monk6573ef22016-04-06 12:37:18 -040082 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}