Fix tinting on injected pref icon in Tile

We used to tint the icon when refreshing Tile.  However, when we getIcon
from Tile, we get the icon directly from the icon resource but not the
copy of the tinted icon.  The tinting was not honored in this case.

Do a little refactor in this cl to tint the icon in Tile.getIcon
instead.

Test: Manual/Visual inspection, robotests
Fixes: 129010399
Change-Id: I88c9b00046534d8b9ddedce9009a6e340d42fca6
This commit is contained in:
lindatseng
2019-04-02 11:23:22 -07:00
parent 51b78e305d
commit fff2b19e56
3 changed files with 16 additions and 20 deletions

View File

@@ -33,6 +33,7 @@ import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.drawable.Icon;
import android.os.Bundle;
import android.os.Parcel;
@@ -299,7 +300,15 @@ public class Tile implements Parcelable {
}
}
if (iconResId != 0) {
return Icon.createWithResource(activityInfo.packageName, iconResId);
final Icon icon = Icon.createWithResource(activityInfo.packageName, iconResId);
if (isIconTintable(context)) {
final TypedArray a = context.obtainStyledAttributes(new int[] {
android.R.attr.colorControlNormal});
final int tintColor = a.getColor(0, 0);
a.recycle();
icon.setTint(tintColor);
}
return icon;
} else {
return null;
}
@@ -309,16 +318,12 @@ public class Tile implements Parcelable {
* Whether the icon can be tinted. This is true when icon needs to be monochrome (single-color)
*/
public boolean isIconTintable(Context context) {
ensureMetadataNotStale(context);
if (mMetaData != null
&& mMetaData.containsKey(TileUtils.META_DATA_PREFERENCE_ICON_TINTABLE)) {
return mMetaData.getBoolean(TileUtils.META_DATA_PREFERENCE_ICON_TINTABLE);
}
ensureMetadataNotStale(context);
final String pkgName = context.getPackageName();
// If this drawable is coming from outside Settings, tint it to match the color.
final ActivityInfo activityInfo = getActivityInfo(context);
return activityInfo != null
&& !TextUtils.equals(pkgName, activityInfo.packageName);
return false;
}
/**

View File

@@ -116,16 +116,11 @@ public class TileTest {
}
@Test
public void isIconTintable_noMetadata_shouldReturnPackageNameCheck() {
final Tile tile1 = new Tile(mActivityInfo, "category");
assertThat(tile1.isIconTintable(RuntimeEnvironment.application)).isFalse();
public void isIconTintable_noTintableMetadata_shouldReturnFalse() {
final Tile tile = new Tile(mActivityInfo, "category");
mActivityInfo.metaData.putInt(META_DATA_PREFERENCE_ICON, android.R.drawable.ic_info);
final ActivityInfo activityInfo = new ActivityInfo();
activityInfo.packageName = "blah";
activityInfo.name = "abc";
final Tile tile2 = new Tile(activityInfo, "category");
assertThat(tile2.isIconTintable(RuntimeEnvironment.application)).isTrue();
assertThat(tile.isIconTintable(RuntimeEnvironment.application)).isFalse();
}
@Test

View File

@@ -205,10 +205,6 @@ public class TileUtilsTest {
null /* defaultCategory */, outTiles, false /* usePriority */);
assertThat(outTiles.size()).isEqualTo(1);
assertThat(outTiles.get(0).getTitle(mContext)).isEqualTo("my localized title");
// Icon should be tintable because the tile is not from settings package, and
// "forceTintExternalIcon" is set
assertThat(outTiles.get(0).isIconTintable(mContext)).isTrue();
}
@Test