BubbleTextView.java revision c9c548eb1093be6106279a792df5ba8bb36b6326
1/*
2 * Copyright (C) 2008 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 */
16
17package com.android.launcher2;
18
19import com.android.launcher.R;
20
21import android.content.Context;
22import android.content.res.Resources;
23import android.graphics.Bitmap;
24import android.graphics.Canvas;
25import android.graphics.Color;
26import android.graphics.Paint;
27import android.graphics.Rect;
28import android.graphics.Region;
29import android.graphics.Region.Op;
30import android.graphics.drawable.Drawable;
31import android.util.AttributeSet;
32import android.view.MotionEvent;
33import android.view.View;
34
35/**
36 * TextView that draws a bubble behind the text. We cannot use a LineBackgroundSpan
37 * because we want to make the bubble taller than the text and TextView's clip is
38 * too aggressive.
39 */
40public class BubbleTextView extends CacheableTextView implements VisibilityChangedBroadcaster {
41    static final float CORNER_RADIUS = 4.0f;
42    static final float SHADOW_LARGE_RADIUS = 4.0f;
43    static final float SHADOW_SMALL_RADIUS = 1.75f;
44    static final float SHADOW_Y_OFFSET = 2.0f;
45    static final int SHADOW_LARGE_COLOUR = 0xCC000000;
46    static final int SHADOW_SMALL_COLOUR = 0xBB000000;
47    static final float PADDING_H = 8.0f;
48    static final float PADDING_V = 3.0f;
49
50    private Paint mPaint;
51    private float mBubbleColorAlpha;
52    private int mPrevAlpha = -1;
53
54    private final HolographicOutlineHelper mOutlineHelper = new HolographicOutlineHelper();
55    private final Canvas mTempCanvas = new Canvas();
56    private final Rect mTempRect = new Rect();
57    private final Paint mTempPaint = new Paint();
58    private boolean mDidInvalidateForPressedState;
59    private Bitmap mPressedOrFocusedBackground;
60    private int mFocusedOutlineColor;
61    private int mFocusedGlowColor;
62    private int mPressedOutlineColor;
63    private int mPressedGlowColor;
64
65    private boolean mBackgroundSizeChanged;
66    private Drawable mBackground;
67
68    private VisibilityChangedListener mOnVisibilityChangedListener;
69
70    public BubbleTextView(Context context) {
71        super(context);
72        init();
73    }
74
75    public BubbleTextView(Context context, AttributeSet attrs) {
76        super(context, attrs);
77        init();
78    }
79
80    public BubbleTextView(Context context, AttributeSet attrs, int defStyle) {
81        super(context, attrs, defStyle);
82        init();
83    }
84
85    private void init() {
86        mBackground = getBackground();
87        setFocusable(true);
88        setBackgroundDrawable(null);
89
90        final Resources res = getContext().getResources();
91        int bubbleColor = res.getColor(R.color.bubble_dark_background);
92        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
93        mPaint.setColor(bubbleColor);
94        mBubbleColorAlpha = Color.alpha(bubbleColor) / 255.0f;
95        mFocusedOutlineColor = res.getColor(R.color.workspace_item_focused_outline_color);
96        mFocusedGlowColor = res.getColor(R.color.workspace_item_focused_glow_color);
97        mPressedOutlineColor = res.getColor(R.color.workspace_item_pressed_outline_color);
98        mPressedGlowColor = res.getColor(R.color.workspace_item_pressed_glow_color);
99    }
100
101    protected int getCacheTopPadding() {
102        return (int) PADDING_V;
103    }
104    protected int getCacheBottomPadding() {
105        return (int) (PADDING_V + SHADOW_LARGE_RADIUS + SHADOW_Y_OFFSET);
106    }
107    protected int getCacheLeftPadding() {
108        return (int) (PADDING_H + SHADOW_LARGE_RADIUS);
109    }
110    protected int getCacheRightPadding() {
111        return (int) (PADDING_H + SHADOW_LARGE_RADIUS);
112    }
113
114    public void applyFromShortcutInfo(ShortcutInfo info, IconCache iconCache) {
115        Bitmap b = info.getIcon(iconCache);
116
117        setCompoundDrawablesWithIntrinsicBounds(null,
118                new FastBitmapDrawable(b),
119                null, null);
120        setText(info.title);
121        setTag(info);
122    }
123
124    @Override
125    protected boolean setFrame(int left, int top, int right, int bottom) {
126        if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) {
127            mBackgroundSizeChanged = true;
128        }
129        return super.setFrame(left, top, right, bottom);
130    }
131
132    @Override
133    protected boolean verifyDrawable(Drawable who) {
134        return who == mBackground || super.verifyDrawable(who);
135    }
136
137    @Override
138    protected void drawableStateChanged() {
139        if (isPressed()) {
140            // In this case, we have already created the pressed outline on ACTION_DOWN,
141            // so we just need to do an invalidate to trigger draw
142            if (!mDidInvalidateForPressedState) {
143                invalidate();
144            }
145        } else {
146            // Otherwise, either clear the pressed/focused background, or create a background
147            // for the focused state
148            final boolean backgroundEmptyBefore = mPressedOrFocusedBackground == null;
149            mPressedOrFocusedBackground = null;
150            if (isFocused()) {
151                mPressedOrFocusedBackground = createGlowingOutline(
152                        mTempCanvas, mFocusedGlowColor, mFocusedOutlineColor);
153                invalidate();
154            }
155            final boolean backgroundEmptyNow = mPressedOrFocusedBackground == null;
156            if (!backgroundEmptyBefore && backgroundEmptyNow) {
157                invalidate();
158            }
159        }
160
161        Drawable d = mBackground;
162        if (d != null && d.isStateful()) {
163            d.setState(getDrawableState());
164        }
165        super.drawableStateChanged();
166    }
167
168    /**
169     * Draw the View v into the given Canvas.
170     *
171     * @param v the view to draw
172     * @param destCanvas the canvas to draw on
173     * @param padding the horizontal and vertical padding to use when drawing
174     */
175    private void drawWithPadding(Canvas destCanvas, int padding) {
176        final Rect clipRect = mTempRect;
177        getDrawingRect(clipRect);
178
179        // adjust the clip rect so that we don't include the text label
180        clipRect.bottom =
181            getExtendedPaddingTop() - (int) BubbleTextView.PADDING_V + getLayout().getLineTop(0);
182
183        // Draw the View into the bitmap.
184        // The translate of scrollX and scrollY is necessary when drawing TextViews, because
185        // they set scrollX and scrollY to large values to achieve centered text
186        destCanvas.save();
187        destCanvas.translate(-getScrollX() + padding / 2, -getScrollY() + padding / 2);
188        destCanvas.clipRect(clipRect, Op.REPLACE);
189        draw(destCanvas);
190        destCanvas.restore();
191    }
192
193    /**
194     * Returns a new bitmap to be used as the object outline, e.g. to visualize the drop location.
195     * Responsibility for the bitmap is transferred to the caller.
196     */
197    private Bitmap createGlowingOutline(Canvas canvas, int outlineColor, int glowColor) {
198        final int padding = HolographicOutlineHelper.MAX_OUTER_BLUR_RADIUS;
199        final Bitmap b = Bitmap.createBitmap(
200                getWidth() + padding, getHeight() + padding, Bitmap.Config.ARGB_8888);
201
202        canvas.setBitmap(b);
203        drawWithPadding(canvas, padding);
204        mOutlineHelper.applyExtraThickExpensiveOutlineWithBlur(b, canvas, glowColor, outlineColor);
205
206        return b;
207    }
208
209    @Override
210    public boolean onTouchEvent(MotionEvent event) {
211        // Call the superclass onTouchEvent first, because sometimes it changes the state to
212        // isPressed() on an ACTION_UP
213        boolean result = super.onTouchEvent(event);
214
215        switch (event.getAction()) {
216            case MotionEvent.ACTION_DOWN:
217                // So that the pressed outline is visible immediately when isPressed() is true,
218                // we pre-create it on ACTION_DOWN (it takes a small but perceptible amount of time
219                // to create it)
220                if (mPressedOrFocusedBackground == null) {
221                    mPressedOrFocusedBackground = createGlowingOutline(
222                            mTempCanvas, mPressedGlowColor, mPressedOutlineColor);
223                }
224                // Invalidate so the pressed state is visible, or set a flag so we know that we
225                // have to call invalidate as soon as the state is "pressed"
226                if (isPressed()) {
227                    mDidInvalidateForPressedState = true;
228                    invalidate();
229                } else {
230                    mDidInvalidateForPressedState = false;
231                }
232                break;
233            case MotionEvent.ACTION_CANCEL:
234            case MotionEvent.ACTION_UP:
235                // If we've touched down and up on an item, and it's still not "pressed", then
236                // destroy the pressed outline
237                if (!isPressed()) {
238                    mPressedOrFocusedBackground = null;
239                }
240                break;
241        }
242        return result;
243    }
244
245    public void setVisibilityChangedListener(VisibilityChangedListener listener) {
246        mOnVisibilityChangedListener = listener;
247    }
248
249    @Override
250    protected void onVisibilityChanged(View changedView, int visibility) {
251        if (mOnVisibilityChangedListener != null) {
252            mOnVisibilityChangedListener.receiveVisibilityChangedMessage(this);
253        }
254        super.onVisibilityChanged(changedView, visibility);
255    }
256
257    @Override
258    public void draw(Canvas canvas) {
259        if (mPressedOrFocusedBackground != null && (isPressed() || isFocused())) {
260            // The blue glow can extend outside of our clip region, so we first temporarily expand
261            // the canvas's clip region
262            canvas.save(Canvas.CLIP_SAVE_FLAG);
263            int padding = HolographicOutlineHelper.MAX_OUTER_BLUR_RADIUS / 2;
264            canvas.clipRect(-padding + mScrollX, -padding + mScrollY,
265                    getWidth() + padding + mScrollX, getHeight() + padding + mScrollY,
266                    Region.Op.REPLACE);
267            // draw blue glow
268            canvas.drawBitmap(mPressedOrFocusedBackground,
269                    mScrollX - padding, mScrollY - padding, mTempPaint);
270            canvas.restore();
271        }
272        if (isBuildingCache()) {
273            // We enhance the shadow by drawing the shadow twice
274            this.setShadowLayer(SHADOW_LARGE_RADIUS, 0.0f, SHADOW_Y_OFFSET, SHADOW_LARGE_COLOUR);
275            super.draw(canvas);
276            this.setShadowLayer(SHADOW_SMALL_RADIUS, 0.0f, 0.0f, SHADOW_SMALL_COLOUR);
277            super.draw(canvas);
278        } else {
279            final Drawable background = mBackground;
280            if (background != null) {
281                final int scrollX = mScrollX;
282                final int scrollY = mScrollY;
283
284                if (mBackgroundSizeChanged) {
285                    background.setBounds(0, 0,  mRight - mLeft, mBottom - mTop);
286                    mBackgroundSizeChanged = false;
287                }
288
289                if ((scrollX | scrollY) == 0) {
290                    background.draw(canvas);
291                } else {
292                    canvas.translate(scrollX, scrollY);
293                    background.draw(canvas);
294                    canvas.translate(-scrollX, -scrollY);
295                }
296            }
297            super.draw(canvas);
298        }
299    }
300
301    @Override
302    protected void onAttachedToWindow() {
303        super.onAttachedToWindow();
304        if (mBackground != null) mBackground.setCallback(this);
305    }
306
307    @Override
308    protected void onDetachedFromWindow() {
309        super.onDetachedFromWindow();
310        if (mBackground != null) mBackground.setCallback(null);
311    }
312
313    @Override
314    protected boolean onSetAlpha(int alpha) {
315        if (mPrevAlpha != alpha) {
316            mPrevAlpha = alpha;
317            mPaint.setAlpha((int) (alpha * mBubbleColorAlpha));
318            super.onSetAlpha(alpha);
319        }
320        return true;
321    }
322}
323