Jason Monk | caf3762 | 2015-08-18 12:33:50 -0400 | [diff] [blame] | 1 | package com.android.systemui.qs; |
| 2 | |
Amin Shaikh | a07a17b | 2018-02-23 16:02:52 -0500 | [diff] [blame] | 3 | import android.animation.Animator; |
| 4 | import android.animation.AnimatorListenerAdapter; |
| 5 | import android.animation.AnimatorSet; |
| 6 | import android.animation.ObjectAnimator; |
| 7 | import android.animation.PropertyValuesHolder; |
Jason Monk | caf3762 | 2015-08-18 12:33:50 -0400 | [diff] [blame] | 8 | import android.content.Context; |
Jason Monk | 6573ef2 | 2016-04-06 12:37:18 -0400 | [diff] [blame] | 9 | import android.content.res.Configuration; |
| 10 | import android.content.res.Resources; |
Aurimas Liutikas | fd52c14 | 2018-04-17 09:50:46 -0700 | [diff] [blame] | 11 | import androidx.viewpager.widget.PagerAdapter; |
| 12 | import androidx.viewpager.widget.ViewPager; |
Jason Monk | caf3762 | 2015-08-18 12:33:50 -0400 | [diff] [blame] | 13 | import android.util.AttributeSet; |
| 14 | import android.util.Log; |
| 15 | import android.view.LayoutInflater; |
Amin Shaikh | a07a17b | 2018-02-23 16:02:52 -0500 | [diff] [blame] | 16 | import android.view.MotionEvent; |
Jason Monk | caf3762 | 2015-08-18 12:33:50 -0400 | [diff] [blame] | 17 | import android.view.View; |
| 18 | import android.view.ViewGroup; |
Amin Shaikh | a07a17b | 2018-02-23 16:02:52 -0500 | [diff] [blame] | 19 | import android.view.animation.Interpolator; |
| 20 | import android.view.animation.OvershootInterpolator; |
| 21 | import android.widget.Scroller; |
Jason Monk | df36aed | 2016-07-25 11:21:56 -0400 | [diff] [blame] | 22 | |
Jason Monk | caf3762 | 2015-08-18 12:33:50 -0400 | [diff] [blame] | 23 | import com.android.systemui.R; |
| 24 | import com.android.systemui.qs.QSPanel.QSTileLayout; |
| 25 | import com.android.systemui.qs.QSPanel.TileRecord; |
| 26 | |
| 27 | import java.util.ArrayList; |
Amin Shaikh | a07a17b | 2018-02-23 16:02:52 -0500 | [diff] [blame] | 28 | import java.util.Set; |
Jason Monk | caf3762 | 2015-08-18 12:33:50 -0400 | [diff] [blame] | 29 | |
| 30 | public class PagedTileLayout extends ViewPager implements QSTileLayout { |
| 31 | |
| 32 | private static final boolean DEBUG = false; |
| 33 | |
| 34 | private static final String TAG = "PagedTileLayout"; |
Amin Shaikh | a07a17b | 2018-02-23 16:02:52 -0500 | [diff] [blame] | 35 | 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 Monk | caf3762 | 2015-08-18 12:33:50 -0400 | [diff] [blame] | 44 | |
Amin Shaikh | 9978c55 | 2018-03-22 07:24:41 -0400 | [diff] [blame] | 45 | private final ArrayList<TileRecord> mTiles = new ArrayList<>(); |
| 46 | private final ArrayList<TilePage> mPages = new ArrayList<>(); |
Jason Monk | caf3762 | 2015-08-18 12:33:50 -0400 | [diff] [blame] | 47 | |
Jason Monk | caf3762 | 2015-08-18 12:33:50 -0400 | [diff] [blame] | 48 | private PageIndicator mPageIndicator; |
Rohan Shah | 3090e79 | 2018-04-12 00:01:00 -0400 | [diff] [blame] | 49 | private float mPageIndicatorPosition; |
Jason Monk | caf3762 | 2015-08-18 12:33:50 -0400 | [diff] [blame] | 50 | |
| 51 | private int mNumPages; |
Jason Monk | 162011e | 2016-02-19 08:11:55 -0500 | [diff] [blame] | 52 | private PageListener mPageListener; |
Jason Monk | caf3762 | 2015-08-18 12:33:50 -0400 | [diff] [blame] | 53 | |
Jason Monk | e5107a3 | 2016-05-31 15:40:58 -0400 | [diff] [blame] | 54 | private boolean mListening; |
Amin Shaikh | a07a17b | 2018-02-23 16:02:52 -0500 | [diff] [blame] | 55 | private Scroller mScroller; |
| 56 | |
| 57 | private AnimatorSet mBounceAnimatorSet; |
| 58 | private int mAnimatingToPage = -1; |
Amin Shaikh | 4c9048c | 2018-04-20 11:27:46 -0400 | [diff] [blame] | 59 | private float mLastExpansion; |
Jason Monk | e5107a3 | 2016-05-31 15:40:58 -0400 | [diff] [blame] | 60 | |
Jason Monk | caf3762 | 2015-08-18 12:33:50 -0400 | [diff] [blame] | 61 | public PagedTileLayout(Context context, AttributeSet attrs) { |
| 62 | super(context, attrs); |
Amin Shaikh | a07a17b | 2018-02-23 16:02:52 -0500 | [diff] [blame] | 63 | mScroller = new Scroller(context, SCROLL_CUBIC); |
Jason Monk | caf3762 | 2015-08-18 12:33:50 -0400 | [diff] [blame] | 64 | setAdapter(mAdapter); |
Amin Shaikh | a07a17b | 2018-02-23 16:02:52 -0500 | [diff] [blame] | 65 | setOnPageChangeListener(mOnPageChangeListener); |
| 66 | setCurrentItem(0, false); |
Jason Monk | caf3762 | 2015-08-18 12:33:50 -0400 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | @Override |
Jason Monk | 51fb85a | 2016-03-28 14:06:04 -0400 | [diff] [blame] | 70 | public void onRtlPropertiesChanged(int layoutDirection) { |
| 71 | super.onRtlPropertiesChanged(layoutDirection); |
| 72 | setAdapter(mAdapter); |
| 73 | setCurrentItem(0, false); |
| 74 | } |
| 75 | |
| 76 | @Override |
| 77 | public void setCurrentItem(int item, boolean smoothScroll) { |
| 78 | if (isLayoutRtl()) { |
| 79 | item = mPages.size() - 1 - item; |
| 80 | } |
| 81 | super.setCurrentItem(item, smoothScroll); |
| 82 | } |
| 83 | |
| 84 | @Override |
Jason Monk | e5107a3 | 2016-05-31 15:40:58 -0400 | [diff] [blame] | 85 | public void setListening(boolean listening) { |
| 86 | if (mListening == listening) return; |
| 87 | mListening = listening; |
Amin Shaikh | 9978c55 | 2018-03-22 07:24:41 -0400 | [diff] [blame] | 88 | updateListening(); |
| 89 | } |
| 90 | |
| 91 | private void updateListening() { |
| 92 | for (TilePage tilePage : mPages) { |
| 93 | tilePage.setListening(tilePage.getParent() == null ? false : mListening); |
Jason Monk | e5107a3 | 2016-05-31 15:40:58 -0400 | [diff] [blame] | 94 | } |
| 95 | } |
| 96 | |
Amin Shaikh | a07a17b | 2018-02-23 16:02:52 -0500 | [diff] [blame] | 97 | @Override |
| 98 | public boolean onInterceptTouchEvent(MotionEvent ev) { |
| 99 | // Suppress all touch event during reveal animation. |
| 100 | if (mAnimatingToPage != -1) { |
| 101 | return true; |
| 102 | } |
| 103 | return super.onInterceptTouchEvent(ev); |
| 104 | } |
| 105 | |
| 106 | @Override |
| 107 | public boolean onTouchEvent(MotionEvent ev) { |
| 108 | // Suppress all touch event during reveal animation. |
| 109 | if (mAnimatingToPage != -1) { |
| 110 | return true; |
| 111 | } |
| 112 | return super.onTouchEvent(ev); |
| 113 | } |
| 114 | |
| 115 | @Override |
| 116 | public void computeScroll() { |
| 117 | if (!mScroller.isFinished() && mScroller.computeScrollOffset()) { |
| 118 | scrollTo(mScroller.getCurrX(), mScroller.getCurrY()); |
| 119 | float pageFraction = (float) getScrollX() / getWidth(); |
| 120 | int position = (int) pageFraction; |
| 121 | float positionOffset = pageFraction - position; |
| 122 | mOnPageChangeListener.onPageScrolled(position, positionOffset, getScrollX()); |
| 123 | // Keep on drawing until the animation has finished. |
| 124 | postInvalidateOnAnimation(); |
| 125 | return; |
| 126 | } |
| 127 | if (mAnimatingToPage != -1) { |
| 128 | setCurrentItem(mAnimatingToPage, true); |
| 129 | mBounceAnimatorSet.start(); |
| 130 | setOffscreenPageLimit(1); |
| 131 | mAnimatingToPage = -1; |
| 132 | } |
| 133 | super.computeScroll(); |
| 134 | } |
| 135 | |
Jason Monk | e5107a3 | 2016-05-31 15:40:58 -0400 | [diff] [blame] | 136 | @Override |
Jason Monk | c5bdafb | 2016-02-25 16:24:21 -0500 | [diff] [blame] | 137 | public boolean hasOverlappingRendering() { |
| 138 | return false; |
| 139 | } |
| 140 | |
| 141 | @Override |
Jason Monk | caf3762 | 2015-08-18 12:33:50 -0400 | [diff] [blame] | 142 | protected void onFinishInflate() { |
| 143 | super.onFinishInflate(); |
Jason Monk | 3250885 | 2017-01-18 09:17:13 -0500 | [diff] [blame] | 144 | mPages.add((TilePage) LayoutInflater.from(getContext()) |
Jason Monk | e4e6930 | 2016-01-20 11:27:15 -0500 | [diff] [blame] | 145 | .inflate(R.layout.qs_paged_page, this, false)); |
Jason Monk | caf3762 | 2015-08-18 12:33:50 -0400 | [diff] [blame] | 146 | } |
| 147 | |
Jason Monk | 3250885 | 2017-01-18 09:17:13 -0500 | [diff] [blame] | 148 | public void setPageIndicator(PageIndicator indicator) { |
| 149 | mPageIndicator = indicator; |
Rohan Shah | 3090e79 | 2018-04-12 00:01:00 -0400 | [diff] [blame] | 150 | mPageIndicator.setNumPages(mNumPages); |
| 151 | mPageIndicator.setLocation(mPageIndicatorPosition); |
Jason Monk | 3250885 | 2017-01-18 09:17:13 -0500 | [diff] [blame] | 152 | } |
| 153 | |
Jason Monk | caf3762 | 2015-08-18 12:33:50 -0400 | [diff] [blame] | 154 | @Override |
| 155 | public int getOffsetTop(TileRecord tile) { |
Jason Monk | ae5bd03 | 2016-03-02 14:38:31 -0500 | [diff] [blame] | 156 | final ViewGroup parent = (ViewGroup) tile.tileView.getParent(); |
| 157 | if (parent == null) return 0; |
| 158 | return parent.getTop() + getTop(); |
Jason Monk | caf3762 | 2015-08-18 12:33:50 -0400 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | @Override |
Jason Monk | caf3762 | 2015-08-18 12:33:50 -0400 | [diff] [blame] | 162 | public void addTile(TileRecord tile) { |
| 163 | mTiles.add(tile); |
Jason Monk | 9d02a43 | 2016-01-20 16:33:46 -0500 | [diff] [blame] | 164 | postDistributeTiles(); |
Jason Monk | caf3762 | 2015-08-18 12:33:50 -0400 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | @Override |
| 168 | public void removeTile(TileRecord tile) { |
| 169 | if (mTiles.remove(tile)) { |
Jason Monk | 9d02a43 | 2016-01-20 16:33:46 -0500 | [diff] [blame] | 170 | postDistributeTiles(); |
Jason Monk | caf3762 | 2015-08-18 12:33:50 -0400 | [diff] [blame] | 171 | } |
| 172 | } |
| 173 | |
Amin Shaikh | 0f8ea543 | 2018-03-27 11:09:27 -0400 | [diff] [blame] | 174 | @Override |
| 175 | public void setExpansion(float expansion) { |
Amin Shaikh | 4c9048c | 2018-04-20 11:27:46 -0400 | [diff] [blame] | 176 | mLastExpansion = expansion; |
| 177 | updateSelected(); |
| 178 | } |
| 179 | |
| 180 | private void updateSelected() { |
| 181 | // Start the marquee when fully expanded and stop when fully collapsed. Leave as is for |
| 182 | // other expansion ratios since there is no way way to pause the marquee. |
| 183 | if (mLastExpansion > 0f && mLastExpansion < 1f) { |
| 184 | return; |
| 185 | } |
| 186 | boolean selected = mLastExpansion == 1f; |
| 187 | for (int i = 0; i < mPages.size(); i++) { |
| 188 | mPages.get(i).setSelected(i == getCurrentItem() ? selected : false); |
Amin Shaikh | 0f8ea543 | 2018-03-27 11:09:27 -0400 | [diff] [blame] | 189 | } |
| 190 | } |
| 191 | |
Jason Monk | 162011e | 2016-02-19 08:11:55 -0500 | [diff] [blame] | 192 | public void setPageListener(PageListener listener) { |
| 193 | mPageListener = listener; |
| 194 | } |
| 195 | |
Jason Monk | 9d02a43 | 2016-01-20 16:33:46 -0500 | [diff] [blame] | 196 | private void postDistributeTiles() { |
| 197 | removeCallbacks(mDistribute); |
| 198 | post(mDistribute); |
| 199 | } |
| 200 | |
Jason Monk | caf3762 | 2015-08-18 12:33:50 -0400 | [diff] [blame] | 201 | private void distributeTiles() { |
| 202 | if (DEBUG) Log.d(TAG, "Distributing tiles"); |
Jason Monk | caf3762 | 2015-08-18 12:33:50 -0400 | [diff] [blame] | 203 | final int NP = mPages.size(); |
| 204 | for (int i = 0; i < NP; i++) { |
Jason Monk | 9d02a43 | 2016-01-20 16:33:46 -0500 | [diff] [blame] | 205 | mPages.get(i).removeAllViews(); |
Jason Monk | caf3762 | 2015-08-18 12:33:50 -0400 | [diff] [blame] | 206 | } |
| 207 | int index = 0; |
| 208 | final int NT = mTiles.size(); |
| 209 | for (int i = 0; i < NT; i++) { |
| 210 | TileRecord tile = mTiles.get(i); |
Jason Monk | caf3762 | 2015-08-18 12:33:50 -0400 | [diff] [blame] | 211 | if (mPages.get(index).isFull()) { |
| 212 | if (++index == mPages.size()) { |
Jason Monk | 8e73ef3 | 2016-05-04 11:36:46 -0400 | [diff] [blame] | 213 | if (DEBUG) Log.d(TAG, "Adding page for " |
| 214 | + tile.tile.getClass().getSimpleName()); |
Jason Monk | 3250885 | 2017-01-18 09:17:13 -0500 | [diff] [blame] | 215 | mPages.add((TilePage) LayoutInflater.from(getContext()) |
Jason Monk | caf3762 | 2015-08-18 12:33:50 -0400 | [diff] [blame] | 216 | .inflate(R.layout.qs_paged_page, this, false)); |
| 217 | } |
| 218 | } |
| 219 | if (DEBUG) Log.d(TAG, "Adding " + tile.tile.getClass().getSimpleName() + " to " |
| 220 | + index); |
| 221 | mPages.get(index).addTile(tile); |
| 222 | } |
| 223 | if (mNumPages != index + 1) { |
| 224 | mNumPages = index + 1; |
Jason Monk | 8e73ef3 | 2016-05-04 11:36:46 -0400 | [diff] [blame] | 225 | while (mPages.size() > mNumPages) { |
| 226 | mPages.remove(mPages.size() - 1); |
| 227 | } |
| 228 | if (DEBUG) Log.d(TAG, "Size: " + mNumPages); |
Jason Monk | caf3762 | 2015-08-18 12:33:50 -0400 | [diff] [blame] | 229 | mPageIndicator.setNumPages(mNumPages); |
Jason Monk | 8e73ef3 | 2016-05-04 11:36:46 -0400 | [diff] [blame] | 230 | setAdapter(mAdapter); |
Jason Monk | caf3762 | 2015-08-18 12:33:50 -0400 | [diff] [blame] | 231 | mAdapter.notifyDataSetChanged(); |
Jason Monk | 51fb85a | 2016-03-28 14:06:04 -0400 | [diff] [blame] | 232 | setCurrentItem(0, false); |
Jason Monk | caf3762 | 2015-08-18 12:33:50 -0400 | [diff] [blame] | 233 | } |
| 234 | } |
| 235 | |
| 236 | @Override |
Jason Monk | 9d02a43 | 2016-01-20 16:33:46 -0500 | [diff] [blame] | 237 | public boolean updateResources() { |
Rohan Shah | 3090e79 | 2018-04-12 00:01:00 -0400 | [diff] [blame] | 238 | // Update bottom padding, useful for removing extra space once the panel page indicator is |
| 239 | // hidden. |
| 240 | setPadding(0, 0, 0, |
| 241 | getContext().getResources().getDimensionPixelSize( |
| 242 | R.dimen.qs_paged_tile_layout_padding_bottom)); |
| 243 | |
Jason Monk | 9d02a43 | 2016-01-20 16:33:46 -0500 | [diff] [blame] | 244 | boolean changed = false; |
Jason Monk | caf3762 | 2015-08-18 12:33:50 -0400 | [diff] [blame] | 245 | for (int i = 0; i < mPages.size(); i++) { |
Jason Monk | 9d02a43 | 2016-01-20 16:33:46 -0500 | [diff] [blame] | 246 | changed |= mPages.get(i).updateResources(); |
Jason Monk | caf3762 | 2015-08-18 12:33:50 -0400 | [diff] [blame] | 247 | } |
Jason Monk | 9d02a43 | 2016-01-20 16:33:46 -0500 | [diff] [blame] | 248 | if (changed) { |
| 249 | distributeTiles(); |
| 250 | } |
| 251 | return changed; |
Jason Monk | caf3762 | 2015-08-18 12:33:50 -0400 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | @Override |
| 255 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 256 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
| 257 | // The ViewPager likes to eat all of the space, instead force it to wrap to the max height |
| 258 | // of the pages. |
| 259 | int maxHeight = 0; |
| 260 | final int N = getChildCount(); |
| 261 | for (int i = 0; i < N; i++) { |
| 262 | int height = getChildAt(i).getMeasuredHeight(); |
| 263 | if (height > maxHeight) { |
| 264 | maxHeight = height; |
| 265 | } |
| 266 | } |
Jason Monk | f13413e | 2017-02-15 15:49:32 -0500 | [diff] [blame] | 267 | setMeasuredDimension(getMeasuredWidth(), maxHeight + getPaddingBottom()); |
Jason Monk | caf3762 | 2015-08-18 12:33:50 -0400 | [diff] [blame] | 268 | } |
| 269 | |
Jason Monk | 9d02a43 | 2016-01-20 16:33:46 -0500 | [diff] [blame] | 270 | private final Runnable mDistribute = new Runnable() { |
| 271 | @Override |
| 272 | public void run() { |
| 273 | distributeTiles(); |
| 274 | } |
| 275 | }; |
| 276 | |
Jason Monk | 8fb7787 | 2016-03-03 16:39:42 -0500 | [diff] [blame] | 277 | public int getColumnCount() { |
| 278 | if (mPages.size() == 0) return 0; |
| 279 | return mPages.get(0).mColumns; |
| 280 | } |
| 281 | |
Amin Shaikh | a07a17b | 2018-02-23 16:02:52 -0500 | [diff] [blame] | 282 | public void startTileReveal(Set<String> tileSpecs, final Runnable postAnimation) { |
| 283 | if (tileSpecs.isEmpty() || mPages.size() < 2 || getScrollX() != 0) { |
| 284 | // Do not start the reveal animation unless there are tiles to animate, multiple |
| 285 | // TilePages available and the user has not already started dragging. |
| 286 | return; |
| 287 | } |
| 288 | |
| 289 | final int lastPageNumber = mPages.size() - 1; |
| 290 | final TilePage lastPage = mPages.get(lastPageNumber); |
| 291 | final ArrayList<Animator> bounceAnims = new ArrayList<>(); |
| 292 | for (TileRecord tr : lastPage.mRecords) { |
| 293 | if (tileSpecs.contains(tr.tile.getTileSpec())) { |
| 294 | bounceAnims.add(setupBounceAnimator(tr.tileView, bounceAnims.size())); |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | if (bounceAnims.isEmpty()) { |
| 299 | // All tileSpecs are on the first page. Nothing to do. |
| 300 | // TODO: potentially show a bounce animation for first page QS tiles |
| 301 | return; |
| 302 | } |
| 303 | |
| 304 | mBounceAnimatorSet = new AnimatorSet(); |
| 305 | mBounceAnimatorSet.playTogether(bounceAnims); |
| 306 | mBounceAnimatorSet.addListener(new AnimatorListenerAdapter() { |
| 307 | @Override |
| 308 | public void onAnimationEnd(Animator animation) { |
| 309 | mBounceAnimatorSet = null; |
| 310 | postAnimation.run(); |
| 311 | } |
| 312 | }); |
| 313 | mAnimatingToPage = lastPageNumber; |
| 314 | setOffscreenPageLimit(mAnimatingToPage); // Ensure the page to reveal has been inflated. |
| 315 | mScroller.startScroll(getScrollX(), getScrollY(), getWidth() * mAnimatingToPage, 0, |
| 316 | REVEAL_SCROLL_DURATION_MILLIS); |
| 317 | postInvalidateOnAnimation(); |
| 318 | } |
| 319 | |
| 320 | private static Animator setupBounceAnimator(View view, int ordinal) { |
| 321 | view.setAlpha(0f); |
| 322 | view.setScaleX(0f); |
| 323 | view.setScaleY(0f); |
| 324 | ObjectAnimator animator = ObjectAnimator.ofPropertyValuesHolder(view, |
| 325 | PropertyValuesHolder.ofFloat(View.ALPHA, 1), |
| 326 | PropertyValuesHolder.ofFloat(View.SCALE_X, 1), |
| 327 | PropertyValuesHolder.ofFloat(View.SCALE_Y, 1)); |
| 328 | animator.setDuration(BOUNCE_ANIMATION_DURATION); |
| 329 | animator.setStartDelay(ordinal * TILE_ANIMATION_STAGGER_DELAY); |
| 330 | animator.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION)); |
| 331 | return animator; |
| 332 | } |
| 333 | |
| 334 | private final ViewPager.OnPageChangeListener mOnPageChangeListener = |
| 335 | new ViewPager.SimpleOnPageChangeListener() { |
| 336 | @Override |
| 337 | public void onPageSelected(int position) { |
Amin Shaikh | 4c9048c | 2018-04-20 11:27:46 -0400 | [diff] [blame] | 338 | updateSelected(); |
Amin Shaikh | a07a17b | 2018-02-23 16:02:52 -0500 | [diff] [blame] | 339 | if (mPageIndicator == null) return; |
| 340 | if (mPageListener != null) { |
| 341 | mPageListener.onPageChanged(isLayoutRtl() ? position == mPages.size() - 1 |
| 342 | : position == 0); |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | @Override |
| 347 | public void onPageScrolled(int position, float positionOffset, |
| 348 | int positionOffsetPixels) { |
| 349 | if (mPageIndicator == null) return; |
Rohan Shah | 3090e79 | 2018-04-12 00:01:00 -0400 | [diff] [blame] | 350 | mPageIndicatorPosition = position + positionOffset; |
| 351 | mPageIndicator.setLocation(mPageIndicatorPosition); |
Amin Shaikh | a07a17b | 2018-02-23 16:02:52 -0500 | [diff] [blame] | 352 | if (mPageListener != null) { |
| 353 | mPageListener.onPageChanged(positionOffsetPixels == 0 && |
| 354 | (isLayoutRtl() ? position == mPages.size() - 1 : position == 0)); |
| 355 | } |
| 356 | } |
| 357 | }; |
| 358 | |
Jason Monk | caf3762 | 2015-08-18 12:33:50 -0400 | [diff] [blame] | 359 | public static class TilePage extends TileLayout { |
Jason Monk | 94a1bf6 | 2015-10-20 08:43:36 -0700 | [diff] [blame] | 360 | private int mMaxRows = 3; |
Jason Monk | caf3762 | 2015-08-18 12:33:50 -0400 | [diff] [blame] | 361 | public TilePage(Context context, AttributeSet attrs) { |
| 362 | super(context, attrs); |
Jason Monk | b9c0019 | 2015-10-07 11:45:33 -0400 | [diff] [blame] | 363 | updateResources(); |
Jason Monk | caf3762 | 2015-08-18 12:33:50 -0400 | [diff] [blame] | 364 | } |
| 365 | |
Jason Monk | 9d02a43 | 2016-01-20 16:33:46 -0500 | [diff] [blame] | 366 | @Override |
| 367 | public boolean updateResources() { |
Jason Monk | 6573ef2 | 2016-04-06 12:37:18 -0400 | [diff] [blame] | 368 | final int rows = getRows(); |
| 369 | boolean changed = rows != mMaxRows; |
| 370 | if (changed) { |
| 371 | mMaxRows = rows; |
| 372 | requestLayout(); |
Jason Monk | 9d02a43 | 2016-01-20 16:33:46 -0500 | [diff] [blame] | 373 | } |
Jason Monk | 6573ef2 | 2016-04-06 12:37:18 -0400 | [diff] [blame] | 374 | return super.updateResources() || changed; |
| 375 | } |
| 376 | |
| 377 | private int getRows() { |
Amin Shaikh | 78d834b | 2018-04-23 13:07:31 -0400 | [diff] [blame] | 378 | return Math.max(1, getResources().getInteger(R.integer.quick_settings_num_rows)); |
Jason Monk | 9d02a43 | 2016-01-20 16:33:46 -0500 | [diff] [blame] | 379 | } |
| 380 | |
Jason Monk | bd6dbb0 | 2015-09-03 15:46:25 -0400 | [diff] [blame] | 381 | public void setMaxRows(int maxRows) { |
| 382 | mMaxRows = maxRows; |
| 383 | } |
| 384 | |
Jason Monk | bd6dbb0 | 2015-09-03 15:46:25 -0400 | [diff] [blame] | 385 | public boolean isFull() { |
Jason Monk | caf3762 | 2015-08-18 12:33:50 -0400 | [diff] [blame] | 386 | return mRecords.size() >= mColumns * mMaxRows; |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | private final PagerAdapter mAdapter = new PagerAdapter() { |
Amin Shaikh | 9978c55 | 2018-03-22 07:24:41 -0400 | [diff] [blame] | 391 | @Override |
Jason Monk | caf3762 | 2015-08-18 12:33:50 -0400 | [diff] [blame] | 392 | public void destroyItem(ViewGroup container, int position, Object object) { |
| 393 | if (DEBUG) Log.d(TAG, "Destantiating " + position); |
Jason Monk | caf3762 | 2015-08-18 12:33:50 -0400 | [diff] [blame] | 394 | container.removeView((View) object); |
Amin Shaikh | 9978c55 | 2018-03-22 07:24:41 -0400 | [diff] [blame] | 395 | updateListening(); |
Jason Monk | caf3762 | 2015-08-18 12:33:50 -0400 | [diff] [blame] | 396 | } |
| 397 | |
Amin Shaikh | 9978c55 | 2018-03-22 07:24:41 -0400 | [diff] [blame] | 398 | @Override |
Jason Monk | caf3762 | 2015-08-18 12:33:50 -0400 | [diff] [blame] | 399 | public Object instantiateItem(ViewGroup container, int position) { |
| 400 | if (DEBUG) Log.d(TAG, "Instantiating " + position); |
Jason Monk | 51fb85a | 2016-03-28 14:06:04 -0400 | [diff] [blame] | 401 | if (isLayoutRtl()) { |
| 402 | position = mPages.size() - 1 - position; |
| 403 | } |
Jason Monk | e4e6930 | 2016-01-20 11:27:15 -0500 | [diff] [blame] | 404 | ViewGroup view = mPages.get(position); |
Jason Monk | caf3762 | 2015-08-18 12:33:50 -0400 | [diff] [blame] | 405 | container.addView(view); |
Amin Shaikh | 9978c55 | 2018-03-22 07:24:41 -0400 | [diff] [blame] | 406 | updateListening(); |
Jason Monk | caf3762 | 2015-08-18 12:33:50 -0400 | [diff] [blame] | 407 | return view; |
| 408 | } |
| 409 | |
| 410 | @Override |
| 411 | public int getCount() { |
| 412 | return mNumPages; |
| 413 | } |
| 414 | |
| 415 | @Override |
| 416 | public boolean isViewFromObject(View view, Object object) { |
| 417 | return view == object; |
| 418 | } |
| 419 | }; |
Jason Monk | 162011e | 2016-02-19 08:11:55 -0500 | [diff] [blame] | 420 | |
| 421 | public interface PageListener { |
Jason Monk | 66eaf31 | 2016-02-25 12:29:29 -0500 | [diff] [blame] | 422 | void onPageChanged(boolean isFirst); |
Jason Monk | 162011e | 2016-02-19 08:11:55 -0500 | [diff] [blame] | 423 | } |
Jason Monk | caf3762 | 2015-08-18 12:33:50 -0400 | [diff] [blame] | 424 | } |