Example #1
0
 /**
  * Create intent to launch OAuthActivity using information from the given session.
  *
  * @param context context
  * @param session the BoxSession to use to get parameters required to authenticate via this
  *     activity.
  * @param loginViaBoxApp Whether login should be handled by the installed box android app. Set
  *     this to true only when you are sure or want to make sure user installed box android app and
  *     want to use box android app to login.
  * @return intent to launch OAuthActivity.
  */
 public static Intent createOAuthActivityIntent(
     final Context context, BoxSession session, boolean loginViaBoxApp) {
   Intent intent =
       createOAuthActivityIntent(
           context,
           session.getClientId(),
           session.getClientSecret(),
           session.getRedirectUrl(),
           loginViaBoxApp);
   intent.putExtra(EXTRA_SESSION, session);
   if (!SdkUtils.isEmptyString(session.getUserId())) {
     intent.putExtra(EXTRA_USER_ID_RESTRICTION, session.getUserId());
   }
   return intent;
 }
    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;
 }