1 package com.android.mail.bitmap; 2 3 import android.content.res.Resources; 4 import android.graphics.Canvas; 5 import android.graphics.Paint; 6 import android.graphics.Paint.Align; 7 import android.graphics.Rect; 8 import android.graphics.drawable.Drawable; 9 10 import com.android.bitmap.BitmapCache; 11 import com.android.bitmap.DecodeAggregator; 12 import com.android.mail.R; 13 import com.android.mail.browse.ConversationItemViewCoordinates; 14 15 16 /** 17 * A 2x1 grid of attachment drawables. Supports showing a small "+N" badge in the corner. 18 */ 19 public class AttachmentGridDrawable extends CompositeDrawable<AttachmentDrawable> 20 implements Parallaxable { 21 22 public static final int MAX_VISIBLE_ATTACHMENT_COUNT = 2; 23 24 private BitmapCache mCache; 25 private DecodeAggregator mDecodeAggregator; 26 private String mOverflowText; 27 private ConversationItemViewCoordinates mCoordinates; 28 private float mParallaxFraction = 0.5f; 29 30 private final Resources mResources; 31 private final Drawable mPlaceholder; 32 private final Drawable mProgress; 33 private final int mOverflowTextColor; 34 private final int mOverflowBadgeColor; 35 private final Paint mPaint = new Paint(); 36 private final Rect mRect = new Rect(); 37 38 public AttachmentGridDrawable(Resources res, Drawable placeholder, Drawable progress) { 39 super(MAX_VISIBLE_ATTACHMENT_COUNT); 40 mResources = res; 41 mPlaceholder = placeholder; 42 mProgress = progress; 43 mOverflowTextColor = res.getColor(R.color.ap_overflow_text_color); 44 mOverflowBadgeColor = res.getColor(R.color.ap_overflow_badge_color); 45 46 mPaint.setAntiAlias(true); 47 } 48 49 @Override 50 protected AttachmentDrawable createDivisionDrawable() { 51 final AttachmentDrawable result = new AttachmentDrawable(mResources, mCache, 52 mDecodeAggregator, mCoordinates, mPlaceholder, mProgress); 53 return result; 54 } 55 56 public void setBitmapCache(BitmapCache cache) { 57 mCache = cache; 58 } 59 60 public void setDecodeAggregator(final DecodeAggregator decodeAggregator) { 61 this.mDecodeAggregator = decodeAggregator; 62 } 63 64 public void setOverflowText(String text) { 65 mOverflowText = text; 66 layoutOverflowBadge(); 67 } 68 69 public void setCoordinates(ConversationItemViewCoordinates coordinates) { 70 mCoordinates = coordinates; 71 layoutOverflowBadge(); 72 } 73 74 private void layoutOverflowBadge() { 75 if (mCoordinates == null || mOverflowText == null) { 76 return; 77 } 78 mPaint.setTextSize(mCoordinates.overflowFontSize); 79 mPaint.setTypeface(mCoordinates.overflowTypeface); 80 mPaint.setTextAlign(Align.CENTER); 81 mPaint.getTextBounds(mOverflowText, 0, mOverflowText.length(), mRect); 82 } 83 84 @Override 85 public void draw(Canvas canvas) { 86 for (int i = 0; i < mCount; i++) { 87 mDrawables.get(i).setParallaxFraction(mParallaxFraction); 88 } 89 90 super.draw(canvas); 91 92 // Overflow badge and count 93 if (mOverflowText != null && mCoordinates != null) { 94 final float radius = mCoordinates.overflowDiameter / 2f; 95 // transform item-level coordinates into local drawable coordinate space 96 final float x = mCoordinates.overflowXEnd - mCoordinates.attachmentPreviewsX - radius; 97 final float y = mCoordinates.overflowYEnd - mCoordinates.attachmentPreviewsY - radius; 98 99 mPaint.setColor(mOverflowBadgeColor); 100 canvas.drawCircle(x, y, radius, mPaint); 101 102 mPaint.setColor(mOverflowTextColor); 103 canvas.drawText(mOverflowText, x, y + (mRect.height() / 2f), mPaint); 104 } 105 } 106 107 @Override 108 public void setParallaxFraction(float fraction) { 109 mParallaxFraction = fraction; 110 } 111 112 } 113