blob: 3b26aa6d09f39b5eaa191e3bd3940adf657e799b [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
63 protected void onFinishInflate() {
64 super.onFinishInflate();
65 mPageIndicator = (PageIndicator) findViewById(R.id.page_indicator);
Jason Monk21428432016-02-10 16:42:38 -050066 mDecorGroup = findViewById(R.id.page_decor);
67 ((LayoutParams) mDecorGroup.getLayoutParams()).isDecor = true;
Jason Monkcaf37622015-08-18 12:33:50 -040068
Jason Monke4e69302016-01-20 11:27:15 -050069 mPages.add((TilePage) LayoutInflater.from(mContext)
70 .inflate(R.layout.qs_paged_page, this, false));
Jason Monkcaf37622015-08-18 12:33:50 -040071 }
72
73 @Override
74 public int getOffsetTop(TileRecord tile) {
Jason Monk9d02a432016-01-20 16:33:46 -050075 return ((ViewGroup) tile.tileView.getParent()).getTop() + getTop();
Jason Monkcaf37622015-08-18 12:33:50 -040076 }
77
78 @Override
Jason Monkcaf37622015-08-18 12:33:50 -040079 public void addTile(TileRecord tile) {
80 mTiles.add(tile);
Jason Monk9d02a432016-01-20 16:33:46 -050081 postDistributeTiles();
Jason Monkcaf37622015-08-18 12:33:50 -040082 }
83
84 @Override
85 public void removeTile(TileRecord tile) {
86 if (mTiles.remove(tile)) {
Jason Monk9d02a432016-01-20 16:33:46 -050087 postDistributeTiles();
Jason Monkcaf37622015-08-18 12:33:50 -040088 }
89 }
90
Jason Monk162011e2016-02-19 08:11:55 -050091 public void setPageListener(PageListener listener) {
92 mPageListener = listener;
93 }
94
Jason Monk9d02a432016-01-20 16:33:46 -050095 private void postDistributeTiles() {
96 removeCallbacks(mDistribute);
97 post(mDistribute);
98 }
99
Jason Monkcaf37622015-08-18 12:33:50 -0400100 private void distributeTiles() {
101 if (DEBUG) Log.d(TAG, "Distributing tiles");
Jason Monkcaf37622015-08-18 12:33:50 -0400102 final int NP = mPages.size();
103 for (int i = 0; i < NP; i++) {
Jason Monk9d02a432016-01-20 16:33:46 -0500104 mPages.get(i).removeAllViews();
Jason Monkcaf37622015-08-18 12:33:50 -0400105 }
106 int index = 0;
107 final int NT = mTiles.size();
108 for (int i = 0; i < NT; i++) {
109 TileRecord tile = mTiles.get(i);
Jason Monkcaf37622015-08-18 12:33:50 -0400110 if (mPages.get(index).isFull()) {
111 if (++index == mPages.size()) {
112 if (DEBUG) Log.d(TAG, "Adding page for " + tile.tile.getClass().getSimpleName());
113 mPages.add((TilePage) LayoutInflater.from(mContext)
114 .inflate(R.layout.qs_paged_page, this, false));
115 }
116 }
117 if (DEBUG) Log.d(TAG, "Adding " + tile.tile.getClass().getSimpleName() + " to "
118 + index);
119 mPages.get(index).addTile(tile);
120 }
121 if (mNumPages != index + 1) {
122 mNumPages = index + 1;
123 mPageIndicator.setNumPages(mNumPages);
124 mAdapter.notifyDataSetChanged();
125 }
126 }
127
128 @Override
Jason Monk9d02a432016-01-20 16:33:46 -0500129 public boolean updateResources() {
130 boolean changed = false;
Jason Monkcaf37622015-08-18 12:33:50 -0400131 for (int i = 0; i < mPages.size(); i++) {
Jason Monk9d02a432016-01-20 16:33:46 -0500132 changed |= mPages.get(i).updateResources();
Jason Monkcaf37622015-08-18 12:33:50 -0400133 }
Jason Monk9d02a432016-01-20 16:33:46 -0500134 if (changed) {
135 distributeTiles();
136 }
137 return changed;
Jason Monkcaf37622015-08-18 12:33:50 -0400138 }
139
140 @Override
141 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
142 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
143 // The ViewPager likes to eat all of the space, instead force it to wrap to the max height
144 // of the pages.
145 int maxHeight = 0;
146 final int N = getChildCount();
147 for (int i = 0; i < N; i++) {
148 int height = getChildAt(i).getMeasuredHeight();
149 if (height > maxHeight) {
150 maxHeight = height;
151 }
152 }
Jason Monk21428432016-02-10 16:42:38 -0500153 setMeasuredDimension(getMeasuredWidth(), maxHeight + mDecorGroup.getMeasuredHeight());
Jason Monkcaf37622015-08-18 12:33:50 -0400154 }
155
Jason Monk9d02a432016-01-20 16:33:46 -0500156 private final Runnable mDistribute = new Runnable() {
157 @Override
158 public void run() {
159 distributeTiles();
160 }
161 };
162
Jason Monkcaf37622015-08-18 12:33:50 -0400163 public static class TilePage extends TileLayout {
Jason Monk94a1bf62015-10-20 08:43:36 -0700164 private int mMaxRows = 3;
Jason Monkcaf37622015-08-18 12:33:50 -0400165
166 public TilePage(Context context, AttributeSet attrs) {
167 super(context, attrs);
Jason Monkb9c00192015-10-07 11:45:33 -0400168 updateResources();
Jason Monkcaf37622015-08-18 12:33:50 -0400169 }
170
Jason Monk9d02a432016-01-20 16:33:46 -0500171 @Override
172 public boolean updateResources() {
173 if (super.updateResources()) {
174 mMaxRows = mColumns != 3 ? 2 : 3;
175 return true;
176 }
177 return false;
178 }
179
Jason Monkbd6dbb02015-09-03 15:46:25 -0400180 public void setMaxRows(int maxRows) {
181 mMaxRows = maxRows;
182 }
183
Jason Monkbd6dbb02015-09-03 15:46:25 -0400184 public boolean isFull() {
Jason Monkcaf37622015-08-18 12:33:50 -0400185 return mRecords.size() >= mColumns * mMaxRows;
186 }
187 }
188
189 private final PagerAdapter mAdapter = new PagerAdapter() {
190 public void destroyItem(ViewGroup container, int position, Object object) {
191 if (DEBUG) Log.d(TAG, "Destantiating " + position);
192 // TODO: Find way to clean up the extra pages.
193 container.removeView((View) object);
194 }
195
196 public Object instantiateItem(ViewGroup container, int position) {
197 if (DEBUG) Log.d(TAG, "Instantiating " + position);
Jason Monke4e69302016-01-20 11:27:15 -0500198 ViewGroup view = mPages.get(position);
Jason Monkcaf37622015-08-18 12:33:50 -0400199 container.addView(view);
200 return view;
201 }
202
203 @Override
204 public int getCount() {
205 return mNumPages;
206 }
207
208 @Override
209 public boolean isViewFromObject(View view, Object object) {
210 return view == object;
211 }
212 };
Jason Monk162011e2016-02-19 08:11:55 -0500213
214 public interface PageListener {
Jason Monk66eaf312016-02-25 12:29:29 -0500215 void onPageChanged(boolean isFirst);
Jason Monk162011e2016-02-19 08:11:55 -0500216 }
Jason Monkcaf37622015-08-18 12:33:50 -0400217}