blob: 8eceec04ec669c596036d9aa9c6aa36902caca2c [file] [log] [blame]
Winson Chung94804152015-05-08 13:06:44 -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 */
16
17package com.android.launcher3;
18
vadimt31fa6532020-04-14 19:21:07 -070019import static android.view.accessibility.AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED;
20
Winson Chung94804152015-05-08 13:06:44 -070021import android.content.Context;
Winson Chung94804152015-05-08 13:06:44 -070022import android.util.AttributeSet;
vadimtac752e32020-03-24 17:58:03 -070023import android.util.Log;
Winson Chung94804152015-05-08 13:06:44 -070024import android.view.MotionEvent;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070025import android.view.View;
Sunny Goyal5d9fb0e2016-10-08 17:43:48 -070026import android.view.ViewGroup;
vadimt31fa6532020-04-14 19:21:07 -070027import android.view.accessibility.AccessibilityNodeInfo;
Sunny Goyal5d9fb0e2016-10-08 17:43:48 -070028
Samuel Fufaf5a4deb2020-03-04 16:24:06 -080029import androidx.recyclerview.widget.RecyclerView;
30
vadimt758a1d92019-09-04 12:08:27 -070031import com.android.launcher3.compat.AccessibilityManagerCompat;
vadimtac752e32020-03-24 17:58:03 -070032import com.android.launcher3.testing.TestProtocol;
vadimt31fa6532020-04-14 19:21:07 -070033import com.android.launcher3.views.ActivityContext;
Sunny Goyal89d5c5a2017-06-23 16:12:50 -070034import com.android.launcher3.views.RecyclerViewFastScroller;
Winson Chung94804152015-05-08 13:06:44 -070035
Winson Chungb1777442015-06-16 13:35:04 -070036
Winson Chung94804152015-05-08 13:06:44 -070037/**
Hyunyoung Songac5f6af2015-05-29 12:00:44 -070038 * A base {@link RecyclerView}, which does the following:
39 * <ul>
40 * <li> NOT intercept a touch unless the scrolling velocity is below a predefined threshold.
41 * <li> Enable fast scroller.
42 * </ul>
Winson Chung94804152015-05-08 13:06:44 -070043 */
Sunny Goyal3661bfa2018-03-01 16:29:15 -080044public abstract class BaseRecyclerView extends RecyclerView {
Winson Chung94804152015-05-08 13:06:44 -070045
Sunny Goyal89d5c5a2017-06-23 16:12:50 -070046 protected RecyclerViewFastScroller mScrollbar;
Sunny Goyal9e4c3592017-06-07 14:05:08 -070047
Winson Chung5f4e0fd2015-05-22 11:12:27 -070048 public BaseRecyclerView(Context context) {
Winson Chung94804152015-05-08 13:06:44 -070049 this(context, null);
50 }
51
Winson Chung5f4e0fd2015-05-22 11:12:27 -070052 public BaseRecyclerView(Context context, AttributeSet attrs) {
Winson Chung94804152015-05-08 13:06:44 -070053 this(context, attrs, 0);
54 }
55
Winson Chung5f4e0fd2015-05-22 11:12:27 -070056 public BaseRecyclerView(Context context, AttributeSet attrs, int defStyleAttr) {
Winson Chung94804152015-05-08 13:06:44 -070057 super(context, attrs, defStyleAttr);
Winson Chung94804152015-05-08 13:06:44 -070058 }
59
Winson Chung94804152015-05-08 13:06:44 -070060 @Override
Sunny Goyal5d9fb0e2016-10-08 17:43:48 -070061 protected void onAttachedToWindow() {
62 super.onAttachedToWindow();
Mario Bertschlerac9408a2017-10-18 10:31:36 -070063 bindFastScrollbar();
64 }
65
66 public void bindFastScrollbar() {
67 ViewGroup parent = (ViewGroup) getParent().getParent();
Sunny Goyal89d5c5a2017-06-23 16:12:50 -070068 mScrollbar = parent.findViewById(R.id.fast_scroller);
Mario Bertschlerac9408a2017-10-18 10:31:36 -070069 mScrollbar.setRecyclerView(this, parent.findViewById(R.id.fast_scroller_popup));
Mario Bertschler2d3157a2017-11-27 13:10:44 -080070 onUpdateScrollbar(0);
Sunny Goyal5d9fb0e2016-10-08 17:43:48 -070071 }
72
Sunny Goyal3661bfa2018-03-01 16:29:15 -080073 public RecyclerViewFastScroller getScrollbar() {
74 return mScrollbar;
Winson Chung94804152015-05-08 13:06:44 -070075 }
76
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070077 public int getScrollBarTop() {
78 return getPaddingTop();
79 }
80
Winson Chung94804152015-05-08 13:06:44 -070081 /**
Sunny Goyal00e10682016-10-18 14:23:46 +010082 * Returns the height of the fast scroll bar
Winsonc0880492015-08-21 11:16:27 -070083 */
Sunny Goyal89d5c5a2017-06-23 16:12:50 -070084 public int getScrollbarTrackHeight() {
Mario Bertschler2d3157a2017-11-27 13:10:44 -080085 return mScrollbar.getHeight() - getScrollBarTop() - getPaddingBottom();
Winsonc0880492015-08-21 11:16:27 -070086 }
87
88 /**
Winson Chungb1777442015-06-16 13:35:04 -070089 * Returns the available scroll height:
90 * AvailableScrollHeight = Total height of the all items - last page height
Winson Chungb1777442015-06-16 13:35:04 -070091 */
Winsonb655b882016-07-11 18:59:18 -070092 protected abstract int getAvailableScrollHeight();
Winson Chungb1777442015-06-16 13:35:04 -070093
94 /**
95 * Returns the available scroll bar height:
96 * AvailableScrollBarHeight = Total height of the visible view - thumb height
97 */
98 protected int getAvailableScrollBarHeight() {
Sunny Goyal00e10682016-10-18 14:23:46 +010099 int availableScrollBarHeight = getScrollbarTrackHeight() - mScrollbar.getThumbHeight();
Winson Chungb1777442015-06-16 13:35:04 -0700100 return availableScrollBarHeight;
101 }
102
Hyunyoung Songac5f6af2015-05-29 12:00:44 -0700103 /**
Winson Chungb1777442015-06-16 13:35:04 -0700104 * Updates the scrollbar thumb offset to match the visible scroll of the recycler view. It does
105 * this by mapping the available scroll area of the recycler view to the available space for the
106 * scroll bar.
107 *
Winsonb655b882016-07-11 18:59:18 -0700108 * @param scrollY the current scroll y
Hyunyoung Songac5f6af2015-05-29 12:00:44 -0700109 */
Winsonb655b882016-07-11 18:59:18 -0700110 protected void synchronizeScrollBarThumbOffsetToViewScroll(int scrollY,
111 int availableScrollHeight) {
Winson Chungb1777442015-06-16 13:35:04 -0700112 // Only show the scrollbar if there is height to be scrolled
113 if (availableScrollHeight <= 0) {
Sunny Goyal5d9fb0e2016-10-08 17:43:48 -0700114 mScrollbar.setThumbOffsetY(-1);
Winson Chungb1777442015-06-16 13:35:04 -0700115 return;
Hyunyoung Songac5f6af2015-05-29 12:00:44 -0700116 }
Hyunyoung Songac5f6af2015-05-29 12:00:44 -0700117
Winson Chungb1777442015-06-16 13:35:04 -0700118 // Calculate the current scroll position, the scrollY of the recycler view accounts for the
119 // view padding, while the scrollBarY is drawn right up to the background padding (ignoring
120 // padding)
Sunny Goyal00e10682016-10-18 14:23:46 +0100121 int scrollBarY =
122 (int) (((float) scrollY / availableScrollHeight) * getAvailableScrollBarHeight());
Hyunyoung Songac5f6af2015-05-29 12:00:44 -0700123
Winson Chungb1777442015-06-16 13:35:04 -0700124 // Calculate the position and size of the scroll bar
Sunny Goyal5d9fb0e2016-10-08 17:43:48 -0700125 mScrollbar.setThumbOffsetY(scrollBarY);
Winson Chung4b9f9792015-06-12 18:02:52 -0700126 }
127
128 /**
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700129 * Returns whether the view itself will handle the touch event or not.
130 * @param ev MotionEvent in {@param eventSource}
131 */
132 public boolean shouldContainerScroll(MotionEvent ev, View eventSource) {
Sunny Goyalae6e3182019-04-30 12:04:37 -0700133 float[] point = new float[2];
134 point[0] = ev.getX();
135 point[1] = ev.getY();
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700136 Utilities.mapCoordInSelfToDescendant(mScrollbar, eventSource, point);
137 // IF the MotionEvent is inside the thumb, container should not be pulled down.
Sunny Goyalae6e3182019-04-30 12:04:37 -0700138 if (mScrollbar.shouldBlockIntercept((int) point[0], (int) point[1])) {
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700139 return false;
140 }
141
142 // IF scroller is at the very top OR there is no scroll bar because there is probably not
143 // enough items to scroll, THEN it's okay for the container to be pulled down.
144 if (getCurrentScrollY() == 0) {
145 return true;
146 }
Samuel Fufaf5a4deb2020-03-04 16:24:06 -0800147 return getAdapter() == null || getAdapter().getItemCount() == 0;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700148 }
149
150 /**
Winsonc0880492015-08-21 11:16:27 -0700151 * @return whether fast scrolling is supported in the current state.
Winson646c2362015-09-03 11:46:11 -0700152 */
Sunny Goyal89d5c5a2017-06-23 16:12:50 -0700153 public boolean supportsFastScrolling() {
Winson646c2362015-09-03 11:46:11 -0700154 return true;
155 }
156
157 /**
Hyunyoung Songac5f6af2015-05-29 12:00:44 -0700158 * Maps the touch (from 0..1) to the adapter position that should be visible.
159 * <p>Override in each subclass of this base class.
Winsonc0880492015-08-21 11:16:27 -0700160 *
161 * @return the scroll top of this recycler view.
Hyunyoung Songac5f6af2015-05-29 12:00:44 -0700162 */
Peter Schiller2e22b5d2016-07-12 12:45:10 -0700163 public abstract int getCurrentScrollY();
Winsonc0880492015-08-21 11:16:27 -0700164
165 /**
166 * Maps the touch (from 0..1) to the adapter position that should be visible.
167 * <p>Override in each subclass of this base class.
168 */
Sunny Goyal89d5c5a2017-06-23 16:12:50 -0700169 public abstract String scrollToPositionAtProgress(float touchFraction);
Hyunyoung Songac5f6af2015-05-29 12:00:44 -0700170
171 /**
172 * Updates the bounds for the scrollbar.
173 * <p>Override in each subclass of this base class.
174 */
Sunny Goyal89d5c5a2017-06-23 16:12:50 -0700175 public abstract void onUpdateScrollbar(int dy);
Hyunyoung Songac5f6af2015-05-29 12:00:44 -0700176
177 /**
Winson Chungb1777442015-06-16 13:35:04 -0700178 * <p>Override in each subclass of this base class.
Hyunyoung Songac5f6af2015-05-29 12:00:44 -0700179 */
Sunny Goyal89d5c5a2017-06-23 16:12:50 -0700180 public void onFastScrollCompleted() {}
vadimt758a1d92019-09-04 12:08:27 -0700181
182 @Override
183 public void onScrollStateChanged(int state) {
184 super.onScrollStateChanged(state);
185
186 if (state == SCROLL_STATE_IDLE) {
187 AccessibilityManagerCompat.sendScrollFinishedEventToTest(getContext());
188 }
189 }
vadimt31fa6532020-04-14 19:21:07 -0700190
191 @Override
192 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
193 super.onInitializeAccessibilityNodeInfo(info);
194 if (isLayoutSuppressed()) info.setScrollable(false);
vadimt31fa6532020-04-14 19:21:07 -0700195 }
196
197 @Override
198 public void setLayoutFrozen(boolean frozen) {
199 final boolean changing = frozen != isLayoutSuppressed();
200 super.setLayoutFrozen(frozen);
201 if (changing) {
vadimt822efc92020-04-17 19:04:41 -0700202 ActivityContext.lookupContext(getContext()).getDragLayer()
203 .sendAccessibilityEvent(TYPE_WINDOW_CONTENT_CHANGED);
vadimt31fa6532020-04-14 19:21:07 -0700204 }
205 }
Winson Chung94804152015-05-08 13:06:44 -0700206}