From fdc41d4ffc29d29c9c6d8e0b81db98aaf7afa92e Mon Sep 17 00:00:00 2001 From: Marc Blank Date: Sat, 23 Oct 2010 12:14:00 -0700 Subject: [PATCH] Delay appearance of "stop" button when loading attachments Bug: 3082804 Change-Id: Ib8b0fcec36a7303b2094bd0e49645d11aa2ff2db --- .../activity/MessageViewFragmentBase.java | 28 +++++++++++++++++-- .../service/AttachmentDownloadService.java | 15 ++++++++++ 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/src/com/android/email/activity/MessageViewFragmentBase.java b/src/com/android/email/activity/MessageViewFragmentBase.java index 52aa006f9..3ed51d9a3 100644 --- a/src/com/android/email/activity/MessageViewFragmentBase.java +++ b/src/com/android/email/activity/MessageViewFragmentBase.java @@ -663,9 +663,33 @@ public abstract class MessageViewFragmentBase extends Fragment implements View.O } } - private void onLoadAttachment(AttachmentInfo attachment) { + private void onLoadAttachment(final AttachmentInfo attachment) { attachment.loadButton.setVisibility(View.GONE); - attachment.cancelButton.setVisibility(View.VISIBLE); + // If there's nothing in the download queue, we'll probably start right away so wait a + // second before showing the cancel button + if (AttachmentDownloadService.getQueueSize() == 0) { + // Set to invisible; if the button is still in this state one second from now, we'll + // assume the download won't start right away, and we make the cancel button visible + attachment.cancelButton.setVisibility(View.INVISIBLE); + // Create the timed task that will change the button state + new AsyncTask() { + @Override + protected Void doInBackground(Void... params) { + try { + Thread.sleep(1000L); + } catch (InterruptedException e) { } + return null; + } + @Override + protected void onPostExecute(Void result) { + if (attachment.cancelButton.getVisibility() == View.INVISIBLE) { + attachment.cancelButton.setVisibility(View.VISIBLE); + } + } + }.execute(); + } else { + attachment.cancelButton.setVisibility(View.VISIBLE); + } ProgressBar bar = attachment.progressView; bar.setVisibility(View.VISIBLE); bar.setIndeterminate(true); diff --git a/src/com/android/email/service/AttachmentDownloadService.java b/src/com/android/email/service/AttachmentDownloadService.java index 0806a2a98..07fd341dc 100644 --- a/src/com/android/email/service/AttachmentDownloadService.java +++ b/src/com/android/email/service/AttachmentDownloadService.java @@ -492,6 +492,10 @@ public class AttachmentDownloadService extends Service implements Runnable { return mDownloadSet.findDownloadRequest(attachmentId) != null; } + /*package*/ int getSize() { + return mDownloadSet.size(); + } + /*package*/ boolean dequeue(long attachmentId) { DownloadRequest req = mDownloadSet.findDownloadRequest(attachmentId); if (req != null) { @@ -504,6 +508,17 @@ public class AttachmentDownloadService extends Service implements Runnable { return false; } + /** + * Ask the service for the number of items in the download queue + * @return the number of items queued for download + */ + public static int getQueueSize() { + if (sRunningService != null) { + return sRunningService.getSize(); + } + return 0; + } + /** * Ask the service whether a particular attachment is queued for download * @param attachmentId the id of the Attachment (as stored by EmailProvider)