public static void goToRoomPage(
      final MXSession aSession,
      final String roomId,
      final Activity fromActivity,
      final Intent intentParam) {
    // check first if the 1:1 room already exists
    MXSession session = (aSession == null) ? Matrix.getMXSession(fromActivity, null) : aSession;

    // sanity check
    if ((null == session) || !session.isActive()) {
      return;
    }

    final MXSession fSession = session;

    Room room = session.getDataHandler().getRoom(roomId);

    // do not open a leaving room.
    // it does not make.
    if ((null != room) && (room.isLeaving())) {
      return;
    }

    fromActivity.runOnUiThread(
        new Runnable() {
          @Override
          public void run() {
            // if the activity is not the home activity
            if (!(fromActivity instanceof HomeActivity)) {
              // pop to the home activity
              Intent intent = new Intent(fromActivity, HomeActivity.class);
              intent.setFlags(
                  android.content.Intent.FLAG_ACTIVITY_CLEAR_TOP
                      | android.content.Intent.FLAG_ACTIVITY_SINGLE_TOP);
              intent.putExtra(HomeActivity.EXTRA_JUMP_TO_ROOM_ID, roomId);
              intent.putExtra(HomeActivity.EXTRA_JUMP_MATRIX_ID, fSession.getCredentials().userId);
              if (null != intentParam) {
                intent.putExtra(HomeActivity.EXTRA_ROOM_INTENT, intentParam);
              }
              fromActivity.startActivity(intent);
            } else {
              // already to the home activity
              // so just need to open the room activity
              Intent intent = new Intent(fromActivity, RoomActivity.class);
              intent.putExtra(RoomActivity.EXTRA_ROOM_ID, roomId);
              intent.putExtra(RoomActivity.EXTRA_MATRIX_ID, fSession.getCredentials().userId);
              if (null != intentParam) {
                intent.putExtra(HomeActivity.EXTRA_ROOM_INTENT, intentParam);
              }
              fromActivity.startActivity(intent);
            }
          }
        });
  }
  public static void logout(Activity activity, MXSession session, Boolean clearCredentials) {
    if (session.isActive()) {
      // stop the service
      EventStreamService eventStreamService = EventStreamService.getInstance();
      ArrayList<String> matrixIds = new ArrayList<String>();
      matrixIds.add(session.getMyUser().userId);
      eventStreamService.stopAccounts(matrixIds);

      // Publish to the server that we're now offline
      MyPresenceManager.getInstance(activity, session).advertiseOffline();
      MyPresenceManager.remove(session);

      // unregister from the GCM.
      Matrix.getInstance(activity)
          .getSharedGcmRegistrationManager()
          .unregisterSession(session, null);

      // clear credentials
      Matrix.getInstance(activity).clearSession(activity, session, clearCredentials);
    }
  }
  /**
   * Offer to send some dedicated intent data to an existing room
   *
   * @param fromActivity the caller activity
   * @param intent the intent param
   * @param session the session/
   */
  public static void sendFilesTo(
      final Activity fromActivity, final Intent intent, final MXSession session) {
    // sanity check
    if ((null == session) || !session.isActive()) {
      return;
    }

    final ArrayList<RoomSummary> mergedSummaries = new ArrayList<RoomSummary>();
    mergedSummaries.addAll(session.getDataHandler().getStore().getSummaries());

    Collections.sort(
        mergedSummaries,
        new Comparator<RoomSummary>() {
          @Override
          public int compare(RoomSummary lhs, RoomSummary rhs) {
            if (lhs == null || lhs.getLatestEvent() == null) {
              return 1;
            } else if (rhs == null || rhs.getLatestEvent() == null) {
              return -1;
            }

            if (lhs.getLatestEvent().getOriginServerTs()
                > rhs.getLatestEvent().getOriginServerTs()) {
              return -1;
            } else if (lhs.getLatestEvent().getOriginServerTs()
                < rhs.getLatestEvent().getOriginServerTs()) {
              return 1;
            }
            return 0;
          }
        });

    AlertDialog.Builder builderSingle = new AlertDialog.Builder(fromActivity);
    builderSingle.setTitle(fromActivity.getText(R.string.send_files_in));
    final ArrayAdapter<String> arrayAdapter =
        new ArrayAdapter<String>(fromActivity, R.layout.dialog_room_selection);

    for (RoomSummary summary : mergedSummaries) {
      arrayAdapter.add(summary.getRoomName());
    }

    builderSingle.setNegativeButton(
        fromActivity.getText(R.string.cancel),
        new DialogInterface.OnClickListener() {
          @Override
          public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
          }
        });

    builderSingle.setAdapter(
        arrayAdapter,
        new DialogInterface.OnClickListener() {

          @Override
          public void onClick(DialogInterface dialog, final int which) {
            dialog.dismiss();
            fromActivity.runOnUiThread(
                new Runnable() {
                  @Override
                  public void run() {
                    RoomSummary summary = mergedSummaries.get(which);
                    CommonActivityUtils.goToRoomPage(
                        session, summary.getRoomId(), fromActivity, intent);
                  }
                });
          }
        });
    builderSingle.show();
  }
  public static void goToOneToOneRoom(
      final MXSession aSession,
      final String otherUserId,
      final Activity fromActivity,
      final ApiCallback<Void> callback) {
    // sanity check
    if (null == otherUserId) {
      return;
    }

    // check first if the 1:1 room already exists
    MXSession session = (aSession == null) ? Matrix.getMXSession(fromActivity, null) : aSession;

    // no session is provided
    if (null == session) {
      // get the default one.
      session = Matrix.getInstance(fromActivity.getApplicationContext()).getDefaultSession();
    }

    // sanity check
    if ((null == session) || !session.isActive()) {
      return;
    }

    final MXSession fSession = session;

    // so, list the existing room, and search the 2 users room with this other users
    String roomId = null;
    Collection<Room> rooms = session.getDataHandler().getStore().getRooms();

    for (Room room : rooms) {
      Collection<RoomMember> members = room.getMembers();

      if (members.size() == 2) {
        for (RoomMember member : members) {
          if (member.getUserId().equals(otherUserId)) {
            roomId = room.getRoomId();
            break;
          }
        }
      }
    }

    // the room already exists -> switch to it
    if (null != roomId) {
      CommonActivityUtils.goToRoomPage(session, roomId, fromActivity, null);

      // everything is ok
      if (null != callback) {
        callback.onSuccess(null);
      }
    } else {
      session.createRoom(
          null,
          null,
          RoomState.VISIBILITY_PRIVATE,
          null,
          new SimpleApiCallback<String>(fromActivity) {

            @Override
            public void onSuccess(String roomId) {
              final Room room = fSession.getDataHandler().getRoom(roomId);

              room.invite(
                  otherUserId,
                  new SimpleApiCallback<Void>(this) {
                    @Override
                    public void onSuccess(Void info) {
                      CommonActivityUtils.goToRoomPage(
                          fSession, room.getRoomId(), fromActivity, null);

                      callback.onSuccess(null);
                    }

                    @Override
                    public void onMatrixError(MatrixError e) {
                      if (null != callback) {
                        callback.onMatrixError(e);
                      }
                    }

                    @Override
                    public void onNetworkError(Exception e) {
                      if (null != callback) {
                        callback.onNetworkError(e);
                      }
                    }

                    @Override
                    public void onUnexpectedError(Exception e) {
                      if (null != callback) {
                        callback.onUnexpectedError(e);
                      }
                    }
                  });
            }

            @Override
            public void onMatrixError(MatrixError e) {
              if (null != callback) {
                callback.onMatrixError(e);
              }
            }

            @Override
            public void onNetworkError(Exception e) {
              if (null != callback) {
                callback.onNetworkError(e);
              }
            }

            @Override
            public void onUnexpectedError(Exception e) {
              if (null != callback) {
                callback.onUnexpectedError(e);
              }
            }
          });
    }
  }