MessageAttachmentTile.java revision 1aee17e324f204080baaabceb64f4edf73681542
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.widget.ProgressBar; 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.List; 49 50/** 51 * View for a single attachment in conversation view. Shows download status and allows launching 52 * intents to act on an attachment. 53 * 54 */ 55public class MessageAttachmentTile extends AttachmentTile implements OnClickListener, 56 AttachmentViewInterface { 57 58 private int mPhotoIndex; 59 private Uri mAttachmentsListUri; 60 private View mTextContainer; 61 62 private final AttachmentActionHandler mActionHandler; 63 private ProgressBar mProgress; 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 mProgress = (ProgressBar) findViewById(R.id.attachment_progress); 111 112 setOnClickListener(this); 113 } 114 115 @Override 116 public void onClick(View v) { 117 onClick(v.getId(), v); 118 } 119 120 private boolean onClick(int res, View v) { 121 mActionHandler.showAndDownloadAttachments(); 122 123 return true; 124 } 125 126 @Override 127 public void viewAttachment() { 128 if (ImageUtils.isImageMimeType(Utils.normalizeMimeType(mAttachment.contentType))) { 129 final PhotoViewIntentBuilder builder = 130 Intents.newPhotoViewIntentBuilder(getContext(), MailPhotoViewActivity.class); 131 builder 132 .setPhotosUri(mAttachmentsListUri.toString()) 133 .setProjection(UIProvider.ATTACHMENT_PROJECTION) 134 .setPhotoIndex(mPhotoIndex); 135 136 getContext().startActivity(builder.build()); 137 return; 138 } 139 140 Intent intent = new Intent(Intent.ACTION_VIEW); 141 intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION 142 | Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET); 143 Utils.setIntentDataAndTypeAndNormalize(intent, mAttachment.contentUri, 144 mAttachment.contentType); 145 try { 146 getContext().startActivity(intent); 147 } catch (ActivityNotFoundException e) { 148 // couldn't find activity for View intent 149 LogUtils.e(LOG_TAG, "Coun't find Activity for intent", e); 150 } 151 } 152 153 @Override 154 public void updateProgress(boolean showDeterminateProgress) { 155 if (mAttachment.isDownloading()) { 156 mProgress.setMax(mAttachment.size); 157 mProgress.setProgress(mAttachment.downloadedSize); 158 mProgress.setIndeterminate(!showDeterminateProgress); 159 mProgress.setVisibility(VISIBLE); 160 } else { 161 mProgress.setVisibility(GONE); 162 } 163 } 164 165 @Override 166 public void onUpdateStatus() { 167 } 168 169 @Override 170 public void setThumbnailToDefault() { 171 super.setThumbnailToDefault(); 172 mTextContainer.setVisibility(VISIBLE); 173 } 174 175 @Override 176 public void setThumbnail(Bitmap result) { 177 super.setThumbnail(result); 178 mTextContainer.setVisibility(GONE); 179 } 180 181 @Override 182 public List<Attachment> getAttachments() { 183 return ((AttachmentTileGrid) getParent()).getAttachments(); 184 } 185 186 @Override 187 public void thumbnailLoadFailed() { 188 super.thumbnailLoadFailed(); 189 mActionHandler.startDownloadingAttachment( 190 AttachmentDestination.CACHE, AttachmentRendition.SIMPLE); 191 } 192} 193