AttachmentDownloadService.java revision 3bbc690600d5fe697f02b739d4fe18f0f9f03314
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.Controller.ControllerService;
20import com.android.email.Email;
21import com.android.email.ExchangeUtils.NullEmailService;
22import com.android.email.NotificationController;
23import com.android.email.Utility;
24import com.android.email.provider.AttachmentProvider;
25import com.android.email.provider.EmailContent;
26import com.android.email.provider.EmailContent.Account;
27import com.android.email.provider.EmailContent.Attachment;
28import com.android.email.provider.EmailContent.Message;
29import com.android.exchange.ExchangeService;
30
31import android.app.AlarmManager;
32import android.app.PendingIntent;
33import android.app.Service;
34import android.content.BroadcastReceiver;
35import android.content.ContentValues;
36import android.content.Context;
37import android.content.Intent;
38import android.database.Cursor;
39import android.os.IBinder;
40import android.os.RemoteException;
41import android.text.format.DateUtils;
42import android.util.Log;
43
44import java.io.File;
45import java.io.FileDescriptor;
46import java.io.PrintWriter;
47import java.util.Comparator;
48import java.util.HashMap;
49import java.util.Iterator;
50import java.util.TreeSet;
51import java.util.concurrent.ConcurrentHashMap;
52
53public class AttachmentDownloadService extends Service implements Runnable {
54    public static final String TAG = "AttachmentService";
55
56    // Our idle time, waiting for notifications; this is something of a failsafe
57    private static final int PROCESS_QUEUE_WAIT_TIME = 30 * ((int)DateUtils.MINUTE_IN_MILLIS);
58    // How often our watchdog checks for callback timeouts
59    private static final int WATCHDOG_CHECK_INTERVAL = 15 * ((int)DateUtils.SECOND_IN_MILLIS);
60    // How long we'll wait for a callback before canceling a download and retrying
61    private static final int CALLBACK_TIMEOUT = 30 * ((int)DateUtils.SECOND_IN_MILLIS);
62
63    private static final int PRIORITY_NONE = -1;
64    @SuppressWarnings("unused")
65    // Low priority will be used for opportunistic downloads
66    private static final int PRIORITY_LOW = 0;
67    // Normal priority is for forwarded downloads in outgoing mail
68    private static final int PRIORITY_NORMAL = 1;
69    // High priority is for user requests
70    private static final int PRIORITY_HIGH = 2;
71
72    // We can try various values here; I think 2 is completely reasonable as a first pass
73    private static final int MAX_SIMULTANEOUS_DOWNLOADS = 2;
74    // Limit on the number of simultaneous downloads per account
75    // Note that a limit of 1 is currently enforced by both Services (MailService and Controller)
76    private static final int MAX_SIMULTANEOUS_DOWNLOADS_PER_ACCOUNT = 1;
77
78    /*package*/ static AttachmentDownloadService sRunningService = null;
79
80    /*package*/ Context mContext;
81    /*package*/ final DownloadSet mDownloadSet = new DownloadSet(new DownloadComparator());
82
83    private final HashMap<Long, Class<? extends Service>> mAccountServiceMap =
84        new HashMap<Long, Class<? extends Service>>();
85    private final ServiceCallback mServiceCallback = new ServiceCallback();
86    private final Object mLock = new Object();
87    private volatile boolean mStop = false;
88
89
90    /**
91     * Watchdog alarm receiver; responsible for making sure that downloads in progress are not
92     * stalled, as determined by the timing of the most recent service callback
93     */
94    public static class Watchdog extends BroadcastReceiver {
95        @Override
96        public void onReceive(final Context context, Intent intent) {
97            new Thread(new Runnable() {
98                public void run() {
99                    watchdogAlarm();
100                }
101            }, "AttachmentDownloadService Watchdog").start();
102        }
103    }
104
105    public static class DownloadRequest {
106        final int priority;
107        final long time;
108        final long attachmentId;
109        final long messageId;
110        final long accountId;
111        boolean inProgress = false;
112        int lastStatusCode;
113        int lastProgress;
114        long lastCallbackTime;
115        long startTime;
116
117        private DownloadRequest(Context context, Attachment attachment) {
118            attachmentId = attachment.mId;
119            Message msg = Message.restoreMessageWithId(context, attachment.mMessageKey);
120            if (msg != null) {
121                accountId = msg.mAccountKey;
122                messageId = msg.mId;
123            } else {
124                accountId = messageId = -1;
125            }
126            priority = getPriority(attachment);
127            time = System.currentTimeMillis();
128        }
129
130        @Override
131        public int hashCode() {
132            return (int)attachmentId;
133        }
134
135        /**
136         * Two download requests are equals if their attachment id's are equals
137         */
138        @Override
139        public boolean equals(Object object) {
140            if (!(object instanceof DownloadRequest)) return false;
141            DownloadRequest req = (DownloadRequest)object;
142            return req.attachmentId == attachmentId;
143        }
144    }
145
146    /**
147     * Comparator class for the download set; we first compare by priority.  Requests with equal
148     * priority are compared by the time the request was created (older requests come first)
149     */
150    /*protected*/ static class DownloadComparator implements Comparator<DownloadRequest> {
151        @Override
152        public int compare(DownloadRequest req1, DownloadRequest req2) {
153            int res;
154            if (req1.priority != req2.priority) {
155                res = (req1.priority < req2.priority) ? -1 : 1;
156            } else {
157                if (req1.time == req2.time) {
158                    res = 0;
159                } else {
160                    res = (req1.time > req2.time) ? -1 : 1;
161                }
162            }
163            //Log.d(TAG, "Compare " + req1.attachmentId + " to " + req2.attachmentId + " = " + res);
164            return res;
165        }
166    }
167
168    /**
169     * The DownloadSet is a TreeSet sorted by priority class (e.g. low, high, etc.) and the
170     * time of the request.  Higher priority requests
171     * are always processed first; among equals, the oldest request is processed first.  The
172     * priority key represents this ordering.  Note: All methods that change the attachment map are
173     * synchronized on the map itself
174     */
175    /*package*/ class DownloadSet extends TreeSet<DownloadRequest> {
176        private static final long serialVersionUID = 1L;
177        private PendingIntent mWatchdogPendingIntent;
178        private AlarmManager mAlarmManager;
179
180        /*package*/ DownloadSet(Comparator<? super DownloadRequest> comparator) {
181            super(comparator);
182        }
183
184        /**
185         * Maps attachment id to DownloadRequest
186         */
187        /*package*/ final ConcurrentHashMap<Long, DownloadRequest> mDownloadsInProgress =
188            new ConcurrentHashMap<Long, DownloadRequest>();
189
190        /**
191         * onChange is called by the AttachmentReceiver upon receipt of a valid notification from
192         * EmailProvider that an attachment has been inserted or modified.  It's not strictly
193         * necessary that we detect a deleted attachment, as the code always checks for the
194         * existence of an attachment before acting on it.
195         */
196        public synchronized void onChange(Attachment att) {
197            DownloadRequest req = findDownloadRequest(att.mId);
198            long priority = getPriority(att);
199            if (priority == PRIORITY_NONE) {
200                if (Email.DEBUG) {
201                    Log.d(TAG, "== Attachment changed: " + att.mId);
202                }
203                // In this case, there is no download priority for this attachment
204                if (req != null) {
205                    // If it exists in the map, remove it
206                    // NOTE: We don't yet support deleting downloads in progress
207                    if (Email.DEBUG) {
208                        Log.d(TAG, "== Attachment " + att.mId + " was in queue, removing");
209                    }
210                    remove(req);
211                }
212            } else {
213                // Ignore changes that occur during download
214                if (mDownloadsInProgress.containsKey(att.mId)) return;
215                // If this is new, add the request to the queue
216                if (req == null) {
217                    req = new DownloadRequest(mContext, att);
218                    add(req);
219                }
220                // If the request already existed, we'll update the priority (so that the time is
221                // up-to-date); otherwise, we create a new request
222                if (Email.DEBUG) {
223                    Log.d(TAG, "== Download queued for attachment " + att.mId + ", class " +
224                            req.priority + ", priority time " + req.time);
225                }
226            }
227            // Process the queue if we're in a wait
228            kick();
229        }
230
231        /**
232         * Find a queued DownloadRequest, given the attachment's id
233         * @param id the id of the attachment
234         * @return the DownloadRequest for that attachment (or null, if none)
235         */
236        /*package*/ synchronized DownloadRequest findDownloadRequest(long id) {
237            Iterator<DownloadRequest> iterator = iterator();
238            while(iterator.hasNext()) {
239                DownloadRequest req = iterator.next();
240                if (req.attachmentId == id) {
241                    return req;
242                }
243            }
244            return null;
245        }
246
247        /**
248         * Run through the AttachmentMap and find DownloadRequests that can be executed, enforcing
249         * the limit on maximum downloads
250         */
251        /*package*/ synchronized void processQueue() {
252            if (Email.DEBUG) {
253                Log.d(TAG, "== Checking attachment queue, " + mDownloadSet.size() + " entries");
254            }
255            Iterator<DownloadRequest> iterator = mDownloadSet.descendingIterator();
256            // First, start up any required downloads, in priority order
257            while (iterator.hasNext() &&
258                    (mDownloadsInProgress.size() < MAX_SIMULTANEOUS_DOWNLOADS)) {
259                DownloadRequest req = iterator.next();
260                if (!req.inProgress) {
261                    mDownloadSet.tryStartDownload(req);
262                }
263            }
264            // Then, try opportunistic download of appropriate attachments
265            int backgroundDownloads = MAX_SIMULTANEOUS_DOWNLOADS - mDownloadsInProgress.size();
266            if (backgroundDownloads > 0) {
267                // TODO Code for background downloads here
268                if (Email.DEBUG) {
269                    Log.d(TAG, "== We'd look for up to " + backgroundDownloads +
270                            " background download(s) now...");
271                }
272            }
273        }
274
275        /**
276         * Count the number of running downloads in progress for this account
277         * @param accountId the id of the account
278         * @return the count of running downloads
279         */
280        /*package*/ synchronized int downloadsForAccount(long accountId) {
281            int count = 0;
282            for (DownloadRequest req: mDownloadsInProgress.values()) {
283                if (req.accountId == accountId) {
284                    count++;
285                }
286            }
287            return count;
288        }
289
290        private void onWatchdogAlarm() {
291            long now = System.currentTimeMillis();
292            for (DownloadRequest req: mDownloadsInProgress.values()) {
293                // Check how long it's been since receiving a callback
294                long timeSinceCallback = now - req.lastCallbackTime;
295                if (timeSinceCallback > CALLBACK_TIMEOUT) {
296                    if (Email.DEBUG) {
297                        Log.d(TAG, "== ,  Download of " + req.attachmentId +
298                                " timed out");
299                    }
300                    mDownloadsInProgress.remove(req);
301                // STOPSHIP Remove this before ship
302                } else if (Email.DEBUG) {
303                    Log.d(TAG, "== ,  Download of " + req.attachmentId +
304                    " last callback " + (timeSinceCallback/1000) + "  secs ago");
305                }
306            }
307            // If there are downloads in progress, reset alarm
308            if (mDownloadsInProgress.isEmpty()) {
309                if (mAlarmManager != null && mWatchdogPendingIntent != null) {
310                    mAlarmManager.cancel(mWatchdogPendingIntent);
311                }
312            }
313            // Check whether we can start new downloads...
314            processQueue();
315        }
316
317        /**
318         * Do the work of starting an attachment download using the EmailService interface, and
319         * set our watchdog alarm
320         *
321         * @param serviceClass the class that will attempt the download
322         * @param req the DownloadRequest
323         * @throws RemoteException
324         */
325        private void startDownload(Class<? extends Service> serviceClass, DownloadRequest req)
326                throws RemoteException {
327            File file = AttachmentProvider.getAttachmentFilename(mContext, req.accountId,
328                    req.attachmentId);
329            req.startTime = System.currentTimeMillis();
330            req.inProgress = true;
331            mDownloadsInProgress.put(req.attachmentId, req);
332            if (serviceClass.equals(NullEmailService.class)) return;
333            // Now, call the service
334            EmailServiceProxy proxy =
335                new EmailServiceProxy(mContext, serviceClass, mServiceCallback);
336            proxy.loadAttachment(req.attachmentId, file.getAbsolutePath(),
337                    AttachmentProvider.getAttachmentUri(req.accountId, req.attachmentId)
338                    .toString());
339            // Lazily initialize our (reusable) pending intent
340            if (mWatchdogPendingIntent == null) {
341                Intent alarmIntent = new Intent(mContext, Watchdog.class);
342                mWatchdogPendingIntent = PendingIntent.getBroadcast(mContext, 0, alarmIntent, 0);
343                mAlarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
344            }
345            // Set the alarm
346            mAlarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
347                    System.currentTimeMillis() + WATCHDOG_CHECK_INTERVAL, WATCHDOG_CHECK_INTERVAL,
348                    mWatchdogPendingIntent);
349        }
350
351        /**
352         * Attempt to execute the DownloadRequest, enforcing the maximum downloads per account
353         * parameter
354         * @param req the DownloadRequest
355         * @return whether or not the download was started
356         */
357        /*package*/ synchronized boolean tryStartDownload(DownloadRequest req) {
358            // Enforce per-account limit
359            if (downloadsForAccount(req.accountId) >= MAX_SIMULTANEOUS_DOWNLOADS_PER_ACCOUNT) {
360                if (Email.DEBUG) {
361                    Log.d(TAG, "== Skip #" + req.attachmentId + "; maxed for acct #" +
362                            req.accountId);
363                }
364                return false;
365            }
366            Class<? extends Service> serviceClass = getServiceClassForAccount(req.accountId);
367            if (serviceClass == null) return false;
368            try {
369                if (Email.DEBUG) {
370                    Log.d(TAG, ">> Starting download for attachment #" + req.attachmentId);
371                }
372                startDownload(serviceClass, req);
373            } catch (RemoteException e) {
374                // TODO: Consider whether we need to do more in this case...
375                // For now, fix up our data to reflect the failure
376                mDownloadsInProgress.remove(req.attachmentId);
377                req.inProgress = false;
378            }
379            return true;
380        }
381
382        /**
383         * Called when a download is finished; we get notified of this via our EmailServiceCallback
384         * @param attachmentId the id of the attachment whose download is finished
385         * @param statusCode the EmailServiceStatus code returned by the Service
386         */
387        /*package*/ synchronized void endDownload(long attachmentId, int statusCode) {
388            // Say we're no longer downloading this
389            mDownloadsInProgress.remove(attachmentId);
390            DownloadRequest req = mDownloadSet.findDownloadRequest(attachmentId);
391            if (statusCode == EmailServiceStatus.CONNECTION_ERROR) {
392                // If this needs to be retried, just process the queue again
393                if (Email.DEBUG) {
394                    Log.d(TAG, "== The download for attachment #" + attachmentId +
395                            " will be retried");
396                }
397                if (req != null) {
398                    req.inProgress = false;
399                }
400                kick();
401                return;
402            }
403
404            // Remove the request from the queue
405            remove(req);
406            if (Email.DEBUG) {
407                long secs = 0;
408                if (req != null) {
409                    secs = (System.currentTimeMillis() - req.time) / 1000;
410                }
411                String status = (statusCode == EmailServiceStatus.SUCCESS) ? "Success" :
412                    "Error " + statusCode;
413                Log.d(TAG, "<< Download finished for attachment #" + attachmentId + "; " + secs +
414                           " seconds from request, status: " + status);
415            }
416
417            Attachment attachment = Attachment.restoreAttachmentWithId(mContext, attachmentId);
418            if (attachment != null) {
419                boolean deleted = false;
420                if ((attachment.mFlags & Attachment.FLAG_DOWNLOAD_FORWARD) != 0) {
421                    if (statusCode == EmailServiceStatus.ATTACHMENT_NOT_FOUND) {
422                        // If this is a forwarding download, and the attachment doesn't exist (or
423                        // can't be downloaded) delete it from the outgoing message, lest that
424                        // message never get sent
425                        EmailContent.delete(mContext, Attachment.CONTENT_URI, attachment.mId);
426                        // TODO: Talk to UX about whether this is even worth doing
427                        NotificationController nc = NotificationController.getInstance(mContext);
428                        nc.showDownloadForwardFailedNotification(attachment);
429                        deleted = true;
430                    }
431                    // If we're an attachment on forwarded mail, and if we're not still blocked,
432                    // try to send pending mail now (as mediated by MailService)
433                    if ((req != null) &&
434                            !Utility.hasUnloadedAttachments(mContext, attachment.mMessageKey)) {
435                        if (Email.DEBUG) {
436                            Log.d(TAG, "== Downloads finished for outgoing msg #" + req.messageId);
437                        }
438                        MailService.actionSendPendingMail(mContext, req.accountId);
439                    }
440                }
441                if (!deleted) {
442                    // Clear the download flags, since we're done for now.  Note that this happens
443                    // only for non-recoverable errors.  When these occur for forwarded mail, we can
444                    // ignore it and continue; otherwise, it was either 1) a user request, in which
445                    // case the user can retry manually or 2) an opportunistic download, in which
446                    // case the download wasn't critical
447                    ContentValues cv = new ContentValues();
448                    int flags =
449                        Attachment.FLAG_DOWNLOAD_FORWARD | Attachment.FLAG_DOWNLOAD_USER_REQUEST;
450                    cv.put(Attachment.FLAGS, attachment.mFlags &= ~flags);
451                    attachment.update(mContext, cv);
452                }
453            }
454            // Process the queue
455            kick();
456        }
457    }
458
459    /**
460     * Calculate the download priority of an Attachment.  A priority of zero means that the
461     * attachment is not marked for download.
462     * @param att the Attachment
463     * @return the priority key of the Attachment
464     */
465    private static int getPriority(Attachment att) {
466        int priorityClass = PRIORITY_NONE;
467        int flags = att.mFlags;
468        if ((flags & Attachment.FLAG_DOWNLOAD_FORWARD) != 0) {
469            priorityClass = PRIORITY_NORMAL;
470        } else if ((flags & Attachment.FLAG_DOWNLOAD_USER_REQUEST) != 0) {
471            priorityClass = PRIORITY_HIGH;
472        }
473        return priorityClass;
474    }
475
476    private void kick() {
477        synchronized(mLock) {
478            mLock.notify();
479        }
480    }
481
482    /**
483     * We use an EmailServiceCallback to keep track of the progress of downloads.  These callbacks
484     * come from either Controller (IMAP) or ExchangeService (EAS).  Note that we only implement the
485     * single callback that's defined by the EmailServiceCallback interface.
486     */
487    private class ServiceCallback extends IEmailServiceCallback.Stub {
488        public void loadAttachmentStatus(long messageId, long attachmentId, int statusCode,
489                int progress) {
490            if (Email.DEBUG) {
491                String code;
492                switch(statusCode) {
493                    case EmailServiceStatus.SUCCESS:
494                        code = "Success";
495                        break;
496                    case EmailServiceStatus.IN_PROGRESS:
497                        code = "In progress";
498                        break;
499                    default:
500                        code = Integer.toString(statusCode);
501                }
502                Log.d(TAG, "loadAttachmentStatus, id = " + attachmentId + " code = "+ code +
503                        ", " + progress + "%");
504            }
505            // Record status and progress
506            DownloadRequest req = mDownloadSet.findDownloadRequest(attachmentId);
507            if (req != null) {
508                req.lastStatusCode = statusCode;
509                req.lastProgress = progress;
510                req.lastCallbackTime = System.currentTimeMillis();
511            }
512            switch (statusCode) {
513                case EmailServiceStatus.IN_PROGRESS:
514                    break;
515                default:
516                    mDownloadSet.endDownload(attachmentId, statusCode);
517                    break;
518            }
519        }
520
521        @Override
522        public void sendMessageStatus(long messageId, String subject, int statusCode, int progress)
523                throws RemoteException {
524        }
525
526        @Override
527        public void syncMailboxListStatus(long accountId, int statusCode, int progress)
528                throws RemoteException {
529        }
530
531        @Override
532        public void syncMailboxStatus(long mailboxId, int statusCode, int progress)
533                throws RemoteException {
534        }
535    }
536
537    /**
538     * Return the class of the service used by the account type of the provided account id.  We
539     * cache the results to avoid repeated database access
540     * @param accountId the id of the account
541     * @return the service class for the account
542     */
543    private synchronized Class<? extends Service> getServiceClassForAccount(long accountId) {
544        // TODO: We should have some more data-driven way of determining the service class. I'd
545        // suggest adding an attribute in the stores.xml file
546        Class<? extends Service> serviceClass = mAccountServiceMap.get(accountId);
547        if (serviceClass == null) {
548            String protocol = Account.getProtocol(mContext, accountId);
549            if (protocol.equals("eas")) {
550                serviceClass = ExchangeService.class;
551            } else {
552                serviceClass = ControllerService.class;
553            }
554            mAccountServiceMap.put(accountId, serviceClass);
555        }
556        return serviceClass;
557    }
558
559    /*protected*/ void addServiceClass(long accountId, Class<? extends Service> serviceClass) {
560        mAccountServiceMap.put(accountId, serviceClass);
561    }
562
563    /*package*/ void onChange(Attachment att) {
564        mDownloadSet.onChange(att);
565    }
566
567    /*package*/ boolean isQueued(long attachmentId) {
568        return mDownloadSet.findDownloadRequest(attachmentId) != null;
569    }
570
571    /*package*/ int getSize() {
572        return mDownloadSet.size();
573    }
574
575    /*package*/ boolean dequeue(long attachmentId) {
576        DownloadRequest req = mDownloadSet.findDownloadRequest(attachmentId);
577        if (req != null) {
578            if (Email.DEBUG) {
579                Log.d(TAG, "Dequeued attachmentId:  " + attachmentId);
580            }
581            mDownloadSet.remove(req);
582            return true;
583        }
584        return false;
585    }
586
587    /**
588     * Ask the service for the number of items in the download queue
589     * @return the number of items queued for download
590     */
591    public static int getQueueSize() {
592        if (sRunningService != null) {
593            return sRunningService.getSize();
594        }
595        return 0;
596    }
597
598    /**
599     * Ask the service whether a particular attachment is queued for download
600     * @param attachmentId the id of the Attachment (as stored by EmailProvider)
601     * @return whether or not the attachment is queued for download
602     */
603    public static boolean isAttachmentQueued(long attachmentId) {
604        if (sRunningService != null) {
605            return sRunningService.isQueued(attachmentId);
606        }
607        return false;
608    }
609
610    /**
611     * Ask the service to remove an attachment from the download queue
612     * @param attachmentId the id of the Attachment (as stored by EmailProvider)
613     * @return whether or not the attachment was removed from the queue
614     */
615    public static boolean cancelQueuedAttachment(long attachmentId) {
616        if (sRunningService != null) {
617            return sRunningService.dequeue(attachmentId);
618        }
619        return false;
620    }
621
622    public static void watchdogAlarm() {
623        if (sRunningService != null) {
624            sRunningService.mDownloadSet.onWatchdogAlarm();
625        }
626    }
627
628    /**
629     * Called directly by EmailProvider whenever an attachment is inserted or changed
630     * @param id the attachment's id
631     * @param flags the new flags for the attachment
632     */
633    public static void attachmentChanged(final long id, final int flags) {
634        if (sRunningService == null) return;
635        Utility.runAsync(new Runnable() {
636            public void run() {
637                final Attachment attachment =
638                    Attachment.restoreAttachmentWithId(sRunningService, id);
639                if (attachment != null) {
640                    // Store the flags we got from EmailProvider; given that all of this
641                    // activity is asynchronous, we need to use the newest data from
642                    // EmailProvider
643                    attachment.mFlags = flags;
644                    sRunningService.onChange(attachment);
645                }
646            }});
647    }
648
649    public void run() {
650        mContext = this;
651        // Run through all attachments in the database that require download and add them to
652        // the queue
653        int mask = Attachment.FLAG_DOWNLOAD_FORWARD | Attachment.FLAG_DOWNLOAD_USER_REQUEST;
654        Cursor c = getContentResolver().query(Attachment.CONTENT_URI,
655                EmailContent.ID_PROJECTION, "(" + Attachment.FLAGS + " & ?) != 0",
656                new String[] {Integer.toString(mask)}, null);
657        try {
658            Log.d(TAG, "Count: " + c.getCount());
659            while (c.moveToNext()) {
660                Attachment attachment = Attachment.restoreAttachmentWithId(
661                        this, c.getLong(EmailContent.ID_PROJECTION_COLUMN));
662                if (attachment != null) {
663                    mDownloadSet.onChange(attachment);
664                }
665            }
666        } catch (Exception e) {
667            e.printStackTrace();
668        }
669        finally {
670            c.close();
671        }
672
673        // Loop until stopped, with a 30 minute wait loop
674        while (!mStop) {
675            // Here's where we run our attachment loading logic...
676            mDownloadSet.processQueue();
677            synchronized(mLock) {
678                try {
679                    mLock.wait(PROCESS_QUEUE_WAIT_TIME);
680                } catch (InterruptedException e) {
681                    // That's ok; we'll just keep looping
682                }
683            }
684        }
685    }
686
687    @Override
688    public int onStartCommand(Intent intent, int flags, int startId) {
689        sRunningService = this;
690        return Service.START_STICKY;
691    }
692
693    /**
694     * The lifecycle of this service is managed by Email.setServicesEnabled(), which is called
695     * throughout the code, in particular 1) after boot and 2) after accounts are added or removed
696     * The goal is that this service should be running at all times when there's at least one
697     * email account present.
698     */
699    @Override
700    public void onCreate() {
701        // Start up our service thread
702        new Thread(this, "AttachmentDownloadService").start();
703    }
704    @Override
705    public IBinder onBind(Intent intent) {
706        return null;
707    }
708
709    @Override
710    public void onDestroy() {
711        Log.d(TAG, "**** ON DESTROY!");
712        if (sRunningService != null) {
713            mStop = true;
714            kick();
715        }
716        sRunningService = null;
717    }
718
719    @Override
720    public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
721        pw.println("AttachmentDownloadService");
722        long time = System.currentTimeMillis();
723        synchronized(mDownloadSet) {
724            pw.println("  Queue, " + mDownloadSet.size() + " entries");
725            Iterator<DownloadRequest> iterator = mDownloadSet.descendingIterator();
726            // First, start up any required downloads, in priority order
727            while (iterator.hasNext()) {
728                DownloadRequest req = iterator.next();
729                pw.println("    Account: " + req.accountId + ", Attachment: " + req.attachmentId);
730                pw.println("      Priority: " + req.priority + ", Time: " + req.time +
731                        (req.inProgress ? " [In progress]" : ""));
732                Attachment att = Attachment.restoreAttachmentWithId(mContext, req.attachmentId);
733                if (att == null) {
734                    pw.println("      Attachment not in database?");
735                } else if (att.mFileName != null) {
736                    String fileName = att.mFileName;
737                    String suffix = "[none]";
738                    int lastDot = fileName.lastIndexOf('.');
739                    if (lastDot >= 0) {
740                        suffix = fileName.substring(lastDot);
741                    }
742                    pw.print("      Suffix: " + suffix);
743                    if (att.mContentUri != null) {
744                        pw.print(" ContentUri: " + att.mContentUri);
745                    }
746                    pw.print(" Mime: ");
747                    if (att.mMimeType != null) {
748                        pw.print(att.mMimeType);
749                    } else {
750                        pw.print(AttachmentProvider.inferMimeType(fileName, null));
751                        pw.print(" [inferred]");
752                    }
753                    pw.println(" Size: " + att.mSize);
754                }
755                if (req.inProgress) {
756                    pw.println("      Status: " + req.lastStatusCode + ", Progress: " +
757                            req.lastProgress);
758                    pw.println("      Started: " + req.startTime + ", Callback: " +
759                            req.lastCallbackTime);
760                    pw.println("      Elapsed: " + ((time - req.startTime) / 1000L) + "s");
761                    if (req.lastCallbackTime > 0) {
762                        pw.println("      CB: " + ((time - req.lastCallbackTime) / 1000L) + "s");
763                    }
764                }
765            }
766        }
767    }
768}
769