Treat icon color metadata as raw value instead of reference

Bug: 79841665
Test: robotests
Change-Id: I1216718e45e80a428e8a0edb7b9accf5325be769
This commit is contained in:
Fan Zhang
2018-08-06 15:23:45 -07:00
parent 3cd093efe8
commit 58543721f8
2 changed files with 42 additions and 8 deletions

View File

@@ -64,7 +64,7 @@ public class DashboardAdapter extends RecyclerView.Adapter<DashboardAdapter.Dash
@VisibleForTesting @VisibleForTesting
static final String STATE_CONDITION_EXPANDED = "condition_expanded"; static final String STATE_CONDITION_EXPANDED = "condition_expanded";
static final String META_DATA_PREFERENCE_ICON_BACKGROUND_ARGB = "com.android.settings.bg.argb";
private final IconCache mCache; private final IconCache mCache;
private final Context mContext; private final Context mContext;
private final MetricsFeatureProvider mMetricsFeatureProvider; private final MetricsFeatureProvider mMetricsFeatureProvider;
@@ -320,15 +320,25 @@ public class DashboardAdapter extends RecyclerView.Adapter<DashboardAdapter.Dash
if (!TextUtils.equals(tileIcon.getResPackage(), mContext.getPackageName()) if (!TextUtils.equals(tileIcon.getResPackage(), mContext.getPackageName())
&& !(icon instanceof RoundedHomepageIcon)) { && !(icon instanceof RoundedHomepageIcon)) {
icon = new RoundedHomepageIcon(mContext, icon); icon = new RoundedHomepageIcon(mContext, icon);
final Bundle metaData = tile.getMetaData();
try { try {
final Bundle metaData = tile.getMetaData();
if (metaData != null) { if (metaData != null) {
final int colorRes = metaData.getInt( // Load from bg.argb first
TileUtils.META_DATA_PREFERENCE_ICON_BACKGROUND_HINT, 0 /* default */); int bgColor = metaData.getInt(META_DATA_PREFERENCE_ICON_BACKGROUND_ARGB,
if (colorRes != 0) { 0 /* default */);
final int bgColor = mContext.getPackageManager() // Not found, load from bg.hint
.getResourcesForApplication(tileIcon.getResPackage()) if (bgColor == 0) {
.getColor(colorRes, null /* theme */); final int colorRes = metaData.getInt(
TileUtils.META_DATA_PREFERENCE_ICON_BACKGROUND_HINT,
0 /* default */);
if (colorRes != 0) {
bgColor = mContext.getPackageManager()
.getResourcesForApplication(tileIcon.getResPackage())
.getColor(colorRes, null /* theme */);
}
}
// If found anything, use it.
if (bgColor != 0) {
((RoundedHomepageIcon) icon).setBackgroundColor(bgColor); ((RoundedHomepageIcon) icon).setBackgroundColor(bgColor);
} }
} }

View File

@@ -241,6 +241,30 @@ public class DashboardAdapterTest {
.isInstanceOf(RoundedHomepageIcon.class); .isInstanceOf(RoundedHomepageIcon.class);
} }
@Test
public void onBindTile_externalTileWithBackgroundColorRawValue_shouldUpdateIcon() {
final Context context = spy(RuntimeEnvironment.application);
final View view = LayoutInflater.from(context).inflate(R.layout.dashboard_tile, null);
final DashboardAdapter.DashboardItemHolder holder =
new DashboardAdapter.DashboardItemHolder(view);
final Tile tile = spy(new Tile(mActivityInfo, CategoryKey.CATEGORY_HOMEPAGE));
tile.getMetaData().putInt(DashboardAdapter.META_DATA_PREFERENCE_ICON_BACKGROUND_ARGB,
0xff0000);
doReturn(Icon.createWithResource(context, R.drawable.ic_settings))
.when(tile).getIcon(context);
final IconCache iconCache = new IconCache(context);
mDashboardAdapter = new DashboardAdapter(context, null /* savedInstanceState */,
null /* conditions */, null /* suggestionControllerMixin */, null /* lifecycle */);
ReflectionHelpers.setField(mDashboardAdapter, "mCache", iconCache);
doReturn("another.package").when(context).getPackageName();
mDashboardAdapter.onBindTile(holder, tile);
final RoundedHomepageIcon homepageIcon = (RoundedHomepageIcon) iconCache.getIcon(
tile.getIcon(context));
assertThat(homepageIcon.mBackgroundColor).isEqualTo(0xff0000);
}
@Test @Test
public void onBindTile_externalTileWithBackgroundColorHint_shouldUpdateIcon() { public void onBindTile_externalTileWithBackgroundColorHint_shouldUpdateIcon() {
final Context context = spy(RuntimeEnvironment.application); final Context context = spy(RuntimeEnvironment.application);