List of usage examples for android.accounts AccountManager getAuthToken
@Deprecated public AccountManagerFuture<Bundle> getAuthToken(final Account account, final String authTokenType, final boolean notifyAuthFailure, AccountManagerCallback<Bundle> callback, Handler handler)
From source file:com.google.wireless.speed.speedometer.AccountSelector.java
/** Starts an authentication request */ public void authenticate() throws OperationCanceledException, AuthenticatorException, IOException { Log.i(SpeedometerApp.TAG, "AccountSelector.authenticate() running"); /* We only need to authenticate every AUTHENTICATE_PERIOD_MILLI milliseconds, during * which we can reuse the cookie. If authentication fails due to expired * authToken, the client of AccountSelector can call authImmedately() to request * authenticate() upon the next checkin */// w w w . j a va2 s .c om long authTimeLast = this.getLastAuthTime(); long timeSinceLastAuth = System.currentTimeMillis() - authTimeLast; if (!this.shouldAuthImmediately() && authTimeLast != 0 && (timeSinceLastAuth < AUTHENTICATE_PERIOD_MSEC)) { return; } Log.i(SpeedometerApp.TAG, "Authenticating. Last authentication is " + timeSinceLastAuth / 1000 / 60 + " minutes ago. "); AccountManager accountManager = AccountManager.get(context.getApplicationContext()); if (this.authToken != null) { // There will be no effect on the token if it is still valid Log.i(SpeedometerApp.TAG, "Invalidating token"); accountManager.invalidateAuthToken(ACCOUNT_TYPE, this.authToken); } Account[] accounts = accountManager.getAccountsByType(ACCOUNT_TYPE); Log.i(SpeedometerApp.TAG, "Got " + accounts.length + " accounts"); if (accounts != null && accounts.length > 0) { // TODO(mdw): If multiple accounts, need to pick the correct one Account accountToUse = accounts[0]; // We prefer google's corporate account to personal accounts such as somebody@gmail.com for (Account account : accounts) { if (account.name.toLowerCase().trim().endsWith(ACCOUNT_NAME)) { Log.i(SpeedometerApp.TAG, "Using the preferred google.com account: " + account.name); accountToUse = account; break; } } Log.i(SpeedometerApp.TAG, "Trying to get auth token for " + accountToUse); AccountManagerFuture<Bundle> future = accountManager.getAuthToken(accountToUse, "ah", false, new AccountManagerCallback<Bundle>() { @Override public void run(AccountManagerFuture<Bundle> result) { Log.i(SpeedometerApp.TAG, "AccountManagerCallback invoked"); try { getAuthToken(result); } catch (RuntimeException e) { Log.e(SpeedometerApp.TAG, "Failed to get authToken", e); /* TODO(Wenjie): May ask the user whether to quit the app nicely here if a number * of trials have been made and failed. Since Speedometer is basically useless * without checkin */ } } }, null); Log.i(SpeedometerApp.TAG, "AccountManager.getAuthToken returned " + future); } else { throw new RuntimeException("No google account found"); } }
From source file:com.flowzr.budget.holo.export.flowzr.FlowzrSyncTask.java
protected Object work(Context context, DatabaseAdapter dba, String... params) throws ImportExportException { AccountManager accountManager = AccountManager.get(context); android.accounts.Account[] accounts = accountManager.getAccountsByType("com.google"); String accountName = MyPreferences.getFlowzrAccount(context); if (accountName == null) { NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); Intent notificationIntent = new Intent(context, FlowzrSyncActivity.class); PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT); Builder mNotifyBuilder = new NotificationCompat.Builder(context); mNotifyBuilder.setContentIntent(contentIntent).setSmallIcon(R.drawable.icon) .setWhen(System.currentTimeMillis()).setAutoCancel(true) .setContentTitle(context.getString(R.string.flowzr_sync)) .setContentText(context.getString(R.string.flowzr_choose_account)); nm.notify(0, mNotifyBuilder.build()); Log.i("Financisto", "account name is null"); throw new ImportExportException(R.string.flowzr_choose_account); }/*from w w w . j a va2 s.co m*/ Account useCredential = null; for (int i = 0; i < accounts.length; i++) { if (accountName.equals(((android.accounts.Account) accounts[i]).name)) { useCredential = accounts[i]; } } accountManager.getAuthToken(useCredential, "ah", false, new GetAuthTokenCallback(), null); return null; }
From source file:com.mobiperf.speedometer.AccountSelector.java
/** Starts an authentication request */ public void authenticate() throws OperationCanceledException, AuthenticatorException, IOException { Logger.i("AccountSelector.authenticate() running"); /*// ww w .j ava 2 s . c o m * We only need to authenticate every AUTHENTICATE_PERIOD_MILLI milliseconds, during which we * can reuse the cookie. If authentication fails due to expired authToken, the client of * AccountSelector can call authImmedately() to request authenticate() upon the next checkin */ long authTimeLast = this.getLastAuthTime(); long timeSinceLastAuth = System.currentTimeMillis() - authTimeLast; if (!this.shouldAuthImmediately() && authTimeLast != 0 && (timeSinceLastAuth < AUTHENTICATE_PERIOD_MSEC)) { return; } Logger.i("Authenticating. Last authentication is " + timeSinceLastAuth / 1000 / 60 + " minutes ago. "); AccountManager accountManager = AccountManager.get(context.getApplicationContext()); if (this.authToken != null) { // There will be no effect on the token if it is still valid Logger.i("Invalidating token"); accountManager.invalidateAuthToken(ACCOUNT_TYPE, this.authToken); } Account[] accounts = accountManager.getAccountsByType(ACCOUNT_TYPE); Logger.i("Got " + accounts.length + " accounts"); // get selected account SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this.context); String selectedAccount = prefs.getString(Config.PREF_KEY_SELECTED_ACCOUNT, null); if (accounts != null && accounts.length > 0 && selectedAccount != null) { Account accountToUse = null; for (Account account : accounts) { // if (account.name.toLowerCase().trim().endsWith(ACCOUNT_NAME)) { Logger.i("account list: " + account.name + " " + account.type + " " + account.toString()); // If one of the available accounts is the one selected by user, use that if (account.name.equals(selectedAccount)) { accountToUse = account; Logger.i("selected account: " + account.name + " " + account.type + " " + account.toString()); } } Logger.i("Trying to get auth token for " + accountToUse); AccountManagerFuture<Bundle> future = accountManager.getAuthToken(accountToUse, "ah", false, new AccountManagerCallback<Bundle>() { @Override public void run(AccountManagerFuture<Bundle> result) { Logger.i("AccountManagerCallback invoked"); try { getAuthToken(result); } catch (RuntimeException e) { Logger.e("Failed to get authToken", e); /* * TODO(Wenjie): May ask the user whether to quit the app nicely here if a number of * trials have been made and failed. Since Speedometer is basically useless without * checkin */ } } }, null); Logger.i("AccountManager.getAuthToken returned " + future); } else { throw new RuntimeException("No google account found or no google account selected"); } }
From source file:com.mobiperf_library.AccountSelector.java
/** Starts an authentication request */ public void authenticate() throws OperationCanceledException, AuthenticatorException, IOException { Logger.i("AccountSelector.authenticate() running"); /* We only need to authenticate every AUTHENTICATE_PERIOD_MILLI milliseconds, during * which we can reuse the cookie. If authentication fails due to expired * authToken, the client of AccountSelector can call authImmedately() to request * authenticate() upon the next checkin *//* w ww .ja va2 s . c o m*/ long authTimeLast = this.getLastAuthTime(); long timeSinceLastAuth = System.currentTimeMillis() - authTimeLast; if (!this.shouldAuthImmediately() && authTimeLast != 0 && (timeSinceLastAuth < AUTHENTICATE_PERIOD_MSEC)) { return; } Logger.i("Authenticating. Last authentication is " + timeSinceLastAuth / 1000 / 60 + " minutes ago. "); AccountManager accountManager = AccountManager.get(context.getApplicationContext()); if (this.authToken != null) { // There will be no effect on the token if it is still valid Logger.i("Invalidating token"); accountManager.invalidateAuthToken(ACCOUNT_TYPE, this.authToken); } Account[] accounts = accountManager.getAccountsByType(ACCOUNT_TYPE); Logger.i("Got " + accounts.length + " accounts"); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this.context); String selectedAccount = prefs.getString(Config.PREF_KEY_SELECTED_ACCOUNT, null); final String defaultUserName = Config.DEFAULT_USER; isAnonymous = true; if (selectedAccount != null && selectedAccount.equals(defaultUserName)) { return; } if (accounts != null && accounts.length > 0) { // Default account should be the Anonymous account Account accountToUse = accounts[accounts.length - 1]; if (!accounts[accounts.length - 1].name.equals(defaultUserName)) { for (Account account : accounts) { if (account.name.equals(defaultUserName)) { accountToUse = account; break; } } } if (selectedAccount != null) { for (Account account : accounts) { if (account.name.equals(selectedAccount)) { accountToUse = account; break; } } } isAnonymous = accountToUse.name.equals(defaultUserName); if (isAnonymous) { Logger.d("Skipping authentication as account is " + defaultUserName); return; } Logger.i("Trying to get auth token for " + accountToUse); AccountManagerFuture<Bundle> future = accountManager.getAuthToken(accountToUse, "ah", false, new AccountManagerCallback<Bundle>() { @Override public void run(AccountManagerFuture<Bundle> result) { Logger.i("AccountManagerCallback invoked"); try { getAuthToken(result); } catch (RuntimeException e) { Logger.e("Failed to get authToken", e); /* TODO(Wenjie): May ask the user whether to quit the app nicely here if a number * of trials have been made and failed. Since Speedometer is basically useless * without checkin */ } } }, null); Logger.i("AccountManager.getAuthToken returned " + future); } else { throw new RuntimeException("No google account found"); } }
From source file:com.mobiperf.AccountSelector.java
/** Starts an authentication request */ public void authenticate() throws OperationCanceledException, AuthenticatorException, IOException { Logger.i("AccountSelector.authenticate() running"); /* We only need to authenticate every AUTHENTICATE_PERIOD_MILLI milliseconds, during * which we can reuse the cookie. If authentication fails due to expired * authToken, the client of AccountSelector can call authImmedately() to request * authenticate() upon the next checkin *///from w ww. j a va2s. com long authTimeLast = this.getLastAuthTime(); long timeSinceLastAuth = System.currentTimeMillis() - authTimeLast; if (!this.shouldAuthImmediately() && authTimeLast != 0 && (timeSinceLastAuth < AUTHENTICATE_PERIOD_MSEC)) { return; } Logger.i("Authenticating. Last authentication is " + timeSinceLastAuth / 1000 / 60 + " minutes ago. "); AccountManager accountManager = AccountManager.get(context.getApplicationContext()); if (this.authToken != null) { // There will be no effect on the token if it is still valid Logger.i("Invalidating token"); accountManager.invalidateAuthToken(ACCOUNT_TYPE, this.authToken); } Account[] accounts = accountManager.getAccountsByType(ACCOUNT_TYPE); Logger.i("Got " + accounts.length + " accounts"); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this.context); String selectedAccount = prefs.getString(Config.PREF_KEY_SELECTED_ACCOUNT, null); final String defaultUserName = context.getString(R.string.defaultUser); isAnonymous = true; if (selectedAccount != null && selectedAccount.equals(defaultUserName)) { return; } if (accounts != null && accounts.length > 0) { // Default account should be the Anonymous account Account accountToUse = accounts[accounts.length - 1]; if (!accounts[accounts.length - 1].name.equals(defaultUserName)) { for (Account account : accounts) { if (account.name.equals(defaultUserName)) { accountToUse = account; break; } } } if (selectedAccount != null) { for (Account account : accounts) { if (account.name.equals(selectedAccount)) { accountToUse = account; break; } } } isAnonymous = accountToUse.name.equals(defaultUserName); if (isAnonymous) { Logger.d("Skipping authentication as account is " + defaultUserName); return; } Logger.i("Trying to get auth token for " + accountToUse); AccountManagerFuture<Bundle> future = accountManager.getAuthToken(accountToUse, "ah", false, new AccountManagerCallback<Bundle>() { @Override public void run(AccountManagerFuture<Bundle> result) { Logger.i("AccountManagerCallback invoked"); try { getAuthToken(result); } catch (RuntimeException e) { Logger.e("Failed to get authToken", e); /* TODO(Wenjie): May ask the user whether to quit the app nicely here if a number * of trials have been made and failed. Since Speedometer is basically useless * without checkin */ } } }, null); Logger.i("AccountManager.getAuthToken returned " + future); } else { throw new RuntimeException("No google account found"); } }
From source file:com.num.mobiperf.AccountSelector.java
/** Starts an authentication request */ public void authenticate() throws OperationCanceledException, AuthenticatorException, IOException { //Logger.i("AccountSelector.authenticate() running"); /* We only need to authenticate every AUTHENTICATE_PERIOD_MILLI milliseconds, during * which we can reuse the cookie. If authentication fails due to expired * authToken, the client of AccountSelector can call authImmedately() to request * authenticate() upon the next checkin *///from w w w . java 2s. co m long authTimeLast = this.getLastAuthTime(); long timeSinceLastAuth = System.currentTimeMillis() - authTimeLast; if (!this.shouldAuthImmediately() && authTimeLast != 0 && (timeSinceLastAuth < AUTHENTICATE_PERIOD_MSEC)) { return; } //Logger.i("Authenticating. Last authentication is " + // timeSinceLastAuth / 1000 / 60 + " minutes ago. "); AccountManager accountManager = AccountManager.get(context.getApplicationContext()); if (this.authToken != null) { // There will be no effect on the token if it is still valid //Logger.i("Invalidating token"); accountManager.invalidateAuthToken(ACCOUNT_TYPE, this.authToken); } Account[] accounts = accountManager.getAccountsByType(ACCOUNT_TYPE); //Logger.i("Got " + accounts.length + " accounts"); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this.context); String selectedAccount = prefs.getString("PREF_KEY_SELECTED_ACCOUNT", null); final String defaultUserName = "Anonymous"; isAnonymous = true; if (selectedAccount != null && selectedAccount.equals(defaultUserName)) { return; } if (accounts != null && accounts.length > 0) { // Default account should be the Anonymous account Account accountToUse = accounts[accounts.length - 1]; if (!accounts[accounts.length - 1].name.equals(defaultUserName)) { for (Account account : accounts) { if (account.name.equals(defaultUserName)) { accountToUse = account; break; } } } if (selectedAccount != null) { for (Account account : accounts) { if (account.name.equals(selectedAccount)) { accountToUse = account; break; } } } isAnonymous = accountToUse.name.equals(defaultUserName); if (isAnonymous) { //Logger.d("Skipping authentication as account is " + defaultUserName); return; } // WHERE YOU GET TOKEN!!!!!! //Logger.i("Trying to get auth token for " + accountToUse); AccountManagerFuture<Bundle> future = accountManager.getAuthToken(accountToUse, "ah", false, new AccountManagerCallback<Bundle>() { public void run(AccountManagerFuture<Bundle> result) { //Logger.i("AccountManagerCallback invoked"); try { getAuthToken(result); } catch (RuntimeException e) { System.out.println("Failed to get authToken" + e.getLocalizedMessage()); //Logger.e("Failed to get authToken", e); /* TODO(Wenjie): May ask the user whether to quit the app nicely here if a number * of trials have been made and failed. Since Speedometer is basically useless * without checkin */ } } }, null); //Logger.i("AccountManager.getAuthToken returned " + future); } else { throw new RuntimeException("No google account found"); } }
From source file:com.mobilyzer.AccountSelector.java
/** Starts an authentication request */ public void authenticate() throws OperationCanceledException, AuthenticatorException, IOException { Logger.i("AccountSelector.authenticate() running"); /* We only need to authenticate every AUTHENTICATE_PERIOD_MILLI milliseconds, during * which we can reuse the cookie. If authentication fails due to expired * authToken, the client of AccountSelector can call authImmedately() to request * authenticate() upon the next checkin *//*from w w w .j a v a2s . c om*/ long authTimeLast = this.getLastAuthTime(); long timeSinceLastAuth = System.currentTimeMillis() - authTimeLast; if (!this.shouldAuthImmediately() && authTimeLast != 0 && (timeSinceLastAuth < AUTHENTICATE_PERIOD_MSEC)) { return; } Logger.i("Authenticating. Last authentication is " + timeSinceLastAuth / 1000 / 60 + " minutes ago. "); AccountManager accountManager = AccountManager.get(context.getApplicationContext()); if (this.authToken != null) { // There will be no effect on the token if it is still valid Logger.i("Invalidating token"); accountManager.invalidateAuthToken(ACCOUNT_TYPE, this.authToken); } if (!hasGetAccountsPermission(context)) { isAnonymous = true; return; } Account[] accounts = accountManager.getAccountsByType(ACCOUNT_TYPE); Logger.i("Got " + accounts.length + " accounts"); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext()); String selectedAccount = prefs.getString(Config.PREF_KEY_SELECTED_ACCOUNT, null); Logger.i("Selected account = " + selectedAccount); final String defaultUserName = Config.DEFAULT_USER; isAnonymous = true; if (selectedAccount != null && selectedAccount.equals(defaultUserName)) { return; } if (accounts != null && accounts.length > 0) { // Default account should be the Anonymous account Account accountToUse = new Account(Config.DEFAULT_USER, ACCOUNT_TYPE); if (!accounts[accounts.length - 1].name.equals(defaultUserName)) { for (Account account : accounts) { if (account.name.equals(defaultUserName)) { accountToUse = account; break; } } } if (selectedAccount != null) { for (Account account : accounts) { if (account.name.equals(selectedAccount)) { accountToUse = account; break; } } } isAnonymous = accountToUse.name.equals(defaultUserName); if (isAnonymous) { Logger.d("Skipping authentication as account is " + defaultUserName); return; } Logger.i("Trying to get auth token for " + accountToUse); AccountManagerFuture<Bundle> future = accountManager.getAuthToken(accountToUse, "ah", false, new AccountManagerCallback<Bundle>() { @Override public void run(AccountManagerFuture<Bundle> result) { Logger.i("AccountManagerCallback invoked"); try { getAuthToken(result); } catch (RuntimeException e) { Logger.e("Failed to get authToken", e); /* TODO(Wenjie): May ask the user whether to quit the app nicely here if a number * of trials have been made and failed. Since Speedometer is basically useless * without checkin */ } } }, null); Logger.i("AccountManager.getAuthToken returned " + future); } else { throw new RuntimeException("No google account found"); } }