From 0e161088fdfdb830483f506cf22e03b0d73a8bb4 Mon Sep 17 00:00:00 2001 From: Jordan Liu Date: Tue, 21 Jan 2020 10:55:08 -0800 Subject: [PATCH] Create new context with new resources The old implementation of this function had the unintended side effect of modifying the default configuration for the context which the caller passed in. This CL avoids that by creating a new context with the new configuration. Test: atest SubscriptionControllerTest#testGetResourcesForSubId Bug: 146481715 Change-Id: I6539817caa67e1100e50018fa0186993a1f56ccb Merged-In: I6539817caa67e1100e50018fa0186993a1f56ccb --- .../telephony/SubscriptionManager.java | 27 +++++++++---------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/telephony/java/android/telephony/SubscriptionManager.java b/telephony/java/android/telephony/SubscriptionManager.java index d4ab04cc21703..ff032101eb645 100644 --- a/telephony/java/android/telephony/SubscriptionManager.java +++ b/telephony/java/android/telephony/SubscriptionManager.java @@ -16,8 +16,6 @@ package android.telephony; -import com.android.telephony.Rlog; - import static android.net.NetworkPolicyManager.OVERRIDE_CONGESTED; import static android.net.NetworkPolicyManager.OVERRIDE_UNMETERED; @@ -58,7 +56,6 @@ import android.os.ServiceManager; import android.provider.Telephony.SimInfo; import android.telephony.euicc.EuiccManager; import android.telephony.ims.ImsMmTelManager; -import android.util.DisplayMetrics; import android.util.Log; import android.util.Pair; @@ -67,6 +64,7 @@ import com.android.internal.telephony.ISub; import com.android.internal.telephony.PhoneConstants; import com.android.internal.telephony.util.HandlerExecutor; import com.android.internal.util.Preconditions; +import com.android.telephony.Rlog; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; @@ -2401,23 +2399,24 @@ public class SubscriptionManager { final SubscriptionInfo subInfo = SubscriptionManager.from(context).getActiveSubscriptionInfo(subId); - Configuration config = context.getResources().getConfiguration(); - Configuration newConfig = new Configuration(); - newConfig.setTo(config); + Configuration overrideConfig = new Configuration(); if (subInfo != null) { - newConfig.mcc = subInfo.getMcc(); - newConfig.mnc = subInfo.getMnc(); - if (newConfig.mnc == 0) newConfig.mnc = Configuration.MNC_ZERO; + overrideConfig.mcc = subInfo.getMcc(); + overrideConfig.mnc = subInfo.getMnc(); + if (overrideConfig.mnc == 0) overrideConfig.mnc = Configuration.MNC_ZERO; } if (useRootLocale) { - newConfig.setLocale(Locale.ROOT); + overrideConfig.setLocale(Locale.ROOT); } - DisplayMetrics metrics = context.getResources().getDisplayMetrics(); - DisplayMetrics newMetrics = new DisplayMetrics(); - newMetrics.setTo(metrics); - Resources res = new Resources(context.getResources().getAssets(), newMetrics, newConfig); + // Create new context with new configuration so that we can avoid modifying the passed in + // context. + // Note that if the original context configuration changes, the resources here will also + // change for all values except those overridden by newConfig (e.g. if the device has an + // orientation change). + Context newContext = context.createConfigurationContext(overrideConfig); + Resources res = newContext.getResources(); if (cacheKey != null) { // Save the newly created Resources in the resource cache.