The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 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 | |
Joe Onorato | a590252 | 2009-07-30 13:37:37 -0700 | [diff] [blame] | 17 | package com.android.launcher2; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 18 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 19 | import android.content.Context; |
Winson Chung | 656d11c | 2010-11-29 17:15:47 -0800 | [diff] [blame^] | 20 | import android.content.res.Resources; |
Michael Jurka | 67b2f6c | 2010-11-17 12:33:46 -0800 | [diff] [blame] | 21 | import android.graphics.Bitmap; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 22 | import android.graphics.Canvas; |
Winson Chung | 656d11c | 2010-11-29 17:15:47 -0800 | [diff] [blame^] | 23 | import android.graphics.Color; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 24 | import android.graphics.Paint; |
| 25 | import android.graphics.RectF; |
| 26 | import android.graphics.drawable.Drawable; |
| 27 | import android.text.Layout; |
Winson Chung | 656d11c | 2010-11-29 17:15:47 -0800 | [diff] [blame^] | 28 | import android.util.AttributeSet; |
| 29 | import android.view.View.MeasureSpec; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 30 | |
Romain Guy | edcce09 | 2010-03-04 13:03:17 -0800 | [diff] [blame] | 31 | import com.android.launcher.R; |
| 32 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 33 | /** |
| 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 Jurka | 67b2f6c | 2010-11-17 12:33:46 -0800 | [diff] [blame] | 38 | public class BubbleTextView extends CacheableTextView { |
Winson Chung | 656d11c | 2010-11-29 17:15:47 -0800 | [diff] [blame^] | 39 | 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 Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 44 | |
| 45 | private final RectF mRect = new RectF(); |
| 46 | private Paint mPaint; |
Winson Chung | 656d11c | 2010-11-29 17:15:47 -0800 | [diff] [blame^] | 47 | private float mBubbleColorAlpha; |
Winson Chung | e22a8e9 | 2010-11-12 13:40:58 -0800 | [diff] [blame] | 48 | private int mPrevAlpha = -1; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 49 | |
| 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 Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 72 | mBackground = getBackground(); |
Winson Chung | 656d11c | 2010-11-29 17:15:47 -0800 | [diff] [blame^] | 73 | setFocusable(true); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 74 | setBackgroundDrawable(null); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 75 | |
Winson Chung | 656d11c | 2010-11-29 17:15:47 -0800 | [diff] [blame^] | 76 | final Resources res = getContext().getResources(); |
| 77 | int bubbleColor = res.getColor(R.color.bubble_dark_background); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 78 | mPaint = new Paint(Paint.ANTI_ALIAS_FLAG); |
Winson Chung | 656d11c | 2010-11-29 17:15:47 -0800 | [diff] [blame^] | 79 | mPaint.setColor(bubbleColor); |
| 80 | mBubbleColorAlpha = Color.alpha(bubbleColor) / 255.0f; |
| 81 | mAppCellWidth = (int) res.getDimension(R.dimen.app_icon_size); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 82 | |
Winson Chung | 656d11c | 2010-11-29 17:15:47 -0800 | [diff] [blame^] | 83 | final float scale = res.getDisplayMetrics().density; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 84 | mCornerRadius = CORNER_RADIUS * scale; |
| 85 | mPaddingH = PADDING_H * scale; |
| 86 | //noinspection PointlessArithmeticExpression |
| 87 | mPaddingV = PADDING_V * scale; |
| 88 | } |
| 89 | |
Winson Chung | 656d11c | 2010-11-29 17:15:47 -0800 | [diff] [blame^] | 90 | protected int getVerticalPadding() { |
| 91 | return (int) PADDING_V; |
| 92 | } |
| 93 | protected int getHorizontalPadding() { |
| 94 | return (int) PADDING_H; |
| 95 | } |
| 96 | |
Michael Jurka | 67b2f6c | 2010-11-17 12:33:46 -0800 | [diff] [blame] | 97 | 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 Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 109 | @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 Chung | 656d11c | 2010-11-29 17:15:47 -0800 | [diff] [blame^] | 152 | // Draw the hotdog bubble |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 153 | final Layout layout = getLayout(); |
Winson Chung | 656d11c | 2010-11-29 17:15:47 -0800 | [diff] [blame^] | 154 | 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 Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 162 | |
Winson Chung | 656d11c | 2010-11-29 17:15:47 -0800 | [diff] [blame^] | 163 | canvas.drawRoundRect(mRect, mCornerRadius, mCornerRadius, mPaint); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 164 | |
| 165 | super.draw(canvas); |
| 166 | } |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 167 | |
| 168 | @Override |
Winson Chung | 656d11c | 2010-11-29 17:15:47 -0800 | [diff] [blame^] | 169 | 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 Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 183 | protected void onAttachedToWindow() { |
| 184 | super.onAttachedToWindow(); |
Winson Chung | 656d11c | 2010-11-29 17:15:47 -0800 | [diff] [blame^] | 185 | if (mBackground != null) mBackground.setCallback(this); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | @Override |
| 189 | protected void onDetachedFromWindow() { |
| 190 | super.onDetachedFromWindow(); |
Winson Chung | 656d11c | 2010-11-29 17:15:47 -0800 | [diff] [blame^] | 191 | if (mBackground != null) mBackground.setCallback(null); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 192 | } |
Winson Chung | affd7b4 | 2010-08-20 15:11:56 -0700 | [diff] [blame] | 193 | |
| 194 | @Override |
| 195 | protected boolean onSetAlpha(int alpha) { |
Winson Chung | e22a8e9 | 2010-11-12 13:40:58 -0800 | [diff] [blame] | 196 | if (mPrevAlpha != alpha) { |
| 197 | mPrevAlpha = alpha; |
Winson Chung | 656d11c | 2010-11-29 17:15:47 -0800 | [diff] [blame^] | 198 | mPaint.setAlpha((int) (alpha * mBubbleColorAlpha)); |
Winson Chung | e22a8e9 | 2010-11-12 13:40:58 -0800 | [diff] [blame] | 199 | super.onSetAlpha(alpha); |
| 200 | } |
| 201 | return true; |
Winson Chung | affd7b4 | 2010-08-20 15:11:56 -0700 | [diff] [blame] | 202 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 203 | } |