blob: d8d07c01fc964bbffda465607f07d035a72762ef [file] [log] [blame]
Jason Monkcaf37622015-08-18 12:33:50 -04001package com.android.systemui.qs;
2
Amin Shaikha07a17b2018-02-23 16:02:52 -05003import android.animation.Animator;
4import android.animation.AnimatorListenerAdapter;
5import android.animation.AnimatorSet;
6import android.animation.ObjectAnimator;
7import android.animation.PropertyValuesHolder;
Jason Monkcaf37622015-08-18 12:33:50 -04008import android.content.Context;
Jason Monk6573ef22016-04-06 12:37:18 -04009import android.content.res.Configuration;
10import android.content.res.Resources;
Jason Monk51fb85a2016-03-28 14:06:04 -040011import android.support.v4.view.PagerAdapter;
12import android.support.v4.view.ViewPager;
Jason Monkcaf37622015-08-18 12:33:50 -040013import android.util.AttributeSet;
14import android.util.Log;
15import android.view.LayoutInflater;
Amin Shaikha07a17b2018-02-23 16:02:52 -050016import android.view.MotionEvent;
Jason Monkcaf37622015-08-18 12:33:50 -040017import android.view.View;
18import android.view.ViewGroup;
Amin Shaikha07a17b2018-02-23 16:02:52 -050019import android.view.animation.Interpolator;
20import android.view.animation.OvershootInterpolator;
21import android.widget.Scroller;
Jason Monkdf36aed2016-07-25 11:21:56 -040022
Jason Monkcaf37622015-08-18 12:33:50 -040023import com.android.systemui.R;
24import com.android.systemui.qs.QSPanel.QSTileLayout;
25import com.android.systemui.qs.QSPanel.TileRecord;
26
27import java.util.ArrayList;
Amin Shaikha07a17b2018-02-23 16:02:52 -050028import java.util.Set;
Jason Monkcaf37622015-08-18 12:33:50 -040029
30public class PagedTileLayout extends ViewPager implements QSTileLayout {
31
32 private static final boolean DEBUG = false;
33
34 private static final String TAG = "PagedTileLayout";
Amin Shaikha07a17b2018-02-23 16:02:52 -050035 private static final int REVEAL_SCROLL_DURATION_MILLIS = 750;
36 private static final float BOUNCE_ANIMATION_TENSION = 1.3f;
37 private static final long BOUNCE_ANIMATION_DURATION = 450L;
38 private static final int TILE_ANIMATION_STAGGER_DELAY = 85;
39 private static final Interpolator SCROLL_CUBIC = (t) -> {
40 t -= 1.0f;
41 return t * t * t + 1.0f;
42 };
43
Jason Monkcaf37622015-08-18 12:33:50 -040044
Amin Shaikh9978c552018-03-22 07:24:41 -040045 private final ArrayList<TileRecord> mTiles = new ArrayList<>();
46 private final ArrayList<TilePage> mPages = new ArrayList<>();
Jason Monkcaf37622015-08-18 12:33:50 -040047
Jason Monkcaf37622015-08-18 12:33:50 -040048 private PageIndicator mPageIndicator;
Rohan Shah3090e792018-04-12 00:01:00 -040049 private float mPageIndicatorPosition;
Jason Monkcaf37622015-08-18 12:33:50 -040050
51 private int mNumPages;
Jason Monk162011e2016-02-19 08:11:55 -050052 private PageListener mPageListener;
Jason Monkcaf37622015-08-18 12:33:50 -040053
Jason Monke5107a32016-05-31 15:40:58 -040054 private boolean mListening;
Amin Shaikha07a17b2018-02-23 16:02:52 -050055 private Scroller mScroller;
56
57 private AnimatorSet mBounceAnimatorSet;
58 private int mAnimatingToPage = -1;
Jason Monke5107a32016-05-31 15:40:58 -040059
Jason Monkcaf37622015-08-18 12:33:50 -040060 public PagedTileLayout(Context context, AttributeSet attrs) {
61 super(context, attrs);
Amin Shaikha07a17b2018-02-23 16:02:52 -050062 mScroller = new Scroller(context, SCROLL_CUBIC);
Jason Monkcaf37622015-08-18 12:33:50 -040063 setAdapter(mAdapter);
Amin Shaikha07a17b2018-02-23 16:02:52 -050064 setOnPageChangeListener(mOnPageChangeListener);
65 setCurrentItem(0, false);
Jason Monkcaf37622015-08-18 12:33:50 -040066 }
67
68 @Override
Jason Monk51fb85a2016-03-28 14:06:04 -040069 public void onRtlPropertiesChanged(int layoutDirection) {
70 super.onRtlPropertiesChanged(layoutDirection);
71 setAdapter(mAdapter);
72 setCurrentItem(0, false);
73 }
74
75 @Override
76 public void setCurrentItem(int item, boolean smoothScroll) {
77 if (isLayoutRtl()) {
78 item = mPages.size() - 1 - item;
79 }
80 super.setCurrentItem(item, smoothScroll);
81 }
82
83 @Override
Jason Monke5107a32016-05-31 15:40:58 -040084 public void setListening(boolean listening) {
85 if (mListening == listening) return;
86 mListening = listening;
Amin Shaikh9978c552018-03-22 07:24:41 -040087 updateListening();
88 }
89
90 private void updateListening() {
91 for (TilePage tilePage : mPages) {
92 tilePage.setListening(tilePage.getParent() == null ? false : mListening);
Jason Monke5107a32016-05-31 15:40:58 -040093 }
94 }
95
Amin Shaikha07a17b2018-02-23 16:02:52 -050096 @Override
97 public boolean onInterceptTouchEvent(MotionEvent ev) {
98 // Suppress all touch event during reveal animation.
99 if (mAnimatingToPage != -1) {
100 return true;
101 }
102 return super.onInterceptTouchEvent(ev);
103 }
104
105 @Override
106 public boolean onTouchEvent(MotionEvent ev) {
107 // Suppress all touch event during reveal animation.
108 if (mAnimatingToPage != -1) {
109 return true;
110 }
111 return super.onTouchEvent(ev);
112 }
113
114 @Override
115 public void computeScroll() {
116 if (!mScroller.isFinished() && mScroller.computeScrollOffset()) {
117 scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
118 float pageFraction = (float) getScrollX() / getWidth();
119 int position = (int) pageFraction;
120 float positionOffset = pageFraction - position;
121 mOnPageChangeListener.onPageScrolled(position, positionOffset, getScrollX());
122 // Keep on drawing until the animation has finished.
123 postInvalidateOnAnimation();
124 return;
125 }
126 if (mAnimatingToPage != -1) {
127 setCurrentItem(mAnimatingToPage, true);
128 mBounceAnimatorSet.start();
129 setOffscreenPageLimit(1);
130 mAnimatingToPage = -1;
131 }
132 super.computeScroll();
133 }
134
Jason Monke5107a32016-05-31 15:40:58 -0400135 @Override
Jason Monkc5bdafb2016-02-25 16:24:21 -0500136 public boolean hasOverlappingRendering() {
137 return false;
138 }
139
140 @Override
Jason Monkcaf37622015-08-18 12:33:50 -0400141 protected void onFinishInflate() {
142 super.onFinishInflate();
Jason Monk32508852017-01-18 09:17:13 -0500143 mPages.add((TilePage) LayoutInflater.from(getContext())
Jason Monke4e69302016-01-20 11:27:15 -0500144 .inflate(R.layout.qs_paged_page, this, false));
Jason Monkcaf37622015-08-18 12:33:50 -0400145 }
146
Jason Monk32508852017-01-18 09:17:13 -0500147 public void setPageIndicator(PageIndicator indicator) {
148 mPageIndicator = indicator;
Rohan Shah3090e792018-04-12 00:01:00 -0400149 mPageIndicator.setNumPages(mNumPages);
150 mPageIndicator.setLocation(mPageIndicatorPosition);
Jason Monk32508852017-01-18 09:17:13 -0500151 }
152
Jason Monkcaf37622015-08-18 12:33:50 -0400153 @Override
154 public int getOffsetTop(TileRecord tile) {
Jason Monkae5bd032016-03-02 14:38:31 -0500155 final ViewGroup parent = (ViewGroup) tile.tileView.getParent();
156 if (parent == null) return 0;
157 return parent.getTop() + getTop();
Jason Monkcaf37622015-08-18 12:33:50 -0400158 }
159
160 @Override
Jason Monkcaf37622015-08-18 12:33:50 -0400161 public void addTile(TileRecord tile) {
162 mTiles.add(tile);
Jason Monk9d02a432016-01-20 16:33:46 -0500163 postDistributeTiles();
Jason Monkcaf37622015-08-18 12:33:50 -0400164 }
165
166 @Override
167 public void removeTile(TileRecord tile) {
168 if (mTiles.remove(tile)) {
Jason Monk9d02a432016-01-20 16:33:46 -0500169 postDistributeTiles();
Jason Monkcaf37622015-08-18 12:33:50 -0400170 }
171 }
172
Amin Shaikh0f8ea5432018-03-27 11:09:27 -0400173 @Override
174 public void setExpansion(float expansion) {
175 for (TileRecord tr : mTiles) {
176 tr.tileView.setExpansion(expansion);
177 }
178 }
179
Jason Monk162011e2016-02-19 08:11:55 -0500180 public void setPageListener(PageListener listener) {
181 mPageListener = listener;
182 }
183
Jason Monk9d02a432016-01-20 16:33:46 -0500184 private void postDistributeTiles() {
185 removeCallbacks(mDistribute);
186 post(mDistribute);
187 }
188
Jason Monkcaf37622015-08-18 12:33:50 -0400189 private void distributeTiles() {
190 if (DEBUG) Log.d(TAG, "Distributing tiles");
Jason Monkcaf37622015-08-18 12:33:50 -0400191 final int NP = mPages.size();
192 for (int i = 0; i < NP; i++) {
Jason Monk9d02a432016-01-20 16:33:46 -0500193 mPages.get(i).removeAllViews();
Jason Monkcaf37622015-08-18 12:33:50 -0400194 }
195 int index = 0;
196 final int NT = mTiles.size();
197 for (int i = 0; i < NT; i++) {
198 TileRecord tile = mTiles.get(i);
Jason Monkcaf37622015-08-18 12:33:50 -0400199 if (mPages.get(index).isFull()) {
200 if (++index == mPages.size()) {
Jason Monk8e73ef32016-05-04 11:36:46 -0400201 if (DEBUG) Log.d(TAG, "Adding page for "
202 + tile.tile.getClass().getSimpleName());
Jason Monk32508852017-01-18 09:17:13 -0500203 mPages.add((TilePage) LayoutInflater.from(getContext())
Jason Monkcaf37622015-08-18 12:33:50 -0400204 .inflate(R.layout.qs_paged_page, this, false));
205 }
206 }
207 if (DEBUG) Log.d(TAG, "Adding " + tile.tile.getClass().getSimpleName() + " to "
208 + index);
209 mPages.get(index).addTile(tile);
210 }
211 if (mNumPages != index + 1) {
212 mNumPages = index + 1;
Jason Monk8e73ef32016-05-04 11:36:46 -0400213 while (mPages.size() > mNumPages) {
214 mPages.remove(mPages.size() - 1);
215 }
216 if (DEBUG) Log.d(TAG, "Size: " + mNumPages);
Jason Monkcaf37622015-08-18 12:33:50 -0400217 mPageIndicator.setNumPages(mNumPages);
Jason Monk8e73ef32016-05-04 11:36:46 -0400218 setAdapter(mAdapter);
Jason Monkcaf37622015-08-18 12:33:50 -0400219 mAdapter.notifyDataSetChanged();
Jason Monk51fb85a2016-03-28 14:06:04 -0400220 setCurrentItem(0, false);
Jason Monkcaf37622015-08-18 12:33:50 -0400221 }
222 }
223
224 @Override
Jason Monk9d02a432016-01-20 16:33:46 -0500225 public boolean updateResources() {
Rohan Shah3090e792018-04-12 00:01:00 -0400226 // Update bottom padding, useful for removing extra space once the panel page indicator is
227 // hidden.
228 setPadding(0, 0, 0,
229 getContext().getResources().getDimensionPixelSize(
230 R.dimen.qs_paged_tile_layout_padding_bottom));
231
Jason Monk9d02a432016-01-20 16:33:46 -0500232 boolean changed = false;
Jason Monkcaf37622015-08-18 12:33:50 -0400233 for (int i = 0; i < mPages.size(); i++) {
Jason Monk9d02a432016-01-20 16:33:46 -0500234 changed |= mPages.get(i).updateResources();
Jason Monkcaf37622015-08-18 12:33:50 -0400235 }
Jason Monk9d02a432016-01-20 16:33:46 -0500236 if (changed) {
237 distributeTiles();
238 }
239 return changed;
Jason Monkcaf37622015-08-18 12:33:50 -0400240 }
241
242 @Override
243 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
244 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
245 // The ViewPager likes to eat all of the space, instead force it to wrap to the max height
246 // of the pages.
247 int maxHeight = 0;
248 final int N = getChildCount();
249 for (int i = 0; i < N; i++) {
250 int height = getChildAt(i).getMeasuredHeight();
251 if (height > maxHeight) {
252 maxHeight = height;
253 }
254 }
Jason Monkf13413e2017-02-15 15:49:32 -0500255 setMeasuredDimension(getMeasuredWidth(), maxHeight + getPaddingBottom());
Jason Monkcaf37622015-08-18 12:33:50 -0400256 }
257
Jason Monk9d02a432016-01-20 16:33:46 -0500258 private final Runnable mDistribute = new Runnable() {
259 @Override
260 public void run() {
261 distributeTiles();
262 }
263 };
264
Jason Monk8fb77872016-03-03 16:39:42 -0500265 public int getColumnCount() {
266 if (mPages.size() == 0) return 0;
267 return mPages.get(0).mColumns;
268 }
269
Amin Shaikha07a17b2018-02-23 16:02:52 -0500270 public void startTileReveal(Set<String> tileSpecs, final Runnable postAnimation) {
271 if (tileSpecs.isEmpty() || mPages.size() < 2 || getScrollX() != 0) {
272 // Do not start the reveal animation unless there are tiles to animate, multiple
273 // TilePages available and the user has not already started dragging.
274 return;
275 }
276
277 final int lastPageNumber = mPages.size() - 1;
278 final TilePage lastPage = mPages.get(lastPageNumber);
279 final ArrayList<Animator> bounceAnims = new ArrayList<>();
280 for (TileRecord tr : lastPage.mRecords) {
281 if (tileSpecs.contains(tr.tile.getTileSpec())) {
282 bounceAnims.add(setupBounceAnimator(tr.tileView, bounceAnims.size()));
283 }
284 }
285
286 if (bounceAnims.isEmpty()) {
287 // All tileSpecs are on the first page. Nothing to do.
288 // TODO: potentially show a bounce animation for first page QS tiles
289 return;
290 }
291
292 mBounceAnimatorSet = new AnimatorSet();
293 mBounceAnimatorSet.playTogether(bounceAnims);
294 mBounceAnimatorSet.addListener(new AnimatorListenerAdapter() {
295 @Override
296 public void onAnimationEnd(Animator animation) {
297 mBounceAnimatorSet = null;
298 postAnimation.run();
299 }
300 });
301 mAnimatingToPage = lastPageNumber;
302 setOffscreenPageLimit(mAnimatingToPage); // Ensure the page to reveal has been inflated.
303 mScroller.startScroll(getScrollX(), getScrollY(), getWidth() * mAnimatingToPage, 0,
304 REVEAL_SCROLL_DURATION_MILLIS);
305 postInvalidateOnAnimation();
306 }
307
308 private static Animator setupBounceAnimator(View view, int ordinal) {
309 view.setAlpha(0f);
310 view.setScaleX(0f);
311 view.setScaleY(0f);
312 ObjectAnimator animator = ObjectAnimator.ofPropertyValuesHolder(view,
313 PropertyValuesHolder.ofFloat(View.ALPHA, 1),
314 PropertyValuesHolder.ofFloat(View.SCALE_X, 1),
315 PropertyValuesHolder.ofFloat(View.SCALE_Y, 1));
316 animator.setDuration(BOUNCE_ANIMATION_DURATION);
317 animator.setStartDelay(ordinal * TILE_ANIMATION_STAGGER_DELAY);
318 animator.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
319 return animator;
320 }
321
322 private final ViewPager.OnPageChangeListener mOnPageChangeListener =
323 new ViewPager.SimpleOnPageChangeListener() {
324 @Override
325 public void onPageSelected(int position) {
326 if (mPageIndicator == null) return;
327 if (mPageListener != null) {
328 mPageListener.onPageChanged(isLayoutRtl() ? position == mPages.size() - 1
329 : position == 0);
330 }
331 }
332
333 @Override
334 public void onPageScrolled(int position, float positionOffset,
335 int positionOffsetPixels) {
336 if (mPageIndicator == null) return;
Rohan Shah3090e792018-04-12 00:01:00 -0400337 mPageIndicatorPosition = position + positionOffset;
338 mPageIndicator.setLocation(mPageIndicatorPosition);
Amin Shaikha07a17b2018-02-23 16:02:52 -0500339 if (mPageListener != null) {
340 mPageListener.onPageChanged(positionOffsetPixels == 0 &&
341 (isLayoutRtl() ? position == mPages.size() - 1 : position == 0));
342 }
343 }
344 };
345
Jason Monkcaf37622015-08-18 12:33:50 -0400346 public static class TilePage extends TileLayout {
Jason Monk94a1bf62015-10-20 08:43:36 -0700347 private int mMaxRows = 3;
Jason Monkcaf37622015-08-18 12:33:50 -0400348 public TilePage(Context context, AttributeSet attrs) {
349 super(context, attrs);
Jason Monkb9c00192015-10-07 11:45:33 -0400350 updateResources();
Jason Monkcaf37622015-08-18 12:33:50 -0400351 }
352
Jason Monk9d02a432016-01-20 16:33:46 -0500353 @Override
354 public boolean updateResources() {
Jason Monk6573ef22016-04-06 12:37:18 -0400355 final int rows = getRows();
356 boolean changed = rows != mMaxRows;
357 if (changed) {
358 mMaxRows = rows;
359 requestLayout();
Jason Monk9d02a432016-01-20 16:33:46 -0500360 }
Jason Monk6573ef22016-04-06 12:37:18 -0400361 return super.updateResources() || changed;
362 }
363
364 private int getRows() {
365 final Resources res = getContext().getResources();
366 if (res.getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
Jason Monk290a4192017-04-21 11:51:17 -0400367 return res.getInteger(R.integer.quick_settings_num_rows_portrait);
Jason Monk6573ef22016-04-06 12:37:18 -0400368 }
369 return Math.max(1, res.getInteger(R.integer.quick_settings_num_rows));
Jason Monk9d02a432016-01-20 16:33:46 -0500370 }
371
Jason Monkbd6dbb02015-09-03 15:46:25 -0400372 public void setMaxRows(int maxRows) {
373 mMaxRows = maxRows;
374 }
375
Jason Monkbd6dbb02015-09-03 15:46:25 -0400376 public boolean isFull() {
Jason Monkcaf37622015-08-18 12:33:50 -0400377 return mRecords.size() >= mColumns * mMaxRows;
378 }
379 }
380
381 private final PagerAdapter mAdapter = new PagerAdapter() {
Amin Shaikh9978c552018-03-22 07:24:41 -0400382 @Override
Jason Monkcaf37622015-08-18 12:33:50 -0400383 public void destroyItem(ViewGroup container, int position, Object object) {
384 if (DEBUG) Log.d(TAG, "Destantiating " + position);
Jason Monkcaf37622015-08-18 12:33:50 -0400385 container.removeView((View) object);
Amin Shaikh9978c552018-03-22 07:24:41 -0400386 updateListening();
Jason Monkcaf37622015-08-18 12:33:50 -0400387 }
388
Amin Shaikh9978c552018-03-22 07:24:41 -0400389 @Override
Jason Monkcaf37622015-08-18 12:33:50 -0400390 public Object instantiateItem(ViewGroup container, int position) {
391 if (DEBUG) Log.d(TAG, "Instantiating " + position);
Jason Monk51fb85a2016-03-28 14:06:04 -0400392 if (isLayoutRtl()) {
393 position = mPages.size() - 1 - position;
394 }
Jason Monke4e69302016-01-20 11:27:15 -0500395 ViewGroup view = mPages.get(position);
Jason Monkcaf37622015-08-18 12:33:50 -0400396 container.addView(view);
Amin Shaikh9978c552018-03-22 07:24:41 -0400397 updateListening();
Jason Monkcaf37622015-08-18 12:33:50 -0400398 return view;
399 }
400
401 @Override
402 public int getCount() {
403 return mNumPages;
404 }
405
406 @Override
407 public boolean isViewFromObject(View view, Object object) {
408 return view == object;
409 }
410 };
Jason Monk162011e2016-02-19 08:11:55 -0500411
412 public interface PageListener {
Jason Monk66eaf312016-02-25 12:29:29 -0500413 void onPageChanged(boolean isFirst);
Jason Monk162011e2016-02-19 08:11:55 -0500414 }
Jason Monkcaf37622015-08-18 12:33:50 -0400415}