Пример #1
0
  @Override
  public void loadMore(long messageId) throws RemoteException {
    /// M: We Can't load more in low storage state @{
    if (StorageLowState.checkIfStorageLow(mContext)) {
      LogUtils.e(Logging.LOG_TAG, "Can't load more due to low storage");
      return;
    }
    /// @}
    // Load a message for view...
    try {
      // 1. Resample the message, in case it disappeared or synced while
      // this command was in queue
      final EmailContent.Message message =
          EmailContent.Message.restoreMessageWithId(mContext, messageId);
      if (message == null) {
        return;
      }
      if (message.mFlagLoaded == EmailContent.Message.FLAG_LOADED_COMPLETE) {
        // We should NEVER get here
        return;
      }

      // 2. Open the remote folder.
      // TODO combine with common code in loadAttachment
      final Account account = Account.restoreAccountWithId(mContext, message.mAccountKey);
      final Mailbox mailbox = Mailbox.restoreMailboxWithId(mContext, message.mMailboxKey);
      if (account == null || mailbox == null) {
        // mListeners.loadMessageForViewFailed(messageId, "null account or mailbox");
        return;
      }
      TrafficStats.setThreadStatsTag(TrafficFlags.getSyncFlags(mContext, account));

      final Store remoteStore = Store.getInstance(account, mContext);
      final String remoteServerId;
      // If this is a search result, use the protocolSearchInfo field to get the
      // correct remote location
      if (!TextUtils.isEmpty(message.mProtocolSearchInfo)) {
        remoteServerId = message.mProtocolSearchInfo;
      } else {
        remoteServerId = mailbox.mServerId;
      }
      final Folder remoteFolder = remoteStore.getFolder(remoteServerId);
      remoteFolder.open(OpenMode.READ_WRITE);

      // 3. Set up to download the entire message
      final Message remoteMessage = remoteFolder.getMessage(message.mServerId);
      final FetchProfile fp = new FetchProfile();
      fp.add(FetchProfile.Item.BODY);
      remoteFolder.fetch(new Message[] {remoteMessage}, fp, null);

      // 4. Write to provider
      Utilities.copyOneMessageToProvider(
          mContext, remoteMessage, account, mailbox, EmailContent.Message.FLAG_LOADED_COMPLETE);
    } catch (MessagingException me) {
      if (Logging.LOGD) LogUtils.v(Logging.LOG_TAG, "", me);

    } catch (RuntimeException rte) {
      LogUtils.d(Logging.LOG_TAG, "RTE During loadMore");
    }
  }
  /**
   * M: Load the structure and body of messages not yet synced
   *
   * @param account the account we're syncing
   * @param remoteFolder the (open) Folder we're working on
   * @param messages an array of Messages we've got headers for
   * @param toMailbox the destination mailbox we're syncing
   * @throws MessagingException
   */
  public static void loadUnsyncedMessages(
      final Context context,
      final Account account,
      Folder remoteFolder,
      ArrayList<Message> unsyncedMessages,
      final Mailbox toMailbox)
      throws MessagingException {
    FetchProfile fp = new FetchProfile();

    for (Message message : unsyncedMessages) {
      // Also need to pay attention to the 0 RFC822.SIZE that may occurs in using Gmail Imap.
      // Cope with it as partial download in case that the mail has a big size attachment
      // which can lead to potential OutOfMemory

      // Download messages. We ask the server to give us the message structure,
      // but not all of the attachments.
      fp.clear();
      fp.add(FetchProfile.Item.STRUCTURE);
      remoteFolder.fetch(new Message[] {message}, fp, null);

      boolean isPartialDownload = imapPartialFetchMessage(message, remoteFolder);
      // Store the updated message locally and mark it fully loaded
      Utilities.copyOneMessageToProvider(
          context,
          message,
          account,
          toMailbox,
          isPartialDownload
              ? EmailContent.Message.FLAG_LOADED_PARTIAL
              : EmailContent.Message.FLAG_LOADED_COMPLETE);
    }
  }