コード例 #1
0
    @Override
    public void processMessage(Chat chat, org.jivesoftware.smack.packet.Message message) {

      if (message.getBody() == null) {
        // TODO: investigate why some message bodies are null
        return;
      }

      // TODO: implement builder pattern for domain Message and Chat classes
      final Message m = new Message();
      m.setTimestamp(System.currentTimeMillis());
      m.setBody(message.getBody());
      m.setLocal(false);

      Log.d(
          TAG,
          String.format(
              "chatId %s message <%s> received from %s",
              currentChatId, m.getBody(), chat.getParticipant()));

      updateChatTimestamp(pocChat.getId());

      ContentValues values = new ContentValues();
      values.put(SQLiteHelper.COLUMN_BODY, m.getBody());
      values.put(SQLiteHelper.COLUMN_TIMESTAMP, m.getTimestamp()); // current time
      values.put(SQLiteHelper.COLUMN_IS_LOCAL, m.isLocal() ? 1 : 0);
      values.put(SQLiteHelper.COLUMN_CHAT_ID, pocChat.getId());

      // once insert is successful, loader updates the list automatically
      context.getContentResolver().insert(MessageProvider.URI_MESSAGES, values);

      // show the toast regardless if messages UI is opened
      showToast(context, m.getBody());
    }
コード例 #2
0
  public void sendMessage(long currentChatId, Message chatMessage) {
    try {
      Log.d(TAG, "messaged sent: " + chatMessage.getBody());

      if (!chatsMap.isEmpty()) {
        chatsMap.get(currentChatId).getSmackChat().sendMessage(chatMessage.getBody());
      } else {
        // comes here when no internet connection
        // which results in no chats got registered
      }
    } catch (NotConnectedException | InterruptedException e) {
      Log.e(TAG, e.getMessage());
    }
  }