private void saveMessageAndCallback(){
  try {
    final String senderId=mMessageModel.sender_id;
    final String receiverId=mMessageModel.receiver_id;
    final String sentAtTime=mMessageModel.sent_at;
    final String messageText=mMessageModel.message;
    final String serverReplyId=mMessageModel.reply_id;
    final ContentValues chatValues=new ContentValues(10);
    final String chatId=Utils.generateChatId(senderId,receiverId);
    Logger.d(TAG,"chat id : " + chatId);
    chatValues.put(DatabaseColumns.CHAT_ID,chatId);
    chatValues.put(DatabaseColumns.SERVER_CHAT_ID,serverReplyId);
    chatValues.put(DatabaseColumns.SENDER_ID,senderId);
    chatValues.put(DatabaseColumns.RECEIVER_ID,receiverId);
    chatValues.put(DatabaseColumns.MESSAGE,messageText);
    chatValues.put(DatabaseColumns.TIMESTAMP,sentAtTime);
    chatValues.put(DatabaseColumns.SENT_AT,sentAtTime);
    chatValues.put(DatabaseColumns.CHAT_STATUS,ChatStatus.SENDING);
    chatValues.put(DatabaseColumns.TIMESTAMP_EPOCH,mMessageDateFormatter.getEpoch(sentAtTime) + AppConstants.SERVER_LAG_TIME);
    chatValues.put(DatabaseColumns.TIMESTAMP_HUMAN,mMessageDateFormatter.getOutputTimestamp(sentAtTime));
    final long insertRowId=DBInterface.insert(TableChatMessages.NAME,null,chatValues,true);
    Logger.d(TAG,insertRowId + "");
    if (insertRowId >= 0) {
      final ContentValues values=new ContentValues(7);
      values.put(DatabaseColumns.CHAT_ID,chatId);
      values.put(DatabaseColumns.SERVER_CHAT_ID,serverReplyId);
      values.put(DatabaseColumns.LAST_MESSAGE_ID,insertRowId);
      values.put(DatabaseColumns.CHAT_TYPE,ChatType.PERSONAL);
      values.put(DatabaseColumns.TIMESTAMP,sentAtTime);
      try {
        values.put(DatabaseColumns.TIMESTAMP_HUMAN,mChatDateFormatter.getOutputTimestamp(sentAtTime));
        values.put(DatabaseColumns.TIMESTAMP_EPOCH,mChatDateFormatter.getEpoch(sentAtTime));
      }
 catch (      ParseException e) {
        e.printStackTrace();
      }
      values.put(DatabaseColumns.ID,receiverId);
      Logger.v(TAG,"Updating chats for Id %s",chatId);
      final int updateCount=DBInterface.update(TableChats.NAME,values,CHAT_SELECTION,new String[]{chatId},true);
      if (updateCount == 0) {
        Logger.v(TAG,"INSERTED chats for Id %s",chatId);
        DBInterface.insert(TableChats.NAME,null,values,true);
      }
    }
    mSendChatCallback.sendChat(mMessageModel,insertRowId);
  }
 catch (  ParseException e) {
    Logger.e(TAG,e,"Invalid timestamp");
  }
}

private void saveMessageAndCallback ( ) { try { final String senderId = mMessageModel . sender_id ; final String receiverId = mMessageModel . receiver_id ; final String sentAtTime = mMessageModel . sent_at ; final String messageText = mMessageModel . message ; final String serverReplyId = mMessageModel . reply_id ; final ContentValues chatValues = new ContentValues ( 10 ) ; final String chatId = Utils . generateChatId ( senderId , receiverId ) ; Logger . d ( TAG , "chat id : " + chatId ) ; chatValues . put ( DatabaseColumns . CHAT_ID , chatId ) ; chatValues . put ( DatabaseColumns . SERVER_CHAT_ID , serverReplyId ) ; chatValues . put ( DatabaseColumns . SENDER_ID , senderId ) ; chatValues . put ( DatabaseColumns . RECEIVER_ID , receiverId ) ; chatValues . put ( DatabaseColumns . MESSAGE , messageText ) ; chatValues . put ( DatabaseColumns . TIMESTAMP , sentAtTime ) ; chatValues . put ( DatabaseColumns . SENT_AT , sentAtTime ) ; chatValues . put ( DatabaseColumns . CHAT_STATUS , ChatStatus . SENDING ) ; chatValues . put ( DatabaseColumns . TIMESTAMP_EPOCH , mMessageDateFormatter . getEpoch ( sentAtTime ) + AppConstants . SERVER_LAG_TIME ) ; chatValues . put ( DatabaseColumns . TIMESTAMP_HUMAN , mMessageDateFormatter . getOutputTimestamp ( sentAtTime ) ) ; final long insertRowId = DBInterface . insert ( TableChatMessages . NAME , null , chatValues , true ) ; Logger . d ( TAG , insertRowId + "" ) ; if ( insertRowId >= 0 ) { final ContentValues values = new ContentValues ( 7 ) ; values . put ( DatabaseColumns . CHAT_ID , chatId ) ; values . put ( DatabaseColumns . SERVER_CHAT_ID , serverReplyId ) ; values . put ( DatabaseColumns . LAST_MESSAGE_ID , insertRowId ) ; values . put ( DatabaseColumns . CHAT_TYPE , ChatType . PERSONAL ) ; values . put ( DatabaseColumns . TIMESTAMP , sentAtTime ) ; try { values . put ( DatabaseColumns . TIMESTAMP_HUMAN , mChatDateFormatter . getOutputTimestamp ( sentAtTime ) ) ; values . put ( DatabaseColumns . TIMESTAMP_EPOCH , mChatDateFormatter . getEpoch ( sentAtTime ) ) ; } catch ( ParseException e ) { e . printStackTrace ( ) ; } values . put ( DatabaseColumns . ID , receiverId ) ; Logger . v ( TAG , "Updating chats for Id %s" , chatId ) ; final int updateCount = DBInterface . update ( TableChats . NAME , values , CHAT_SELECTION , new String [ ] { chatId } , true ) ; if ( updateCount == 0 ) { Logger . v ( TAG , "INSERTED chats for Id %s" , chatId ) ; DBInterface . insert ( TableChats . NAME , null , values , true ) ; } } mSendChatCallback . sendChat ( mMessageModel , insertRowId ) ; } catch ( ParseException e ) { Logger . e ( TAG , e , "Invalid timestamp" ) ; } }
save a local message in the database , and give a callback to make the chat send request once it is saved
