/**
  * Is group chat active
  *
  * @param chatId Chat ID
  * @return Boolean
  */
 private IChatSession isGroupChatActive(String chatId) {
   try {
     List<IBinder> chatSessionsBinder = messagingApi.getGroupChatSessions();
     for (IBinder binder : chatSessionsBinder) {
       IChatSession chatSession = IChatSession.Stub.asInterface(binder);
       if (chatSession.getChatID().equals(chatId)) {
         return chatSession;
       }
     }
     return null;
   } catch (Exception e) {
     return null;
   }
 }
  /**
   * Load history
   *
   * @param session Chat session
   */
  public void loadHistory(IChatSession session) {
    try {
      EventsLogApi log = new EventsLogApi(this);
      Uri uri = log.getGroupChatLogContentProviderUri();
      Cursor cursor =
          getContentResolver()
              .query(
                  uri,
                  new String[] {
                    RichMessagingData.KEY_CONTACT,
                    RichMessagingData.KEY_DATA,
                    RichMessagingData.KEY_TIMESTAMP,
                    RichMessagingData.KEY_STATUS,
                    RichMessagingData.KEY_TYPE
                  },
                  RichMessagingData.KEY_CHAT_ID + "='" + session.getChatID() + "'",
                  null,
                  RichMessagingData.KEY_TIMESTAMP + " DESC");

      // The system message are not loaded
      while (cursor.moveToNext()) {
        int messageMessageType = cursor.getInt(EventsLogApi.TYPE_COLUMN);
        switch (messageMessageType) {
          case EventsLogApi.TYPE_OUTGOING_GROUP_CHAT_MESSAGE:
          case EventsLogApi.TYPE_INCOMING_GROUP_CHAT_MESSAGE:
          case EventsLogApi.TYPE_OUTGOING_GROUP_GEOLOC:
          case EventsLogApi.TYPE_INCOMING_GROUP_GEOLOC:
            updateView(cursor);
            break;
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
        public void onClick(View v) {
          if (!isServiceAvailable()) {
            Utils.showMessage(ChatList.this, getString(R.string.label_continue_chat_failed));
            return;
          }

          // Get selected item
          ChatListItemCache cache = (ChatListItemCache) v.getTag();
          if (cache.isGroupChat()) {
            // Group chat
            IChatSession session = isGroupChatActive(cache.chatId);
            if (session != null) {
              // Session already active on the device: just reload it in the UI
              try {
                Intent intent = new Intent(ChatList.this, GroupChatView.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                intent.putExtra("subject", session.getSubject());
                intent.putExtra("sessionId", session.getSessionID());
                startActivity(intent);
              } catch (Exception e) {
                Utils.showMessage(ChatList.this, getString(R.string.label_api_failed));
              }
            } else {
              // Test if the session may be rejoined or not
              int status = RichMessaging.getInstance().getGroupChatStatus(cache.chatId);
              if (status == EventsLogApi.STATUS_TERMINATED_BY_USER) {
                // The session was terminated by user itself: rejoin or restart are not authorized
                Utils.showMessage(ChatList.this, getString(R.string.label_rejoin_unauthorized));
              } else if (status == EventsLogApi.STATUS_TERMINATED_BY_REMOTE) {
                // The session was terminated: only a restart may be done
                restartChat = new RestartChat(ChatList.this, messagingApi, cache.chatId);
                restartChat.start();
              } else {
                // Session terminated on the device: try to rejoin the session
                rejoinChat = new RejoinChat(ChatList.this, messagingApi, cache.chatId);
                rejoinChat.start();
              }
            }
          } else {
            // 1-1 chat
            IChatSession session = isChatSessionActive(cache.sessionId);
            if (session != null) {
              // Session already active on the device: just reload it in the UI
              try {
                Intent intent = new Intent(ChatList.this, OneToOneChatView.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                intent.putExtra("contact", session.getRemoteContact());
                intent.putExtra("sessionId", session.getSessionID());
                startActivity(intent);
              } catch (Exception e) {
                Utils.showMessage(ChatList.this, getString(R.string.label_api_failed));
              }
            } else {
              // Session terminated on the device: create a new one on the first message
              Intent intent = new Intent(ChatList.this, OneToOneChatView.class);
              intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
              intent.putExtra("contact", cache.contact);
              startActivity(intent);
            }
          }
        }