summaryrefslogtreecommitdiffstats
path: root/src/com/android/launcher3/BaseRecyclerViewFastScrollPopup.java
blob: b9e627775680d5d7a21fd5218a9d539f3cb66ccc (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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*
 * 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;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;

/**
 * The fast scroller popup that shows the section name the list will jump to.
 */
public class BaseRecyclerViewFastScrollPopup {

    private static final float FAST_SCROLL_OVERLAY_Y_OFFSET_FACTOR = 1.5f;

    private static final int SHADOW_INSET = 3;
    private static final int SHADOW_SHIFT_Y = 2;
    private static final float SHADOW_ALPHA_MULTIPLIER = 0.67f;

    private Resources mRes;
    private BaseRecyclerView mRv;

    private Bitmap mShadow;
    private Paint mShadowPaint;

    private Drawable mBg;
    // The absolute bounds of the fast scroller bg
    private Rect mBgBounds = new Rect();
    private int mBgOriginalSize;
    private Rect mInvalidateRect = new Rect();
    private Rect mTmpRect = new Rect();

    private String mSectionName;
    private Paint mTextPaint;
    private Rect mTextBounds = new Rect();
    private float mAlpha;

    private Animator mAlphaAnimator;
    private boolean mVisible;

    public BaseRecyclerViewFastScrollPopup(BaseRecyclerView rv, Resources res) {
        mRes = res;
        mRv = rv;

        mBgOriginalSize = res.getDimensionPixelSize(R.dimen.container_fastscroll_popup_size);
        mBg = rv.getContext().getDrawable(R.drawable.container_fastscroll_popup_bg);
        mBg.setBounds(0, 0, mBgOriginalSize, mBgOriginalSize);

        mTextPaint = new Paint();
        mTextPaint.setColor(Color.WHITE);
        mTextPaint.setAntiAlias(true);
        mTextPaint.setTextSize(res.getDimensionPixelSize(R.dimen.container_fastscroll_popup_text_size));

        mShadowPaint = new Paint();
        mShadowPaint.setAntiAlias(true);
        mShadowPaint.setFilterBitmap(true);
        mShadowPaint.setDither(true);
    }

    /**
     * Sets the section name.
     */
    public void setSectionName(String sectionName) {
        if (!sectionName.equals(mSectionName)) {
            mSectionName = sectionName;
            mTextPaint.getTextBounds(sectionName, 0, sectionName.length(), mTextBounds);
            // Update the width to use measureText since that is more accurate
            mTextBounds.right = (int) (mTextBounds.left + mTextPaint.measureText(sectionName));
        }
    }

    /**
     * Updates the bounds for the fast scroller.
     *
     * @return the invalidation rect for this update.
     */
    public Rect updateFastScrollerBounds(int lastTouchY) {
        mInvalidateRect.set(mBgBounds);

        if (isVisible()) {
            // Calculate the dimensions and position of the fast scroller popup
            int edgePadding = mRv.getMaxScrollbarWidth();
            int bgPadding = (mBgOriginalSize - mTextBounds.height()) / 2;
            int bgHeight = mBgOriginalSize;
            int bgWidth = Math.max(mBgOriginalSize, mTextBounds.width() + (2 * bgPadding));
            if (Utilities.isRtl(mRes)) {
                mBgBounds.left = mRv.getBackgroundPadding().left + (2 * mRv.getMaxScrollbarWidth());
                mBgBounds.right = mBgBounds.left + bgWidth;
            } else {
                mBgBounds.right = mRv.getWidth() - mRv.getBackgroundPadding().right -
                        (2 * mRv.getMaxScrollbarWidth());
                mBgBounds.left = mBgBounds.right - bgWidth;
            }
            mBgBounds.top = lastTouchY - (int) (FAST_SCROLL_OVERLAY_Y_OFFSET_FACTOR * bgHeight);
            mBgBounds.top = Math.max(edgePadding,
                    Math.min(mBgBounds.top, mRv.getVisibleHeight() - edgePadding - bgHeight));
            mBgBounds.bottom = mBgBounds.top + bgHeight;

            // Generate a bitmap for a shadow matching these bounds
            mShadow = HolographicOutlineHelper.obtain(
                    mRv.getContext()).createMediumDropShadow(mBg, false /* shouldCache */);
        } else {
            mShadow = null;
            mBgBounds.setEmpty();
        }

        // Combine the old and new fast scroller bounds to create the full invalidate rect
        mInvalidateRect.union(mBgBounds);
        return mInvalidateRect;
    }

    /**
     * Animates the visibility of the fast scroller popup.
     */
    public void animateVisibility(boolean visible) {
        if (mVisible != visible) {
            mVisible = visible;
            if (mAlphaAnimator != null) {
                mAlphaAnimator.cancel();
            }
            mAlphaAnimator = ObjectAnimator.ofFloat(this, "alpha", visible ? 1f : 0f);
            mAlphaAnimator.setDuration(visible ? 200 : 150);
            mAlphaAnimator.start();
        }
    }

    // Setter/getter for the popup alpha for animations
    public void setAlpha(float alpha) {
        mAlpha = alpha;
        mRv.invalidate(mBgBounds);
    }

    public float getAlpha() {
        return mAlpha;
    }

    public int getHeight() {
        return mBgOriginalSize;
    }

    public void draw(Canvas c) {
        if (isVisible()) {
            // Determine the alpha and prepare the canvas
            final int alpha = (int) (mAlpha * 255);
            int restoreCount = c.save(Canvas.MATRIX_SAVE_FLAG);
            c.translate(mBgBounds.left, mBgBounds.top);
            mTmpRect.set(mBgBounds);
            mTmpRect.offsetTo(0, 0);

            // Expand the rect (with a negative inset), translate it, and draw the shadow
            if (mShadow != null) {
                mTmpRect.inset(-SHADOW_INSET * 2, -SHADOW_INSET * 2);
                mTmpRect.offset(0, SHADOW_SHIFT_Y);
                mShadowPaint.setAlpha((int) (alpha * SHADOW_ALPHA_MULTIPLIER));
                c.drawBitmap(mShadow, null, mTmpRect, mShadowPaint);
                mTmpRect.inset(SHADOW_INSET * 2, SHADOW_INSET * 2);
                mTmpRect.offset(0, -SHADOW_SHIFT_Y);
            }

            // Draw the background
            mBg.setBounds(mTmpRect);
            mBg.setAlpha(alpha);
            mBg.draw(c);

            // Draw the text
            mTextPaint.setAlpha(alpha);
            c.drawText(mSectionName, (mBgBounds.width() - mTextBounds.width()) / 2,
                    mBgBounds.height() - (mBgBounds.height() / 2) - mTextBounds.exactCenterY(),
                    mTextPaint);
            c.restoreToCount(restoreCount);
        }
    }

    public boolean isVisible() {
        return (mAlpha > 0f) && (mSectionName != null);
    }
}