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; |
Fabian Kozynski | 407ddb2 | 2018-10-03 15:04:56 -0400 | [diff] [blame] | 9 | import android.content.res.Configuration; |
Fabian Kozynski | 6eaf3ac | 2018-10-01 15:59:05 -0400 | [diff] [blame] | 10 | import android.content.res.Resources; |
Fabian Kozynski | 52bd8c2 | 2018-10-08 12:52:51 -0400 | [diff] [blame] | 11 | import android.graphics.Rect; |
Fabian Kozynski | 407ddb2 | 2018-10-03 15:04:56 -0400 | [diff] [blame] | 12 | import android.os.Bundle; |
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; |
| 16 | import android.view.View; |
| 17 | import android.view.ViewGroup; |
Amin Shaikh | a07a17b | 2018-02-23 16:02:52 -0500 | [diff] [blame] | 18 | import android.view.animation.Interpolator; |
| 19 | import android.view.animation.OvershootInterpolator; |
| 20 | import android.widget.Scroller; |
Jason Monk | df36aed | 2016-07-25 11:21:56 -0400 | [diff] [blame] | 21 | |
Fabian Kozynski | 6eaf3ac | 2018-10-01 15:59:05 -0400 | [diff] [blame] | 22 | import androidx.viewpager.widget.PagerAdapter; |
| 23 | import androidx.viewpager.widget.ViewPager; |
| 24 | |
Fabian Kozynski | 2ff6df9 | 2020-04-24 12:00:49 -0400 | [diff] [blame] | 25 | import com.android.internal.logging.UiEventLogger; |
Jason Monk | caf3762 | 2015-08-18 12:33:50 -0400 | [diff] [blame] | 26 | import com.android.systemui.R; |
Fabian Kozynski | 2ff6df9 | 2020-04-24 12:00:49 -0400 | [diff] [blame] | 27 | import com.android.systemui.plugins.qs.QSTile; |
Jason Monk | caf3762 | 2015-08-18 12:33:50 -0400 | [diff] [blame] | 28 | import com.android.systemui.qs.QSPanel.QSTileLayout; |
| 29 | import com.android.systemui.qs.QSPanel.TileRecord; |
| 30 | |
| 31 | import java.util.ArrayList; |
Amin Shaikh | a07a17b | 2018-02-23 16:02:52 -0500 | [diff] [blame] | 32 | import java.util.Set; |
Jason Monk | caf3762 | 2015-08-18 12:33:50 -0400 | [diff] [blame] | 33 | |
| 34 | public class PagedTileLayout extends ViewPager implements QSTileLayout { |
| 35 | |
| 36 | private static final boolean DEBUG = false; |
Fabian Kozynski | 407ddb2 | 2018-10-03 15:04:56 -0400 | [diff] [blame] | 37 | private static final String CURRENT_PAGE = "current_page"; |
Jason Monk | caf3762 | 2015-08-18 12:33:50 -0400 | [diff] [blame] | 38 | |
| 39 | private static final String TAG = "PagedTileLayout"; |
Amin Shaikh | a07a17b | 2018-02-23 16:02:52 -0500 | [diff] [blame] | 40 | private static final int REVEAL_SCROLL_DURATION_MILLIS = 750; |
| 41 | private static final float BOUNCE_ANIMATION_TENSION = 1.3f; |
| 42 | private static final long BOUNCE_ANIMATION_DURATION = 450L; |
| 43 | private static final int TILE_ANIMATION_STAGGER_DELAY = 85; |
| 44 | private static final Interpolator SCROLL_CUBIC = (t) -> { |
| 45 | t -= 1.0f; |
| 46 | return t * t * t + 1.0f; |
| 47 | }; |
| 48 | |
Amin Shaikh | 9978c55 | 2018-03-22 07:24:41 -0400 | [diff] [blame] | 49 | private final ArrayList<TileRecord> mTiles = new ArrayList<>(); |
| 50 | private final ArrayList<TilePage> mPages = new ArrayList<>(); |
Jason Monk | caf3762 | 2015-08-18 12:33:50 -0400 | [diff] [blame] | 51 | |
Jason Monk | caf3762 | 2015-08-18 12:33:50 -0400 | [diff] [blame] | 52 | private PageIndicator mPageIndicator; |
Rohan Shah | 3090e79 | 2018-04-12 00:01:00 -0400 | [diff] [blame] | 53 | private float mPageIndicatorPosition; |
Jason Monk | caf3762 | 2015-08-18 12:33:50 -0400 | [diff] [blame] | 54 | |
Jason Monk | 162011e | 2016-02-19 08:11:55 -0500 | [diff] [blame] | 55 | private PageListener mPageListener; |
Jason Monk | caf3762 | 2015-08-18 12:33:50 -0400 | [diff] [blame] | 56 | |
Jason Monk | e5107a3 | 2016-05-31 15:40:58 -0400 | [diff] [blame] | 57 | private boolean mListening; |
Amin Shaikh | a07a17b | 2018-02-23 16:02:52 -0500 | [diff] [blame] | 58 | private Scroller mScroller; |
| 59 | |
| 60 | private AnimatorSet mBounceAnimatorSet; |
Amin Shaikh | 4c9048c | 2018-04-20 11:27:46 -0400 | [diff] [blame] | 61 | private float mLastExpansion; |
Fabian Kozynski | 712ae39 | 2018-09-12 09:11:16 -0400 | [diff] [blame] | 62 | private boolean mDistributeTiles = false; |
Fabian Kozynski | 407ddb2 | 2018-10-03 15:04:56 -0400 | [diff] [blame] | 63 | private int mPageToRestore = -1; |
| 64 | private int mLayoutOrientation; |
| 65 | private int mLayoutDirection; |
Fabian Kozynski | 52bd8c2 | 2018-10-08 12:52:51 -0400 | [diff] [blame] | 66 | private int mHorizontalClipBound; |
Fabian Kozynski | 87bdf0d | 2019-04-01 09:52:57 -0400 | [diff] [blame] | 67 | private final Rect mClippingRect; |
Fabian Kozynski | 2ff6df9 | 2020-04-24 12:00:49 -0400 | [diff] [blame] | 68 | private final UiEventLogger mUiEventLogger = QSEvents.INSTANCE.getQsUiEventsLogger(); |
Jason Monk | e5107a3 | 2016-05-31 15:40:58 -0400 | [diff] [blame] | 69 | |
Jason Monk | caf3762 | 2015-08-18 12:33:50 -0400 | [diff] [blame] | 70 | public PagedTileLayout(Context context, AttributeSet attrs) { |
| 71 | super(context, attrs); |
Amin Shaikh | a07a17b | 2018-02-23 16:02:52 -0500 | [diff] [blame] | 72 | mScroller = new Scroller(context, SCROLL_CUBIC); |
Jason Monk | caf3762 | 2015-08-18 12:33:50 -0400 | [diff] [blame] | 73 | setAdapter(mAdapter); |
Amin Shaikh | a07a17b | 2018-02-23 16:02:52 -0500 | [diff] [blame] | 74 | setOnPageChangeListener(mOnPageChangeListener); |
| 75 | setCurrentItem(0, false); |
Fabian Kozynski | 407ddb2 | 2018-10-03 15:04:56 -0400 | [diff] [blame] | 76 | mLayoutOrientation = getResources().getConfiguration().orientation; |
| 77 | mLayoutDirection = getLayoutDirection(); |
Fabian Kozynski | 87bdf0d | 2019-04-01 09:52:57 -0400 | [diff] [blame] | 78 | mClippingRect = new Rect(); |
Fabian Kozynski | 407ddb2 | 2018-10-03 15:04:56 -0400 | [diff] [blame] | 79 | } |
Fabian Kozynski | 2ff6df9 | 2020-04-24 12:00:49 -0400 | [diff] [blame] | 80 | private int mLastMaxHeight = -1; |
Fabian Kozynski | 407ddb2 | 2018-10-03 15:04:56 -0400 | [diff] [blame] | 81 | |
| 82 | public void saveInstanceState(Bundle outState) { |
| 83 | outState.putInt(CURRENT_PAGE, getCurrentItem()); |
| 84 | } |
| 85 | |
| 86 | public void restoreInstanceState(Bundle savedInstanceState) { |
| 87 | // There's only 1 page at this point. We want to restore the correct page once the |
| 88 | // pages have been inflated |
| 89 | mPageToRestore = savedInstanceState.getInt(CURRENT_PAGE, -1); |
| 90 | } |
| 91 | |
| 92 | @Override |
| 93 | protected void onConfigurationChanged(Configuration newConfig) { |
| 94 | super.onConfigurationChanged(newConfig); |
| 95 | if (mLayoutOrientation != newConfig.orientation) { |
| 96 | mLayoutOrientation = newConfig.orientation; |
| 97 | setCurrentItem(0, false); |
Fabian Kozynski | d9e8aa3 | 2019-04-12 10:09:48 -0400 | [diff] [blame] | 98 | mPageToRestore = 0; |
Fabian Kozynski | 407ddb2 | 2018-10-03 15:04:56 -0400 | [diff] [blame] | 99 | } |
Jason Monk | caf3762 | 2015-08-18 12:33:50 -0400 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | @Override |
Jason Monk | 51fb85a | 2016-03-28 14:06:04 -0400 | [diff] [blame] | 103 | public void onRtlPropertiesChanged(int layoutDirection) { |
| 104 | super.onRtlPropertiesChanged(layoutDirection); |
Fabian Kozynski | 407ddb2 | 2018-10-03 15:04:56 -0400 | [diff] [blame] | 105 | if (mLayoutDirection != layoutDirection) { |
| 106 | mLayoutDirection = layoutDirection; |
| 107 | setAdapter(mAdapter); |
| 108 | setCurrentItem(0, false); |
Fabian Kozynski | d9e8aa3 | 2019-04-12 10:09:48 -0400 | [diff] [blame] | 109 | mPageToRestore = 0; |
Fabian Kozynski | 407ddb2 | 2018-10-03 15:04:56 -0400 | [diff] [blame] | 110 | } |
Jason Monk | 51fb85a | 2016-03-28 14:06:04 -0400 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | @Override |
| 114 | public void setCurrentItem(int item, boolean smoothScroll) { |
| 115 | if (isLayoutRtl()) { |
| 116 | item = mPages.size() - 1 - item; |
| 117 | } |
| 118 | super.setCurrentItem(item, smoothScroll); |
| 119 | } |
| 120 | |
Fabian Kozynski | d9e8aa3 | 2019-04-12 10:09:48 -0400 | [diff] [blame] | 121 | /** |
| 122 | * Obtains the current page number respecting RTL |
| 123 | */ |
| 124 | private int getCurrentPageNumber() { |
| 125 | int page = getCurrentItem(); |
| 126 | if (mLayoutDirection == LAYOUT_DIRECTION_RTL) { |
| 127 | page = mPages.size() - 1 - page; |
| 128 | } |
| 129 | return page; |
| 130 | } |
| 131 | |
Fabian Kozynski | 2ff6df9 | 2020-04-24 12:00:49 -0400 | [diff] [blame] | 132 | // This will dump to the ui log all the tiles that are visible in this page |
| 133 | private void logVisibleTiles(TilePage page) { |
| 134 | for (int i = 0; i < page.mRecords.size(); i++) { |
| 135 | QSTile t = page.mRecords.get(i).tile; |
| 136 | mUiEventLogger.logWithInstanceId(QSEvent.QS_TILE_VISIBLE, 0, t.getMetricsSpec(), |
| 137 | t.getInstanceId()); |
| 138 | } |
| 139 | } |
| 140 | |
Jason Monk | 51fb85a | 2016-03-28 14:06:04 -0400 | [diff] [blame] | 141 | @Override |
Jason Monk | e5107a3 | 2016-05-31 15:40:58 -0400 | [diff] [blame] | 142 | public void setListening(boolean listening) { |
| 143 | if (mListening == listening) return; |
| 144 | mListening = listening; |
Amin Shaikh | 9978c55 | 2018-03-22 07:24:41 -0400 | [diff] [blame] | 145 | updateListening(); |
| 146 | } |
| 147 | |
| 148 | private void updateListening() { |
| 149 | for (TilePage tilePage : mPages) { |
| 150 | tilePage.setListening(tilePage.getParent() == null ? false : mListening); |
Jason Monk | e5107a3 | 2016-05-31 15:40:58 -0400 | [diff] [blame] | 151 | } |
| 152 | } |
| 153 | |
Amin Shaikh | a07a17b | 2018-02-23 16:02:52 -0500 | [diff] [blame] | 154 | @Override |
Fabian Kozynski | 545f250 | 2020-04-13 11:37:10 -0400 | [diff] [blame] | 155 | public void fakeDragBy(float xOffset) { |
| 156 | try { |
| 157 | super.fakeDragBy(xOffset); |
| 158 | // Keep on drawing until the animation has finished. |
| 159 | postInvalidateOnAnimation(); |
| 160 | } catch (NullPointerException e) { |
| 161 | Log.e(TAG, "FakeDragBy called before begin", e); |
| 162 | // If we were trying to fake drag, it means we just added a new tile to the last |
| 163 | // page, so animate there. |
| 164 | final int lastPageNumber = mPages.size() - 1; |
| 165 | post(() -> { |
| 166 | setCurrentItem(lastPageNumber, true); |
| 167 | if (mBounceAnimatorSet != null) { |
| 168 | mBounceAnimatorSet.start(); |
| 169 | } |
| 170 | setOffscreenPageLimit(1); |
| 171 | }); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | @Override |
Amin Shaikh | a07a17b | 2018-02-23 16:02:52 -0500 | [diff] [blame] | 176 | public void computeScroll() { |
| 177 | if (!mScroller.isFinished() && mScroller.computeScrollOffset()) { |
Fabian Kozynski | 9073ef0 | 2020-03-16 11:50:12 -0400 | [diff] [blame] | 178 | if (!isFakeDragging()) { |
| 179 | beginFakeDrag(); |
| 180 | } |
Amin Shaikh | 5484d0f | 2018-07-31 10:44:11 -0400 | [diff] [blame] | 181 | fakeDragBy(getScrollX() - mScroller.getCurrX()); |
Amin Shaikh | 5484d0f | 2018-07-31 10:44:11 -0400 | [diff] [blame] | 182 | } else if (isFakeDragging()) { |
| 183 | endFakeDrag(); |
Amin Shaikh | a07a17b | 2018-02-23 16:02:52 -0500 | [diff] [blame] | 184 | mBounceAnimatorSet.start(); |
| 185 | setOffscreenPageLimit(1); |
Amin Shaikh | a07a17b | 2018-02-23 16:02:52 -0500 | [diff] [blame] | 186 | } |
| 187 | super.computeScroll(); |
| 188 | } |
| 189 | |
Jason Monk | e5107a3 | 2016-05-31 15:40:58 -0400 | [diff] [blame] | 190 | @Override |
Jason Monk | c5bdafb | 2016-02-25 16:24:21 -0500 | [diff] [blame] | 191 | public boolean hasOverlappingRendering() { |
| 192 | return false; |
| 193 | } |
| 194 | |
| 195 | @Override |
Jason Monk | caf3762 | 2015-08-18 12:33:50 -0400 | [diff] [blame] | 196 | protected void onFinishInflate() { |
| 197 | super.onFinishInflate(); |
Jason Monk | 3250885 | 2017-01-18 09:17:13 -0500 | [diff] [blame] | 198 | mPages.add((TilePage) LayoutInflater.from(getContext()) |
Jason Monk | e4e6930 | 2016-01-20 11:27:15 -0500 | [diff] [blame] | 199 | .inflate(R.layout.qs_paged_page, this, false)); |
Fabian Kozynski | fae42a8 | 2018-10-04 11:18:42 -0400 | [diff] [blame] | 200 | mAdapter.notifyDataSetChanged(); |
Jason Monk | caf3762 | 2015-08-18 12:33:50 -0400 | [diff] [blame] | 201 | } |
| 202 | |
Jason Monk | 3250885 | 2017-01-18 09:17:13 -0500 | [diff] [blame] | 203 | public void setPageIndicator(PageIndicator indicator) { |
| 204 | mPageIndicator = indicator; |
Fabian Kozynski | 712ae39 | 2018-09-12 09:11:16 -0400 | [diff] [blame] | 205 | mPageIndicator.setNumPages(mPages.size()); |
Rohan Shah | 3090e79 | 2018-04-12 00:01:00 -0400 | [diff] [blame] | 206 | mPageIndicator.setLocation(mPageIndicatorPosition); |
Jason Monk | 3250885 | 2017-01-18 09:17:13 -0500 | [diff] [blame] | 207 | } |
| 208 | |
Jason Monk | caf3762 | 2015-08-18 12:33:50 -0400 | [diff] [blame] | 209 | @Override |
| 210 | public int getOffsetTop(TileRecord tile) { |
Jason Monk | ae5bd03 | 2016-03-02 14:38:31 -0500 | [diff] [blame] | 211 | final ViewGroup parent = (ViewGroup) tile.tileView.getParent(); |
| 212 | if (parent == null) return 0; |
| 213 | return parent.getTop() + getTop(); |
Jason Monk | caf3762 | 2015-08-18 12:33:50 -0400 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | @Override |
Jason Monk | caf3762 | 2015-08-18 12:33:50 -0400 | [diff] [blame] | 217 | public void addTile(TileRecord tile) { |
| 218 | mTiles.add(tile); |
Fabian Kozynski | 712ae39 | 2018-09-12 09:11:16 -0400 | [diff] [blame] | 219 | mDistributeTiles = true; |
| 220 | requestLayout(); |
Jason Monk | caf3762 | 2015-08-18 12:33:50 -0400 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | @Override |
| 224 | public void removeTile(TileRecord tile) { |
| 225 | if (mTiles.remove(tile)) { |
Fabian Kozynski | 712ae39 | 2018-09-12 09:11:16 -0400 | [diff] [blame] | 226 | mDistributeTiles = true; |
| 227 | requestLayout(); |
Jason Monk | caf3762 | 2015-08-18 12:33:50 -0400 | [diff] [blame] | 228 | } |
| 229 | } |
| 230 | |
Amin Shaikh | 0f8ea543 | 2018-03-27 11:09:27 -0400 | [diff] [blame] | 231 | @Override |
| 232 | public void setExpansion(float expansion) { |
Amin Shaikh | 4c9048c | 2018-04-20 11:27:46 -0400 | [diff] [blame] | 233 | mLastExpansion = expansion; |
| 234 | updateSelected(); |
| 235 | } |
| 236 | |
| 237 | private void updateSelected() { |
| 238 | // Start the marquee when fully expanded and stop when fully collapsed. Leave as is for |
| 239 | // other expansion ratios since there is no way way to pause the marquee. |
| 240 | if (mLastExpansion > 0f && mLastExpansion < 1f) { |
| 241 | return; |
| 242 | } |
| 243 | boolean selected = mLastExpansion == 1f; |
Rohan Shah | 2dbcb57 | 2018-05-25 10:51:22 -0700 | [diff] [blame] | 244 | |
| 245 | // Disable accessibility temporarily while we update selected state purely for the |
| 246 | // marquee. This will ensure that accessibility doesn't announce the TYPE_VIEW_SELECTED |
| 247 | // event on any of the children. |
| 248 | setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS); |
Fabian Kozynski | d9e8aa3 | 2019-04-12 10:09:48 -0400 | [diff] [blame] | 249 | int currentItem = getCurrentPageNumber(); |
Amin Shaikh | 4c9048c | 2018-04-20 11:27:46 -0400 | [diff] [blame] | 250 | for (int i = 0; i < mPages.size(); i++) { |
Fabian Kozynski | 2ff6df9 | 2020-04-24 12:00:49 -0400 | [diff] [blame] | 251 | TilePage page = mPages.get(i); |
| 252 | page.setSelected(i == currentItem ? selected : false); |
| 253 | if (page.isSelected()) { |
| 254 | logVisibleTiles(page); |
| 255 | } |
Amin Shaikh | 0f8ea543 | 2018-03-27 11:09:27 -0400 | [diff] [blame] | 256 | } |
Rohan Shah | 2dbcb57 | 2018-05-25 10:51:22 -0700 | [diff] [blame] | 257 | setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_AUTO); |
Amin Shaikh | 0f8ea543 | 2018-03-27 11:09:27 -0400 | [diff] [blame] | 258 | } |
| 259 | |
Jason Monk | 162011e | 2016-02-19 08:11:55 -0500 | [diff] [blame] | 260 | public void setPageListener(PageListener listener) { |
| 261 | mPageListener = listener; |
| 262 | } |
| 263 | |
Jason Monk | caf3762 | 2015-08-18 12:33:50 -0400 | [diff] [blame] | 264 | private void distributeTiles() { |
Fabian Kozynski | 712ae39 | 2018-09-12 09:11:16 -0400 | [diff] [blame] | 265 | emptyAndInflateOrRemovePages(); |
| 266 | |
| 267 | final int tileCount = mPages.get(0).maxTiles(); |
Jason Monk | caf3762 | 2015-08-18 12:33:50 -0400 | [diff] [blame] | 268 | if (DEBUG) Log.d(TAG, "Distributing tiles"); |
Jason Monk | caf3762 | 2015-08-18 12:33:50 -0400 | [diff] [blame] | 269 | int index = 0; |
| 270 | final int NT = mTiles.size(); |
| 271 | for (int i = 0; i < NT; i++) { |
| 272 | TileRecord tile = mTiles.get(i); |
Fabian Kozynski | 712ae39 | 2018-09-12 09:11:16 -0400 | [diff] [blame] | 273 | if (mPages.get(index).mRecords.size() == tileCount) index++; |
| 274 | if (DEBUG) { |
| 275 | Log.d(TAG, "Adding " + tile.tile.getClass().getSimpleName() + " to " |
| 276 | + index); |
Jason Monk | caf3762 | 2015-08-18 12:33:50 -0400 | [diff] [blame] | 277 | } |
Jason Monk | caf3762 | 2015-08-18 12:33:50 -0400 | [diff] [blame] | 278 | mPages.get(index).addTile(tile); |
| 279 | } |
Fabian Kozynski | 712ae39 | 2018-09-12 09:11:16 -0400 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | private void emptyAndInflateOrRemovePages() { |
| 283 | final int nTiles = mTiles.size(); |
Fabian Kozynski | 6ff5c18 | 2019-06-20 14:57:47 -0400 | [diff] [blame] | 284 | // We should always have at least one page, even if it's empty. |
| 285 | int numPages = Math.max(nTiles / mPages.get(0).maxTiles(), 1); |
| 286 | |
Fabian Kozynski | 712ae39 | 2018-09-12 09:11:16 -0400 | [diff] [blame] | 287 | // Add one more not full page if needed |
Fabian Kozynski | 492be7c | 2019-07-31 14:41:18 -0400 | [diff] [blame] | 288 | if (nTiles > numPages * mPages.get(0).maxTiles()) { |
| 289 | numPages++; |
| 290 | } |
Fabian Kozynski | 712ae39 | 2018-09-12 09:11:16 -0400 | [diff] [blame] | 291 | |
| 292 | final int NP = mPages.size(); |
| 293 | for (int i = 0; i < NP; i++) { |
| 294 | mPages.get(i).removeAllViews(); |
Jason Monk | caf3762 | 2015-08-18 12:33:50 -0400 | [diff] [blame] | 295 | } |
Fabian Kozynski | 712ae39 | 2018-09-12 09:11:16 -0400 | [diff] [blame] | 296 | if (NP == numPages) { |
| 297 | return; |
| 298 | } |
| 299 | while (mPages.size() < numPages) { |
| 300 | if (DEBUG) Log.d(TAG, "Adding page"); |
| 301 | mPages.add((TilePage) LayoutInflater.from(getContext()) |
| 302 | .inflate(R.layout.qs_paged_page, this, false)); |
| 303 | } |
| 304 | while (mPages.size() > numPages) { |
| 305 | if (DEBUG) Log.d(TAG, "Removing page"); |
| 306 | mPages.remove(mPages.size() - 1); |
| 307 | } |
| 308 | mPageIndicator.setNumPages(mPages.size()); |
| 309 | setAdapter(mAdapter); |
| 310 | mAdapter.notifyDataSetChanged(); |
Fabian Kozynski | 407ddb2 | 2018-10-03 15:04:56 -0400 | [diff] [blame] | 311 | if (mPageToRestore != -1) { |
| 312 | setCurrentItem(mPageToRestore, false); |
| 313 | mPageToRestore = -1; |
| 314 | } |
Jason Monk | caf3762 | 2015-08-18 12:33:50 -0400 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | @Override |
Jason Monk | 9d02a43 | 2016-01-20 16:33:46 -0500 | [diff] [blame] | 318 | public boolean updateResources() { |
Rohan Shah | 3090e79 | 2018-04-12 00:01:00 -0400 | [diff] [blame] | 319 | // Update bottom padding, useful for removing extra space once the panel page indicator is |
| 320 | // hidden. |
Fabian Kozynski | 6eaf3ac | 2018-10-01 15:59:05 -0400 | [diff] [blame] | 321 | Resources res = getContext().getResources(); |
Fabian Kozynski | 52bd8c2 | 2018-10-08 12:52:51 -0400 | [diff] [blame] | 322 | mHorizontalClipBound = res.getDimensionPixelSize(R.dimen.notification_side_paddings); |
| 323 | setPadding(0, 0, 0, |
Rohan Shah | 3090e79 | 2018-04-12 00:01:00 -0400 | [diff] [blame] | 324 | getContext().getResources().getDimensionPixelSize( |
| 325 | R.dimen.qs_paged_tile_layout_padding_bottom)); |
Jason Monk | 9d02a43 | 2016-01-20 16:33:46 -0500 | [diff] [blame] | 326 | boolean changed = false; |
Jason Monk | caf3762 | 2015-08-18 12:33:50 -0400 | [diff] [blame] | 327 | for (int i = 0; i < mPages.size(); i++) { |
Jason Monk | 9d02a43 | 2016-01-20 16:33:46 -0500 | [diff] [blame] | 328 | changed |= mPages.get(i).updateResources(); |
Jason Monk | caf3762 | 2015-08-18 12:33:50 -0400 | [diff] [blame] | 329 | } |
Jason Monk | 9d02a43 | 2016-01-20 16:33:46 -0500 | [diff] [blame] | 330 | if (changed) { |
Fabian Kozynski | 712ae39 | 2018-09-12 09:11:16 -0400 | [diff] [blame] | 331 | mDistributeTiles = true; |
| 332 | requestLayout(); |
Jason Monk | 9d02a43 | 2016-01-20 16:33:46 -0500 | [diff] [blame] | 333 | } |
| 334 | return changed; |
Jason Monk | caf3762 | 2015-08-18 12:33:50 -0400 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | @Override |
Fabian Kozynski | 52bd8c2 | 2018-10-08 12:52:51 -0400 | [diff] [blame] | 338 | protected void onLayout(boolean changed, int l, int t, int r, int b) { |
| 339 | super.onLayout(changed, l, t, r, b); |
Fabian Kozynski | 87bdf0d | 2019-04-01 09:52:57 -0400 | [diff] [blame] | 340 | mClippingRect.set(mHorizontalClipBound, 0, (r - l) - mHorizontalClipBound, b - t); |
| 341 | setClipBounds(mClippingRect); |
Fabian Kozynski | 52bd8c2 | 2018-10-08 12:52:51 -0400 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | @Override |
Jason Monk | caf3762 | 2015-08-18 12:33:50 -0400 | [diff] [blame] | 345 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
Fabian Kozynski | 712ae39 | 2018-09-12 09:11:16 -0400 | [diff] [blame] | 346 | |
| 347 | final int nTiles = mTiles.size(); |
Fabian Kozynski | 72cd84a9 | 2019-04-15 14:09:43 -0400 | [diff] [blame] | 348 | // If we have no reason to recalculate the number of rows, skip this step. In particular, |
| 349 | // if the height passed by its parent is the same as the last time, we try not to remeasure. |
| 350 | if (mDistributeTiles || mLastMaxHeight != MeasureSpec.getSize(heightMeasureSpec)) { |
Fabian Kozynski | 712ae39 | 2018-09-12 09:11:16 -0400 | [diff] [blame] | 351 | |
Fabian Kozynski | 72cd84a9 | 2019-04-15 14:09:43 -0400 | [diff] [blame] | 352 | mLastMaxHeight = MeasureSpec.getSize(heightMeasureSpec); |
Fabian Kozynski | 712ae39 | 2018-09-12 09:11:16 -0400 | [diff] [blame] | 353 | // Only change the pages if the number of rows or columns (from updateResources) has |
| 354 | // changed or the tiles have changed |
| 355 | if (mPages.get(0).updateMaxRows(heightMeasureSpec, nTiles) || mDistributeTiles) { |
| 356 | mDistributeTiles = false; |
| 357 | distributeTiles(); |
| 358 | } |
| 359 | |
| 360 | final int nRows = mPages.get(0).mRows; |
| 361 | for (int i = 0; i < mPages.size(); i++) { |
| 362 | TilePage t = mPages.get(i); |
| 363 | t.mRows = nRows; |
| 364 | } |
| 365 | } |
| 366 | |
Jason Monk | caf3762 | 2015-08-18 12:33:50 -0400 | [diff] [blame] | 367 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
Fabian Kozynski | 712ae39 | 2018-09-12 09:11:16 -0400 | [diff] [blame] | 368 | |
Jason Monk | caf3762 | 2015-08-18 12:33:50 -0400 | [diff] [blame] | 369 | // The ViewPager likes to eat all of the space, instead force it to wrap to the max height |
| 370 | // of the pages. |
| 371 | int maxHeight = 0; |
| 372 | final int N = getChildCount(); |
| 373 | for (int i = 0; i < N; i++) { |
| 374 | int height = getChildAt(i).getMeasuredHeight(); |
| 375 | if (height > maxHeight) { |
| 376 | maxHeight = height; |
| 377 | } |
| 378 | } |
Jason Monk | f13413e | 2017-02-15 15:49:32 -0500 | [diff] [blame] | 379 | setMeasuredDimension(getMeasuredWidth(), maxHeight + getPaddingBottom()); |
Jason Monk | caf3762 | 2015-08-18 12:33:50 -0400 | [diff] [blame] | 380 | } |
| 381 | |
Jason Monk | 8fb7787 | 2016-03-03 16:39:42 -0500 | [diff] [blame] | 382 | public int getColumnCount() { |
| 383 | if (mPages.size() == 0) return 0; |
| 384 | return mPages.get(0).mColumns; |
| 385 | } |
| 386 | |
Fabian Kozynski | 802279f | 2018-09-07 13:44:54 -0400 | [diff] [blame] | 387 | public int getNumVisibleTiles() { |
| 388 | if (mPages.size() == 0) return 0; |
Fabian Kozynski | d9e8aa3 | 2019-04-12 10:09:48 -0400 | [diff] [blame] | 389 | TilePage currentPage = mPages.get(getCurrentPageNumber()); |
Fabian Kozynski | 802279f | 2018-09-07 13:44:54 -0400 | [diff] [blame] | 390 | return currentPage.mRecords.size(); |
| 391 | } |
| 392 | |
Amin Shaikh | a07a17b | 2018-02-23 16:02:52 -0500 | [diff] [blame] | 393 | public void startTileReveal(Set<String> tileSpecs, final Runnable postAnimation) { |
Amin Shaikh | 5484d0f | 2018-07-31 10:44:11 -0400 | [diff] [blame] | 394 | if (tileSpecs.isEmpty() || mPages.size() < 2 || getScrollX() != 0 || !beginFakeDrag()) { |
Amin Shaikh | a07a17b | 2018-02-23 16:02:52 -0500 | [diff] [blame] | 395 | // Do not start the reveal animation unless there are tiles to animate, multiple |
| 396 | // TilePages available and the user has not already started dragging. |
| 397 | return; |
| 398 | } |
| 399 | |
| 400 | final int lastPageNumber = mPages.size() - 1; |
| 401 | final TilePage lastPage = mPages.get(lastPageNumber); |
| 402 | final ArrayList<Animator> bounceAnims = new ArrayList<>(); |
| 403 | for (TileRecord tr : lastPage.mRecords) { |
| 404 | if (tileSpecs.contains(tr.tile.getTileSpec())) { |
| 405 | bounceAnims.add(setupBounceAnimator(tr.tileView, bounceAnims.size())); |
| 406 | } |
| 407 | } |
| 408 | |
| 409 | if (bounceAnims.isEmpty()) { |
| 410 | // All tileSpecs are on the first page. Nothing to do. |
| 411 | // TODO: potentially show a bounce animation for first page QS tiles |
Amin Shaikh | 5484d0f | 2018-07-31 10:44:11 -0400 | [diff] [blame] | 412 | endFakeDrag(); |
Amin Shaikh | a07a17b | 2018-02-23 16:02:52 -0500 | [diff] [blame] | 413 | return; |
| 414 | } |
| 415 | |
| 416 | mBounceAnimatorSet = new AnimatorSet(); |
| 417 | mBounceAnimatorSet.playTogether(bounceAnims); |
| 418 | mBounceAnimatorSet.addListener(new AnimatorListenerAdapter() { |
| 419 | @Override |
| 420 | public void onAnimationEnd(Animator animation) { |
| 421 | mBounceAnimatorSet = null; |
| 422 | postAnimation.run(); |
| 423 | } |
| 424 | }); |
Amin Shaikh | 5484d0f | 2018-07-31 10:44:11 -0400 | [diff] [blame] | 425 | setOffscreenPageLimit(lastPageNumber); // Ensure the page to reveal has been inflated. |
| 426 | int dx = getWidth() * lastPageNumber; |
| 427 | mScroller.startScroll(getScrollX(), getScrollY(), isLayoutRtl() ? -dx : dx, 0, |
| 428 | REVEAL_SCROLL_DURATION_MILLIS); |
Amin Shaikh | a07a17b | 2018-02-23 16:02:52 -0500 | [diff] [blame] | 429 | postInvalidateOnAnimation(); |
| 430 | } |
| 431 | |
| 432 | private static Animator setupBounceAnimator(View view, int ordinal) { |
| 433 | view.setAlpha(0f); |
| 434 | view.setScaleX(0f); |
| 435 | view.setScaleY(0f); |
| 436 | ObjectAnimator animator = ObjectAnimator.ofPropertyValuesHolder(view, |
| 437 | PropertyValuesHolder.ofFloat(View.ALPHA, 1), |
| 438 | PropertyValuesHolder.ofFloat(View.SCALE_X, 1), |
| 439 | PropertyValuesHolder.ofFloat(View.SCALE_Y, 1)); |
| 440 | animator.setDuration(BOUNCE_ANIMATION_DURATION); |
| 441 | animator.setStartDelay(ordinal * TILE_ANIMATION_STAGGER_DELAY); |
| 442 | animator.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION)); |
| 443 | return animator; |
| 444 | } |
| 445 | |
| 446 | private final ViewPager.OnPageChangeListener mOnPageChangeListener = |
| 447 | new ViewPager.SimpleOnPageChangeListener() { |
| 448 | @Override |
| 449 | public void onPageSelected(int position) { |
Amin Shaikh | 4c9048c | 2018-04-20 11:27:46 -0400 | [diff] [blame] | 450 | updateSelected(); |
Amin Shaikh | a07a17b | 2018-02-23 16:02:52 -0500 | [diff] [blame] | 451 | if (mPageIndicator == null) return; |
| 452 | if (mPageListener != null) { |
| 453 | mPageListener.onPageChanged(isLayoutRtl() ? position == mPages.size() - 1 |
| 454 | : position == 0); |
| 455 | } |
Fabian Kozynski | 2ff6df9 | 2020-04-24 12:00:49 -0400 | [diff] [blame] | 456 | |
Amin Shaikh | a07a17b | 2018-02-23 16:02:52 -0500 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | @Override |
| 460 | public void onPageScrolled(int position, float positionOffset, |
| 461 | int positionOffsetPixels) { |
| 462 | if (mPageIndicator == null) return; |
Rohan Shah | 3090e79 | 2018-04-12 00:01:00 -0400 | [diff] [blame] | 463 | mPageIndicatorPosition = position + positionOffset; |
| 464 | mPageIndicator.setLocation(mPageIndicatorPosition); |
Amin Shaikh | a07a17b | 2018-02-23 16:02:52 -0500 | [diff] [blame] | 465 | if (mPageListener != null) { |
| 466 | mPageListener.onPageChanged(positionOffsetPixels == 0 && |
| 467 | (isLayoutRtl() ? position == mPages.size() - 1 : position == 0)); |
| 468 | } |
| 469 | } |
| 470 | }; |
| 471 | |
Jason Monk | caf3762 | 2015-08-18 12:33:50 -0400 | [diff] [blame] | 472 | public static class TilePage extends TileLayout { |
Fabian Kozynski | 712ae39 | 2018-09-12 09:11:16 -0400 | [diff] [blame] | 473 | |
Jason Monk | caf3762 | 2015-08-18 12:33:50 -0400 | [diff] [blame] | 474 | public TilePage(Context context, AttributeSet attrs) { |
| 475 | super(context, attrs); |
Jason Monk | bd6dbb0 | 2015-09-03 15:46:25 -0400 | [diff] [blame] | 476 | } |
| 477 | |
Jason Monk | bd6dbb0 | 2015-09-03 15:46:25 -0400 | [diff] [blame] | 478 | public boolean isFull() { |
Fabian Kozynski | 6ff5c18 | 2019-06-20 14:57:47 -0400 | [diff] [blame] | 479 | return mRecords.size() >= maxTiles(); |
Fabian Kozynski | 712ae39 | 2018-09-12 09:11:16 -0400 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | public int maxTiles() { |
Fabian Kozynski | 6ff5c18 | 2019-06-20 14:57:47 -0400 | [diff] [blame] | 483 | // Each page should be able to hold at least one tile. If there's not enough room to |
| 484 | // show even 1 or there are no tiles, it probably means we are in the middle of setting |
| 485 | // up. |
| 486 | return Math.max(mColumns * mRows, 1); |
Jason Monk | caf3762 | 2015-08-18 12:33:50 -0400 | [diff] [blame] | 487 | } |
Fabian Kozynski | 52bd8c2 | 2018-10-08 12:52:51 -0400 | [diff] [blame] | 488 | |
| 489 | @Override |
| 490 | public boolean updateResources() { |
| 491 | final int sidePadding = getContext().getResources().getDimensionPixelSize( |
| 492 | R.dimen.notification_side_paddings); |
| 493 | setPadding(sidePadding, 0, sidePadding, 0); |
| 494 | return super.updateResources(); |
| 495 | } |
Jason Monk | caf3762 | 2015-08-18 12:33:50 -0400 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | private final PagerAdapter mAdapter = new PagerAdapter() { |
Amin Shaikh | 9978c55 | 2018-03-22 07:24:41 -0400 | [diff] [blame] | 499 | @Override |
Jason Monk | caf3762 | 2015-08-18 12:33:50 -0400 | [diff] [blame] | 500 | public void destroyItem(ViewGroup container, int position, Object object) { |
| 501 | if (DEBUG) Log.d(TAG, "Destantiating " + position); |
Jason Monk | caf3762 | 2015-08-18 12:33:50 -0400 | [diff] [blame] | 502 | container.removeView((View) object); |
Amin Shaikh | 9978c55 | 2018-03-22 07:24:41 -0400 | [diff] [blame] | 503 | updateListening(); |
Jason Monk | caf3762 | 2015-08-18 12:33:50 -0400 | [diff] [blame] | 504 | } |
| 505 | |
Amin Shaikh | 9978c55 | 2018-03-22 07:24:41 -0400 | [diff] [blame] | 506 | @Override |
Jason Monk | caf3762 | 2015-08-18 12:33:50 -0400 | [diff] [blame] | 507 | public Object instantiateItem(ViewGroup container, int position) { |
| 508 | if (DEBUG) Log.d(TAG, "Instantiating " + position); |
Jason Monk | 51fb85a | 2016-03-28 14:06:04 -0400 | [diff] [blame] | 509 | if (isLayoutRtl()) { |
| 510 | position = mPages.size() - 1 - position; |
| 511 | } |
Jason Monk | e4e6930 | 2016-01-20 11:27:15 -0500 | [diff] [blame] | 512 | ViewGroup view = mPages.get(position); |
Amin Shaikh | 53bc083 | 2018-09-17 15:06:18 -0400 | [diff] [blame] | 513 | if (view.getParent() != null) { |
| 514 | container.removeView(view); |
| 515 | } |
Jason Monk | caf3762 | 2015-08-18 12:33:50 -0400 | [diff] [blame] | 516 | container.addView(view); |
Amin Shaikh | 9978c55 | 2018-03-22 07:24:41 -0400 | [diff] [blame] | 517 | updateListening(); |
Jason Monk | caf3762 | 2015-08-18 12:33:50 -0400 | [diff] [blame] | 518 | return view; |
| 519 | } |
| 520 | |
| 521 | @Override |
| 522 | public int getCount() { |
Fabian Kozynski | 712ae39 | 2018-09-12 09:11:16 -0400 | [diff] [blame] | 523 | return mPages.size(); |
Jason Monk | caf3762 | 2015-08-18 12:33:50 -0400 | [diff] [blame] | 524 | } |
| 525 | |
| 526 | @Override |
| 527 | public boolean isViewFromObject(View view, Object object) { |
| 528 | return view == object; |
| 529 | } |
| 530 | }; |
Jason Monk | 162011e | 2016-02-19 08:11:55 -0500 | [diff] [blame] | 531 | |
| 532 | public interface PageListener { |
Jason Monk | 66eaf31 | 2016-02-25 12:29:29 -0500 | [diff] [blame] | 533 | void onPageChanged(boolean isFirst); |
Jason Monk | 162011e | 2016-02-19 08:11:55 -0500 | [diff] [blame] | 534 | } |
Jason Monk | caf3762 | 2015-08-18 12:33:50 -0400 | [diff] [blame] | 535 | } |