blob: cf96457ce09c62bd3d0318977ce57cdc24213513 [file] [log] [blame]
Jason Monkcaf37622015-08-18 12:33:50 -04001package com.android.systemui.qs;
2
3import android.content.Context;
Jason Monk6573ef22016-04-06 12:37:18 -04004import android.content.res.Configuration;
5import android.content.res.Resources;
Jason Monk51fb85a2016-03-28 14:06:04 -04006import android.support.v4.view.PagerAdapter;
7import android.support.v4.view.ViewPager;
Jason Monkcaf37622015-08-18 12:33:50 -04008import android.util.AttributeSet;
9import android.util.Log;
10import android.view.LayoutInflater;
11import android.view.View;
12import android.view.ViewGroup;
Jason Monkcaf37622015-08-18 12:33:50 -040013import com.android.systemui.R;
14import com.android.systemui.qs.QSPanel.QSTileLayout;
15import com.android.systemui.qs.QSPanel.TileRecord;
16
17import java.util.ArrayList;
18
19public class PagedTileLayout extends ViewPager implements QSTileLayout {
20
21 private static final boolean DEBUG = false;
22
23 private static final String TAG = "PagedTileLayout";
24
25 private final ArrayList<TileRecord> mTiles = new ArrayList<TileRecord>();
26 private final ArrayList<TilePage> mPages = new ArrayList<TilePage>();
27
Jason Monkcaf37622015-08-18 12:33:50 -040028 private PageIndicator mPageIndicator;
29
30 private int mNumPages;
Jason Monk21428432016-02-10 16:42:38 -050031 private View mDecorGroup;
Jason Monk162011e2016-02-19 08:11:55 -050032 private PageListener mPageListener;
Jason Monkcaf37622015-08-18 12:33:50 -040033
Jason Monke5107a32016-05-31 15:40:58 -040034 private int mPosition;
35 private boolean mOffPage;
36 private boolean mListening;
37
Jason Monkcaf37622015-08-18 12:33:50 -040038 public PagedTileLayout(Context context, AttributeSet attrs) {
39 super(context, attrs);
40 setAdapter(mAdapter);
41 setOnPageChangeListener(new OnPageChangeListener() {
42 @Override
43 public void onPageSelected(int position) {
44 if (mPageIndicator == null) return;
Jason Monk162011e2016-02-19 08:11:55 -050045 if (mPageListener != null) {
Jason Monk51fb85a2016-03-28 14:06:04 -040046 mPageListener.onPageChanged(isLayoutRtl() ? position == mPages.size() - 1
47 : position == 0);
Jason Monk162011e2016-02-19 08:11:55 -050048 }
Jason Monkcaf37622015-08-18 12:33:50 -040049 }
50
51 @Override
Jason Monk162011e2016-02-19 08:11:55 -050052 public void onPageScrolled(int position, float positionOffset,
53 int positionOffsetPixels) {
Jason Monkcaf37622015-08-18 12:33:50 -040054 if (mPageIndicator == null) return;
Jason Monke5107a32016-05-31 15:40:58 -040055 setCurrentPage(position, positionOffset != 0);
Jason Monkcaf37622015-08-18 12:33:50 -040056 mPageIndicator.setLocation(position + positionOffset);
Jason Monk66eaf312016-02-25 12:29:29 -050057 if (mPageListener != null) {
Jason Monk51fb85a2016-03-28 14:06:04 -040058 mPageListener.onPageChanged(positionOffsetPixels == 0 &&
59 (isLayoutRtl() ? position == mPages.size() - 1 : position == 0));
Jason Monk66eaf312016-02-25 12:29:29 -050060 }
Jason Monkcaf37622015-08-18 12:33:50 -040061 }
62
63 @Override
64 public void onPageScrollStateChanged(int state) {
65 }
66 });
67 setCurrentItem(0);
68 }
69
70 @Override
Jason Monk51fb85a2016-03-28 14:06:04 -040071 public void onRtlPropertiesChanged(int layoutDirection) {
72 super.onRtlPropertiesChanged(layoutDirection);
73 setAdapter(mAdapter);
74 setCurrentItem(0, false);
75 }
76
77 @Override
78 public void setCurrentItem(int item, boolean smoothScroll) {
79 if (isLayoutRtl()) {
80 item = mPages.size() - 1 - item;
81 }
82 super.setCurrentItem(item, smoothScroll);
83 }
84
85 @Override
Jason Monke5107a32016-05-31 15:40:58 -040086 public void setListening(boolean listening) {
87 if (mListening == listening) return;
88 mListening = listening;
89 if (mListening) {
90 mPages.get(mPosition).setListening(listening);
91 if (mOffPage) {
92 mPages.get(mPosition + 1).setListening(listening);
93 }
94 } else {
95 // Make sure no pages are listening.
96 for (int i = 0; i < mPages.size(); i++) {
97 mPages.get(i).setListening(false);
98 }
99 }
100 }
101
102 /**
103 * Sets individual pages to listening or not. If offPage it will set
104 * the next page after position to listening as well since we are in between
105 * pages.
106 */
107 private void setCurrentPage(int position, boolean offPage) {
108 if (mPosition == position && mOffPage == offPage) return;
109 if (mListening) {
110 if (mPosition != position) {
111 // Clear out the last pages from listening.
112 mPages.get(mPosition).setListening(false);
113 if (mOffPage) {
114 mPages.get(mPosition + 1).setListening(false);
115 }
116 // Set the new pages to listening
117 mPages.get(position).setListening(true);
118 if (offPage) {
119 mPages.get(position + 1).setListening(true);
120 }
121 } else if (mOffPage != offPage) {
122 // Whether we are showing position + 1 has changed.
123 mPages.get(mPosition + 1).setListening(offPage);
124 }
125 }
126 // Save the current state.
127 mPosition = position;
128 mOffPage = offPage;
129 }
130
131 @Override
Jason Monkc5bdafb2016-02-25 16:24:21 -0500132 public boolean hasOverlappingRendering() {
133 return false;
134 }
135
136 @Override
Jason Monkcaf37622015-08-18 12:33:50 -0400137 protected void onFinishInflate() {
138 super.onFinishInflate();
139 mPageIndicator = (PageIndicator) findViewById(R.id.page_indicator);
Jason Monk21428432016-02-10 16:42:38 -0500140 mDecorGroup = findViewById(R.id.page_decor);
141 ((LayoutParams) mDecorGroup.getLayoutParams()).isDecor = true;
Jason Monkcaf37622015-08-18 12:33:50 -0400142
Jason Monke4e69302016-01-20 11:27:15 -0500143 mPages.add((TilePage) LayoutInflater.from(mContext)
144 .inflate(R.layout.qs_paged_page, this, false));
Jason Monkcaf37622015-08-18 12:33:50 -0400145 }
146
147 @Override
148 public int getOffsetTop(TileRecord tile) {
Jason Monkae5bd032016-03-02 14:38:31 -0500149 final ViewGroup parent = (ViewGroup) tile.tileView.getParent();
150 if (parent == null) return 0;
151 return parent.getTop() + getTop();
Jason Monkcaf37622015-08-18 12:33:50 -0400152 }
153
154 @Override
Jason Monkcaf37622015-08-18 12:33:50 -0400155 public void addTile(TileRecord tile) {
156 mTiles.add(tile);
Jason Monk9d02a432016-01-20 16:33:46 -0500157 postDistributeTiles();
Jason Monkcaf37622015-08-18 12:33:50 -0400158 }
159
160 @Override
161 public void removeTile(TileRecord tile) {
162 if (mTiles.remove(tile)) {
Jason Monk9d02a432016-01-20 16:33:46 -0500163 postDistributeTiles();
Jason Monkcaf37622015-08-18 12:33:50 -0400164 }
165 }
166
Jason Monk162011e2016-02-19 08:11:55 -0500167 public void setPageListener(PageListener listener) {
168 mPageListener = listener;
169 }
170
Jason Monk9d02a432016-01-20 16:33:46 -0500171 private void postDistributeTiles() {
172 removeCallbacks(mDistribute);
173 post(mDistribute);
174 }
175
Jason Monkcaf37622015-08-18 12:33:50 -0400176 private void distributeTiles() {
177 if (DEBUG) Log.d(TAG, "Distributing tiles");
Jason Monkcaf37622015-08-18 12:33:50 -0400178 final int NP = mPages.size();
179 for (int i = 0; i < NP; i++) {
Jason Monk9d02a432016-01-20 16:33:46 -0500180 mPages.get(i).removeAllViews();
Jason Monkcaf37622015-08-18 12:33:50 -0400181 }
182 int index = 0;
183 final int NT = mTiles.size();
184 for (int i = 0; i < NT; i++) {
185 TileRecord tile = mTiles.get(i);
Jason Monkcaf37622015-08-18 12:33:50 -0400186 if (mPages.get(index).isFull()) {
187 if (++index == mPages.size()) {
Jason Monk8e73ef32016-05-04 11:36:46 -0400188 if (DEBUG) Log.d(TAG, "Adding page for "
189 + tile.tile.getClass().getSimpleName());
Jason Monkcaf37622015-08-18 12:33:50 -0400190 mPages.add((TilePage) LayoutInflater.from(mContext)
191 .inflate(R.layout.qs_paged_page, this, false));
192 }
193 }
194 if (DEBUG) Log.d(TAG, "Adding " + tile.tile.getClass().getSimpleName() + " to "
195 + index);
196 mPages.get(index).addTile(tile);
197 }
198 if (mNumPages != index + 1) {
199 mNumPages = index + 1;
Jason Monk8e73ef32016-05-04 11:36:46 -0400200 while (mPages.size() > mNumPages) {
201 mPages.remove(mPages.size() - 1);
202 }
203 if (DEBUG) Log.d(TAG, "Size: " + mNumPages);
Jason Monkcaf37622015-08-18 12:33:50 -0400204 mPageIndicator.setNumPages(mNumPages);
Jason Monk8e73ef32016-05-04 11:36:46 -0400205 setAdapter(mAdapter);
Jason Monkcaf37622015-08-18 12:33:50 -0400206 mAdapter.notifyDataSetChanged();
Jason Monk51fb85a2016-03-28 14:06:04 -0400207 setCurrentItem(0, false);
Jason Monkcaf37622015-08-18 12:33:50 -0400208 }
209 }
210
211 @Override
Jason Monk9d02a432016-01-20 16:33:46 -0500212 public boolean updateResources() {
213 boolean changed = false;
Jason Monkcaf37622015-08-18 12:33:50 -0400214 for (int i = 0; i < mPages.size(); i++) {
Jason Monk9d02a432016-01-20 16:33:46 -0500215 changed |= mPages.get(i).updateResources();
Jason Monkcaf37622015-08-18 12:33:50 -0400216 }
Jason Monk9d02a432016-01-20 16:33:46 -0500217 if (changed) {
218 distributeTiles();
219 }
220 return changed;
Jason Monkcaf37622015-08-18 12:33:50 -0400221 }
222
223 @Override
224 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
225 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
226 // The ViewPager likes to eat all of the space, instead force it to wrap to the max height
227 // of the pages.
228 int maxHeight = 0;
229 final int N = getChildCount();
230 for (int i = 0; i < N; i++) {
231 int height = getChildAt(i).getMeasuredHeight();
232 if (height > maxHeight) {
233 maxHeight = height;
234 }
235 }
Jason Monk21428432016-02-10 16:42:38 -0500236 setMeasuredDimension(getMeasuredWidth(), maxHeight + mDecorGroup.getMeasuredHeight());
Jason Monkcaf37622015-08-18 12:33:50 -0400237 }
238
Jason Monk9d02a432016-01-20 16:33:46 -0500239 private final Runnable mDistribute = new Runnable() {
240 @Override
241 public void run() {
242 distributeTiles();
243 }
244 };
245
Jason Monk8fb77872016-03-03 16:39:42 -0500246 public int getColumnCount() {
247 if (mPages.size() == 0) return 0;
248 return mPages.get(0).mColumns;
249 }
250
Jason Monkcaf37622015-08-18 12:33:50 -0400251 public static class TilePage extends TileLayout {
Jason Monk94a1bf62015-10-20 08:43:36 -0700252 private int mMaxRows = 3;
Jason Monkcaf37622015-08-18 12:33:50 -0400253
254 public TilePage(Context context, AttributeSet attrs) {
255 super(context, attrs);
Jason Monkb9c00192015-10-07 11:45:33 -0400256 updateResources();
Julia Reynolds20aef8a2016-05-04 16:44:08 -0400257 setContentDescription(mContext.getString(R.string.accessibility_desc_quick_settings));
Jason Monkcaf37622015-08-18 12:33:50 -0400258 }
259
Jason Monk9d02a432016-01-20 16:33:46 -0500260 @Override
261 public boolean updateResources() {
Jason Monk6573ef22016-04-06 12:37:18 -0400262 final int rows = getRows();
263 boolean changed = rows != mMaxRows;
264 if (changed) {
265 mMaxRows = rows;
266 requestLayout();
Jason Monk9d02a432016-01-20 16:33:46 -0500267 }
Jason Monk6573ef22016-04-06 12:37:18 -0400268 return super.updateResources() || changed;
269 }
270
271 private int getRows() {
272 final Resources res = getContext().getResources();
273 if (res.getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
274 // Always have 3 rows in portrait.
275 return 3;
276 }
277 return Math.max(1, res.getInteger(R.integer.quick_settings_num_rows));
Jason Monk9d02a432016-01-20 16:33:46 -0500278 }
279
Jason Monkbd6dbb02015-09-03 15:46:25 -0400280 public void setMaxRows(int maxRows) {
281 mMaxRows = maxRows;
282 }
283
Jason Monkbd6dbb02015-09-03 15:46:25 -0400284 public boolean isFull() {
Jason Monkcaf37622015-08-18 12:33:50 -0400285 return mRecords.size() >= mColumns * mMaxRows;
286 }
287 }
288
289 private final PagerAdapter mAdapter = new PagerAdapter() {
290 public void destroyItem(ViewGroup container, int position, Object object) {
291 if (DEBUG) Log.d(TAG, "Destantiating " + position);
Jason Monkcaf37622015-08-18 12:33:50 -0400292 container.removeView((View) object);
293 }
294
295 public Object instantiateItem(ViewGroup container, int position) {
296 if (DEBUG) Log.d(TAG, "Instantiating " + position);
Jason Monk51fb85a2016-03-28 14:06:04 -0400297 if (isLayoutRtl()) {
298 position = mPages.size() - 1 - position;
299 }
Jason Monke4e69302016-01-20 11:27:15 -0500300 ViewGroup view = mPages.get(position);
Jason Monkcaf37622015-08-18 12:33:50 -0400301 container.addView(view);
302 return view;
303 }
304
305 @Override
306 public int getCount() {
307 return mNumPages;
308 }
309
310 @Override
311 public boolean isViewFromObject(View view, Object object) {
312 return view == object;
313 }
314 };
Jason Monk162011e2016-02-19 08:11:55 -0500315
316 public interface PageListener {
Jason Monk66eaf312016-02-25 12:29:29 -0500317 void onPageChanged(boolean isFirst);
Jason Monk162011e2016-02-19 08:11:55 -0500318 }
Jason Monkcaf37622015-08-18 12:33:50 -0400319}