From 7f279cef11f1ae39a4c2386d004c8eef5180ab5b Mon Sep 17 00:00:00 2001 From: Alexandra Gherghina Date: Wed, 24 Sep 2014 11:29:48 +0100 Subject: [PATCH] Add null check for sync authorities of an account type This protects against a potential NPE if there are no sync authorities for an account of a given type. Bug: 17612697 Change-Id: Ifd34b5b7692cbcc8de78d353eedd284e499f954e --- .../settings/accounts/AccountSettings.java | 37 ++++++++++++------- .../accounts/AuthenticatorHelper.java | 2 +- 2 files changed, 24 insertions(+), 15 deletions(-) diff --git a/src/com/android/settings/accounts/AccountSettings.java b/src/com/android/settings/accounts/AccountSettings.java index 3c0d9e9b92a..8a183da5473 100644 --- a/src/com/android/settings/accounts/AccountSettings.java +++ b/src/com/android/settings/accounts/AccountSettings.java @@ -361,20 +361,9 @@ public class AccountSettings extends SettingsPreferenceFragment for (int i = 0; i < accountTypes.length; i++) { final String accountType = accountTypes[i]; - if (mAuthoritiesCount > 0) { - // Skip showing any account that does not have any of the requested authorities - final ArrayList authoritiesForType = helper.getAuthoritiesForAccountType( - accountType); - boolean show = false; - for (int j = 0; j < mAuthoritiesCount; j++) { - if (authoritiesForType.contains(mAuthorities[j])) { - show = true; - break; - } - } - if (!show) { - continue; - } + // Skip showing any account that does not have any of the requested authorities + if (!accountTypeHasAnyRequestedAuthorities(helper, accountType)) { + continue; } final CharSequence label = helper.getLabelForType(getActivity(), accountType); if (label == null) { @@ -418,6 +407,26 @@ public class AccountSettings extends SettingsPreferenceFragment return accountTypePreferences; } + private boolean accountTypeHasAnyRequestedAuthorities(AuthenticatorHelper helper, + String accountType) { + if (mAuthoritiesCount == 0) { + // No authorities required + return true; + } + final ArrayList authoritiesForType = helper.getAuthoritiesForAccountType( + accountType); + if (authoritiesForType == null) { + Log.d(TAG, "No sync authorities for account type: " + accountType); + return false; + } + for (int j = 0; j < mAuthoritiesCount; j++) { + if (authoritiesForType.contains(mAuthorities[j])) { + return true; + } + } + return false; + } + private class AccountPreference extends Preference implements OnPreferenceClickListener { /** * Title of the tile that is shown to the user. diff --git a/src/com/android/settings/accounts/AuthenticatorHelper.java b/src/com/android/settings/accounts/AuthenticatorHelper.java index 6cebf5f9980..cc8a6d58c7c 100644 --- a/src/com/android/settings/accounts/AuthenticatorHelper.java +++ b/src/com/android/settings/accounts/AuthenticatorHelper.java @@ -190,10 +190,10 @@ final public class AuthenticatorHelper extends BroadcastReceiver { mEnabledAccountTypes.add(account.type); } } + buildAccountTypeToAuthoritiesMap(); if (mListeningToAccountUpdates) { mListener.onAccountsUpdate(mUserHandle); } - buildAccountTypeToAuthoritiesMap(); } @Override