diff --git a/src/com/android/email/Controller.java b/src/com/android/email/Controller.java index 8c9ccdfe8..2abcde14f 100644 --- a/src/com/android/email/Controller.java +++ b/src/com/android/email/Controller.java @@ -1329,13 +1329,13 @@ public class Controller { @Override public void loadAttachmentFailed(long accountId, long messageId, long attachmentId, - MessagingException me) { + MessagingException me, boolean background) { try { // If the cause of the MessagingException is an IOException, we send a status of // CONNECTION_ERROR; in this case, AttachmentDownloadService will try again to // download the attachment. Otherwise, the error is considered non-recoverable. int status = EmailServiceStatus.ATTACHMENT_NOT_FOUND; - if (me.getCause() instanceof IOException) { + if (me != null && me.getCause() instanceof IOException) { status = EmailServiceStatus.CONNECTION_ERROR; } mCallbackProxy.loadAttachmentStatus(messageId, attachmentId, status, 0); @@ -1343,7 +1343,15 @@ public class Controller { } synchronized (mListeners) { for (Result listener : mListeners) { - listener.loadAttachmentCallback(me, accountId, messageId, attachmentId, 0); + // TODO We are overloading the exception here. The UI listens for this + // callback and displays a toast if the exception is not null. Since we + // want to avoid displaying toast for background operations, we force + // the exception to be null. This needs to be re-worked so the UI will + // only receive (or at least pays attention to) responses for requests + // it explicitly cares about. Then we would not need to overload the + // exception parameter. + listener.loadAttachmentCallback(background ? null : me, accountId, messageId, + attachmentId, 0); } } } @@ -1600,7 +1608,7 @@ public class Controller { } public void loadAttachment(long attachmentId, String destinationFile, - String contentUriString) throws RemoteException { + String contentUriString, boolean background) throws RemoteException { if (Email.DEBUG) { Log.d(TAG, "loadAttachment: " + attachmentId + " to " + destinationFile); } @@ -1629,7 +1637,7 @@ public class Controller { MessagingController legacyController = sInstance.mLegacyController; LegacyListener legacyListener = sInstance.mLegacyListener; legacyController.loadAttachment(msg.mAccountKey, msg.mId, msg.mMailboxKey, - attachmentId, legacyListener); + attachmentId, legacyListener, background); } else { // Send back the specific error status for this case sInstance.mCallbackProxy.loadAttachmentStatus(att.mMessageKey, attachmentId, diff --git a/src/com/android/email/ExchangeUtils.java b/src/com/android/email/ExchangeUtils.java index 219ade3ac..0be3debaf 100644 --- a/src/com/android/email/ExchangeUtils.java +++ b/src/com/android/email/ExchangeUtils.java @@ -98,7 +98,7 @@ public class ExchangeUtils { } public void loadAttachment(long attachmentId, String destinationFile, - String contentUriString) throws RemoteException { + String contentUriString, boolean background) throws RemoteException { } public void loadMore(long messageId) throws RemoteException { diff --git a/src/com/android/email/GroupMessagingListener.java b/src/com/android/email/GroupMessagingListener.java index 45924593c..53694a136 100644 --- a/src/com/android/email/GroupMessagingListener.java +++ b/src/com/android/email/GroupMessagingListener.java @@ -186,9 +186,10 @@ public class GroupMessagingListener extends MessagingListener { long accountId, long messageId, long attachmentId, - MessagingException me) { + MessagingException me, + boolean background) { for (MessagingListener l : mListeners) { - l.loadAttachmentFailed(accountId, messageId, attachmentId, me); + l.loadAttachmentFailed(accountId, messageId, attachmentId, me, background); } } diff --git a/src/com/android/email/MessagingController.java b/src/com/android/email/MessagingController.java index 38ad539ac..6c5320597 100644 --- a/src/com/android/email/MessagingController.java +++ b/src/com/android/email/MessagingController.java @@ -1870,7 +1870,7 @@ public class MessagingController implements Runnable { * @param listener */ public void loadAttachment(final long accountId, final long messageId, final long mailboxId, - final long attachmentId, MessagingListener listener) { + final long attachmentId, MessagingListener listener, final boolean background) { mListeners.loadAttachmentStarted(accountId, messageId, attachmentId, true); put("loadAttachment", listener, new Runnable() { @@ -1881,7 +1881,8 @@ public class MessagingController implements Runnable { Attachment.restoreAttachmentWithId(mContext, attachmentId); if (attachment == null) { mListeners.loadAttachmentFailed(accountId, messageId, attachmentId, - new MessagingException("The attachment is null")); + new MessagingException("The attachment is null"), + background); return; } if (Utility.attachmentExists(mContext, attachment)) { @@ -1901,7 +1902,8 @@ public class MessagingController implements Runnable { if (account == null || mailbox == null || message == null) { mListeners.loadAttachmentFailed(accountId, messageId, attachmentId, new MessagingException( - "Account, mailbox, message or attachment are null")); + "Account, mailbox, message or attachment are null"), + background); return; } @@ -1954,7 +1956,8 @@ public class MessagingController implements Runnable { } catch (MessagingException me) { if (Email.LOGD) Log.v(Email.LOG_TAG, "", me); - mListeners.loadAttachmentFailed(accountId, messageId, attachmentId, me); + mListeners.loadAttachmentFailed( + accountId, messageId, attachmentId, me, background); } catch (IOException ioe) { Log.e(Email.LOG_TAG, "Error while storing attachment." + ioe.toString()); } diff --git a/src/com/android/email/MessagingListener.java b/src/com/android/email/MessagingListener.java index 3d12a945e..535c32dab 100644 --- a/src/com/android/email/MessagingListener.java +++ b/src/com/android/email/MessagingListener.java @@ -96,7 +96,8 @@ public class MessagingListener { long accountId, long messageId, long attachmentId, - MessagingException me) { + MessagingException me, + boolean background) { } /** diff --git a/src/com/android/email/service/AttachmentDownloadService.java b/src/com/android/email/service/AttachmentDownloadService.java index e6a8473d6..e0872674b 100644 --- a/src/com/android/email/service/AttachmentDownloadService.java +++ b/src/com/android/email/service/AttachmentDownloadService.java @@ -68,11 +68,11 @@ public class AttachmentDownloadService extends Service implements Runnable { private static final int PRIORITY_NONE = -1; @SuppressWarnings("unused") // Low priority will be used for opportunistic downloads - private static final int PRIORITY_LOW = 0; + private static final int PRIORITY_BACKGROUND = 0; // Normal priority is for forwarded downloads in outgoing mail - private static final int PRIORITY_NORMAL = 1; + private static final int PRIORITY_SEND_MAIL = 1; // High priority is for user requests - private static final int PRIORITY_HIGH = 2; + private static final int PRIORITY_FOREGROUND = 2; // Minimum free storage in order to perform prefetch (25% of total memory) private static final float PREFETCH_MINIMUM_STORAGE_AVAILABLE = 0.25F; @@ -416,7 +416,7 @@ public class AttachmentDownloadService extends Service implements Runnable { new EmailServiceProxy(mContext, serviceClass, mServiceCallback); proxy.loadAttachment(req.attachmentId, file.getAbsolutePath(), AttachmentProvider.getAttachmentUri(req.accountId, req.attachmentId) - .toString()); + .toString(), req.priority != PRIORITY_FOREGROUND); // Lazily initialize our (reusable) pending intent if (mWatchdogPendingIntent == null) { Intent alarmIntent = new Intent(mContext, Watchdog.class); @@ -563,9 +563,9 @@ public class AttachmentDownloadService extends Service implements Runnable { int priorityClass = PRIORITY_NONE; int flags = att.mFlags; if ((flags & Attachment.FLAG_DOWNLOAD_FORWARD) != 0) { - priorityClass = PRIORITY_NORMAL; + priorityClass = PRIORITY_SEND_MAIL; } else if ((flags & Attachment.FLAG_DOWNLOAD_USER_REQUEST) != 0) { - priorityClass = PRIORITY_HIGH; + priorityClass = PRIORITY_FOREGROUND; } return priorityClass; } diff --git a/src/com/android/email/service/EmailServiceProxy.java b/src/com/android/email/service/EmailServiceProxy.java index 6b3502c9b..959843201 100644 --- a/src/com/android/email/service/EmailServiceProxy.java +++ b/src/com/android/email/service/EmailServiceProxy.java @@ -160,12 +160,13 @@ public class EmailServiceProxy implements IEmailService { } public void loadAttachment(final long attachmentId, final String destinationFile, - final String contentUriString) throws RemoteException { + final String contentUriString, final boolean background) throws RemoteException { setTask(new Runnable () { public void run() { try { if (mCallback != null) mService.setCallback(mCallback); - mService.loadAttachment(attachmentId, destinationFile, contentUriString); + mService.loadAttachment( + attachmentId, destinationFile, contentUriString, background); } catch (RemoteException e) { try { // Try to send a callback (if set) diff --git a/src/com/android/email/service/IEmailService.aidl b/src/com/android/email/service/IEmailService.aidl index 02164746f..187320895 100644 --- a/src/com/android/email/service/IEmailService.aidl +++ b/src/com/android/email/service/IEmailService.aidl @@ -27,7 +27,8 @@ interface IEmailService { void stopSync(long mailboxId); void loadMore(long messageId); - void loadAttachment(long attachmentId, String destinationFile, String contentUriString); + void loadAttachment(long attachmentId, String destinationFile, String contentUriString, + boolean background); void updateFolderList(long accountId); diff --git a/src/com/android/exchange/ExchangeService.java b/src/com/android/exchange/ExchangeService.java index c3a7b593f..594561785 100644 --- a/src/com/android/exchange/ExchangeService.java +++ b/src/com/android/exchange/ExchangeService.java @@ -379,7 +379,7 @@ public class ExchangeService extends Service implements Runnable { } public void loadAttachment(long attachmentId, String destinationFile, - String contentUriString) throws RemoteException { + String contentUriString, boolean background) throws RemoteException { if (Email.DEBUG) { Log.d(TAG, "loadAttachment: " + attachmentId + " to " + destinationFile); }