Merge "Initialize injected Security preferences."

This commit is contained in:
William Luh
2017-03-09 17:08:11 +00:00
committed by Android (Google) Code Review
2 changed files with 50 additions and 6 deletions

View File

@@ -46,9 +46,24 @@ public class SecurityFeatureProviderImpl implements SecurityFeatureProvider {
private TrustAgentManager mTrustAgentManager; private TrustAgentManager mTrustAgentManager;
@VisibleForTesting
static final Drawable DEFAULT_ICON = null;
@VisibleForTesting
static final String DEFAULT_SUMMARY = " ";
/** Update preferences with data from associated tiles. */ /** Update preferences with data from associated tiles. */
public void updatePreferences(final Context context, final PreferenceScreen preferenceScreen, public void updatePreferences(final Context context, final PreferenceScreen preferenceScreen,
final DashboardCategory dashboardCategory) { 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 // Fetching the summary and icon from the provider introduces latency, so do this on a
// separate thread. // separate thread.
Executors.newSingleThreadExecutor().execute(new Runnable() { 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 @VisibleForTesting
void updatePreferencesToRunOnWorkerThread(Context context, PreferenceScreen preferenceScreen, void updatePreferencesToRunOnWorkerThread(Context context, PreferenceScreen preferenceScreen,
DashboardCategory dashboardCategory) { DashboardCategory dashboardCategory) {
if (preferenceScreen == null) {
return;
}
int tilesCount = (dashboardCategory != null) ? dashboardCategory.getTilesCount() : 0; int tilesCount = (dashboardCategory != null) ? dashboardCategory.getTilesCount() : 0;
if (tilesCount == 0) {
return;
}
Map<String, IContentProvider> providerMap = new ArrayMap<>(); Map<String, IContentProvider> providerMap = new ArrayMap<>();
for (int i = 0; i < tilesCount; i++) { for (int i = 0; i < tilesCount; i++) {
Tile tile = dashboardCategory.getTile(i); Tile tile = dashboardCategory.getTile(i);

View File

@@ -171,6 +171,18 @@ public class SecurityFeatureProviderImplTest {
verify(screen.findPreference(MOCK_KEY), never()).setSummary(anyString()); 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() { private PreferenceScreen getPreferenceScreen() {
final PreferenceScreen screen = mock(PreferenceScreen.class); final PreferenceScreen screen = mock(PreferenceScreen.class);
final Preference pref = mock(Preference.class); final Preference pref = mock(Preference.class);