blob: 995877b91783e4123ba95695a1ab8379757c911d [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;
Winson Chung88127032010-12-13 12:11:33 -080039 static final float SHADOW_LARGE_RADIUS = 4.0f;
40 static final float SHADOW_SMALL_RADIUS = 1.75f;
41 static final float SHADOW_Y_OFFSET = 2.0f;
42 static final int SHADOW_LARGE_COLOUR = 0xCC000000;
43 static final int SHADOW_SMALL_COLOUR = 0xBB000000;
Winson Chung656d11c2010-11-29 17:15:47 -080044 static final float PADDING_H = 8.0f;
45 static final float PADDING_V = 3.0f;
46
The Android Open Source Project31dd5032009-03-03 19:32:27 -080047 private final RectF mRect = new RectF();
48 private Paint mPaint;
Winson Chung656d11c2010-11-29 17:15:47 -080049 private float mBubbleColorAlpha;
Winson Chunge22a8e92010-11-12 13:40:58 -080050 private int mPrevAlpha = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080051
52 private boolean mBackgroundSizeChanged;
53 private Drawable mBackground;
54 private float mCornerRadius;
55 private float mPaddingH;
56 private float mPaddingV;
57
58 public BubbleTextView(Context context) {
59 super(context);
60 init();
61 }
62
63 public BubbleTextView(Context context, AttributeSet attrs) {
64 super(context, attrs);
65 init();
66 }
67
68 public BubbleTextView(Context context, AttributeSet attrs, int defStyle) {
69 super(context, attrs, defStyle);
70 init();
71 }
72
73 private void init() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -080074 mBackground = getBackground();
Winson Chung656d11c2010-11-29 17:15:47 -080075 setFocusable(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080076 setBackgroundDrawable(null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080077
Winson Chung656d11c2010-11-29 17:15:47 -080078 final Resources res = getContext().getResources();
79 int bubbleColor = res.getColor(R.color.bubble_dark_background);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080080 mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
Winson Chung656d11c2010-11-29 17:15:47 -080081 mPaint.setColor(bubbleColor);
82 mBubbleColorAlpha = Color.alpha(bubbleColor) / 255.0f;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080083
Winson Chung656d11c2010-11-29 17:15:47 -080084 final float scale = res.getDisplayMetrics().density;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080085 mCornerRadius = CORNER_RADIUS * scale;
86 mPaddingH = PADDING_H * scale;
87 //noinspection PointlessArithmeticExpression
88 mPaddingV = PADDING_V * scale;
89 }
90
Winson Chung88127032010-12-13 12:11:33 -080091 protected int getCacheTopPadding() {
Winson Chung656d11c2010-11-29 17:15:47 -080092 return (int) PADDING_V;
93 }
Winson Chung88127032010-12-13 12:11:33 -080094 protected int getCacheBottomPadding() {
95 return (int) (PADDING_V + SHADOW_LARGE_RADIUS + SHADOW_Y_OFFSET);
96 }
97 protected int getCacheLeftPadding() {
98 return (int) (PADDING_H + SHADOW_LARGE_RADIUS);
99 }
100 protected int getCacheRightPadding() {
101 return (int) (PADDING_H + SHADOW_LARGE_RADIUS);
Winson Chung656d11c2010-11-29 17:15:47 -0800102 }
103
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800104 public void applyFromShortcutInfo(ShortcutInfo info, IconCache iconCache) {
105 Bitmap b = info.getIcon(iconCache);
106
107 setCompoundDrawablesWithIntrinsicBounds(null,
108 new FastBitmapDrawable(b),
109 null, null);
110 setText(info.title);
111 buildAndEnableCache();
112 setTag(info);
113
114 }
115
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800116 @Override
117 protected boolean setFrame(int left, int top, int right, int bottom) {
118 if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) {
119 mBackgroundSizeChanged = true;
120 }
121 return super.setFrame(left, top, right, bottom);
122 }
123
124 @Override
125 protected boolean verifyDrawable(Drawable who) {
126 return who == mBackground || super.verifyDrawable(who);
127 }
128
129 @Override
130 protected void drawableStateChanged() {
131 Drawable d = mBackground;
132 if (d != null && d.isStateful()) {
133 d.setState(getDrawableState());
134 }
135 super.drawableStateChanged();
136 }
137
138 @Override
139 public void draw(Canvas canvas) {
Winson Chung88127032010-12-13 12:11:33 -0800140 if (isBuildingCache()) {
141 // We enhance the shadow by drawing the shadow twice
142 this.setShadowLayer(SHADOW_LARGE_RADIUS, 0.0f, SHADOW_Y_OFFSET, SHADOW_LARGE_COLOUR);
143 super.draw(canvas);
144 this.setShadowLayer(SHADOW_SMALL_RADIUS, 0.0f, 0.0f, SHADOW_SMALL_COLOUR);
145 super.draw(canvas);
146 } else {
147 final Drawable background = mBackground;
148 if (background != null) {
149 final int scrollX = mScrollX;
150 final int scrollY = mScrollY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800151
Winson Chung88127032010-12-13 12:11:33 -0800152 if (mBackgroundSizeChanged) {
153 background.setBounds(0, 0, mRight - mLeft, mBottom - mTop);
154 mBackgroundSizeChanged = false;
155 }
156
157 if ((scrollX | scrollY) == 0) {
158 background.draw(canvas);
159 } else {
160 canvas.translate(scrollX, scrollY);
161 background.draw(canvas);
162 canvas.translate(-scrollX, -scrollY);
163 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800164 }
165
Winson Chung88127032010-12-13 12:11:33 -0800166 super.draw(canvas);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800167 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800168 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400169
170 @Override
171 protected void onAttachedToWindow() {
172 super.onAttachedToWindow();
Winson Chung656d11c2010-11-29 17:15:47 -0800173 if (mBackground != null) mBackground.setCallback(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400174 }
175
176 @Override
177 protected void onDetachedFromWindow() {
178 super.onDetachedFromWindow();
Winson Chung656d11c2010-11-29 17:15:47 -0800179 if (mBackground != null) mBackground.setCallback(null);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400180 }
Winson Chungaffd7b42010-08-20 15:11:56 -0700181
182 @Override
183 protected boolean onSetAlpha(int alpha) {
Winson Chunge22a8e92010-11-12 13:40:58 -0800184 if (mPrevAlpha != alpha) {
185 mPrevAlpha = alpha;
Winson Chung656d11c2010-11-29 17:15:47 -0800186 mPaint.setAlpha((int) (alpha * mBubbleColorAlpha));
Winson Chunge22a8e92010-11-12 13:40:58 -0800187 super.onSetAlpha(alpha);
188 }
189 return true;
Winson Chungaffd7b42010-08-20 15:11:56 -0700190 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800191}