@Override
 public void onAuthCreated(BoxAuthentication.BoxAuthenticationInfo info) {
   BoxAuthentication.BoxAuthenticationInfo.cloneInfo(mSession.mAuthInfo, info);
   mSession.setUserId(info.getUser().getId());
   mSession.onAuthCreated(info);
   authLatch.countDown();
 }
 /**
  * This is an advanced constructor that can be used when implementing an authentication flow that
  * differs from the default oauth 2 flow.
  *
  * @param context current context.
  * @param authInfo authentication information that should be used. (Must at the minimum provide an
  *     access token).
  * @param refreshProvider the refresh provider to use when the access token expires and needs to
  *     be refreshed.
  */
 public BoxSession(
     Context context,
     BoxAuthentication.BoxAuthenticationInfo authInfo,
     BoxAuthentication.AuthenticationRefreshProvider refreshProvider) {
   mApplicationContext = context.getApplicationContext();
   mAuthInfo = authInfo;
   mRefreshProvider = refreshProvider;
   if (authInfo.getUser() != null) {
     if (!SdkUtils.isBlank(authInfo.getUser().getId())) {
       mUserId = authInfo.getUser().getId();
     }
   }
 }
 /**
  * Called when this user has logged in.
  *
  * @param info the latest info from going through the login flow.
  */
 @Override
 public void onAuthCreated(BoxAuthentication.BoxAuthenticationInfo info) {
   if (sameUser(info)) {
     BoxAuthentication.BoxAuthenticationInfo.cloneInfo(mAuthInfo, info);
     if (sessionAuthListener != null) {
       sessionAuthListener.onAuthCreated(info);
     }
   }
 }
    public BoxSession send() throws BoxException {
      synchronized (mSession) {
        if (mSession.getUser() == null) {
          if (mSession.getAuthInfo() != null
              && !SdkUtils.isBlank(mSession.getAuthInfo().accessToken())) {

            // if we have an access token, but no user try to repair by making the call to user
            // endpoint.
            try {
              // TODO: show some ui while requestion user info
              BoxApiUser apiUser = new BoxApiUser(mSession);
              BoxUser user = apiUser.getCurrentUserInfoRequest().send();

              mSession.setUserId(user.getId());
              mSession.getAuthInfo().setUser(user);
              mSession.onAuthCreated(mSession.getAuthInfo());
              return mSession;

            } catch (BoxException e) {
              BoxLogUtils.e("BoxSession", "Unable to repair user", e);
              if (e instanceof BoxException.RefreshFailure
                  && ((BoxException.RefreshFailure) e).isErrorFatal()) {
                // if the refresh failure is unrecoverable have the user login again.
                toastString(mSession.getApplicationContext(), R.string.boxsdk_error_fatal_refresh);
              } else if (e.getErrorType() == BoxException.ErrorType.TERMS_OF_SERVICE_REQUIRED) {
                toastString(
                    mSession.getApplicationContext(), R.string.boxsdk_error_terms_of_service);
              } else {
                mSession.onAuthFailure(null, e);
                throw e;
              }
            }
            // at this point we were unable to repair.

          }
          BoxAuthentication.getInstance().addListener(this);
          launchAuthUI();
          return mSession;
        } else {
          BoxAuthentication.BoxAuthenticationInfo info =
              BoxAuthentication.getInstance()
                  .getAuthInfo(mSession.getUserId(), mSession.getApplicationContext());
          if (info != null) {
            BoxAuthentication.BoxAuthenticationInfo.cloneInfo(mSession.mAuthInfo, info);
            mSession.onAuthCreated(mSession.getAuthInfo());
          } else {
            // Fail to get information of current user. current use no longer valid.
            mSession.mAuthInfo.setUser(null);
            launchAuthUI();
          }
        }

        return mSession;
      }
    }
 public BoxSession send() throws BoxException {
   try {
     // block until this session is finished refreshing.
     BoxAuthentication.BoxAuthenticationInfo refreshedInfo =
         BoxAuthentication.getInstance().refresh(mSession).get();
   } catch (Exception e) {
     BoxException r = (BoxException) e.getCause();
     if (e.getCause() instanceof BoxException) {
       throw (BoxException) e.getCause();
     } else {
       throw new BoxException("BoxSessionRefreshRequest failed", e);
     }
   }
   BoxAuthentication.BoxAuthenticationInfo.cloneInfo(
       mSession.mAuthInfo,
       BoxAuthentication.getInstance()
           .getAuthInfo(mSession.getUserId(), mSession.getApplicationContext()));
   return mSession;
 }
 /**
  * Create a BoxSession using a specific box clientId, secret, and redirectUrl. This constructor is
  * not necessary unless an application uses multiple api keys. Note: When setting the userId to
  * null ui will be shown to ask which user to authenticate as if at least one user is logged in.
  * If no user has been stored will show login ui.
  *
  * @param context current context.
  * @param clientId the developer's client id to access the box api.
  * @param clientSecret the developer's secret used to interpret the response coming from Box.
  * @param redirectUrl the developer's redirect url to use for authenticating via Box.
  * @param userId user id to login as or null to login as a new user.
  */
 public BoxSession(
     Context context, String userId, String clientId, String clientSecret, String redirectUrl) {
   mClientId = clientId;
   mClientSecret = clientSecret;
   mClientRedirectUrl = redirectUrl;
   if (SdkUtils.isEmptyString(mClientId) || SdkUtils.isEmptyString(mClientSecret)) {
     throw new RuntimeException(
         "Session must have a valid client id and client secret specified.");
   }
   mApplicationContext = context.getApplicationContext();
   if (!SdkUtils.isEmptyString(userId)) {
     mAuthInfo = BoxAuthentication.getInstance().getAuthInfo(userId, context);
     mUserId = userId;
   }
   if (mAuthInfo == null) {
     mUserId = userId;
     mAuthInfo = new BoxAuthentication.BoxAuthenticationInfo();
   }
   mAuthInfo.setClientId(mClientId);
   setupSession();
 }
 private boolean sameUser(BoxAuthentication.BoxAuthenticationInfo info) {
   return info != null
       && info.getUser() != null
       && getUserId() != null
       && getUserId().equals(info.getUser().getId());
 }
 /**
  * @return the user associated with this session. May return null if this is a new session before
  *     authentication.
  */
 public BoxUser getUser() {
   return mAuthInfo.getUser();
 }
 private static BoxAuthentication.BoxAuthenticationInfo createSimpleBoxAuthenticationInfo(
     final String accessToken) {
   BoxAuthentication.BoxAuthenticationInfo info = new BoxAuthentication.BoxAuthenticationInfo();
   info.setAccessToken(accessToken);
   return info;
 }