Java Code Examples for android.accounts.AccountManagerFuture#getResult()

The following examples show how to use android.accounts.AccountManagerFuture#getResult() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AndroidAuthenticator.java    From android_tv_metro with Apache License 2.0 6 votes vote down vote up
@Override
public String getAuthToken() throws AuthFailureError {
    final AccountManager accountManager = AccountManager.get(mContext);
    AccountManagerFuture<Bundle> future = accountManager.getAuthToken(mAccount,
            mAuthTokenType, mNotifyAuthFailure, null, null);
    Bundle result;
    try {
        result = future.getResult();
    } catch (Exception e) {
        throw new AuthFailureError("Error while retrieving auth token", e);
    }
    String authToken = null;
    if (future.isDone() && !future.isCancelled()) {
        if (result.containsKey(AccountManager.KEY_INTENT)) {
            Intent intent = result.getParcelable(AccountManager.KEY_INTENT);
            throw new AuthFailureError(intent);
        }
        authToken = result.getString(AccountManager.KEY_AUTHTOKEN);
    }
    if (authToken == null) {
        throw new AuthFailureError("Got null auth token for type: " + mAuthTokenType);
    }

    return authToken;
}
 
Example 2
Source File: AndroidAuthenticator.java    From TitanjumNote with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("deprecation")
@Override
public String getAuthToken() throws AuthFailureError {
    AccountManagerFuture<Bundle> future = mAccountManager.getAuthToken(mAccount,
            mAuthTokenType, mNotifyAuthFailure, null, null);
    Bundle result;
    try {
        result = future.getResult();
    } catch (Exception e) {
        throw new AuthFailureError("Error while retrieving auth token", e);
    }
    String authToken = null;
    if (future.isDone() && !future.isCancelled()) {
        if (result.containsKey(AccountManager.KEY_INTENT)) {
            Intent intent = result.getParcelable(AccountManager.KEY_INTENT);
            throw new AuthFailureError(intent);
        }
        authToken = result.getString(AccountManager.KEY_AUTHTOKEN);
    }
    if (authToken == null) {
        throw new AuthFailureError("Got null auth token for type: " + mAuthTokenType);
    }

    return authToken;
}
 
Example 3
Source File: AndroidAuthenticator.java    From WayHoo with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("deprecation")
@Override
public String getAuthToken() throws AuthFailureError {
    final AccountManager accountManager = AccountManager.get(mContext);
    AccountManagerFuture<Bundle> future = accountManager.getAuthToken(mAccount,
            mAuthTokenType, mNotifyAuthFailure, null, null);
    Bundle result;
    try {
        result = future.getResult();
    } catch (Exception e) {
        throw new AuthFailureError("Error while retrieving auth token", e);
    }
    String authToken = null;
    if (future.isDone() && !future.isCancelled()) {
        if (result.containsKey(AccountManager.KEY_INTENT)) {
            Intent intent = result.getParcelable(AccountManager.KEY_INTENT);
            throw new AuthFailureError(intent);
        }
        authToken = result.getString(AccountManager.KEY_AUTHTOKEN);
    }
    if (authToken == null) {
        throw new AuthFailureError("Got null auth token for type: " + mAuthTokenType);
    }

    return authToken;
}
 
Example 4
Source File: AndroidAuthenticator.java    From DaVinci with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("deprecation")
@Override
public String getAuthToken() throws AuthFailureError {
    AccountManagerFuture<Bundle> future = mAccountManager.getAuthToken(mAccount,
            mAuthTokenType, mNotifyAuthFailure, null, null);
    Bundle result;
    try {
        result = future.getResult();
    } catch (Exception e) {
        throw new AuthFailureError("Error while retrieving auth token", e);
    }
    String authToken = null;
    if (future.isDone() && !future.isCancelled()) {
        if (result.containsKey(AccountManager.KEY_INTENT)) {
            Intent intent = result.getParcelable(AccountManager.KEY_INTENT);
            throw new AuthFailureError(intent);
        }
        authToken = result.getString(AccountManager.KEY_AUTHTOKEN);
    }
    if (authToken == null) {
        throw new AuthFailureError("Got null auth token for type: " + mAuthTokenType);
    }

    return authToken;
}
 
Example 5
Source File: BaseActivity.java    From attendee-checkin with Apache License 2.0 6 votes vote down vote up
@Override
public void run(AccountManagerFuture<Bundle> bundleAccountManagerFuture) {
    try {
        Bundle result = bundleAccountManagerFuture.getResult();
        Intent intent = (Intent) result.get(AccountManager.KEY_INTENT);
        if (intent == null) {
            String authToken = result.getString(AccountManager.KEY_AUTHTOKEN);
            GutenbergApplication app = GutenbergApplication.from(BaseActivity.this);
            app.setAuthToken(authToken);
            app.requestSync(false);
        } else {
            startActivityForResult(intent, REQUEST_AUTHENTICATE);
        }
    } catch (OperationCanceledException | IOException | AuthenticatorException e) {
        Log.e(TAG, "Error authenticating.", e);
    }
}
 
Example 6
Source File: AndroidAuthenticator.java    From product-emm with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("deprecation")
@Override
public String getAuthToken() throws AuthFailureError {
    AccountManagerFuture<Bundle> future = mAccountManager.getAuthToken(mAccount,
            mAuthTokenType, mNotifyAuthFailure, null, null);
    Bundle result;
    try {
        result = future.getResult();
    } catch (Exception e) {
        throw new AuthFailureError("Error while retrieving auth token", e);
    }
    String authToken = null;
    if (future.isDone() && !future.isCancelled()) {
        if (result.containsKey(AccountManager.KEY_INTENT)) {
            Intent intent = result.getParcelable(AccountManager.KEY_INTENT);
            throw new AuthFailureError(intent);
        }
        authToken = result.getString(AccountManager.KEY_AUTHTOKEN);
    }
    if (authToken == null) {
        throw new AuthFailureError("Got null auth token for type: " + mAuthTokenType);
    }

    return authToken;
}
 
Example 7
Source File: AndroidAuthenticator.java    From android-project-wo2b with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("deprecation")
@Override
public String getAuthToken() throws AuthFailureError {
    final AccountManager accountManager = AccountManager.get(mContext);
    AccountManagerFuture<Bundle> future = accountManager.getAuthToken(mAccount,
            mAuthTokenType, mNotifyAuthFailure, null, null);
    Bundle result;
    try {
        result = future.getResult();
    } catch (Exception e) {
        throw new AuthFailureError("Error while retrieving auth token", e);
    }
    String authToken = null;
    if (future.isDone() && !future.isCancelled()) {
        if (result.containsKey(AccountManager.KEY_INTENT)) {
            Intent intent = result.getParcelable(AccountManager.KEY_INTENT);
            throw new AuthFailureError(intent);
        }
        authToken = result.getString(AccountManager.KEY_AUTHTOKEN);
    }
    if (authToken == null) {
        throw new AuthFailureError("Got null auth token for type: " + mAuthTokenType);
    }

    return authToken;
}
 
Example 8
Source File: AccountResolver.java    From RememBirthday with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Remove account from Android system
 */
@SuppressWarnings("deprecation")
public boolean removeAccount() {
    Log.d(getClass().getSimpleName(), "Removing account : " + account.name);

    AccountManager accountManager = AccountManager.get(context);
    // remove account
    AccountManagerFuture accountManagerFuture;
    if(android.os.Build.VERSION.SDK_INT < 23) {
        accountManagerFuture = accountManager.removeAccount(account, null, null);
    } else {
        accountManagerFuture = accountManager.removeAccount(account, null, null, null);
    }
    if (accountManagerFuture.isDone()) {
        try {
            accountManagerFuture.getResult();
            return true;
        } catch (Exception e) {
            Log.e(getClass().getSimpleName(), "Problem while removing account!", e);
            return false;
        }
    } else {
        return false;
    }
}
 
Example 9
Source File: OdooAccountManager.java    From framework with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Remove account from device
 *
 * @param context
 * @param username
 * @return true, if account removed successfully
 */
public static boolean removeAccount(Context context, String username) {
    OUser user = getDetails(context, username);
    if (user != null) {
        AccountManager accountManager = AccountManager.get(context);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
            if (accountManager.removeAccountExplicitly(user.getAccount())) {
                dropDatabase(user);
            }
            return true;
        } else {
            try {
                AccountManagerFuture<Boolean> result = accountManager.
                        removeAccount(user.getAccount(), null, null);
                if (result.getResult()) {
                    dropDatabase(user);
                }
                return true;
            } catch (OperationCanceledException | IOException | AuthenticatorException e) {
                e.printStackTrace();
            }
        }
    }
    return false;
}
 
Example 10
Source File: AndroidAuthenticator.java    From SaveVolley with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("deprecation") @Override public String getAuthToken()
        throws AuthFailureError {
    AccountManagerFuture<Bundle> future = mAccountManager.getAuthToken(mAccount, mAuthTokenType,
            mNotifyAuthFailure, null, null);
    Bundle result;
    try {
        result = future.getResult();
    } catch (Exception e) {
        throw new AuthFailureError("Error while retrieving auth token", e);
    }
    String authToken = null;
    if (future.isDone() && !future.isCancelled()) {
        if (result.containsKey(AccountManager.KEY_INTENT)) {
            Intent intent = result.getParcelable(AccountManager.KEY_INTENT);
            throw new AuthFailureError(intent);
        }
        authToken = result.getString(AccountManager.KEY_AUTHTOKEN);
    }
    if (authToken == null) {
        throw new AuthFailureError("Got null auth token for type: " + mAuthTokenType);
    }

    return authToken;
}
 
Example 11
Source File: AndroidAuthenticator.java    From device-database with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("deprecation")
@Override
public String getAuthToken() throws AuthFailureError {
    AccountManagerFuture<Bundle> future = mAccountManager.getAuthToken(mAccount,
            mAuthTokenType, mNotifyAuthFailure, null, null);
    Bundle result;
    try {
        result = future.getResult();
    } catch (Exception e) {
        throw new AuthFailureError("Error while retrieving auth token", e);
    }
    String authToken = null;
    if (future.isDone() && !future.isCancelled()) {
        if (result.containsKey(AccountManager.KEY_INTENT)) {
            Intent intent = result.getParcelable(AccountManager.KEY_INTENT);
            throw new AuthFailureError(intent);
        }
        authToken = result.getString(AccountManager.KEY_AUTHTOKEN);
    }
    if (authToken == null) {
        throw new AuthFailureError("Got null auth token for type: " + mAuthTokenType);
    }

    return authToken;
}
 
Example 12
Source File: AndroidAuthenticator.java    From AndroidProjects with MIT License 6 votes vote down vote up
@SuppressWarnings("deprecation")
@Override
public String getAuthToken() throws AuthFailureError {
    AccountManagerFuture<Bundle> future = mAccountManager.getAuthToken(mAccount,
            mAuthTokenType, mNotifyAuthFailure, null, null);
    Bundle result;
    try {
        result = future.getResult();
    } catch (Exception e) {
        throw new AuthFailureError("Error while retrieving auth token", e);
    }
    String authToken = null;
    if (future.isDone() && !future.isCancelled()) {
        if (result.containsKey(AccountManager.KEY_INTENT)) {
            Intent intent = result.getParcelable(AccountManager.KEY_INTENT);
            throw new AuthFailureError(intent);
        }
        authToken = result.getString(AccountManager.KEY_AUTHTOKEN);
    }
    if (authToken == null) {
        throw new AuthFailureError("Got null auth token for type: " + mAuthTokenType);
    }

    return authToken;
}
 
Example 13
Source File: AndroidAuthenticator.java    From pearl with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("deprecation")
@Override
public String getAuthToken() throws AuthFailureError {
    AccountManagerFuture<Bundle> future = mAccountManager.getAuthToken(mAccount,
            mAuthTokenType, mNotifyAuthFailure, null, null);
    Bundle result;
    try {
        result = future.getResult();
    } catch (Exception e) {
        throw new AuthFailureError("Error while retrieving auth token", e);
    }
    String authToken = null;
    if (future.isDone() && !future.isCancelled()) {
        if (result.containsKey(AccountManager.KEY_INTENT)) {
            Intent intent = result.getParcelable(AccountManager.KEY_INTENT);
            throw new AuthFailureError(intent);
        }
        authToken = result.getString(AccountManager.KEY_AUTHTOKEN);
    }
    if (authToken == null) {
        throw new AuthFailureError("Got null auth token for type: " + mAuthTokenType);
    }

    return authToken;
}
 
Example 14
Source File: AndroidAuthenticator.java    From volley with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("deprecation")
@Override
public String getAuthToken() throws AuthFailureError {
    final AccountManager accountManager = AccountManager.get(mContext);
    AccountManagerFuture<Bundle> future = accountManager.getAuthToken(mAccount,
            mAuthTokenType, mNotifyAuthFailure, null, null);
    Bundle result;
    try {
        result = future.getResult();
    } catch (Exception e) {
        throw new AuthFailureError("Error while retrieving auth token", e);
    }
    String authToken = null;
    if (future.isDone() && !future.isCancelled()) {
        if (result.containsKey(AccountManager.KEY_INTENT)) {
            Intent intent = result.getParcelable(AccountManager.KEY_INTENT);
            throw new AuthFailureError(intent);
        }
        authToken = result.getString(AccountManager.KEY_AUTHTOKEN);
    }
    if (authToken == null) {
        throw new AuthFailureError("Got null auth token for type: " + mAuthTokenType);
    }

    return authToken;
}
 
Example 15
Source File: AndroidAuthenticator.java    From barterli_android with Apache License 2.0 6 votes vote down vote up
@Override
public String getAuthToken() throws AuthFailureError {
    final AccountManager accountManager = AccountManager.get(mContext);
    AccountManagerFuture<Bundle> future = accountManager.getAuthToken(mAccount,
            mAuthTokenType, mNotifyAuthFailure, null, null);
    Bundle result;
    try {
        result = future.getResult();
    } catch (Exception e) {
        throw new AuthFailureError("Error while retrieving auth token", e);
    }
    String authToken = null;
    if (future.isDone() && !future.isCancelled()) {
        if (result.containsKey(AccountManager.KEY_INTENT)) {
            Intent intent = result.getParcelable(AccountManager.KEY_INTENT);
            throw new AuthFailureError(intent);
        }
        authToken = result.getString(AccountManager.KEY_AUTHTOKEN);
    }
    if (authToken == null) {
        throw new AuthFailureError("Got null auth token for type: " + mAuthTokenType);
    }

    return authToken;
}
 
Example 16
Source File: AndroidAuthenticator.java    From jus with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("deprecation")
@Override
public String getToken() throws AndroidAuthError {
    AccountManagerFuture<Bundle> future = mAccountManager.getAuthToken(mAccount,
            mAuthTokenType, mNotifyAuthFailure, null, null);
    Bundle result;
    try {
        result = future.getResult();
    } catch (Exception e) {
        throw new AndroidAuthError("Error while retrieving auth token", e);
    }
    if (future.isDone() && !future.isCancelled()) {
        if (result.containsKey(AccountManager.KEY_INTENT)) {
            Intent intent = result.getParcelable(AccountManager.KEY_INTENT);
            throw new AndroidAuthError(intent, null);
        }
        authToken = result.getString(AccountManager.KEY_AUTHTOKEN);
    }
    if (authToken == null) {
        throw new AndroidAuthError("Got null auth token for type: " +
                mAuthTokenType);
    }

    return authToken;
}
 
Example 17
Source File: AndroidAuthenticator.java    From SaveVolley with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("deprecation") @Override public String getAuthToken()
        throws AuthFailureError {
    /*
     * 先通过 Account + tokenType
     * 获取一个 AmsTask<Bundle> ( AccountManagerFuture<Bundle> 的实现类 )
     */
    AccountManagerFuture<Bundle> future = mAccountManager.getAuthToken(mAccount, mAuthTokenType,
            mNotifyAuthFailure, null, null);
    Bundle result;
    try {
        // 从 AmsTask<Bundle> 抽出出 Bundle 数据
        result = future.getResult();
    } catch (Exception e) {
        throw new AuthFailureError("Error while retrieving auth token", e);
    }
    String authToken = null;
    // 判断 AmsTask<Bundle> 是否正常执行
    if (future.isDone() && !future.isCancelled()) {
        if (result.containsKey(AccountManager.KEY_INTENT)) {
            Intent intent = result.getParcelable(AccountManager.KEY_INTENT);
            throw new AuthFailureError(intent);
        }
        // 拿到认证 token 数据
        authToken = result.getString(AccountManager.KEY_AUTHTOKEN);
    }
    if (authToken == null) {
        throw new AuthFailureError("Got null auth token for type: " + mAuthTokenType);
    }

    return authToken;
}
 
Example 18
Source File: LogonManager.java    From letv with Apache License 2.0 5 votes vote down vote up
public void run(AccountManagerFuture<Bundle> arg0) {
    try {
        Bundle result = (Bundle) arg0.getResult();
        if (result != null) {
            LemallPlatform.getInstance().setSsoToken(result.getString("authtoken"));
        }
    } catch (OperationCanceledException e) {
        e.printStackTrace();
    } catch (AuthenticatorException e2) {
        e2.printStackTrace();
    } catch (IOException e3) {
        e3.printStackTrace();
    }
}
 
Example 19
Source File: OIDCAccountManager.java    From android-java-connect-rest-sample with MIT License 5 votes vote down vote up
public boolean removeAccount(Account account) {
    boolean removed = false;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1){
        removed = this.manager.removeAccountExplicitly(account);
    }
    else {
        @SuppressWarnings("deprecation") AccountManagerFuture<Boolean> futureRemoved = this.manager.removeAccount(account, null, null);
        try {
            removed = futureRemoved.getResult();
        } catch (OperationCanceledException | IOException | AuthenticatorException e) {
            Log.w("LogoutTask", "Coudln't remove account using pre LOLIPOP remove call");
        }
    }
    return removed;
}
 
Example 20
Source File: AccountSnippet.java    From mobly-bundled-snippets with Apache License 2.0 4 votes vote down vote up
/**
 * Adds a Google account to the device.
 *
 * @param username Username of the account to add (including @gmail.com).
 * @param password Password of the account to add.
 */
@Rpc(
        description =
                "Add a Google (GMail) account to the device, with account data sync disabled.")
public void addAccount(String username, String password)
        throws AccountSnippetException, AccountsException, IOException {
    // Check for existing account. If we try to re-add an existing account, Android throws an
    // exception that says "Account does not exist or not visible. Maybe change pwd?" which is
    // a little hard to understand.
    if (listAccounts().contains(username)) {
        throw new AccountSnippetException(
                "Account " + username + " already exists on the device");
    }
    Bundle addAccountOptions = new Bundle();
    addAccountOptions.putString("username", username);
    addAccountOptions.putString("password", password);
    AccountManagerFuture<Bundle> future =
            mAccountManager.addAccount(
                    GOOGLE_ACCOUNT_TYPE,
                    AUTH_TOKEN_TYPE,
                    null /* requiredFeatures */,
                    addAccountOptions,
                    null /* activity */,
                    null /* authCallback */,
                    null /* handler */);
    Bundle result = future.getResult();
    if (result.containsKey(AccountManager.KEY_ERROR_CODE)) {
        throw new AccountSnippetException(
                String.format(
                        Locale.US,
                        "Failed to add account due to code %d: %s",
                        result.getInt(AccountManager.KEY_ERROR_CODE),
                        result.getString(AccountManager.KEY_ERROR_MESSAGE)));
    }

    // Disable sync to avoid test flakiness as accounts fetch additional data.
    // It takes a while for all sync adapters to be populated, so register for broadcasts when
    // sync is starting and disable them there.
    // NOTE: this listener is NOT unregistered because several sync requests for the new account
    // will come in over time.
    Account account = new Account(username, GOOGLE_ACCOUNT_TYPE);
    Object handle =
            ContentResolver.addStatusChangeListener(
                    ContentResolver.SYNC_OBSERVER_TYPE_ACTIVE
                            | ContentResolver.SYNC_OBSERVER_TYPE_PENDING,
                    which -> {
                        for (SyncAdapterType adapter : ContentResolver.getSyncAdapterTypes()) {
                            // Ignore non-Google account types.
                            if (!adapter.accountType.equals(GOOGLE_ACCOUNT_TYPE)) {
                                continue;
                            }
                            // If a content provider is not whitelisted, then disable it.
                            // Because startSync and stopSync synchronously update the whitelist
                            // and sync settings, writelock both the whitelist check and the
                            // call to sync together.
                            mLock.writeLock().lock();
                            try {
                                if (!isAdapterWhitelisted(username, adapter.authority)) {
                                    updateSync(account, adapter.authority, false /* sync */);
                                }
                            } finally {
                                mLock.writeLock().unlock();
                            }
                        }
                    });
    mSyncStatusObserverHandles.add(handle);
}