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.widget; 18 19 import android.content.Context; 20 import android.graphics.Point; 21 import android.util.AttributeSet; 22 import android.view.MotionEvent; 23 import android.view.View; 24 25 import com.android.launcher3.BaseRecyclerView; 26 import com.android.launcher3.R; 27 28 import androidx.recyclerview.widget.LinearLayoutManager; 29 import androidx.recyclerview.widget.RecyclerView; 30 import androidx.recyclerview.widget.RecyclerView.OnItemTouchListener; 31 32 /** 33 * The widgets recycler view. 34 */ 35 public class WidgetsRecyclerView extends BaseRecyclerView implements OnItemTouchListener { 36 37 private WidgetsListAdapter mAdapter; 38 39 private final int mScrollbarTop; 40 41 private final Point mFastScrollerOffset = new Point(); 42 private boolean mTouchDownOnScroller; 43 WidgetsRecyclerView(Context context)44 public WidgetsRecyclerView(Context context) { 45 this(context, null); 46 } 47 WidgetsRecyclerView(Context context, AttributeSet attrs)48 public WidgetsRecyclerView(Context context, AttributeSet attrs) { 49 this(context, attrs, 0); 50 } 51 WidgetsRecyclerView(Context context, AttributeSet attrs, int defStyleAttr)52 public WidgetsRecyclerView(Context context, AttributeSet attrs, int defStyleAttr) { 53 // API 21 and below only support 3 parameter ctor. 54 super(context, attrs, defStyleAttr); 55 mScrollbarTop = getResources().getDimensionPixelSize(R.dimen.dynamic_grid_edge_margin); 56 addOnItemTouchListener(this); 57 } 58 59 @Override onFinishInflate()60 protected void onFinishInflate() { 61 super.onFinishInflate(); 62 // create a layout manager with Launcher's context so that scroll position 63 // can be preserved during screen rotation. 64 setLayoutManager(new LinearLayoutManager(getContext())); 65 } 66 67 @Override setAdapter(Adapter adapter)68 public void setAdapter(Adapter adapter) { 69 super.setAdapter(adapter); 70 mAdapter = (WidgetsListAdapter) adapter; 71 } 72 73 /** 74 * Maps the touch (from 0..1) to the adapter position that should be visible. 75 */ 76 @Override scrollToPositionAtProgress(float touchFraction)77 public String scrollToPositionAtProgress(float touchFraction) { 78 // Skip early if widgets are not bound. 79 if (isModelNotReady()) { 80 return ""; 81 } 82 83 // Stop the scroller if it is scrolling 84 stopScroll(); 85 86 int rowCount = mAdapter.getItemCount(); 87 float pos = rowCount * touchFraction; 88 int availableScrollHeight = getAvailableScrollHeight(); 89 LinearLayoutManager layoutManager = ((LinearLayoutManager) getLayoutManager()); 90 layoutManager.scrollToPositionWithOffset(0, (int) -(availableScrollHeight * touchFraction)); 91 92 int posInt = (int) ((touchFraction == 1)? pos -1 : pos); 93 return mAdapter.getSectionName(posInt); 94 } 95 96 /** 97 * Updates the bounds for the scrollbar. 98 */ 99 @Override onUpdateScrollbar(int dy)100 public void onUpdateScrollbar(int dy) { 101 // Skip early if widgets are not bound. 102 if (isModelNotReady()) { 103 return; 104 } 105 106 // Skip early if, there no child laid out in the container. 107 int scrollY = getCurrentScrollY(); 108 if (scrollY < 0) { 109 mScrollbar.setThumbOffsetY(-1); 110 return; 111 } 112 113 synchronizeScrollBarThumbOffsetToViewScroll(scrollY, getAvailableScrollHeight()); 114 } 115 116 @Override getCurrentScrollY()117 public int getCurrentScrollY() { 118 // Skip early if widgets are not bound. 119 if (isModelNotReady() || getChildCount() == 0) { 120 return -1; 121 } 122 123 View child = getChildAt(0); 124 int rowIndex = getChildPosition(child); 125 int y = (child.getMeasuredHeight() * rowIndex); 126 int offset = getLayoutManager().getDecoratedTop(child); 127 128 return getPaddingTop() + y - offset; 129 } 130 131 /** 132 * Returns the available scroll height: 133 * AvailableScrollHeight = Total height of the all items - last page height 134 */ 135 @Override getAvailableScrollHeight()136 protected int getAvailableScrollHeight() { 137 View child = getChildAt(0); 138 return child.getMeasuredHeight() * mAdapter.getItemCount() - getScrollbarTrackHeight() 139 - mScrollbarTop; 140 } 141 isModelNotReady()142 private boolean isModelNotReady() { 143 return mAdapter.getItemCount() == 0; 144 } 145 146 @Override getScrollBarTop()147 public int getScrollBarTop() { 148 return mScrollbarTop; 149 } 150 151 @Override onInterceptTouchEvent(RecyclerView rv, MotionEvent e)152 public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) { 153 if (e.getAction() == MotionEvent.ACTION_DOWN) { 154 mTouchDownOnScroller = 155 mScrollbar.isHitInParent(e.getX(), e.getY(), mFastScrollerOffset); 156 } 157 if (mTouchDownOnScroller) { 158 return mScrollbar.handleTouchEvent(e, mFastScrollerOffset); 159 } 160 return false; 161 } 162 163 @Override onTouchEvent(RecyclerView rv, MotionEvent e)164 public void onTouchEvent(RecyclerView rv, MotionEvent e) { 165 if (mTouchDownOnScroller) { 166 mScrollbar.handleTouchEvent(e, mFastScrollerOffset); 167 } 168 } 169 170 @Override onRequestDisallowInterceptTouchEvent(boolean disallowIntercept)171 public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) { } 172 }