List of usage examples for android.accounts AccountManager getAccountsByType
@NonNull
public Account[] getAccountsByType(String type)
From source file:eu.trentorise.smartcampus.ac.authenticator.AMSCAccessProvider.java
@Override public String getAuthToken(final Activity activity, String inAuthority) throws OperationCanceledException, AuthenticatorException, IOException { final String authority = inAuthority == null ? Constants.AUTHORITY_DEFAULT : inAuthority; final AccountManager am = AccountManager.get(activity); String token = am.peekAuthToken( new Account(Constants.getAccountName(activity), Constants.getAccountType(activity)), authority); if (token == null) { final Account a = new Account(Constants.getAccountName(activity), Constants.getAccountType(activity)); Account[] accounts = am.getAccountsByType(Constants.getAccountType(activity)); if (accounts == null || accounts.length == 0) { am.addAccount(Constants.getAccountType(activity), authority, null, null, null, new Callback(authority, activity), null); } else {//from ww w . j a v a2 s .c o m am.getAuthToken(a, authority, null, null, new Callback(authority, activity), null); } return null; } return token; }
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 *///from w w w . j a v a 2 s . c o m 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:cn.code.notes.gtask.remote.GTaskClient.java
private String loginGoogleAccount(Activity activity, boolean invalidateToken) { String authToken;/* www . j av a 2 s .com*/ 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.mobiperf.speedometer.AccountSelector.java
/** Starts an authentication request */ public void authenticate() throws OperationCanceledException, AuthenticatorException, IOException { Logger.i("AccountSelector.authenticate() running"); /*/*from w ww . ja v a 2 s. com*/ * 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.ntsync.android.sync.activities.ShopActivity.java
public void messageDialogClosed(CharSequence dlgTitle, boolean positiveBtnPressed) { if (positiveBtnPressed) { // Delete all PaymentData AccountManager acm = AccountManager.get(this); Account[] accounts = acm.getAccountsByType(Constants.ACCOUNT_TYPE); for (Account account : accounts) { PaymentData paymentData = SyncUtils.getPayment(account, acm); if (paymentData != null) { SyncUtils.savePayment(account, acm, null, null); break; }//from w w w.j av a 2 s.c o m } } else { finish(); } }
From source file:com.example.simba.myapplicationnote.gtask.remote.GTaskClient.java
private String loginGoogleAccount(Activity activity, boolean invalidateToken) { String authToken;/*from www .j a va2s .com*/ 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:ru.orangesoftware.financisto.activity.FlowzrSyncActivity.java
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); FlowzrSyncActivity.me = this; mNotifyBuilder = new NotificationCompat.Builder(getApplicationContext()); nm = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE); setContentView(R.layout.flowzr_sync); restoreUIFromPref();// www . j ava 2 s. c o m AccountManager accountManager = AccountManager.get(getApplicationContext()); final Account[] accounts = accountManager.getAccountsByType("com.google"); if (accounts.length < 1) { new AlertDialog.Builder(this).setTitle(getString(R.string.flowzr_sync_error)) .setMessage(R.string.account_required) .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); finish(); } }).show(); } //checkbox CheckBox chk = (CheckBox) findViewById(R.id.chk_sync_from_zero); OnClickListener chk_listener = new OnClickListener() { public void onClick(View v) { lastSyncLocalTimestamp = 0; renderLastTime(0); flowzrSyncEngine.resetLastTime(getApplicationContext()); } }; chk.setOnClickListener(chk_listener); //radio crendentials RadioGroup radioGroupCredentials = (RadioGroup) findViewById(R.id.radioCredentials); OnClickListener radio_listener = new OnClickListener() { public void onClick(View v) { RadioButton radioButtonSelected = (RadioButton) findViewById(v.getId()); for (Account account : accounts) { if (account.name == radioButtonSelected.getText()) { lastSyncLocalTimestamp = 0; renderLastTime(0); flowzrSyncEngine.resetLastTime(getApplicationContext()); useCredential = account; } } } }; radioGroupCredentials.setOnClickListener(radio_listener); //initialize value for (int i = 0; i < accounts.length; i++) { RadioButton rb = new RadioButton(this); radioGroupCredentials.addView(rb); //, 0, lp); rb.setOnClickListener(radio_listener); rb.setText(((Account) accounts[i]).name); String prefAccount = MyPreferences.getFlowzrAccount(this); if (prefAccount != null) { if (accounts[i].name.equals(prefAccount)) { useCredential = accounts[i]; rb.toggle(); //.setChecked(true); } } } bOk = (Button) findViewById(R.id.bOK); bOk.setOnClickListener(new View.OnClickListener() { public void onClick(View view) { startSync(); } }); Button bCancel = (Button) findViewById(R.id.bCancel); bCancel.setOnClickListener(new View.OnClickListener() { public void onClick(View view) { if (flowzrSyncEngine != null) { flowzrSyncEngine.isCanceled = true; } isRunning = false; setResult(RESULT_CANCELED); setReady(); startActivity(new Intent(getApplicationContext(), MainActivity.class)); //finish(); } }); Button textViewAbout = (Button) findViewById(R.id.buySubscription); textViewAbout.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { if (isOnline(FlowzrSyncActivity.this)) { visitFlowzr(useCredential); } else { showErrorPopup(FlowzrSyncActivity.this, R.string.flowzr_sync_error_no_network); } } }); Button textViewAboutAnon = (Button) findViewById(R.id.visitFlowzr); textViewAboutAnon.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { if (isOnline(FlowzrSyncActivity.this)) { visitFlowzr(null); } else { showErrorPopup(FlowzrSyncActivity.this, R.string.flowzr_sync_error_no_network); } } }); TextView textViewNotes = (TextView) findViewById(R.id.flowzrPleaseNote); textViewNotes.setMovementMethod(LinkMovementMethod.getInstance()); textViewNotes.setText(Html.fromHtml(getString(R.string.flowzr_terms_of_use))); if (MyPreferences.isAutoSync(this)) { if (checkPlayServices()) { gcm = GoogleCloudMessaging.getInstance(this); regid = getRegistrationId(getApplicationContext()); if (regid.equals("")) { registerInBackground(); } Log.i(TAG, "Google Cloud Messaging registered as :" + regid); } else { Log.i(TAG, "No valid Google Play Services APK 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 *//* w w w . jav a 2 s . 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(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.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 *//*from w w w . j a v a 2 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.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 ww w . j av a2s . 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("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"); } }