1 package com.android.mail.bitmap; 2 3 import android.content.ContentResolver; 4 import android.content.Context; 5 import android.content.res.AssetFileDescriptor; 6 import android.database.Cursor; 7 import android.net.Uri; 8 import android.text.TextUtils; 9 10 import com.android.bitmap.DecodeTask; 11 import com.android.mail.providers.Attachment; 12 import com.android.mail.providers.UIProvider; 13 14 import java.io.IOException; 15 import java.io.InputStream; 16 17 /** 18 * A request object for image attachment previews. 19 * <p> 20 * All requests are the same size to promote bitmap reuse. 21 */ 22 public class ImageAttachmentRequest implements DecodeTask.Request { 23 private final Context mContext; 24 private final String mLookupUri; 25 private final int mRendition; 26 public final int mDestW; 27 28 public ImageAttachmentRequest(final Context context, final String lookupUri, 29 final int rendition, final int destW) { 30 mContext = context; 31 mLookupUri = lookupUri; 32 mRendition = rendition; 33 mDestW = destW; 34 } 35 36 @Override 37 public boolean equals(Object o) { 38 if (o == null || !(o instanceof ImageAttachmentRequest)) { 39 return false; 40 } 41 final ImageAttachmentRequest other = (ImageAttachmentRequest) o; 42 return TextUtils.equals(mLookupUri, other.mLookupUri) && mRendition == other.mRendition 43 && mDestW == other.mDestW; 44 } 45 46 @Override 47 public int hashCode() { 48 int hash = 17; 49 hash += 31 * hash + mLookupUri.hashCode(); 50 hash += 31 * hash + mRendition; 51 hash += 31 * hash + mDestW; 52 return hash; 53 } 54 55 @Override 56 public String toString() { 57 final StringBuilder sb = new StringBuilder("["); 58 sb.append(super.toString()); 59 sb.append(" uri="); 60 sb.append(mLookupUri); 61 sb.append(" rendition="); 62 sb.append(mRendition); 63 sb.append(" w="); 64 sb.append(mDestW); 65 sb.append("]"); 66 return sb.toString(); 67 } 68 69 /** 70 * Returns true iff the other request is for the same attachment, and differs only in which 71 * rendition is being requested. 72 * 73 */ 74 public boolean matches(ImageAttachmentRequest other) { 75 return other != null && TextUtils.equals(mLookupUri, other.mLookupUri) 76 && mDestW == other.mDestW; 77 } 78 79 @Override 80 public AssetFileDescriptor createFd() throws IOException { 81 AssetFileDescriptor result = null; 82 Cursor cursor = null; 83 final ContentResolver cr = mContext.getContentResolver(); 84 try { 85 cursor = cr.query(Uri.parse(mLookupUri), UIProvider.ATTACHMENT_PROJECTION, null, null, 86 null); 87 if (cursor != null && cursor.moveToFirst()) { 88 final Attachment a = new Attachment(cursor); 89 result = cr.openAssetFileDescriptor(a.getUriForRendition(mRendition), "r"); 90 } 91 } finally { 92 if (cursor != null) { 93 cursor.close(); 94 } 95 } 96 return result; 97 } 98 99 @Override 100 public InputStream createInputStream() throws IOException { 101 return null; 102 } 103 } 104