blob: 89a6303b41e5e3ac23960735f0f7cf6ba8bf641c [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
The Android Open Source Project31dd5032009-03-03 19:32:27 -080019import android.content.Context;
Winson Chung656d11c2010-11-29 17:15:47 -080020import android.content.res.Resources;
Michael Jurka67b2f6c2010-11-17 12:33:46 -080021import android.graphics.Bitmap;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080022import android.graphics.Canvas;
Winson Chung656d11c2010-11-29 17:15:47 -080023import android.graphics.Color;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080024import android.graphics.Paint;
25import android.graphics.RectF;
26import android.graphics.drawable.Drawable;
27import android.text.Layout;
Winson Chung656d11c2010-11-29 17:15:47 -080028import android.util.AttributeSet;
29import android.view.View.MeasureSpec;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080030
Romain Guyedcce092010-03-04 13:03:17 -080031import com.android.launcher.R;
32
The Android Open Source Project31dd5032009-03-03 19:32:27 -080033/**
34 * TextView that draws a bubble behind the text. We cannot use a LineBackgroundSpan
35 * because we want to make the bubble taller than the text and TextView's clip is
36 * too aggressive.
37 */
Michael Jurka67b2f6c2010-11-17 12:33:46 -080038public class BubbleTextView extends CacheableTextView {
Winson Chung656d11c2010-11-29 17:15:47 -080039 static final float CORNER_RADIUS = 4.0f;
40 static final float PADDING_H = 8.0f;
41 static final float PADDING_V = 3.0f;
42
43 private int mAppCellWidth;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080044
45 private final RectF mRect = new RectF();
46 private Paint mPaint;
Winson Chung656d11c2010-11-29 17:15:47 -080047 private float mBubbleColorAlpha;
Winson Chunge22a8e92010-11-12 13:40:58 -080048 private int mPrevAlpha = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080049
50 private boolean mBackgroundSizeChanged;
51 private Drawable mBackground;
52 private float mCornerRadius;
53 private float mPaddingH;
54 private float mPaddingV;
55
56 public BubbleTextView(Context context) {
57 super(context);
58 init();
59 }
60
61 public BubbleTextView(Context context, AttributeSet attrs) {
62 super(context, attrs);
63 init();
64 }
65
66 public BubbleTextView(Context context, AttributeSet attrs, int defStyle) {
67 super(context, attrs, defStyle);
68 init();
69 }
70
71 private void init() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -080072 mBackground = getBackground();
Winson Chung656d11c2010-11-29 17:15:47 -080073 setFocusable(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080074 setBackgroundDrawable(null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080075
Winson Chung656d11c2010-11-29 17:15:47 -080076 final Resources res = getContext().getResources();
77 int bubbleColor = res.getColor(R.color.bubble_dark_background);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080078 mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
Winson Chung656d11c2010-11-29 17:15:47 -080079 mPaint.setColor(bubbleColor);
80 mBubbleColorAlpha = Color.alpha(bubbleColor) / 255.0f;
81 mAppCellWidth = (int) res.getDimension(R.dimen.app_icon_size);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080082
Winson Chung656d11c2010-11-29 17:15:47 -080083 final float scale = res.getDisplayMetrics().density;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080084 mCornerRadius = CORNER_RADIUS * scale;
85 mPaddingH = PADDING_H * scale;
86 //noinspection PointlessArithmeticExpression
87 mPaddingV = PADDING_V * scale;
88 }
89
Winson Chung656d11c2010-11-29 17:15:47 -080090 protected int getVerticalPadding() {
91 return (int) PADDING_V;
92 }
93 protected int getHorizontalPadding() {
94 return (int) PADDING_H;
95 }
96
Michael Jurka67b2f6c2010-11-17 12:33:46 -080097 public void applyFromShortcutInfo(ShortcutInfo info, IconCache iconCache) {
98 Bitmap b = info.getIcon(iconCache);
99
100 setCompoundDrawablesWithIntrinsicBounds(null,
101 new FastBitmapDrawable(b),
102 null, null);
103 setText(info.title);
104 buildAndEnableCache();
105 setTag(info);
106
107 }
108
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800109 @Override
110 protected boolean setFrame(int left, int top, int right, int bottom) {
111 if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) {
112 mBackgroundSizeChanged = true;
113 }
114 return super.setFrame(left, top, right, bottom);
115 }
116
117 @Override
118 protected boolean verifyDrawable(Drawable who) {
119 return who == mBackground || super.verifyDrawable(who);
120 }
121
122 @Override
123 protected void drawableStateChanged() {
124 Drawable d = mBackground;
125 if (d != null && d.isStateful()) {
126 d.setState(getDrawableState());
127 }
128 super.drawableStateChanged();
129 }
130
131 @Override
132 public void draw(Canvas canvas) {
133 final Drawable background = mBackground;
134 if (background != null) {
135 final int scrollX = mScrollX;
136 final int scrollY = mScrollY;
137
138 if (mBackgroundSizeChanged) {
139 background.setBounds(0, 0, mRight - mLeft, mBottom - mTop);
140 mBackgroundSizeChanged = false;
141 }
142
143 if ((scrollX | scrollY) == 0) {
144 background.draw(canvas);
145 } else {
146 canvas.translate(scrollX, scrollY);
147 background.draw(canvas);
148 canvas.translate(-scrollX, -scrollY);
149 }
150 }
151
Winson Chung656d11c2010-11-29 17:15:47 -0800152 // Draw the hotdog bubble
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800153 final Layout layout = getLayout();
Winson Chung656d11c2010-11-29 17:15:47 -0800154 final int offset = getExtendedPaddingTop();
155 final int paddingLeft = getPaddingLeft();
156 final int paddingRight = getPaddingRight();
157 final float left = layout.getLineLeft(0) + paddingLeft;
158 final float right = Math.min(layout.getLineRight(0) + paddingRight,
159 left + getWidth() - paddingLeft - paddingRight);
160 mRect.set(left - mPaddingH, offset + (int) layout.getLineTop(0) - mPaddingV,
161 right + mPaddingH, offset + (int) layout.getLineBottom(0) + mPaddingV);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800162
Winson Chung656d11c2010-11-29 17:15:47 -0800163 canvas.drawRoundRect(mRect, mCornerRadius, mCornerRadius, mPaint);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800164
165 super.draw(canvas);
166 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400167
168 @Override
Winson Chung656d11c2010-11-29 17:15:47 -0800169 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
170 if (w > 0 && h > 0) {
171 // Temporary Workaround: We need to set padding to compress the text so that we can draw
172 // a hotdog around it. Currently, the background images prevent us from applying the
173 // padding in XML, so we are doing this programmatically
174 int d = w - mAppCellWidth;
175 int pL = d - (d / 2);
176 int pR = d - pL;
177 setPadding(pL, getPaddingTop(), pR, getPaddingBottom());
178 }
179 super.onSizeChanged(w, h, oldw, oldh);
180 }
181
182 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -0400183 protected void onAttachedToWindow() {
184 super.onAttachedToWindow();
Winson Chung656d11c2010-11-29 17:15:47 -0800185 if (mBackground != null) mBackground.setCallback(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400186 }
187
188 @Override
189 protected void onDetachedFromWindow() {
190 super.onDetachedFromWindow();
Winson Chung656d11c2010-11-29 17:15:47 -0800191 if (mBackground != null) mBackground.setCallback(null);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400192 }
Winson Chungaffd7b42010-08-20 15:11:56 -0700193
194 @Override
195 protected boolean onSetAlpha(int alpha) {
Winson Chunge22a8e92010-11-12 13:40:58 -0800196 if (mPrevAlpha != alpha) {
197 mPrevAlpha = alpha;
Winson Chung656d11c2010-11-29 17:15:47 -0800198 mPaint.setAlpha((int) (alpha * mBubbleColorAlpha));
Winson Chunge22a8e92010-11-12 13:40:58 -0800199 super.onSetAlpha(alpha);
200 }
201 return true;
Winson Chungaffd7b42010-08-20 15:11:56 -0700202 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800203}