blob: b9b044db9d2ae59e7fe9de544fc0d1d246e0177b [file] [log] [blame]
Winson Chungb1777442015-06-16 13:35:04 -07001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package com.android.launcher3;
17
18import android.animation.Animator;
19import android.animation.ObjectAnimator;
20import android.content.res.Resources;
Peter Schiller1d62b8a2016-06-29 09:46:43 -070021import android.graphics.Bitmap;
Winson Chungb1777442015-06-16 13:35:04 -070022import android.graphics.Canvas;
23import android.graphics.Color;
24import android.graphics.Paint;
25import android.graphics.Rect;
26import android.graphics.drawable.Drawable;
27
Sunny Goyal10629b02016-09-01 12:50:11 -070028import com.android.launcher3.graphics.HolographicOutlineHelper;
29
Winson Chungb1777442015-06-16 13:35:04 -070030/**
31 * The fast scroller popup that shows the section name the list will jump to.
32 */
33public class BaseRecyclerViewFastScrollPopup {
34
35 private static final float FAST_SCROLL_OVERLAY_Y_OFFSET_FACTOR = 1.5f;
36
Peter Schiller62d70232016-07-28 16:58:06 -070037 private static final int SHADOW_INSET = 3;
38 private static final int SHADOW_SHIFT_Y = 2;
39 private static final float SHADOW_ALPHA_MULTIPLIER = 0.67f;
Peter Schiller1d62b8a2016-06-29 09:46:43 -070040
Winson Chungb1777442015-06-16 13:35:04 -070041 private Resources mRes;
42 private BaseRecyclerView mRv;
43
Peter Schiller1d62b8a2016-06-29 09:46:43 -070044 private Bitmap mShadow;
45 private Paint mShadowPaint;
46
Winson Chungb1777442015-06-16 13:35:04 -070047 private Drawable mBg;
48 // The absolute bounds of the fast scroller bg
49 private Rect mBgBounds = new Rect();
50 private int mBgOriginalSize;
51 private Rect mInvalidateRect = new Rect();
52 private Rect mTmpRect = new Rect();
53
54 private String mSectionName;
55 private Paint mTextPaint;
56 private Rect mTextBounds = new Rect();
57 private float mAlpha;
58
59 private Animator mAlphaAnimator;
60 private boolean mVisible;
61
62 public BaseRecyclerViewFastScrollPopup(BaseRecyclerView rv, Resources res) {
63 mRes = res;
64 mRv = rv;
Peter Schiller1d62b8a2016-06-29 09:46:43 -070065
Winson Chungb1777442015-06-16 13:35:04 -070066 mBgOriginalSize = res.getDimensionPixelSize(R.dimen.container_fastscroll_popup_size);
Andrew Sappersteinabef55a2016-06-19 12:49:00 -070067 mBg = rv.getContext().getDrawable(R.drawable.container_fastscroll_popup_bg);
Winson Chungb1777442015-06-16 13:35:04 -070068 mBg.setBounds(0, 0, mBgOriginalSize, mBgOriginalSize);
Peter Schiller1d62b8a2016-06-29 09:46:43 -070069
Winson Chungb1777442015-06-16 13:35:04 -070070 mTextPaint = new Paint();
71 mTextPaint.setColor(Color.WHITE);
72 mTextPaint.setAntiAlias(true);
73 mTextPaint.setTextSize(res.getDimensionPixelSize(R.dimen.container_fastscroll_popup_text_size));
Peter Schiller1d62b8a2016-06-29 09:46:43 -070074
75 mShadowPaint = new Paint();
76 mShadowPaint.setAntiAlias(true);
77 mShadowPaint.setFilterBitmap(true);
78 mShadowPaint.setDither(true);
Winson Chungb1777442015-06-16 13:35:04 -070079 }
80
81 /**
82 * Sets the section name.
83 */
84 public void setSectionName(String sectionName) {
85 if (!sectionName.equals(mSectionName)) {
86 mSectionName = sectionName;
87 mTextPaint.getTextBounds(sectionName, 0, sectionName.length(), mTextBounds);
88 // Update the width to use measureText since that is more accurate
89 mTextBounds.right = (int) (mTextBounds.left + mTextPaint.measureText(sectionName));
90 }
91 }
92
93 /**
94 * Updates the bounds for the fast scroller.
Peter Schiller1d62b8a2016-06-29 09:46:43 -070095 *
Winson Chungb1777442015-06-16 13:35:04 -070096 * @return the invalidation rect for this update.
97 */
Winsonc0880492015-08-21 11:16:27 -070098 public Rect updateFastScrollerBounds(int lastTouchY) {
Winson Chungb1777442015-06-16 13:35:04 -070099 mInvalidateRect.set(mBgBounds);
100
101 if (isVisible()) {
102 // Calculate the dimensions and position of the fast scroller popup
Winsonc0880492015-08-21 11:16:27 -0700103 int edgePadding = mRv.getMaxScrollbarWidth();
Winson Chungb1777442015-06-16 13:35:04 -0700104 int bgPadding = (mBgOriginalSize - mTextBounds.height()) / 2;
105 int bgHeight = mBgOriginalSize;
106 int bgWidth = Math.max(mBgOriginalSize, mTextBounds.width() + (2 * bgPadding));
107 if (Utilities.isRtl(mRes)) {
Winsonc0880492015-08-21 11:16:27 -0700108 mBgBounds.left = mRv.getBackgroundPadding().left + (2 * mRv.getMaxScrollbarWidth());
Winson Chungb1777442015-06-16 13:35:04 -0700109 mBgBounds.right = mBgBounds.left + bgWidth;
110 } else {
Winsonc0880492015-08-21 11:16:27 -0700111 mBgBounds.right = mRv.getWidth() - mRv.getBackgroundPadding().right -
112 (2 * mRv.getMaxScrollbarWidth());
Winson Chungb1777442015-06-16 13:35:04 -0700113 mBgBounds.left = mBgBounds.right - bgWidth;
114 }
115 mBgBounds.top = lastTouchY - (int) (FAST_SCROLL_OVERLAY_Y_OFFSET_FACTOR * bgHeight);
116 mBgBounds.top = Math.max(edgePadding,
Hyunyoung Song4ebc3d02016-08-05 10:59:17 -0700117 Math.min(mBgBounds.top, mRv.getVisibleHeight() - edgePadding - bgHeight));
Winson Chungb1777442015-06-16 13:35:04 -0700118 mBgBounds.bottom = mBgBounds.top + bgHeight;
Peter Schiller1d62b8a2016-06-29 09:46:43 -0700119
120 // Generate a bitmap for a shadow matching these bounds
Sunny Goyal10629b02016-09-01 12:50:11 -0700121 mShadow = HolographicOutlineHelper.getInstance(
Peter Schiller1d62b8a2016-06-29 09:46:43 -0700122 mRv.getContext()).createMediumDropShadow(mBg, false /* shouldCache */);
Winson Chungb1777442015-06-16 13:35:04 -0700123 } else {
Peter Schillerf9507122016-06-29 13:23:24 -0700124 mShadow = null;
Winson Chungb1777442015-06-16 13:35:04 -0700125 mBgBounds.setEmpty();
126 }
127
128 // Combine the old and new fast scroller bounds to create the full invalidate rect
129 mInvalidateRect.union(mBgBounds);
130 return mInvalidateRect;
131 }
132
133 /**
134 * Animates the visibility of the fast scroller popup.
135 */
136 public void animateVisibility(boolean visible) {
137 if (mVisible != visible) {
138 mVisible = visible;
139 if (mAlphaAnimator != null) {
140 mAlphaAnimator.cancel();
141 }
142 mAlphaAnimator = ObjectAnimator.ofFloat(this, "alpha", visible ? 1f : 0f);
143 mAlphaAnimator.setDuration(visible ? 200 : 150);
144 mAlphaAnimator.start();
145 }
146 }
147
148 // Setter/getter for the popup alpha for animations
149 public void setAlpha(float alpha) {
150 mAlpha = alpha;
151 mRv.invalidate(mBgBounds);
152 }
153
154 public float getAlpha() {
155 return mAlpha;
156 }
157
158 public int getHeight() {
159 return mBgOriginalSize;
160 }
161
162 public void draw(Canvas c) {
163 if (isVisible()) {
Peter Schiller1d62b8a2016-06-29 09:46:43 -0700164 // Determine the alpha and prepare the canvas
165 final int alpha = (int) (mAlpha * 255);
Winson Chungb1777442015-06-16 13:35:04 -0700166 int restoreCount = c.save(Canvas.MATRIX_SAVE_FLAG);
167 c.translate(mBgBounds.left, mBgBounds.top);
168 mTmpRect.set(mBgBounds);
169 mTmpRect.offsetTo(0, 0);
Peter Schiller1d62b8a2016-06-29 09:46:43 -0700170
171 // Expand the rect (with a negative inset), translate it, and draw the shadow
172 if (mShadow != null) {
173 mTmpRect.inset(-SHADOW_INSET * 2, -SHADOW_INSET * 2);
174 mTmpRect.offset(0, SHADOW_SHIFT_Y);
175 mShadowPaint.setAlpha((int) (alpha * SHADOW_ALPHA_MULTIPLIER));
176 c.drawBitmap(mShadow, null, mTmpRect, mShadowPaint);
177 mTmpRect.inset(SHADOW_INSET * 2, SHADOW_INSET * 2);
178 mTmpRect.offset(0, -SHADOW_SHIFT_Y);
179 }
180
181 // Draw the background
Winson Chungb1777442015-06-16 13:35:04 -0700182 mBg.setBounds(mTmpRect);
Peter Schiller1d62b8a2016-06-29 09:46:43 -0700183 mBg.setAlpha(alpha);
Winson Chungb1777442015-06-16 13:35:04 -0700184 mBg.draw(c);
Peter Schiller1d62b8a2016-06-29 09:46:43 -0700185
186 // Draw the text
187 mTextPaint.setAlpha(alpha);
Winson Chungb1777442015-06-16 13:35:04 -0700188 c.drawText(mSectionName, (mBgBounds.width() - mTextBounds.width()) / 2,
Peter Schiller1d62b8a2016-06-29 09:46:43 -0700189 mBgBounds.height() - (mBgBounds.height() / 2) - mTextBounds.exactCenterY(),
Winson Chungb1777442015-06-16 13:35:04 -0700190 mTextPaint);
191 c.restoreToCount(restoreCount);
192 }
193 }
194
195 public boolean isVisible() {
196 return (mAlpha > 0f) && (mSectionName != null);
197 }
198}