@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()); }
/** * Creates a map in memory and assigns listeners to each chat Once connection is established chats * are taken from DB, created in memory and listeners assigned. */ public void registerChats() { Cursor cursor = context .getContentResolver() .query(ChatProvider.URI_CHATS, ChatProvider.allColumns, null, null, null); while (cursor.moveToNext()) { com.gordbilyi.jellyfish.im.domain.Chat pocChat = ChatProvider.cursorToChat(cursor); try { Chat c = ChatManager.getInstanceFor(connection) .createChat( JidCreate.entityBareFrom(pocChat.getTo()), new LocalChatMessageListener(pocChat)); pocChat.setSmackChat(c); } catch (XmppStringprepException e) { Log.e(TAG, e.getMessage()); } chatsMap.put(pocChat.getId(), pocChat); } // this needs to kick necessary listener registerIncomingChats(); }