From 82c7fdb1f6346862de373c95c618e370f81d8df6 Mon Sep 17 00:00:00 2001 From: Deepanshu Gupta Date: Tue, 4 Aug 2015 16:17:46 -0700 Subject: [PATCH] Replace Locale.getDefault with custom impl. In LayoutLib the default locale should always be the locale set the rendering params. This change replaces all calls to Locale.getDefault in the framework with calls to AndroidLocale.getDefault() which tries to find the locale from the current context, but falls back to the original call. Change-Id: I496b35dcfc17fd61fedee21c7495541ab870b1fc --- .../layoutlib/bridge/android/AndroidLocale.java | 13 +++++++++++++ .../android/layoutlib/bridge/impl/RenderAction.java | 3 +++ .../layoutlib/create/ReplaceMethodCallsAdapter.java | 8 ++++++-- 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/android/AndroidLocale.java b/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/android/AndroidLocale.java index e589d9e70b8a1..faaf1056bc9d3 100644 --- a/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/android/AndroidLocale.java +++ b/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/android/AndroidLocale.java @@ -16,6 +16,8 @@ package com.android.layoutlib.bridge.android; +import com.android.layoutlib.bridge.impl.RenderAction; + import android.icu.util.ULocale; import java.util.Locale; @@ -56,4 +58,15 @@ public class AndroidLocale { public static String getScript(Locale locale) { return ULocale.forLocale(locale).getScript(); } + + public static Locale getDefault() { + BridgeContext context = RenderAction.getCurrentContext(); + if (context != null) { + Locale locale = context.getConfiguration().locale; + if (locale != null) { + return locale; + } + } + return Locale.getDefault(); + } } diff --git a/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/impl/RenderAction.java b/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/impl/RenderAction.java index 9380ca798ee54..a833ebe8015eb 100644 --- a/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/impl/RenderAction.java +++ b/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/impl/RenderAction.java @@ -37,6 +37,7 @@ import android.view.ViewConfiguration_Accessor; import android.view.inputmethod.InputMethodManager; import android.view.inputmethod.InputMethodManager_Accessor; +import java.util.Locale; import java.util.concurrent.TimeUnit; import java.util.concurrent.locks.ReentrantLock; @@ -397,6 +398,8 @@ public abstract class RenderAction extends FrameworkReso // preview releases of API 15. // TODO: Remove the try catch around Oct 2015. } + String locale = getParams().getLocale(); + if (locale != null && !locale.isEmpty()) config.locale = new Locale(locale); // TODO: fill in more config info. diff --git a/tools/layoutlib/create/src/com/android/tools/layoutlib/create/ReplaceMethodCallsAdapter.java b/tools/layoutlib/create/src/com/android/tools/layoutlib/create/ReplaceMethodCallsAdapter.java index 43691482dc66c..0b85c48b4e681 100644 --- a/tools/layoutlib/create/src/com/android/tools/layoutlib/create/ReplaceMethodCallsAdapter.java +++ b/tools/layoutlib/create/src/com/android/tools/layoutlib/create/ReplaceMethodCallsAdapter.java @@ -93,18 +93,22 @@ public class ReplaceMethodCallsAdapter extends ClassVisitor { } }); - // Case 3: java.util.Locale.adjustLanguageCode() or java.util.Locale.forLanguageTag() + // Case 3: java.util.Locale.adjustLanguageCode() or java.util.Locale.forLanguageTag() or + // java.util.Locale.getDefault() METHOD_REPLACERS.add(new MethodReplacer() { private final String STRING_TO_STRING = Type.getMethodDescriptor(STRING, STRING); private final String STRING_TO_LOCALE = Type.getMethodDescriptor( Type.getType(Locale.class), STRING); + private final String VOID_TO_LOCALE = + Type.getMethodDescriptor(Type.getType(Locale.class)); @Override public boolean isNeeded(String owner, String name, String desc, String sourceClass) { return JAVA_LOCALE_CLASS.equals(owner) && ("adjustLanguageCode".equals(name) && desc.equals(STRING_TO_STRING) || - "forLanguageTag".equals(name) && desc.equals(STRING_TO_LOCALE)); + "forLanguageTag".equals(name) && desc.equals(STRING_TO_LOCALE) || + "getDefault".equals(name) && desc.equals(VOID_TO_LOCALE)); } @Override