blob: 6039307921db38ba908959ed821c2bcd652ea57a [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
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
Joe Onoratoa5902522009-07-30 13:37:37 -070017package com.android.launcher2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
19import android.widget.TextView;
20import android.content.Context;
21import android.util.AttributeSet;
Michael Jurka67b2f6c2010-11-17 12:33:46 -080022import android.graphics.Bitmap;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080023import android.graphics.Canvas;
24import android.graphics.Paint;
25import android.graphics.RectF;
26import android.graphics.drawable.Drawable;
27import android.text.Layout;
28
Romain Guyedcce092010-03-04 13:03:17 -080029import com.android.launcher.R;
30
The Android Open Source Project31dd5032009-03-03 19:32:27 -080031/**
32 * TextView that draws a bubble behind the text. We cannot use a LineBackgroundSpan
33 * because we want to make the bubble taller than the text and TextView's clip is
34 * too aggressive.
35 */
Michael Jurka67b2f6c2010-11-17 12:33:46 -080036public class BubbleTextView extends CacheableTextView {
Joe Onoratobf15cb42009-08-07 14:33:40 -070037 static final float CORNER_RADIUS = 8.0f;
38 static final float PADDING_H = 5.0f;
39 static final float PADDING_V = 1.0f;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080040
41 private final RectF mRect = new RectF();
42 private Paint mPaint;
Winson Chunge22a8e92010-11-12 13:40:58 -080043 private int mPrevAlpha = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080044
45 private boolean mBackgroundSizeChanged;
46 private Drawable mBackground;
47 private float mCornerRadius;
48 private float mPaddingH;
49 private float mPaddingV;
50
51 public BubbleTextView(Context context) {
52 super(context);
53 init();
54 }
55
56 public BubbleTextView(Context context, AttributeSet attrs) {
57 super(context, attrs);
58 init();
59 }
60
61 public BubbleTextView(Context context, AttributeSet attrs, int defStyle) {
62 super(context, attrs, defStyle);
63 init();
64 }
65
66 private void init() {
67 setFocusable(true);
68 mBackground = getBackground();
69 setBackgroundDrawable(null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080070
71 mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
72 mPaint.setColor(getContext().getResources().getColor(R.color.bubble_dark_background));
73
74 final float scale = getContext().getResources().getDisplayMetrics().density;
75 mCornerRadius = CORNER_RADIUS * scale;
76 mPaddingH = PADDING_H * scale;
77 //noinspection PointlessArithmeticExpression
78 mPaddingV = PADDING_V * scale;
79 }
80
Michael Jurka67b2f6c2010-11-17 12:33:46 -080081 public void applyFromShortcutInfo(ShortcutInfo info, IconCache iconCache) {
82 Bitmap b = info.getIcon(iconCache);
83
84 setCompoundDrawablesWithIntrinsicBounds(null,
85 new FastBitmapDrawable(b),
86 null, null);
87 setText(info.title);
88 buildAndEnableCache();
89 setTag(info);
90
91 }
92
The Android Open Source Project31dd5032009-03-03 19:32:27 -080093 @Override
94 protected boolean setFrame(int left, int top, int right, int bottom) {
95 if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) {
96 mBackgroundSizeChanged = true;
97 }
98 return super.setFrame(left, top, right, bottom);
99 }
100
101 @Override
102 protected boolean verifyDrawable(Drawable who) {
103 return who == mBackground || super.verifyDrawable(who);
104 }
105
106 @Override
107 protected void drawableStateChanged() {
108 Drawable d = mBackground;
109 if (d != null && d.isStateful()) {
110 d.setState(getDrawableState());
111 }
112 super.drawableStateChanged();
113 }
114
115 @Override
116 public void draw(Canvas canvas) {
117 final Drawable background = mBackground;
118 if (background != null) {
119 final int scrollX = mScrollX;
120 final int scrollY = mScrollY;
121
122 if (mBackgroundSizeChanged) {
123 background.setBounds(0, 0, mRight - mLeft, mBottom - mTop);
124 mBackgroundSizeChanged = false;
125 }
126
127 if ((scrollX | scrollY) == 0) {
128 background.draw(canvas);
129 } else {
130 canvas.translate(scrollX, scrollY);
131 background.draw(canvas);
132 canvas.translate(-scrollX, -scrollY);
133 }
134 }
135
136 final Layout layout = getLayout();
137 final RectF rect = mRect;
138 final int left = getCompoundPaddingLeft();
139 final int top = getExtendedPaddingTop();
140
141 rect.set(left + layout.getLineLeft(0) - mPaddingH,
142 top + layout.getLineTop(0) - mPaddingV,
143 Math.min(left + layout.getLineRight(0) + mPaddingH, mScrollX + mRight - mLeft),
144 top + layout.getLineBottom(0) + mPaddingV);
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800145 // TEMPORARILY DISABLE DRAWING ROUND RECT -- re-enable this when we tweak CacheableTextView
146 // to support padding so we can capture the "rounded" edges
147 //canvas.drawRoundRect(rect, mCornerRadius, mCornerRadius, mPaint);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800148
149 super.draw(canvas);
150 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400151
152 @Override
153 protected void onAttachedToWindow() {
154 super.onAttachedToWindow();
155 mBackground.setCallback(this);
156 }
157
158 @Override
159 protected void onDetachedFromWindow() {
160 super.onDetachedFromWindow();
161 mBackground.setCallback(null);
162 }
Winson Chungaffd7b42010-08-20 15:11:56 -0700163
164 @Override
165 protected boolean onSetAlpha(int alpha) {
Winson Chunge22a8e92010-11-12 13:40:58 -0800166 if (mPrevAlpha != alpha) {
167 mPrevAlpha = alpha;
168 mPaint.setAlpha(alpha);
169 super.onSetAlpha(alpha);
170 }
171 return true;
Winson Chungaffd7b42010-08-20 15:11:56 -0700172 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800173}