OSDN Git Service

Fix tinting on injected pref icon in Tile
authorlindatseng <lindatseng@google.com>
Tue, 2 Apr 2019 18:23:22 +0000 (11:23 -0700)
committerlindatseng <lindatseng@google.com>
Tue, 2 Apr 2019 23:29:04 +0000 (16:29 -0700)
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

packages/SettingsLib/Tile/src/com/android/settingslib/drawer/Tile.java
packages/SettingsLib/tests/robotests/src/com/android/settingslib/drawer/TileTest.java
packages/SettingsLib/tests/robotests/src/com/android/settingslib/drawer/TileUtilsTest.java

index 5108efb..0ffd471 100644 (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;
     }
 
     /**
index bfda888..d0d1e58 100644 (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();
-
-        final ActivityInfo activityInfo = new ActivityInfo();
-        activityInfo.packageName = "blah";
-        activityInfo.name = "abc";
+    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 Tile tile2 = new Tile(activityInfo, "category");
-        assertThat(tile2.isIconTintable(RuntimeEnvironment.application)).isTrue();
+        assertThat(tile.isIconTintable(RuntimeEnvironment.application)).isFalse();
     }
 
     @Test
index c892711..aa1ac4e 100644 (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