Winson Chung | 9480415 | 2015-05-08 13:06:44 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.launcher3; |
| 18 | |
vadimt | 31fa653 | 2020-04-14 19:21:07 -0700 | [diff] [blame] | 19 | import static android.view.accessibility.AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED; |
| 20 | |
Winson Chung | 9480415 | 2015-05-08 13:06:44 -0700 | [diff] [blame] | 21 | import android.content.Context; |
Winson Chung | 9480415 | 2015-05-08 13:06:44 -0700 | [diff] [blame] | 22 | import android.util.AttributeSet; |
vadimt | ac752e3 | 2020-03-24 17:58:03 -0700 | [diff] [blame] | 23 | import android.util.Log; |
Winson Chung | 9480415 | 2015-05-08 13:06:44 -0700 | [diff] [blame] | 24 | import android.view.MotionEvent; |
Sunny Goyal | f1fbc3f | 2017-10-10 15:21:15 -0700 | [diff] [blame] | 25 | import android.view.View; |
Sunny Goyal | 5d9fb0e | 2016-10-08 17:43:48 -0700 | [diff] [blame] | 26 | import android.view.ViewGroup; |
vadimt | 31fa653 | 2020-04-14 19:21:07 -0700 | [diff] [blame] | 27 | import android.view.accessibility.AccessibilityNodeInfo; |
Sunny Goyal | 5d9fb0e | 2016-10-08 17:43:48 -0700 | [diff] [blame] | 28 | |
Samuel Fufa | f5a4deb | 2020-03-04 16:24:06 -0800 | [diff] [blame] | 29 | import androidx.recyclerview.widget.RecyclerView; |
| 30 | |
vadimt | 758a1d9 | 2019-09-04 12:08:27 -0700 | [diff] [blame] | 31 | import com.android.launcher3.compat.AccessibilityManagerCompat; |
vadimt | ac752e3 | 2020-03-24 17:58:03 -0700 | [diff] [blame] | 32 | import com.android.launcher3.testing.TestProtocol; |
vadimt | 31fa653 | 2020-04-14 19:21:07 -0700 | [diff] [blame] | 33 | import com.android.launcher3.views.ActivityContext; |
Sunny Goyal | 89d5c5a | 2017-06-23 16:12:50 -0700 | [diff] [blame] | 34 | import com.android.launcher3.views.RecyclerViewFastScroller; |
Winson Chung | 9480415 | 2015-05-08 13:06:44 -0700 | [diff] [blame] | 35 | |
Winson Chung | b177744 | 2015-06-16 13:35:04 -0700 | [diff] [blame] | 36 | |
Winson Chung | 9480415 | 2015-05-08 13:06:44 -0700 | [diff] [blame] | 37 | /** |
Hyunyoung Song | ac5f6af | 2015-05-29 12:00:44 -0700 | [diff] [blame] | 38 | * 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 Chung | 9480415 | 2015-05-08 13:06:44 -0700 | [diff] [blame] | 43 | */ |
Sunny Goyal | 3661bfa | 2018-03-01 16:29:15 -0800 | [diff] [blame] | 44 | public abstract class BaseRecyclerView extends RecyclerView { |
Winson Chung | 9480415 | 2015-05-08 13:06:44 -0700 | [diff] [blame] | 45 | |
Sunny Goyal | 89d5c5a | 2017-06-23 16:12:50 -0700 | [diff] [blame] | 46 | protected RecyclerViewFastScroller mScrollbar; |
Sunny Goyal | 9e4c359 | 2017-06-07 14:05:08 -0700 | [diff] [blame] | 47 | |
Winson Chung | 5f4e0fd | 2015-05-22 11:12:27 -0700 | [diff] [blame] | 48 | public BaseRecyclerView(Context context) { |
Winson Chung | 9480415 | 2015-05-08 13:06:44 -0700 | [diff] [blame] | 49 | this(context, null); |
| 50 | } |
| 51 | |
Winson Chung | 5f4e0fd | 2015-05-22 11:12:27 -0700 | [diff] [blame] | 52 | public BaseRecyclerView(Context context, AttributeSet attrs) { |
Winson Chung | 9480415 | 2015-05-08 13:06:44 -0700 | [diff] [blame] | 53 | this(context, attrs, 0); |
| 54 | } |
| 55 | |
Winson Chung | 5f4e0fd | 2015-05-22 11:12:27 -0700 | [diff] [blame] | 56 | public BaseRecyclerView(Context context, AttributeSet attrs, int defStyleAttr) { |
Winson Chung | 9480415 | 2015-05-08 13:06:44 -0700 | [diff] [blame] | 57 | super(context, attrs, defStyleAttr); |
Winson Chung | 9480415 | 2015-05-08 13:06:44 -0700 | [diff] [blame] | 58 | } |
| 59 | |
Winson Chung | 9480415 | 2015-05-08 13:06:44 -0700 | [diff] [blame] | 60 | @Override |
Sunny Goyal | 5d9fb0e | 2016-10-08 17:43:48 -0700 | [diff] [blame] | 61 | protected void onAttachedToWindow() { |
| 62 | super.onAttachedToWindow(); |
Mario Bertschler | ac9408a | 2017-10-18 10:31:36 -0700 | [diff] [blame] | 63 | bindFastScrollbar(); |
| 64 | } |
| 65 | |
| 66 | public void bindFastScrollbar() { |
| 67 | ViewGroup parent = (ViewGroup) getParent().getParent(); |
Sunny Goyal | 89d5c5a | 2017-06-23 16:12:50 -0700 | [diff] [blame] | 68 | mScrollbar = parent.findViewById(R.id.fast_scroller); |
Mario Bertschler | ac9408a | 2017-10-18 10:31:36 -0700 | [diff] [blame] | 69 | mScrollbar.setRecyclerView(this, parent.findViewById(R.id.fast_scroller_popup)); |
Mario Bertschler | 2d3157a | 2017-11-27 13:10:44 -0800 | [diff] [blame] | 70 | onUpdateScrollbar(0); |
Sunny Goyal | 5d9fb0e | 2016-10-08 17:43:48 -0700 | [diff] [blame] | 71 | } |
| 72 | |
Sunny Goyal | 3661bfa | 2018-03-01 16:29:15 -0800 | [diff] [blame] | 73 | public RecyclerViewFastScroller getScrollbar() { |
| 74 | return mScrollbar; |
Winson Chung | 9480415 | 2015-05-08 13:06:44 -0700 | [diff] [blame] | 75 | } |
| 76 | |
Sunny Goyal | f1fbc3f | 2017-10-10 15:21:15 -0700 | [diff] [blame] | 77 | public int getScrollBarTop() { |
| 78 | return getPaddingTop(); |
| 79 | } |
| 80 | |
Winson Chung | 9480415 | 2015-05-08 13:06:44 -0700 | [diff] [blame] | 81 | /** |
Sunny Goyal | 00e1068 | 2016-10-18 14:23:46 +0100 | [diff] [blame] | 82 | * Returns the height of the fast scroll bar |
Winson | c088049 | 2015-08-21 11:16:27 -0700 | [diff] [blame] | 83 | */ |
Sunny Goyal | 89d5c5a | 2017-06-23 16:12:50 -0700 | [diff] [blame] | 84 | public int getScrollbarTrackHeight() { |
Mario Bertschler | 2d3157a | 2017-11-27 13:10:44 -0800 | [diff] [blame] | 85 | return mScrollbar.getHeight() - getScrollBarTop() - getPaddingBottom(); |
Winson | c088049 | 2015-08-21 11:16:27 -0700 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | /** |
Winson Chung | b177744 | 2015-06-16 13:35:04 -0700 | [diff] [blame] | 89 | * Returns the available scroll height: |
| 90 | * AvailableScrollHeight = Total height of the all items - last page height |
Winson Chung | b177744 | 2015-06-16 13:35:04 -0700 | [diff] [blame] | 91 | */ |
Winson | b655b88 | 2016-07-11 18:59:18 -0700 | [diff] [blame] | 92 | protected abstract int getAvailableScrollHeight(); |
Winson Chung | b177744 | 2015-06-16 13:35:04 -0700 | [diff] [blame] | 93 | |
| 94 | /** |
| 95 | * Returns the available scroll bar height: |
| 96 | * AvailableScrollBarHeight = Total height of the visible view - thumb height |
| 97 | */ |
| 98 | protected int getAvailableScrollBarHeight() { |
Sunny Goyal | 00e1068 | 2016-10-18 14:23:46 +0100 | [diff] [blame] | 99 | int availableScrollBarHeight = getScrollbarTrackHeight() - mScrollbar.getThumbHeight(); |
Winson Chung | b177744 | 2015-06-16 13:35:04 -0700 | [diff] [blame] | 100 | return availableScrollBarHeight; |
| 101 | } |
| 102 | |
Hyunyoung Song | ac5f6af | 2015-05-29 12:00:44 -0700 | [diff] [blame] | 103 | /** |
Winson Chung | b177744 | 2015-06-16 13:35:04 -0700 | [diff] [blame] | 104 | * 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 | * |
Winson | b655b88 | 2016-07-11 18:59:18 -0700 | [diff] [blame] | 108 | * @param scrollY the current scroll y |
Hyunyoung Song | ac5f6af | 2015-05-29 12:00:44 -0700 | [diff] [blame] | 109 | */ |
Winson | b655b88 | 2016-07-11 18:59:18 -0700 | [diff] [blame] | 110 | protected void synchronizeScrollBarThumbOffsetToViewScroll(int scrollY, |
| 111 | int availableScrollHeight) { |
Winson Chung | b177744 | 2015-06-16 13:35:04 -0700 | [diff] [blame] | 112 | // Only show the scrollbar if there is height to be scrolled |
| 113 | if (availableScrollHeight <= 0) { |
Sunny Goyal | 5d9fb0e | 2016-10-08 17:43:48 -0700 | [diff] [blame] | 114 | mScrollbar.setThumbOffsetY(-1); |
Winson Chung | b177744 | 2015-06-16 13:35:04 -0700 | [diff] [blame] | 115 | return; |
Hyunyoung Song | ac5f6af | 2015-05-29 12:00:44 -0700 | [diff] [blame] | 116 | } |
Hyunyoung Song | ac5f6af | 2015-05-29 12:00:44 -0700 | [diff] [blame] | 117 | |
Winson Chung | b177744 | 2015-06-16 13:35:04 -0700 | [diff] [blame] | 118 | // 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 Goyal | 00e1068 | 2016-10-18 14:23:46 +0100 | [diff] [blame] | 121 | int scrollBarY = |
| 122 | (int) (((float) scrollY / availableScrollHeight) * getAvailableScrollBarHeight()); |
Hyunyoung Song | ac5f6af | 2015-05-29 12:00:44 -0700 | [diff] [blame] | 123 | |
Winson Chung | b177744 | 2015-06-16 13:35:04 -0700 | [diff] [blame] | 124 | // Calculate the position and size of the scroll bar |
Sunny Goyal | 5d9fb0e | 2016-10-08 17:43:48 -0700 | [diff] [blame] | 125 | mScrollbar.setThumbOffsetY(scrollBarY); |
Winson Chung | 4b9f979 | 2015-06-12 18:02:52 -0700 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | /** |
Sunny Goyal | f1fbc3f | 2017-10-10 15:21:15 -0700 | [diff] [blame] | 129 | * 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 Goyal | ae6e318 | 2019-04-30 12:04:37 -0700 | [diff] [blame] | 133 | float[] point = new float[2]; |
| 134 | point[0] = ev.getX(); |
| 135 | point[1] = ev.getY(); |
Sunny Goyal | f1fbc3f | 2017-10-10 15:21:15 -0700 | [diff] [blame] | 136 | Utilities.mapCoordInSelfToDescendant(mScrollbar, eventSource, point); |
| 137 | // IF the MotionEvent is inside the thumb, container should not be pulled down. |
Sunny Goyal | ae6e318 | 2019-04-30 12:04:37 -0700 | [diff] [blame] | 138 | if (mScrollbar.shouldBlockIntercept((int) point[0], (int) point[1])) { |
Sunny Goyal | f1fbc3f | 2017-10-10 15:21:15 -0700 | [diff] [blame] | 139 | 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 Fufa | f5a4deb | 2020-03-04 16:24:06 -0800 | [diff] [blame] | 147 | return getAdapter() == null || getAdapter().getItemCount() == 0; |
Sunny Goyal | f1fbc3f | 2017-10-10 15:21:15 -0700 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | /** |
Winson | c088049 | 2015-08-21 11:16:27 -0700 | [diff] [blame] | 151 | * @return whether fast scrolling is supported in the current state. |
Winson | 646c236 | 2015-09-03 11:46:11 -0700 | [diff] [blame] | 152 | */ |
Sunny Goyal | 89d5c5a | 2017-06-23 16:12:50 -0700 | [diff] [blame] | 153 | public boolean supportsFastScrolling() { |
Winson | 646c236 | 2015-09-03 11:46:11 -0700 | [diff] [blame] | 154 | return true; |
| 155 | } |
| 156 | |
| 157 | /** |
Hyunyoung Song | ac5f6af | 2015-05-29 12:00:44 -0700 | [diff] [blame] | 158 | * Maps the touch (from 0..1) to the adapter position that should be visible. |
| 159 | * <p>Override in each subclass of this base class. |
Winson | c088049 | 2015-08-21 11:16:27 -0700 | [diff] [blame] | 160 | * |
| 161 | * @return the scroll top of this recycler view. |
Hyunyoung Song | ac5f6af | 2015-05-29 12:00:44 -0700 | [diff] [blame] | 162 | */ |
Peter Schiller | 2e22b5d | 2016-07-12 12:45:10 -0700 | [diff] [blame] | 163 | public abstract int getCurrentScrollY(); |
Winson | c088049 | 2015-08-21 11:16:27 -0700 | [diff] [blame] | 164 | |
| 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 Goyal | 89d5c5a | 2017-06-23 16:12:50 -0700 | [diff] [blame] | 169 | public abstract String scrollToPositionAtProgress(float touchFraction); |
Hyunyoung Song | ac5f6af | 2015-05-29 12:00:44 -0700 | [diff] [blame] | 170 | |
| 171 | /** |
| 172 | * Updates the bounds for the scrollbar. |
| 173 | * <p>Override in each subclass of this base class. |
| 174 | */ |
Sunny Goyal | 89d5c5a | 2017-06-23 16:12:50 -0700 | [diff] [blame] | 175 | public abstract void onUpdateScrollbar(int dy); |
Hyunyoung Song | ac5f6af | 2015-05-29 12:00:44 -0700 | [diff] [blame] | 176 | |
| 177 | /** |
Winson Chung | b177744 | 2015-06-16 13:35:04 -0700 | [diff] [blame] | 178 | * <p>Override in each subclass of this base class. |
Hyunyoung Song | ac5f6af | 2015-05-29 12:00:44 -0700 | [diff] [blame] | 179 | */ |
Sunny Goyal | 89d5c5a | 2017-06-23 16:12:50 -0700 | [diff] [blame] | 180 | public void onFastScrollCompleted() {} |
vadimt | 758a1d9 | 2019-09-04 12:08:27 -0700 | [diff] [blame] | 181 | |
| 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 | } |
vadimt | 31fa653 | 2020-04-14 19:21:07 -0700 | [diff] [blame] | 190 | |
| 191 | @Override |
| 192 | public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) { |
| 193 | super.onInitializeAccessibilityNodeInfo(info); |
| 194 | if (isLayoutSuppressed()) info.setScrollable(false); |
vadimt | 31fa653 | 2020-04-14 19:21:07 -0700 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | @Override |
| 198 | public void setLayoutFrozen(boolean frozen) { |
| 199 | final boolean changing = frozen != isLayoutSuppressed(); |
| 200 | super.setLayoutFrozen(frozen); |
| 201 | if (changing) { |
vadimt | 822efc9 | 2020-04-17 19:04:41 -0700 | [diff] [blame] | 202 | ActivityContext.lookupContext(getContext()).getDragLayer() |
| 203 | .sendAccessibilityEvent(TYPE_WINDOW_CONTENT_CHANGED); |
vadimt | 31fa653 | 2020-04-14 19:21:07 -0700 | [diff] [blame] | 204 | } |
| 205 | } |
Winson Chung | 9480415 | 2015-05-08 13:06:44 -0700 | [diff] [blame] | 206 | } |