Example #1
0
    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;
    }