From 91eaa85c52f59166eafb2412aef65d47886ed627 Mon Sep 17 00:00:00 2001 From: William Luh Date: Wed, 8 Mar 2017 11:44:54 -0800 Subject: [PATCH] Initialize injected Security preferences. Previously a default "G" icon was loaded before the injected ones and this would be visually jarring. Also the text summary would cause the titles to shift up, so now load an empty icon and empty summary so no more jarring visual effects. Bug: 35994047 Test: make RunSettingsRoboTests Change-Id: Ia06535215432fe350a3bb06f541e817a566772e0 --- .../security/SecurityFeatureProviderImpl.java | 44 ++++++++++++++++--- .../SecurityFeatureProviderImplTest.java | 12 +++++ 2 files changed, 50 insertions(+), 6 deletions(-) diff --git a/src/com/android/settings/security/SecurityFeatureProviderImpl.java b/src/com/android/settings/security/SecurityFeatureProviderImpl.java index 2d782a66852..7be2d662061 100644 --- a/src/com/android/settings/security/SecurityFeatureProviderImpl.java +++ b/src/com/android/settings/security/SecurityFeatureProviderImpl.java @@ -46,9 +46,24 @@ public class SecurityFeatureProviderImpl implements SecurityFeatureProvider { private TrustAgentManager mTrustAgentManager; + @VisibleForTesting + static final Drawable DEFAULT_ICON = null; + @VisibleForTesting + static final String DEFAULT_SUMMARY = " "; + /** Update preferences with data from associated tiles. */ public void updatePreferences(final Context context, final PreferenceScreen preferenceScreen, final DashboardCategory dashboardCategory) { + if (preferenceScreen == null) { + return; + } + int tilesCount = (dashboardCategory != null) ? dashboardCategory.getTilesCount() : 0; + if (tilesCount == 0) { + return; + } + + initPreferences(context, preferenceScreen, dashboardCategory); + // Fetching the summary and icon from the provider introduces latency, so do this on a // separate thread. Executors.newSingleThreadExecutor().execute(new Runnable() { @@ -59,17 +74,34 @@ public class SecurityFeatureProviderImpl implements SecurityFeatureProvider { }); } + @VisibleForTesting + static void initPreferences(Context context, PreferenceScreen preferenceScreen, + DashboardCategory dashboardCategory) { + int tilesCount = (dashboardCategory != null) ? dashboardCategory.getTilesCount() : 0; + for (int i = 0; i < tilesCount; i++) { + Tile tile = dashboardCategory.getTile(i); + // If the tile does not have a key or appropriate meta data, skip it. + if (TextUtils.isEmpty(tile.key) || (tile.metaData == null)) { + continue; + } + Preference matchingPref = preferenceScreen.findPreference(tile.key); + // If the tile does not have a matching preference, skip it. + if (matchingPref == null) { + continue; + } + // Remove any icons that may be loaded before we inject the final icon. + matchingPref.setIcon(DEFAULT_ICON); + // Reserve room for the summary. This prevents the title from having to shift when the + // final title is injected. + matchingPref.setSummary(DEFAULT_SUMMARY); + } + } + @VisibleForTesting void updatePreferencesToRunOnWorkerThread(Context context, PreferenceScreen preferenceScreen, DashboardCategory dashboardCategory) { - if (preferenceScreen == null) { - return; - } int tilesCount = (dashboardCategory != null) ? dashboardCategory.getTilesCount() : 0; - if (tilesCount == 0) { - return; - } Map providerMap = new ArrayMap<>(); for (int i = 0; i < tilesCount; i++) { Tile tile = dashboardCategory.getTile(i); diff --git a/tests/robotests/src/com/android/settings/security/SecurityFeatureProviderImplTest.java b/tests/robotests/src/com/android/settings/security/SecurityFeatureProviderImplTest.java index 49eb48b33e1..50debb46e9b 100644 --- a/tests/robotests/src/com/android/settings/security/SecurityFeatureProviderImplTest.java +++ b/tests/robotests/src/com/android/settings/security/SecurityFeatureProviderImplTest.java @@ -171,6 +171,18 @@ public class SecurityFeatureProviderImplTest { verify(screen.findPreference(MOCK_KEY), never()).setSummary(anyString()); } + @Test + public void initPreferences_shouldLoadDefaults() { + PreferenceScreen screen = getPreferenceScreen(); + DashboardCategory dashboardCategory = getDashboardCategory(); + dashboardCategory.getTile(0).metaData = new Bundle(); + + mImpl.initPreferences(mContext, screen, dashboardCategory); + verify(screen.findPreference(MOCK_KEY)).setIcon(SecurityFeatureProviderImpl.DEFAULT_ICON); + verify(screen.findPreference(MOCK_KEY)) + .setSummary(SecurityFeatureProviderImpl.DEFAULT_SUMMARY); + } + private PreferenceScreen getPreferenceScreen() { final PreferenceScreen screen = mock(PreferenceScreen.class); final Preference pref = mock(Preference.class);