Utilities.java revision 97d85d23b013347bead4e2f5fa430a79ce69431e
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 17package com.android.launcher2; 18 19import java.util.Random; 20 21import android.content.Context; 22import android.content.res.Resources; 23import android.graphics.Bitmap; 24import android.graphics.BlurMaskFilter; 25import android.graphics.Canvas; 26import android.graphics.ColorMatrix; 27import android.graphics.ColorMatrixColorFilter; 28import android.graphics.Paint; 29import android.graphics.PaintFlagsDrawFilter; 30import android.graphics.PorterDuff; 31import android.graphics.Rect; 32import android.graphics.RectF; 33import android.graphics.TableMaskFilter; 34import android.graphics.Typeface; 35import android.graphics.drawable.BitmapDrawable; 36import android.graphics.drawable.Drawable; 37import android.graphics.drawable.PaintDrawable; 38import android.text.StaticLayout; 39import android.text.TextPaint; 40import android.text.Layout.Alignment; 41import android.util.DisplayMetrics; 42import android.util.Log; 43 44import com.android.launcher.R; 45 46/** 47 * Various utilities shared amongst the Launcher's classes. 48 */ 49final class Utilities { 50 private static final String TAG = "Launcher.Utilities"; 51 52 private static final boolean TEXT_BURN = false; 53 54 private static int sIconWidth = -1; 55 private static int sIconHeight = -1; 56 private static int sIconContentSize = -1; 57 private static int sIconTextureWidth = -1; 58 private static int sIconTextureHeight = -1; 59 60 private static final Paint sBlurPaint = new Paint(); 61 private static final Paint sGlowColorPressedPaint = new Paint(); 62 private static final Paint sGlowColorFocusedPaint = new Paint(); 63 private static final Paint sDisabledPaint = new Paint(); 64 private static final Rect sOldBounds = new Rect(); 65 private static final Canvas sCanvas = new Canvas(); 66 67 static { 68 sCanvas.setDrawFilter(new PaintFlagsDrawFilter(Paint.DITHER_FLAG, 69 Paint.FILTER_BITMAP_FLAG)); 70 } 71 72 static Bitmap centerToFit(Bitmap bitmap, int width, int height, Context context) { 73 final int bitmapWidth = bitmap.getWidth(); 74 final int bitmapHeight = bitmap.getHeight(); 75 76 if (bitmapWidth < width || bitmapHeight < height) { 77 int color = context.getResources().getColor(R.color.window_background); 78 79 Bitmap centered = Bitmap.createBitmap(bitmapWidth < width ? width : bitmapWidth, 80 bitmapHeight < height ? height : bitmapHeight, Bitmap.Config.RGB_565); 81 centered.setDensity(bitmap.getDensity()); 82 Canvas canvas = new Canvas(centered); 83 canvas.drawColor(color); 84 canvas.drawBitmap(bitmap, (width - bitmapWidth) / 2.0f, (height - bitmapHeight) / 2.0f, 85 null); 86 87 bitmap = centered; 88 } 89 90 return bitmap; 91 } 92 93 static int sColors[] = { 0xffff0000, 0xff00ff00, 0xff0000ff }; 94 static int sColorIndex = 0; 95 96 static int getIconContentSize() { 97 return sIconContentSize; 98 } 99 100 /** 101 * Returns a bitmap suitable for the all apps view. The bitmap will be a power 102 * of two sized ARGB_8888 bitmap that can be used as a gl texture. 103 */ 104 static Bitmap createIconBitmap(Drawable icon, Context context) { 105 synchronized (sCanvas) { // we share the statics :-( 106 if (sIconWidth == -1) { 107 initStatics(context); 108 } 109 110 int width = sIconWidth; 111 int height = sIconHeight; 112 113 if (icon instanceof PaintDrawable) { 114 PaintDrawable painter = (PaintDrawable) icon; 115 painter.setIntrinsicWidth(width); 116 painter.setIntrinsicHeight(height); 117 } else if (icon instanceof BitmapDrawable) { 118 // Ensure the bitmap has a density. 119 BitmapDrawable bitmapDrawable = (BitmapDrawable) icon; 120 Bitmap bitmap = bitmapDrawable.getBitmap(); 121 if (bitmap.getDensity() == Bitmap.DENSITY_NONE) { 122 bitmapDrawable.setTargetDensity(context.getResources().getDisplayMetrics()); 123 } 124 } 125 int sourceWidth = icon.getIntrinsicWidth(); 126 int sourceHeight = icon.getIntrinsicHeight(); 127 128 if (sourceWidth > 0 && sourceWidth > 0) { 129 // There are intrinsic sizes. 130 if (width < sourceWidth || height < sourceHeight) { 131 // It's too big, scale it down. 132 final float ratio = (float) sourceWidth / sourceHeight; 133 if (sourceWidth > sourceHeight) { 134 height = (int) (width / ratio); 135 } else if (sourceHeight > sourceWidth) { 136 width = (int) (height * ratio); 137 } 138 } else if (sourceWidth < width && sourceHeight < height) { 139 // It's small, use the size they gave us. 140 width = sourceWidth; 141 height = sourceHeight; 142 } 143 } 144 145 // no intrinsic size --> use default size 146 int textureWidth = sIconTextureWidth; 147 int textureHeight = sIconTextureHeight; 148 149 final Bitmap bitmap = Bitmap.createBitmap(textureWidth, textureHeight, 150 Bitmap.Config.ARGB_8888); 151 final Canvas canvas = sCanvas; 152 canvas.setBitmap(bitmap); 153 154 final int left = (textureWidth-width) / 2; 155 final int top = (textureHeight-height) / 2; 156 157 if (false) { 158 // draw a big box for the icon for debugging 159 canvas.drawColor(sColors[sColorIndex]); 160 if (++sColorIndex >= sColors.length) sColorIndex = 0; 161 Paint debugPaint = new Paint(); 162 debugPaint.setColor(0xffcccc00); 163 canvas.drawRect(left, top, left+width, top+height, debugPaint); 164 } 165 166 sOldBounds.set(icon.getBounds()); 167 icon.setBounds(left, top, left+width, top+height); 168 icon.draw(canvas); 169 icon.setBounds(sOldBounds); 170 171 return bitmap; 172 } 173 } 174 175 static void drawSelectedAllAppsBitmap(Canvas dest, int destWidth, int destHeight, 176 boolean pressed, Bitmap src) { 177 synchronized (sCanvas) { // we share the statics :-( 178 if (sIconWidth == -1) { 179 // We can't have gotten to here without src being initialized, which 180 // comes from this file already. So just assert. 181 //initStatics(context); 182 throw new RuntimeException("Assertion failed: Utilities not initialized"); 183 } 184 185 dest.drawColor(0, PorterDuff.Mode.CLEAR); 186 187 int[] xy = new int[2]; 188 Bitmap mask = src.extractAlpha(sBlurPaint, xy); 189 190 float px = (destWidth - src.getWidth()) / 2; 191 float py = (destHeight - src.getHeight()) / 2; 192 dest.drawBitmap(mask, px + xy[0], py + xy[1], 193 pressed ? sGlowColorPressedPaint : sGlowColorFocusedPaint); 194 195 mask.recycle(); 196 } 197 } 198 199 /** 200 * Returns a Bitmap representing the thumbnail of the specified Bitmap. 201 * The size of the thumbnail is defined by the dimension 202 * android.R.dimen.launcher_application_icon_size. 203 * 204 * @param bitmap The bitmap to get a thumbnail of. 205 * @param context The application's context. 206 * 207 * @return A thumbnail for the specified bitmap or the bitmap itself if the 208 * thumbnail could not be created. 209 */ 210 static Bitmap resampleIconBitmap(Bitmap bitmap, Context context) { 211 synchronized (sCanvas) { // we share the statics :-( 212 if (sIconWidth == -1) { 213 initStatics(context); 214 } 215 216 if (bitmap.getWidth() == sIconWidth && bitmap.getHeight() == sIconHeight) { 217 return bitmap; 218 } else { 219 return createIconBitmap(new BitmapDrawable(bitmap), context); 220 } 221 } 222 } 223 224 static Bitmap drawDisabledBitmap(Bitmap bitmap, Context context) { 225 synchronized (sCanvas) { // we share the statics :-( 226 if (sIconWidth == -1) { 227 initStatics(context); 228 } 229 final Bitmap disabled = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), 230 Bitmap.Config.ARGB_8888); 231 final Canvas canvas = sCanvas; 232 canvas.setBitmap(disabled); 233 234 canvas.drawBitmap(bitmap, 0.0f, 0.0f, sDisabledPaint); 235 236 return disabled; 237 } 238 } 239 240 private static void initStatics(Context context) { 241 final Resources resources = context.getResources(); 242 final DisplayMetrics metrics = resources.getDisplayMetrics(); 243 final float density = metrics.density; 244 245 sIconWidth = sIconHeight = (int) resources.getDimension(R.dimen.app_icon_size); 246 if (LauncherApplication.isScreenXLarge()) { 247 sIconContentSize = (int) resources.getDimension(R.dimen.app_icon_content_size); 248 } 249 sIconTextureWidth = sIconTextureHeight = sIconWidth + 2; 250 251 sBlurPaint.setMaskFilter(new BlurMaskFilter(5 * density, BlurMaskFilter.Blur.NORMAL)); 252 sGlowColorPressedPaint.setColor(0xffffc300); 253 sGlowColorPressedPaint.setMaskFilter(TableMaskFilter.CreateClipTable(0, 30)); 254 sGlowColorFocusedPaint.setColor(0xffff8e00); 255 sGlowColorFocusedPaint.setMaskFilter(TableMaskFilter.CreateClipTable(0, 30)); 256 257 ColorMatrix cm = new ColorMatrix(); 258 cm.setSaturation(0.2f); 259 sDisabledPaint.setColorFilter(new ColorMatrixColorFilter(cm)); 260 sDisabledPaint.setAlpha(0x88); 261 } 262 263 static class BubbleText { 264 private static final int MAX_LINES = 2; 265 266 private final TextPaint mTextPaint; 267 268 private final RectF mBubbleRect = new RectF(); 269 270 private final float mTextWidth; 271 private final int mLeading; 272 private final int mFirstLineY; 273 private final int mLineHeight; 274 275 private final int mBitmapWidth; 276 private final int mBitmapHeight; 277 private final int mDensity; 278 279 BubbleText(Context context) { 280 final Resources resources = context.getResources(); 281 282 final DisplayMetrics metrics = resources.getDisplayMetrics(); 283 final float scale = metrics.density; 284 mDensity = metrics.densityDpi; 285 286 final float paddingLeft = 2.0f * scale; 287 final float paddingRight = 2.0f * scale; 288 final float cellWidth = resources.getDimension(R.dimen.title_texture_width); 289 290 RectF bubbleRect = mBubbleRect; 291 bubbleRect.left = 0; 292 bubbleRect.top = 0; 293 bubbleRect.right = (int) cellWidth; 294 295 mTextWidth = cellWidth - paddingLeft - paddingRight; 296 297 TextPaint textPaint = mTextPaint = new TextPaint(); 298 textPaint.setTypeface(Typeface.DEFAULT); 299 textPaint.setTextSize(13*scale); 300 textPaint.setColor(0xffffffff); 301 textPaint.setAntiAlias(true); 302 if (TEXT_BURN) { 303 textPaint.setShadowLayer(8, 0, 0, 0xff000000); 304 } 305 306 float ascent = -textPaint.ascent(); 307 float descent = textPaint.descent(); 308 float leading = 0.0f;//(ascent+descent) * 0.1f; 309 mLeading = (int)(leading + 0.5f); 310 mFirstLineY = (int)(leading + ascent + 0.5f); 311 mLineHeight = (int)(leading + ascent + descent + 0.5f); 312 313 mBitmapWidth = (int)(mBubbleRect.width() + 0.5f); 314 mBitmapHeight = roundToPow2((int)((MAX_LINES * mLineHeight) + leading + 0.5f)); 315 316 mBubbleRect.offsetTo((mBitmapWidth-mBubbleRect.width())/2, 0); 317 318 if (false) { 319 Log.d(TAG, "mBitmapWidth=" + mBitmapWidth + " mBitmapHeight=" 320 + mBitmapHeight + " w=" + ((int)(mBubbleRect.width() + 0.5f)) 321 + " h=" + ((int)((MAX_LINES * mLineHeight) + leading + 0.5f))); 322 } 323 } 324 325 /** You own the bitmap after this and you must call recycle on it. */ 326 Bitmap createTextBitmap(String text) { 327 Bitmap b = Bitmap.createBitmap(mBitmapWidth, mBitmapHeight, Bitmap.Config.ALPHA_8); 328 b.setDensity(mDensity); 329 Canvas c = new Canvas(b); 330 331 StaticLayout layout = new StaticLayout(text, mTextPaint, (int)mTextWidth, 332 Alignment.ALIGN_CENTER, 1, 0, true); 333 int lineCount = layout.getLineCount(); 334 if (lineCount > MAX_LINES) { 335 lineCount = MAX_LINES; 336 } 337 //if (!TEXT_BURN && lineCount > 0) { 338 //RectF bubbleRect = mBubbleRect; 339 //bubbleRect.bottom = height(lineCount); 340 //c.drawRoundRect(bubbleRect, mCornerRadius, mCornerRadius, mRectPaint); 341 //} 342 for (int i=0; i<lineCount; i++) { 343 //int x = (int)((mBubbleRect.width() - layout.getLineMax(i)) / 2.0f); 344 //int y = mFirstLineY + (i * mLineHeight); 345 final String lineText = text.substring(layout.getLineStart(i), layout.getLineEnd(i)); 346 int x = (int)(mBubbleRect.left 347 + ((mBubbleRect.width() - mTextPaint.measureText(lineText)) * 0.5f)); 348 int y = mFirstLineY + (i * mLineHeight); 349 c.drawText(lineText, x, y, mTextPaint); 350 } 351 352 return b; 353 } 354 355 private int height(int lineCount) { 356 return (int)((lineCount * mLineHeight) + mLeading + mLeading + 0.0f); 357 } 358 359 int getBubbleWidth() { 360 return (int)(mBubbleRect.width() + 0.5f); 361 } 362 363 int getMaxBubbleHeight() { 364 return height(MAX_LINES); 365 } 366 367 int getBitmapWidth() { 368 return mBitmapWidth; 369 } 370 371 int getBitmapHeight() { 372 return mBitmapHeight; 373 } 374 } 375 376 /** Only works for positive numbers. */ 377 static int roundToPow2(int n) { 378 int orig = n; 379 n >>= 1; 380 int mask = 0x8000000; 381 while (mask != 0 && (n & mask) == 0) { 382 mask >>= 1; 383 } 384 while (mask != 0) { 385 n |= mask; 386 mask >>= 1; 387 } 388 n += 1; 389 if (n != orig) { 390 n <<= 1; 391 } 392 return n; 393 } 394 395 static int generateRandomId() { 396 return new Random(System.currentTimeMillis()).nextInt(1 << 24); 397 } 398} 399