MessageAttachmentTile.java revision 1867a56f7b9b3146829752b942285ffdc9b3f3de
1/* 2 * Copyright (C) 2012 Google Inc. 3 * Licensed to The Android Open Source Project. 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18package com.android.mail.browse; 19 20import android.app.FragmentManager; 21import android.content.ActivityNotFoundException; 22import android.content.Context; 23import android.content.Intent; 24import android.graphics.Bitmap; 25import android.net.Uri; 26import android.util.AttributeSet; 27import android.view.LayoutInflater; 28import android.view.View; 29import android.view.View.OnClickListener; 30import android.view.ViewGroup; 31import android.view.ViewParent; 32 33import com.android.ex.photo.Intents; 34import com.android.ex.photo.Intents.PhotoViewIntentBuilder; 35import com.android.ex.photo.util.ImageUtils; 36import com.android.mail.R; 37import com.android.mail.photo.MailPhotoViewActivity; 38import com.android.mail.providers.Attachment; 39import com.android.mail.providers.UIProvider; 40import com.android.mail.providers.UIProvider.AttachmentDestination; 41import com.android.mail.providers.UIProvider.AttachmentRendition; 42import com.android.mail.ui.AttachmentTile; 43import com.android.mail.ui.AttachmentTileGrid; 44import com.android.mail.utils.LogTag; 45import com.android.mail.utils.LogUtils; 46import com.android.mail.utils.Utils; 47 48import java.util.Comparator; 49import java.util.PriorityQueue; 50 51/** 52 * View for a single attachment in conversation view. Shows download status and allows launching 53 * intents to act on an attachment. 54 * 55 */ 56public class MessageAttachmentTile extends AttachmentTile implements OnClickListener, 57 AttachmentViewInterface { 58 59 private int mPhotoIndex; 60 private Uri mAttachmentsListUri; 61 private View mTextContainer; 62 63 private final AttachmentActionHandler mActionHandler; 64 65 private static final String LOG_TAG = LogTag.getLogTag(); 66 67 public MessageAttachmentTile(Context context) { 68 this(context, null); 69 } 70 71 public MessageAttachmentTile(Context context, AttributeSet attrs) { 72 super(context, attrs); 73 74 mActionHandler = new AttachmentActionHandler(context, this); 75 } 76 77 public void initialize(FragmentManager fragmentManager) { 78 mActionHandler.initialize(fragmentManager); 79 } 80 81 /** 82 * Render or update an attachment's view. This happens immediately upon instantiation, and 83 * repeatedly as status updates stream in, so only properties with new or changed values will 84 * cause sub-views to update. 85 */ 86 @Override 87 public void render(Attachment attachment, Uri attachmentsListUri, int index, 88 AttachmentPreviewCache attachmentPreviewCache, boolean loaderResult) { 89 super.render(attachment, attachmentsListUri, index, attachmentPreviewCache, loaderResult); 90 91 mAttachmentsListUri = attachmentsListUri; 92 mPhotoIndex = index; 93 94 mActionHandler.setAttachment(mAttachment); 95 mActionHandler.updateStatus(loaderResult); 96 } 97 98 public static MessageAttachmentTile inflate(LayoutInflater inflater, ViewGroup parent) { 99 MessageAttachmentTile view = (MessageAttachmentTile) inflater.inflate( 100 R.layout.conversation_message_attachment_tile, parent, false); 101 return view; 102 } 103 104 105 @Override 106 protected void onFinishInflate() { 107 super.onFinishInflate(); 108 109 mTextContainer = findViewById(R.id.attachment_tile_text_container); 110 111 setOnClickListener(this); 112 } 113 114 @Override 115 public void onClick(View v) { 116 onClick(v.getId(), v); 117 } 118 119 private boolean onClick(int res, View v) { 120 showAndDownloadAttachments(); 121 return true; 122 } 123 124 private void showAndDownloadAttachments() { 125 AttachmentTileGrid tileGrid = ((AttachmentTileGrid) getParent()); 126 int childCount = tileGrid.getChildCount(); 127 128 PriorityQueue<MessageAttachmentTile> queue = new PriorityQueue<MessageAttachmentTile>( 129 childCount, new ViewIndexDistanceComparator(mPhotoIndex)); 130 for (int i = 0; i < childCount; i++) { 131 MessageAttachmentTile tile = (MessageAttachmentTile) tileGrid.getChildAt(i); 132 queue.add(tile); 133 } 134 135 // we want our downloads to have higher priority than the highest background downloads 136 int maxAdditionalPriority = childCount; 137 for (int i = 0; i < childCount; i++) { 138 // higher priority tiles are returned first 139 MessageAttachmentTile tile = queue.remove(); 140 tile.downloadAttachment(maxAdditionalPriority - i, i != 0); 141 } 142 143 viewAttachment(); 144 } 145 146 public void downloadAttachment(int additionalPriority, boolean delayDownload) { 147 if (!mAttachment.isPresentLocally()) { 148 mActionHandler.startDownloadingAttachment(AttachmentDestination.CACHE, 149 UIProvider.AttachmentRendition.BEST, additionalPriority, delayDownload); 150 } 151 } 152 153 @Override 154 public void viewAttachment() { 155 if (ImageUtils.isImageMimeType( 156 Utils.normalizeMimeType(mAttachment.name, mAttachment.contentType))) { 157 final PhotoViewIntentBuilder builder = 158 Intents.newPhotoViewIntentBuilder(getContext(), MailPhotoViewActivity.class); 159 builder 160 .setPhotosUri(mAttachmentsListUri.toString()) 161 .setProjection(UIProvider.ATTACHMENT_PROJECTION) 162 .setPhotoIndex(mPhotoIndex); 163 164 getContext().startActivity(builder.build()); 165 return; 166 } 167 168 Intent intent = new Intent(Intent.ACTION_VIEW); 169 intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION 170 | Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET); 171 Utils.setIntentDataAndTypeAndNormalize(intent, mAttachment.contentUri, mAttachment.name, 172 mAttachment.contentType); 173 try { 174 getContext().startActivity(intent); 175 } catch (ActivityNotFoundException e) { 176 // couldn't find activity for View intent 177 LogUtils.e(LOG_TAG, "Coun't find Activity for intent", e); 178 } 179 } 180 181 @Override 182 public void updateProgress(boolean showDeterminateProgress) { 183 // do not show progress for image tiles 184 } 185 186 @Override 187 public void onUpdateStatus() { 188 } 189 190 @Override 191 public void setThumbnailToDefault() { 192 super.setThumbnailToDefault(); 193 mTextContainer.setVisibility(VISIBLE); 194 } 195 196 @Override 197 public void setThumbnail(Bitmap result) { 198 super.setThumbnail(result); 199 mTextContainer.setVisibility(GONE); 200 } 201 202 @Override 203 public void thumbnailLoadFailed() { 204 super.thumbnailLoadFailed(); 205 mActionHandler.startDownloadingAttachment( 206 AttachmentDestination.CACHE, AttachmentRendition.SIMPLE, 0, false); 207 } 208 209 /** 210 * Given two child views, figure out whose index is closest to the specified 211 * index. (generated by markwei) 212 */ 213 public static class ViewIndexDistanceComparator implements Comparator<View>{ 214 final private int mIndex; 215 /** 216 * @param index Compare based on each view's distance to this index 217 */ 218 public ViewIndexDistanceComparator(int index) { 219 mIndex = index; 220 } 221 222 @Override 223 public int compare(View lhs, View rhs) { 224 ViewParent parent = lhs.getParent(); 225 if (parent == rhs.getParent()) { 226 if (parent instanceof ViewGroup) { 227 ViewGroup p = (ViewGroup) parent; 228 int lhsIndex = p.indexOfChild(lhs); 229 int rhsIndex = p.indexOfChild(rhs); 230 int lhsDistance = Math.abs(mIndex - lhsIndex); 231 int rhsDistance = Math.abs(mIndex - rhsIndex); 232 // prefer shorter distance since they are the next ones to be swiped to 233 int result = lhsDistance - rhsDistance; 234 if (result == 0) { 235 // prefer higher index since they are to the right in the photoviewer 236 return rhsIndex - lhsIndex; 237 } 238 return result; 239 } 240 } 241 return 0; 242 } 243 } 244} 245