@SuppressLint("MissingPermission") public static void registerAccountsUpdatedListener(final Context context) { AccountManager.get(context).addOnAccountsUpdatedListener(accounts -> { String username = Preferences.getUsername(context); if (TextUtils.isEmpty(username)) { return; } for (Account account : accounts) { if (TextUtils.equals(account.name, username)) { return; } } Preferences.setUsername(context, null); }, null, true); }
@Test public void testAccountsUpdateListener_updateImmediately() { TestOnAccountsUpdateListener listener = new TestOnAccountsUpdateListener(); am.addOnAccountsUpdatedListener(listener, null, true); assertThat(listener.getInvocationCount()).isEqualTo(1); }
@Test public void testAccountsUpdateListener_duplicate() { TestOnAccountsUpdateListener listener = new TestOnAccountsUpdateListener(); am.addOnAccountsUpdatedListener(listener, null, false); am.addOnAccountsUpdatedListener(listener, null, false); assertThat(listener.getInvocationCount()).isEqualTo(0); Account account = new Account("name", "type"); shadowOf(am).addAccount(account); assertThat(listener.getInvocationCount()).isEqualTo(1); }
@Test public void testAddAccountExplicitly_notifiesListenersIfSuccessful() { TestOnAccountsUpdateListener listener = new TestOnAccountsUpdateListener(); am.addOnAccountsUpdatedListener(listener, null, false); assertThat(listener.getInvocationCount()).isEqualTo(0); Account account = new Account("name", "type"); boolean accountAdded = am.addAccountExplicitly(account, "passwd", null); assertThat(accountAdded).isTrue(); assertThat(listener.getInvocationCount()).isEqualTo(1); }
@Test public void testAddAccountExplicitly_doesNotNotifyListenersIfUnsuccessful() { Account account = new Account("name", "type"); boolean accountAdded = am.addAccountExplicitly(account, "passwd", null); assertThat(accountAdded).isTrue(); TestOnAccountsUpdateListener listener = new TestOnAccountsUpdateListener(); am.addOnAccountsUpdatedListener(listener, null, false); assertThat(listener.getInvocationCount()).isEqualTo(0); // This account is added already, so it'll fail boolean accountAdded2 = am.addAccountExplicitly(account, "passwd", null); assertThat(accountAdded2).isFalse(); assertThat(listener.getInvocationCount()).isEqualTo(0); }
@Test public void removeAccount_doesNotNotifyIfUnuccessful() { Account account = new Account("name", "type"); TestOnAccountsUpdateListener listener = new TestOnAccountsUpdateListener(); am.addOnAccountsUpdatedListener(listener, null, false); assertThat(listener.getInvocationCount()).isEqualTo(0); // The account has not been added am.removeAccount(account, null, null); assertThat(listener.getInvocationCount()).isEqualTo(0); }
@Test public void removeAccount_notifiesListenersIfSuccessful() { Account account = new Account("name", "type"); am.addAccountExplicitly(account, "passwd", null); TestOnAccountsUpdateListener listener = new TestOnAccountsUpdateListener(); am.addOnAccountsUpdatedListener(listener, null, false); assertThat(listener.getInvocationCount()).isEqualTo(0); am.removeAccount(account, null, null); assertThat(listener.getInvocationCount()).isEqualTo(1); }
@Test public void testAccountsUpdateListener() { TestOnAccountsUpdateListener listener = new TestOnAccountsUpdateListener(); am.addOnAccountsUpdatedListener(listener, null, false); assertThat(listener.getInvocationCount()).isEqualTo(0); Account account = new Account("name", "type"); shadowOf(am).addAccount(account); assertThat(listener.getInvocationCount()).isEqualTo(1); }
@Test public void testAccountsUpdateListener_listenerNotInvokedAfterRemoval() { TestOnAccountsUpdateListener listener = new TestOnAccountsUpdateListener(); am.addOnAccountsUpdatedListener(listener, null, false); assertThat(listener.getInvocationCount()).isEqualTo(0); Account account = new Account("name", "type"); shadowOf(am).addAccount(account); assertThat(listener.getInvocationCount()).isEqualTo(1); am.removeOnAccountsUpdatedListener(listener); shadowOf(am).addAccount(account); assertThat(listener.getInvocationCount()).isEqualTo(1); }
private void setUpAuthenticationListener() { getAccountManager().addOnAccountsUpdatedListener(this, null, false); }
mContext.registerReceiver(mBroadcastReceiver, filter); mAccountManager.addOnAccountsUpdatedListener(this, mListenerHandler, false);
/** * This method should be called only from Singletons so the lifecycle * is tied with the application, so we don't have to cleanup the listeners * * @param callback to be called on accounts updated */ public void addOnAccountsUpdatedListener(OnAccountsUpdatedListener callback) { try { OnAccountsUpdateListener listener = accounts -> { Set<MovirtAccount> filtered = new HashSet<>(); for (Account account : accounts) { if (Constants.ACCOUNT_TYPE.equals(account.type)) { try { filtered.add(asMoAccount(account)); } catch (IllegalStateException incompatibleAccount) { removeAccount(new MovirtAccount("", account), null); // remove old account } } } callback.onAccountsUpdated(filtered); }; accountManager.addOnAccountsUpdatedListener(listener, null, true); } catch (SecurityException e) { commonMessageHelper.showError(ErrorType.NORMAL, resources.getMissingAccountsPermissionError()); } }
@Override public void logout() { final AccountManager am = AccountManager.get(TomahawkApp.getContext()); if (am != null && getAccount() != null) { am.removeAccount(getAccount(), null, null); mWaitingForAccountRemoval = true; am.addOnAccountsUpdatedListener(new OnAccountsUpdateListener() { @Override public void onAccountsUpdated(Account[] accounts) { if (mWaitingForAccountRemoval && getAccount() == null) { am.removeOnAccountsUpdatedListener(this); mWaitingForAccountRemoval = false; onLogout(); } } }, null, false); } }
Listactivity.accountManager.addOnAccountsUpdatedListener((OnAccountsUpdateListener) new AccountsUpdateListener(), null, true);
accountManager.addOnAccountsUpdatedListener(this, null, true);