public void sendMessage(String text, boolean isResend) {

    if (mConnection.getState() != ImConnection.LOGGED_IN) {
      // connection has been suspended, save the message without send it
      long now = System.currentTimeMillis();
      insertMessageInDb(null, text, now, Imps.MessageType.POSTPONED);
      return;
    }

    org.awesomeapp.messenger.model.Message msg = new org.awesomeapp.messenger.model.Message(text);
    msg.setID(nextID());

    msg.setFrom(mConnection.getLoginUser().getAddress());
    msg.setType(Imps.MessageType.OUTGOING);

    long sendTime = System.currentTimeMillis();

    if (!isResend) {
      insertMessageInDb(null, text, sendTime, msg.getType(), 0, msg.getID());
      insertOrUpdateChat(text);
    }

    int newType = mChatSession.sendMessageAsync(msg);

    if (msg.getDateTime() != null) sendTime = msg.getDateTime().getTime();

    updateMessageInDb(msg.getID(), newType, sendTime);
  }
    public synchronized boolean onIncomingMessage(
        ChatSession ses, final org.awesomeapp.messenger.model.Message msg) {
      String body = msg.getBody();
      String username = msg.getFrom().getAddress();
      String bareUsername = msg.getFrom().getBareAddress();
      String nickname = getNickName(username);
      long time = msg.getDateTime().getTime();

      if (msg.getID() != null && Imps.messageExists(mContentResolver, msg.getID())) {
        return false; // this message is a duplicate
      }

      insertOrUpdateChat(body);

      boolean wasMessageSeen = false;

      if (msg.getID() == null) insertMessageInDb(nickname, body, time, msg.getType());
      else insertMessageInDb(nickname, body, time, msg.getType(), 0, msg.getID());

      int N = mRemoteListeners.beginBroadcast();
      for (int i = 0; i < N; i++) {
        IChatListener listener = mRemoteListeners.getBroadcastItem(i);
        try {
          boolean wasSeen = listener.onIncomingMessage(ChatSessionAdapter.this, msg);

          if (wasSeen) wasMessageSeen = wasSeen;

        } catch (RemoteException e) {
          // The RemoteCallbackList will take care of removing the
          // dead listeners.
        }
      }
      mRemoteListeners.finishBroadcast();

      // Due to the move to fragments, we could have listeners for ChatViews that are not visible on
      // the screen.
      // This is for fragments adjacent to the current one.  Therefore we can't use the existence of
      // listeners
      // as a filter on notifications.
      if (!wasMessageSeen) {
        // reinstated body display here in the notification; perhaps add preferences to turn that
        // off
        mStatusBarNotifier.notifyChat(
            mConnection.getProviderId(),
            mConnection.getAccountId(),
            getId(),
            bareUsername,
            nickname,
            body,
            false);
      }

      mHasUnreadMessages = true;
      return true;
    }