blob: fd0045e6305637377ae089793736cebf0a16876f [file] [log] [blame]
Winson Chungb1777442015-06-16 13:35:04 -07001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package com.android.launcher3;
17
Winson Chungb1777442015-06-16 13:35:04 -070018import android.animation.AnimatorSet;
19import android.animation.ArgbEvaluator;
20import android.animation.ObjectAnimator;
21import android.animation.ValueAnimator;
22import android.content.res.Resources;
23import android.graphics.Canvas;
24import android.graphics.Color;
25import android.graphics.Paint;
Winson67795952015-08-20 12:23:52 -070026import android.graphics.Path;
Winson Chungb1777442015-06-16 13:35:04 -070027import android.graphics.Point;
28import android.graphics.Rect;
29import android.view.MotionEvent;
30import android.view.ViewConfiguration;
31
Sunny Goyalb713ad42015-06-24 11:45:32 -070032import com.android.launcher3.util.Thunk;
33
Winson Chungb1777442015-06-16 13:35:04 -070034/**
35 * The track and scrollbar that shows when you scroll the list.
36 */
37public class BaseRecyclerViewFastScrollBar {
38
39 public interface FastScrollFocusableView {
Winsonc0880492015-08-21 11:16:27 -070040 void setFastScrollFocusState(final FastBitmapDrawable.State focusState, boolean animated);
Winson Chungb1777442015-06-16 13:35:04 -070041 }
42
43 private final static int MAX_TRACK_ALPHA = 30;
44 private final static int SCROLL_BAR_VIS_DURATION = 150;
45
Sunny Goyalb713ad42015-06-24 11:45:32 -070046 @Thunk BaseRecyclerView mRv;
Winson Chungb1777442015-06-16 13:35:04 -070047 private BaseRecyclerViewFastScrollPopup mPopup;
48
49 private AnimatorSet mScrollbarAnimator;
50
51 private int mThumbInactiveColor;
52 private int mThumbActiveColor;
Sunny Goyalb713ad42015-06-24 11:45:32 -070053 @Thunk Point mThumbOffset = new Point(-1, -1);
54 @Thunk Paint mThumbPaint;
Winson Chungb1777442015-06-16 13:35:04 -070055 private int mThumbMinWidth;
56 private int mThumbMaxWidth;
Sunny Goyalb713ad42015-06-24 11:45:32 -070057 @Thunk int mThumbWidth;
58 @Thunk int mThumbHeight;
Winson67795952015-08-20 12:23:52 -070059 private int mThumbCurvature;
60 private Path mThumbPath = new Path();
61 private Paint mTrackPaint;
62 private int mTrackWidth;
Winsond2eb49e2015-08-18 17:43:02 -070063 private float mLastTouchY;
Winson Chungb1777442015-06-16 13:35:04 -070064 // The inset is the buffer around which a point will still register as a click on the scrollbar
65 private int mTouchInset;
66 private boolean mIsDragging;
Winsond2eb49e2015-08-18 17:43:02 -070067 private boolean mIsThumbDetached;
68 private boolean mCanThumbDetach;
Winsonec4845b2015-08-27 10:19:48 -070069 private boolean mIgnoreDragGesture;
Winson Chungb1777442015-06-16 13:35:04 -070070
71 // This is the offset from the top of the scrollbar when the user first starts touching. To
72 // prevent jumping, this offset is applied as the user scrolls.
73 private int mTouchOffset;
74
75 private Rect mInvalidateRect = new Rect();
76 private Rect mTmpRect = new Rect();
77
78 public BaseRecyclerViewFastScrollBar(BaseRecyclerView rv, Resources res) {
79 mRv = rv;
80 mPopup = new BaseRecyclerViewFastScrollPopup(rv, res);
81 mTrackPaint = new Paint();
82 mTrackPaint.setColor(rv.getFastScrollerTrackColor(Color.BLACK));
Winson67795952015-08-20 12:23:52 -070083 mTrackPaint.setAlpha(MAX_TRACK_ALPHA);
Winson Chungb1777442015-06-16 13:35:04 -070084 mThumbInactiveColor = rv.getFastScrollerThumbInactiveColor(
85 res.getColor(R.color.container_fastscroll_thumb_inactive_color));
86 mThumbActiveColor = res.getColor(R.color.container_fastscroll_thumb_active_color);
87 mThumbPaint = new Paint();
Winson67795952015-08-20 12:23:52 -070088 mThumbPaint.setAntiAlias(true);
Winson Chungb1777442015-06-16 13:35:04 -070089 mThumbPaint.setColor(mThumbInactiveColor);
Winson67795952015-08-20 12:23:52 -070090 mThumbPaint.setStyle(Paint.Style.FILL);
Winson Chungb1777442015-06-16 13:35:04 -070091 mThumbWidth = mThumbMinWidth = res.getDimensionPixelSize(R.dimen.container_fastscroll_thumb_min_width);
92 mThumbMaxWidth = res.getDimensionPixelSize(R.dimen.container_fastscroll_thumb_max_width);
93 mThumbHeight = res.getDimensionPixelSize(R.dimen.container_fastscroll_thumb_height);
Winson67795952015-08-20 12:23:52 -070094 mThumbCurvature = mThumbMaxWidth - mThumbMinWidth;
Winson Chungb1777442015-06-16 13:35:04 -070095 mTouchInset = res.getDimensionPixelSize(R.dimen.container_fastscroll_thumb_touch_inset);
96 }
97
Winsond2eb49e2015-08-18 17:43:02 -070098 public void setDetachThumbOnFastScroll() {
99 mCanThumbDetach = true;
100 }
101
102 public void reattachThumbToScroll() {
103 mIsThumbDetached = false;
104 }
105
106 public void setThumbOffset(int x, int y) {
Winson Chungb1777442015-06-16 13:35:04 -0700107 if (mThumbOffset.x == x && mThumbOffset.y == y) {
108 return;
109 }
Winson67795952015-08-20 12:23:52 -0700110 mInvalidateRect.set(mThumbOffset.x - mThumbCurvature, mThumbOffset.y,
111 mThumbOffset.x + mThumbWidth, mThumbOffset.y + mThumbHeight);
Winson Chungb1777442015-06-16 13:35:04 -0700112 mThumbOffset.set(x, y);
Winson67795952015-08-20 12:23:52 -0700113 updateThumbPath();
114 mInvalidateRect.union(mThumbOffset.x - mThumbCurvature, mThumbOffset.y,
115 mThumbOffset.x + mThumbWidth, mThumbOffset.y + mThumbHeight);
Winson Chungb1777442015-06-16 13:35:04 -0700116 mRv.invalidate(mInvalidateRect);
117 }
118
Winsond2eb49e2015-08-18 17:43:02 -0700119 public Point getThumbOffset() {
120 return mThumbOffset;
121 }
122
Winson67795952015-08-20 12:23:52 -0700123 // Setter/getter for the thumb bar width for animations
Winsond2eb49e2015-08-18 17:43:02 -0700124 public void setThumbWidth(int width) {
Winson67795952015-08-20 12:23:52 -0700125 mInvalidateRect.set(mThumbOffset.x - mThumbCurvature, mThumbOffset.y,
126 mThumbOffset.x + mThumbWidth, mThumbOffset.y + mThumbHeight);
Winson Chungb1777442015-06-16 13:35:04 -0700127 mThumbWidth = width;
Winson67795952015-08-20 12:23:52 -0700128 updateThumbPath();
129 mInvalidateRect.union(mThumbOffset.x - mThumbCurvature, mThumbOffset.y,
130 mThumbOffset.x + mThumbWidth, mThumbOffset.y + mThumbHeight);
Winson Chungb1777442015-06-16 13:35:04 -0700131 mRv.invalidate(mInvalidateRect);
132 }
133
Winsond2eb49e2015-08-18 17:43:02 -0700134 public int getThumbWidth() {
Winson Chungb1777442015-06-16 13:35:04 -0700135 return mThumbWidth;
136 }
137
Winson67795952015-08-20 12:23:52 -0700138 // Setter/getter for the track bar width for animations
139 public void setTrackWidth(int width) {
140 mInvalidateRect.set(mThumbOffset.x - mThumbCurvature, 0, mThumbOffset.x + mThumbWidth,
141 mRv.getHeight());
142 mTrackWidth = width;
143 updateThumbPath();
144 mInvalidateRect.union(mThumbOffset.x - mThumbCurvature, 0, mThumbOffset.x + mThumbWidth,
145 mRv.getHeight());
Winson Chungb1777442015-06-16 13:35:04 -0700146 mRv.invalidate(mInvalidateRect);
147 }
148
Winson67795952015-08-20 12:23:52 -0700149 public int getTrackWidth() {
150 return mTrackWidth;
Winson Chungb1777442015-06-16 13:35:04 -0700151 }
152
153 public int getThumbHeight() {
154 return mThumbHeight;
155 }
156
157 public int getThumbMaxWidth() {
158 return mThumbMaxWidth;
159 }
160
Winsond2eb49e2015-08-18 17:43:02 -0700161 public float getLastTouchY() {
162 return mLastTouchY;
163 }
164
165 public boolean isDraggingThumb() {
Winson Chungb1777442015-06-16 13:35:04 -0700166 return mIsDragging;
167 }
168
Winsond2eb49e2015-08-18 17:43:02 -0700169 public boolean isThumbDetached() {
170 return mIsThumbDetached;
171 }
172
Winson Chungb1777442015-06-16 13:35:04 -0700173 /**
174 * Handles the touch event and determines whether to show the fast scroller (or updates it if
175 * it is already showing).
176 */
177 public void handleTouchEvent(MotionEvent ev, int downX, int downY, int lastY) {
178 ViewConfiguration config = ViewConfiguration.get(mRv.getContext());
179
180 int action = ev.getAction();
181 int y = (int) ev.getY();
182 switch (action) {
183 case MotionEvent.ACTION_DOWN:
Winsonec4845b2015-08-27 10:19:48 -0700184 if (isNearThumb(downX, downY)) {
Winson Chungb1777442015-06-16 13:35:04 -0700185 mTouchOffset = downY - mThumbOffset.y;
186 }
187 break;
188 case MotionEvent.ACTION_MOVE:
Winsonec4845b2015-08-27 10:19:48 -0700189 // Check if we should start scrolling, but ignore this fastscroll gesture if we have
190 // exceeded some fixed movement
191 mIgnoreDragGesture |= Math.abs(y - downY) > config.getScaledPagingTouchSlop();
Winson646c2362015-09-03 11:46:11 -0700192 if (!mIsDragging && !mIgnoreDragGesture && mRv.supportsFastScrolling() &&
193 isNearThumb(downX, lastY) &&
Winson Chungb1777442015-06-16 13:35:04 -0700194 Math.abs(y - downY) > config.getScaledTouchSlop()) {
195 mRv.getParent().requestDisallowInterceptTouchEvent(true);
196 mIsDragging = true;
Winsond2eb49e2015-08-18 17:43:02 -0700197 if (mCanThumbDetach) {
198 mIsThumbDetached = true;
199 }
Winson Chungb1777442015-06-16 13:35:04 -0700200 mTouchOffset += (lastY - downY);
201 mPopup.animateVisibility(true);
Winsonc0880492015-08-21 11:16:27 -0700202 showActiveScrollbar(true);
Winson Chungb1777442015-06-16 13:35:04 -0700203 }
204 if (mIsDragging) {
205 // Update the fastscroller section name at this touch position
206 int top = mRv.getBackgroundPadding().top;
207 int bottom = mRv.getHeight() - mRv.getBackgroundPadding().bottom - mThumbHeight;
208 float boundedY = (float) Math.max(top, Math.min(bottom, y - mTouchOffset));
209 String sectionName = mRv.scrollToPositionAtProgress((boundedY - top) /
210 (bottom - top));
211 mPopup.setSectionName(sectionName);
212 mPopup.animateVisibility(!sectionName.isEmpty());
Winsonc0880492015-08-21 11:16:27 -0700213 mRv.invalidate(mPopup.updateFastScrollerBounds(lastY));
Winsond2eb49e2015-08-18 17:43:02 -0700214 mLastTouchY = boundedY;
Winson Chungb1777442015-06-16 13:35:04 -0700215 }
216 break;
217 case MotionEvent.ACTION_UP:
218 case MotionEvent.ACTION_CANCEL:
Winson Chungb1777442015-06-16 13:35:04 -0700219 mTouchOffset = 0;
Winsond2eb49e2015-08-18 17:43:02 -0700220 mLastTouchY = 0;
Winsonec4845b2015-08-27 10:19:48 -0700221 mIgnoreDragGesture = false;
Winson Chung4c7fc622015-06-24 16:59:31 -0700222 if (mIsDragging) {
223 mIsDragging = false;
224 mPopup.animateVisibility(false);
Winsonc0880492015-08-21 11:16:27 -0700225 showActiveScrollbar(false);
Winson Chung4c7fc622015-06-24 16:59:31 -0700226 }
Winson Chungb1777442015-06-16 13:35:04 -0700227 break;
228 }
229 }
230
231 public void draw(Canvas canvas) {
232 if (mThumbOffset.x < 0 || mThumbOffset.y < 0) {
233 return;
234 }
235
236 // Draw the scroll bar track and thumb
237 if (mTrackPaint.getAlpha() > 0) {
238 canvas.drawRect(mThumbOffset.x, 0, mThumbOffset.x + mThumbWidth, mRv.getHeight(), mTrackPaint);
239 }
Winson67795952015-08-20 12:23:52 -0700240 canvas.drawPath(mThumbPath, mThumbPaint);
Winson Chungb1777442015-06-16 13:35:04 -0700241
242 // Draw the popup
243 mPopup.draw(canvas);
244 }
245
246 /**
247 * Animates the width and color of the scrollbar.
248 */
Winsonc0880492015-08-21 11:16:27 -0700249 private void showActiveScrollbar(boolean isScrolling) {
Winson Chungb1777442015-06-16 13:35:04 -0700250 if (mScrollbarAnimator != null) {
251 mScrollbarAnimator.cancel();
252 }
Winson67795952015-08-20 12:23:52 -0700253
254 mScrollbarAnimator = new AnimatorSet();
255 ObjectAnimator trackWidthAnim = ObjectAnimator.ofInt(this, "trackWidth",
256 isScrolling ? mThumbMaxWidth : mThumbMinWidth);
Winsond2eb49e2015-08-18 17:43:02 -0700257 ObjectAnimator thumbWidthAnim = ObjectAnimator.ofInt(this, "thumbWidth",
Winson Chungb1777442015-06-16 13:35:04 -0700258 isScrolling ? mThumbMaxWidth : mThumbMinWidth);
Winson67795952015-08-20 12:23:52 -0700259 mScrollbarAnimator.playTogether(trackWidthAnim, thumbWidthAnim);
260 if (mThumbActiveColor != mThumbInactiveColor) {
261 ValueAnimator colorAnimation = ValueAnimator.ofObject(new ArgbEvaluator(),
262 mThumbPaint.getColor(), isScrolling ? mThumbActiveColor : mThumbInactiveColor);
263 colorAnimation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
264 @Override
265 public void onAnimationUpdate(ValueAnimator animator) {
266 mThumbPaint.setColor((Integer) animator.getAnimatedValue());
267 mRv.invalidate(mThumbOffset.x, mThumbOffset.y, mThumbOffset.x + mThumbWidth,
268 mThumbOffset.y + mThumbHeight);
269 }
270 });
271 mScrollbarAnimator.play(colorAnimation);
272 }
Winson Chungb1777442015-06-16 13:35:04 -0700273 mScrollbarAnimator.setDuration(SCROLL_BAR_VIS_DURATION);
274 mScrollbarAnimator.start();
275 }
276
277 /**
Winson67795952015-08-20 12:23:52 -0700278 * Updates the path for the thumb drawable.
279 */
280 private void updateThumbPath() {
281 mThumbCurvature = mThumbMaxWidth - mThumbWidth;
282 mThumbPath.reset();
283 mThumbPath.moveTo(mThumbOffset.x + mThumbWidth, mThumbOffset.y); // tr
284 mThumbPath.lineTo(mThumbOffset.x + mThumbWidth, mThumbOffset.y + mThumbHeight); // br
285 mThumbPath.lineTo(mThumbOffset.x, mThumbOffset.y + mThumbHeight); // bl
286 mThumbPath.cubicTo(mThumbOffset.x, mThumbOffset.y + mThumbHeight,
287 mThumbOffset.x - mThumbCurvature, mThumbOffset.y + mThumbHeight / 2,
288 mThumbOffset.x, mThumbOffset.y); // bl2tl
289 mThumbPath.close();
290 }
291
292 /**
Winson Chungb1777442015-06-16 13:35:04 -0700293 * Returns whether the specified points are near the scroll bar bounds.
294 */
Hyunyoung Songf4cbb142016-06-10 12:00:02 -0700295 public boolean isNearThumb(int x, int y) {
Winson Chungb1777442015-06-16 13:35:04 -0700296 mTmpRect.set(mThumbOffset.x, mThumbOffset.y, mThumbOffset.x + mThumbWidth,
297 mThumbOffset.y + mThumbHeight);
298 mTmpRect.inset(mTouchInset, mTouchInset);
299 return mTmpRect.contains(x, y);
300 }
301}