2015-05-06 17:49:33 -07:00
|
|
|
/*
|
|
|
|
|
* Copyright (C) 2015 The Android Open Source Project
|
|
|
|
|
*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package com.android.launcher3.widget;
|
|
|
|
|
|
|
|
|
|
import android.content.Context;
|
2015-06-22 19:48:07 -07:00
|
|
|
import android.graphics.Canvas;
|
2015-06-16 13:35:04 -07:00
|
|
|
import android.graphics.Color;
|
2015-06-04 11:37:46 -07:00
|
|
|
import android.support.v7.widget.LinearLayoutManager;
|
2015-05-06 17:49:33 -07:00
|
|
|
import android.util.AttributeSet;
|
2015-06-04 11:37:46 -07:00
|
|
|
import android.view.View;
|
2015-05-22 11:12:27 -07:00
|
|
|
import com.android.launcher3.BaseRecyclerView;
|
2015-06-04 11:37:46 -07:00
|
|
|
import com.android.launcher3.model.PackageItemInfo;
|
2015-06-16 13:35:04 -07:00
|
|
|
import com.android.launcher3.model.WidgetsModel;
|
2015-05-06 18:58:31 -07:00
|
|
|
|
2015-05-06 17:49:33 -07:00
|
|
|
/**
|
2015-05-22 11:12:27 -07:00
|
|
|
* The widgets recycler view.
|
2015-05-06 17:49:33 -07:00
|
|
|
*/
|
2015-05-22 11:12:27 -07:00
|
|
|
public class WidgetsRecyclerView extends BaseRecyclerView {
|
2015-05-06 17:49:33 -07:00
|
|
|
|
2015-06-04 11:37:46 -07:00
|
|
|
private static final String TAG = "WidgetsRecyclerView";
|
2015-05-29 12:00:44 -07:00
|
|
|
private WidgetsModel mWidgets;
|
2015-06-16 13:35:04 -07:00
|
|
|
private ScrollPositionState mScrollPosState = new ScrollPositionState();
|
2015-05-29 12:00:44 -07:00
|
|
|
|
2015-05-22 11:12:27 -07:00
|
|
|
public WidgetsRecyclerView(Context context) {
|
2015-05-06 17:49:33 -07:00
|
|
|
this(context, null);
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-22 11:12:27 -07:00
|
|
|
public WidgetsRecyclerView(Context context, AttributeSet attrs) {
|
2015-05-06 17:49:33 -07:00
|
|
|
this(context, attrs, 0);
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-22 11:12:27 -07:00
|
|
|
public WidgetsRecyclerView(Context context, AttributeSet attrs, int defStyleAttr) {
|
2015-06-08 18:17:13 -07:00
|
|
|
// API 21 and below only support 3 parameter ctor.
|
2015-05-06 17:49:33 -07:00
|
|
|
super(context, attrs, defStyleAttr);
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-08 18:17:13 -07:00
|
|
|
public WidgetsRecyclerView(Context context, AttributeSet attrs, int defStyleAttr,
|
|
|
|
|
int defStyleRes) {
|
|
|
|
|
this(context, attrs, defStyleAttr);
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-29 12:00:44 -07:00
|
|
|
@Override
|
|
|
|
|
protected void onFinishInflate() {
|
|
|
|
|
super.onFinishInflate();
|
|
|
|
|
addOnItemTouchListener(this);
|
2016-05-10 13:46:13 -07:00
|
|
|
// create a layout manager with Launcher's context so that scroll position
|
|
|
|
|
// can be preserved during screen rotation.
|
|
|
|
|
setLayoutManager(new LinearLayoutManager(getContext()));
|
2015-05-29 12:00:44 -07:00
|
|
|
}
|
|
|
|
|
|
2015-06-16 13:35:04 -07:00
|
|
|
public int getFastScrollerTrackColor(int defaultTrackColor) {
|
|
|
|
|
return Color.WHITE;
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-29 12:00:44 -07:00
|
|
|
/**
|
|
|
|
|
* Sets the widget model in this view, used to determine the fast scroll position.
|
|
|
|
|
*/
|
|
|
|
|
public void setWidgets(WidgetsModel widgets) {
|
|
|
|
|
mWidgets = widgets;
|
|
|
|
|
}
|
2015-06-22 19:48:07 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* We need to override the draw to ensure that we don't draw the overscroll effect beyond the
|
|
|
|
|
* background bounds.
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
protected void dispatchDraw(Canvas canvas) {
|
|
|
|
|
canvas.clipRect(mBackgroundPadding.left, mBackgroundPadding.top,
|
|
|
|
|
getWidth() - mBackgroundPadding.right,
|
|
|
|
|
getHeight() - mBackgroundPadding.bottom);
|
|
|
|
|
super.dispatchDraw(canvas);
|
|
|
|
|
}
|
2015-05-29 12:00:44 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Maps the touch (from 0..1) to the adapter position that should be visible.
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public String scrollToPositionAtProgress(float touchFraction) {
|
2015-08-31 15:02:26 -07:00
|
|
|
// Skip early if widgets are not bound.
|
|
|
|
|
if (mWidgets == null) {
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Skip early if there are no widgets.
|
2015-06-16 13:35:04 -07:00
|
|
|
int rowCount = mWidgets.getPackageSize();
|
|
|
|
|
if (rowCount == 0) {
|
|
|
|
|
return "";
|
|
|
|
|
}
|
2015-06-04 11:37:46 -07:00
|
|
|
|
2015-06-16 13:35:04 -07:00
|
|
|
// Stop the scroller if it is scrolling
|
|
|
|
|
stopScroll();
|
|
|
|
|
|
2015-08-21 11:16:27 -07:00
|
|
|
getCurScrollState(mScrollPosState, -1);
|
2015-06-16 13:35:04 -07:00
|
|
|
float pos = rowCount * touchFraction;
|
2015-08-21 11:16:27 -07:00
|
|
|
int availableScrollHeight = getAvailableScrollHeight(rowCount);
|
2015-06-04 11:37:46 -07:00
|
|
|
LinearLayoutManager layoutManager = ((LinearLayoutManager) getLayoutManager());
|
2015-06-16 13:35:04 -07:00
|
|
|
layoutManager.scrollToPositionWithOffset(0, (int) -(availableScrollHeight * touchFraction));
|
2015-06-04 11:37:46 -07:00
|
|
|
|
2015-06-16 13:35:04 -07:00
|
|
|
int posInt = (int) ((touchFraction == 1)? pos -1 : pos);
|
2015-06-04 11:37:46 -07:00
|
|
|
PackageItemInfo p = mWidgets.getPackageItemInfo(posInt);
|
|
|
|
|
return p.titleSectionName;
|
2015-05-29 12:00:44 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Updates the bounds for the scrollbar.
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
2015-08-18 17:43:02 -07:00
|
|
|
public void onUpdateScrollbar(int dy) {
|
2015-08-31 15:02:26 -07:00
|
|
|
// Skip early if widgets are not bound.
|
|
|
|
|
if (mWidgets == null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2015-05-29 12:00:44 -07:00
|
|
|
|
2015-08-31 15:02:26 -07:00
|
|
|
// Skip early if there are no widgets.
|
|
|
|
|
int rowCount = mWidgets.getPackageSize();
|
2015-05-29 12:00:44 -07:00
|
|
|
if (rowCount == 0) {
|
2015-08-18 17:43:02 -07:00
|
|
|
mScrollbar.setThumbOffset(-1, -1);
|
2015-05-29 12:00:44 -07:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-04 11:37:46 -07:00
|
|
|
// Skip early if, there no child laid out in the container.
|
2015-08-21 11:16:27 -07:00
|
|
|
getCurScrollState(mScrollPosState, -1);
|
2015-06-16 13:35:04 -07:00
|
|
|
if (mScrollPosState.rowIndex < 0) {
|
2015-08-18 17:43:02 -07:00
|
|
|
mScrollbar.setThumbOffset(-1, -1);
|
2015-06-04 11:37:46 -07:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-18 17:43:02 -07:00
|
|
|
synchronizeScrollBarThumbOffsetToViewScroll(mScrollPosState, rowCount);
|
2015-05-29 12:00:44 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the current scroll state.
|
|
|
|
|
*/
|
2015-08-21 11:16:27 -07:00
|
|
|
protected void getCurScrollState(ScrollPositionState stateOut, int viewTypeMask) {
|
2015-05-29 12:00:44 -07:00
|
|
|
stateOut.rowIndex = -1;
|
|
|
|
|
stateOut.rowTopOffset = -1;
|
2015-08-21 11:16:27 -07:00
|
|
|
stateOut.itemPos = -1;
|
2015-05-29 12:00:44 -07:00
|
|
|
|
2015-08-31 15:02:26 -07:00
|
|
|
// Skip early if widgets are not bound.
|
|
|
|
|
if (mWidgets == null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2015-05-29 12:00:44 -07:00
|
|
|
|
|
|
|
|
// Return early if there are no items
|
2015-08-31 15:02:26 -07:00
|
|
|
int rowCount = mWidgets.getPackageSize();
|
2015-05-29 12:00:44 -07:00
|
|
|
if (rowCount == 0) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2015-06-04 11:37:46 -07:00
|
|
|
View child = getChildAt(0);
|
|
|
|
|
int position = getChildPosition(child);
|
|
|
|
|
|
|
|
|
|
stateOut.rowIndex = position;
|
|
|
|
|
stateOut.rowTopOffset = getLayoutManager().getDecoratedTop(child);
|
2015-08-21 11:16:27 -07:00
|
|
|
stateOut.itemPos = position;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected int getTop(int rowIndex) {
|
|
|
|
|
if (getChildCount() == 0) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// All the rows are the same height, return any child height
|
|
|
|
|
View child = getChildAt(0);
|
|
|
|
|
return child.getMeasuredHeight() * rowIndex;
|
2015-05-29 12:00:44 -07:00
|
|
|
}
|
2015-05-06 17:49:33 -07:00
|
|
|
}
|