blob: ebc3a6adaeee58562b19616e9aab7540f48d0b15 [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;
Fabian Kozynski407ddb22018-10-03 15:04:56 -04009import android.content.res.Configuration;
Fabian Kozynski6eaf3ac2018-10-01 15:59:05 -040010import android.content.res.Resources;
Fabian Kozynski52bd8c22018-10-08 12:52:51 -040011import android.graphics.Rect;
Fabian Kozynski407ddb22018-10-03 15:04:56 -040012import android.os.Bundle;
Jason Monkcaf37622015-08-18 12:33:50 -040013import android.util.AttributeSet;
14import android.util.Log;
15import android.view.LayoutInflater;
16import android.view.View;
17import android.view.ViewGroup;
Amin Shaikha07a17b2018-02-23 16:02:52 -050018import android.view.animation.Interpolator;
19import android.view.animation.OvershootInterpolator;
20import android.widget.Scroller;
Jason Monkdf36aed2016-07-25 11:21:56 -040021
Fabian Kozynski6eaf3ac2018-10-01 15:59:05 -040022import androidx.viewpager.widget.PagerAdapter;
23import androidx.viewpager.widget.ViewPager;
24
Jason Monkcaf37622015-08-18 12:33:50 -040025import com.android.systemui.R;
26import com.android.systemui.qs.QSPanel.QSTileLayout;
27import com.android.systemui.qs.QSPanel.TileRecord;
28
29import java.util.ArrayList;
Amin Shaikha07a17b2018-02-23 16:02:52 -050030import java.util.Set;
Jason Monkcaf37622015-08-18 12:33:50 -040031
32public class PagedTileLayout extends ViewPager implements QSTileLayout {
33
34 private static final boolean DEBUG = false;
Fabian Kozynski407ddb22018-10-03 15:04:56 -040035 private static final String CURRENT_PAGE = "current_page";
Jason Monkcaf37622015-08-18 12:33:50 -040036
37 private static final String TAG = "PagedTileLayout";
Amin Shaikha07a17b2018-02-23 16:02:52 -050038 private static final int REVEAL_SCROLL_DURATION_MILLIS = 750;
39 private static final float BOUNCE_ANIMATION_TENSION = 1.3f;
40 private static final long BOUNCE_ANIMATION_DURATION = 450L;
41 private static final int TILE_ANIMATION_STAGGER_DELAY = 85;
42 private static final Interpolator SCROLL_CUBIC = (t) -> {
43 t -= 1.0f;
44 return t * t * t + 1.0f;
45 };
46
Amin Shaikh9978c552018-03-22 07:24:41 -040047 private final ArrayList<TileRecord> mTiles = new ArrayList<>();
48 private final ArrayList<TilePage> mPages = new ArrayList<>();
Jason Monkcaf37622015-08-18 12:33:50 -040049
Jason Monkcaf37622015-08-18 12:33:50 -040050 private PageIndicator mPageIndicator;
Rohan Shah3090e792018-04-12 00:01:00 -040051 private float mPageIndicatorPosition;
Jason Monkcaf37622015-08-18 12:33:50 -040052
Jason Monk162011e2016-02-19 08:11:55 -050053 private PageListener mPageListener;
Jason Monkcaf37622015-08-18 12:33:50 -040054
Jason Monke5107a32016-05-31 15:40:58 -040055 private boolean mListening;
Amin Shaikha07a17b2018-02-23 16:02:52 -050056 private Scroller mScroller;
57
58 private AnimatorSet mBounceAnimatorSet;
Amin Shaikh4c9048c2018-04-20 11:27:46 -040059 private float mLastExpansion;
Fabian Kozynski712ae392018-09-12 09:11:16 -040060 private boolean mDistributeTiles = false;
Fabian Kozynski407ddb22018-10-03 15:04:56 -040061 private int mPageToRestore = -1;
62 private int mLayoutOrientation;
63 private int mLayoutDirection;
Fabian Kozynski52bd8c22018-10-08 12:52:51 -040064 private int mHorizontalClipBound;
Fabian Kozynski87bdf0d2019-04-01 09:52:57 -040065 private final Rect mClippingRect;
Jason Monke5107a32016-05-31 15:40:58 -040066
Jason Monkcaf37622015-08-18 12:33:50 -040067 public PagedTileLayout(Context context, AttributeSet attrs) {
68 super(context, attrs);
Amin Shaikha07a17b2018-02-23 16:02:52 -050069 mScroller = new Scroller(context, SCROLL_CUBIC);
Jason Monkcaf37622015-08-18 12:33:50 -040070 setAdapter(mAdapter);
Amin Shaikha07a17b2018-02-23 16:02:52 -050071 setOnPageChangeListener(mOnPageChangeListener);
72 setCurrentItem(0, false);
Fabian Kozynski407ddb22018-10-03 15:04:56 -040073 mLayoutOrientation = getResources().getConfiguration().orientation;
74 mLayoutDirection = getLayoutDirection();
Fabian Kozynski87bdf0d2019-04-01 09:52:57 -040075 mClippingRect = new Rect();
Fabian Kozynski407ddb22018-10-03 15:04:56 -040076 }
77
78 public void saveInstanceState(Bundle outState) {
79 outState.putInt(CURRENT_PAGE, getCurrentItem());
80 }
81
82 public void restoreInstanceState(Bundle savedInstanceState) {
83 // There's only 1 page at this point. We want to restore the correct page once the
84 // pages have been inflated
85 mPageToRestore = savedInstanceState.getInt(CURRENT_PAGE, -1);
86 }
87
88 @Override
89 protected void onConfigurationChanged(Configuration newConfig) {
90 super.onConfigurationChanged(newConfig);
91 if (mLayoutOrientation != newConfig.orientation) {
92 mLayoutOrientation = newConfig.orientation;
93 setCurrentItem(0, false);
Fabian Kozynskid9e8aa32019-04-12 10:09:48 -040094 mPageToRestore = 0;
Fabian Kozynski407ddb22018-10-03 15:04:56 -040095 }
Jason Monkcaf37622015-08-18 12:33:50 -040096 }
97
98 @Override
Jason Monk51fb85a2016-03-28 14:06:04 -040099 public void onRtlPropertiesChanged(int layoutDirection) {
100 super.onRtlPropertiesChanged(layoutDirection);
Fabian Kozynski407ddb22018-10-03 15:04:56 -0400101 if (mLayoutDirection != layoutDirection) {
102 mLayoutDirection = layoutDirection;
103 setAdapter(mAdapter);
104 setCurrentItem(0, false);
Fabian Kozynskid9e8aa32019-04-12 10:09:48 -0400105 mPageToRestore = 0;
Fabian Kozynski407ddb22018-10-03 15:04:56 -0400106 }
Jason Monk51fb85a2016-03-28 14:06:04 -0400107 }
108
109 @Override
110 public void setCurrentItem(int item, boolean smoothScroll) {
111 if (isLayoutRtl()) {
112 item = mPages.size() - 1 - item;
113 }
114 super.setCurrentItem(item, smoothScroll);
115 }
116
Fabian Kozynskid9e8aa32019-04-12 10:09:48 -0400117 /**
118 * Obtains the current page number respecting RTL
119 */
120 private int getCurrentPageNumber() {
121 int page = getCurrentItem();
122 if (mLayoutDirection == LAYOUT_DIRECTION_RTL) {
123 page = mPages.size() - 1 - page;
124 }
125 return page;
126 }
127
Jason Monk51fb85a2016-03-28 14:06:04 -0400128 @Override
Jason Monke5107a32016-05-31 15:40:58 -0400129 public void setListening(boolean listening) {
130 if (mListening == listening) return;
131 mListening = listening;
Amin Shaikh9978c552018-03-22 07:24:41 -0400132 updateListening();
133 }
134
135 private void updateListening() {
136 for (TilePage tilePage : mPages) {
137 tilePage.setListening(tilePage.getParent() == null ? false : mListening);
Jason Monke5107a32016-05-31 15:40:58 -0400138 }
139 }
140
Amin Shaikha07a17b2018-02-23 16:02:52 -0500141 @Override
Amin Shaikha07a17b2018-02-23 16:02:52 -0500142 public void computeScroll() {
143 if (!mScroller.isFinished() && mScroller.computeScrollOffset()) {
Amin Shaikh5484d0f2018-07-31 10:44:11 -0400144 fakeDragBy(getScrollX() - mScroller.getCurrX());
Amin Shaikha07a17b2018-02-23 16:02:52 -0500145 // Keep on drawing until the animation has finished.
146 postInvalidateOnAnimation();
147 return;
Amin Shaikh5484d0f2018-07-31 10:44:11 -0400148 } else if (isFakeDragging()) {
149 endFakeDrag();
Amin Shaikha07a17b2018-02-23 16:02:52 -0500150 mBounceAnimatorSet.start();
151 setOffscreenPageLimit(1);
Amin Shaikha07a17b2018-02-23 16:02:52 -0500152 }
153 super.computeScroll();
154 }
155
Jason Monke5107a32016-05-31 15:40:58 -0400156 @Override
Jason Monkc5bdafb2016-02-25 16:24:21 -0500157 public boolean hasOverlappingRendering() {
158 return false;
159 }
160
161 @Override
Jason Monkcaf37622015-08-18 12:33:50 -0400162 protected void onFinishInflate() {
163 super.onFinishInflate();
Jason Monk32508852017-01-18 09:17:13 -0500164 mPages.add((TilePage) LayoutInflater.from(getContext())
Jason Monke4e69302016-01-20 11:27:15 -0500165 .inflate(R.layout.qs_paged_page, this, false));
Fabian Kozynskifae42a82018-10-04 11:18:42 -0400166 mAdapter.notifyDataSetChanged();
Jason Monkcaf37622015-08-18 12:33:50 -0400167 }
168
Jason Monk32508852017-01-18 09:17:13 -0500169 public void setPageIndicator(PageIndicator indicator) {
170 mPageIndicator = indicator;
Fabian Kozynski712ae392018-09-12 09:11:16 -0400171 mPageIndicator.setNumPages(mPages.size());
Rohan Shah3090e792018-04-12 00:01:00 -0400172 mPageIndicator.setLocation(mPageIndicatorPosition);
Jason Monk32508852017-01-18 09:17:13 -0500173 }
174
Jason Monkcaf37622015-08-18 12:33:50 -0400175 @Override
176 public int getOffsetTop(TileRecord tile) {
Jason Monkae5bd032016-03-02 14:38:31 -0500177 final ViewGroup parent = (ViewGroup) tile.tileView.getParent();
178 if (parent == null) return 0;
179 return parent.getTop() + getTop();
Jason Monkcaf37622015-08-18 12:33:50 -0400180 }
181
182 @Override
Jason Monkcaf37622015-08-18 12:33:50 -0400183 public void addTile(TileRecord tile) {
184 mTiles.add(tile);
Fabian Kozynski712ae392018-09-12 09:11:16 -0400185 mDistributeTiles = true;
186 requestLayout();
Jason Monkcaf37622015-08-18 12:33:50 -0400187 }
188
189 @Override
190 public void removeTile(TileRecord tile) {
191 if (mTiles.remove(tile)) {
Fabian Kozynski712ae392018-09-12 09:11:16 -0400192 mDistributeTiles = true;
193 requestLayout();
Jason Monkcaf37622015-08-18 12:33:50 -0400194 }
195 }
196
Amin Shaikh0f8ea5432018-03-27 11:09:27 -0400197 @Override
198 public void setExpansion(float expansion) {
Amin Shaikh4c9048c2018-04-20 11:27:46 -0400199 mLastExpansion = expansion;
200 updateSelected();
201 }
202
203 private void updateSelected() {
204 // Start the marquee when fully expanded and stop when fully collapsed. Leave as is for
205 // other expansion ratios since there is no way way to pause the marquee.
206 if (mLastExpansion > 0f && mLastExpansion < 1f) {
207 return;
208 }
209 boolean selected = mLastExpansion == 1f;
Rohan Shah2dbcb572018-05-25 10:51:22 -0700210
211 // Disable accessibility temporarily while we update selected state purely for the
212 // marquee. This will ensure that accessibility doesn't announce the TYPE_VIEW_SELECTED
213 // event on any of the children.
214 setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
Fabian Kozynskid9e8aa32019-04-12 10:09:48 -0400215 int currentItem = getCurrentPageNumber();
Amin Shaikh4c9048c2018-04-20 11:27:46 -0400216 for (int i = 0; i < mPages.size(); i++) {
Amin Shaikh6f570742018-06-01 17:18:19 -0400217 mPages.get(i).setSelected(i == currentItem ? selected : false);
Amin Shaikh0f8ea5432018-03-27 11:09:27 -0400218 }
Rohan Shah2dbcb572018-05-25 10:51:22 -0700219 setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_AUTO);
Amin Shaikh0f8ea5432018-03-27 11:09:27 -0400220 }
221
Jason Monk162011e2016-02-19 08:11:55 -0500222 public void setPageListener(PageListener listener) {
223 mPageListener = listener;
224 }
225
Jason Monkcaf37622015-08-18 12:33:50 -0400226 private void distributeTiles() {
Fabian Kozynski712ae392018-09-12 09:11:16 -0400227 emptyAndInflateOrRemovePages();
228
229 final int tileCount = mPages.get(0).maxTiles();
Jason Monkcaf37622015-08-18 12:33:50 -0400230 if (DEBUG) Log.d(TAG, "Distributing tiles");
Jason Monkcaf37622015-08-18 12:33:50 -0400231 int index = 0;
232 final int NT = mTiles.size();
233 for (int i = 0; i < NT; i++) {
234 TileRecord tile = mTiles.get(i);
Fabian Kozynski712ae392018-09-12 09:11:16 -0400235 if (mPages.get(index).mRecords.size() == tileCount) index++;
236 if (DEBUG) {
237 Log.d(TAG, "Adding " + tile.tile.getClass().getSimpleName() + " to "
238 + index);
Jason Monkcaf37622015-08-18 12:33:50 -0400239 }
Jason Monkcaf37622015-08-18 12:33:50 -0400240 mPages.get(index).addTile(tile);
241 }
Fabian Kozynski712ae392018-09-12 09:11:16 -0400242 }
243
244 private void emptyAndInflateOrRemovePages() {
245 final int nTiles = mTiles.size();
246 int numPages = nTiles / mPages.get(0).maxTiles();
247 // Add one more not full page if needed
248 numPages += (nTiles % mPages.get(0).maxTiles() == 0 ? 0 : 1);
249
250 final int NP = mPages.size();
251 for (int i = 0; i < NP; i++) {
252 mPages.get(i).removeAllViews();
Jason Monkcaf37622015-08-18 12:33:50 -0400253 }
Fabian Kozynski712ae392018-09-12 09:11:16 -0400254 if (NP == numPages) {
255 return;
256 }
257 while (mPages.size() < numPages) {
258 if (DEBUG) Log.d(TAG, "Adding page");
259 mPages.add((TilePage) LayoutInflater.from(getContext())
260 .inflate(R.layout.qs_paged_page, this, false));
261 }
262 while (mPages.size() > numPages) {
263 if (DEBUG) Log.d(TAG, "Removing page");
264 mPages.remove(mPages.size() - 1);
265 }
266 mPageIndicator.setNumPages(mPages.size());
267 setAdapter(mAdapter);
268 mAdapter.notifyDataSetChanged();
Fabian Kozynski407ddb22018-10-03 15:04:56 -0400269 if (mPageToRestore != -1) {
270 setCurrentItem(mPageToRestore, false);
271 mPageToRestore = -1;
272 }
Jason Monkcaf37622015-08-18 12:33:50 -0400273 }
274
275 @Override
Jason Monk9d02a432016-01-20 16:33:46 -0500276 public boolean updateResources() {
Rohan Shah3090e792018-04-12 00:01:00 -0400277 // Update bottom padding, useful for removing extra space once the panel page indicator is
278 // hidden.
Fabian Kozynski6eaf3ac2018-10-01 15:59:05 -0400279 Resources res = getContext().getResources();
Fabian Kozynski52bd8c22018-10-08 12:52:51 -0400280 mHorizontalClipBound = res.getDimensionPixelSize(R.dimen.notification_side_paddings);
281 setPadding(0, 0, 0,
Rohan Shah3090e792018-04-12 00:01:00 -0400282 getContext().getResources().getDimensionPixelSize(
283 R.dimen.qs_paged_tile_layout_padding_bottom));
Jason Monk9d02a432016-01-20 16:33:46 -0500284 boolean changed = false;
Jason Monkcaf37622015-08-18 12:33:50 -0400285 for (int i = 0; i < mPages.size(); i++) {
Jason Monk9d02a432016-01-20 16:33:46 -0500286 changed |= mPages.get(i).updateResources();
Jason Monkcaf37622015-08-18 12:33:50 -0400287 }
Jason Monk9d02a432016-01-20 16:33:46 -0500288 if (changed) {
Fabian Kozynski712ae392018-09-12 09:11:16 -0400289 mDistributeTiles = true;
290 requestLayout();
Jason Monk9d02a432016-01-20 16:33:46 -0500291 }
292 return changed;
Jason Monkcaf37622015-08-18 12:33:50 -0400293 }
294
295 @Override
Fabian Kozynski52bd8c22018-10-08 12:52:51 -0400296 protected void onLayout(boolean changed, int l, int t, int r, int b) {
297 super.onLayout(changed, l, t, r, b);
Fabian Kozynski87bdf0d2019-04-01 09:52:57 -0400298 mClippingRect.set(mHorizontalClipBound, 0, (r - l) - mHorizontalClipBound, b - t);
299 setClipBounds(mClippingRect);
Fabian Kozynski52bd8c22018-10-08 12:52:51 -0400300 }
301
302 @Override
Jason Monkcaf37622015-08-18 12:33:50 -0400303 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Fabian Kozynski712ae392018-09-12 09:11:16 -0400304
305 final int nTiles = mTiles.size();
306 if (MeasureSpec.getMode(heightMeasureSpec) == MeasureSpec.AT_MOST) {
307
308 // Only change the pages if the number of rows or columns (from updateResources) has
309 // changed or the tiles have changed
310 if (mPages.get(0).updateMaxRows(heightMeasureSpec, nTiles) || mDistributeTiles) {
311 mDistributeTiles = false;
312 distributeTiles();
313 }
314
315 final int nRows = mPages.get(0).mRows;
316 for (int i = 0; i < mPages.size(); i++) {
317 TilePage t = mPages.get(i);
318 t.mRows = nRows;
319 }
320 }
321
Jason Monkcaf37622015-08-18 12:33:50 -0400322 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
Fabian Kozynski712ae392018-09-12 09:11:16 -0400323
Jason Monkcaf37622015-08-18 12:33:50 -0400324 // The ViewPager likes to eat all of the space, instead force it to wrap to the max height
325 // of the pages.
326 int maxHeight = 0;
327 final int N = getChildCount();
328 for (int i = 0; i < N; i++) {
329 int height = getChildAt(i).getMeasuredHeight();
330 if (height > maxHeight) {
331 maxHeight = height;
332 }
333 }
Jason Monkf13413e2017-02-15 15:49:32 -0500334 setMeasuredDimension(getMeasuredWidth(), maxHeight + getPaddingBottom());
Jason Monkcaf37622015-08-18 12:33:50 -0400335 }
336
Jason Monk8fb77872016-03-03 16:39:42 -0500337 public int getColumnCount() {
338 if (mPages.size() == 0) return 0;
339 return mPages.get(0).mColumns;
340 }
341
Fabian Kozynski802279f2018-09-07 13:44:54 -0400342 public int getNumVisibleTiles() {
343 if (mPages.size() == 0) return 0;
Fabian Kozynskid9e8aa32019-04-12 10:09:48 -0400344 TilePage currentPage = mPages.get(getCurrentPageNumber());
Fabian Kozynski802279f2018-09-07 13:44:54 -0400345 return currentPage.mRecords.size();
346 }
347
Amin Shaikha07a17b2018-02-23 16:02:52 -0500348 public void startTileReveal(Set<String> tileSpecs, final Runnable postAnimation) {
Amin Shaikh5484d0f2018-07-31 10:44:11 -0400349 if (tileSpecs.isEmpty() || mPages.size() < 2 || getScrollX() != 0 || !beginFakeDrag()) {
Amin Shaikha07a17b2018-02-23 16:02:52 -0500350 // Do not start the reveal animation unless there are tiles to animate, multiple
351 // TilePages available and the user has not already started dragging.
352 return;
353 }
354
355 final int lastPageNumber = mPages.size() - 1;
356 final TilePage lastPage = mPages.get(lastPageNumber);
357 final ArrayList<Animator> bounceAnims = new ArrayList<>();
358 for (TileRecord tr : lastPage.mRecords) {
359 if (tileSpecs.contains(tr.tile.getTileSpec())) {
360 bounceAnims.add(setupBounceAnimator(tr.tileView, bounceAnims.size()));
361 }
362 }
363
364 if (bounceAnims.isEmpty()) {
365 // All tileSpecs are on the first page. Nothing to do.
366 // TODO: potentially show a bounce animation for first page QS tiles
Amin Shaikh5484d0f2018-07-31 10:44:11 -0400367 endFakeDrag();
Amin Shaikha07a17b2018-02-23 16:02:52 -0500368 return;
369 }
370
371 mBounceAnimatorSet = new AnimatorSet();
372 mBounceAnimatorSet.playTogether(bounceAnims);
373 mBounceAnimatorSet.addListener(new AnimatorListenerAdapter() {
374 @Override
375 public void onAnimationEnd(Animator animation) {
376 mBounceAnimatorSet = null;
377 postAnimation.run();
378 }
379 });
Amin Shaikh5484d0f2018-07-31 10:44:11 -0400380 setOffscreenPageLimit(lastPageNumber); // Ensure the page to reveal has been inflated.
381 int dx = getWidth() * lastPageNumber;
382 mScroller.startScroll(getScrollX(), getScrollY(), isLayoutRtl() ? -dx : dx, 0,
383 REVEAL_SCROLL_DURATION_MILLIS);
Amin Shaikha07a17b2018-02-23 16:02:52 -0500384 postInvalidateOnAnimation();
385 }
386
387 private static Animator setupBounceAnimator(View view, int ordinal) {
388 view.setAlpha(0f);
389 view.setScaleX(0f);
390 view.setScaleY(0f);
391 ObjectAnimator animator = ObjectAnimator.ofPropertyValuesHolder(view,
392 PropertyValuesHolder.ofFloat(View.ALPHA, 1),
393 PropertyValuesHolder.ofFloat(View.SCALE_X, 1),
394 PropertyValuesHolder.ofFloat(View.SCALE_Y, 1));
395 animator.setDuration(BOUNCE_ANIMATION_DURATION);
396 animator.setStartDelay(ordinal * TILE_ANIMATION_STAGGER_DELAY);
397 animator.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
398 return animator;
399 }
400
401 private final ViewPager.OnPageChangeListener mOnPageChangeListener =
402 new ViewPager.SimpleOnPageChangeListener() {
403 @Override
404 public void onPageSelected(int position) {
Amin Shaikh4c9048c2018-04-20 11:27:46 -0400405 updateSelected();
Amin Shaikha07a17b2018-02-23 16:02:52 -0500406 if (mPageIndicator == null) return;
407 if (mPageListener != null) {
408 mPageListener.onPageChanged(isLayoutRtl() ? position == mPages.size() - 1
409 : position == 0);
410 }
411 }
412
413 @Override
414 public void onPageScrolled(int position, float positionOffset,
415 int positionOffsetPixels) {
416 if (mPageIndicator == null) return;
Rohan Shah3090e792018-04-12 00:01:00 -0400417 mPageIndicatorPosition = position + positionOffset;
418 mPageIndicator.setLocation(mPageIndicatorPosition);
Amin Shaikha07a17b2018-02-23 16:02:52 -0500419 if (mPageListener != null) {
420 mPageListener.onPageChanged(positionOffsetPixels == 0 &&
421 (isLayoutRtl() ? position == mPages.size() - 1 : position == 0));
422 }
423 }
424 };
425
Jason Monkcaf37622015-08-18 12:33:50 -0400426 public static class TilePage extends TileLayout {
Fabian Kozynski712ae392018-09-12 09:11:16 -0400427
Jason Monkcaf37622015-08-18 12:33:50 -0400428 public TilePage(Context context, AttributeSet attrs) {
429 super(context, attrs);
Jason Monkbd6dbb02015-09-03 15:46:25 -0400430 }
431
Jason Monkbd6dbb02015-09-03 15:46:25 -0400432 public boolean isFull() {
Fabian Kozynski712ae392018-09-12 09:11:16 -0400433 return mRecords.size() >= mColumns * mRows;
434 }
435
436 public int maxTiles() {
437 return mColumns * mRows;
Jason Monkcaf37622015-08-18 12:33:50 -0400438 }
Fabian Kozynski52bd8c22018-10-08 12:52:51 -0400439
440 @Override
441 public boolean updateResources() {
442 final int sidePadding = getContext().getResources().getDimensionPixelSize(
443 R.dimen.notification_side_paddings);
444 setPadding(sidePadding, 0, sidePadding, 0);
445 return super.updateResources();
446 }
Jason Monkcaf37622015-08-18 12:33:50 -0400447 }
448
449 private final PagerAdapter mAdapter = new PagerAdapter() {
Amin Shaikh9978c552018-03-22 07:24:41 -0400450 @Override
Jason Monkcaf37622015-08-18 12:33:50 -0400451 public void destroyItem(ViewGroup container, int position, Object object) {
452 if (DEBUG) Log.d(TAG, "Destantiating " + position);
Jason Monkcaf37622015-08-18 12:33:50 -0400453 container.removeView((View) object);
Amin Shaikh9978c552018-03-22 07:24:41 -0400454 updateListening();
Jason Monkcaf37622015-08-18 12:33:50 -0400455 }
456
Amin Shaikh9978c552018-03-22 07:24:41 -0400457 @Override
Jason Monkcaf37622015-08-18 12:33:50 -0400458 public Object instantiateItem(ViewGroup container, int position) {
459 if (DEBUG) Log.d(TAG, "Instantiating " + position);
Jason Monk51fb85a2016-03-28 14:06:04 -0400460 if (isLayoutRtl()) {
461 position = mPages.size() - 1 - position;
462 }
Jason Monke4e69302016-01-20 11:27:15 -0500463 ViewGroup view = mPages.get(position);
Amin Shaikh53bc0832018-09-17 15:06:18 -0400464 if (view.getParent() != null) {
465 container.removeView(view);
466 }
Jason Monkcaf37622015-08-18 12:33:50 -0400467 container.addView(view);
Amin Shaikh9978c552018-03-22 07:24:41 -0400468 updateListening();
Jason Monkcaf37622015-08-18 12:33:50 -0400469 return view;
470 }
471
472 @Override
473 public int getCount() {
Fabian Kozynski712ae392018-09-12 09:11:16 -0400474 return mPages.size();
Jason Monkcaf37622015-08-18 12:33:50 -0400475 }
476
477 @Override
478 public boolean isViewFromObject(View view, Object object) {
479 return view == object;
480 }
481 };
Jason Monk162011e2016-02-19 08:11:55 -0500482
483 public interface PageListener {
Jason Monk66eaf312016-02-25 12:29:29 -0500484 void onPageChanged(boolean isFirst);
Jason Monk162011e2016-02-19 08:11:55 -0500485 }
Jason Monkcaf37622015-08-18 12:33:50 -0400486}