Merge "Create new context with new resources"

This commit is contained in:
Jordan Liu
2020-02-03 21:39:30 +00:00
committed by Gerrit Code Review

View File

@@ -56,7 +56,6 @@ import android.os.ServiceManager;
import android.provider.Telephony.SimInfo; import android.provider.Telephony.SimInfo;
import android.telephony.euicc.EuiccManager; import android.telephony.euicc.EuiccManager;
import android.telephony.ims.ImsMmTelManager; import android.telephony.ims.ImsMmTelManager;
import android.util.DisplayMetrics;
import android.util.Log; import android.util.Log;
import android.util.Pair; import android.util.Pair;
@@ -2374,23 +2373,24 @@ public class SubscriptionManager {
final SubscriptionInfo subInfo = final SubscriptionInfo subInfo =
SubscriptionManager.from(context).getActiveSubscriptionInfo(subId); SubscriptionManager.from(context).getActiveSubscriptionInfo(subId);
Configuration config = context.getResources().getConfiguration(); Configuration overrideConfig = new Configuration();
Configuration newConfig = new Configuration();
newConfig.setTo(config);
if (subInfo != null) { if (subInfo != null) {
newConfig.mcc = subInfo.getMcc(); overrideConfig.mcc = subInfo.getMcc();
newConfig.mnc = subInfo.getMnc(); overrideConfig.mnc = subInfo.getMnc();
if (newConfig.mnc == 0) newConfig.mnc = Configuration.MNC_ZERO; if (overrideConfig.mnc == 0) overrideConfig.mnc = Configuration.MNC_ZERO;
} }
if (useRootLocale) { if (useRootLocale) {
newConfig.setLocale(Locale.ROOT); overrideConfig.setLocale(Locale.ROOT);
} }
DisplayMetrics metrics = context.getResources().getDisplayMetrics(); // Create new context with new configuration so that we can avoid modifying the passed in
DisplayMetrics newMetrics = new DisplayMetrics(); // context.
newMetrics.setTo(metrics); // Note that if the original context configuration changes, the resources here will also
Resources res = new Resources(context.getResources().getAssets(), newMetrics, newConfig); // 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) { if (cacheKey != null) {
// Save the newly created Resources in the resource cache. // Save the newly created Resources in the resource cache.