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:com.android.browser.GoogleAccountLogin.java

@Override
public void run(AccountManagerFuture<Bundle> value) {
    try {//from   w  w  w  .j a  va2s  . com
        String id = value.getResult().getString(AccountManager.KEY_AUTHTOKEN);
        switch (mState) {
        default:
        case 0:
            throw new IllegalStateException("Impossible to get into this state");
        case 1:
            mSid = id;
            mState = 2; // LSID
            AccountManager.get(mActivity).getAuthToken(mAccount, "LSID", null, mActivity, this, null);
            break;
        case 2:
            mLsid = id;
            new Thread(this).start();
            break;
        }
    } catch (Exception e) {
        Log.d(LOGTAG, "LOGIN_FAIL: Exception in state " + mState + " " + e);
        // For all exceptions load the original signin page.
        // TODO: toast login failed?
        done();
    }
}

From source file:com.newtifry.android.remote.BackendClient.java

private String getAuthToken(Context context, Account account) throws PendingAuthException {
    String authToken = null;//w  ww.j av  a 2  s . com
    AccountManager accountManager = AccountManager.get(context);
    try {
        AccountManagerFuture<Bundle> future = accountManager.getAuthToken(account, "ah", false, null, null);
        Bundle bundle = future.getResult();
        authToken = bundle.getString(AccountManager.KEY_AUTHTOKEN);
        // User will be asked for "App Engine" permission.
        if (authToken == null) {
            // No authorization token - will need to ask permission from user.
            Intent intent = (Intent) bundle.get(AccountManager.KEY_INTENT);
            if (intent != null) {
                // User input required
                context.startActivity(intent);
                throw new PendingAuthException("Asking user for permission.");
            }
        }
    } catch (OperationCanceledException e) {
        Log.d(TAG, e.getMessage());
    } catch (AuthenticatorException e) {
        Log.d(TAG, e.getMessage());
    } catch (IOException e) {
        Log.d(TAG, e.getMessage());
    }

    return authToken;
}

From source file:com.gelakinetic.inboxwidget.InboxCheckerAppWidgetConfigure.java

/**
 * Start the process to get all Inbox accounts. Will call onAccountResults() when the accounts
 * are retrieved./*from ww w. ja v a 2s .c  o m*/
 *
 * @throws SecurityException If permissions weren't granted, this is called
 */
private void getAccounts() throws SecurityException {
    /* Get all eligible accounts */
    AccountManager.get(this).getAccountsByTypeAndFeatures(ACCOUNT_TYPE_GOOGLE, FEATURES_MAIL,
            new AccountManagerCallback<Account[]>() {
                @Override
                public void run(AccountManagerFuture<Account[]> future) {
                    Account[] accounts = null;
                    try {
                        accounts = future.getResult();
                    } catch (OperationCanceledException | IOException | AuthenticatorException oce) {
                        /* Eat it */
                    }
                    onAccountResults(accounts);
                }
            }, null);
}

From source file:com.arthackday.killerapp.util.Util.java

public String getGoogleAuth(String type) {
    AccountManager mgr = AccountManager.get(activity);
    Account[] accts = mgr.getAccountsByType("com.google");

    if (accts.length == 0) {
        return null;
    }//w w  w . ja v  a2  s .c o m

    try {
        Account acct = accts[0];
        Log.d(LOG_TAG, "acct name=" + acct.name);
        AccountManagerFuture<Bundle> accountManagerFuture = mgr.getAuthToken(acct, type, null, activity, null,
                null);

        Bundle authTokenBundle = accountManagerFuture.getResult();

        if (authTokenBundle.containsKey(AccountManager.KEY_INTENT)) {
            Intent authRequestIntent = (Intent) authTokenBundle.get(AccountManager.KEY_INTENT);
            activity.startActivity(authRequestIntent);
        }

        return authTokenBundle.get(AccountManager.KEY_AUTHTOKEN).toString();
    } catch (OperationCanceledException e) {
        e.printStackTrace();
    } catch (AuthenticatorException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:uk.co.bubblebearapps.contactsintegration.MainActivity.java

private void removeAccount(Account account) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
        mAccountManager.removeAccount(account, this, new AccountManagerCallback<Bundle>() {
            @Override// w  w  w.j av  a  2  s  .c o m
            public void run(AccountManagerFuture<Bundle> future) {

                try {
                    Boolean result = future.getResult().getBoolean(AccountManager.KEY_BOOLEAN_RESULT);
                    if (result) {
                        showMessage("Account deleted");
                    } else {
                        showMessage("Account not deleted");
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                    showMessage(e.getMessage());
                }
            }
        }, new Handler());
    } else {
        mAccountManager.removeAccount(account, new AccountManagerCallback<Boolean>() {
            @Override
            public void run(AccountManagerFuture<Boolean> future) {
                try {
                    Boolean result = future.getResult();
                    if (result) {
                        showMessage("Account deleted");
                    } else {
                        showMessage("Account not deleted");
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                    showMessage(e.getMessage());
                }

            }
        }, new Handler());
    }
}

From source file:com.capstonecontrol.AccountsActivity.java

private String getAuthToken(AccountManagerFuture<Bundle> future) {
    try {/*from  w ww .j  ava  2  s.c  om*/
        Bundle authTokenBundle = future.getResult();
        String authToken = authTokenBundle.get(AccountManager.KEY_AUTHTOKEN).toString();
        return authToken;
    } catch (Exception e) {
        Log.w(TAG, "Got Exception " + e);
        return null;
    }
}

From source file:com.google.appinventor.components.runtime.util.ClientLoginHelper.java

/**
 * Uses Google Account Manager to retrieve auth token that can
 * be used to access various Google APIs -- e.g., the Google Voice api.
 *//*from  www  .ja  v a2  s.c  o m*/
public String getAuthToken() throws ClientProtocolException {
    Account account = accountChooser.findAccount();
    if (account != null) {
        AccountManagerFuture<Bundle> future;
        future = accountManager.getAuthToken(account, service, null, activity, null, null);
        Log.i(LOG_TAG, "Have account, auth token: " + future);
        Bundle result;
        try {
            result = future.getResult();
            return result.getString(AccountManager.KEY_AUTHTOKEN);
        } catch (AuthenticatorException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (OperationCanceledException e) {
            e.printStackTrace();
        }
    }
    throw new ClientProtocolException("Can't get valid authentication token");
}

From source file:com.github.riotopsys.shoppinglist.activity.ShoppingListPreview.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_shopping_list_preview);

    AccountUtils accountUtils = new AccountUtils();
    String account = accountUtils.getAccountName(this);

    if (account == null) {
        AccountManager am = AccountManager.get(this);
        am.getAuthTokenByFeatures(AppKeys.ACCOUNT_TYPE, AppKeys.OAUTH2_SCOPE, null, this, null, null,
                new AccountManagerCallback<Bundle>() {
                    @Override/*from  w  ww  .ja  va 2  s. co  m*/
                    public void run(AccountManagerFuture<Bundle> future) {
                        try {
                            Bundle bundle = future.getResult();
                            Log.i(TAG, "Got Bundle:\n" + " act name: "
                                    + bundle.getString(AccountManager.KEY_ACCOUNT_NAME) + "\n act type: "
                                    + bundle.getString(AccountManager.KEY_ACCOUNT_TYPE) + "\n auth token: "
                                    + bundle.getString(AccountManager.KEY_AUTHTOKEN));
                            AccountUtils accountUtils = new AccountUtils();
                            accountUtils.setAccountName(getBaseContext(),
                                    bundle.getString(AccountManager.KEY_ACCOUNT_NAME));
                            accountUtils.setToken(getBaseContext(),
                                    bundle.getString(AccountManager.KEY_AUTHTOKEN));
                        } catch (Exception e) {
                            Log.i(TAG, "getAuthTokenByFeatures() cancelled or failed:", e);
                            Toast.makeText(getBaseContext(), R.string.no_account, Toast.LENGTH_LONG).show();
                            finish();
                        }
                    }
                }, null);
    }

    mShoppingListCollection = new ShoppingListCollection();

    mShoppingListCollectionAdapter = new ShoppingListCollectionAdapter(this, getSupportFragmentManager(),
            mShoppingListCollection);

    //      mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mShoppingListCollectionAdapter);

    Intent startingIntent = getIntent();
    if (startingIntent != null) {
        Uri data = startingIntent.getData();
        if (data != null) {
            UUID guid = UUID.fromString(data.getLastPathSegment());
            Intent i = new Intent(this, ServerInterfaceService.class);
            i.putExtra(AppKeys.SERVER_TASK_KEY, ServerTask.SUBSCRIBE);
            i.putExtra(AppKeys.GUID_KEY, guid);
            startService(i);
        }
    }

    IConfigurations config = new Configurations();

    GCMRegistrar.checkDevice(this);
    GCMRegistrar.checkManifest(this);
    final String regId = GCMRegistrar.getRegistrationId(this);
    if (regId.equals("")) {
        GCMRegistrar.register(this, config.getGCMSenderID());
    } else {
        Log.v(TAG, "Already registered");
    }

}

From source file:eu.trentorise.smartcampus.ac.authenticator.AMSCAccessProvider.java

@Override
public String promote(final Activity activity, String inAuthority, final String token) {
    final String authority = inAuthority == null ? Constants.AUTHORITY_DEFAULT : inAuthority;
    final AccountManager am = AccountManager.get(activity);
    //      Bundle options = new Bundle();
    //      if (token != null) {
    //         options.putString(Constants.PROMOTION_TOKEN, token);
    //      }//  ww w .j  a  v a  2 s  .c  om
    final Account a = new Account(Constants.getAccountName(activity), Constants.getAccountType(activity));
    final String userDataString = am.getUserData(a, AccountManager.KEY_USERDATA);

    invalidateToken(activity, authority);

    am.getAuthToken(new Account(Constants.getAccountName(activity), Constants.getAccountType(activity)),
            authority, null, null, new AccountManagerCallback<Bundle>() {
                @Override
                public void run(AccountManagerFuture<Bundle> result) {
                    Bundle bundle = null;
                    try {
                        bundle = result.getResult();
                        Intent launch = (Intent) bundle.get(AccountManager.KEY_INTENT);
                        if (launch != null) {
                            launch.putExtra(Constants.KEY_AUTHORITY, authority);
                            launch.putExtra(Constants.PROMOTION_TOKEN, token);
                            launch.putExtra(Constants.OLD_DATA, userDataString);
                            activity.startActivityForResult(launch, SC_AUTH_ACTIVITY_REQUEST_CODE);
                        } else if (bundle.getString(AccountManager.KEY_AUTHTOKEN) != null) {
                            //                         am.setAuthToken(a, authority, bundle.getString(AccountManager.KEY_AUTHTOKEN));
                            //                         am.addAccountExplicitly(a, null, null);
                            // no token acquired
                        } else {
                            storeAnonymousToken(token, authority, am, a);
                        }
                    } catch (Exception e) {
                        // revert the invalidated token
                        storeAnonymousToken(token, authority, am, a);
                        return;
                    }
                }
            }, null);
    return null;
}

From source file:com.google.sampling.experiential.android.lib.GoogleAccountLoginHelper.java

private String getNewAuthToken(AccountManager accountManager, Account account)
        throws OperationCanceledException, IOException, AuthenticatorException {
    AccountManagerFuture<Bundle> accountManagerFuture = accountManager.getAuthToken(account, "ah", null,
            context, null, null);/*ww w  .java  2s.c  o  m*/

    Bundle authTokenBundle = accountManagerFuture.getResult();
    return authTokenBundle.get(AccountManager.KEY_AUTHTOKEN).toString();
}