Andrew Sapperstein | 4aa1c13 | 2012-06-08 17:53:41 -0700 | [diff] [blame] | 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 | |
| 18 | package com.android.mail.browse; |
| 19 | |
| 20 | import android.app.AlertDialog; |
Paul Westbrook | 8081df4 | 2012-09-10 15:43:36 -0700 | [diff] [blame] | 21 | import android.app.FragmentManager; |
Andrew Sapperstein | 4aa1c13 | 2012-06-08 17:53:41 -0700 | [diff] [blame] | 22 | import android.content.ActivityNotFoundException; |
| 23 | import android.content.Context; |
| 24 | import android.content.Intent; |
Andrew Sapperstein | 2fd167d | 2014-01-28 10:07:38 -0800 | [diff] [blame] | 25 | import android.support.v4.text.BidiFormatter; |
Andrew Sapperstein | 4aa1c13 | 2012-06-08 17:53:41 -0700 | [diff] [blame] | 26 | import android.text.TextUtils; |
| 27 | import android.util.AttributeSet; |
| 28 | import android.view.LayoutInflater; |
| 29 | import android.view.Menu; |
| 30 | import android.view.MenuItem; |
| 31 | import android.view.View; |
| 32 | import android.view.View.OnClickListener; |
| 33 | import android.view.ViewGroup; |
Mark Wei | e18f42d | 2012-10-17 15:06:19 -0700 | [diff] [blame] | 34 | import android.widget.FrameLayout; |
Andrew Sapperstein | 4c7d8b5 | 2012-06-11 18:44:42 -0700 | [diff] [blame] | 35 | import android.widget.ImageButton; |
Andrew Sapperstein | 4aa1c13 | 2012-06-08 17:53:41 -0700 | [diff] [blame] | 36 | import android.widget.ImageView; |
| 37 | import android.widget.PopupMenu; |
| 38 | import android.widget.PopupMenu.OnMenuItemClickListener; |
| 39 | import android.widget.ProgressBar; |
| 40 | import android.widget.TextView; |
| 41 | |
| 42 | import com.android.mail.R; |
Andy Huang | e003b4c | 2013-08-16 10:32:05 -0700 | [diff] [blame] | 43 | import com.android.mail.analytics.Analytics; |
Andrew Sapperstein | f59d01c | 2014-02-20 10:27:06 -0800 | [diff] [blame] | 44 | import com.android.mail.providers.Account; |
Andrew Sapperstein | 4aa1c13 | 2012-06-08 17:53:41 -0700 | [diff] [blame] | 45 | import com.android.mail.providers.Attachment; |
| 46 | import com.android.mail.providers.UIProvider.AttachmentDestination; |
| 47 | import com.android.mail.providers.UIProvider.AttachmentState; |
Andrew Sapperstein | f59d01c | 2014-02-20 10:27:06 -0800 | [diff] [blame] | 48 | import com.android.mail.ui.AccountFeedbackActivity; |
Andrew Sapperstein | 4aa1c13 | 2012-06-08 17:53:41 -0700 | [diff] [blame] | 49 | import com.android.mail.utils.AttachmentUtils; |
Paul Westbrook | b334c90 | 2012-06-25 11:42:46 -0700 | [diff] [blame] | 50 | import com.android.mail.utils.LogTag; |
Andrew Sapperstein | 4aa1c13 | 2012-06-08 17:53:41 -0700 | [diff] [blame] | 51 | import com.android.mail.utils.LogUtils; |
| 52 | import com.android.mail.utils.MimeType; |
| 53 | import com.android.mail.utils.Utils; |
Andrew Sapperstein | 4cb51db | 2012-07-20 15:54:27 -0700 | [diff] [blame] | 54 | |
Andrew Sapperstein | 4aa1c13 | 2012-06-08 17:53:41 -0700 | [diff] [blame] | 55 | /** |
| 56 | * View for a single attachment in conversation view. Shows download status and allows launching |
| 57 | * intents to act on an attachment. |
| 58 | * |
| 59 | */ |
Mark Wei | e18f42d | 2012-10-17 15:06:19 -0700 | [diff] [blame] | 60 | public class MessageAttachmentBar extends FrameLayout implements OnClickListener, |
Andrew Sapperstein | 4aa1c13 | 2012-06-08 17:53:41 -0700 | [diff] [blame] | 61 | OnMenuItemClickListener, AttachmentViewInterface { |
| 62 | |
| 63 | private Attachment mAttachment; |
| 64 | private TextView mTitle; |
| 65 | private TextView mSubTitle; |
| 66 | private String mAttachmentSizeText; |
| 67 | private String mDisplayType; |
| 68 | private ProgressBar mProgress; |
Andrew Sapperstein | 4c7d8b5 | 2012-06-11 18:44:42 -0700 | [diff] [blame] | 69 | private ImageButton mCancelButton; |
Andrew Sapperstein | 4aa1c13 | 2012-06-08 17:53:41 -0700 | [diff] [blame] | 70 | private PopupMenu mPopup; |
| 71 | private ImageView mOverflowButton; |
| 72 | |
| 73 | private final AttachmentActionHandler mActionHandler; |
Andrew Sapperstein | c445aba | 2012-08-01 15:38:40 -0700 | [diff] [blame] | 74 | private boolean mSaveClicked; |
Andrew Sapperstein | f59d01c | 2014-02-20 10:27:06 -0800 | [diff] [blame] | 75 | private Account mAccount; |
Andrew Sapperstein | 4aa1c13 | 2012-06-08 17:53:41 -0700 | [diff] [blame] | 76 | |
Mark Wei | 74313d9 | 2012-10-18 12:51:50 -0700 | [diff] [blame] | 77 | private final Runnable mUpdateRunnable = new Runnable() { |
| 78 | @Override |
| 79 | public void run() { |
| 80 | updateActionsInternal(); |
| 81 | } |
| 82 | }; |
| 83 | |
Paul Westbrook | b334c90 | 2012-06-25 11:42:46 -0700 | [diff] [blame] | 84 | private static final String LOG_TAG = LogTag.getLogTag(); |
Andrew Sapperstein | 4aa1c13 | 2012-06-08 17:53:41 -0700 | [diff] [blame] | 85 | |
Andrew Sapperstein | 7638a10 | 2013-06-25 13:54:52 -0700 | [diff] [blame] | 86 | |
Andrew Sapperstein | 4aa1c13 | 2012-06-08 17:53:41 -0700 | [diff] [blame] | 87 | public MessageAttachmentBar(Context context) { |
| 88 | this(context, null); |
| 89 | } |
| 90 | |
| 91 | public MessageAttachmentBar(Context context, AttributeSet attrs) { |
| 92 | super(context, attrs); |
| 93 | |
| 94 | mActionHandler = new AttachmentActionHandler(context, this); |
| 95 | } |
| 96 | |
Paul Westbrook | 8081df4 | 2012-09-10 15:43:36 -0700 | [diff] [blame] | 97 | public void initialize(FragmentManager fragmentManager) { |
| 98 | mActionHandler.initialize(fragmentManager); |
| 99 | } |
| 100 | |
Andrew Sapperstein | 4aa1c13 | 2012-06-08 17:53:41 -0700 | [diff] [blame] | 101 | public static MessageAttachmentBar inflate(LayoutInflater inflater, ViewGroup parent) { |
| 102 | MessageAttachmentBar view = (MessageAttachmentBar) inflater.inflate( |
| 103 | R.layout.conversation_message_attachment_bar, parent, false); |
| 104 | return view; |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Render or update an attachment's view. This happens immediately upon instantiation, and |
| 109 | * repeatedly as status updates stream in, so only properties with new or changed values will |
| 110 | * cause sub-views to update. |
Andrew Sapperstein | 4aa1c13 | 2012-06-08 17:53:41 -0700 | [diff] [blame] | 111 | */ |
Andy Huang | 4f347e8 | 2014-02-25 17:32:28 -0800 | [diff] [blame] | 112 | public void render(Attachment attachment, Account account, ConversationMessage message, |
Andrew Sapperstein | 2fd167d | 2014-01-28 10:07:38 -0800 | [diff] [blame] | 113 | boolean loaderResult, BidiFormatter bidiFormatter) { |
Andrew Sapperstein | 7638a10 | 2013-06-25 13:54:52 -0700 | [diff] [blame] | 114 | // get account uri for potential eml viewer usage |
Andrew Sapperstein | f59d01c | 2014-02-20 10:27:06 -0800 | [diff] [blame] | 115 | mAccount = account; |
Andrew Sapperstein | 7638a10 | 2013-06-25 13:54:52 -0700 | [diff] [blame] | 116 | |
Andrew Sapperstein | 4aa1c13 | 2012-06-08 17:53:41 -0700 | [diff] [blame] | 117 | final Attachment prevAttachment = mAttachment; |
| 118 | mAttachment = attachment; |
Andy Huang | 023f8bd | 2014-03-26 17:44:53 -0700 | [diff] [blame] | 119 | mActionHandler.setAccount(mAccount.getEmailAddress()); |
Andy Huang | 4f347e8 | 2014-02-25 17:32:28 -0800 | [diff] [blame] | 120 | mActionHandler.setMessage(message); |
Andrew Sapperstein | 4aa1c13 | 2012-06-08 17:53:41 -0700 | [diff] [blame] | 121 | mActionHandler.setAttachment(mAttachment); |
| 122 | |
Andrew Sapperstein | c445aba | 2012-08-01 15:38:40 -0700 | [diff] [blame] | 123 | // reset mSaveClicked if we are not currently downloading |
| 124 | // So if the download fails or the download completes, we stop |
| 125 | // showing progress, etc |
| 126 | mSaveClicked = !attachment.isDownloading() ? false : mSaveClicked; |
| 127 | |
Andrew Sapperstein | 4aa1c13 | 2012-06-08 17:53:41 -0700 | [diff] [blame] | 128 | LogUtils.d(LOG_TAG, "got attachment list row: name=%s state/dest=%d/%d dled=%d" + |
Martin Hibdon | 519c218 | 2013-09-20 17:27:57 -0700 | [diff] [blame] | 129 | " contentUri=%s MIME=%s flags=%d", attachment.getName(), attachment.state, |
Andrew Sapperstein | 4aa1c13 | 2012-06-08 17:53:41 -0700 | [diff] [blame] | 130 | attachment.destination, attachment.downloadedSize, attachment.contentUri, |
Martin Hibdon | 519c218 | 2013-09-20 17:27:57 -0700 | [diff] [blame] | 131 | attachment.getContentType(), attachment.flags); |
Andrew Sapperstein | 4aa1c13 | 2012-06-08 17:53:41 -0700 | [diff] [blame] | 132 | |
Andrew Sapperstein | 7348cf2 | 2014-02-20 10:53:23 -0800 | [diff] [blame] | 133 | final String attachmentName = attachment.getName(); |
Martin Hibdon | 519c218 | 2013-09-20 17:27:57 -0700 | [diff] [blame] | 134 | if ((attachment.flags & Attachment.FLAG_DUMMY_ATTACHMENT) != 0) { |
| 135 | mTitle.setText(R.string.load_attachment); |
| 136 | } else if (prevAttachment == null |
Andrew Sapperstein | 7348cf2 | 2014-02-20 10:53:23 -0800 | [diff] [blame] | 137 | || !TextUtils.equals(attachmentName, prevAttachment.getName())) { |
Andrew Sapperstein | f59d01c | 2014-02-20 10:27:06 -0800 | [diff] [blame] | 138 | mTitle.setText(attachmentName); |
Andrew Sapperstein | 4aa1c13 | 2012-06-08 17:53:41 -0700 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | if (prevAttachment == null || attachment.size != prevAttachment.size) { |
Andrew Sapperstein | 2fd167d | 2014-01-28 10:07:38 -0800 | [diff] [blame] | 142 | mAttachmentSizeText = bidiFormatter.unicodeWrap( |
| 143 | AttachmentUtils.convertToHumanReadableSize(getContext(), attachment.size)); |
| 144 | mDisplayType = bidiFormatter.unicodeWrap( |
| 145 | AttachmentUtils.getDisplayType(getContext(), attachment)); |
Mark Wei | c40e5e8 | 2013-01-08 18:12:08 -0800 | [diff] [blame] | 146 | updateSubtitleText(); |
Andrew Sapperstein | 4aa1c13 | 2012-06-08 17:53:41 -0700 | [diff] [blame] | 147 | } |
| 148 | |
Andrew Sapperstein | 4aa1c13 | 2012-06-08 17:53:41 -0700 | [diff] [blame] | 149 | updateActions(); |
Paul Westbrook | 8081df4 | 2012-09-10 15:43:36 -0700 | [diff] [blame] | 150 | mActionHandler.updateStatus(loaderResult); |
Andrew Sapperstein | 4aa1c13 | 2012-06-08 17:53:41 -0700 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | @Override |
| 154 | protected void onFinishInflate() { |
| 155 | super.onFinishInflate(); |
| 156 | |
| 157 | mTitle = (TextView) findViewById(R.id.attachment_title); |
| 158 | mSubTitle = (TextView) findViewById(R.id.attachment_subtitle); |
| 159 | mProgress = (ProgressBar) findViewById(R.id.attachment_progress); |
| 160 | mOverflowButton = (ImageView) findViewById(R.id.overflow); |
Andrew Sapperstein | 4c7d8b5 | 2012-06-11 18:44:42 -0700 | [diff] [blame] | 161 | mCancelButton = (ImageButton) findViewById(R.id.cancel_attachment); |
Andrew Sapperstein | 4aa1c13 | 2012-06-08 17:53:41 -0700 | [diff] [blame] | 162 | |
| 163 | setOnClickListener(this); |
| 164 | mOverflowButton.setOnClickListener(this); |
| 165 | mCancelButton.setOnClickListener(this); |
| 166 | } |
| 167 | |
| 168 | @Override |
| 169 | public void onClick(View v) { |
| 170 | onClick(v.getId(), v); |
| 171 | } |
| 172 | |
| 173 | @Override |
| 174 | public boolean onMenuItemClick(MenuItem item) { |
| 175 | mPopup.dismiss(); |
| 176 | return onClick(item.getItemId(), null); |
| 177 | } |
| 178 | |
Scott Kennedy | 2b9d80e | 2013-07-30 23:03:45 -0700 | [diff] [blame] | 179 | private boolean onClick(final int res, final View v) { |
| 180 | if (res == R.id.preview_attachment) { |
| 181 | previewAttachment(); |
| 182 | } else if (res == R.id.save_attachment) { |
| 183 | if (mAttachment.canSave()) { |
| 184 | mActionHandler.startDownloadingAttachment(AttachmentDestination.EXTERNAL); |
| 185 | mSaveClicked = true; |
Andy Huang | e003b4c | 2013-08-16 10:32:05 -0700 | [diff] [blame] | 186 | |
| 187 | Analytics.getInstance().sendEvent( |
| 188 | "save_attachment", Utils.normalizeMimeType(mAttachment.getContentType()), |
| 189 | "attachment_bar", mAttachment.size); |
Scott Kennedy | 2b9d80e | 2013-07-30 23:03:45 -0700 | [diff] [blame] | 190 | } |
| 191 | } else if (res == R.id.download_again) { |
| 192 | if (mAttachment.isPresentLocally()) { |
Mark Wei | ce9db80 | 2013-08-08 14:57:29 -0700 | [diff] [blame] | 193 | mActionHandler.showDownloadingDialog(); |
Scott Kennedy | 2b9d80e | 2013-07-30 23:03:45 -0700 | [diff] [blame] | 194 | mActionHandler.startRedownloadingAttachment(mAttachment); |
Andy Huang | e003b4c | 2013-08-16 10:32:05 -0700 | [diff] [blame] | 195 | |
| 196 | Analytics.getInstance().sendEvent("redownload_attachment", |
| 197 | Utils.normalizeMimeType(mAttachment.getContentType()), "attachment_bar", |
| 198 | mAttachment.size); |
Scott Kennedy | 2b9d80e | 2013-07-30 23:03:45 -0700 | [diff] [blame] | 199 | } |
| 200 | } else if (res == R.id.cancel_attachment) { |
| 201 | mActionHandler.cancelAttachment(); |
| 202 | mSaveClicked = false; |
Andy Huang | e003b4c | 2013-08-16 10:32:05 -0700 | [diff] [blame] | 203 | |
| 204 | Analytics.getInstance().sendEvent( |
| 205 | "cancel_attachment", Utils.normalizeMimeType(mAttachment.getContentType()), |
| 206 | "attachment_bar", mAttachment.size); |
Andy Huang | 4f347e8 | 2014-02-25 17:32:28 -0800 | [diff] [blame] | 207 | } else if (res == R.id.attachment_extra_option1) { |
| 208 | mActionHandler.handleOption1(); |
Scott Kennedy | 2b9d80e | 2013-07-30 23:03:45 -0700 | [diff] [blame] | 209 | } else if (res == R.id.overflow) { |
| 210 | // If no overflow items are visible, just bail out. |
| 211 | // We shouldn't be able to get here anyhow since the overflow |
| 212 | // button should be hidden. |
| 213 | if (shouldShowOverflow()) { |
Andrew Sapperstein | 4aa1c13 | 2012-06-08 17:53:41 -0700 | [diff] [blame] | 214 | if (mPopup == null) { |
| 215 | mPopup = new PopupMenu(getContext(), v); |
| 216 | mPopup.getMenuInflater().inflate(R.menu.message_footer_overflow_menu, |
| 217 | mPopup.getMenu()); |
| 218 | mPopup.setOnMenuItemClickListener(this); |
| 219 | } |
| 220 | |
| 221 | final Menu menu = mPopup.getMenu(); |
Mark Wei | 856200a | 2012-11-09 17:26:09 -0800 | [diff] [blame] | 222 | menu.findItem(R.id.preview_attachment).setVisible(shouldShowPreview()); |
| 223 | menu.findItem(R.id.save_attachment).setVisible(shouldShowSave()); |
| 224 | menu.findItem(R.id.download_again).setVisible(shouldShowDownloadAgain()); |
Andy Huang | 4f347e8 | 2014-02-25 17:32:28 -0800 | [diff] [blame] | 225 | menu.findItem(R.id.attachment_extra_option1).setVisible(shouldShowExtraOption1()); |
Andrew Sapperstein | 4aa1c13 | 2012-06-08 17:53:41 -0700 | [diff] [blame] | 226 | |
| 227 | mPopup.show(); |
Andrew Sapperstein | 4aa1c13 | 2012-06-08 17:53:41 -0700 | [diff] [blame] | 228 | } |
Scott Kennedy | 2b9d80e | 2013-07-30 23:03:45 -0700 | [diff] [blame] | 229 | } else { |
| 230 | // Handles clicking the attachment |
| 231 | // in any area that is not the overflow |
| 232 | // button or cancel button or one of the |
| 233 | // overflow items. |
Andy Huang | e003b4c | 2013-08-16 10:32:05 -0700 | [diff] [blame] | 234 | final String mime = Utils.normalizeMimeType(mAttachment.getContentType()); |
| 235 | final String action; |
| 236 | |
Martin Hibdon | 519c218 | 2013-09-20 17:27:57 -0700 | [diff] [blame] | 237 | if ((mAttachment.flags & Attachment.FLAG_DUMMY_ATTACHMENT) != 0) { |
| 238 | // This is a dummy. We need to download it, but not attempt to open or preview. |
| 239 | mActionHandler.showDownloadingDialog(); |
| 240 | mActionHandler.setViewOnFinish(false); |
| 241 | mActionHandler.startDownloadingAttachment(AttachmentDestination.CACHE); |
| 242 | |
| 243 | action = null; |
| 244 | } |
Scott Kennedy | 2b9d80e | 2013-07-30 23:03:45 -0700 | [diff] [blame] | 245 | // If we can install, install. |
| 246 | else if (MimeType.isInstallable(mAttachment.getContentType())) { |
| 247 | // Save to external because the package manager only handles |
| 248 | // file:// uris not content:// uris. We do the same |
| 249 | // workaround in |
| 250 | // UiProvider#getUiAttachmentsCursorForUIAttachments() |
| 251 | mActionHandler.showAttachment(AttachmentDestination.EXTERNAL); |
Andy Huang | e003b4c | 2013-08-16 10:32:05 -0700 | [diff] [blame] | 252 | |
| 253 | action = "attachment_bar_install"; |
Scott Kennedy | 2b9d80e | 2013-07-30 23:03:45 -0700 | [diff] [blame] | 254 | } |
| 255 | // If we can view or play with an on-device app, |
| 256 | // view or play. |
| 257 | else if (MimeType.isViewable( |
| 258 | getContext(), mAttachment.contentUri, mAttachment.getContentType())) { |
| 259 | mActionHandler.showAttachment(AttachmentDestination.CACHE); |
Andy Huang | e003b4c | 2013-08-16 10:32:05 -0700 | [diff] [blame] | 260 | |
| 261 | action = "attachment_bar"; |
Scott Kennedy | 2b9d80e | 2013-07-30 23:03:45 -0700 | [diff] [blame] | 262 | } |
| 263 | // If we can only preview the attachment, preview. |
| 264 | else if (mAttachment.canPreview()) { |
| 265 | previewAttachment(); |
Andy Huang | e003b4c | 2013-08-16 10:32:05 -0700 | [diff] [blame] | 266 | |
| 267 | action = null; |
Scott Kennedy | 2b9d80e | 2013-07-30 23:03:45 -0700 | [diff] [blame] | 268 | } |
| 269 | // Otherwise, if we cannot do anything, show the info dialog. |
| 270 | else { |
| 271 | AlertDialog.Builder builder = new AlertDialog.Builder(getContext()); |
| 272 | int dialogMessage = R.string.no_application_found; |
| 273 | builder.setTitle(R.string.more_info_attachment) |
| 274 | .setMessage(dialogMessage) |
| 275 | .show(); |
Andy Huang | e003b4c | 2013-08-16 10:32:05 -0700 | [diff] [blame] | 276 | |
| 277 | action = "attachment_bar_no_viewer"; |
| 278 | } |
| 279 | |
| 280 | if (action != null) { |
| 281 | Analytics.getInstance() |
| 282 | .sendEvent("view_attachment", mime, action, mAttachment.size); |
Scott Kennedy | 2b9d80e | 2013-07-30 23:03:45 -0700 | [diff] [blame] | 283 | } |
Andrew Sapperstein | 4aa1c13 | 2012-06-08 17:53:41 -0700 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | return true; |
| 287 | } |
| 288 | |
Mark Wei | 856200a | 2012-11-09 17:26:09 -0800 | [diff] [blame] | 289 | private boolean shouldShowPreview() { |
| 290 | // state could be anything |
| 291 | return mAttachment.canPreview(); |
| 292 | } |
| 293 | |
| 294 | private boolean shouldShowSave() { |
| 295 | return mAttachment.canSave() && !mSaveClicked; |
| 296 | } |
| 297 | |
| 298 | private boolean shouldShowDownloadAgain() { |
Mark Wei | b5ef9a9 | 2013-01-30 17:32:37 -0800 | [diff] [blame] | 299 | // implies state == SAVED || state == FAILED |
Andrew Sapperstein | 7434e80 | 2013-06-21 11:26:49 -0700 | [diff] [blame] | 300 | // and the attachment supports re-download |
| 301 | return mAttachment.supportsDownloadAgain() && mAttachment.isDownloadFinishedOrFailed(); |
Mark Wei | 856200a | 2012-11-09 17:26:09 -0800 | [diff] [blame] | 302 | } |
| 303 | |
Andy Huang | 4f347e8 | 2014-02-25 17:32:28 -0800 | [diff] [blame] | 304 | private boolean shouldShowExtraOption1() { |
| 305 | return mActionHandler.shouldShowExtraOption1(); |
| 306 | } |
| 307 | |
Mark Wei | 856200a | 2012-11-09 17:26:09 -0800 | [diff] [blame] | 308 | private boolean shouldShowOverflow() { |
Andy Huang | 4f347e8 | 2014-02-25 17:32:28 -0800 | [diff] [blame] | 309 | return (shouldShowPreview() || shouldShowSave() || shouldShowDownloadAgain() |
| 310 | || shouldShowExtraOption1()) && !shouldShowCancel(); |
Mark Wei | 856200a | 2012-11-09 17:26:09 -0800 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | private boolean shouldShowCancel() { |
| 314 | return mAttachment.isDownloading() && mSaveClicked; |
| 315 | } |
| 316 | |
Mark Wei | c40e5e8 | 2013-01-08 18:12:08 -0800 | [diff] [blame] | 317 | @Override |
Andrew Sapperstein | 4aa1c13 | 2012-06-08 17:53:41 -0700 | [diff] [blame] | 318 | public void viewAttachment() { |
Paul Westbrook | 591eb37 | 2012-07-25 12:07:20 -0700 | [diff] [blame] | 319 | if (mAttachment.contentUri == null) { |
| 320 | LogUtils.e(LOG_TAG, "viewAttachment with null content uri"); |
| 321 | return; |
| 322 | } |
| 323 | |
Andrew Sapperstein | 4aa1c13 | 2012-06-08 17:53:41 -0700 | [diff] [blame] | 324 | Intent intent = new Intent(Intent.ACTION_VIEW); |
| 325 | intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION |
| 326 | | Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET); |
Andrew Sapperstein | 8d4791c | 2013-06-11 15:04:38 -0700 | [diff] [blame] | 327 | |
| 328 | final String contentType = mAttachment.getContentType(); |
Mark Wei | bf3a5c5 | 2013-02-14 15:18:43 -0800 | [diff] [blame] | 329 | Utils.setIntentDataAndTypeAndNormalize( |
Andrew Sapperstein | 8d4791c | 2013-06-11 15:04:38 -0700 | [diff] [blame] | 330 | intent, mAttachment.contentUri, contentType); |
| 331 | |
| 332 | // For EML files, we want to open our dedicated |
| 333 | // viewer rather than let any activity open it. |
| 334 | if (MimeType.isEmlMimeType(contentType)) { |
Andrew Sapperstein | f59d01c | 2014-02-20 10:27:06 -0800 | [diff] [blame] | 335 | intent.setClass(getContext().getApplicationContext(), EmlViewerActivity.class); |
| 336 | intent.putExtra(AccountFeedbackActivity.EXTRA_ACCOUNT_URI, |
| 337 | mAccount != null ? mAccount.uri : null); |
Andrew Sapperstein | 8d4791c | 2013-06-11 15:04:38 -0700 | [diff] [blame] | 338 | } |
| 339 | |
Andrew Sapperstein | 4aa1c13 | 2012-06-08 17:53:41 -0700 | [diff] [blame] | 340 | try { |
| 341 | getContext().startActivity(intent); |
| 342 | } catch (ActivityNotFoundException e) { |
| 343 | // couldn't find activity for View intent |
Paul Westbrook | 591eb37 | 2012-07-25 12:07:20 -0700 | [diff] [blame] | 344 | LogUtils.e(LOG_TAG, e, "Couldn't find Activity for intent"); |
Andrew Sapperstein | 4aa1c13 | 2012-06-08 17:53:41 -0700 | [diff] [blame] | 345 | } |
| 346 | } |
| 347 | |
| 348 | private void previewAttachment() { |
Andrew Sapperstein | d6318e5 | 2012-08-06 10:27:07 -0700 | [diff] [blame] | 349 | if (mAttachment.canPreview()) { |
Paul Westbrook | d49db8e | 2012-08-03 17:58:46 -0700 | [diff] [blame] | 350 | final Intent previewIntent = |
| 351 | new Intent(Intent.ACTION_VIEW, mAttachment.previewIntentUri); |
| 352 | getContext().startActivity(previewIntent); |
Andy Huang | e003b4c | 2013-08-16 10:32:05 -0700 | [diff] [blame] | 353 | |
| 354 | Analytics.getInstance().sendEvent( |
| 355 | "preview_attachment", Utils.normalizeMimeType(mAttachment.getContentType()), |
| 356 | null, mAttachment.size); |
Paul Westbrook | d49db8e | 2012-08-03 17:58:46 -0700 | [diff] [blame] | 357 | } |
Andrew Sapperstein | 4aa1c13 | 2012-06-08 17:53:41 -0700 | [diff] [blame] | 358 | } |
| 359 | |
Scott Kennedy | 3b965d7 | 2013-06-25 14:36:55 -0700 | [diff] [blame] | 360 | private static void setButtonVisible(View button, boolean visible) { |
Andrew Sapperstein | 4aa1c13 | 2012-06-08 17:53:41 -0700 | [diff] [blame] | 361 | button.setVisibility(visible ? VISIBLE : GONE); |
| 362 | } |
| 363 | |
| 364 | /** |
| 365 | * Update all actions based on current downloading state. |
| 366 | */ |
| 367 | private void updateActions() { |
Mark Wei | 74313d9 | 2012-10-18 12:51:50 -0700 | [diff] [blame] | 368 | removeCallbacks(mUpdateRunnable); |
| 369 | post(mUpdateRunnable); |
| 370 | } |
| 371 | |
| 372 | private void updateActionsInternal() { |
Andrew Sapperstein | a95161c | 2012-07-31 16:00:21 -0700 | [diff] [blame] | 373 | // If the progress dialog is visible, skip any of the updating |
Mark Wei | bbe74ae | 2012-11-19 11:20:09 -0800 | [diff] [blame] | 374 | if (mActionHandler.isProgressDialogVisible()) { |
Andrew Sapperstein | a95161c | 2012-07-31 16:00:21 -0700 | [diff] [blame] | 375 | return; |
| 376 | } |
| 377 | |
Andrew Sapperstein | 4aa1c13 | 2012-06-08 17:53:41 -0700 | [diff] [blame] | 378 | // To avoid visibility state transition bugs, every button's visibility should be touched |
| 379 | // once by this routine. |
Mark Wei | 856200a | 2012-11-09 17:26:09 -0800 | [diff] [blame] | 380 | setButtonVisible(mCancelButton, shouldShowCancel()); |
| 381 | setButtonVisible(mOverflowButton, shouldShowOverflow()); |
Andrew Sapperstein | 4aa1c13 | 2012-06-08 17:53:41 -0700 | [diff] [blame] | 382 | } |
| 383 | |
Mark Wei | c40e5e8 | 2013-01-08 18:12:08 -0800 | [diff] [blame] | 384 | @Override |
Andrew Sapperstein | 532f482 | 2012-06-14 12:48:35 -0700 | [diff] [blame] | 385 | public void onUpdateStatus() { |
Mark Wei | c40e5e8 | 2013-01-08 18:12:08 -0800 | [diff] [blame] | 386 | updateSubtitleText(); |
Andrew Sapperstein | 4aa1c13 | 2012-06-08 17:53:41 -0700 | [diff] [blame] | 387 | } |
| 388 | |
Mark Wei | c40e5e8 | 2013-01-08 18:12:08 -0800 | [diff] [blame] | 389 | @Override |
Andrew Sapperstein | 4aa1c13 | 2012-06-08 17:53:41 -0700 | [diff] [blame] | 390 | public void updateProgress(boolean showProgress) { |
Paul Westbrook | 4dabcc9 | 2012-09-19 16:31:23 -0700 | [diff] [blame] | 391 | if (mAttachment.isDownloading()) { |
Andrew Sapperstein | c445aba | 2012-08-01 15:38:40 -0700 | [diff] [blame] | 392 | mProgress.setMax(mAttachment.size); |
Andrew Sapperstein | 4aa1c13 | 2012-06-08 17:53:41 -0700 | [diff] [blame] | 393 | mProgress.setProgress(mAttachment.downloadedSize); |
Andrew Sapperstein | 4aa1c13 | 2012-06-08 17:53:41 -0700 | [diff] [blame] | 394 | mProgress.setIndeterminate(!showProgress); |
Andrew Sapperstein | 4aa1c13 | 2012-06-08 17:53:41 -0700 | [diff] [blame] | 395 | mProgress.setVisibility(VISIBLE); |
Mark Wei | e18f42d | 2012-10-17 15:06:19 -0700 | [diff] [blame] | 396 | mSubTitle.setVisibility(INVISIBLE); |
Andrew Sapperstein | 4aa1c13 | 2012-06-08 17:53:41 -0700 | [diff] [blame] | 397 | } else { |
| 398 | mProgress.setVisibility(INVISIBLE); |
Mark Wei | e18f42d | 2012-10-17 15:06:19 -0700 | [diff] [blame] | 399 | mSubTitle.setVisibility(VISIBLE); |
Andrew Sapperstein | 4aa1c13 | 2012-06-08 17:53:41 -0700 | [diff] [blame] | 400 | } |
| 401 | } |
| 402 | |
Mark Wei | c40e5e8 | 2013-01-08 18:12:08 -0800 | [diff] [blame] | 403 | private void updateSubtitleText() { |
Andrew Sapperstein | 4aa1c13 | 2012-06-08 17:53:41 -0700 | [diff] [blame] | 404 | // TODO: make this a formatted resource when we have a UX design. |
| 405 | // not worth translation right now. |
Mark Wei | c40e5e8 | 2013-01-08 18:12:08 -0800 | [diff] [blame] | 406 | final StringBuilder sb = new StringBuilder(); |
| 407 | if (mAttachment.state == AttachmentState.FAILED) { |
| 408 | sb.append(getResources().getString(R.string.download_failed)); |
| 409 | } else { |
| 410 | if (mAttachment.isSavedToExternal()) { |
| 411 | sb.append(getResources().getString(R.string.saved, mAttachmentSizeText)); |
| 412 | } else { |
| 413 | sb.append(mAttachmentSizeText); |
| 414 | } |
Martin Hibdon | 519c218 | 2013-09-20 17:27:57 -0700 | [diff] [blame] | 415 | if (mDisplayType != null) { |
| 416 | sb.append(' '); |
| 417 | sb.append(mDisplayType); |
| 418 | } |
Andrew Sapperstein | 4aa1c13 | 2012-06-08 17:53:41 -0700 | [diff] [blame] | 419 | } |
Andrew Sapperstein | 4aa1c13 | 2012-06-08 17:53:41 -0700 | [diff] [blame] | 420 | mSubTitle.setText(sb.toString()); |
| 421 | } |
Andrew Sapperstein | 4aa1c13 | 2012-06-08 17:53:41 -0700 | [diff] [blame] | 422 | } |