blob: e22a21ad1fedf132b41803154fef71a4fde63100 [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;
Fabian Kozynski72cd84a92019-04-15 14:09:43 -040066 private int mLastMaxHeight = -1;
Jason Monke5107a32016-05-31 15:40:58 -040067
Jason Monkcaf37622015-08-18 12:33:50 -040068 public PagedTileLayout(Context context, AttributeSet attrs) {
69 super(context, attrs);
Amin Shaikha07a17b2018-02-23 16:02:52 -050070 mScroller = new Scroller(context, SCROLL_CUBIC);
Jason Monkcaf37622015-08-18 12:33:50 -040071 setAdapter(mAdapter);
Amin Shaikha07a17b2018-02-23 16:02:52 -050072 setOnPageChangeListener(mOnPageChangeListener);
73 setCurrentItem(0, false);
Fabian Kozynski407ddb22018-10-03 15:04:56 -040074 mLayoutOrientation = getResources().getConfiguration().orientation;
75 mLayoutDirection = getLayoutDirection();
Fabian Kozynski87bdf0d2019-04-01 09:52:57 -040076 mClippingRect = new Rect();
Fabian Kozynski407ddb22018-10-03 15:04:56 -040077 }
78
79 public void saveInstanceState(Bundle outState) {
80 outState.putInt(CURRENT_PAGE, getCurrentItem());
81 }
82
83 public void restoreInstanceState(Bundle savedInstanceState) {
84 // There's only 1 page at this point. We want to restore the correct page once the
85 // pages have been inflated
86 mPageToRestore = savedInstanceState.getInt(CURRENT_PAGE, -1);
87 }
88
89 @Override
90 protected void onConfigurationChanged(Configuration newConfig) {
91 super.onConfigurationChanged(newConfig);
92 if (mLayoutOrientation != newConfig.orientation) {
93 mLayoutOrientation = newConfig.orientation;
94 setCurrentItem(0, false);
Fabian Kozynskid9e8aa32019-04-12 10:09:48 -040095 mPageToRestore = 0;
Fabian Kozynski407ddb22018-10-03 15:04:56 -040096 }
Jason Monkcaf37622015-08-18 12:33:50 -040097 }
98
99 @Override
Jason Monk51fb85a2016-03-28 14:06:04 -0400100 public void onRtlPropertiesChanged(int layoutDirection) {
101 super.onRtlPropertiesChanged(layoutDirection);
Fabian Kozynski407ddb22018-10-03 15:04:56 -0400102 if (mLayoutDirection != layoutDirection) {
103 mLayoutDirection = layoutDirection;
104 setAdapter(mAdapter);
105 setCurrentItem(0, false);
Fabian Kozynskid9e8aa32019-04-12 10:09:48 -0400106 mPageToRestore = 0;
Fabian Kozynski407ddb22018-10-03 15:04:56 -0400107 }
Jason Monk51fb85a2016-03-28 14:06:04 -0400108 }
109
110 @Override
111 public void setCurrentItem(int item, boolean smoothScroll) {
112 if (isLayoutRtl()) {
113 item = mPages.size() - 1 - item;
114 }
115 super.setCurrentItem(item, smoothScroll);
116 }
117
Fabian Kozynskid9e8aa32019-04-12 10:09:48 -0400118 /**
119 * Obtains the current page number respecting RTL
120 */
121 private int getCurrentPageNumber() {
122 int page = getCurrentItem();
123 if (mLayoutDirection == LAYOUT_DIRECTION_RTL) {
124 page = mPages.size() - 1 - page;
125 }
126 return page;
127 }
128
Jason Monk51fb85a2016-03-28 14:06:04 -0400129 @Override
Jason Monke5107a32016-05-31 15:40:58 -0400130 public void setListening(boolean listening) {
131 if (mListening == listening) return;
132 mListening = listening;
Amin Shaikh9978c552018-03-22 07:24:41 -0400133 updateListening();
134 }
135
136 private void updateListening() {
137 for (TilePage tilePage : mPages) {
138 tilePage.setListening(tilePage.getParent() == null ? false : mListening);
Jason Monke5107a32016-05-31 15:40:58 -0400139 }
140 }
141
Amin Shaikha07a17b2018-02-23 16:02:52 -0500142 @Override
Amin Shaikha07a17b2018-02-23 16:02:52 -0500143 public void computeScroll() {
144 if (!mScroller.isFinished() && mScroller.computeScrollOffset()) {
Amin Shaikh5484d0f2018-07-31 10:44:11 -0400145 fakeDragBy(getScrollX() - mScroller.getCurrX());
Amin Shaikha07a17b2018-02-23 16:02:52 -0500146 // Keep on drawing until the animation has finished.
147 postInvalidateOnAnimation();
148 return;
Amin Shaikh5484d0f2018-07-31 10:44:11 -0400149 } else if (isFakeDragging()) {
150 endFakeDrag();
Amin Shaikha07a17b2018-02-23 16:02:52 -0500151 mBounceAnimatorSet.start();
152 setOffscreenPageLimit(1);
Amin Shaikha07a17b2018-02-23 16:02:52 -0500153 }
154 super.computeScroll();
155 }
156
Jason Monke5107a32016-05-31 15:40:58 -0400157 @Override
Jason Monkc5bdafb2016-02-25 16:24:21 -0500158 public boolean hasOverlappingRendering() {
159 return false;
160 }
161
162 @Override
Jason Monkcaf37622015-08-18 12:33:50 -0400163 protected void onFinishInflate() {
164 super.onFinishInflate();
Jason Monk32508852017-01-18 09:17:13 -0500165 mPages.add((TilePage) LayoutInflater.from(getContext())
Jason Monke4e69302016-01-20 11:27:15 -0500166 .inflate(R.layout.qs_paged_page, this, false));
Fabian Kozynskifae42a82018-10-04 11:18:42 -0400167 mAdapter.notifyDataSetChanged();
Jason Monkcaf37622015-08-18 12:33:50 -0400168 }
169
Jason Monk32508852017-01-18 09:17:13 -0500170 public void setPageIndicator(PageIndicator indicator) {
171 mPageIndicator = indicator;
Fabian Kozynski712ae392018-09-12 09:11:16 -0400172 mPageIndicator.setNumPages(mPages.size());
Rohan Shah3090e792018-04-12 00:01:00 -0400173 mPageIndicator.setLocation(mPageIndicatorPosition);
Jason Monk32508852017-01-18 09:17:13 -0500174 }
175
Jason Monkcaf37622015-08-18 12:33:50 -0400176 @Override
177 public int getOffsetTop(TileRecord tile) {
Jason Monkae5bd032016-03-02 14:38:31 -0500178 final ViewGroup parent = (ViewGroup) tile.tileView.getParent();
179 if (parent == null) return 0;
180 return parent.getTop() + getTop();
Jason Monkcaf37622015-08-18 12:33:50 -0400181 }
182
183 @Override
Jason Monkcaf37622015-08-18 12:33:50 -0400184 public void addTile(TileRecord tile) {
185 mTiles.add(tile);
Fabian Kozynski712ae392018-09-12 09:11:16 -0400186 mDistributeTiles = true;
187 requestLayout();
Jason Monkcaf37622015-08-18 12:33:50 -0400188 }
189
190 @Override
191 public void removeTile(TileRecord tile) {
192 if (mTiles.remove(tile)) {
Fabian Kozynski712ae392018-09-12 09:11:16 -0400193 mDistributeTiles = true;
194 requestLayout();
Jason Monkcaf37622015-08-18 12:33:50 -0400195 }
196 }
197
Amin Shaikh0f8ea5432018-03-27 11:09:27 -0400198 @Override
199 public void setExpansion(float expansion) {
Amin Shaikh4c9048c2018-04-20 11:27:46 -0400200 mLastExpansion = expansion;
201 updateSelected();
202 }
203
204 private void updateSelected() {
205 // Start the marquee when fully expanded and stop when fully collapsed. Leave as is for
206 // other expansion ratios since there is no way way to pause the marquee.
207 if (mLastExpansion > 0f && mLastExpansion < 1f) {
208 return;
209 }
210 boolean selected = mLastExpansion == 1f;
Rohan Shah2dbcb572018-05-25 10:51:22 -0700211
212 // Disable accessibility temporarily while we update selected state purely for the
213 // marquee. This will ensure that accessibility doesn't announce the TYPE_VIEW_SELECTED
214 // event on any of the children.
215 setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
Fabian Kozynskid9e8aa32019-04-12 10:09:48 -0400216 int currentItem = getCurrentPageNumber();
Amin Shaikh4c9048c2018-04-20 11:27:46 -0400217 for (int i = 0; i < mPages.size(); i++) {
Amin Shaikh6f570742018-06-01 17:18:19 -0400218 mPages.get(i).setSelected(i == currentItem ? selected : false);
Amin Shaikh0f8ea5432018-03-27 11:09:27 -0400219 }
Rohan Shah2dbcb572018-05-25 10:51:22 -0700220 setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_AUTO);
Amin Shaikh0f8ea5432018-03-27 11:09:27 -0400221 }
222
Jason Monk162011e2016-02-19 08:11:55 -0500223 public void setPageListener(PageListener listener) {
224 mPageListener = listener;
225 }
226
Jason Monkcaf37622015-08-18 12:33:50 -0400227 private void distributeTiles() {
Fabian Kozynski712ae392018-09-12 09:11:16 -0400228 emptyAndInflateOrRemovePages();
229
230 final int tileCount = mPages.get(0).maxTiles();
Jason Monkcaf37622015-08-18 12:33:50 -0400231 if (DEBUG) Log.d(TAG, "Distributing tiles");
Jason Monkcaf37622015-08-18 12:33:50 -0400232 int index = 0;
233 final int NT = mTiles.size();
234 for (int i = 0; i < NT; i++) {
235 TileRecord tile = mTiles.get(i);
Fabian Kozynski712ae392018-09-12 09:11:16 -0400236 if (mPages.get(index).mRecords.size() == tileCount) index++;
237 if (DEBUG) {
238 Log.d(TAG, "Adding " + tile.tile.getClass().getSimpleName() + " to "
239 + index);
Jason Monkcaf37622015-08-18 12:33:50 -0400240 }
Jason Monkcaf37622015-08-18 12:33:50 -0400241 mPages.get(index).addTile(tile);
242 }
Fabian Kozynski712ae392018-09-12 09:11:16 -0400243 }
244
245 private void emptyAndInflateOrRemovePages() {
246 final int nTiles = mTiles.size();
247 int numPages = nTiles / mPages.get(0).maxTiles();
248 // Add one more not full page if needed
249 numPages += (nTiles % mPages.get(0).maxTiles() == 0 ? 0 : 1);
250
251 final int NP = mPages.size();
252 for (int i = 0; i < NP; i++) {
253 mPages.get(i).removeAllViews();
Jason Monkcaf37622015-08-18 12:33:50 -0400254 }
Fabian Kozynski712ae392018-09-12 09:11:16 -0400255 if (NP == numPages) {
256 return;
257 }
258 while (mPages.size() < numPages) {
259 if (DEBUG) Log.d(TAG, "Adding page");
260 mPages.add((TilePage) LayoutInflater.from(getContext())
261 .inflate(R.layout.qs_paged_page, this, false));
262 }
263 while (mPages.size() > numPages) {
264 if (DEBUG) Log.d(TAG, "Removing page");
265 mPages.remove(mPages.size() - 1);
266 }
267 mPageIndicator.setNumPages(mPages.size());
268 setAdapter(mAdapter);
269 mAdapter.notifyDataSetChanged();
Fabian Kozynski407ddb22018-10-03 15:04:56 -0400270 if (mPageToRestore != -1) {
271 setCurrentItem(mPageToRestore, false);
272 mPageToRestore = -1;
273 }
Jason Monkcaf37622015-08-18 12:33:50 -0400274 }
275
276 @Override
Jason Monk9d02a432016-01-20 16:33:46 -0500277 public boolean updateResources() {
Rohan Shah3090e792018-04-12 00:01:00 -0400278 // Update bottom padding, useful for removing extra space once the panel page indicator is
279 // hidden.
Fabian Kozynski6eaf3ac2018-10-01 15:59:05 -0400280 Resources res = getContext().getResources();
Fabian Kozynski52bd8c22018-10-08 12:52:51 -0400281 mHorizontalClipBound = res.getDimensionPixelSize(R.dimen.notification_side_paddings);
282 setPadding(0, 0, 0,
Rohan Shah3090e792018-04-12 00:01:00 -0400283 getContext().getResources().getDimensionPixelSize(
284 R.dimen.qs_paged_tile_layout_padding_bottom));
Jason Monk9d02a432016-01-20 16:33:46 -0500285 boolean changed = false;
Jason Monkcaf37622015-08-18 12:33:50 -0400286 for (int i = 0; i < mPages.size(); i++) {
Jason Monk9d02a432016-01-20 16:33:46 -0500287 changed |= mPages.get(i).updateResources();
Jason Monkcaf37622015-08-18 12:33:50 -0400288 }
Jason Monk9d02a432016-01-20 16:33:46 -0500289 if (changed) {
Fabian Kozynski712ae392018-09-12 09:11:16 -0400290 mDistributeTiles = true;
291 requestLayout();
Jason Monk9d02a432016-01-20 16:33:46 -0500292 }
293 return changed;
Jason Monkcaf37622015-08-18 12:33:50 -0400294 }
295
296 @Override
Fabian Kozynski52bd8c22018-10-08 12:52:51 -0400297 protected void onLayout(boolean changed, int l, int t, int r, int b) {
298 super.onLayout(changed, l, t, r, b);
Fabian Kozynski87bdf0d2019-04-01 09:52:57 -0400299 mClippingRect.set(mHorizontalClipBound, 0, (r - l) - mHorizontalClipBound, b - t);
300 setClipBounds(mClippingRect);
Fabian Kozynski52bd8c22018-10-08 12:52:51 -0400301 }
302
303 @Override
Jason Monkcaf37622015-08-18 12:33:50 -0400304 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Fabian Kozynski712ae392018-09-12 09:11:16 -0400305
306 final int nTiles = mTiles.size();
Fabian Kozynski72cd84a92019-04-15 14:09:43 -0400307 // If we have no reason to recalculate the number of rows, skip this step. In particular,
308 // if the height passed by its parent is the same as the last time, we try not to remeasure.
309 if (mDistributeTiles || mLastMaxHeight != MeasureSpec.getSize(heightMeasureSpec)) {
Fabian Kozynski712ae392018-09-12 09:11:16 -0400310
Fabian Kozynski72cd84a92019-04-15 14:09:43 -0400311 mLastMaxHeight = MeasureSpec.getSize(heightMeasureSpec);
Fabian Kozynski712ae392018-09-12 09:11:16 -0400312 // Only change the pages if the number of rows or columns (from updateResources) has
313 // changed or the tiles have changed
314 if (mPages.get(0).updateMaxRows(heightMeasureSpec, nTiles) || mDistributeTiles) {
315 mDistributeTiles = false;
316 distributeTiles();
317 }
318
319 final int nRows = mPages.get(0).mRows;
320 for (int i = 0; i < mPages.size(); i++) {
321 TilePage t = mPages.get(i);
322 t.mRows = nRows;
323 }
324 }
325
Jason Monkcaf37622015-08-18 12:33:50 -0400326 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
Fabian Kozynski712ae392018-09-12 09:11:16 -0400327
Jason Monkcaf37622015-08-18 12:33:50 -0400328 // The ViewPager likes to eat all of the space, instead force it to wrap to the max height
329 // of the pages.
330 int maxHeight = 0;
331 final int N = getChildCount();
332 for (int i = 0; i < N; i++) {
333 int height = getChildAt(i).getMeasuredHeight();
334 if (height > maxHeight) {
335 maxHeight = height;
336 }
337 }
Jason Monkf13413e2017-02-15 15:49:32 -0500338 setMeasuredDimension(getMeasuredWidth(), maxHeight + getPaddingBottom());
Jason Monkcaf37622015-08-18 12:33:50 -0400339 }
340
Jason Monk8fb77872016-03-03 16:39:42 -0500341 public int getColumnCount() {
342 if (mPages.size() == 0) return 0;
343 return mPages.get(0).mColumns;
344 }
345
Fabian Kozynski802279f2018-09-07 13:44:54 -0400346 public int getNumVisibleTiles() {
347 if (mPages.size() == 0) return 0;
Fabian Kozynskid9e8aa32019-04-12 10:09:48 -0400348 TilePage currentPage = mPages.get(getCurrentPageNumber());
Fabian Kozynski802279f2018-09-07 13:44:54 -0400349 return currentPage.mRecords.size();
350 }
351
Amin Shaikha07a17b2018-02-23 16:02:52 -0500352 public void startTileReveal(Set<String> tileSpecs, final Runnable postAnimation) {
Amin Shaikh5484d0f2018-07-31 10:44:11 -0400353 if (tileSpecs.isEmpty() || mPages.size() < 2 || getScrollX() != 0 || !beginFakeDrag()) {
Amin Shaikha07a17b2018-02-23 16:02:52 -0500354 // Do not start the reveal animation unless there are tiles to animate, multiple
355 // TilePages available and the user has not already started dragging.
356 return;
357 }
358
359 final int lastPageNumber = mPages.size() - 1;
360 final TilePage lastPage = mPages.get(lastPageNumber);
361 final ArrayList<Animator> bounceAnims = new ArrayList<>();
362 for (TileRecord tr : lastPage.mRecords) {
363 if (tileSpecs.contains(tr.tile.getTileSpec())) {
364 bounceAnims.add(setupBounceAnimator(tr.tileView, bounceAnims.size()));
365 }
366 }
367
368 if (bounceAnims.isEmpty()) {
369 // All tileSpecs are on the first page. Nothing to do.
370 // TODO: potentially show a bounce animation for first page QS tiles
Amin Shaikh5484d0f2018-07-31 10:44:11 -0400371 endFakeDrag();
Amin Shaikha07a17b2018-02-23 16:02:52 -0500372 return;
373 }
374
375 mBounceAnimatorSet = new AnimatorSet();
376 mBounceAnimatorSet.playTogether(bounceAnims);
377 mBounceAnimatorSet.addListener(new AnimatorListenerAdapter() {
378 @Override
379 public void onAnimationEnd(Animator animation) {
380 mBounceAnimatorSet = null;
381 postAnimation.run();
382 }
383 });
Amin Shaikh5484d0f2018-07-31 10:44:11 -0400384 setOffscreenPageLimit(lastPageNumber); // Ensure the page to reveal has been inflated.
385 int dx = getWidth() * lastPageNumber;
386 mScroller.startScroll(getScrollX(), getScrollY(), isLayoutRtl() ? -dx : dx, 0,
387 REVEAL_SCROLL_DURATION_MILLIS);
Amin Shaikha07a17b2018-02-23 16:02:52 -0500388 postInvalidateOnAnimation();
389 }
390
391 private static Animator setupBounceAnimator(View view, int ordinal) {
392 view.setAlpha(0f);
393 view.setScaleX(0f);
394 view.setScaleY(0f);
395 ObjectAnimator animator = ObjectAnimator.ofPropertyValuesHolder(view,
396 PropertyValuesHolder.ofFloat(View.ALPHA, 1),
397 PropertyValuesHolder.ofFloat(View.SCALE_X, 1),
398 PropertyValuesHolder.ofFloat(View.SCALE_Y, 1));
399 animator.setDuration(BOUNCE_ANIMATION_DURATION);
400 animator.setStartDelay(ordinal * TILE_ANIMATION_STAGGER_DELAY);
401 animator.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
402 return animator;
403 }
404
405 private final ViewPager.OnPageChangeListener mOnPageChangeListener =
406 new ViewPager.SimpleOnPageChangeListener() {
407 @Override
408 public void onPageSelected(int position) {
Amin Shaikh4c9048c2018-04-20 11:27:46 -0400409 updateSelected();
Amin Shaikha07a17b2018-02-23 16:02:52 -0500410 if (mPageIndicator == null) return;
411 if (mPageListener != null) {
412 mPageListener.onPageChanged(isLayoutRtl() ? position == mPages.size() - 1
413 : position == 0);
414 }
415 }
416
417 @Override
418 public void onPageScrolled(int position, float positionOffset,
419 int positionOffsetPixels) {
420 if (mPageIndicator == null) return;
Rohan Shah3090e792018-04-12 00:01:00 -0400421 mPageIndicatorPosition = position + positionOffset;
422 mPageIndicator.setLocation(mPageIndicatorPosition);
Amin Shaikha07a17b2018-02-23 16:02:52 -0500423 if (mPageListener != null) {
424 mPageListener.onPageChanged(positionOffsetPixels == 0 &&
425 (isLayoutRtl() ? position == mPages.size() - 1 : position == 0));
426 }
427 }
428 };
429
Jason Monkcaf37622015-08-18 12:33:50 -0400430 public static class TilePage extends TileLayout {
Fabian Kozynski712ae392018-09-12 09:11:16 -0400431
Jason Monkcaf37622015-08-18 12:33:50 -0400432 public TilePage(Context context, AttributeSet attrs) {
433 super(context, attrs);
Jason Monkbd6dbb02015-09-03 15:46:25 -0400434 }
435
Jason Monkbd6dbb02015-09-03 15:46:25 -0400436 public boolean isFull() {
Fabian Kozynski712ae392018-09-12 09:11:16 -0400437 return mRecords.size() >= mColumns * mRows;
438 }
439
440 public int maxTiles() {
441 return mColumns * mRows;
Jason Monkcaf37622015-08-18 12:33:50 -0400442 }
Fabian Kozynski52bd8c22018-10-08 12:52:51 -0400443
444 @Override
445 public boolean updateResources() {
446 final int sidePadding = getContext().getResources().getDimensionPixelSize(
447 R.dimen.notification_side_paddings);
448 setPadding(sidePadding, 0, sidePadding, 0);
449 return super.updateResources();
450 }
Jason Monkcaf37622015-08-18 12:33:50 -0400451 }
452
453 private final PagerAdapter mAdapter = new PagerAdapter() {
Amin Shaikh9978c552018-03-22 07:24:41 -0400454 @Override
Jason Monkcaf37622015-08-18 12:33:50 -0400455 public void destroyItem(ViewGroup container, int position, Object object) {
456 if (DEBUG) Log.d(TAG, "Destantiating " + position);
Jason Monkcaf37622015-08-18 12:33:50 -0400457 container.removeView((View) object);
Amin Shaikh9978c552018-03-22 07:24:41 -0400458 updateListening();
Jason Monkcaf37622015-08-18 12:33:50 -0400459 }
460
Amin Shaikh9978c552018-03-22 07:24:41 -0400461 @Override
Jason Monkcaf37622015-08-18 12:33:50 -0400462 public Object instantiateItem(ViewGroup container, int position) {
463 if (DEBUG) Log.d(TAG, "Instantiating " + position);
Jason Monk51fb85a2016-03-28 14:06:04 -0400464 if (isLayoutRtl()) {
465 position = mPages.size() - 1 - position;
466 }
Jason Monke4e69302016-01-20 11:27:15 -0500467 ViewGroup view = mPages.get(position);
Amin Shaikh53bc0832018-09-17 15:06:18 -0400468 if (view.getParent() != null) {
469 container.removeView(view);
470 }
Jason Monkcaf37622015-08-18 12:33:50 -0400471 container.addView(view);
Amin Shaikh9978c552018-03-22 07:24:41 -0400472 updateListening();
Jason Monkcaf37622015-08-18 12:33:50 -0400473 return view;
474 }
475
476 @Override
477 public int getCount() {
Fabian Kozynski712ae392018-09-12 09:11:16 -0400478 return mPages.size();
Jason Monkcaf37622015-08-18 12:33:50 -0400479 }
480
481 @Override
482 public boolean isViewFromObject(View view, Object object) {
483 return view == object;
484 }
485 };
Jason Monk162011e2016-02-19 08:11:55 -0500486
487 public interface PageListener {
Jason Monk66eaf312016-02-25 12:29:29 -0500488 void onPageChanged(boolean isFirst);
Jason Monk162011e2016-02-19 08:11:55 -0500489 }
Jason Monkcaf37622015-08-18 12:33:50 -0400490}