Consider res package when updating security icons.

Bug: 31002801
Test: make RunSettingsRoboTests
Change-Id: Icef83cbe18d4b48c4091bdf421018bc817bcf33c
This commit is contained in:
Shahriyar Amini
2017-01-18 19:00:56 -08:00
parent 54175aaaba
commit 31357bb9a8
2 changed files with 15 additions and 27 deletions

View File

@@ -28,6 +28,7 @@ import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceScreen; import android.support.v7.preference.PreferenceScreen;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.ArrayMap; import android.util.ArrayMap;
import android.util.Pair;
import com.android.settingslib.drawer.Tile; import com.android.settingslib.drawer.Tile;
import com.android.settingslib.drawer.TileUtils; import com.android.settingslib.drawer.TileUtils;
@@ -64,37 +65,23 @@ public class SecurityFeatureProviderImpl implements SecurityFeatureProvider {
String summaryUri = String summaryUri =
tile.metaData.getString(TileUtils.META_DATA_PREFERENCE_SUMMARY_URI, null); tile.metaData.getString(TileUtils.META_DATA_PREFERENCE_SUMMARY_URI, null);
if (!TextUtils.isEmpty(iconUri)) { if (!TextUtils.isEmpty(iconUri)) {
int icon = TileUtils.getIconFromUri(context, iconUri, providerMap);
boolean updateIcon = true;
String packageName = null; String packageName = null;
// Dynamic icon has to come from the same package that the preference launches.
if (tile.intent != null) { if (tile.intent != null) {
Intent intent = tile.intent; Intent intent = tile.intent;
if (!TextUtils.isEmpty(intent.getPackage())) { if (!TextUtils.isEmpty(intent.getPackage())) {
packageName = intent.getPackage(); packageName = intent.getPackage();
} else if (intent.getComponent() != null) { } else if (intent.getComponent() != null) {
packageName = intent.getComponent().getPackageName(); packageName = intent.getComponent().getPackageName();
}
}
if (TextUtils.isEmpty(packageName)) {
updateIcon = false;
} else {
if (tile.icon == null) {
// If the tile does not have an icon already, only update if the suggested
// icon is non-zero.
updateIcon = (icon != 0);
} else {
// If the existing icon has the same resource package and resource id, the
// icon does not need to be updated.
updateIcon = !(packageName.equals(tile.icon.getResPackage())
&& (icon == tile.icon.getResId()));
} }
} }
if (updateIcon) { Pair<String, Integer> icon =
TileUtils.getIconFromUri(context, packageName, iconUri, providerMap);
if (icon != null) {
// Icon is only returned if the icon belongs to Settings or the target app.
try { try {
matchingPref.setIcon(context.getPackageManager() matchingPref.setIcon(context.getPackageManager()
.getResourcesForApplication(packageName) .getResourcesForApplication(icon.first /* package name */)
.getDrawable(icon, context.getTheme())); .getDrawable(icon.second /* res id */, context.getTheme()));
} catch (PackageManager.NameNotFoundException | Resources.NotFoundException e) { } catch (PackageManager.NameNotFoundException | Resources.NotFoundException e) {
// Intentionally ignored. If icon resources cannot be found, do not update. // Intentionally ignored. If icon resources cannot be found, do not update.
} }

View File

@@ -25,6 +25,7 @@ import android.graphics.drawable.Drawable;
import android.os.Bundle; import android.os.Bundle;
import android.support.v7.preference.Preference; import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceScreen; import android.support.v7.preference.PreferenceScreen;
import android.util.Pair;
import com.android.settings.SettingsRobolectricTestRunner; import com.android.settings.SettingsRobolectricTestRunner;
import com.android.settings.TestConfig; import com.android.settings.TestConfig;
@@ -77,9 +78,9 @@ public class SecurityFeatureProviderImplTest {
@Implements(com.android.settingslib.drawer.TileUtils.class) @Implements(com.android.settingslib.drawer.TileUtils.class)
public static class TileUtilsMock { public static class TileUtilsMock {
@Implementation @Implementation
public static int getIconFromUri(Context context, String uriString, public static Pair getIconFromUri(Context context, String packageName, String uriString,
Map<String, IContentProvider> providerMap) { Map<String, IContentProvider> providerMap) {
return 161803; return Pair.create("package", 161803);
} }
@Implementation @Implementation