コード例 #1
0
  /**
   * M: Load the structure and body of messages not yet synced in multithread
   *
   * @param context the context
   * @param account the account we're syncing
   * @param remoteStore the Store we're working on
   * @param unsyncedMessages an array of Message's we've got headers for
   * @param toMailbox the destination mailbox we're syncing
   * @throws MessagingException
   */
  public void loadUnsyncedMessagesInMultiThread(
      Context context,
      final Account account,
      Store remoteStore,
      ArrayList<Message> unsyncedMessages,
      final Mailbox toMailbox)
      throws MessagingException {
    LogUtils.d(
        Logging.LOG_TAG, "loadUnsyncedMessagesInMultiThread message: " + unsyncedMessages.size());
    /** M: Put IMAP synchronize process into Multi-Thread, left POP3 run as used to @{ */
    mUnsyncedMessages = unsyncedMessages;

    /** M: Start {@link MAX_ACCOUNT_SYNC_THREADS} threads to synchronize Messages concurrently */
    synchronized (mUnsyncedMessages) {
      mRunningSyncThreadCount = 0;
      final int unsyncedMessagesCount = unsyncedMessages.size();
      while (unsyncedMessagesCount > mRunningSyncThreadCount
          && mRunningSyncThreadCount < MAX_ACCOUNT_SYNC_THREADS) {
        Logging.v(
            "unsyncedMessages size: "
                + unsyncedMessagesCount
                + " threadIndex: "
                + mRunningSyncThreadCount);
        MESSAGE_SYNC_THREAD_POOL.execute(
            new LoadUnsyncMessageTask(context, account, remoteStore, toMailbox));
        mRunningSyncThreadCount++;
      }
      // wait until all messages has been fetched.
      while (mRunningSyncThreadCount > 0) {
        try {
          mUnsyncedMessages.wait();
        } catch (InterruptedException e) {
          Logging.e("loadUnsyncedMessages " + e.getMessage(), e);
        }
      }
    }
    if (mMessagingException != null) {
      MessagingException me = mMessagingException;
      mMessagingException = null;
      throw me;
    }
  }
コード例 #2
0
  public void sendMessage() throws IOException {
    Logging.d(TAG, "Start send message ... for Uri " + mUri);
    // Initialize return value
    int resultType = EmailExternalConstants.TYPE_SEND;
    int result = EmailExternalConstants.RESULT_SUCCESS;

    mInputStream = mContentResolver.openInputStream(mUri);
    if (null == mInputStream) {
      Logging.w(
          TAG,
          "Send Message Failed in sendMessage() method , "
              + "Can't get InputStream from the given uri: "
              + mUri);
      result = EmailExternalConstants.RESULT_FAIL;
      sendCallback(result, resultType);
      return;
    }

    // Write the output to a temporary file
    File cacheDir = mContext.getCacheDir();
    File tmpFile = File.createTempFile("eas_", "tmp", cacheDir);
    writToFile(mInputStream, tmpFile);
    try {
      mInputStream.close();
    } catch (Exception e) {
      Logging.e(TAG, "Closes inputstream fail.", e);
    }

    try {
      // Get an input stream to our temporary file and create an entity with it
      FileInputStream fileInputStream = new FileInputStream(tmpFile);
      InputStreamEntity inputEntity = new InputStreamEntity(fileInputStream, tmpFile.length());

      // Create the appropriate command and POST it to the server
      String cmd = "SendMail&SaveInSent=F";
      if (mSaveInSent) {
        cmd = "SendMail&SaveInSent=T";
      }
      // if (smartSend) {
      // cmd = reply ? "SmartReply" : "SmartForward";
      // cmd += "&ItemId=" + itemId + "&CollectionId=" + collectionId +
      // "&SaveInSent=T";
      // }
      Logging.d(TAG, "Send cmd: " + cmd);
      EasResponse resp = sendHttpClientPost(cmd, inputEntity, EasOutboxService.SEND_MAIL_TIMEOUT);
      fileInputStream.close();

      // Send feedback
      // sendCallback(result, resultType);

      // TODO:How to define the Deliver complete.
      // resultType = EmailExternalConstants.TYPE_DELIVER;
      int code = resp.getStatus();
      if (code == HttpStatus.SC_OK) {
        Logging.d(TAG, "EAS Message sending success, code:" + code);
        result = EmailExternalConstants.RESULT_SUCCESS;
        sendCallback(result, resultType);
        return;
      } else {
        Logging.d(TAG, "EAS Message sending failed, code:" + code);
        result = EmailExternalConstants.RESULT_FAIL;
      }
    } catch (FileNotFoundException e) {
      Logging.e(TAG, "EAS SendMessage FileNotFoundException " + e.getMessage());
      result = EmailExternalConstants.RESULT_FAIL;
    } catch (IOException e) {
      Logging.e(TAG, "EAS SendMessage Exception " + e.getMessage());
      result = EmailExternalConstants.RESULT_FAIL;
    } finally {
      // Clean up the temporary file
      if (tmpFile.exists()) {
        tmpFile.delete();
      }
    }
    Logging.d(TAG, "EAS send Message feedback result = " + result + " resultType = " + resultType);
    sendCallback(result, resultType);
  }