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:de.msal.shoutemo.connector.GetPostsService.java

/**
 *
 *///from  ww w . j a  v  a 2s  . c o  m
private void startGetPostsTask() {
    mAccountManager.getAuthToken(mAccount, LoginActivity.PARAM_AUTHTOKEN_TYPE, null, false,
            new AccountManagerCallback<Bundle>() {
                @Override
                public void run(AccountManagerFuture<Bundle> result) {
                    Bundle bundle;
                    try {
                        bundle = result.getResult();
                    } catch (OperationCanceledException e) {
                        e.printStackTrace();
                        return;
                    } catch (AuthenticatorException e) {
                        e.printStackTrace();
                        return;
                    } catch (IOException e) {
                        e.printStackTrace();
                        return;
                    }
                    mAuthToken = bundle.getString(AccountManager.KEY_AUTHTOKEN);
                    Log.v(TAG, "Received authentication token=" + mAuthToken);
                    // now get messages!
                    if (worker == null || worker.isShutdown()) {
                        worker = Executors.newSingleThreadScheduledExecutor();
                    }
                    // fix (possible) wrong time-setting on autemo.com
                    worker.execute(new SetTimezoneTask());
                    // only now recieve messages (with right time)
                    worker.scheduleAtFixedRate(new GetPostsTask(), 0, INTERVAL, TimeUnit.MILLISECONDS);
                }
            }, null);
}

From source file:com.friedran.appengine.dashboard.client.AppEngineDashboardAuthenticator.java

public void executeAuthentication() {
    // Gets the auth token asynchronously, calling the callback with its result (uses the
    // deprecated API which is the only one supported from API level 5).
    AccountManager.get(mApplicationContext).getAuthToken(mAccount, AUTH_TOKEN_TYPE, false,
            new AccountManagerCallback<Bundle>() {
                public void run(AccountManagerFuture result) {
                    Bundle bundle;//  ww  w. j a  v  a  2s  .com
                    try {
                        LogUtils.i("AppEngineDashboardAuthenticator",
                                "GetAuthTokenCallback.onPostExecute started...");
                        bundle = (Bundle) result.getResult();
                        Intent intent = (Intent) bundle.get(AccountManager.KEY_INTENT);
                        if (intent != null) {
                            // User input required
                            LogUtils.i("AppEngineDashboardAuthenticator", "User input is required...");
                            mOnUserInputRequiredCallback.onUserInputRequired(intent);
                        } else {
                            LogUtils.i("AppEngineDashboardAuthenticator",
                                    "Authenticated, getting auth token...");
                            onGetAuthToken(bundle);
                        }
                    } catch (Exception e) {
                        // Can happen because of various like connectivity issues, google server errors, etc.
                        LogUtils.e("AppEngineDashboardAuthenticator",
                                "Exception caught from GetAuthTokenCallback", e);
                        mPostAuthenticateCallback.run(false);
                    }
                }
            }, null);
}

From source file:org.pixmob.feedme.ui.EntriesFragment.java

private void authenticateAccount(String accountName) {
    Log.i(TAG, "Authenticating account: " + accountName);

    final Activity a = getActivity();
    final AccountManager am = (AccountManager) a.getSystemService(Context.ACCOUNT_SERVICE);
    final Account account = new Account(accountName, GOOGLE_ACCOUNT);
    am.getAuthToken(account, "reader", null, a, new AccountManagerCallback<Bundle>() {
        @Override/*from w ww. j av a 2s  .  c o m*/
        public void run(AccountManagerFuture<Bundle> resultContainer) {
            String authToken = null;
            final Bundle result;
            try {
                result = resultContainer.getResult();
                authToken = result.getString(AccountManager.KEY_AUTHTOKEN);
            } catch (IOException e) {
                Log.w(TAG, "I/O error while authenticating account " + account.name, e);
            } catch (OperationCanceledException e) {
                Log.w(TAG, "Authentication was canceled for account " + account.name, e);
            } catch (AuthenticatorException e) {
                Log.w(TAG, "Authentication failed for account " + account.name, e);
            }

            if (authToken == null) {
                Toast.makeText(a, a.getString(R.string.authentication_failed), Toast.LENGTH_SHORT).show();
            } else {
                Log.i(TAG, "Authentication done");
                onAuthenticationDone(account.name, authToken);
            }
        }
    }, null);
}

From source file:org.jnrain.mobile.ui.MainActivity.java

@Override
public void run(AccountManagerFuture<Account[]> response) {
    Account[] accounts;/*from  ww w.j  a v a  2s . c  o  m*/

    try {
        accounts = response.getResult();

        if (accounts.length > 0) {
            onAccountAcquired(accounts[0]);
            return;
        }

        // no account
        // try to create one, but don't recurse infinitely
        if (GlobalState.getAccountInitLevel() > 2) {
            // finish self
            finish();
            return;
        }

        // create account
        new AsyncTask<Void, Void, Void>() {
            @Override
            protected Void doInBackground(Void... arg0) {
                AccountManager am = AccountManager.get(getThisActivity());

                try {
                    am.addAccount(AccountConstants.ACCOUNT_TYPE_KBS, null, null, null, getThisActivity(),
                            new AccountManagerCallback<Bundle>() {
                                @Override
                                public void run(AccountManagerFuture<Bundle> response) {
                                    try {
                                        response.getResult();
                                    } catch (OperationCanceledException e) {
                                        // TODO Auto-generated catch
                                        // block
                                        e.printStackTrace();
                                    } catch (AuthenticatorException e) {
                                        // TODO Auto-generated catch
                                        // block
                                        e.printStackTrace();
                                    } catch (IOException e) {
                                        // TODO Auto-generated catch
                                        // block
                                        e.printStackTrace();
                                    }

                                    MainActivity.this.initAccount();
                                }
                            }, null).getResult();
                } catch (OperationCanceledException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (AuthenticatorException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                return null;
            }
        }.execute((Void) null);
    } catch (OperationCanceledException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        finish();
    } catch (AuthenticatorException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:com.google.wireless.speed.speedometer.AccountSelector.java

private void getAuthToken(AccountManagerFuture<Bundle> result) {
    Log.i(SpeedometerApp.TAG, "getAuthToken() called, result " + result);
    String errMsg = "Failed to get login cookie. ";
    Bundle bundle;//from   w w w.  j av  a2s .  co  m
    try {
        bundle = result.getResult();
        Intent intent = (Intent) bundle.get(AccountManager.KEY_INTENT);
        if (intent != null) {
            // User input required. (A UI will pop up for user's consent to allow
            // this app access account information.)
            Log.i(SpeedometerApp.TAG, "Starting account manager activity");
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);
        } else {
            Log.i(SpeedometerApp.TAG, "Executing getCookie task");
            synchronized (this) {
                this.authToken = bundle.getString(AccountManager.KEY_AUTHTOKEN);
                this.checkinFuture = checkinExecutor.submit(new GetCookieTask());
            }
        }
    } catch (OperationCanceledException e) {
        Log.e(SpeedometerApp.TAG, errMsg, e);
        throw new RuntimeException("Can't get login cookie", e);
    } catch (AuthenticatorException e) {
        Log.e(SpeedometerApp.TAG, errMsg, e);
        throw new RuntimeException("Can't get login cookie", e);
    } catch (IOException e) {
        Log.e(SpeedometerApp.TAG, errMsg, e);
        throw new RuntimeException("Can't get login cookie", e);
    }
}

From source file:de.msal.shoutemo.connector.GetPostsService.java

@Override
public void onCreate() {
    super.onCreate();

    broadcaster = LocalBroadcastManager.getInstance(this);

    mAccountManager = AccountManager.get(this);
    Account[] acc = mAccountManager.getAccountsByType(AccountAuthenticator.ACCOUNT_TYPE);

    /* No account; push the user into adding one */
    if (acc.length == 0) {
        Log.v(TAG, "No suitable account found, directing user to add one.");
        mAccountManager.addAccount(AccountAuthenticator.ACCOUNT_TYPE, null, null, new Bundle(), null,
                new AccountManagerCallback<Bundle>() {
                    @Override// w ww  .j a  v  a2 s.  c o  m
                    public void run(AccountManagerFuture<Bundle> result) {
                        Bundle bundle;
                        try {
                            bundle = result.getResult();
                        } catch (OperationCanceledException e) {
                            e.printStackTrace();
                            return;
                        } catch (AuthenticatorException e) {
                            e.printStackTrace();
                            return;
                        } catch (IOException e) {
                            e.printStackTrace();
                            return;
                        }

                        /* no accounts saved, yet; ask the user for credentials */
                        Intent launch = bundle.getParcelable(AccountManager.KEY_INTENT);
                        if (launch != null) {
                            launch.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            startActivity(launch);
                            return;
                        }

                        mAccount = new Account(bundle.getString(AccountManager.KEY_ACCOUNT_NAME),
                                bundle.getString(AccountManager.KEY_ACCOUNT_TYPE));
                        Log.v(TAG, "Added account " + mAccount.name + "; now fetching new posts.");
                        startGetPostsTask();
                    }
                }, null);
    } else {
        mAccount = acc[0];
        startGetPostsTask();
    }
}

From source file:com.mobiperf.speedometer.AccountSelector.java

private void getAuthToken(AccountManagerFuture<Bundle> result) {
    Logger.i("getAuthToken() called, result " + result);
    String errMsg = "Failed to get login cookie. ";
    Bundle bundle;/*from w ww  . j  ava2  s  .  co  m*/
    try {
        bundle = result.getResult();
        Intent intent = (Intent) bundle.get(AccountManager.KEY_INTENT);
        if (intent != null) {
            // User input required. (A UI will pop up for user's consent to
            // allow
            // this app access account information.)
            Logger.i("Starting account manager activity");
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);
        } else {
            Logger.i("Executing getCookie task");
            synchronized (this) {
                this.authToken = bundle.getString(AccountManager.KEY_AUTHTOKEN);
                this.checkinFuture = checkinExecutor.submit(new GetCookieTask());
            }
        }
    } catch (OperationCanceledException e) {
        Logger.e(errMsg, e);
        throw new RuntimeException("Can't get login cookie", e);
    } catch (AuthenticatorException e) {
        Logger.e(errMsg, e);
        throw new RuntimeException("Can't get login cookie", e);
    } catch (IOException e) {
        Logger.e(errMsg, e);
        throw new RuntimeException("Can't get login cookie", e);
    }
}

From source file:com.listomate.activities.AccountsActivity.java

/**
 * Registers for C2DM messaging with the given account name.
 * //from w  w  w. ja  va 2 s. c  o  m
 * @param accountName a String containing a Google account name
 */
private void register(final String accountName) {
    // Store the account name in shared preferences
    final SharedPreferences prefs = Util.getSharedPreferences(mContext);
    SharedPreferences.Editor editor = prefs.edit();
    editor.putString(Util.ACCOUNT_NAME, accountName);
    editor.putString(Util.AUTH_COOKIE, null);
    editor.commit();

    // Obtain an auth token and register
    AccountManager mgr = AccountManager.get(mContext);
    Account[] accts = mgr.getAccountsByType("com.google");
    for (Account acct : accts) {
        if (acct.name.equals(accountName)) {
            if (Util.isDebug(mContext)) {
                // Use a fake cookie for the dev mode app engine server
                // The cookie has the form email:isAdmin:userId
                // We set the userId to be the same as the account name
                String authCookie = "dev_appserver_login=" + accountName + ":false:" + accountName;
                prefs.edit().putString(Util.AUTH_COOKIE, authCookie).commit();
                C2DMessaging.register(mContext, Setup.SENDER_ID);
            } else {
                // Get the auth token from the AccountManager and convert
                // it into a cookie for the appengine server
                mgr.getAuthToken(acct, "ah", null, this, new AccountManagerCallback<Bundle>() {
                    public void run(AccountManagerFuture<Bundle> future) {
                        try {
                            Bundle authTokenBundle = future.getResult();
                            String authToken = authTokenBundle.get(AccountManager.KEY_AUTHTOKEN).toString();
                            String authCookie = getAuthCookie(authToken);
                            prefs.edit().putString(Util.AUTH_COOKIE, authCookie).commit();

                            C2DMessaging.register(mContext, Setup.SENDER_ID);
                        } catch (AuthenticatorException e) {
                            Log.w(TAG, "Got AuthenticatorException " + e);
                            Log.w(TAG, Log.getStackTraceString(e));
                        } catch (IOException e) {
                            Log.w(TAG, "Got IOException " + Log.getStackTraceString(e));
                            Log.w(TAG, Log.getStackTraceString(e));
                        } catch (OperationCanceledException e) {
                            Log.w(TAG, "Got OperationCanceledException " + e);
                            Log.w(TAG, Log.getStackTraceString(e));
                        }
                    }
                }, null);
            }
            break;
        }
    }
}

From source file:com.mobiperf.AccountSelector.java

private void getAuthToken(AccountManagerFuture<Bundle> result) {
    Logger.i("getAuthToken() called, result " + result);
    String errMsg = "Failed to get login cookie. ";
    Bundle bundle;//w  w  w  .  j  a  v  a 2 s . c  o m
    try {
        bundle = result.getResult();
        Intent intent = (Intent) bundle.get(AccountManager.KEY_INTENT);
        if (intent != null) {
            // User input required. (A UI will pop up for user's consent to allow
            // this app access account information.)
            Logger.i("Starting account manager activity");
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);
        } else {
            Logger.i("Executing getCookie task");
            synchronized (this) {
                this.authToken = bundle.getString(AccountManager.KEY_AUTHTOKEN);
                this.checkinFuture = checkinExecutor.submit(new GetCookieTask());
            }
        }
    } catch (OperationCanceledException e) {
        Logger.e(errMsg, e);
        throw new RuntimeException("Can't get login cookie", e);
    } catch (AuthenticatorException e) {
        Logger.e(errMsg, e);
        throw new RuntimeException("Can't get login cookie", e);
    } catch (IOException e) {
        Logger.e(errMsg, e);
        throw new RuntimeException("Can't get login cookie", e);
    }
}

From source file:com.num.mobiperf.AccountSelector.java

private void getAuthToken(AccountManagerFuture<Bundle> result) {
    //Logger.i("getAuthToken() called, result " + result);
    String errMsg = "Failed to get login cookie. ";
    Bundle bundle;/*  www  . jav a2 s . c  o  m*/
    try {
        bundle = result.getResult();
        Intent intent = (Intent) bundle.get(AccountManager.KEY_INTENT);
        if (intent != null) {
            // User input required. (A UI will pop up for user's consent to allow
            // this app access account information.)
            //Logger.i("Starting account manager activity");
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);
        } else {
            //Logger.i("Executing getCookie task");
            synchronized (this) {
                this.authToken = bundle.getString(AccountManager.KEY_AUTHTOKEN);
                this.checkinFuture = checkinExecutor.submit(new GetCookieTask());
            }
        }
    } catch (OperationCanceledException e) {
        //Logger.e(errMsg, e);
        throw new RuntimeException("Can't get login cookie", e);
    } catch (AuthenticatorException e) {
        //Logger.e(errMsg, e);
        throw new RuntimeException("Can't get login cookie", e);
    } catch (IOException e) {
        //Logger.e(errMsg, e);
        throw new RuntimeException("Can't get login cookie", e);
    }
}