blob: 40c5ed65db39f5c231655eebd680f8382d187c17 [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.ObjectAnimator;
Winson Chungb1777442015-06-16 13:35:04 -070019import android.content.res.Resources;
20import android.graphics.Canvas;
21import android.graphics.Color;
22import android.graphics.Paint;
Winson67795952015-08-20 12:23:52 -070023import android.graphics.Path;
Winson Chungb1777442015-06-16 13:35:04 -070024import android.graphics.Rect;
Sunny Goyal5d9fb0e2016-10-08 17:43:48 -070025import android.util.Property;
Winson Chungb1777442015-06-16 13:35:04 -070026import android.view.MotionEvent;
Sunny Goyal5d9fb0e2016-10-08 17:43:48 -070027import android.view.View;
Winson Chungb1777442015-06-16 13:35:04 -070028import android.view.ViewConfiguration;
Sunny Goyal5d9fb0e2016-10-08 17:43:48 -070029import android.widget.TextView;
Sunny Goyalb713ad42015-06-24 11:45:32 -070030
Winson Chungb1777442015-06-16 13:35:04 -070031/**
32 * The track and scrollbar that shows when you scroll the list.
33 */
34public class BaseRecyclerViewFastScrollBar {
35
36 public interface FastScrollFocusableView {
Winsonc0880492015-08-21 11:16:27 -070037 void setFastScrollFocusState(final FastBitmapDrawable.State focusState, boolean animated);
Winson Chungb1777442015-06-16 13:35:04 -070038 }
39
Sunny Goyal5d9fb0e2016-10-08 17:43:48 -070040 private static final Property<BaseRecyclerViewFastScrollBar, Integer> TRACK_WIDTH =
41 new Property<BaseRecyclerViewFastScrollBar, Integer>(Integer.class, "width") {
42
43 @Override
44 public Integer get(BaseRecyclerViewFastScrollBar scrollBar) {
45 return scrollBar.mWidth;
46 }
47
48 @Override
49 public void set(BaseRecyclerViewFastScrollBar scrollBar, Integer value) {
50 scrollBar.setTrackWidth(value);
51 }
52 };
53
Winson Chungb1777442015-06-16 13:35:04 -070054 private final static int MAX_TRACK_ALPHA = 30;
55 private final static int SCROLL_BAR_VIS_DURATION = 150;
Sunny Goyal5d9fb0e2016-10-08 17:43:48 -070056 private static final float FAST_SCROLL_OVERLAY_Y_OFFSET_FACTOR = 1.5f;
Winson Chungb1777442015-06-16 13:35:04 -070057
Sunny Goyal5d9fb0e2016-10-08 17:43:48 -070058 private final Rect mTmpRect = new Rect();
59 private final BaseRecyclerView mRv;
Winson Chungb1777442015-06-16 13:35:04 -070060
Sunny Goyal5d9fb0e2016-10-08 17:43:48 -070061 private final boolean mIsRtl;
Winson Chungb1777442015-06-16 13:35:04 -070062
Winson Chungb1777442015-06-16 13:35:04 -070063 // The inset is the buffer around which a point will still register as a click on the scrollbar
Sunny Goyal5d9fb0e2016-10-08 17:43:48 -070064 private final int mTouchInset;
65
66 private final int mMinWidth;
67 private final int mMaxWidth;
68
69 // Current width of the track
70 private int mWidth;
71 private ObjectAnimator mWidthAnimator;
72
73 private final Path mThumbPath = new Path();
74 private final Paint mThumbPaint;
75 private final int mThumbHeight;
76
77 private final Paint mTrackPaint;
78
79 private float mLastTouchY;
Winson Chungb1777442015-06-16 13:35:04 -070080 private boolean mIsDragging;
Winsond2eb49e2015-08-18 17:43:02 -070081 private boolean mIsThumbDetached;
82 private boolean mCanThumbDetach;
Winsonec4845b2015-08-27 10:19:48 -070083 private boolean mIgnoreDragGesture;
Winson Chungb1777442015-06-16 13:35:04 -070084
85 // This is the offset from the top of the scrollbar when the user first starts touching. To
86 // prevent jumping, this offset is applied as the user scrolls.
Sunny Goyal5d9fb0e2016-10-08 17:43:48 -070087 private int mTouchOffsetY;
88 private int mThumbOffsetY;
Winson Chungb1777442015-06-16 13:35:04 -070089
Sunny Goyal5d9fb0e2016-10-08 17:43:48 -070090 // Fast scroller popup
91 private TextView mPopupView;
92 private boolean mPopupVisible;
93 private String mPopupSectionName;
Winson Chungb1777442015-06-16 13:35:04 -070094
95 public BaseRecyclerViewFastScrollBar(BaseRecyclerView rv, Resources res) {
96 mRv = rv;
Winson Chungb1777442015-06-16 13:35:04 -070097 mTrackPaint = new Paint();
98 mTrackPaint.setColor(rv.getFastScrollerTrackColor(Color.BLACK));
Winson67795952015-08-20 12:23:52 -070099 mTrackPaint.setAlpha(MAX_TRACK_ALPHA);
Sunny Goyal5d9fb0e2016-10-08 17:43:48 -0700100
Winson Chungb1777442015-06-16 13:35:04 -0700101 mThumbPaint = new Paint();
Winson67795952015-08-20 12:23:52 -0700102 mThumbPaint.setAntiAlias(true);
Sunny Goyal5d9fb0e2016-10-08 17:43:48 -0700103 mThumbPaint.setColor(Utilities.getColorAccent(rv.getContext()));
Winson67795952015-08-20 12:23:52 -0700104 mThumbPaint.setStyle(Paint.Style.FILL);
Sunny Goyal5d9fb0e2016-10-08 17:43:48 -0700105
106 mWidth = mMinWidth = res.getDimensionPixelSize(R.dimen.container_fastscroll_thumb_min_width);
107 mMaxWidth = res.getDimensionPixelSize(R.dimen.container_fastscroll_thumb_max_width);
Winson Chungb1777442015-06-16 13:35:04 -0700108 mThumbHeight = res.getDimensionPixelSize(R.dimen.container_fastscroll_thumb_height);
109 mTouchInset = res.getDimensionPixelSize(R.dimen.container_fastscroll_thumb_touch_inset);
Sunny Goyal5d9fb0e2016-10-08 17:43:48 -0700110 mIsRtl = Utilities.isRtl(res);
111 updateThumbPath();
112 }
113
114 public void setPopupView(View popup) {
115 mPopupView = (TextView) popup;
Winson Chungb1777442015-06-16 13:35:04 -0700116 }
117
Winsond2eb49e2015-08-18 17:43:02 -0700118 public void setDetachThumbOnFastScroll() {
119 mCanThumbDetach = true;
120 }
121
122 public void reattachThumbToScroll() {
123 mIsThumbDetached = false;
124 }
125
Sunny Goyal5d9fb0e2016-10-08 17:43:48 -0700126 private int getDrawLeft() {
127 return mIsRtl ? 0 : (mRv.getWidth() - mMaxWidth);
128 }
129
130 public void setThumbOffsetY(int y) {
131 if (mThumbOffsetY == y) {
Winson Chungb1777442015-06-16 13:35:04 -0700132 return;
133 }
Sunny Goyal5d9fb0e2016-10-08 17:43:48 -0700134
135 // Invalidate the previous and new thumb area
136 int drawLeft = getDrawLeft();
137 mTmpRect.set(drawLeft, mThumbOffsetY, drawLeft + mMaxWidth, mThumbOffsetY + mThumbHeight);
138 mThumbOffsetY = y;
139 mTmpRect.union(drawLeft, mThumbOffsetY, drawLeft + mMaxWidth, mThumbOffsetY + mThumbHeight);
140 mRv.invalidate(mTmpRect);
141 }
142
143 public int getThumbOffsetY() {
144 return mThumbOffsetY;
145 }
146
147 private void setTrackWidth(int width) {
148 if (mWidth == width) {
149 return;
150 }
151 int left = getDrawLeft();
152 // Invalidate the whole scroll bar area.
Sunny Goyal00e10682016-10-18 14:23:46 +0100153 mRv.invalidate(left, 0, left + mMaxWidth, mRv.getScrollbarTrackHeight());
Sunny Goyal5d9fb0e2016-10-08 17:43:48 -0700154
155 mWidth = width;
Winson67795952015-08-20 12:23:52 -0700156 updateThumbPath();
Winson Chungb1777442015-06-16 13:35:04 -0700157 }
158
Sunny Goyal5d9fb0e2016-10-08 17:43:48 -0700159 /**
160 * Updates the path for the thumb drawable.
161 */
162 private void updateThumbPath() {
163 int smallWidth = mIsRtl ? mWidth : -mWidth;
164 int largeWidth = mIsRtl ? mMaxWidth : -mMaxWidth;
Winsond2eb49e2015-08-18 17:43:02 -0700165
Sunny Goyal5d9fb0e2016-10-08 17:43:48 -0700166 mThumbPath.reset();
167 mThumbPath.moveTo(0, 0);
168 mThumbPath.lineTo(0, mThumbHeight); // Left edge
169 mThumbPath.lineTo(smallWidth, mThumbHeight); // bottom edge
170 mThumbPath.cubicTo(smallWidth, mThumbHeight, // right edge
171 largeWidth, mThumbHeight / 2,
172 smallWidth, 0);
173 mThumbPath.close();
Winson Chungb1777442015-06-16 13:35:04 -0700174 }
175
176 public int getThumbHeight() {
177 return mThumbHeight;
178 }
179
Winsond2eb49e2015-08-18 17:43:02 -0700180 public boolean isDraggingThumb() {
Winson Chungb1777442015-06-16 13:35:04 -0700181 return mIsDragging;
182 }
183
Winsond2eb49e2015-08-18 17:43:02 -0700184 public boolean isThumbDetached() {
185 return mIsThumbDetached;
186 }
187
Winson Chungb1777442015-06-16 13:35:04 -0700188 /**
189 * Handles the touch event and determines whether to show the fast scroller (or updates it if
190 * it is already showing).
191 */
192 public void handleTouchEvent(MotionEvent ev, int downX, int downY, int lastY) {
193 ViewConfiguration config = ViewConfiguration.get(mRv.getContext());
194
195 int action = ev.getAction();
196 int y = (int) ev.getY();
197 switch (action) {
198 case MotionEvent.ACTION_DOWN:
Winsonec4845b2015-08-27 10:19:48 -0700199 if (isNearThumb(downX, downY)) {
Sunny Goyal5d9fb0e2016-10-08 17:43:48 -0700200 mTouchOffsetY = downY - mThumbOffsetY;
Winson Chungb1777442015-06-16 13:35:04 -0700201 }
202 break;
203 case MotionEvent.ACTION_MOVE:
Winsonec4845b2015-08-27 10:19:48 -0700204 // Check if we should start scrolling, but ignore this fastscroll gesture if we have
205 // exceeded some fixed movement
206 mIgnoreDragGesture |= Math.abs(y - downY) > config.getScaledPagingTouchSlop();
Winson646c2362015-09-03 11:46:11 -0700207 if (!mIsDragging && !mIgnoreDragGesture && mRv.supportsFastScrolling() &&
208 isNearThumb(downX, lastY) &&
Winson Chungb1777442015-06-16 13:35:04 -0700209 Math.abs(y - downY) > config.getScaledTouchSlop()) {
210 mRv.getParent().requestDisallowInterceptTouchEvent(true);
211 mIsDragging = true;
Winsond2eb49e2015-08-18 17:43:02 -0700212 if (mCanThumbDetach) {
213 mIsThumbDetached = true;
214 }
Sunny Goyal5d9fb0e2016-10-08 17:43:48 -0700215 mTouchOffsetY += (lastY - downY);
216 animatePopupVisibility(true);
Winsonc0880492015-08-21 11:16:27 -0700217 showActiveScrollbar(true);
Winson Chungb1777442015-06-16 13:35:04 -0700218 }
219 if (mIsDragging) {
220 // Update the fastscroller section name at this touch position
Sunny Goyal00e10682016-10-18 14:23:46 +0100221 int bottom = mRv.getScrollbarTrackHeight() - mThumbHeight;
222 float boundedY = (float) Math.max(0, Math.min(bottom, y - mTouchOffsetY));
223 String sectionName = mRv.scrollToPositionAtProgress(boundedY / bottom);
Sunny Goyal5d9fb0e2016-10-08 17:43:48 -0700224 if (!sectionName.equals(mPopupSectionName)) {
225 mPopupSectionName = sectionName;
226 mPopupView.setText(sectionName);
227 }
228 animatePopupVisibility(!sectionName.isEmpty());
229 updatePopupY(lastY);
Winsond2eb49e2015-08-18 17:43:02 -0700230 mLastTouchY = boundedY;
Sunny Goyal5d9fb0e2016-10-08 17:43:48 -0700231 setThumbOffsetY((int) mLastTouchY);
Winson Chungb1777442015-06-16 13:35:04 -0700232 }
233 break;
234 case MotionEvent.ACTION_UP:
235 case MotionEvent.ACTION_CANCEL:
Sunny Goyal5d9fb0e2016-10-08 17:43:48 -0700236 mTouchOffsetY = 0;
Winsond2eb49e2015-08-18 17:43:02 -0700237 mLastTouchY = 0;
Winsonec4845b2015-08-27 10:19:48 -0700238 mIgnoreDragGesture = false;
Winson Chung4c7fc622015-06-24 16:59:31 -0700239 if (mIsDragging) {
240 mIsDragging = false;
Sunny Goyal5d9fb0e2016-10-08 17:43:48 -0700241 animatePopupVisibility(false);
Winsonc0880492015-08-21 11:16:27 -0700242 showActiveScrollbar(false);
Winson Chung4c7fc622015-06-24 16:59:31 -0700243 }
Winson Chungb1777442015-06-16 13:35:04 -0700244 break;
245 }
246 }
247
248 public void draw(Canvas canvas) {
Sunny Goyal5d9fb0e2016-10-08 17:43:48 -0700249 if (mThumbOffsetY < 0) {
Winson Chungb1777442015-06-16 13:35:04 -0700250 return;
251 }
Sunny Goyal5d9fb0e2016-10-08 17:43:48 -0700252 int saveCount = canvas.save(Canvas.MATRIX_SAVE_FLAG);
253 if (!mIsRtl) {
254 canvas.translate(mRv.getWidth(), 0);
Winson Chungb1777442015-06-16 13:35:04 -0700255 }
Sunny Goyal5d9fb0e2016-10-08 17:43:48 -0700256 // Draw the track
257 int thumbWidth = mIsRtl ? mWidth : -mWidth;
Sunny Goyal00e10682016-10-18 14:23:46 +0100258 canvas.drawRect(0, 0, thumbWidth, mRv.getScrollbarTrackHeight(), mTrackPaint);
Winson Chungb1777442015-06-16 13:35:04 -0700259
Sunny Goyal5d9fb0e2016-10-08 17:43:48 -0700260 canvas.translate(0, mThumbOffsetY);
261 canvas.drawPath(mThumbPath, mThumbPaint);
262 canvas.restoreToCount(saveCount);
Winson Chungb1777442015-06-16 13:35:04 -0700263 }
264
265 /**
Sunny Goyal5d9fb0e2016-10-08 17:43:48 -0700266 * Animates the width of the scrollbar.
Winson Chungb1777442015-06-16 13:35:04 -0700267 */
Winsonc0880492015-08-21 11:16:27 -0700268 private void showActiveScrollbar(boolean isScrolling) {
Sunny Goyal5d9fb0e2016-10-08 17:43:48 -0700269 if (mWidthAnimator != null) {
270 mWidthAnimator.cancel();
Winson Chungb1777442015-06-16 13:35:04 -0700271 }
Winson67795952015-08-20 12:23:52 -0700272
Sunny Goyal5d9fb0e2016-10-08 17:43:48 -0700273 mWidthAnimator = ObjectAnimator.ofInt(this, TRACK_WIDTH,
274 isScrolling ? mMaxWidth : mMinWidth);
275 mWidthAnimator.setDuration(SCROLL_BAR_VIS_DURATION);
276 mWidthAnimator.start();
Winson67795952015-08-20 12:23:52 -0700277 }
278
279 /**
Winson Chungb1777442015-06-16 13:35:04 -0700280 * Returns whether the specified points are near the scroll bar bounds.
281 */
Hyunyoung Songf4cbb142016-06-10 12:00:02 -0700282 public boolean isNearThumb(int x, int y) {
Sunny Goyal5d9fb0e2016-10-08 17:43:48 -0700283 int left = getDrawLeft();
284 mTmpRect.set(left, mThumbOffsetY, left + mMaxWidth, mThumbOffsetY + mThumbHeight);
Winson Chungb1777442015-06-16 13:35:04 -0700285 mTmpRect.inset(mTouchInset, mTouchInset);
286 return mTmpRect.contains(x, y);
287 }
Sunny Goyal5d9fb0e2016-10-08 17:43:48 -0700288
289 private void animatePopupVisibility(boolean visible) {
290 if (mPopupVisible != visible) {
291 mPopupVisible = visible;
292 mPopupView.animate().cancel();
293 mPopupView.animate().alpha(visible ? 1f : 0f).setDuration(visible ? 200 : 150).start();
294 }
295 }
296
297 private void updatePopupY(int lastTouchY) {
298 int height = mPopupView.getHeight();
299 float top = lastTouchY - (FAST_SCROLL_OVERLAY_Y_OFFSET_FACTOR * height);
Sunny Goyal00e10682016-10-18 14:23:46 +0100300 top = Math.max(mMaxWidth, Math.min(top, mRv.getScrollbarTrackHeight() - mMaxWidth - height));
Sunny Goyal5d9fb0e2016-10-08 17:43:48 -0700301 mPopupView.setTranslationY(top);
302 }
Winson Chungb1777442015-06-16 13:35:04 -0700303}