예제 #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;
    }