2016-05-16 09:54:06 -07:00
|
|
|
package com.android.launcher3.pageindicators;
|
|
|
|
|
|
|
|
|
|
import android.animation.Animator;
|
|
|
|
|
import android.animation.AnimatorListenerAdapter;
|
|
|
|
|
import android.animation.ObjectAnimator;
|
|
|
|
|
import android.animation.ValueAnimator;
|
|
|
|
|
import android.content.Context;
|
|
|
|
|
import android.graphics.Canvas;
|
|
|
|
|
import android.graphics.Color;
|
|
|
|
|
import android.graphics.Paint;
|
|
|
|
|
import android.os.Handler;
|
|
|
|
|
import android.os.Looper;
|
|
|
|
|
import android.support.v4.graphics.ColorUtils;
|
|
|
|
|
import android.util.AttributeSet;
|
|
|
|
|
import android.util.Log;
|
|
|
|
|
import android.util.Property;
|
|
|
|
|
import android.view.ViewConfiguration;
|
|
|
|
|
|
|
|
|
|
import com.android.launcher3.Utilities;
|
|
|
|
|
import com.android.launcher3.dynamicui.ExtractedColors;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* A PageIndicator that briefly shows a fraction of a line when moving between pages.
|
|
|
|
|
*
|
|
|
|
|
* The fraction is 1 / number of pages and the position is based on the progress of the page scroll.
|
|
|
|
|
*/
|
2016-05-20 12:48:10 -07:00
|
|
|
public class PageIndicatorLine extends PageIndicator {
|
2016-05-16 09:54:06 -07:00
|
|
|
private static final String TAG = "PageIndicatorLine";
|
|
|
|
|
|
2016-05-31 16:55:01 -07:00
|
|
|
private static final int LINE_ANIMATE_DURATION = ViewConfiguration.getScrollBarFadeDuration();
|
2016-05-16 09:54:06 -07:00
|
|
|
private static final int LINE_FADE_DELAY = ViewConfiguration.getScrollDefaultDelay();
|
|
|
|
|
public static final int WHITE_ALPHA = (int) (0.70f * 255);
|
|
|
|
|
public static final int BLACK_ALPHA = (int) (0.65f * 255);
|
|
|
|
|
|
2016-05-31 16:55:01 -07:00
|
|
|
private static final int LINE_ALPHA_ANIMATOR_INDEX = 0;
|
|
|
|
|
private static final int NUM_PAGES_ANIMATOR_INDEX = 1;
|
|
|
|
|
private static final int TOTAL_SCROLL_ANIMATOR_INDEX = 2;
|
|
|
|
|
|
|
|
|
|
private ValueAnimator[] mAnimators = new ValueAnimator[3];
|
|
|
|
|
|
2016-05-26 13:00:02 -07:00
|
|
|
private final Handler mDelayedLineFadeHandler = new Handler(Looper.getMainLooper());
|
|
|
|
|
|
|
|
|
|
private boolean mShouldAutoHide = true;
|
2016-05-16 09:54:06 -07:00
|
|
|
|
2016-05-31 16:55:01 -07:00
|
|
|
// The alpha of the line when it is showing.
|
|
|
|
|
private int mActiveAlpha = 0;
|
|
|
|
|
// The alpha that the line is being animated to or already at (either 0 or mActiveAlpha).
|
|
|
|
|
private int mToAlpha;
|
|
|
|
|
// A float value representing the number of pages, to allow for an animation when it changes.
|
|
|
|
|
private float mNumPagesFloat;
|
|
|
|
|
private int mCurrentScroll;
|
|
|
|
|
private int mTotalScroll;
|
2016-05-16 09:54:06 -07:00
|
|
|
private Paint mLinePaint;
|
|
|
|
|
|
2016-05-18 14:12:02 -07:00
|
|
|
private static final Property<PageIndicatorLine, Integer> PAINT_ALPHA
|
|
|
|
|
= new Property<PageIndicatorLine, Integer>(Integer.class, "paint_alpha") {
|
2016-05-16 09:54:06 -07:00
|
|
|
@Override
|
2016-05-18 14:12:02 -07:00
|
|
|
public Integer get(PageIndicatorLine obj) {
|
|
|
|
|
return obj.mLinePaint.getAlpha();
|
2016-05-16 09:54:06 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2016-05-18 14:12:02 -07:00
|
|
|
public void set(PageIndicatorLine obj, Integer alpha) {
|
|
|
|
|
obj.mLinePaint.setAlpha(alpha);
|
|
|
|
|
obj.invalidate();
|
2016-05-16 09:54:06 -07:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2016-05-31 16:55:01 -07:00
|
|
|
private static final Property<PageIndicatorLine, Float> NUM_PAGES
|
|
|
|
|
= new Property<PageIndicatorLine, Float>(Float.class, "num_pages") {
|
|
|
|
|
@Override
|
|
|
|
|
public Float get(PageIndicatorLine obj) {
|
|
|
|
|
return obj.mNumPagesFloat;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void set(PageIndicatorLine obj, Float numPages) {
|
|
|
|
|
obj.mNumPagesFloat = numPages;
|
|
|
|
|
obj.invalidate();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
private static final Property<PageIndicatorLine, Integer> TOTAL_SCROLL
|
|
|
|
|
= new Property<PageIndicatorLine, Integer>(Integer.class, "total_scroll") {
|
|
|
|
|
@Override
|
|
|
|
|
public Integer get(PageIndicatorLine obj) {
|
|
|
|
|
return obj.mTotalScroll;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void set(PageIndicatorLine obj, Integer totalScroll) {
|
|
|
|
|
obj.mTotalScroll = totalScroll;
|
|
|
|
|
obj.invalidate();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2016-05-16 09:54:06 -07:00
|
|
|
private Runnable mHideLineRunnable = new Runnable() {
|
|
|
|
|
@Override
|
|
|
|
|
public void run() {
|
|
|
|
|
animateLineToAlpha(0);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
public PageIndicatorLine(Context context) {
|
|
|
|
|
this(context, null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public PageIndicatorLine(Context context, AttributeSet attrs) {
|
|
|
|
|
this(context, attrs, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public PageIndicatorLine(Context context, AttributeSet attrs, int defStyle) {
|
|
|
|
|
super(context, attrs, defStyle);
|
|
|
|
|
mLinePaint = new Paint();
|
|
|
|
|
mLinePaint.setAlpha(0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected void onDraw(Canvas canvas) {
|
2016-05-31 16:55:01 -07:00
|
|
|
if (mTotalScroll == 0 || mNumPagesFloat == 0) {
|
2016-05-16 09:54:06 -07:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-31 16:55:01 -07:00
|
|
|
// Compute and draw line rect.
|
|
|
|
|
float progress = Utilities.boundToRange(((float) mCurrentScroll) / mTotalScroll, 0f, 1f);
|
2016-05-16 09:54:06 -07:00
|
|
|
int availableWidth = canvas.getWidth();
|
2016-05-31 16:55:01 -07:00
|
|
|
int lineWidth = (int) (availableWidth / mNumPagesFloat);
|
|
|
|
|
int lineLeft = (int) (progress * (availableWidth - lineWidth));
|
2016-05-16 09:54:06 -07:00
|
|
|
int lineRight = lineLeft + lineWidth;
|
|
|
|
|
canvas.drawRect(lineLeft, 0, lineRight, canvas.getHeight(), mLinePaint);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2016-05-18 14:12:02 -07:00
|
|
|
public void setScroll(int currentScroll, int totalScroll) {
|
2016-05-16 09:54:06 -07:00
|
|
|
if (getAlpha() == 0) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2016-05-31 16:55:01 -07:00
|
|
|
animateLineToAlpha(mActiveAlpha);
|
|
|
|
|
|
|
|
|
|
mCurrentScroll = currentScroll;
|
|
|
|
|
if (mTotalScroll == 0) {
|
|
|
|
|
mTotalScroll = totalScroll;
|
|
|
|
|
} else if (mTotalScroll != totalScroll) {
|
|
|
|
|
animateToTotalScroll(totalScroll);
|
|
|
|
|
} else {
|
|
|
|
|
invalidate();
|
|
|
|
|
}
|
2016-05-16 09:54:06 -07:00
|
|
|
|
2016-05-26 13:00:02 -07:00
|
|
|
if (mShouldAutoHide) {
|
|
|
|
|
hideAfterDelay();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void hideAfterDelay() {
|
|
|
|
|
mDelayedLineFadeHandler.removeCallbacksAndMessages(null);
|
|
|
|
|
mDelayedLineFadeHandler.postDelayed(mHideLineRunnable, LINE_FADE_DELAY);
|
2016-05-16 09:54:06 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void setActiveMarker(int activePage) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2016-05-20 12:48:10 -07:00
|
|
|
protected void onPageCountChanged() {
|
2016-05-31 16:55:01 -07:00
|
|
|
if (Float.compare(mNumPages, mNumPagesFloat) != 0) {
|
|
|
|
|
animateToNumPages(mNumPages);
|
|
|
|
|
}
|
2016-05-16 09:54:06 -07:00
|
|
|
}
|
|
|
|
|
|
2016-05-26 13:00:02 -07:00
|
|
|
public void setShouldAutoHide(boolean shouldAutoHide) {
|
|
|
|
|
mShouldAutoHide = shouldAutoHide;
|
|
|
|
|
if (shouldAutoHide && mLinePaint.getAlpha() > 0) {
|
|
|
|
|
hideAfterDelay();
|
|
|
|
|
} else if (!shouldAutoHide) {
|
|
|
|
|
mDelayedLineFadeHandler.removeCallbacksAndMessages(null);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-16 09:54:06 -07:00
|
|
|
/**
|
|
|
|
|
* The line's color will be:
|
|
|
|
|
* - mostly opaque white if the hotseat is white (ignoring alpha)
|
|
|
|
|
* - mostly opaque black if the hotseat is black (ignoring alpha)
|
|
|
|
|
*/
|
|
|
|
|
public void updateColor(ExtractedColors extractedColors) {
|
|
|
|
|
int originalLineAlpha = mLinePaint.getAlpha();
|
|
|
|
|
int color = extractedColors.getColor(ExtractedColors.HOTSEAT_INDEX, Color.TRANSPARENT);
|
|
|
|
|
if (color != Color.TRANSPARENT) {
|
|
|
|
|
color = ColorUtils.setAlphaComponent(color, 255);
|
|
|
|
|
if (color == Color.BLACK) {
|
2016-05-31 16:55:01 -07:00
|
|
|
mActiveAlpha = BLACK_ALPHA;
|
2016-05-16 09:54:06 -07:00
|
|
|
} else if (color == Color.WHITE) {
|
2016-05-31 16:55:01 -07:00
|
|
|
mActiveAlpha = WHITE_ALPHA;
|
2016-05-16 09:54:06 -07:00
|
|
|
} else {
|
|
|
|
|
Log.e(TAG, "Setting workspace page indicators to an unsupported color: #"
|
|
|
|
|
+ Integer.toHexString(color));
|
|
|
|
|
}
|
|
|
|
|
mLinePaint.setColor(color);
|
|
|
|
|
mLinePaint.setAlpha(originalLineAlpha);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void animateLineToAlpha(int alpha) {
|
2016-05-31 16:55:01 -07:00
|
|
|
if (alpha == mToAlpha) {
|
|
|
|
|
// Ignore the new animation if it is going to the same alpha as the current animation.
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
mToAlpha = alpha;
|
|
|
|
|
setupAndRunAnimation(ObjectAnimator.ofInt(this, PAINT_ALPHA, alpha),
|
|
|
|
|
LINE_ALPHA_ANIMATOR_INDEX);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void animateToNumPages(int numPages) {
|
|
|
|
|
setupAndRunAnimation(ObjectAnimator.ofFloat(this, NUM_PAGES, numPages),
|
|
|
|
|
NUM_PAGES_ANIMATOR_INDEX);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void animateToTotalScroll(int totalScroll) {
|
|
|
|
|
setupAndRunAnimation(ObjectAnimator.ofInt(this, TOTAL_SCROLL, totalScroll),
|
|
|
|
|
TOTAL_SCROLL_ANIMATOR_INDEX);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Starts the given animator and stores it in the provided index in {@link #mAnimators} until
|
|
|
|
|
* the animation ends.
|
|
|
|
|
*
|
|
|
|
|
* If an animator is already at the index (i.e. it is already playing), it is canceled and
|
|
|
|
|
* replaced with the new animator.
|
|
|
|
|
*/
|
|
|
|
|
private void setupAndRunAnimation(ValueAnimator animator, final int animatorIndex) {
|
|
|
|
|
if (mAnimators[animatorIndex] != null) {
|
|
|
|
|
mAnimators[animatorIndex].cancel();
|
2016-05-16 09:54:06 -07:00
|
|
|
}
|
2016-05-31 16:55:01 -07:00
|
|
|
mAnimators[animatorIndex] = animator;
|
|
|
|
|
mAnimators[animatorIndex].addListener(new AnimatorListenerAdapter() {
|
2016-05-16 09:54:06 -07:00
|
|
|
@Override
|
|
|
|
|
public void onAnimationEnd(Animator animation) {
|
2016-05-31 16:55:01 -07:00
|
|
|
mAnimators[animatorIndex] = null;
|
2016-05-16 09:54:06 -07:00
|
|
|
}
|
|
|
|
|
});
|
2016-05-31 16:55:01 -07:00
|
|
|
mAnimators[animatorIndex].setDuration(LINE_ANIMATE_DURATION);
|
|
|
|
|
mAnimators[animatorIndex].start();
|
2016-05-16 09:54:06 -07:00
|
|
|
}
|
|
|
|
|
}
|