MessageAttachmentTile.java revision 90528f425e09a72b1505c3a1066051ab5a3a67b4
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.ui.AttachmentTile; 41import com.android.mail.ui.AttachmentTileGrid; 42import com.android.mail.ui.AttachmentTile.AttachmentPreviewCache; 43import com.android.mail.utils.LogTag; 44import com.android.mail.utils.LogUtils; 45import com.android.mail.utils.Utils; 46 47import java.util.List; 48 49/** 50 * View for a single attachment in conversation view. Shows download status and allows launching 51 * intents to act on an attachment. 52 * 53 */ 54public class MessageAttachmentTile extends AttachmentTile implements OnClickListener, 55 AttachmentViewInterface { 56 57 private int mPhotoIndex; 58 private Uri mAttachmentsListUri; 59 private View mTextContainer; 60 61 private final AttachmentActionHandler mActionHandler; 62 private ProgressBar mProgress; 63 64 private static final String LOG_TAG = LogTag.getLogTag(); 65 66 public MessageAttachmentTile(Context context) { 67 this(context, null); 68 } 69 70 public MessageAttachmentTile(Context context, AttributeSet attrs) { 71 super(context, attrs); 72 73 mActionHandler = new AttachmentActionHandler(context, this); 74 } 75 76 public void initialize(FragmentManager fragmentManager) { 77 mActionHandler.initialize(fragmentManager); 78 } 79 80 /** 81 * Render or update an attachment's view. This happens immediately upon instantiation, and 82 * repeatedly as status updates stream in, so only properties with new or changed values will 83 * cause sub-views to update. 84 */ 85 @Override 86 public void render(Attachment attachment, Uri attachmentsListUri, int index, 87 AttachmentPreviewCache attachmentPreviewCache, boolean loaderResult) { 88 super.render(attachment, attachmentsListUri, index, attachmentPreviewCache, loaderResult); 89 90 mAttachmentsListUri = attachmentsListUri; 91 mPhotoIndex = index; 92 93 mActionHandler.setAttachment(mAttachment); 94 mActionHandler.updateStatus(loaderResult); 95 } 96 97 public static MessageAttachmentTile inflate(LayoutInflater inflater, ViewGroup parent) { 98 MessageAttachmentTile view = (MessageAttachmentTile) inflater.inflate( 99 R.layout.conversation_message_attachment_tile, parent, false); 100 return view; 101 } 102 103 104 @Override 105 protected void onFinishInflate() { 106 super.onFinishInflate(); 107 108 mTextContainer = findViewById(R.id.attachment_tile_text_container); 109 mProgress = (ProgressBar) findViewById(R.id.attachment_progress); 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 mActionHandler.showAndDownloadAttachments(); 121 122 return true; 123 } 124 125 public void viewAttachment() { 126 if (ImageUtils.isImageMimeType(Utils.normalizeMimeType(mAttachment.contentType))) { 127 final PhotoViewIntentBuilder builder = 128 Intents.newPhotoViewIntentBuilder(getContext(), MailPhotoViewActivity.class); 129 builder 130 .setPhotosUri(mAttachmentsListUri.toString()) 131 .setProjection(UIProvider.ATTACHMENT_PROJECTION) 132 .setPhotoIndex(mPhotoIndex); 133 134 getContext().startActivity(builder.build()); 135 return; 136 } 137 138 Intent intent = new Intent(Intent.ACTION_VIEW); 139 intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION 140 | Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET); 141 Utils.setIntentDataAndTypeAndNormalize(intent, mAttachment.contentUri, 142 mAttachment.contentType); 143 try { 144 getContext().startActivity(intent); 145 } catch (ActivityNotFoundException e) { 146 // couldn't find activity for View intent 147 LogUtils.e(LOG_TAG, "Coun't find Activity for intent", e); 148 } 149 } 150 151 public void updateProgress(boolean showDeterminateProgress) { 152 if (mAttachment.isDownloading()) { 153 mProgress.setMax(mAttachment.size); 154 mProgress.setProgress(mAttachment.downloadedSize); 155 mProgress.setIndeterminate(!showDeterminateProgress); 156 mProgress.setVisibility(VISIBLE); 157 } else { 158 mProgress.setVisibility(GONE); 159 } 160 } 161 162 public void onUpdateStatus() { 163 } 164 165 @Override 166 public void setThumbnailToDefault() { 167 super.setThumbnailToDefault(); 168 mTextContainer.setVisibility(VISIBLE); 169 } 170 171 @Override 172 public void setThumbnail(Bitmap result) { 173 super.setThumbnail(result); 174 mTextContainer.setVisibility(GONE); 175 } 176 177 @Override 178 public List<Attachment> getAttachments() { 179 return ((AttachmentTileGrid) getParent()).getAttachments(); 180 } 181} 182