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