Example usage for android.accounts AccountManagerFuture getResult

List of usage examples for android.accounts AccountManagerFuture getResult

Introduction

In this page you can find the example usage for android.accounts AccountManagerFuture getResult.

Prototype

V getResult() throws OperationCanceledException, IOException, AuthenticatorException;

Source Link

Document

Accessor for the future result the AccountManagerFuture represents.

Usage

From source file:at.bitfire.davdroid.ui.AccountActivity.java

private void deleteAccount() {
    AccountManager accountManager = AccountManager.get(this);

    if (Build.VERSION.SDK_INT >= 22)
        accountManager.removeAccount(account, this, new AccountManagerCallback<Bundle>() {
            @Override/*from   w w  w. j a  v  a 2 s.  co m*/
            public void run(AccountManagerFuture<Bundle> future) {
                try {
                    if (future.getResult().getBoolean(AccountManager.KEY_BOOLEAN_RESULT))
                        finish();
                } catch (OperationCanceledException | IOException | AuthenticatorException e) {
                    App.log.log(Level.SEVERE, "Couldn't remove account", e);
                }
            }
        }, null);
    else
        accountManager.removeAccount(account, new AccountManagerCallback<Boolean>() {
            @Override
            public void run(AccountManagerFuture<Boolean> future) {
                try {
                    if (future.getResult())
                        finish();
                } catch (OperationCanceledException | IOException | AuthenticatorException e) {
                    App.log.log(Level.SEVERE, "Couldn't remove account", e);
                }
            }
        }, null);
}

From source file:cn.code.notes.gtask.remote.GTaskClient.java

private String loginGoogleAccount(Activity activity, boolean invalidateToken) {
    String authToken;//  w  ww .j a v a 2s .c  o m
    AccountManager accountManager = AccountManager.get(activity);
    Account[] accounts = accountManager.getAccountsByType("com.google");

    if (accounts.length == 0) {
        Log.e(TAG, "there is no available google account");
        return null;
    }

    String accountName = SystemEditActivity.getSyncAccountName(activity);
    Account account = null;
    for (Account a : accounts) {
        if (a.name.equals(accountName)) {
            account = a;
            break;
        }
    }
    if (account != null) {
        mAccount = account;
    } else {
        Log.e(TAG, "unable to get an account with the same name in the settings");
        return null;
    }

    // get the token now
    AccountManagerFuture<Bundle> accountManagerFuture = accountManager.getAuthToken(account, "goanna_mobile",
            null, activity, null, null);
    try {
        Bundle authTokenBundle = accountManagerFuture.getResult();
        authToken = authTokenBundle.getString(AccountManager.KEY_AUTHTOKEN);
        if (invalidateToken) {
            accountManager.invalidateAuthToken("com.google", authToken);
            loginGoogleAccount(activity, false);
        }
    } catch (Exception e) {
        Log.e(TAG, "get auth token failed");
        authToken = null;
    }

    return authToken;
}

From source file:com.example.simba.myapplicationnote.gtask.remote.GTaskClient.java

private String loginGoogleAccount(Activity activity, boolean invalidateToken) {
    String authToken;//  w  w  w .  j a  v a 2s  .co  m
    AccountManager accountManager = AccountManager.get(activity);
    Account[] accounts = accountManager.getAccountsByType("com.google");

    if (accounts.length == 0) {
        Log.e(TAG, "there is no available google account");
        return null;
    }

    String accountName = NotesPreferenceActivity.getSyncAccountName(activity);
    Account account = null;
    for (Account a : accounts) {
        if (a.name.equals(accountName)) {
            account = a;
            break;
        }
    }
    if (account != null) {
        mAccount = account;
    } else {
        Log.e(TAG, "unable to get an account with the same name in the settings");
        return null;
    }

    // get the token now
    AccountManagerFuture<Bundle> accountManagerFuture = accountManager.getAuthToken(account, "goanna_mobile",
            null, activity, null, null);
    try {
        Bundle authTokenBundle = accountManagerFuture.getResult();
        authToken = authTokenBundle.getString(AccountManager.KEY_AUTHTOKEN);
        if (invalidateToken) {
            accountManager.invalidateAuthToken("com.google", authToken);
            loginGoogleAccount(activity, false);
        }
    } catch (Exception e) {
        Log.e(TAG, "get auth token failed");
        authToken = null;
    }

    return authToken;
}

From source file:com.jedi.setupwizard.ui.SetupWizardActivity.java

public void launchGoogleAccountSetup() {
    Bundle bundle = new Bundle();
    bundle.putBoolean(SetupWizard.EXTRA_FIRST_RUN, true);
    bundle.putBoolean(SetupWizard.EXTRA_ALLOW_SKIP, true);
    AccountManager.get(this).addAccount(SetupWizard.ACCOUNT_TYPE_GOOGLE, null, null, bundle, this,
            new AccountManagerCallback<Bundle>() {
                @Override/*from  ww w .  j a  va 2  s . c  om*/
                public void run(AccountManagerFuture<Bundle> bundleAccountManagerFuture) {
                    if (isDestroyed())
                        return; //There is a change this activity has been torn down.
                    String token = null;
                    try {
                        token = bundleAccountManagerFuture.getResult().getString(AccountManager.KEY_AUTHTOKEN);
                        mGoogleAccountSetupComplete = true;
                        Page page = mPageList.findPage(R.string.setup_google_account);
                        if (page != null) {
                            onPageFinished(page);
                        }
                    } catch (OperationCanceledException e) {
                    } catch (IOException e) {
                    } catch (AuthenticatorException e) {
                    }

                }
            }, null);
}

From source file:org.creativecommons.thelist.utils.ListUser.java

public void addNewAccount(String accountType, String authTokenType, final AuthCallback callback) {
    final AccountManagerFuture<Bundle> future = am.addAccount(accountType, authTokenType, null, null, mActivity,
            new AccountManagerCallback<Bundle>() {
                @Override/*from  w w  w  . j  av a2s  . co m*/
                public void run(AccountManagerFuture<Bundle> future) {
                    try {
                        Bundle bnd = future.getResult();
                        //TODO: does this addAccountExplicitly
                        //addSavedItemsToUserList();
                        Log.d(TAG, " > addNewAccount Bundle received: " + bnd);
                        callback.onSuccess(bnd.getString(AccountManager.KEY_AUTHTOKEN));

                    } catch (Exception e) {
                        //Log.d(TAG, e.getMessage());
                        Log.d(TAG, "addNewAccount > Error adding new account");
                    }
                }
            }, null);
}

From source file:me.philio.ghost.ui.NavigationDrawerFragment.java

@Override
public void run(AccountManagerFuture<Bundle> future) {
    try {/* w  w  w .  ja  v a  2s.  c o  m*/
        Bundle result = future.getResult();
    } catch (OperationCanceledException | IOException | AuthenticatorException e) {
        Log.e(TAG, "Add account operation failed: " + e.getMessage());
    }
}

From source file:com.he5ed.lib.cloudprovider.apis.BoxApi.java

@Override
public synchronized void prepareApi(OnPrepareListener prepareListener) {
    mPrepareListener = prepareListener;/* w w  w . ja v a2 s . c  o m*/

    AccountManager.get(mContext).getAuthToken(mAccount, CloudProvider.AUTH_TYPE, false,
            new AccountManagerCallback<Bundle>() {
                @Override
                public void run(AccountManagerFuture<Bundle> future) {
                    try {
                        mAccessToken = future.getResult().getString(AccountManager.KEY_AUTHTOKEN);

                        validateAccessToken();
                    } catch (OperationCanceledException e) {
                        e.printStackTrace();
                        Log.e(TAG, e.getMessage());
                        if (mPrepareListener != null)
                            mPrepareListener.onPrepareFail(e);
                    } catch (IOException e) {
                        e.printStackTrace();
                        Log.e(TAG, e.getMessage());
                        if (mPrepareListener != null)
                            mPrepareListener.onPrepareFail(e);
                    } catch (AuthenticatorException e) {
                        e.printStackTrace();
                        Log.e(TAG, e.getMessage());
                        if (mPrepareListener != null)
                            mPrepareListener.onPrepareFail(e);
                    }
                }
            }, null);
}

From source file:com.he5ed.lib.cloudprovider.apis.CloudDriveApi.java

@Override
public synchronized void prepareApi(BaseApi.OnPrepareListener prepareListener) {
    mPrepareListener = prepareListener;//from ww w  .ja v a2  s.  co  m

    AccountManager.get(mContext).getAuthToken(mAccount, CloudProvider.AUTH_TYPE, false,
            new AccountManagerCallback<Bundle>() {
                @Override
                public void run(AccountManagerFuture<Bundle> future) {
                    try {
                        mAccessToken = future.getResult().getString(AccountManager.KEY_AUTHTOKEN);

                        validateAccessToken();
                    } catch (OperationCanceledException e) {
                        e.printStackTrace();
                        Log.e(TAG, e.getMessage());
                        if (mPrepareListener != null)
                            mPrepareListener.onPrepareFail(e);
                    } catch (IOException e) {
                        e.printStackTrace();
                        Log.e(TAG, e.getMessage());
                        if (mPrepareListener != null)
                            mPrepareListener.onPrepareFail(e);
                    } catch (AuthenticatorException e) {
                        e.printStackTrace();
                        Log.e(TAG, e.getMessage());
                        if (mPrepareListener != null)
                            mPrepareListener.onPrepareFail(e);
                    }
                }
            }, null);
}

From source file:com.nononsenseapps.notepad.MainActivity.java

private void enableSync() {
    final Activity activity = this;
    // Get the first Google account on the device
    final Account[] accounts = AccountManager.get(activity).getAccountsByType("com.google");
    if (accounts.length > 0) {
        final Account account = accounts[0];

        // Request access
        AccountManager.get(activity).getAuthToken(account, SyncAdapter.AUTH_TOKEN_TYPE, null, activity,
                new AccountManagerCallback<Bundle>() {

                    @Override//  w ww .  j  av  a 2s  .  co m
                    public void run(AccountManagerFuture<Bundle> future) {
                        // This is the callback class, it handles all the
                        // steps
                        // after requesting access
                        try {
                            String token = future.getResult().getString(AccountManager.KEY_AUTHTOKEN);
                            if (token != null && !token.equals("") && account != null) {
                                // Get preference editor
                                Editor editor = PreferenceManager.getDefaultSharedPreferences(activity).edit();

                                // Write account name to prefs
                                editor.putString(SyncPrefs.KEY_ACCOUNT, account.name);

                                // Set it syncable
                                ContentResolver.setIsSyncable(account, NotePad.AUTHORITY, 1);

                                // Write to pref
                                editor.putBoolean(SyncPrefs.KEY_SYNC_ENABLE, true);

                                // Commit prefs
                                editor.commit();

                                // Then request sync
                                requestSync(account.name);
                            }
                        } catch (OperationCanceledException e) {
                            Log.e("SyncFix", "Error1");
                            // if the request was canceled for any reason
                        } catch (AuthenticatorException e) {
                            Log.e("SyncFix", "Error2");
                            // if there was an error communicating with the
                            // authenticator or
                            // if the authenticator returned an invalid
                            // response
                        } catch (IOException e) {
                            Log.e("SyncFix", "Error3");
                            // if the authenticator returned an error
                            // response that
                            // indicates that it encountered an IOException
                            // while
                            // communicating with the authentication server
                        }
                    }
                }, null);

    }
}

From source file:com.android.exchange.SyncManager.java

/**
 * Compare our account list (obtained from EmailProvider) with the account list owned by
 * AccountManager.  If there are any orphans (an account in one list without a corresponding
 * account in the other list), delete the orphan, as these must remain in sync.
 *
 * Note that the duplication of account information is caused by the Email application's
 * incomplete integration with AccountManager.
 *
 * This function may not be called from the main/UI thread, because it makes blocking calls
 * into the account manager.//ww  w  .  ja  v a 2s.c o  m
 *
 * @param context The context in which to operate
 * @param cachedEasAccounts the exchange provider accounts to work from
 * @param accountManagerAccounts The account manager accounts to work from
 * @param blockExternalChanges FOR TESTING ONLY - block backups, security changes, etc.
 * @param resolver the content resolver for making provider updates (injected for testability)
 */
/* package */ static void reconcileAccountsWithAccountManager(Context context, List<Account> cachedEasAccounts,
        android.accounts.Account[] accountManagerAccounts, boolean blockExternalChanges,
        ContentResolver resolver) {
    // First, look through our cached EAS Accounts (from EmailProvider) to make sure there's a
    // corresponding AccountManager account
    boolean accountsDeleted = false;
    for (Account providerAccount : cachedEasAccounts) {
        String providerAccountName = providerAccount.mEmailAddress;
        boolean found = false;
        for (android.accounts.Account accountManagerAccount : accountManagerAccounts) {
            if (accountManagerAccount.name.equalsIgnoreCase(providerAccountName)) {
                found = true;
                break;
            }
        }
        if (!found) {
            if ((providerAccount.mFlags & Account.FLAGS_INCOMPLETE) != 0) {
                log("Account reconciler noticed incomplete account; ignoring");
                continue;
            }
            // This account has been deleted in the AccountManager!
            alwaysLog("Account deleted in AccountManager; deleting from provider: " + providerAccountName);
            // TODO This will orphan downloaded attachments; need to handle this
            resolver.delete(ContentUris.withAppendedId(Account.CONTENT_URI, providerAccount.mId), null, null);
            accountsDeleted = true;
        }
    }
    // Now, look through AccountManager accounts to make sure we have a corresponding cached EAS
    // account from EmailProvider
    for (android.accounts.Account accountManagerAccount : accountManagerAccounts) {
        String accountManagerAccountName = accountManagerAccount.name;
        boolean found = false;
        for (Account cachedEasAccount : cachedEasAccounts) {
            if (cachedEasAccount.mEmailAddress.equalsIgnoreCase(accountManagerAccountName)) {
                found = true;
            }
        }
        if (!found) {
            // This account has been deleted from the EmailProvider database
            alwaysLog("Account deleted from provider; deleting from AccountManager: "
                    + accountManagerAccountName);
            // Delete the account
            AccountManagerFuture<Boolean> blockingResult = AccountManager.get(context)
                    .removeAccount(accountManagerAccount, null, null);
            try {
                // Note: All of the potential errors from removeAccount() are simply logged
                // here, as there is nothing to actually do about them.
                blockingResult.getResult();
            } catch (OperationCanceledException e) {
                Log.w(Email.LOG_TAG, e.toString());
            } catch (AuthenticatorException e) {
                Log.w(Email.LOG_TAG, e.toString());
            } catch (IOException e) {
                Log.w(Email.LOG_TAG, e.toString());
            }
            accountsDeleted = true;
        }
    }
    // If we changed the list of accounts, refresh the backup & security settings
    if (!blockExternalChanges && accountsDeleted) {
        AccountBackupRestore.backupAccounts(context);
        SecurityPolicy.getInstance(context).reducePolicies();
        Email.setNotifyUiAccountsChanged(true);
    }
}