/**
   * Logout the current user.
   *
   * @param activity the caller activity
   */
  public static void logout(Activity activity) {
    stopEventStream(activity);

    try {
      ShortcutBadger.setBadge(activity, 0);
    } catch (Exception e) {
    }

    // warn that the user logs out
    Collection<MXSession> sessions = Matrix.getMXSessions(activity);
    for (MXSession session : sessions) {
      // Publish to the server that we're now offline
      MyPresenceManager.getInstance(activity, session).advertiseOffline();
      MyPresenceManager.remove(session);
    }

    // clear the preferences
    PreferenceManager.getDefaultSharedPreferences(activity).edit().clear().commit();

    // clear credentials
    Matrix.getInstance(activity).clearSessions(activity, true);

    // reset the GCM
    Matrix.getInstance(activity).getSharedGcmRegistrationManager().reset();

    // reset the contacts
    PIDsRetriever.getIntance().reset();
    ContactsManager.reset();

    MXMediasCache.clearThumbnailsCache(activity);

    // go to login page
    activity.startActivity(new Intent(activity, LoginActivity.class));
    activity.finish();
  }
  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);
    }
  }