Example #1
0
    public Message[] getMessagesSince(final Date since, final int max, final boolean flagged)
        throws MessagingException {
      ImapSearcher searcher =
          new ImapSearcher() {
            public List<ImapResponse> search() throws IOException, MessagingException {
              String sentSince = since != null ? " SENTSINCE " + RFC3501_DATE.format(since) : "";
              return executeSimpleCommand(
                  "UID SEARCH 1:* NOT DELETED" + sentSince + (flagged ? " FLAGGED" : ""));
            }
          };

      Message[] msgs = search(searcher, null);

      Log.i(
          TAG, "Found " + msgs.length + " msgs" + (since == null ? "" : " (since " + since + ")"));
      if (max > 0 && msgs.length > max) {
        if (LOCAL_LOGV) Log.v(TAG, "Fetching envelopes");

        FetchProfile fp = new FetchProfile();
        fp.add(FetchProfile.Item.DATE);
        fetch(msgs, fp, null);

        if (LOCAL_LOGV) Log.v(TAG, "Sorting");
        // Debug.startMethodTracing("sorting");
        Arrays.sort(
            msgs,
            new Comparator<Message>() {
              public int compare(Message m1, Message m2) {
                return (m2 != null
                        && m2.getSentDate() != null
                        && m1 != null
                        && m1.getSentDate() != null)
                    ? m2.getSentDate().compareTo(m1.getSentDate())
                    : -1;
              }
            });
        // Debug.stopMethodTracing();
        if (LOCAL_LOGV) Log.v(TAG, "Sorting done");

        Message[] recent = new Message[max];
        System.arraycopy(msgs, 0, recent, 0, max);

        return recent;
      }
      return msgs;
    }
 /**
  * Fetch the items contained in the FetchProfile into the given set of Messages in as efficient
  * a manner as possible.
  *
  * @param messages
  * @param fp
  * @throws MessagingException
  */
 @Override
 public void fetch(
     List<Pop3Message> messages, FetchProfile fp, MessageRetrievalListener<Pop3Message> listener)
     throws MessagingException {
   if (messages == null || messages.isEmpty()) {
     return;
   }
   List<String> uids = new ArrayList<String>();
   for (Message message : messages) {
     uids.add(message.getUid());
   }
   try {
     indexUids(uids);
   } catch (IOException ioe) {
     throw new MessagingException("fetch", ioe);
   }
   try {
     if (fp.contains(FetchProfile.Item.ENVELOPE)) {
       /*
        * We pass the listener only if there are other things to do in the
        * FetchProfile. Since fetchEnvelop works in bulk and eveything else
        * works one at a time if we let fetchEnvelope send events the
        * event would get sent twice.
        */
       fetchEnvelope(messages, fp.size() == 1 ? listener : null);
     }
   } catch (IOException ioe) {
     throw new MessagingException("fetch", ioe);
   }
   for (int i = 0, count = messages.size(); i < count; i++) {
     Pop3Message pop3Message = messages.get(i);
     try {
       if (listener != null && !fp.contains(FetchProfile.Item.ENVELOPE)) {
         listener.messageStarted(pop3Message.getUid(), i, count);
       }
       if (fp.contains(FetchProfile.Item.BODY)) {
         fetchBody(pop3Message, -1);
       } else if (fp.contains(FetchProfile.Item.BODY_SANE)) {
         /*
          * To convert the suggested download size we take the size
          * divided by the maximum line size (76).
          */
         if (mStoreConfig.getMaximumAutoDownloadMessageSize() > 0) {
           fetchBody(pop3Message, (mStoreConfig.getMaximumAutoDownloadMessageSize() / 76));
         } else {
           fetchBody(pop3Message, -1);
         }
       } else if (fp.contains(FetchProfile.Item.STRUCTURE)) {
         /*
          * If the user is requesting STRUCTURE we are required to set the body
          * to null since we do not support the function.
          */
         pop3Message.setBody(null);
       }
       if (listener != null && !(fp.contains(FetchProfile.Item.ENVELOPE) && fp.size() == 1)) {
         listener.messageFinished(pop3Message, i, count);
       }
     } catch (IOException ioe) {
       throw new MessagingException("Unable to fetch message", ioe);
     }
   }
 }