private void handleServerFolderChange(Folder folder) throws AuthenticationFailedException {
    long accountId = folder.accountId;
    List<Message> m = new ArrayList<Message>();
    List<String> remoteSIds = new ArrayList<String>();
    List<String> localSids =
        Note.getAllSyncedNoteSidByFolderId(folder.id, accountId, application.getDBHelper());
    try {
      Message[] messages = imapStoreClient.getMessages(folder.name);
      for (Message msg : messages) {
        remoteSIds.add(msg.getUid());
        if (!localSids.contains(msg.getUid())) m.add(msg);
      }
      if (m.size() > 0) {
        fetMessages(m, folder);
      }
    } catch (AuthenticationFailedException e) {
      Log.e(TAG, "", e);
      throw new AuthenticationFailedException(e.getLocalizedMessage());
    } catch (MessagingException e) {
      Log.e(TAG, "", e);
      throw new AuthenticationFailedException(e.getLocalizedMessage());
    }

    handleRemoteDeletedNotes(accountId, folder.id, remoteSIds);
  }
  private void doDeleteNotes(long accountId) throws MessagingException {
    Log.i(TAG, "doDeleteNotes() begin");
    MimeMessage msg;
    List<MimeMessage> pendingDeleteMessages = new ArrayList<MimeMessage>();
    HashMap<Long, List<Note>> folderId2NotesMap =
        buildLocalChangedFolder2NotesMap(
            Note.getLocalDeletedNotes(accountId, application.getDBHelper()));

    if (folderId2NotesMap.size() == 0) {
      Log.i(TAG, "doDeleteNotes() end... no deleted notes");
      return;
    }
    try {
      for (long folderId : folderId2NotesMap.keySet()) {
        for (Note note : folderId2NotesMap.get(folderId)) {
          msg = MessageCompose.messageFromDeletedNotes(note);
          if (loginUser != null) {
            msg.setFrom(new Address(loginUser));
          }
          msg.getMessageId();
          pendingDeleteMessages.add(msg);
        }
        Message[] delMessages =
            (Message[]) pendingDeleteMessages.toArray(new Message[pendingDeleteMessages.size()]);
        if (delMessages.length > 0) {
          if (folderId == Folder.ALL_FOLDER_ID) {
            imapStoreClient.deleteNote(delMessages, ImapConsts.IMAP__INOTES_FOLDER);
          } else {
            Folder folder = Folder.getFolderById(folderId, accountId, application.getDBHelper());
            imapStoreClient.deleteNote(delMessages, folder.name);
          }
          pendingDeleteMessages.clear();
        }
        // TODO 感觉可能有点危险,确保删除都是成功的
        for (Note note : folderId2NotesMap.get(folderId)) {
          Note.deleteNoteByIdForever(note.id, accountId, application.getDBHelper());
        }
      }
    } catch (MessagingException e) {
      Log.e(TAG, " ", e);
      throw new MessagingException(e.getLocalizedMessage());
    }
    Log.i(TAG, "doDeleteNotes() end");
  }
 private void fetMessages(List<Message> pendingMessages, Folder folder) throws MessagingException {
   Message[] messages =
       imapStoreClient.fetchMessages(
           pendingMessages.toArray(new Message[pendingMessages.size()]),
           folder.name,
           folder.accountId);
   if (messages != null && messages.length > 0) {
     refreshSyncingViews();
   }
 }
  private void doAddNotes(long accountId) throws MessagingException {
    Log.i(TAG, "doAddNotes() begin");
    MimeMessage msg;
    List<MimeMessage> pendingComposeMessages = new ArrayList<MimeMessage>();
    HashMap<Long, List<Note>> folderIdNotesMap =
        buildLocalChangedFolder2NotesMap(
            Note.getLocalAddedNotes(accountId, application.getDBHelper()));
    if (folderIdNotesMap.size() == 0) {
      Log.i(TAG, "doAddNotes() end ..no added notes");
      return;
    }
    try {

      for (long folderId : folderIdNotesMap.keySet()) {
        for (Note note : folderIdNotesMap.get(folderId)) {
          msg = MessageCompose.messageFromNotes(note, application);
          if (loginUser != null) {
            msg.setFrom(new Address(loginUser));
          }
          pendingComposeMessages.add(msg);
        }
        if (pendingComposeMessages.size() > 0) {
          if (folderId == Folder.ALL_FOLDER_ID) {
            imapStoreClient.createNotes(pendingComposeMessages, ImapConsts.IMAP__INOTES_FOLDER);
          } else {
            Folder folder = Folder.getFolderById(folderId, accountId, application.getDBHelper());
            imapStoreClient.createNotes(
                pendingComposeMessages,
                folder == null ? ImapConsts.IMAP__INOTES_FOLDER : folder.name);
          }
          pendingComposeMessages.clear();
        }
      }

      handleRemoteUidNullErro(accountId);

    } catch (MessagingException e) {
      Log.e(TAG, " ", e);
      throw new MessagingException(e.getLocalizedMessage());
    }
    Log.i(TAG, "doAddNotes() end");
  }
  private void doFetchRemoteMessages(long accountId) throws AuthenticationFailedException {

    imapStoreClient.fetchNewFolder(accountId);

    List<Folder> localFolders =
        Folder.getAllFoldersByAccountId(accountId, null, null, null, application.getDBHelper());

    for (Folder folder : localFolders) {
      handleServerFolderChange(folder);
    }
    Log.i(TAG, "doFetchRemoteMessages() end");
  }
 @Override
 public void instance(INotesApplication application) {
   super.instance(application);
   imapStoreClient = new ImapStoreClient();
   try {
     if (!application.getAccountManager().isLocalMode()) {
       imapStoreClient.init(application.getAccountManager().getAccount(), application);
       loginUser = application.getAccountManager().getEmail();
     }
   } catch (AuthenticationFailedException e) {
     Log.e(TAG, "", e);
     throw new AuthenticationErrorException(e.getLocalizedMessage());
   }
 }