From 857ba4af8519479298a671461cd6f5cace35b99b Mon Sep 17 00:00:00 2001 From: Deepanshu Gupta Date: Fri, 14 Feb 2014 15:21:53 -0800 Subject: [PATCH] Fix layoutlib by avoiding Java7 calls This change also fixes a NPE in TypedArray caused by changes in the framework. Change-Id: Ie64237c567fa16c35c807db22ce9a4b609ee411d --- core/java/android/content/res/Resources.java | 8 +++- .../android/content/res/BridgeTypedArray.java | 2 +- .../content/res/Resources_Delegate.java | 37 +++++++++++++++++++ .../tools/layoutlib/create/CreateInfo.java | 1 + 4 files changed, 46 insertions(+), 2 deletions(-) create mode 100644 tools/layoutlib/bridge/src/android/content/res/Resources_Delegate.java diff --git a/core/java/android/content/res/Resources.java b/core/java/android/content/res/Resources.java index de00f713ef8c9..da6ae56d0c606 100644 --- a/core/java/android/content/res/Resources.java +++ b/core/java/android/content/res/Resources.java @@ -1625,7 +1625,7 @@ public class Resources { String locale = null; if (mConfiguration.locale != null) { - locale = mConfiguration.locale.toLanguageTag(); + locale = localeToLanguageTag(mConfiguration.locale); } int width, height; if (mMetrics.widthPixels >= mMetrics.heightPixels) { @@ -1706,6 +1706,12 @@ public class Resources { } } + // Locale.toLanguageTag() is not available in Java6. LayoutLib overrides + // this method to enable users to use Java6. + private String localeToLanguageTag(Locale locale) { + return locale.toLanguageTag(); + } + /** * Update the system resources configuration if they have previously * been initialized. diff --git a/tools/layoutlib/bridge/src/android/content/res/BridgeTypedArray.java b/tools/layoutlib/bridge/src/android/content/res/BridgeTypedArray.java index 446d139b21615..cfe7525ead3a3 100644 --- a/tools/layoutlib/bridge/src/android/content/res/BridgeTypedArray.java +++ b/tools/layoutlib/bridge/src/android/content/res/BridgeTypedArray.java @@ -58,7 +58,7 @@ public final class BridgeTypedArray extends TypedArray { public BridgeTypedArray(BridgeResources resources, BridgeContext context, int len, boolean platformFile) { - super(null, null, null, 0); + super(resources, null, null, 0); mBridgeResources = resources; mContext = context; mPlatformFile = platformFile; diff --git a/tools/layoutlib/bridge/src/android/content/res/Resources_Delegate.java b/tools/layoutlib/bridge/src/android/content/res/Resources_Delegate.java new file mode 100644 index 0000000000000..112250d01d4de --- /dev/null +++ b/tools/layoutlib/bridge/src/android/content/res/Resources_Delegate.java @@ -0,0 +1,37 @@ +/* + * Copyright (C) 2014 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.content.res; + +import java.util.Locale; + +import com.android.tools.layoutlib.annotations.LayoutlibDelegate; +import com.ibm.icu.util.ULocale; + +/** + * Delegate used to provide new implementation of a select few methods of {@link Resources} + * + * Through the layoutlib_create tool, the original methods of Resources have been replaced + * by calls to methods of the same name in this delegate class. + * + */ +public class Resources_Delegate { + + @LayoutlibDelegate + /*package*/ static String localeToLanguageTag(Resources res, Locale locale) { + return ULocale.forLocale(locale).toLanguageTag(); + } +} diff --git a/tools/layoutlib/create/src/com/android/tools/layoutlib/create/CreateInfo.java b/tools/layoutlib/create/src/com/android/tools/layoutlib/create/CreateInfo.java index 79aa642b7024c..42360388f53b7 100644 --- a/tools/layoutlib/create/src/com/android/tools/layoutlib/create/CreateInfo.java +++ b/tools/layoutlib/create/src/com/android/tools/layoutlib/create/CreateInfo.java @@ -125,6 +125,7 @@ public final class CreateInfo implements ICreateInfo { "android.app.Fragment#instantiate", //(Landroid/content/Context;Ljava/lang/String;Landroid/os/Bundle;)Landroid/app/Fragment;", "android.content.res.Resources$Theme#obtainStyledAttributes", "android.content.res.Resources$Theme#resolveAttribute", + "android.content.res.Resources#localeToLanguageTag", "android.content.res.TypedArray#getValueAt", "android.graphics.BitmapFactory#finishDecode", "android.os.Handler#sendMessageAtTime",