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