• 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.Color;
21 import android.support.v7.widget.LinearLayoutManager;
22 import android.util.AttributeSet;
23 import android.view.View;
24 import com.android.launcher3.BaseRecyclerView;
25 import com.android.launcher3.model.PackageItemInfo;
26 import com.android.launcher3.model.WidgetsModel;
27 
28 /**
29  * The widgets recycler view.
30  */
31 public class WidgetsRecyclerView extends BaseRecyclerView {
32 
33     private static final String TAG = "WidgetsRecyclerView";
34     private WidgetsListAdapter mAdapter;
35 
WidgetsRecyclerView(Context context)36     public WidgetsRecyclerView(Context context) {
37         this(context, null);
38     }
39 
WidgetsRecyclerView(Context context, AttributeSet attrs)40     public WidgetsRecyclerView(Context context, AttributeSet attrs) {
41         this(context, attrs, 0);
42     }
43 
WidgetsRecyclerView(Context context, AttributeSet attrs, int defStyleAttr)44     public WidgetsRecyclerView(Context context, AttributeSet attrs, int defStyleAttr) {
45         // API 21 and below only support 3 parameter ctor.
46         super(context, attrs, defStyleAttr);
47     }
48 
WidgetsRecyclerView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)49     public WidgetsRecyclerView(Context context, AttributeSet attrs, int defStyleAttr,
50             int defStyleRes) {
51         this(context, attrs, defStyleAttr);
52     }
53 
54     @Override
onFinishInflate()55     protected void onFinishInflate() {
56         super.onFinishInflate();
57         addOnItemTouchListener(this);
58         // create a layout manager with Launcher's context so that scroll position
59         // can be preserved during screen rotation.
60         setLayoutManager(new LinearLayoutManager(getContext()));
61     }
62 
getFastScrollerTrackColor(int defaultTrackColor)63     public int getFastScrollerTrackColor(int defaultTrackColor) {
64         return Color.WHITE;
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         int height = child.getMeasuredHeight() * mAdapter.getItemCount();
139         int totalHeight = getPaddingTop() + height + getPaddingBottom();
140         int availableScrollHeight = totalHeight - getScrollbarTrackHeight();
141         return availableScrollHeight;
142     }
143 
isModelNotReady()144     private boolean isModelNotReady() {
145         return mAdapter.getItemCount() == 0;
146     }
147 }