/**
   * Creates a WebdavClient setup for an ownCloud account
   *
   * <p>Do not call this method from the main thread.
   *
   * @param account The ownCloud account
   * @param appContext Android application context
   * @return A WebdavClient object ready to be used
   * @throws AuthenticatorException If the authenticator failed to get the authorization token for
   *     the account.
   * @throws OperationCanceledException If the authenticator operation was cancelled while getting
   *     the authorization token for the account.
   * @throws IOException If there was some I/O error while getting the authorization token for the
   *     account.
   * @throws AccountNotFoundException If 'account' is unknown for the AccountManager
   */
  public static WebdavClient createOwnCloudClient(Account account, Context appContext)
      throws OperationCanceledException, AuthenticatorException, IOException,
          AccountNotFoundException {
    // Log_OC.d(TAG, "Creating WebdavClient associated to " + account.name);

    Uri uri = Uri.parse(AccountUtils.constructFullURLForAccount(appContext, account));
    AccountManager am = AccountManager.get(appContext);
    boolean isOauth2 =
        am.getUserData(account, AccountAuthenticator.KEY_SUPPORTS_OAUTH2)
            != null; // TODO avoid calling to getUserData here
    boolean isSamlSso =
        am.getUserData(account, AccountAuthenticator.KEY_SUPPORTS_SAML_WEB_SSO) != null;
    WebdavClient client = createOwnCloudClient(uri, appContext, !isSamlSso);
    if (isOauth2) {
      String accessToken =
          am.blockingGetAuthToken(account, MainApp.getAuthTokenTypeAccessToken(), false);
      client.setBearerCredentials(
          accessToken); // TODO not assume that the access token is a bearer token

    } else if (isSamlSso) { // TODO avoid a call to getUserData here
      String accessToken =
          am.blockingGetAuthToken(account, MainApp.getAuthTokenTypeSamlSessionCookie(), false);
      client.setSsoSessionCookie(accessToken);

    } else {
      String username = account.name.substring(0, account.name.lastIndexOf('@'));
      // String password = am.getPassword(account);
      String password = am.blockingGetAuthToken(account, MainApp.getAuthTokenTypePass(), false);
      client.setBasicCredentials(username, password);
    }

    return client;
  }