blob: 113f35dd8ed205986a3555aa2b0d58d712f69e9a [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;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080029
Romain Guyedcce092010-03-04 13:03:17 -080030import com.android.launcher.R;
31
The Android Open Source Project31dd5032009-03-03 19:32:27 -080032/**
33 * TextView that draws a bubble behind the text. We cannot use a LineBackgroundSpan
34 * because we want to make the bubble taller than the text and TextView's clip is
35 * too aggressive.
36 */
Michael Jurka67b2f6c2010-11-17 12:33:46 -080037public class BubbleTextView extends CacheableTextView {
Winson Chung656d11c2010-11-29 17:15:47 -080038 static final float CORNER_RADIUS = 4.0f;
39 static final float PADDING_H = 8.0f;
40 static final float PADDING_V = 3.0f;
41
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042 private final RectF mRect = new RectF();
43 private Paint mPaint;
Winson Chung656d11c2010-11-29 17:15:47 -080044 private float mBubbleColorAlpha;
Winson Chunge22a8e92010-11-12 13:40:58 -080045 private int mPrevAlpha = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080046
47 private boolean mBackgroundSizeChanged;
48 private Drawable mBackground;
49 private float mCornerRadius;
50 private float mPaddingH;
51 private float mPaddingV;
52
53 public BubbleTextView(Context context) {
54 super(context);
55 init();
56 }
57
58 public BubbleTextView(Context context, AttributeSet attrs) {
59 super(context, attrs);
60 init();
61 }
62
63 public BubbleTextView(Context context, AttributeSet attrs, int defStyle) {
64 super(context, attrs, defStyle);
65 init();
66 }
67
68 private void init() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -080069 mBackground = getBackground();
Winson Chung656d11c2010-11-29 17:15:47 -080070 setFocusable(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080071 setBackgroundDrawable(null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080072
Winson Chung656d11c2010-11-29 17:15:47 -080073 final Resources res = getContext().getResources();
74 int bubbleColor = res.getColor(R.color.bubble_dark_background);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080075 mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
Winson Chung656d11c2010-11-29 17:15:47 -080076 mPaint.setColor(bubbleColor);
77 mBubbleColorAlpha = Color.alpha(bubbleColor) / 255.0f;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080078
Winson Chung656d11c2010-11-29 17:15:47 -080079 final float scale = res.getDisplayMetrics().density;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080080 mCornerRadius = CORNER_RADIUS * scale;
81 mPaddingH = PADDING_H * scale;
82 //noinspection PointlessArithmeticExpression
83 mPaddingV = PADDING_V * scale;
84 }
85
Winson Chung656d11c2010-11-29 17:15:47 -080086 protected int getVerticalPadding() {
87 return (int) PADDING_V;
88 }
89 protected int getHorizontalPadding() {
90 return (int) PADDING_H;
91 }
92
Michael Jurka67b2f6c2010-11-17 12:33:46 -080093 public void applyFromShortcutInfo(ShortcutInfo info, IconCache iconCache) {
94 Bitmap b = info.getIcon(iconCache);
95
96 setCompoundDrawablesWithIntrinsicBounds(null,
97 new FastBitmapDrawable(b),
98 null, null);
99 setText(info.title);
100 buildAndEnableCache();
101 setTag(info);
102
103 }
104
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800105 @Override
106 protected boolean setFrame(int left, int top, int right, int bottom) {
107 if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) {
108 mBackgroundSizeChanged = true;
109 }
110 return super.setFrame(left, top, right, bottom);
111 }
112
113 @Override
114 protected boolean verifyDrawable(Drawable who) {
115 return who == mBackground || super.verifyDrawable(who);
116 }
117
118 @Override
119 protected void drawableStateChanged() {
120 Drawable d = mBackground;
121 if (d != null && d.isStateful()) {
122 d.setState(getDrawableState());
123 }
124 super.drawableStateChanged();
125 }
126
127 @Override
128 public void draw(Canvas canvas) {
129 final Drawable background = mBackground;
130 if (background != null) {
131 final int scrollX = mScrollX;
132 final int scrollY = mScrollY;
133
134 if (mBackgroundSizeChanged) {
135 background.setBounds(0, 0, mRight - mLeft, mBottom - mTop);
136 mBackgroundSizeChanged = false;
137 }
138
139 if ((scrollX | scrollY) == 0) {
140 background.draw(canvas);
141 } else {
142 canvas.translate(scrollX, scrollY);
143 background.draw(canvas);
144 canvas.translate(-scrollX, -scrollY);
145 }
146 }
147
Winson Chung656d11c2010-11-29 17:15:47 -0800148 // Draw the hotdog bubble
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800149 final Layout layout = getLayout();
Winson Chung656d11c2010-11-29 17:15:47 -0800150 final int offset = getExtendedPaddingTop();
151 final int paddingLeft = getPaddingLeft();
152 final int paddingRight = getPaddingRight();
153 final float left = layout.getLineLeft(0) + paddingLeft;
154 final float right = Math.min(layout.getLineRight(0) + paddingRight,
155 left + getWidth() - paddingLeft - paddingRight);
156 mRect.set(left - mPaddingH, offset + (int) layout.getLineTop(0) - mPaddingV,
157 right + mPaddingH, offset + (int) layout.getLineBottom(0) + mPaddingV);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800158
Winson Chung656d11c2010-11-29 17:15:47 -0800159 canvas.drawRoundRect(mRect, mCornerRadius, mCornerRadius, mPaint);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800160
161 super.draw(canvas);
162 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400163
164 @Override
165 protected void onAttachedToWindow() {
166 super.onAttachedToWindow();
Winson Chung656d11c2010-11-29 17:15:47 -0800167 if (mBackground != null) mBackground.setCallback(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400168 }
169
170 @Override
171 protected void onDetachedFromWindow() {
172 super.onDetachedFromWindow();
Winson Chung656d11c2010-11-29 17:15:47 -0800173 if (mBackground != null) mBackground.setCallback(null);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400174 }
Winson Chungaffd7b42010-08-20 15:11:56 -0700175
176 @Override
177 protected boolean onSetAlpha(int alpha) {
Winson Chunge22a8e92010-11-12 13:40:58 -0800178 if (mPrevAlpha != alpha) {
179 mPrevAlpha = alpha;
Winson Chung656d11c2010-11-29 17:15:47 -0800180 mPaint.setAlpha((int) (alpha * mBubbleColorAlpha));
Winson Chunge22a8e92010-11-12 13:40:58 -0800181 super.onSetAlpha(alpha);
182 }
183 return true;
Winson Chungaffd7b42010-08-20 15:11:56 -0700184 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800185}