AttachmentActionHandler.java revision 267453587f13986acc7de3d52de95184c38fb4e1
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 20 21import android.app.DialogFragment; 22import android.app.Fragment; 23import android.app.FragmentManager; 24import android.app.FragmentTransaction; 25import android.content.ActivityNotFoundException; 26import android.content.ContentValues; 27import android.content.Context; 28import android.content.Intent; 29import android.net.Uri; 30import android.os.Handler; 31import android.os.Parcelable; 32 33import com.android.mail.providers.Attachment; 34import com.android.mail.providers.UIProvider; 35import com.android.mail.providers.UIProvider.AttachmentColumns; 36import com.android.mail.providers.UIProvider.AttachmentContentValueKeys; 37import com.android.mail.providers.UIProvider.AttachmentDestination; 38import com.android.mail.providers.UIProvider.AttachmentState; 39import com.android.mail.utils.LogTag; 40import com.android.mail.utils.LogUtils; 41import com.android.mail.utils.Utils; 42 43import java.util.ArrayList; 44import java.util.List; 45 46public class AttachmentActionHandler { 47 private static final String PROGRESS_FRAGMENT_TAG = "attachment-progress"; 48 49 private Attachment mAttachment; 50 51 private final AttachmentCommandHandler mCommandHandler; 52 private final AttachmentViewInterface mView; 53 private final Context mContext; 54 private final Handler mHandler; 55 private FragmentManager mFragmentManager; 56 57 private static final String LOG_TAG = LogTag.getLogTag(); 58 59 public AttachmentActionHandler(Context context, AttachmentViewInterface view) { 60 mCommandHandler = new AttachmentCommandHandler(context); 61 mView = view; 62 mContext = context; 63 mHandler = new Handler(); 64 } 65 66 public void initialize(FragmentManager fragmentManager) { 67 mFragmentManager = fragmentManager; 68 } 69 70 public void setAttachment(Attachment attachment) { 71 mAttachment = attachment; 72 } 73 74 public void showAttachment(int destination) { 75 // If the caller requested that this attachments be saved to the external storage, we should 76 // verify that the it was saved there. 77 if (mAttachment.isPresentLocally() && 78 (destination == AttachmentDestination.CACHE || 79 mAttachment.destination == destination)) { 80 mView.viewAttachment(); 81 } else { 82 showDownloadingDialog(destination); 83 startDownloadingAttachment(destination); 84 } 85 } 86 87 public void startDownloadingAttachment(int destination) { 88 startDownloadingAttachment(destination, UIProvider.AttachmentRendition.BEST, 0, false); 89 } 90 91 public void startDownloadingAttachment( 92 int destination, int rendition, int additionalPriority, boolean delayDownload) { 93 startDownloadingAttachment( 94 mAttachment, destination, rendition, additionalPriority, delayDownload); 95 } 96 97 private void startDownloadingAttachment( 98 Attachment attachment, int destination, int rendition, int additionalPriority, 99 boolean delayDownload) { 100 final ContentValues params = new ContentValues(2); 101 params.put(AttachmentColumns.STATE, AttachmentState.DOWNLOADING); 102 params.put(AttachmentColumns.DESTINATION, destination); 103 params.put(AttachmentContentValueKeys.RENDITION, rendition); 104 params.put(AttachmentContentValueKeys.ADDITIONAL_PRIORITY, additionalPriority); 105 params.put(AttachmentContentValueKeys.DELAY_DOWNLOAD, delayDownload); 106 107 mCommandHandler.sendCommand(attachment.uri, params); 108 } 109 110 public void cancelAttachment() { 111 final ContentValues params = new ContentValues(1); 112 params.put(AttachmentColumns.STATE, AttachmentState.NOT_SAVED); 113 114 mCommandHandler.sendCommand(mAttachment.uri, params); 115 } 116 117 public void startRedownloadingAttachment(Attachment attachment) { 118 showDownloadingDialog(attachment.destination); 119 final ContentValues params = new ContentValues(2); 120 params.put(AttachmentColumns.STATE, AttachmentState.REDOWNLOADING); 121 params.put(AttachmentColumns.DESTINATION, attachment.destination); 122 123 mCommandHandler.sendCommand(attachment.uri, params); 124 } 125 126 /** 127 * Displays a loading dialog to be used for downloading attachments. 128 * Must be called on the UI thread. 129 */ 130 private void showDownloadingDialog(int destination) { 131 final FragmentTransaction ft = mFragmentManager.beginTransaction(); 132 final Fragment prev = mFragmentManager.findFragmentByTag(PROGRESS_FRAGMENT_TAG); 133 if (prev != null) { 134 ft.remove(prev); 135 } 136 ft.addToBackStack(null); 137 138 // Create and show the dialog. 139 final DialogFragment newFragment = AttachmentProgressDialogFragment.newInstance( 140 mAttachment, destination); 141 newFragment.show(ft, PROGRESS_FRAGMENT_TAG); 142 } 143 144 /** 145 * Update progress-related views. Will also trigger a view intent if a progress dialog was 146 * previously brought up (by tapping 'View') and the download has now finished. 147 */ 148 public void updateStatus(boolean loaderResult) { 149 final boolean showProgress = mAttachment.shouldShowProgress(); 150 151 final AttachmentProgressDialogFragment dialog = (AttachmentProgressDialogFragment) 152 mFragmentManager.findFragmentByTag(PROGRESS_FRAGMENT_TAG); 153 if (dialog != null && dialog.isShowingDialogForAttachment(mAttachment)) { 154 dialog.setProgress(mAttachment.downloadedSize); 155 156 // We don't want the progress bar to switch back to indeterminate mode after 157 // have been in determinate progress mode. 158 final boolean indeterminate = !showProgress && dialog.isIndeterminate(); 159 dialog.setIndeterminate(indeterminate); 160 161 if (loaderResult && mAttachment.isDownloadFinishedOrFailed()) { 162 mHandler.post(new Runnable() { 163 @Override 164 public void run() { 165 dialog.dismiss(); 166 } 167 }); 168 } 169 170 if (mAttachment.state == AttachmentState.SAVED) { 171 mView.viewAttachment(); 172 } 173 } else { 174 mView.updateProgress(showProgress); 175 } 176 177 // Call on update status for the view so that it can do some specific things. 178 mView.onUpdateStatus(); 179 } 180 181 public boolean isProgressDialogVisible() { 182 final Fragment dialog = mFragmentManager.findFragmentByTag(PROGRESS_FRAGMENT_TAG); 183 return dialog != null && dialog.isVisible(); 184 } 185 186 public void shareAttachment() { 187 if (mAttachment.contentUri == null) { 188 return; 189 } 190 191 Intent intent = new Intent(Intent.ACTION_SEND); 192 intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION 193 | Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET); 194 195 final Uri uri = Utils.normalizeUri(mAttachment.contentUri); 196 intent.putExtra(Intent.EXTRA_STREAM, uri); 197 intent.setType(Utils.normalizeMimeType(mAttachment.contentType)); 198 199 try { 200 mContext.startActivity(intent); 201 } catch (ActivityNotFoundException e) { 202 // couldn't find activity for SEND intent 203 LogUtils.e(LOG_TAG, "Couldn't find Activity for intent", e); 204 } 205 } 206 207 public void shareAttachments(ArrayList<Parcelable> uris) { 208 Intent intent = new Intent(Intent.ACTION_SEND_MULTIPLE); 209 intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION 210 | Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET); 211 212 intent.setType("image/*"); 213 intent.putParcelableArrayListExtra( 214 Intent.EXTRA_STREAM, uris); 215 216 try { 217 mContext.startActivity(intent); 218 } catch (ActivityNotFoundException e) { 219 // couldn't find activity for SEND_MULTIPLE intent 220 LogUtils.e(LOG_TAG, "Couldn't find Activity for intent", e); 221 } 222 } 223} 224