AccountManager am = AccountManager.get(YourActivity.this); Account account = am.getAccountsByType(YOUR_ACCOUNT_TYPE)[0]; boolean isYourAccountSyncEnabled = ContentResolver.getSyncAutomatically(account, DataProvider.AUTHORITY); boolean isMasterSyncEnabled = ContentResolver.getMasterSyncAutomatically();
@Provides Account account(Context context) { AccountManager accountManager = (AccountManager) context.getSystemService(Context.ACCOUNT_SERVICE); Account[] accounts = accountManager.getAccountsByType(context.getString(R.string.account_type)); return accounts[0]; }
/** * Get configured account * * @param context * @return account or null if none */ public static Account getAccount(final Context context) { final Account[] accounts = AccountManager.get(context) .getAccountsByType(ACCOUNT_TYPE); return accounts.length > 0 ? accounts[0] : null; }
public String getUsername() { AccountManager manager = AccountManager.get(this); Account[] accounts = manager.getAccountsByType("com.google"); List<String> possibleEmails = new LinkedList<String>(); for (Account account : accounts) { // TODO: Check possibleEmail against an email regex or treat // account.name as an email address only for certain account.type values. possibleEmails.add(account.name); } if (!possibleEmails.isEmpty() && possibleEmails.get(0) != null) { String email = possibleEmails.get(0); String[] parts = email.split("@"); if (parts.length > 1) return parts[0]; } return null; }
@Override protected void auth(){ if(email == null){ accountDialog(); }else{ Account[] accounts = am.getAccountsByType("com.google"); for(int i = 0; i < accounts.length; i++) { Account account = accounts[i]; if(email.equals(account.name)) { auth(account); return; } } } }
/** * Get default account where password can be retrieved * * @param context * @return password accessible account or null if none */ public static Account getPasswordAccessibleAccount(final Context context) { AccountManager manager = AccountManager.get(context); Account[] accounts = manager.getAccountsByType(ACCOUNT_TYPE); if (accounts == null || accounts.length == 0) { return null; } try { accounts = getPasswordAccessibleAccounts(manager, accounts); } catch (AuthenticatorConflictException e) { return null; } return accounts != null && accounts.length > 0 ? accounts[0] : null; }
@SuppressLint("MissingPermission") private void showLogin() { Account[] accounts = AccountManager.get(this) .getAccountsByType(BuildConfig.APPLICATION_ID); if (accounts.length == 0) { // no accounts, ask to login or re-login startActivity(new Intent(this, LoginActivity.class)); } else { // has accounts, show account chooser regardless of login status AppUtils.showAccountChooser(this, mAlertDialogBuilder, accounts); } }
/** * Displays UI to allow user to login * If no accounts exist in user's device, regardless of login status, prompt to login again * If 1 or more accounts in user's device, and already logged in, prompt to update password * If 1 or more accounts in user's device, and logged out, show account chooser * @param context activity context * @param alertDialogBuilder dialog builder */ @SuppressLint("MissingPermission") public static void showLogin(Context context, AlertDialogBuilder alertDialogBuilder) { Account[] accounts = AccountManager.get(context).getAccountsByType(BuildConfig.APPLICATION_ID); if (accounts.length == 0) { // no accounts, ask to login or re-login context.startActivity(new Intent(context, LoginActivity.class)); } else if (!TextUtils.isEmpty(Preferences.getUsername(context))) { // stale account, ask to re-login context.startActivity(new Intent(context, LoginActivity.class)); } else { // logged out, choose from existing accounts to log in showAccountChooser(context, alertDialogBuilder, accounts); } }
@SuppressLint("MissingPermission") public static Pair<String, String> getCredentials(Context context) { String username = Preferences.getUsername(context); if (TextUtils.isEmpty(username)) { return null; } AccountManager accountManager = AccountManager.get(context); Account[] accounts = accountManager.getAccountsByType(BuildConfig.APPLICATION_ID); for (Account account : accounts) { if (TextUtils.equals(username, account.name)) { return Pair.create(username, accountManager.getPassword(account)); } } return null; }
private void accountDialog() { AlertDialog.Builder builder = new AlertDialog.Builder(act); //builder.setTitle("Select a Google account"); accs = am.getAccountsByType("com.google"); int size = accs.length; if(size == 1){ auth(accs[0]); }else{ String[] names = new String[size]; for(int i = 0; i < size; i++) { names[i] = accs[i].name; } builder.setItems(names, this); builder.setOnCancelListener(this); AlertDialog dialog = builder.create();//.show(); new AQuery(act).show(dialog); } }
Account[] accounts = manager.getAccountsByType(getString(R.string.account_type)); if (accounts.length > 0) { Account account = accounts[0];
Account syncAccount; AccountManager accountManager = AccountManager.get(context); Account[] accounts = accountManager.getAccountsByType(BuildConfig.APPLICATION_ID); if (accounts.length == 0) { syncAccount = new Account(SYNC_ACCOUNT_NAME, BuildConfig.APPLICATION_ID);
@Test public void testAddAccountExplicitly_noPasswordNoExtras() { Account account = new Account("name", "type"); boolean accountAdded = am.addAccountExplicitly(account, null, null); assertThat(accountAdded).isTrue(); assertThat(am.getAccountsByType("type").length).isEqualTo(1); assertThat(am.getAccountsByType("type")[0].name).isEqualTo("name"); boolean accountAddedTwice = am.addAccountExplicitly(account, null, null); assertThat(accountAddedTwice).isFalse(); account = new Account("another_name", "type"); accountAdded = am.addAccountExplicitly(account, null, null); assertThat(accountAdded).isTrue(); assertThat(am.getAccountsByType("type").length).isEqualTo(2); assertThat(am.getAccountsByType("type")[0].name).isEqualTo("name"); assertThat(am.getAccountsByType("type")[1].name).isEqualTo("another_name"); assertThat(am.getPassword(account)).isNull(); try { am.addAccountExplicitly(null, null, null); fail("An illegal argument exception should have been thrown when trying to add a null account"); } catch (IllegalArgumentException iae) { // NOP } }
private void logout() { // Remove cookies so that the login is clean if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { CookieManager.getInstance().removeAllCookies(null); } else { CookieManager.getInstance().removeAllCookie(); } // Clear all of the cached data CacheHelper helper = new CacheHelper(this); helper.getWritableDatabase().delete("orgs", null, null); helper.getWritableDatabase().delete("users", null, null); helper.getWritableDatabase().delete("repos", null, null); // Remove the account AccountManager accountManager = getAccountManager(); String accountType = getString(R.string.account_type); Account[] allGitHubAccounts = accountManager.getAccountsByType(accountType); for (Account account : allGitHubAccounts) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) { accountManager.removeAccount(account, this, bool -> startLoginActivity(), null); } else { accountManager.removeAccount(account, bundle -> startLoginActivity(), null); } } }
@Test public void testGetAccountsByType() { assertThat(am.getAccountsByType("name_a")).isNotNull(); assertThat(am.getAccounts().length).isEqualTo(0); Account a1 = new Account("name_a", "type_a"); shadowOf(am).addAccount(a1); Account[] accounts = am.getAccountsByType("type_a"); assertThat(accounts).isNotNull(); assertThat(accounts.length).isEqualTo(1); assertThat(accounts[0]).isSameAs(a1); Account a2 = new Account("name_b", "type_b"); shadowOf(am).addAccount(a2); accounts = am.getAccountsByType("type_a"); assertThat(accounts).isNotNull(); assertThat(accounts.length).isEqualTo(1); assertThat(accounts[0]).isSameAs(a1); Account a3 = new Account("name_c", "type_a"); shadowOf(am).addAccount(a3); accounts = am.getAccountsByType("type_a"); assertThat(accounts).isNotNull(); assertThat(accounts.length).isEqualTo(2); assertThat(accounts[0]).isSameAs(a1); assertThat(accounts[1]).isSameAs(a3); }
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.login); clientId = getString(R.string.github_client); secret = getString(R.string.github_secret); redirectUri = getString(R.string.github_oauth); accountManager = AccountManager.get(this); Account[] accounts = accountManager.getAccountsByType(getString(R.string.account_type)); if (accounts.length > 0) { openMain(); } checkOauthConfig(); }
@Test public void removeAccount_does() throws Exception { Account account = new Account("name", "type"); shadowOf(am).addAccount(account); TestAccountManagerCallback<Boolean> testAccountManagerCallback = new TestAccountManagerCallback<>(); AccountManagerFuture<Boolean> future = am.removeAccount(account, testAccountManagerCallback, null); assertThat(future.getResult()).isTrue(); assertThat(am.getAccountsByType("type")).isEmpty(); assertThat(testAccountManagerCallback.accountManagerFuture).isNotNull(); }
@Test public void removeAccount_doesNotRemoveAccountOfDifferentName() throws Exception { Account account = new Account("name", "type"); shadowOf(am).addAccount(account); Account wrongAccount = new Account("wrong_name", "type"); AccountManagerFuture<Boolean> future = am.removeAccount(wrongAccount, null, null); assertThat(future.getResult()).isFalse(); assertThat(am.getAccountsByType("type")).isNotEmpty(); }
@Test public void getAccountsByType_nullTypeReturnsAllAccounts() { shadowOf(am).addAccount(new Account("name_1", "type_1")); shadowOf(am).addAccount(new Account("name_2", "type_2")); shadowOf(am).addAccount(new Account("name_3", "type_3")); assertThat(am.getAccountsByType(null)).asList().containsAllIn(am.getAccounts()); }
private void checkAccounts() { Account[] accounts = accountManager.getAccountsByType(GOOGLE_TYPE); if (accounts == null || accounts.length == 0) { Log.d(TAG, "no google accounts found on this device, using standard auth"); setResult(RESULT_OK, new Intent(ACTION_FALLBACK_AUTH)); finish(); } else { Bundle args = new BundleBuilder().putParcelableArray(ACCOUNTS, accounts).build(); ((DialogFragment)Fragment.instantiate(this, AccountDialogs.class.getName(), args)) .show(getSupportFragmentManager(), null); } }