blob: 4fecc3da5fe8ea27e7b4b76e1cede7dc5935a479 [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
Mario Bertschleree4ee422016-12-27 15:41:24 -080031import com.android.launcher3.config.FeatureFlags;
32
Winson Chungb1777442015-06-16 13:35:04 -070033/**
34 * The track and scrollbar that shows when you scroll the list.
35 */
36public class BaseRecyclerViewFastScrollBar {
37
38 public interface FastScrollFocusableView {
Winsonc0880492015-08-21 11:16:27 -070039 void setFastScrollFocusState(final FastBitmapDrawable.State focusState, boolean animated);
Winson Chungb1777442015-06-16 13:35:04 -070040 }
41
Sunny Goyal5d9fb0e2016-10-08 17:43:48 -070042 private static final Property<BaseRecyclerViewFastScrollBar, Integer> TRACK_WIDTH =
43 new Property<BaseRecyclerViewFastScrollBar, Integer>(Integer.class, "width") {
44
45 @Override
46 public Integer get(BaseRecyclerViewFastScrollBar scrollBar) {
47 return scrollBar.mWidth;
48 }
49
50 @Override
51 public void set(BaseRecyclerViewFastScrollBar scrollBar, Integer value) {
52 scrollBar.setTrackWidth(value);
53 }
54 };
55
Winson Chungb1777442015-06-16 13:35:04 -070056 private final static int MAX_TRACK_ALPHA = 30;
57 private final static int SCROLL_BAR_VIS_DURATION = 150;
Sunny Goyal5d9fb0e2016-10-08 17:43:48 -070058 private static final float FAST_SCROLL_OVERLAY_Y_OFFSET_FACTOR = 1.5f;
Winson Chungb1777442015-06-16 13:35:04 -070059
Sunny Goyal5d9fb0e2016-10-08 17:43:48 -070060 private final Rect mTmpRect = new Rect();
61 private final BaseRecyclerView mRv;
Winson Chungb1777442015-06-16 13:35:04 -070062
Sunny Goyal5d9fb0e2016-10-08 17:43:48 -070063 private final boolean mIsRtl;
Winson Chungb1777442015-06-16 13:35:04 -070064
Winson Chungb1777442015-06-16 13:35:04 -070065 // 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 -070066 private final int mTouchInset;
67
68 private final int mMinWidth;
69 private final int mMaxWidth;
70
71 // Current width of the track
72 private int mWidth;
73 private ObjectAnimator mWidthAnimator;
74
75 private final Path mThumbPath = new Path();
76 private final Paint mThumbPaint;
77 private final int mThumbHeight;
78
79 private final Paint mTrackPaint;
80
81 private float mLastTouchY;
Winson Chungb1777442015-06-16 13:35:04 -070082 private boolean mIsDragging;
Winsond2eb49e2015-08-18 17:43:02 -070083 private boolean mIsThumbDetached;
84 private boolean mCanThumbDetach;
Winsonec4845b2015-08-27 10:19:48 -070085 private boolean mIgnoreDragGesture;
Winson Chungb1777442015-06-16 13:35:04 -070086
87 // This is the offset from the top of the scrollbar when the user first starts touching. To
88 // prevent jumping, this offset is applied as the user scrolls.
Sunny Goyal5d9fb0e2016-10-08 17:43:48 -070089 private int mTouchOffsetY;
90 private int mThumbOffsetY;
Winson Chungb1777442015-06-16 13:35:04 -070091
Sunny Goyal5d9fb0e2016-10-08 17:43:48 -070092 // Fast scroller popup
93 private TextView mPopupView;
94 private boolean mPopupVisible;
95 private String mPopupSectionName;
Winson Chungb1777442015-06-16 13:35:04 -070096
97 public BaseRecyclerViewFastScrollBar(BaseRecyclerView rv, Resources res) {
98 mRv = rv;
Winson Chungb1777442015-06-16 13:35:04 -070099 mTrackPaint = new Paint();
100 mTrackPaint.setColor(rv.getFastScrollerTrackColor(Color.BLACK));
Winson67795952015-08-20 12:23:52 -0700101 mTrackPaint.setAlpha(MAX_TRACK_ALPHA);
Sunny Goyal5d9fb0e2016-10-08 17:43:48 -0700102
Winson Chungb1777442015-06-16 13:35:04 -0700103 mThumbPaint = new Paint();
Winson67795952015-08-20 12:23:52 -0700104 mThumbPaint.setAntiAlias(true);
Sunny Goyal5d9fb0e2016-10-08 17:43:48 -0700105 mThumbPaint.setColor(Utilities.getColorAccent(rv.getContext()));
Winson67795952015-08-20 12:23:52 -0700106 mThumbPaint.setStyle(Paint.Style.FILL);
Sunny Goyal5d9fb0e2016-10-08 17:43:48 -0700107
108 mWidth = mMinWidth = res.getDimensionPixelSize(R.dimen.container_fastscroll_thumb_min_width);
109 mMaxWidth = res.getDimensionPixelSize(R.dimen.container_fastscroll_thumb_max_width);
Winson Chungb1777442015-06-16 13:35:04 -0700110 mThumbHeight = res.getDimensionPixelSize(R.dimen.container_fastscroll_thumb_height);
111 mTouchInset = res.getDimensionPixelSize(R.dimen.container_fastscroll_thumb_touch_inset);
Sunny Goyal5d9fb0e2016-10-08 17:43:48 -0700112 mIsRtl = Utilities.isRtl(res);
113 updateThumbPath();
114 }
115
116 public void setPopupView(View popup) {
117 mPopupView = (TextView) popup;
Winson Chungb1777442015-06-16 13:35:04 -0700118 }
119
Winsond2eb49e2015-08-18 17:43:02 -0700120 public void setDetachThumbOnFastScroll() {
121 mCanThumbDetach = true;
122 }
123
124 public void reattachThumbToScroll() {
125 mIsThumbDetached = false;
126 }
127
Sunny Goyal5d9fb0e2016-10-08 17:43:48 -0700128 private int getDrawLeft() {
129 return mIsRtl ? 0 : (mRv.getWidth() - mMaxWidth);
130 }
131
132 public void setThumbOffsetY(int y) {
133 if (mThumbOffsetY == y) {
Winson Chungb1777442015-06-16 13:35:04 -0700134 return;
135 }
Sunny Goyal5d9fb0e2016-10-08 17:43:48 -0700136
137 // Invalidate the previous and new thumb area
138 int drawLeft = getDrawLeft();
139 mTmpRect.set(drawLeft, mThumbOffsetY, drawLeft + mMaxWidth, mThumbOffsetY + mThumbHeight);
140 mThumbOffsetY = y;
141 mTmpRect.union(drawLeft, mThumbOffsetY, drawLeft + mMaxWidth, mThumbOffsetY + mThumbHeight);
142 mRv.invalidate(mTmpRect);
143 }
144
145 public int getThumbOffsetY() {
146 return mThumbOffsetY;
147 }
148
149 private void setTrackWidth(int width) {
150 if (mWidth == width) {
151 return;
152 }
153 int left = getDrawLeft();
154 // Invalidate the whole scroll bar area.
Sunny Goyal00e10682016-10-18 14:23:46 +0100155 mRv.invalidate(left, 0, left + mMaxWidth, mRv.getScrollbarTrackHeight());
Sunny Goyal5d9fb0e2016-10-08 17:43:48 -0700156
157 mWidth = width;
Winson67795952015-08-20 12:23:52 -0700158 updateThumbPath();
Winson Chungb1777442015-06-16 13:35:04 -0700159 }
160
Sunny Goyal5d9fb0e2016-10-08 17:43:48 -0700161 /**
162 * Updates the path for the thumb drawable.
163 */
164 private void updateThumbPath() {
165 int smallWidth = mIsRtl ? mWidth : -mWidth;
166 int largeWidth = mIsRtl ? mMaxWidth : -mMaxWidth;
Winsond2eb49e2015-08-18 17:43:02 -0700167
Sunny Goyal5d9fb0e2016-10-08 17:43:48 -0700168 mThumbPath.reset();
169 mThumbPath.moveTo(0, 0);
170 mThumbPath.lineTo(0, mThumbHeight); // Left edge
171 mThumbPath.lineTo(smallWidth, mThumbHeight); // bottom edge
172 mThumbPath.cubicTo(smallWidth, mThumbHeight, // right edge
173 largeWidth, mThumbHeight / 2,
174 smallWidth, 0);
175 mThumbPath.close();
Winson Chungb1777442015-06-16 13:35:04 -0700176 }
177
178 public int getThumbHeight() {
179 return mThumbHeight;
180 }
181
Winsond2eb49e2015-08-18 17:43:02 -0700182 public boolean isDraggingThumb() {
Winson Chungb1777442015-06-16 13:35:04 -0700183 return mIsDragging;
184 }
185
Winsond2eb49e2015-08-18 17:43:02 -0700186 public boolean isThumbDetached() {
187 return mIsThumbDetached;
188 }
189
Winson Chungb1777442015-06-16 13:35:04 -0700190 /**
191 * Handles the touch event and determines whether to show the fast scroller (or updates it if
192 * it is already showing).
193 */
194 public void handleTouchEvent(MotionEvent ev, int downX, int downY, int lastY) {
195 ViewConfiguration config = ViewConfiguration.get(mRv.getContext());
196
197 int action = ev.getAction();
198 int y = (int) ev.getY();
199 switch (action) {
200 case MotionEvent.ACTION_DOWN:
Winsonec4845b2015-08-27 10:19:48 -0700201 if (isNearThumb(downX, downY)) {
Sunny Goyal5d9fb0e2016-10-08 17:43:48 -0700202 mTouchOffsetY = downY - mThumbOffsetY;
Mario Bertschleree4ee422016-12-27 15:41:24 -0800203 } else if (FeatureFlags.LAUNCHER3_DIRECT_SCROLL
204 && mRv.supportsFastScrolling()
205 && isNearScrollBar(downX)) {
206 calcTouchOffsetAndPrepToFastScroll(downY, lastY);
207 updateFastScrollSectionNameAndThumbOffset(lastY, y);
Winson Chungb1777442015-06-16 13:35:04 -0700208 }
209 break;
210 case MotionEvent.ACTION_MOVE:
Winsonec4845b2015-08-27 10:19:48 -0700211 // Check if we should start scrolling, but ignore this fastscroll gesture if we have
212 // exceeded some fixed movement
213 mIgnoreDragGesture |= Math.abs(y - downY) > config.getScaledPagingTouchSlop();
Winson646c2362015-09-03 11:46:11 -0700214 if (!mIsDragging && !mIgnoreDragGesture && mRv.supportsFastScrolling() &&
215 isNearThumb(downX, lastY) &&
Winson Chungb1777442015-06-16 13:35:04 -0700216 Math.abs(y - downY) > config.getScaledTouchSlop()) {
Mario Bertschleree4ee422016-12-27 15:41:24 -0800217 calcTouchOffsetAndPrepToFastScroll(downY, lastY);
Winson Chungb1777442015-06-16 13:35:04 -0700218 }
219 if (mIsDragging) {
Mario Bertschleree4ee422016-12-27 15:41:24 -0800220 updateFastScrollSectionNameAndThumbOffset(lastY, y);
Winson Chungb1777442015-06-16 13:35:04 -0700221 }
222 break;
223 case MotionEvent.ACTION_UP:
224 case MotionEvent.ACTION_CANCEL:
Sunny Goyal5d9fb0e2016-10-08 17:43:48 -0700225 mTouchOffsetY = 0;
Winsond2eb49e2015-08-18 17:43:02 -0700226 mLastTouchY = 0;
Winsonec4845b2015-08-27 10:19:48 -0700227 mIgnoreDragGesture = false;
Winson Chung4c7fc622015-06-24 16:59:31 -0700228 if (mIsDragging) {
229 mIsDragging = false;
Sunny Goyal5d9fb0e2016-10-08 17:43:48 -0700230 animatePopupVisibility(false);
Winsonc0880492015-08-21 11:16:27 -0700231 showActiveScrollbar(false);
Winson Chung4c7fc622015-06-24 16:59:31 -0700232 }
Winson Chungb1777442015-06-16 13:35:04 -0700233 break;
234 }
235 }
236
Mario Bertschleree4ee422016-12-27 15:41:24 -0800237 private void calcTouchOffsetAndPrepToFastScroll(int downY, int lastY) {
238 mRv.getParent().requestDisallowInterceptTouchEvent(true);
239 mIsDragging = true;
240 if (mCanThumbDetach) {
241 mIsThumbDetached = true;
242 }
243 mTouchOffsetY += (lastY - downY);
244 animatePopupVisibility(true);
245 showActiveScrollbar(true);
246 }
247
248 private void updateFastScrollSectionNameAndThumbOffset(int lastY, int y) {
249 // Update the fastscroller section name at this touch position
250 int bottom = mRv.getScrollbarTrackHeight() - mThumbHeight;
251 float boundedY = (float) Math.max(0, Math.min(bottom, y - mTouchOffsetY));
252 String sectionName = mRv.scrollToPositionAtProgress(boundedY / bottom);
253 if (!sectionName.equals(mPopupSectionName)) {
254 mPopupSectionName = sectionName;
255 mPopupView.setText(sectionName);
256 }
257 animatePopupVisibility(!sectionName.isEmpty());
258 updatePopupY(lastY);
259 mLastTouchY = boundedY;
260 setThumbOffsetY((int) mLastTouchY);
261 }
262
Winson Chungb1777442015-06-16 13:35:04 -0700263 public void draw(Canvas canvas) {
Sunny Goyal5d9fb0e2016-10-08 17:43:48 -0700264 if (mThumbOffsetY < 0) {
Winson Chungb1777442015-06-16 13:35:04 -0700265 return;
266 }
Sunny Goyal5d9fb0e2016-10-08 17:43:48 -0700267 int saveCount = canvas.save(Canvas.MATRIX_SAVE_FLAG);
268 if (!mIsRtl) {
269 canvas.translate(mRv.getWidth(), 0);
Winson Chungb1777442015-06-16 13:35:04 -0700270 }
Sunny Goyal5d9fb0e2016-10-08 17:43:48 -0700271 // Draw the track
272 int thumbWidth = mIsRtl ? mWidth : -mWidth;
Sunny Goyal00e10682016-10-18 14:23:46 +0100273 canvas.drawRect(0, 0, thumbWidth, mRv.getScrollbarTrackHeight(), mTrackPaint);
Winson Chungb1777442015-06-16 13:35:04 -0700274
Sunny Goyal5d9fb0e2016-10-08 17:43:48 -0700275 canvas.translate(0, mThumbOffsetY);
276 canvas.drawPath(mThumbPath, mThumbPaint);
277 canvas.restoreToCount(saveCount);
Winson Chungb1777442015-06-16 13:35:04 -0700278 }
279
280 /**
Sunny Goyal5d9fb0e2016-10-08 17:43:48 -0700281 * Animates the width of the scrollbar.
Winson Chungb1777442015-06-16 13:35:04 -0700282 */
Winsonc0880492015-08-21 11:16:27 -0700283 private void showActiveScrollbar(boolean isScrolling) {
Sunny Goyal5d9fb0e2016-10-08 17:43:48 -0700284 if (mWidthAnimator != null) {
285 mWidthAnimator.cancel();
Winson Chungb1777442015-06-16 13:35:04 -0700286 }
Winson67795952015-08-20 12:23:52 -0700287
Sunny Goyal5d9fb0e2016-10-08 17:43:48 -0700288 mWidthAnimator = ObjectAnimator.ofInt(this, TRACK_WIDTH,
289 isScrolling ? mMaxWidth : mMinWidth);
290 mWidthAnimator.setDuration(SCROLL_BAR_VIS_DURATION);
291 mWidthAnimator.start();
Winson67795952015-08-20 12:23:52 -0700292 }
293
294 /**
Mario Bertschleree4ee422016-12-27 15:41:24 -0800295 * Returns whether the specified point is inside the thumb bounds.
Winson Chungb1777442015-06-16 13:35:04 -0700296 */
Hyunyoung Songf4cbb142016-06-10 12:00:02 -0700297 public boolean isNearThumb(int x, int y) {
Sunny Goyal5d9fb0e2016-10-08 17:43:48 -0700298 int left = getDrawLeft();
299 mTmpRect.set(left, mThumbOffsetY, left + mMaxWidth, mThumbOffsetY + mThumbHeight);
Winson Chungb1777442015-06-16 13:35:04 -0700300 mTmpRect.inset(mTouchInset, mTouchInset);
301 return mTmpRect.contains(x, y);
302 }
Sunny Goyal5d9fb0e2016-10-08 17:43:48 -0700303
Mario Bertschleree4ee422016-12-27 15:41:24 -0800304 /**
305 * Returns whether the specified x position is near the scroll bar.
306 */
307 public boolean isNearScrollBar(int x) {
308 int left = getDrawLeft();
309 return x >= left && x <= left + mMaxWidth;
310 }
311
Sunny Goyal5d9fb0e2016-10-08 17:43:48 -0700312 private void animatePopupVisibility(boolean visible) {
313 if (mPopupVisible != visible) {
314 mPopupVisible = visible;
315 mPopupView.animate().cancel();
316 mPopupView.animate().alpha(visible ? 1f : 0f).setDuration(visible ? 200 : 150).start();
317 }
318 }
319
320 private void updatePopupY(int lastTouchY) {
321 int height = mPopupView.getHeight();
322 float top = lastTouchY - (FAST_SCROLL_OVERLAY_Y_OFFSET_FACTOR * height);
Sunny Goyal00e10682016-10-18 14:23:46 +0100323 top = Math.max(mMaxWidth, Math.min(top, mRv.getScrollbarTrackHeight() - mMaxWidth - height));
Sunny Goyal5d9fb0e2016-10-08 17:43:48 -0700324 mPopupView.setTranslationY(top);
325 }
Winson Chungb1777442015-06-16 13:35:04 -0700326}