private boolean ensureUi() {

    SharedPreferences settings = getPreferences(MODE_PRIVATE);
    String tToken = settings.getString("4sqToken", "none");

    if (tToken != "none") {
      ExampleTokenStore.get().setToken(tToken);
    }

    boolean isAuthorized = !TextUtils.isEmpty(ExampleTokenStore.get().getToken());

    if (!isAuthorized) {
      Intent intent = FoursquareOAuth.getConnectIntent(MainActivity.this, CLIENT_ID);

      // If the device does not have the Foursquare app installed, we'd
      // get an intent back that would open the Play Store for download.
      // Otherwise we start the auth flow.
      if (FoursquareOAuth.isPlayStoreIntent(intent)) {
        Intent fourIntent = new Intent(MainActivity.this, DownloadFoursquare.class);
        startActivity(fourIntent);
        return false;
      } else {
        startActivityForResult(intent, REQUEST_CODE_FSQ_CONNECT);
        return true;
      }
    } else {
      SharedPreferences.Editor editor = settings.edit();
      editor.putString("4sqToken", ExampleTokenStore.get().getToken().toString());
      editor.commit();
      return true;
    }
  }
  private void onCompleteTokenExchange(int resultCode, Intent data) {
    AccessTokenResponse tokenResponse = FoursquareOAuth.getTokenFromResult(resultCode, data);
    Exception exception = tokenResponse.getException();

    if (exception == null) {
      String accessToken = tokenResponse.getAccessToken();
      // Success.
      // toastMessage(this, "Access token: " + accessToken);

      // Persist the token for later use. In this example, we save
      // it to shared prefs.
      ExampleTokenStore.get().setToken(accessToken);

      // Refresh UI.
      ensureUi();

    } else {
      if (exception instanceof FoursquareOAuthException) {
        // OAuth error.
        String errorMessage = ((FoursquareOAuthException) exception).getMessage();
        String errorCode = ((FoursquareOAuthException) exception).getErrorCode();
        toastMessage(this, errorMessage + " [" + errorCode + "]");

      } else {
        // Other exception type.
        toastError(this, exception);
      }
    }
  }
  private void onCompleteConnect(int resultCode, Intent data) {
    AuthCodeResponse codeResponse = FoursquareOAuth.getAuthCodeFromResult(resultCode, data);
    Exception exception = codeResponse.getException();

    if (exception == null) {
      // Success.
      String code = codeResponse.getCode();
      performTokenExchange(code);

    } else {
      if (exception instanceof FoursquareCancelException) {
        // Cancel.
        toastMessage(this, "Canceled");

      } else if (exception instanceof FoursquareDenyException) {
        // Deny.
        toastMessage(this, "Denied");

      } else if (exception instanceof FoursquareOAuthException) {
        // OAuth error.
        String errorMessage = exception.getMessage();
        String errorCode = ((FoursquareOAuthException) exception).getErrorCode();
        toastMessage(this, errorMessage + " [" + errorCode + "]");

      } else if (exception instanceof FoursquareUnsupportedVersionException) {
        // Unsupported Fourquare app version on the device.
        toastError(this, exception);

      } else if (exception instanceof FoursquareInvalidRequestException) {
        // Invalid request.
        toastError(this, exception);

      } else {
        // Error.
        toastError(this, exception);
      }
    }
  }
 private void performTokenExchange(String code) {
   Intent intent = FoursquareOAuth.getTokenExchangeIntent(this, CLIENT_ID, CLIENT_SECRET, code);
   startActivityForResult(intent, REQUEST_CODE_FSQ_TOKEN_EXCHANGE);
 }