summaryrefslogtreecommitdiffstats
path: root/src/com/android/launcher3/pageindicators/PageIndicatorLine.java
blob: e48168616f5d9196acce274cb36c70808a8b9d1f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
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.View;
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.
 */
public class PageIndicatorLine extends View implements PageIndicator {
    private static final String TAG = "PageIndicatorLine";

    private static final int LINE_FADE_DURATION = ViewConfiguration.getScrollBarFadeDuration();
    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);

    private final Handler mHandler = new Handler(Looper.getMainLooper());

    private ValueAnimator mLineAlphaAnimator;
    private int mAlpha = 0;
    private float mProgress = 0f;
    private int mNumPages = 1;
    private Paint mLinePaint;

    private static final Property<PageIndicatorLine, Integer> PAINT_ALPHA
            = new Property<PageIndicatorLine, Integer>(Integer.class, "paint_alpha") {
        @Override
        public Integer get(PageIndicatorLine obj) {
            return obj.mLinePaint.getAlpha();
        }

        @Override
        public void set(PageIndicatorLine obj, Integer alpha) {
            obj.mLinePaint.setAlpha(alpha);
            obj.invalidate();
        }
    };

    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) {
        super.onDraw(canvas);

        if (mNumPages == 0) {
            return;
        }

        int availableWidth = canvas.getWidth();
        int lineWidth = availableWidth / mNumPages;
        int lineLeft = (int) (mProgress * (availableWidth - lineWidth));
        int lineRight = lineLeft + lineWidth;
        canvas.drawRect(lineLeft, 0, lineRight, canvas.getHeight(), mLinePaint);
    }

    @Override
    public View getView() {
        return this;
    }

    @Override
    public void setScroll(int currentScroll, int totalScroll) {
        if (getAlpha() == 0) {
            return;
        }
        animateLineToAlpha(mAlpha);
        mProgress = Utilities.boundToRange(((float) currentScroll) / totalScroll, 0f, 1f);;
        invalidate();

        // Hide after a brief period.
        mHandler.removeCallbacksAndMessages(null);
        mHandler.postDelayed(mHideLineRunnable, LINE_FADE_DELAY);
    }

    @Override
    public void setActiveMarker(int activePage) {
    }

    @Override
    public void addMarker() {
        mNumPages++;
    }

    @Override
    public void removeMarker() {
        mNumPages--;
    }

    @Override
    public void setMarkersCount(int numMarkers) {
        mNumPages = numMarkers;
    }

    /**
     * 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) {
                mAlpha = BLACK_ALPHA;
            } else if (color == Color.WHITE) {
                mAlpha = WHITE_ALPHA;
            } 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) {
        if (mLineAlphaAnimator != null) {
            // An animation is already running, so ignore the new animation request unless we are
            // trying to hide the line, in which case we always allow the animation.
            if (alpha != 0) {
                return;
            }
            mLineAlphaAnimator.cancel();
        }
        mLineAlphaAnimator = ObjectAnimator.ofInt(this, PAINT_ALPHA, alpha);
        mLineAlphaAnimator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                mLineAlphaAnimator = null;
            }
        });
        mLineAlphaAnimator.setDuration(LINE_FADE_DURATION);
        mLineAlphaAnimator.start();
    }
}