AttachmentDownloadService.java revision 81273dfcee3b075451860f60ee15f2aa06ba81ec
1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.email.service;
18
19import com.android.email.AttachmentInfo;
20import com.android.email.Controller.ControllerService;
21import com.android.email.Email;
22import com.android.email.EmailConnectivityManager;
23import com.android.email.NotificationController;
24import com.android.emailcommon.provider.EmailContent;
25import com.android.emailcommon.provider.EmailContent.Account;
26import com.android.emailcommon.provider.EmailContent.Attachment;
27import com.android.emailcommon.provider.EmailContent.Message;
28import com.android.emailcommon.service.EmailServiceProxy;
29import com.android.emailcommon.service.EmailServiceStatus;
30import com.android.emailcommon.service.IEmailServiceCallback;
31import com.android.emailcommon.utility.AttachmentUtilities;
32import com.android.emailcommon.utility.Utility;
33
34import android.accounts.AccountManager;
35import android.app.AlarmManager;
36import android.app.PendingIntent;
37import android.app.Service;
38import android.content.BroadcastReceiver;
39import android.content.ContentValues;
40import android.content.Context;
41import android.content.Intent;
42import android.database.Cursor;
43import android.net.Uri;
44import android.os.IBinder;
45import android.os.RemoteException;
46import android.text.format.DateUtils;
47import android.util.Log;
48
49import java.io.File;
50import java.io.FileDescriptor;
51import java.io.PrintWriter;
52import java.util.Comparator;
53import java.util.HashMap;
54import java.util.Iterator;
55import java.util.TreeSet;
56import java.util.concurrent.ConcurrentHashMap;
57
58public class AttachmentDownloadService extends Service implements Runnable {
59    public static final String TAG = "AttachmentService";
60
61    // Our idle time, waiting for notifications; this is something of a failsafe
62    private static final int PROCESS_QUEUE_WAIT_TIME = 30 * ((int)DateUtils.MINUTE_IN_MILLIS);
63    // How often our watchdog checks for callback timeouts
64    private static final int WATCHDOG_CHECK_INTERVAL = 15 * ((int)DateUtils.SECOND_IN_MILLIS);
65    // How long we'll wait for a callback before canceling a download and retrying
66    private static final int CALLBACK_TIMEOUT = 30 * ((int)DateUtils.SECOND_IN_MILLIS);
67    // Try to download an attachment in the background this many times before giving up
68    private static final int MAX_DOWNLOAD_RETRIES = 5;
69    private static final int PRIORITY_NONE = -1;
70    @SuppressWarnings("unused")
71    // Low priority will be used for opportunistic downloads
72    private static final int PRIORITY_BACKGROUND = 0;
73    // Normal priority is for forwarded downloads in outgoing mail
74    private static final int PRIORITY_SEND_MAIL = 1;
75    // High priority is for user requests
76    private static final int PRIORITY_FOREGROUND = 2;
77
78    // Minimum free storage in order to perform prefetch (25% of total memory)
79    private static final float PREFETCH_MINIMUM_STORAGE_AVAILABLE = 0.25F;
80    // Maximum prefetch storage (also 25% of total memory)
81    private static final float PREFETCH_MAXIMUM_ATTACHMENT_STORAGE = 0.25F;
82
83    // We can try various values here; I think 2 is completely reasonable as a first pass
84    private static final int MAX_SIMULTANEOUS_DOWNLOADS = 2;
85    // Limit on the number of simultaneous downloads per account
86    // Note that a limit of 1 is currently enforced by both Services (MailService and Controller)
87    private static final int MAX_SIMULTANEOUS_DOWNLOADS_PER_ACCOUNT = 1;
88    // Limit on the number of attachments we'll check for background download
89    private static final int MAX_ATTACHMENTS_TO_CHECK = 25;
90
91    // sRunningService is only set in the UI thread; it's visibility elsewhere is guaranteed
92    // by the use of "volatile"
93    /*package*/ static volatile AttachmentDownloadService sRunningService = null;
94
95    /*package*/ Context mContext;
96    /*package*/ EmailConnectivityManager mConnectivityManager;
97
98    /*package*/ final DownloadSet mDownloadSet = new DownloadSet(new DownloadComparator());
99
100    private final HashMap<Long, Intent> mAccountServiceMap = new HashMap<Long, Intent>();
101    // A map of attachment storage used per account
102    // NOTE: This map is not kept current in terms of deletions (i.e. it stores the last calculated
103    // amount plus the size of any new attachments laoded).  If and when we reach the per-account
104    // limit, we recalculate the actual usage
105    /*package*/ final HashMap<Long, Long> mAttachmentStorageMap = new HashMap<Long, Long>();
106    // A map of attachment ids to the number of failed attempts to download the attachment
107    // NOTE: We do not want to persist this. This allows us to retry background downloading
108    // if any transient network errors are fixed & and the app is restarted
109    /* package */ final HashMap<Long, Integer> mAttachmentFailureMap = new HashMap<Long, Integer>();
110    private final ServiceCallback mServiceCallback = new ServiceCallback();
111
112    private final Object mLock = new Object();
113    private volatile boolean mStop = false;
114
115    /*package*/ AccountManagerStub mAccountManagerStub;
116
117    /**
118     * We only use the getAccounts() call from AccountManager, so this class wraps that call and
119     * allows us to build a mock account manager stub in the unit tests
120     */
121    /*package*/ static class AccountManagerStub {
122        private int mNumberOfAccounts;
123        private final AccountManager mAccountManager;
124
125        AccountManagerStub(Context context) {
126            if (context != null) {
127                mAccountManager = AccountManager.get(context);
128            } else {
129                mAccountManager = null;
130            }
131        }
132
133        /*package*/ int getNumberOfAccounts() {
134            if (mAccountManager != null) {
135                return mAccountManager.getAccounts().length;
136            } else {
137                return mNumberOfAccounts;
138            }
139        }
140
141        /*package*/ void setNumberOfAccounts(int numberOfAccounts) {
142            mNumberOfAccounts = numberOfAccounts;
143        }
144    }
145
146    /**
147     * Watchdog alarm receiver; responsible for making sure that downloads in progress are not
148     * stalled, as determined by the timing of the most recent service callback
149     */
150    public static class Watchdog extends BroadcastReceiver {
151        @Override
152        public void onReceive(final Context context, Intent intent) {
153            new Thread(new Runnable() {
154                public void run() {
155                    watchdogAlarm();
156                }
157            }, "AttachmentDownloadService Watchdog").start();
158        }
159    }
160
161    public static class DownloadRequest {
162        final int priority;
163        final long time;
164        final long attachmentId;
165        final long messageId;
166        final long accountId;
167        boolean inProgress = false;
168        int lastStatusCode;
169        int lastProgress;
170        long lastCallbackTime;
171        long startTime;
172
173        private DownloadRequest(Context context, Attachment attachment) {
174            attachmentId = attachment.mId;
175            Message msg = Message.restoreMessageWithId(context, attachment.mMessageKey);
176            if (msg != null) {
177                accountId = msg.mAccountKey;
178                messageId = msg.mId;
179            } else {
180                accountId = messageId = -1;
181            }
182            priority = getPriority(attachment);
183            time = System.currentTimeMillis();
184        }
185
186        @Override
187        public int hashCode() {
188            return (int)attachmentId;
189        }
190
191        /**
192         * Two download requests are equals if their attachment id's are equals
193         */
194        @Override
195        public boolean equals(Object object) {
196            if (!(object instanceof DownloadRequest)) return false;
197            DownloadRequest req = (DownloadRequest)object;
198            return req.attachmentId == attachmentId;
199        }
200    }
201
202    /**
203     * Comparator class for the download set; we first compare by priority.  Requests with equal
204     * priority are compared by the time the request was created (older requests come first)
205     */
206    /*protected*/ static class DownloadComparator implements Comparator<DownloadRequest> {
207        @Override
208        public int compare(DownloadRequest req1, DownloadRequest req2) {
209            int res;
210            if (req1.priority != req2.priority) {
211                res = (req1.priority < req2.priority) ? -1 : 1;
212            } else {
213                if (req1.time == req2.time) {
214                    res = 0;
215                } else {
216                    res = (req1.time > req2.time) ? -1 : 1;
217                }
218            }
219            return res;
220        }
221    }
222
223    /**
224     * The DownloadSet is a TreeSet sorted by priority class (e.g. low, high, etc.) and the
225     * time of the request.  Higher priority requests
226     * are always processed first; among equals, the oldest request is processed first.  The
227     * priority key represents this ordering.  Note: All methods that change the attachment map are
228     * synchronized on the map itself
229     */
230    /*package*/ class DownloadSet extends TreeSet<DownloadRequest> {
231        private static final long serialVersionUID = 1L;
232        private PendingIntent mWatchdogPendingIntent;
233        private AlarmManager mAlarmManager;
234
235        /*package*/ DownloadSet(Comparator<? super DownloadRequest> comparator) {
236            super(comparator);
237        }
238
239        /**
240         * Maps attachment id to DownloadRequest
241         */
242        /*package*/ final ConcurrentHashMap<Long, DownloadRequest> mDownloadsInProgress =
243            new ConcurrentHashMap<Long, DownloadRequest>();
244
245        /**
246         * onChange is called by the AttachmentReceiver upon receipt of a valid notification from
247         * EmailProvider that an attachment has been inserted or modified.  It's not strictly
248         * necessary that we detect a deleted attachment, as the code always checks for the
249         * existence of an attachment before acting on it.
250         */
251        public synchronized void onChange(Context context, Attachment att) {
252            DownloadRequest req = findDownloadRequest(att.mId);
253            long priority = getPriority(att);
254            if (priority == PRIORITY_NONE) {
255                if (Email.DEBUG) {
256                    Log.d(TAG, "== Attachment changed: " + att.mId);
257                }
258                // In this case, there is no download priority for this attachment
259                if (req != null) {
260                    // If it exists in the map, remove it
261                    // NOTE: We don't yet support deleting downloads in progress
262                    if (Email.DEBUG) {
263                        Log.d(TAG, "== Attachment " + att.mId + " was in queue, removing");
264                    }
265                    remove(req);
266                }
267            } else {
268                // Ignore changes that occur during download
269                if (mDownloadsInProgress.containsKey(att.mId)) return;
270                // If this is new, add the request to the queue
271                if (req == null) {
272                    req = new DownloadRequest(context, att);
273                    add(req);
274                }
275                // If the request already existed, we'll update the priority (so that the time is
276                // up-to-date); otherwise, we create a new request
277                if (Email.DEBUG) {
278                    Log.d(TAG, "== Download queued for attachment " + att.mId + ", class " +
279                            req.priority + ", priority time " + req.time);
280                }
281            }
282            // Process the queue if we're in a wait
283            kick();
284        }
285
286        /**
287         * Find a queued DownloadRequest, given the attachment's id
288         * @param id the id of the attachment
289         * @return the DownloadRequest for that attachment (or null, if none)
290         */
291        /*package*/ synchronized DownloadRequest findDownloadRequest(long id) {
292            Iterator<DownloadRequest> iterator = iterator();
293            while(iterator.hasNext()) {
294                DownloadRequest req = iterator.next();
295                if (req.attachmentId == id) {
296                    return req;
297                }
298            }
299            return null;
300        }
301
302        /**
303         * Run through the AttachmentMap and find DownloadRequests that can be executed, enforcing
304         * the limit on maximum downloads
305         */
306        /*package*/ synchronized void processQueue() {
307            if (Email.DEBUG) {
308                Log.d(TAG, "== Checking attachment queue, " + mDownloadSet.size() + " entries");
309            }
310
311            Iterator<DownloadRequest> iterator = mDownloadSet.descendingIterator();
312            // First, start up any required downloads, in priority order
313            while (iterator.hasNext() &&
314                    (mDownloadsInProgress.size() < MAX_SIMULTANEOUS_DOWNLOADS)) {
315                DownloadRequest req = iterator.next();
316                 // Enforce per-account limit here
317                if (downloadsForAccount(req.accountId) >= MAX_SIMULTANEOUS_DOWNLOADS_PER_ACCOUNT) {
318                    if (Email.DEBUG) {
319                        Log.d(TAG, "== Skip #" + req.attachmentId + "; maxed for acct #" +
320                                req.accountId);
321                    }
322                    continue;
323                }
324
325                if (!req.inProgress) {
326                    mDownloadSet.tryStartDownload(req);
327                }
328            }
329
330            // Don't prefetch if background downloading is disallowed
331            if (!mConnectivityManager.isBackgroundDataAllowed()) return;
332            // Then, try opportunistic download of appropriate attachments
333            int backgroundDownloads = MAX_SIMULTANEOUS_DOWNLOADS - mDownloadsInProgress.size();
334            // Always leave one slot for user requested download
335            if (backgroundDownloads > (MAX_SIMULTANEOUS_DOWNLOADS - 1)) {
336                // We'll load up the newest 25 attachments that aren't loaded or queued
337                Uri lookupUri = EmailContent.uriWithLimit(Attachment.CONTENT_URI,
338                        MAX_ATTACHMENTS_TO_CHECK);
339                Cursor c = mContext.getContentResolver().query(lookupUri, AttachmentInfo.PROJECTION,
340                        EmailContent.Attachment.EMPTY_URI_INBOX_SELECTION,
341                        null, Attachment.RECORD_ID + " DESC");
342                File cacheDir = mContext.getCacheDir();
343                try {
344                    while (c.moveToNext()) {
345                        long accountKey = c.getLong(AttachmentInfo.COLUMN_ACCOUNT_KEY);
346                        long id = c.getLong(AttachmentInfo.COLUMN_ID);
347                        Account account = Account.restoreAccountWithId(mContext, accountKey);
348                        if (account == null) {
349                            // Clean up this orphaned attachment; there's no point in keeping it
350                            // around; then try to find another one
351                            EmailContent.delete(mContext, Attachment.CONTENT_URI, id);
352                        } else if (canPrefetchForAccount(account, cacheDir)) {
353                            // Check that the attachment meets system requirements for download
354                            AttachmentInfo info = new AttachmentInfo(mContext, c);
355                            if (info.isEligibleForDownload()) {
356                                Attachment att = Attachment.restoreAttachmentWithId(mContext, id);
357                                if (att != null) {
358                                    Integer tryCount;
359                                    tryCount = mAttachmentFailureMap.get(att.mId);
360                                    if (tryCount != null && tryCount > MAX_DOWNLOAD_RETRIES) {
361                                        // move onto the next attachment
362                                        continue;
363                                    }
364                                    // Start this download and we're done
365                                    DownloadRequest req = new DownloadRequest(mContext, att);
366                                    mDownloadSet.tryStartDownload(req);
367                                    break;
368                                }
369                            }
370                        }
371                    }
372                } finally {
373                    c.close();
374                }
375            }
376        }
377
378        /**
379         * Count the number of running downloads in progress for this account
380         * @param accountId the id of the account
381         * @return the count of running downloads
382         */
383        /*package*/ synchronized int downloadsForAccount(long accountId) {
384            int count = 0;
385            for (DownloadRequest req: mDownloadsInProgress.values()) {
386                if (req.accountId == accountId) {
387                    count++;
388                }
389            }
390            return count;
391        }
392
393        private void onWatchdogAlarm() {
394            long now = System.currentTimeMillis();
395            for (DownloadRequest req: mDownloadsInProgress.values()) {
396                // Check how long it's been since receiving a callback
397                long timeSinceCallback = now - req.lastCallbackTime;
398                if (timeSinceCallback > CALLBACK_TIMEOUT) {
399                    if (Email.DEBUG) {
400                        Log.d(TAG, "== Download of " + req.attachmentId + " timed out");
401                    }
402                   cancelDownload(req);
403                }
404            }
405            // If there are downloads in progress, reset alarm
406            if (mDownloadsInProgress.isEmpty()) {
407                if (mAlarmManager != null && mWatchdogPendingIntent != null) {
408                    mAlarmManager.cancel(mWatchdogPendingIntent);
409                }
410            }
411            // Check whether we can start new downloads...
412            if (mConnectivityManager.hasConnectivity()) {
413                processQueue();
414            }
415        }
416
417        /**
418         * Attempt to execute the DownloadRequest, enforcing the maximum downloads per account
419         * parameter
420         * @param req the DownloadRequest
421         * @return whether or not the download was started
422         */
423        /*package*/ synchronized boolean tryStartDownload(DownloadRequest req) {
424            Intent intent = getServiceIntentForAccount(req.accountId);
425            if (intent == null) return false;
426
427            // Do not download the same attachment multiple times
428            boolean alreadyInProgress = mDownloadsInProgress.get(req.attachmentId) != null;
429            if (alreadyInProgress) return false;
430
431            try {
432                if (Email.DEBUG) {
433                    Log.d(TAG, ">> Starting download for attachment #" + req.attachmentId);
434                }
435                startDownload(intent, req);
436            } catch (RemoteException e) {
437                // TODO: Consider whether we need to do more in this case...
438                // For now, fix up our data to reflect the failure
439                cancelDownload(req);
440            }
441            return true;
442        }
443
444        private synchronized DownloadRequest getDownloadInProgress(long attachmentId) {
445            return mDownloadsInProgress.get(attachmentId);
446        }
447
448        /**
449         * Do the work of starting an attachment download using the EmailService interface, and
450         * set our watchdog alarm
451         *
452         * @param serviceClass the class that will attempt the download
453         * @param req the DownloadRequest
454         * @throws RemoteException
455         */
456        private void startDownload(Intent intent, DownloadRequest req)
457                throws RemoteException {
458            req.startTime = System.currentTimeMillis();
459            req.inProgress = true;
460            mDownloadsInProgress.put(req.attachmentId, req);
461            EmailServiceProxy proxy =
462                new EmailServiceProxy(mContext, intent, mServiceCallback);
463            proxy.loadAttachment(req.attachmentId, req.priority != PRIORITY_FOREGROUND);
464            // Lazily initialize our (reusable) pending intent
465            if (mWatchdogPendingIntent == null) {
466                createWatchdogPendingIntent(mContext);
467            }
468            // Set the alarm
469            mAlarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
470                    System.currentTimeMillis() + WATCHDOG_CHECK_INTERVAL, WATCHDOG_CHECK_INTERVAL,
471                    mWatchdogPendingIntent);
472        }
473
474        /*package*/ void createWatchdogPendingIntent(Context context) {
475            Intent alarmIntent = new Intent(context, Watchdog.class);
476            mWatchdogPendingIntent = PendingIntent.getBroadcast(context, 0, alarmIntent, 0);
477            mAlarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
478        }
479        private void cancelDownload(DownloadRequest req) {
480            mDownloadsInProgress.remove(req.attachmentId);
481            req.inProgress = false;
482        }
483
484        /**
485         * Called when a download is finished; we get notified of this via our EmailServiceCallback
486         * @param attachmentId the id of the attachment whose download is finished
487         * @param statusCode the EmailServiceStatus code returned by the Service
488         */
489        /*package*/ synchronized void endDownload(long attachmentId, int statusCode) {
490            // Say we're no longer downloading this
491            mDownloadsInProgress.remove(attachmentId);
492
493            // TODO: This code is conservative and treats connection issues as failures.
494            // Since we have no mechanism to throttle reconnection attempts, it makes
495            // sense to be cautious here. Once logic is in place to prevent connecting
496            // in a tight loop, we can exclude counting connection issues as "failures".
497
498            // Update the attachment failure list if needed
499            Integer downloadCount;
500            downloadCount = mAttachmentFailureMap.remove(attachmentId);
501            if (statusCode != EmailServiceStatus.SUCCESS) {
502                if (downloadCount == null) {
503                    downloadCount = 0;
504                }
505                downloadCount += 1;
506                mAttachmentFailureMap.put(attachmentId, downloadCount);
507            }
508
509            DownloadRequest req = mDownloadSet.findDownloadRequest(attachmentId);
510            if (statusCode == EmailServiceStatus.CONNECTION_ERROR) {
511                // If this needs to be retried, just process the queue again
512                if (Email.DEBUG) {
513                    Log.d(TAG, "== The download for attachment #" + attachmentId +
514                            " will be retried");
515                }
516                if (req != null) {
517                    req.inProgress = false;
518                }
519                kick();
520                return;
521            }
522
523            // If the request is still in the queue, remove it
524            if (req != null) {
525                remove(req);
526            }
527            if (Email.DEBUG) {
528                long secs = 0;
529                if (req != null) {
530                    secs = (System.currentTimeMillis() - req.time) / 1000;
531                }
532                String status = (statusCode == EmailServiceStatus.SUCCESS) ? "Success" :
533                    "Error " + statusCode;
534                Log.d(TAG, "<< Download finished for attachment #" + attachmentId + "; " + secs +
535                           " seconds from request, status: " + status);
536            }
537
538            Attachment attachment = Attachment.restoreAttachmentWithId(mContext, attachmentId);
539            if (attachment != null) {
540                long accountId = attachment.mAccountKey;
541                // Update our attachment storage for this account
542                Long currentStorage = mAttachmentStorageMap.get(accountId);
543                if (currentStorage == null) {
544                    currentStorage = 0L;
545                }
546                mAttachmentStorageMap.put(accountId, currentStorage + attachment.mSize);
547                boolean deleted = false;
548                if ((attachment.mFlags & Attachment.FLAG_DOWNLOAD_FORWARD) != 0) {
549                    if (statusCode == EmailServiceStatus.ATTACHMENT_NOT_FOUND) {
550                        // If this is a forwarding download, and the attachment doesn't exist (or
551                        // can't be downloaded) delete it from the outgoing message, lest that
552                        // message never get sent
553                        EmailContent.delete(mContext, Attachment.CONTENT_URI, attachment.mId);
554                        // TODO: Talk to UX about whether this is even worth doing
555                        NotificationController nc = NotificationController.getInstance(mContext);
556                        nc.showDownloadForwardFailedNotification(attachment);
557                        deleted = true;
558                    }
559                    // If we're an attachment on forwarded mail, and if we're not still blocked,
560                    // try to send pending mail now (as mediated by MailService)
561                    if ((req != null) &&
562                            !Utility.hasUnloadedAttachments(mContext, attachment.mMessageKey)) {
563                        if (Email.DEBUG) {
564                            Log.d(TAG, "== Downloads finished for outgoing msg #" + req.messageId);
565                        }
566                        MailService.actionSendPendingMail(mContext, req.accountId);
567                    }
568                }
569                if (statusCode == EmailServiceStatus.MESSAGE_NOT_FOUND) {
570                    // If there's no associated message, delete the attachment
571                    EmailContent.delete(mContext, Attachment.CONTENT_URI, attachment.mId);
572                } else if (!deleted) {
573                    // Clear the download flags, since we're done for now.  Note that this happens
574                    // only for non-recoverable errors.  When these occur for forwarded mail, we can
575                    // ignore it and continue; otherwise, it was either 1) a user request, in which
576                    // case the user can retry manually or 2) an opportunistic download, in which
577                    // case the download wasn't critical
578                    ContentValues cv = new ContentValues();
579                    int flags =
580                        Attachment.FLAG_DOWNLOAD_FORWARD | Attachment.FLAG_DOWNLOAD_USER_REQUEST;
581                    cv.put(Attachment.FLAGS, attachment.mFlags &= ~flags);
582                    attachment.update(mContext, cv);
583                }
584            }
585            // Process the queue
586            kick();
587        }
588    }
589
590    /**
591     * Calculate the download priority of an Attachment.  A priority of zero means that the
592     * attachment is not marked for download.
593     * @param att the Attachment
594     * @return the priority key of the Attachment
595     */
596    private static int getPriority(Attachment att) {
597        int priorityClass = PRIORITY_NONE;
598        int flags = att.mFlags;
599        if ((flags & Attachment.FLAG_DOWNLOAD_FORWARD) != 0) {
600            priorityClass = PRIORITY_SEND_MAIL;
601        } else if ((flags & Attachment.FLAG_DOWNLOAD_USER_REQUEST) != 0) {
602            priorityClass = PRIORITY_FOREGROUND;
603        }
604        return priorityClass;
605    }
606
607    private void kick() {
608        synchronized(mLock) {
609            mLock.notify();
610        }
611    }
612
613    /**
614     * We use an EmailServiceCallback to keep track of the progress of downloads.  These callbacks
615     * come from either Controller (IMAP) or ExchangeService (EAS).  Note that we only implement the
616     * single callback that's defined by the EmailServiceCallback interface.
617     */
618    private class ServiceCallback extends IEmailServiceCallback.Stub {
619        public void loadAttachmentStatus(long messageId, long attachmentId, int statusCode,
620                int progress) {
621            // Record status and progress
622            DownloadRequest req = mDownloadSet.getDownloadInProgress(attachmentId);
623            if (req != null) {
624                if (Email.DEBUG) {
625                    String code;
626                    switch(statusCode) {
627                        case EmailServiceStatus.SUCCESS: code = "Success"; break;
628                        case EmailServiceStatus.IN_PROGRESS: code = "In progress"; break;
629                        default: code = Integer.toString(statusCode); break;
630                    }
631                    if (statusCode != EmailServiceStatus.IN_PROGRESS) {
632                        Log.d(TAG, ">> Attachment " + attachmentId + ": " + code);
633                    } else if (progress >= (req.lastProgress + 15)) {
634                        Log.d(TAG, ">> Attachment " + attachmentId + ": " + progress + "%");
635                    }
636                }
637                req.lastStatusCode = statusCode;
638                req.lastProgress = progress;
639                req.lastCallbackTime = System.currentTimeMillis();
640            }
641            switch (statusCode) {
642                case EmailServiceStatus.IN_PROGRESS:
643                    break;
644                default:
645                    mDownloadSet.endDownload(attachmentId, statusCode);
646                    break;
647            }
648        }
649
650        @Override
651        public void sendMessageStatus(long messageId, String subject, int statusCode, int progress)
652                throws RemoteException {
653        }
654
655        @Override
656        public void syncMailboxListStatus(long accountId, int statusCode, int progress)
657                throws RemoteException {
658        }
659
660        @Override
661        public void syncMailboxStatus(long mailboxId, int statusCode, int progress)
662                throws RemoteException {
663        }
664    }
665
666    /**
667     * Return an Intent to be used used based on the account type of the provided account id.  We
668     * cache the results to avoid repeated database access
669     * @param accountId the id of the account
670     * @return the Intent to be used for the account or null (if the account no longer exists)
671     */
672    private synchronized Intent getServiceIntentForAccount(long accountId) {
673        // TODO: We should have some more data-driven way of determining the service intent.
674        Intent serviceIntent = mAccountServiceMap.get(accountId);
675        if (serviceIntent == null) {
676            String protocol = Account.getProtocol(mContext, accountId);
677            if (protocol == null) return null;
678            serviceIntent = new Intent(mContext, ControllerService.class);
679            if (protocol.equals("eas")) {
680                serviceIntent = new Intent(EmailServiceProxy.EXCHANGE_INTENT);
681            }
682            mAccountServiceMap.put(accountId, serviceIntent);
683        }
684        return serviceIntent;
685    }
686
687    /*package*/ void addServiceIntentForTest(long accountId, Intent intent) {
688        mAccountServiceMap.put(accountId, intent);
689    }
690
691    /*package*/ void onChange(Attachment att) {
692        mDownloadSet.onChange(this, att);
693    }
694
695    /*package*/ boolean isQueued(long attachmentId) {
696        return mDownloadSet.findDownloadRequest(attachmentId) != null;
697    }
698
699    /*package*/ int getSize() {
700        return mDownloadSet.size();
701    }
702
703    /*package*/ boolean dequeue(long attachmentId) {
704        DownloadRequest req = mDownloadSet.findDownloadRequest(attachmentId);
705        if (req != null) {
706            if (Email.DEBUG) {
707                Log.d(TAG, "Dequeued attachmentId:  " + attachmentId);
708            }
709            mDownloadSet.remove(req);
710            return true;
711        }
712        return false;
713    }
714
715    /**
716     * Ask the service for the number of items in the download queue
717     * @return the number of items queued for download
718     */
719    public static int getQueueSize() {
720        AttachmentDownloadService service = sRunningService;
721        if (service != null) {
722            return service.getSize();
723        }
724        return 0;
725    }
726
727    /**
728     * Ask the service whether a particular attachment is queued for download
729     * @param attachmentId the id of the Attachment (as stored by EmailProvider)
730     * @return whether or not the attachment is queued for download
731     */
732    public static boolean isAttachmentQueued(long attachmentId) {
733        AttachmentDownloadService service = sRunningService;
734        if (service != null) {
735            return service.isQueued(attachmentId);
736        }
737        return false;
738    }
739
740    /**
741     * Ask the service to remove an attachment from the download queue
742     * @param attachmentId the id of the Attachment (as stored by EmailProvider)
743     * @return whether or not the attachment was removed from the queue
744     */
745    public static boolean cancelQueuedAttachment(long attachmentId) {
746        AttachmentDownloadService service = sRunningService;
747        if (service != null) {
748            return service.dequeue(attachmentId);
749        }
750        return false;
751    }
752
753    public static void watchdogAlarm() {
754        AttachmentDownloadService service = sRunningService;
755        if (service != null) {
756            service.mDownloadSet.onWatchdogAlarm();
757        }
758    }
759
760    /**
761     * Called directly by EmailProvider whenever an attachment is inserted or changed
762     * @param id the attachment's id
763     * @param flags the new flags for the attachment
764     */
765    public static void attachmentChanged(final long id, final int flags) {
766        final AttachmentDownloadService service = sRunningService;
767        if (service == null) return;
768        Utility.runAsync(new Runnable() {
769            public void run() {
770                final Attachment attachment =
771                    Attachment.restoreAttachmentWithId(service, id);
772                if (attachment != null) {
773                    // Store the flags we got from EmailProvider; given that all of this
774                    // activity is asynchronous, we need to use the newest data from
775                    // EmailProvider
776                    attachment.mFlags = flags;
777                    service.onChange(attachment);
778                }
779            }});
780    }
781
782    /**
783     * Determine whether an attachment can be prefetched for the given account
784     * @return true if download is allowed, false otherwise
785     */
786    public boolean canPrefetchForAccount(Account account, File dir) {
787        // Check account, just in case
788        if (account == null) return false;
789        // First, check preference and quickly return if prefetch isn't allowed
790        if ((account.mFlags & Account.FLAGS_BACKGROUND_ATTACHMENTS) == 0) return false;
791
792        long totalStorage = dir.getTotalSpace();
793        long usableStorage = dir.getUsableSpace();
794        long minAvailable = (long)(totalStorage * PREFETCH_MINIMUM_STORAGE_AVAILABLE);
795
796        // If there's not enough overall storage available, stop now
797        if (usableStorage < minAvailable) {
798            return false;
799        }
800
801        int numberOfAccounts = mAccountManagerStub.getNumberOfAccounts();
802        long perAccountMaxStorage =
803            (long)(totalStorage * PREFETCH_MAXIMUM_ATTACHMENT_STORAGE / numberOfAccounts);
804
805        // Retrieve our idea of currently used attachment storage; since we don't track deletions,
806        // this number is the "worst case".  If the number is greater than what's allowed per
807        // account, we walk the directory to determine the actual number
808        Long accountStorage = mAttachmentStorageMap.get(account.mId);
809        if (accountStorage == null || (accountStorage > perAccountMaxStorage)) {
810            // Calculate the exact figure for attachment storage for this account
811            accountStorage = 0L;
812            File[] files = dir.listFiles();
813            if (files != null) {
814                for (File file : files) {
815                    accountStorage += file.length();
816                }
817            }
818            // Cache the value
819            mAttachmentStorageMap.put(account.mId, accountStorage);
820        }
821
822        // Return true if we're using less than the maximum per account
823        if (accountStorage < perAccountMaxStorage) {
824            return true;
825        } else {
826            if (Email.DEBUG) {
827                Log.d(TAG, ">> Prefetch not allowed for account " + account.mId + "; used " +
828                        accountStorage + ", limit " + perAccountMaxStorage);
829            }
830            return false;
831        }
832    }
833
834    public void run() {
835        // These fields are only used within the service thread
836        mContext = this;
837        mConnectivityManager = new EmailConnectivityManager(this, TAG);
838        mAccountManagerStub = new AccountManagerStub(this);
839
840        // Run through all attachments in the database that require download and add them to
841        // the queue
842        int mask = Attachment.FLAG_DOWNLOAD_FORWARD | Attachment.FLAG_DOWNLOAD_USER_REQUEST;
843        Cursor c = getContentResolver().query(Attachment.CONTENT_URI,
844                EmailContent.ID_PROJECTION, "(" + Attachment.FLAGS + " & ?) != 0",
845                new String[] {Integer.toString(mask)}, null);
846        try {
847            Log.d(TAG, "Count: " + c.getCount());
848            while (c.moveToNext()) {
849                Attachment attachment = Attachment.restoreAttachmentWithId(
850                        this, c.getLong(EmailContent.ID_PROJECTION_COLUMN));
851                if (attachment != null) {
852                    mDownloadSet.onChange(this, attachment);
853                }
854            }
855        } catch (Exception e) {
856            e.printStackTrace();
857        }
858        finally {
859            c.close();
860        }
861
862        // Loop until stopped, with a 30 minute wait loop
863        while (!mStop) {
864            // Here's where we run our attachment loading logic...
865            mConnectivityManager.waitForConnectivity();
866            mDownloadSet.processQueue();
867            synchronized(mLock) {
868                try {
869                    mLock.wait(PROCESS_QUEUE_WAIT_TIME);
870                } catch (InterruptedException e) {
871                    // That's ok; we'll just keep looping
872                }
873            }
874        }
875
876        // Unregister now that we're done
877        mConnectivityManager.unregister();
878    }
879
880    @Override
881    public int onStartCommand(Intent intent, int flags, int startId) {
882        sRunningService = this;
883        return Service.START_STICKY;
884    }
885
886    /**
887     * The lifecycle of this service is managed by Email.setServicesEnabled(), which is called
888     * throughout the code, in particular 1) after boot and 2) after accounts are added or removed
889     * The goal is that this service should be running at all times when there's at least one
890     * email account present.
891     */
892    @Override
893    public void onCreate() {
894        // Start up our service thread
895        new Thread(this, "AttachmentDownloadService").start();
896    }
897    @Override
898    public IBinder onBind(Intent intent) {
899        return null;
900    }
901
902    @Override
903    public void onDestroy() {
904        Log.d(TAG, "**** ON DESTROY!");
905        if (sRunningService != null) {
906            mStop = true;
907            kick();
908        }
909        sRunningService = null;
910    }
911
912    @Override
913    public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
914        pw.println("AttachmentDownloadService");
915        long time = System.currentTimeMillis();
916        synchronized(mDownloadSet) {
917            pw.println("  Queue, " + mDownloadSet.size() + " entries");
918            Iterator<DownloadRequest> iterator = mDownloadSet.descendingIterator();
919            // First, start up any required downloads, in priority order
920            while (iterator.hasNext()) {
921                DownloadRequest req = iterator.next();
922                pw.println("    Account: " + req.accountId + ", Attachment: " + req.attachmentId);
923                pw.println("      Priority: " + req.priority + ", Time: " + req.time +
924                        (req.inProgress ? " [In progress]" : ""));
925                Attachment att = Attachment.restoreAttachmentWithId(this, req.attachmentId);
926                if (att == null) {
927                    pw.println("      Attachment not in database?");
928                } else if (att.mFileName != null) {
929                    String fileName = att.mFileName;
930                    String suffix = "[none]";
931                    int lastDot = fileName.lastIndexOf('.');
932                    if (lastDot >= 0) {
933                        suffix = fileName.substring(lastDot);
934                    }
935                    pw.print("      Suffix: " + suffix);
936                    if (att.mContentUri != null) {
937                        pw.print(" ContentUri: " + att.mContentUri);
938                    }
939                    pw.print(" Mime: ");
940                    if (att.mMimeType != null) {
941                        pw.print(att.mMimeType);
942                    } else {
943                        pw.print(AttachmentUtilities.inferMimeType(fileName, null));
944                        pw.print(" [inferred]");
945                    }
946                    pw.println(" Size: " + att.mSize);
947                }
948                if (req.inProgress) {
949                    pw.println("      Status: " + req.lastStatusCode + ", Progress: " +
950                            req.lastProgress);
951                    pw.println("      Started: " + req.startTime + ", Callback: " +
952                            req.lastCallbackTime);
953                    pw.println("      Elapsed: " + ((time - req.startTime) / 1000L) + "s");
954                    if (req.lastCallbackTime > 0) {
955                        pw.println("      CB: " + ((time - req.lastCallbackTime) / 1000L) + "s");
956                    }
957                }
958            }
959        }
960    }
961}
962