예제 #1
0
    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;
      }
    }
예제 #2
0
 @Override
 public boolean onPrepareOptionsMenu(Menu menu) {
   int numAccounts = BoxAuthentication.getInstance().getStoredAuthInfo(this).keySet().size();
   menu.findItem(R.id.logoutAll).setVisible(numAccounts > 1);
   menu.findItem(R.id.logout).setVisible(numAccounts > 0);
   menu.findItem(R.id.switch_accounts)
       .setTitle(numAccounts > 0 ? R.string.switch_accounts : R.string.login);
   return true;
 }
예제 #3
0
 /**
  * @return the user id associated with the only logged in user. If no user is logged in or
  *     multiple users are logged in returns null.
  */
 private static String getBestStoredUserId(final Context context) {
   String lastAuthenticatedUserId =
       BoxAuthentication.getInstance().getLastAuthenticatedUserId(context);
   Map<String, BoxAuthentication.BoxAuthenticationInfo> authInfoMap =
       BoxAuthentication.getInstance().getStoredAuthInfo(context);
   if (authInfoMap != null) {
     if (!SdkUtils.isEmptyString(lastAuthenticatedUserId)
         && authInfoMap.get(lastAuthenticatedUserId) != null) {
       return lastAuthenticatedUserId;
     }
     if (authInfoMap.size() == 1) {
       for (String authUserId : authInfoMap.keySet()) {
         return authUserId;
       }
     }
   }
   return null;
 }
예제 #4
0
 public BoxSession send() throws BoxException {
   synchronized (mSession) {
     if (mSession.getUser() != null) {
       BoxAuthentication.getInstance().logout(mSession);
       mSession.getAuthInfo().wipeOutAuth();
     }
   }
   return mSession;
 }
예제 #5
0
 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;
 }
예제 #6
0
 protected void setupSession() {
   // Because BuildConfig.DEBUG is always false when library projects publish their release
   // variants we use ApplicationInfo
   boolean isDebug = false;
   try {
     if (mApplicationContext != null && mApplicationContext.getPackageManager() != null) {
       PackageInfo info =
           mApplicationContext
               .getPackageManager()
               .getPackageInfo(mApplicationContext.getPackageName(), 0);
       isDebug = ((info.applicationInfo.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0);
     }
   } catch (PackageManager.NameNotFoundException e) {
     // Do nothing -- debug mode will default to false
   }
   BoxConfig.IS_DEBUG = isDebug;
   BoxAuthentication.getInstance().addListener(this);
 }
예제 #7
0
 /**
  * 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();
 }