blob: 2c5cb89b3531e3904a1ffe6c9d81115fc1091942 [file] [log] [blame]
Jason Monkcaf37622015-08-18 12:33:50 -04001package com.android.systemui.qs;
2
3import android.content.Context;
4import android.util.AttributeSet;
5import android.util.Log;
6import android.view.LayoutInflater;
7import android.view.View;
8import android.view.ViewGroup;
Jason Monkcaf37622015-08-18 12:33:50 -04009import com.android.internal.widget.PagerAdapter;
10import com.android.internal.widget.ViewPager;
11import com.android.systemui.R;
12import com.android.systemui.qs.QSPanel.QSTileLayout;
13import com.android.systemui.qs.QSPanel.TileRecord;
14
15import java.util.ArrayList;
16
17public class PagedTileLayout extends ViewPager implements QSTileLayout {
18
19 private static final boolean DEBUG = false;
20
21 private static final String TAG = "PagedTileLayout";
22
23 private final ArrayList<TileRecord> mTiles = new ArrayList<TileRecord>();
24 private final ArrayList<TilePage> mPages = new ArrayList<TilePage>();
25
Jason Monkcaf37622015-08-18 12:33:50 -040026 private PageIndicator mPageIndicator;
27
28 private int mNumPages;
Jason Monk21428432016-02-10 16:42:38 -050029 private View mDecorGroup;
Jason Monk162011e2016-02-19 08:11:55 -050030 private PageListener mPageListener;
Jason Monkcaf37622015-08-18 12:33:50 -040031
32 public PagedTileLayout(Context context, AttributeSet attrs) {
33 super(context, attrs);
34 setAdapter(mAdapter);
35 setOnPageChangeListener(new OnPageChangeListener() {
36 @Override
37 public void onPageSelected(int position) {
38 if (mPageIndicator == null) return;
39 mPageIndicator.setLocation(position);
Jason Monk162011e2016-02-19 08:11:55 -050040 if (mPageListener != null) {
Jason Monk66eaf312016-02-25 12:29:29 -050041 mPageListener.onPageChanged(position == 0);
Jason Monk162011e2016-02-19 08:11:55 -050042 }
Jason Monkcaf37622015-08-18 12:33:50 -040043 }
44
45 @Override
Jason Monk162011e2016-02-19 08:11:55 -050046 public void onPageScrolled(int position, float positionOffset,
47 int positionOffsetPixels) {
Jason Monkcaf37622015-08-18 12:33:50 -040048 if (mPageIndicator == null) return;
49 mPageIndicator.setLocation(position + positionOffset);
Jason Monk66eaf312016-02-25 12:29:29 -050050 if (mPageListener != null) {
51 mPageListener.onPageChanged(position == 0 && positionOffsetPixels == 0);
52 }
Jason Monkcaf37622015-08-18 12:33:50 -040053 }
54
55 @Override
56 public void onPageScrollStateChanged(int state) {
57 }
58 });
59 setCurrentItem(0);
60 }
61
62 @Override
Jason Monkc5bdafb2016-02-25 16:24:21 -050063 public boolean hasOverlappingRendering() {
64 return false;
65 }
66
67 @Override
Jason Monkcaf37622015-08-18 12:33:50 -040068 protected void onFinishInflate() {
69 super.onFinishInflate();
70 mPageIndicator = (PageIndicator) findViewById(R.id.page_indicator);
Jason Monk21428432016-02-10 16:42:38 -050071 mDecorGroup = findViewById(R.id.page_decor);
72 ((LayoutParams) mDecorGroup.getLayoutParams()).isDecor = true;
Jason Monkcaf37622015-08-18 12:33:50 -040073
Jason Monke4e69302016-01-20 11:27:15 -050074 mPages.add((TilePage) LayoutInflater.from(mContext)
75 .inflate(R.layout.qs_paged_page, this, false));
Jason Monkcaf37622015-08-18 12:33:50 -040076 }
77
78 @Override
79 public int getOffsetTop(TileRecord tile) {
Jason Monk9d02a432016-01-20 16:33:46 -050080 return ((ViewGroup) tile.tileView.getParent()).getTop() + getTop();
Jason Monkcaf37622015-08-18 12:33:50 -040081 }
82
83 @Override
Jason Monkcaf37622015-08-18 12:33:50 -040084 public void addTile(TileRecord tile) {
85 mTiles.add(tile);
Jason Monk9d02a432016-01-20 16:33:46 -050086 postDistributeTiles();
Jason Monkcaf37622015-08-18 12:33:50 -040087 }
88
89 @Override
90 public void removeTile(TileRecord tile) {
91 if (mTiles.remove(tile)) {
Jason Monk9d02a432016-01-20 16:33:46 -050092 postDistributeTiles();
Jason Monkcaf37622015-08-18 12:33:50 -040093 }
94 }
95
Jason Monk162011e2016-02-19 08:11:55 -050096 public void setPageListener(PageListener listener) {
97 mPageListener = listener;
98 }
99
Jason Monk9d02a432016-01-20 16:33:46 -0500100 private void postDistributeTiles() {
101 removeCallbacks(mDistribute);
102 post(mDistribute);
103 }
104
Jason Monkcaf37622015-08-18 12:33:50 -0400105 private void distributeTiles() {
106 if (DEBUG) Log.d(TAG, "Distributing tiles");
Jason Monkcaf37622015-08-18 12:33:50 -0400107 final int NP = mPages.size();
108 for (int i = 0; i < NP; i++) {
Jason Monk9d02a432016-01-20 16:33:46 -0500109 mPages.get(i).removeAllViews();
Jason Monkcaf37622015-08-18 12:33:50 -0400110 }
111 int index = 0;
112 final int NT = mTiles.size();
113 for (int i = 0; i < NT; i++) {
114 TileRecord tile = mTiles.get(i);
Jason Monkcaf37622015-08-18 12:33:50 -0400115 if (mPages.get(index).isFull()) {
116 if (++index == mPages.size()) {
117 if (DEBUG) Log.d(TAG, "Adding page for " + tile.tile.getClass().getSimpleName());
118 mPages.add((TilePage) LayoutInflater.from(mContext)
119 .inflate(R.layout.qs_paged_page, this, false));
120 }
121 }
122 if (DEBUG) Log.d(TAG, "Adding " + tile.tile.getClass().getSimpleName() + " to "
123 + index);
124 mPages.get(index).addTile(tile);
125 }
126 if (mNumPages != index + 1) {
127 mNumPages = index + 1;
128 mPageIndicator.setNumPages(mNumPages);
129 mAdapter.notifyDataSetChanged();
130 }
131 }
132
133 @Override
Jason Monk9d02a432016-01-20 16:33:46 -0500134 public boolean updateResources() {
135 boolean changed = false;
Jason Monkcaf37622015-08-18 12:33:50 -0400136 for (int i = 0; i < mPages.size(); i++) {
Jason Monk9d02a432016-01-20 16:33:46 -0500137 changed |= mPages.get(i).updateResources();
Jason Monkcaf37622015-08-18 12:33:50 -0400138 }
Jason Monk9d02a432016-01-20 16:33:46 -0500139 if (changed) {
140 distributeTiles();
141 }
142 return changed;
Jason Monkcaf37622015-08-18 12:33:50 -0400143 }
144
145 @Override
146 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
147 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
148 // The ViewPager likes to eat all of the space, instead force it to wrap to the max height
149 // of the pages.
150 int maxHeight = 0;
151 final int N = getChildCount();
152 for (int i = 0; i < N; i++) {
153 int height = getChildAt(i).getMeasuredHeight();
154 if (height > maxHeight) {
155 maxHeight = height;
156 }
157 }
Jason Monk21428432016-02-10 16:42:38 -0500158 setMeasuredDimension(getMeasuredWidth(), maxHeight + mDecorGroup.getMeasuredHeight());
Jason Monkcaf37622015-08-18 12:33:50 -0400159 }
160
Jason Monk9d02a432016-01-20 16:33:46 -0500161 private final Runnable mDistribute = new Runnable() {
162 @Override
163 public void run() {
164 distributeTiles();
165 }
166 };
167
Jason Monkcaf37622015-08-18 12:33:50 -0400168 public static class TilePage extends TileLayout {
Jason Monk94a1bf62015-10-20 08:43:36 -0700169 private int mMaxRows = 3;
Jason Monkcaf37622015-08-18 12:33:50 -0400170
171 public TilePage(Context context, AttributeSet attrs) {
172 super(context, attrs);
Jason Monkb9c00192015-10-07 11:45:33 -0400173 updateResources();
Jason Monkcaf37622015-08-18 12:33:50 -0400174 }
175
Jason Monk9d02a432016-01-20 16:33:46 -0500176 @Override
177 public boolean updateResources() {
178 if (super.updateResources()) {
179 mMaxRows = mColumns != 3 ? 2 : 3;
180 return true;
181 }
182 return false;
183 }
184
Jason Monkbd6dbb02015-09-03 15:46:25 -0400185 public void setMaxRows(int maxRows) {
186 mMaxRows = maxRows;
187 }
188
Jason Monkbd6dbb02015-09-03 15:46:25 -0400189 public boolean isFull() {
Jason Monkcaf37622015-08-18 12:33:50 -0400190 return mRecords.size() >= mColumns * mMaxRows;
191 }
192 }
193
194 private final PagerAdapter mAdapter = new PagerAdapter() {
195 public void destroyItem(ViewGroup container, int position, Object object) {
196 if (DEBUG) Log.d(TAG, "Destantiating " + position);
197 // TODO: Find way to clean up the extra pages.
198 container.removeView((View) object);
199 }
200
201 public Object instantiateItem(ViewGroup container, int position) {
202 if (DEBUG) Log.d(TAG, "Instantiating " + position);
Jason Monke4e69302016-01-20 11:27:15 -0500203 ViewGroup view = mPages.get(position);
Jason Monkcaf37622015-08-18 12:33:50 -0400204 container.addView(view);
205 return view;
206 }
207
208 @Override
209 public int getCount() {
210 return mNumPages;
211 }
212
213 @Override
214 public boolean isViewFromObject(View view, Object object) {
215 return view == object;
216 }
217 };
Jason Monk162011e2016-02-19 08:11:55 -0500218
219 public interface PageListener {
Jason Monk66eaf312016-02-25 12:29:29 -0500220 void onPageChanged(boolean isFirst);
Jason Monk162011e2016-02-19 08:11:55 -0500221 }
Jason Monkcaf37622015-08-18 12:33:50 -0400222}