package android.widget;
import android.animation.Animator;
import android.animation.Animator.AnimatorListener;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.annotation.StyleRes;
import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.SystemClock;
import android.text.TextUtils;
import android.text.TextUtils.TruncateAt;
import android.util.IntProperty;
import android.util.MathUtils;
import android.util.Property;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.PointerIcon;
import android.view.View;
import android.view.View.MeasureSpec;
import android.view.ViewConfiguration;
import android.view.ViewGroup.LayoutParams;
import android.view.ViewGroupOverlay;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ImageView.ScaleType;
import com.android.internal.R;
class FastScroller {
private static final int DURATION_FADE_OUT = 300;
private static final int DURATION_FADE_IN = 150;
private static final int DURATION_CROSS_FADE = 50;
private static final int DURATION_RESIZE = 100;
private static final long FADE_TIMEOUT = 1500;
private static final int MIN_PAGES = 4;
private static final int STATE_NONE = 0;
private static final int STATE_VISIBLE = 1;
private static final int STATE_DRAGGING = 2;
private static final int OVERLAY_FLOATING = 0;
private static final int OVERLAY_AT_THUMB = 1;
private static final int OVERLAY_ABOVE_THUMB = 2;
private static final int THUMB_POSITION_MIDPOINT = 0;
private static final int THUMB_POSITION_INSIDE = 1;
private static final int PREVIEW_LEFT = 0;
private static final int PREVIEW_RIGHT = 1;
private static final long TAP_TIMEOUT = ViewConfiguration.getTapTimeout();
private final Rect mTempBounds = new Rect();
private final Rect mTempMargins = new Rect();
private final Rect mContainerRect = new Rect();
private final AbsListView mList;
private final ViewGroupOverlay mOverlay;
private final TextView mPrimaryText;
private final TextView mSecondaryText;
private final ImageView mThumbImage;
private final ImageView mTrackImage;
private final View mPreviewImage;
private final int[] mPreviewResId = new int[2];
private final int mMinimumTouchTarget;
private int mPreviewPadding;
private int mPreviewMinWidth;
private int mPreviewMinHeight;
private int mThumbMinWidth;
private int mThumbMinHeight;
private float mTextSize;
private ColorStateList mTextColor;
private Drawable mThumbDrawable;
private Drawable mTrackDrawable;
private int mTextAppearance;
private int mThumbPosition;
private float mThumbOffset;
private float mThumbRange;
private int mWidth;
private AnimatorSet mDecorAnimation;
private AnimatorSet mPreviewAnimation;
private boolean mShowingPrimary;
private boolean mScrollCompleted;
private int mFirstVisibleItem;
private int mHeaderCount;
private int mCurrentSection = -1;
private int mScrollbarPosition = -1;
private boolean mLongList;
private Object[] mSections;
private boolean mUpdatingLayout;
private int mState;
private boolean mShowingPreview;
private Adapter mListAdapter;
private SectionIndexer mSectionIndexer;
private boolean mLayoutFromRight;
private boolean mEnabled;
private boolean mAlwaysShow;
private int mOverlayPosition;
private int mScrollBarStyle;
private boolean mMatchDragPosition;
private float mInitialTouchY;
private long mPendingDrag = -1;
private int mScaledTouchSlop;
private int mOldItemCount;
private int mOldChildCount;
private final Runnable mDeferHide = new Runnable() {
@Override
public void run() {
setState(STATE_NONE);
}
};
private final AnimatorListener mSwitchPrimaryListener = new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
mShowingPrimary = !mShowingPrimary;
}
};
public FastScroller(AbsListView listView, int styleResId) {
mList = listView;
mOldItemCount = listView.getCount();
mOldChildCount = listView.getChildCount();
final Context context = listView.getContext();
mScaledTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
mScrollBarStyle = listView.getScrollBarStyle();
mScrollCompleted = true;
mState = STATE_VISIBLE;
mMatchDragPosition =
context.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.HONEYCOMB;
mTrackImage = new ImageView(context);
mTrackImage.setScaleType(ScaleType.FIT_XY);
mThumbImage = new ImageView(context);
mThumbImage.setScaleType(ScaleType.FIT_XY);
mPreviewImage = new View(context);
mPreviewImage.setAlpha(0f);
mPrimaryText = createPreviewTextView(context);
mSecondaryText = createPreviewTextView(context);
mMinimumTouchTarget = listView.getResources().getDimensionPixelSize(
com.android.internal.R.dimen.fast_scroller_minimum_touch_target);
setStyle(styleResId);
final ViewGroupOverlay overlay = listView.getOverlay();
mOverlay = overlay;
overlay.add(mTrackImage);
overlay.add(mThumbImage);
overlay.add(mPreviewImage);
overlay.add(mPrimaryText);
overlay.add(mSecondaryText);
getSectionsFromIndexer();
updateLongList(mOldChildCount, mOldItemCount);
setScrollbarPosition(listView.getVerticalScrollbarPosition());
postAutoHide();
}
private void updateAppearance() {
int width = 0;
mTrackImage.setImageDrawable(mTrackDrawable);
if (mTrackDrawable != null) {
width = Math.max(width, mTrackDrawable.getIntrinsicWidth());
}
mThumbImage.setImageDrawable(mThumbDrawable);
mThumbImage.setMinimumWidth(mThumbMinWidth);
mThumbImage.setMinimumHeight(mThumbMinHeight);
if (mThumbDrawable != null) {
width = Math.max(width, mThumbDrawable.getIntrinsicWidth());
}
mWidth = Math.max(width, mThumbMinWidth);
if (mTextAppearance != 0) {
mPrimaryText.setTextAppearance(mTextAppearance);
mSecondaryText.setTextAppearance(mTextAppearance);
}
if (mTextColor != null) {
mPrimaryText.setTextColor(mTextColor);
mSecondaryText.setTextColor(mTextColor);
}
if (mTextSize > 0) {
mPrimaryText.setTextSize(TypedValue.COMPLEX_UNIT_PX, mTextSize);
mSecondaryText.setTextSize(TypedValue.COMPLEX_UNIT_PX, mTextSize);
}
final int padding = mPreviewPadding;
mPrimaryText.setIncludeFontPadding(false);
mPrimaryText.setPadding(padding, padding, padding, padding);
mSecondaryText.setIncludeFontPadding(false);
mSecondaryText.setPadding(padding, padding, padding, padding);
refreshDrawablePressedState();
}
public void setStyle(@StyleRes int resId) {
final Context context = mList.getContext();
final TypedArray ta = context.obtainStyledAttributes(null,
R.styleable.FastScroll, R.attr.fastScrollStyle, resId);
final int N = ta.getIndexCount();
for (int i = 0; i < N; i++) {
final int index = ta.getIndex(i);
switch (index) {
case R.styleable.FastScroll_position:
mOverlayPosition = ta.getInt(index, OVERLAY_FLOATING);
break;
case R.styleable.FastScroll_backgroundLeft:
mPreviewResId[PREVIEW_LEFT] = ta.getResourceId(index, 0);
break;
case R.styleable.FastScroll_backgroundRight:
mPreviewResId[PREVIEW_RIGHT] = ta.getResourceId(index, 0);
break;
case R.styleable.FastScroll_thumbDrawable:
mThumbDrawable = ta.getDrawable(index);
break;
case R.styleable.FastScroll_trackDrawable:
mTrackDrawable = ta.getDrawable(index);
break;
case R.styleable.FastScroll_textAppearance:
mTextAppearance = ta.getResourceId(index, 0);
break;
case R.styleable.FastScroll_textColor:
mTextColor = ta.getColorStateList(index);
break;
case R.styleable.FastScroll_textSize:
mTextSize = ta.getDimensionPixelSize(index, 0);
break;
case R.styleable.FastScroll_minWidth:
mPreviewMinWidth = ta.getDimensionPixelSize(index, 0);
break;
case R.styleable.FastScroll_minHeight:
mPreviewMinHeight = ta.getDimensionPixelSize(index, 0);
break;
case R.styleable.FastScroll_thumbMinWidth:
mThumbMinWidth = ta.getDimensionPixelSize(index, 0);
break;
case R.styleable.FastScroll_thumbMinHeight:
mThumbMinHeight = ta.getDimensionPixelSize(index, 0);
break;
case R.styleable.FastScroll_padding:
mPreviewPadding = ta.getDimensionPixelSize(index, 0);
break;
case R.styleable.FastScroll_thumbPosition:
mThumbPosition = ta.getInt(index, THUMB_POSITION_MIDPOINT);
break;
}
}
ta.recycle();
updateAppearance();
}
public void remove() {
mOverlay.remove(mTrackImage);
mOverlay.remove(mThumbImage);
mOverlay.remove(mPreviewImage);
mOverlay.remove(mPrimaryText);
mOverlay.remove(mSecondaryText);
}
public void setEnabled(boolean enabled) {
if (mEnabled != enabled) {
mEnabled = enabled;
onStateDependencyChanged(true);
}
}
public boolean isEnabled() {
return mEnabled && (mLongList || mAlwaysShow);
}
public void setAlwaysShow(boolean alwaysShow) {
if (mAlwaysShow != alwaysShow) {
mAlwaysShow = alwaysShow;
onStateDependencyChanged(false);
}
}
public boolean isAlwaysShowEnabled() {
return mAlwaysShow;
}
private void onStateDependencyChanged(boolean peekIfEnabled) {
if (isEnabled()) {
if (isAlwaysShowEnabled()) {
setState(STATE_VISIBLE);
} else if (mState == STATE_VISIBLE) {
postAutoHide();
} else if (peekIfEnabled) {
setState(STATE_VISIBLE);
postAutoHide();
}
} else {
stop();
}
mList.resolvePadding();
}
public void setScrollBarStyle(int style) {
if (mScrollBarStyle != style) {
mScrollBarStyle = style;
updateLayout();
}
}
public void stop() {
setState(STATE_NONE);
}
public void setScrollbarPosition(int position) {
if (position == View.SCROLLBAR_POSITION_DEFAULT) {
position = mList.isLayoutRtl() ?
View.SCROLLBAR_POSITION_LEFT : View.SCROLLBAR_POSITION_RIGHT;
}
if (mScrollbarPosition != position) {
mScrollbarPosition = position;
mLayoutFromRight = position != View.SCROLLBAR_POSITION_LEFT;
final int previewResId = mPreviewResId[mLayoutFromRight ? PREVIEW_RIGHT : PREVIEW_LEFT];
mPreviewImage.setBackgroundResource(previewResId);
final int textMinWidth = Math.max(0, mPreviewMinWidth - mPreviewImage.getPaddingLeft()
- mPreviewImage.getPaddingRight());
mPrimaryText.setMinimumWidth(textMinWidth);
mSecondaryText.setMinimumWidth(textMinWidth);
final int textMinHeight = Math.max(0, mPreviewMinHeight - mPreviewImage.getPaddingTop()
- mPreviewImage.getPaddingBottom());
mPrimaryText.setMinimumHeight(textMinHeight);
mSecondaryText.setMinimumHeight(textMinHeight);
updateLayout();
}
}
public int getWidth() {
return mWidth;
}
public void onSizeChanged(int w, int h, int oldw, int oldh) {
updateLayout();
}
public void onItemCountChanged(int childCount, int itemCount) {
if (mOldItemCount != itemCount || mOldChildCount != childCount) {
mOldItemCount = itemCount;
mOldChildCount = childCount;
final boolean hasMoreItems = itemCount - childCount > 0;
if (hasMoreItems && mState != STATE_DRAGGING) {
final int firstVisibleItem = mList.getFirstVisiblePosition();
setThumbPos(getPosFromItemCount(firstVisibleItem, childCount, itemCount));
}
updateLongList(childCount, itemCount);
}
}
private void updateLongList(int childCount, int itemCount) {
final boolean longList = childCount > 0 && itemCount / childCount >= MIN_PAGES;
if (mLongList != longList) {
mLongList = longList;
onStateDependencyChanged(false);
}
}
private TextView createPreviewTextView(Context context) {
final LayoutParams params = new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
final TextView textView = new TextView(context);
textView.setLayoutParams(params);
textView.setSingleLine(true);
textView.setEllipsize(TruncateAt.MIDDLE);
textView.setGravity(Gravity.CENTER);
textView.setAlpha(0f);
textView.setLayoutDirection(mList.getLayoutDirection());
return textView;
}
public void updateLayout() {
if (mUpdatingLayout) {
return;
}
mUpdatingLayout = true;
updateContainerRect();
layoutThumb();
layoutTrack();
updateOffsetAndRange();
final Rect bounds = mTempBounds;
measurePreview(mPrimaryText, bounds);
applyLayout(mPrimaryText, bounds);
measurePreview(mSecondaryText, bounds);
applyLayout(mSecondaryText, bounds);
if (mPreviewImage != null) {
bounds.left -= mPreviewImage.getPaddingLeft();
bounds.top -= mPreviewImage.getPaddingTop();
bounds.right += mPreviewImage.getPaddingRight();
bounds.bottom += mPreviewImage.getPaddingBottom();
applyLayout(mPreviewImage, bounds);
}
mUpdatingLayout = false;
}
private void applyLayout(View view, Rect bounds) {
view.layout(bounds.left, bounds.top, bounds.right, bounds.bottom);
view.setPivotX(mLayoutFromRight ? bounds.right - bounds.left : 0);
}
private void measurePreview(View v, Rect out) {
final Rect margins = mTempMargins;
margins.left = mPreviewImage.getPaddingLeft();
margins.top = mPreviewImage.getPaddingTop();
margins.right = mPreviewImage.getPaddingRight();
margins.bottom = mPreviewImage.getPaddingBottom();
if (mOverlayPosition == OVERLAY_FLOATING) {
measureFloating(v, margins, out);
} else {
measureViewToSide(v, mThumbImage, margins, out);
}
}
private void measureViewToSide(View view, View adjacent, Rect margins, Rect out) {
final int marginLeft;
final int marginTop;
final int marginRight;
if (margins == null) {
marginLeft = 0;
marginTop = 0;
marginRight = 0;
} else {
marginLeft = margins.left;
marginTop = margins.top;
marginRight = margins.right;
}
final Rect container = mContainerRect;
final int containerWidth = container.width();
final int maxWidth;
if (adjacent == null) {
maxWidth = containerWidth;
} else if (mLayoutFromRight) {
maxWidth = adjacent.getLeft();
} else {
maxWidth = containerWidth - adjacent.getRight();
}
final int adjMaxHeight = Math.max(0, container.height());
final int adjMaxWidth = Math.max(0, maxWidth - marginLeft - marginRight);
final int widthMeasureSpec = MeasureSpec.makeMeasureSpec(adjMaxWidth, MeasureSpec.AT_MOST);
final int heightMeasureSpec = MeasureSpec.makeSafeMeasureSpec(
adjMaxHeight, MeasureSpec.UNSPECIFIED);
view.measure(widthMeasureSpec, heightMeasureSpec);
final int width = Math.min(adjMaxWidth, view.getMeasuredWidth());
final int left;
final int right;
if (mLayoutFromRight) {
right = (adjacent == null ? container.right : adjacent.getLeft()) - marginRight;
left = right - width;
} else {
left = (adjacent == null ? container.left : adjacent.getRight()) + marginLeft;
right = left + width;
}
final int top = marginTop;
final int bottom = top + view.getMeasuredHeight();
out.set(left, top, right, bottom);
}
private void measureFloating(View preview, Rect margins, Rect out) {
final int marginLeft;
final int marginTop;
final int marginRight;
if (margins == null) {
marginLeft = 0;
marginTop = 0;
marginRight = 0;
} else {
marginLeft = margins.left;
marginTop = margins.top;
marginRight = margins.right;
}
final Rect container = mContainerRect;
final int containerWidth = container.width();
final int adjMaxHeight = Math.max(0, container.height());
final int adjMaxWidth = Math.max(0, containerWidth - marginLeft - marginRight);
final int widthMeasureSpec = MeasureSpec.makeMeasureSpec(adjMaxWidth, MeasureSpec.AT_MOST);
final int heightMeasureSpec = MeasureSpec.makeSafeMeasureSpec(
adjMaxHeight, MeasureSpec.UNSPECIFIED);
preview.measure(widthMeasureSpec, heightMeasureSpec);
final int containerHeight = container.height();
final int width = preview.getMeasuredWidth();
final int top = containerHeight / 10 + marginTop + container.top;
final int bottom = top + preview.getMeasuredHeight();
final int left = (containerWidth - width) / 2 + container.left;
final int right = left + width;
out.set(left, top, right, bottom);
}
private void updateContainerRect() {
final AbsListView list = mList;
list.resolvePadding();
final Rect container = mContainerRect;
container.left = 0;
container.top = 0;
container.right = list.getWidth();
container.bottom = list.getHeight();
final int scrollbarStyle = mScrollBarStyle;
if (scrollbarStyle == View.SCROLLBARS_INSIDE_INSET
|| scrollbarStyle == View.SCROLLBARS_INSIDE_OVERLAY) {
container.left += list.getPaddingLeft();
container.top += list.getPaddingTop();
container.right -= list.getPaddingRight();
container.bottom -= list.getPaddingBottom();
if (scrollbarStyle == View.SCROLLBARS_INSIDE_INSET) {
final int width = getWidth();
if (mScrollbarPosition == View.SCROLLBAR_POSITION_RIGHT) {
container.right += width;
} else {
container.left -= width;
}
}
}
}
private void layoutThumb() {
final Rect bounds = mTempBounds;
measureViewToSide(mThumbImage, null, null, bounds);
applyLayout(mThumbImage, bounds);
}
private void layoutTrack() {
final View track = mTrackImage;
final View thumb = mThumbImage;
final Rect container = mContainerRect;
final int maxWidth = Math.max(0, container.width());
final int maxHeight = Math.max(0, container.height());
final int widthMeasureSpec = MeasureSpec.makeMeasureSpec(maxWidth, MeasureSpec.AT_MOST);
final int heightMeasureSpec = MeasureSpec.makeSafeMeasureSpec(
maxHeight, MeasureSpec.UNSPECIFIED);
track.measure(widthMeasureSpec, heightMeasureSpec);
final int top;
final int bottom;
if (mThumbPosition == THUMB_POSITION_INSIDE) {
top = container.top;
bottom = container.bottom;
} else {
final int thumbHalfHeight = thumb.getHeight() / 2;
top = container.top + thumbHalfHeight;
bottom = container.bottom - thumbHalfHeight;
}
final int trackWidth = track.getMeasuredWidth();
final int left = thumb.getLeft() + (thumb.getWidth() - trackWidth) / 2;
final int right = left + trackWidth;
track.layout(left, top, right, bottom);
}
private void updateOffsetAndRange() {
final View trackImage = mTrackImage;
final View thumbImage = mThumbImage;
final float min;
final float max;
if (mThumbPosition == THUMB_POSITION_INSIDE) {
final float halfThumbHeight = thumbImage.getHeight() / 2f;
min = trackImage.getTop() + halfThumbHeight;
max = trackImage.getBottom() - halfThumbHeight;
} else{
min = trackImage.getTop();
max = trackImage.getBottom();
}
mThumbOffset = min;
mThumbRange = max - min;
}
private void setState(int state) {
mList.removeCallbacks(mDeferHide);
if (mAlwaysShow && state == STATE_NONE) {
state = STATE_VISIBLE;
}
if (state == mState) {
return;
}
switch (state) {
case STATE_NONE:
transitionToHidden();
break;
case STATE_VISIBLE:
transitionToVisible();
break;
case STATE_DRAGGING:
if (transitionPreviewLayout(mCurrentSection)) {
transitionToDragging();
} else {
transitionToVisible();
}
break;
}
mState = state;
refreshDrawablePressedState();
}
private void refreshDrawablePressedState() {
final boolean isPressed = mState == STATE_DRAGGING;
mThumbImage.setPressed(isPressed);
mTrackImage.setPressed(isPressed);
}
private void transitionToHidden() {
if (mDecorAnimation != null) {
mDecorAnimation.cancel();
}
final Animator fadeOut = groupAnimatorOfFloat(View.ALPHA, 0f, mThumbImage, mTrackImage,
mPreviewImage, mPrimaryText, mSecondaryText).setDuration(DURATION_FADE_OUT);
final float offset = mLayoutFromRight ? mThumbImage.getWidth() : -mThumbImage.getWidth();
final Animator slideOut = groupAnimatorOfFloat(
View.TRANSLATION_X, offset, mThumbImage, mTrackImage)
.setDuration(DURATION_FADE_OUT);
mDecorAnimation = new AnimatorSet();
mDecorAnimation.playTogether(fadeOut, slideOut);
mDecorAnimation.start();
mShowingPreview = false;
}
private void transitionToVisible() {
if (mDecorAnimation != null) {
mDecorAnimation.cancel();
}
final Animator fadeIn = groupAnimatorOfFloat(View.ALPHA, 1f, mThumbImage, mTrackImage)
.setDuration(DURATION_FADE_IN);
final Animator fadeOut = groupAnimatorOfFloat(
View.ALPHA, 0f, mPreviewImage, mPrimaryText, mSecondaryText)
.setDuration(DURATION_FADE_OUT);
final Animator slideIn = groupAnimatorOfFloat(
View.TRANSLATION_X, 0f, mThumbImage, mTrackImage).setDuration(DURATION_FADE_IN);
mDecorAnimation = new AnimatorSet();
mDecorAnimation.playTogether(fadeIn, fadeOut, slideIn);
mDecorAnimation.start();
mShowingPreview = false;
}
private void transitionToDragging() {
if (mDecorAnimation != null) {
mDecorAnimation.cancel();
}
final Animator fadeIn = groupAnimatorOfFloat(
View.ALPHA, 1f, mThumbImage, mTrackImage, mPreviewImage)
.setDuration(DURATION_FADE_IN);
final Animator slideIn = groupAnimatorOfFloat(
View.TRANSLATION_X, 0f, mThumbImage, mTrackImage).setDuration(DURATION_FADE_IN);
mDecorAnimation = new AnimatorSet();
mDecorAnimation.playTogether(fadeIn, slideIn);
mDecorAnimation.start();
mShowingPreview = true;
}
private void postAutoHide() {
mList.removeCallbacks(mDeferHide);
mList.postDelayed(mDeferHide, FADE_TIMEOUT);
}
public void onScroll(int firstVisibleItem, int visibleItemCount, int totalItemCount) {
if (!isEnabled()) {
setState(STATE_NONE);
return;
}
final boolean hasMoreItems = totalItemCount - visibleItemCount > 0;
if (hasMoreItems && mState != STATE_DRAGGING) {
setThumbPos(getPosFromItemCount(firstVisibleItem, visibleItemCount, totalItemCount));
}
mScrollCompleted = true;
if (mFirstVisibleItem != firstVisibleItem) {
mFirstVisibleItem = firstVisibleItem;
if (mState != STATE_DRAGGING) {
setState(STATE_VISIBLE);
postAutoHide();
}
}
}
private void getSectionsFromIndexer() {
mSectionIndexer = null;
Adapter adapter = mList.getAdapter();
if (adapter instanceof HeaderViewListAdapter) {
mHeaderCount = ((HeaderViewListAdapter) adapter).getHeadersCount();
adapter = ((HeaderViewListAdapter) adapter).getWrappedAdapter();
}
if (adapter instanceof ExpandableListConnector) {
final ExpandableListAdapter expAdapter = ((ExpandableListConnector) adapter)
.getAdapter();
if (expAdapter instanceof SectionIndexer) {
mSectionIndexer = (SectionIndexer) expAdapter;
mListAdapter = adapter;
mSections = mSectionIndexer.getSections();
}
} else if (adapter instanceof SectionIndexer) {
mListAdapter = adapter;
mSectionIndexer = (SectionIndexer) adapter;
mSections = mSectionIndexer.getSections();
} else {
mListAdapter = adapter;
mSections = null;
}
}
public void onSectionsChanged() {
mListAdapter = null;
}
private void scrollTo(float position) {
mScrollCompleted = false;
final int count = mList.getCount();
final Object[] sections = mSections;
final int sectionCount = sections == null ? 0 : sections.length;
int sectionIndex;
if (sections != null && sectionCount > 1) {
final int exactSection = MathUtils.constrain(
(int) (position * sectionCount), 0, sectionCount - 1);
int targetSection = exactSection;
int targetIndex = mSectionIndexer.getPositionForSection(targetSection);
sectionIndex = targetSection;
int nextIndex = count;
int prevIndex = targetIndex;
int prevSection = targetSection;
int nextSection = targetSection + 1;
if (targetSection < sectionCount - 1) {
nextIndex = mSectionIndexer.getPositionForSection(targetSection + 1);
}
if (nextIndex == targetIndex) {
while (targetSection > 0) {
targetSection--;
prevIndex = mSectionIndexer.getPositionForSection(targetSection);
if (prevIndex != targetIndex) {
prevSection = targetSection;
sectionIndex = targetSection;
break;
} else if (targetSection == 0) {
sectionIndex = 0;
break;
}
}
}
int nextNextSection = nextSection + 1;
while (nextNextSection < sectionCount &&
mSectionIndexer.getPositionForSection(nextNextSection) == nextIndex) {
nextNextSection++;
nextSection++;
}
final float prevPosition = (float) prevSection / sectionCount;
final float nextPosition = (float) nextSection / sectionCount;
final float snapThreshold = (count == 0) ? Float.MAX_VALUE : .125f / count;
if (prevSection == exactSection && position - prevPosition < snapThreshold) {
targetIndex = prevIndex;
} else {
targetIndex = prevIndex + (int) ((nextIndex - prevIndex) * (position - prevPosition)
/ (nextPosition - prevPosition));
}
targetIndex = MathUtils.constrain(targetIndex, 0, count - 1);
if (mList instanceof ExpandableListView) {
final ExpandableListView expList = (ExpandableListView) mList;
expList.setSelectionFromTop(expList.getFlatListPosition(
ExpandableListView.getPackedPositionForGroup(targetIndex + mHeaderCount)),
0);
} else if (mList instanceof ListView) {
((ListView) mList).setSelectionFromTop(targetIndex + mHeaderCount, 0);
} else {
mList.setSelection(targetIndex + mHeaderCount);
}
} else {
final int index = MathUtils.constrain((int) (position * count), 0, count - 1);
if (mList instanceof ExpandableListView) {
ExpandableListView expList = (ExpandableListView) mList;
expList.setSelectionFromTop(expList.getFlatListPosition(
ExpandableListView.getPackedPositionForGroup(index + mHeaderCount)), 0);
} else if (mList instanceof ListView) {
((ListView)mList).setSelectionFromTop(index + mHeaderCount, 0);
} else {
mList.setSelection(index + mHeaderCount);
}
sectionIndex = -1;
}
if (mCurrentSection != sectionIndex) {
mCurrentSection = sectionIndex;
final boolean hasPreview = transitionPreviewLayout(sectionIndex);
if (!mShowingPreview && hasPreview) {
transitionToDragging();
} else if (mShowingPreview && !hasPreview) {
transitionToVisible();
}
}
}
private boolean transitionPreviewLayout(int sectionIndex) {
final Object[] sections = mSections;
String text = null;
if (sections != null && sectionIndex >= 0 && sectionIndex < sections.length) {
final Object section = sections[sectionIndex];
if (section != null) {
text = section.toString();
}
}
final Rect bounds = mTempBounds;
final View preview = mPreviewImage;
final TextView showing;
final TextView target;
if (mShowingPrimary) {
showing = mPrimaryText;
target = mSecondaryText;
} else {
showing = mSecondaryText;
target = mPrimaryText;
}
target.setText(text);
measurePreview(target, bounds);
applyLayout(target, bounds);
if (mPreviewAnimation != null) {
mPreviewAnimation.cancel();
}
final Animator showTarget = animateAlpha(target, 1f).setDuration(DURATION_CROSS_FADE);
final Animator hideShowing = animateAlpha(showing, 0f).setDuration(DURATION_CROSS_FADE);
hideShowing.addListener(mSwitchPrimaryListener);
bounds.left -= preview.getPaddingLeft();
bounds.top -= preview.getPaddingTop();
bounds.right += preview.getPaddingRight();
bounds.bottom += preview.getPaddingBottom();
final Animator resizePreview = animateBounds(preview, bounds);
resizePreview.setDuration(DURATION_RESIZE);
mPreviewAnimation = new AnimatorSet();
final AnimatorSet.Builder builder = mPreviewAnimation.play(hideShowing).with(showTarget);
builder.with(resizePreview);
final int previewWidth = preview.getWidth() - preview.getPaddingLeft()
- preview.getPaddingRight();
final int targetWidth = target.getWidth();
if (targetWidth > previewWidth) {
target.setScaleX((float) previewWidth / targetWidth);
final Animator scaleAnim = animateScaleX(target, 1f).setDuration(DURATION_RESIZE);
builder.with(scaleAnim);
} else {
target.setScaleX(1f);
}
final int showingWidth = showing.getWidth();
if (showingWidth > targetWidth) {
final float scale = (float) targetWidth / showingWidth;
final Animator scaleAnim = animateScaleX(showing, scale).setDuration(DURATION_RESIZE);
builder.with(scaleAnim);
}
mPreviewAnimation.start();
return !TextUtils.isEmpty(text);
}
private void setThumbPos(float position) {
final float thumbMiddle = position * mThumbRange + mThumbOffset;
mThumbImage.setTranslationY(thumbMiddle - mThumbImage.getHeight() / 2f);
final View previewImage = mPreviewImage;
final float previewHalfHeight = previewImage.getHeight() / 2f;
final float previewPos;
switch (mOverlayPosition) {
case OVERLAY_AT_THUMB:
previewPos = thumbMiddle;
break;
case OVERLAY_ABOVE_THUMB:
previewPos = thumbMiddle - previewHalfHeight;
break;
case OVERLAY_FLOATING:
default:
previewPos = 0;
break;
}
final Rect container = mContainerRect;
final int top = container.top;
final int bottom = container.bottom;
final float minP = top + previewHalfHeight;
final float maxP = bottom - previewHalfHeight;
final float previewMiddle = MathUtils.constrain(previewPos, minP, maxP);
final float previewTop = previewMiddle - previewHalfHeight;
previewImage.setTranslationY(previewTop);
mPrimaryText.setTranslationY(previewTop);
mSecondaryText.setTranslationY(previewTop);
}
private float getPosFromMotionEvent(float y) {
if (mThumbRange <= 0) {
return 0f;
}
return MathUtils.constrain((y - mThumbOffset) / mThumbRange, 0f, 1f);
}
private float getPosFromItemCount(
int firstVisibleItem, int visibleItemCount, int totalItemCount) {
final SectionIndexer sectionIndexer = mSectionIndexer;
if (sectionIndexer == null || mListAdapter == null) {
getSectionsFromIndexer();
}
if (visibleItemCount == 0 || totalItemCount == 0) {
return 0;
}
final boolean hasSections = sectionIndexer != null && mSections != null
&& mSections.length > 0;
if (!hasSections || !mMatchDragPosition) {
if (visibleItemCount == totalItemCount) {
return 0;
} else {
return (float) firstVisibleItem / (totalItemCount - visibleItemCount);
}
}
firstVisibleItem -= mHeaderCount;
if (firstVisibleItem < 0) {
return 0;
}
totalItemCount -= mHeaderCount;
final View child = mList.getChildAt(0);
final float incrementalPos;
if (child == null || child.getHeight() == 0) {
incrementalPos = 0;
} else {
incrementalPos = (float) (mList.getPaddingTop() - child.getTop()) / child.getHeight();
}
final int section = sectionIndexer.getSectionForPosition(firstVisibleItem);
final int sectionPos = sectionIndexer.getPositionForSection(section);
final int sectionCount = mSections.length;
final int positionsInSection;
if (section < sectionCount - 1) {
final int nextSectionPos;
if (section + 1 < sectionCount) {
nextSectionPos = sectionIndexer.getPositionForSection(section + 1);
} else {
nextSectionPos = totalItemCount - 1;
}
positionsInSection = nextSectionPos - sectionPos;
} else {
positionsInSection = totalItemCount - sectionPos;
}
final float posWithinSection;
if (positionsInSection == 0) {
posWithinSection = 0;
} else {
posWithinSection = (firstVisibleItem + incrementalPos - sectionPos)
/ positionsInSection;
}
float result = (section + posWithinSection) / sectionCount;
if (firstVisibleItem > 0 && firstVisibleItem + visibleItemCount == totalItemCount) {
final View lastChild = mList.getChildAt(visibleItemCount - 1);
final int bottomPadding = mList.getPaddingBottom();
final int maxSize;
final int currentVisibleSize;
if (mList.getClipToPadding()) {
maxSize = lastChild.getHeight();
currentVisibleSize = mList.getHeight() - bottomPadding - lastChild.getTop();
} else {
maxSize = lastChild.getHeight() + bottomPadding;
currentVisibleSize = mList.getHeight() - lastChild.getTop();
}
if (currentVisibleSize > 0 && maxSize > 0) {
result += (1 - result) * ((float) currentVisibleSize / maxSize );
}
}
return result;
}
private void cancelFling() {
final MotionEvent cancelFling = MotionEvent.obtain(
0, 0, MotionEvent.ACTION_CANCEL, 0, 0, 0);
mList.onTouchEvent(cancelFling);
cancelFling.recycle();
}
private void cancelPendingDrag() {
mPendingDrag = -1;
}
private void startPendingDrag() {
mPendingDrag = SystemClock.uptimeMillis() + TAP_TIMEOUT;
}
private void beginDrag() {
mPendingDrag = -1;
setState(STATE_DRAGGING);
if (mListAdapter == null && mList != null) {
getSectionsFromIndexer();
}
if (mList != null) {
mList.requestDisallowInterceptTouchEvent(true);
mList.reportScrollStateChange(OnScrollListener.SCROLL_STATE_TOUCH_SCROLL);
}
cancelFling();
}
public boolean onInterceptTouchEvent(MotionEvent ev) {
if (!isEnabled()) {
return false;
}
switch (ev.getActionMasked()) {
case MotionEvent.ACTION_DOWN:
if (isPointInside(ev.getX(), ev.getY())) {
if (!mList.isInScrollingContainer()) {
return true;
}
mInitialTouchY = ev.getY();
startPendingDrag();
}
break;
case MotionEvent.ACTION_MOVE:
if (!isPointInside(ev.getX(), ev.getY())) {
cancelPendingDrag();
} else if (mPendingDrag >= 0 && mPendingDrag <= SystemClock.uptimeMillis()) {
beginDrag();
final float pos = getPosFromMotionEvent(mInitialTouchY);
scrollTo(pos);
return onTouchEvent(ev);
}
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
cancelPendingDrag();
break;
}
return false;
}
public boolean onInterceptHoverEvent(MotionEvent ev) {
if (!isEnabled()) {
return false;
}
final int actionMasked = ev.getActionMasked();
if ((actionMasked == MotionEvent.ACTION_HOVER_ENTER
|| actionMasked == MotionEvent.ACTION_HOVER_MOVE) && mState == STATE_NONE
&& isPointInside(ev.getX(), ev.getY())) {
setState(STATE_VISIBLE);
postAutoHide();
}
return false;
}
public PointerIcon onResolvePointerIcon(MotionEvent event, int pointerIndex) {
if (mState == STATE_DRAGGING || isPointInside(event.getX(), event.getY())) {
return PointerIcon.getSystemIcon(mList.getContext(), PointerIcon.TYPE_ARROW);
}
return null;
}
public boolean onTouchEvent(MotionEvent me) {
if (!isEnabled()) {
return false;
}
switch (me.getActionMasked()) {
case MotionEvent.ACTION_DOWN: {
if (isPointInside(me.getX(), me.getY())) {
if (!mList.isInScrollingContainer()) {
beginDrag();
return true;
}
}
} break;
case MotionEvent.ACTION_UP: {
if (mPendingDrag >= 0) {
beginDrag();
final float pos = getPosFromMotionEvent(me.getY());
setThumbPos(pos);
scrollTo(pos);
}
if (mState == STATE_DRAGGING) {
if (mList != null) {
mList.requestDisallowInterceptTouchEvent(false);
mList.reportScrollStateChange(OnScrollListener.SCROLL_STATE_IDLE);
}
setState(STATE_VISIBLE);
postAutoHide();
return true;
}
} break;
case MotionEvent.ACTION_MOVE: {
if (mPendingDrag >= 0 && Math.abs(me.getY() - mInitialTouchY) > mScaledTouchSlop) {
beginDrag();
}
if (mState == STATE_DRAGGING) {
final float pos = getPosFromMotionEvent(me.getY());
setThumbPos(pos);
if (mScrollCompleted) {
scrollTo(pos);
}
return true;
}
} break;
case MotionEvent.ACTION_CANCEL: {
cancelPendingDrag();
} break;
}
return false;
}
private boolean isPointInside(float x, float y) {
return isPointInsideX(x) && (mTrackDrawable != null || isPointInsideY(y));
}
private boolean isPointInsideX(float x) {
final float offset = mThumbImage.getTranslationX();
final float left = mThumbImage.getLeft() + offset;
final float right = mThumbImage.getRight() + offset;
final float targetSizeDiff = mMinimumTouchTarget - (right - left);
final float adjust = targetSizeDiff > 0 ? targetSizeDiff : 0;
if (mLayoutFromRight) {
return x >= mThumbImage.getLeft() - adjust;
} else {
return x <= mThumbImage.getRight() + adjust;
}
}
private boolean isPointInsideY(float y) {
final float offset = mThumbImage.getTranslationY();
final float top = mThumbImage.getTop() + offset;
final float bottom = mThumbImage.getBottom() + offset;
final float targetSizeDiff = mMinimumTouchTarget - (bottom - top);
final float adjust = targetSizeDiff > 0 ? targetSizeDiff / 2 : 0;
return y >= (top - adjust) && y <= (bottom + adjust);
}
private static Animator groupAnimatorOfFloat(
Property<View, Float> property, float value, View... views) {
AnimatorSet animSet = new AnimatorSet();
AnimatorSet.Builder builder = null;
for (int i = views.length - 1; i >= 0; i--) {
final Animator anim = ObjectAnimator.ofFloat(views[i], property, value);
if (builder == null) {
builder = animSet.play(anim);
} else {
builder.with(anim);
}
}
return animSet;
}
private static Animator animateScaleX(View v, float target) {
return ObjectAnimator.ofFloat(v, View.SCALE_X, target);
}
private static Animator animateAlpha(View v, float alpha) {
return ObjectAnimator.ofFloat(v, View.ALPHA, alpha);
}
private static Property<View, Integer> LEFT = new IntProperty<View>("left") {
@Override
public void setValue(View object, int value) {
object.setLeft(value);
}
@Override
public Integer get(View object) {
return object.getLeft();
}
};
private static Property<View, Integer> TOP = new IntProperty<View>("top") {
@Override
public void setValue(View object, int value) {
object.setTop(value);
}
@Override
public Integer get(View object) {
return object.getTop();
}
};
private static Property<View, Integer> RIGHT = new IntProperty<View>("right") {
@Override
public void setValue(View object, int value) {
object.setRight(value);
}
@Override
public Integer get(View object) {
return object.getRight();
}
};
private static Property<View, Integer> BOTTOM = new IntProperty<View>("bottom") {
@Override
public void setValue(View object, int value) {
object.setBottom(value);
}
@Override
public Integer get(View object) {
return object.getBottom();
}
};
private static Animator animateBounds(View v, Rect bounds) {
final PropertyValuesHolder left = PropertyValuesHolder.ofInt(LEFT, bounds.left);
final PropertyValuesHolder top = PropertyValuesHolder.ofInt(TOP, bounds.top);
final PropertyValuesHolder right = PropertyValuesHolder.ofInt(RIGHT, bounds.right);
final PropertyValuesHolder bottom = PropertyValuesHolder.ofInt(BOTTOM, bounds.bottom);
return ObjectAnimator.ofPropertyValuesHolder(v, left, top, right, bottom);
}
}