Merge "Work around a framework bug setting LayoutInflater.Factory2" into lmp-mr1-ub-dev
diff --git a/v4/api21/android/support/v4/view/LayoutInflaterCompatLollipop.java b/v4/api21/android/support/v4/view/LayoutInflaterCompatLollipop.java
new file mode 100644
index 0000000..b62c74e
--- /dev/null
+++ b/v4/api21/android/support/v4/view/LayoutInflaterCompatLollipop.java
@@ -0,0 +1,27 @@
+/*
+ * Copyright (C) 2015 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.support.v4.view;
+
+import android.view.LayoutInflater;
+
+class LayoutInflaterCompatLollipop {
+    static void setFactory(LayoutInflater inflater, LayoutInflaterFactory factory) {
+        inflater.setFactory2(factory != null
+                ? new LayoutInflaterCompatHC.FactoryWrapperHC(factory) : null);
+    }
+}
diff --git a/v4/donut/android/support/v4/view/LayoutInflaterCompatBase.java b/v4/donut/android/support/v4/view/LayoutInflaterCompatBase.java
index 8f210d3..ea19333 100644
--- a/v4/donut/android/support/v4/view/LayoutInflaterCompatBase.java
+++ b/v4/donut/android/support/v4/view/LayoutInflaterCompatBase.java
@@ -35,6 +35,10 @@
         public View onCreateView(String name, Context context, AttributeSet attrs) {
             return mDelegateFactory.onCreateView(null, name, context, attrs);
         }
+
+        public String toString() {
+            return getClass().getName() + "{" + mDelegateFactory + "}";
+        }
     }
 
     static void setFactory(LayoutInflater inflater, LayoutInflaterFactory factory) {
diff --git a/v4/honeycomb/android/support/v4/view/LayoutInflaterCompatHC.java b/v4/honeycomb/android/support/v4/view/LayoutInflaterCompatHC.java
index b57731c..ee1a413 100644
--- a/v4/honeycomb/android/support/v4/view/LayoutInflaterCompatHC.java
+++ b/v4/honeycomb/android/support/v4/view/LayoutInflaterCompatHC.java
@@ -18,10 +18,18 @@
 
 import android.content.Context;
 import android.util.AttributeSet;
+import android.util.Log;
 import android.view.LayoutInflater;
 import android.view.View;
 
+import java.lang.reflect.Field;
+import java.util.ArrayList;
+
 class LayoutInflaterCompatHC {
+    private static final String TAG = "LayoutInflaterCompatHC";
+
+    private static Field sLayoutInflaterFactory2Field;
+    private static boolean sCheckedField;
 
     static class FactoryWrapperHC extends LayoutInflaterCompatBase.FactoryWrapper
             implements LayoutInflater.Factory2 {
@@ -38,7 +46,37 @@
     }
 
     static void setFactory(LayoutInflater inflater, LayoutInflaterFactory factory) {
-        inflater.setFactory2(factory != null ? new FactoryWrapperHC(factory) : null);
+        final LayoutInflater.Factory2 factory2 = factory != null
+                ? new FactoryWrapperHC(factory) : null;
+        inflater.setFactory2(factory2);
+        forceSetFactory2(inflater, factory2);
     }
 
+    /**
+     * For APIs >= 11 && < 21, there was a framework bug that prevented a LayoutInflater's
+     * Factory2 from being merged properly if set after a cloneInContext from a LayoutInflater
+     * that already had a Factory2 registered. We work around that bug here. If we can't we
+     * log an error.
+     */
+    static void forceSetFactory2(LayoutInflater inflater, LayoutInflater.Factory2 factory) {
+        if (!sCheckedField) {
+            try {
+                sLayoutInflaterFactory2Field = LayoutInflater.class.getDeclaredField("mFactory2");
+                sLayoutInflaterFactory2Field.setAccessible(true);
+            } catch (NoSuchFieldException e) {
+                Log.e(TAG, "forceSetFactory2 Could not find field 'mFactory2' on class "
+                        + LayoutInflater.class.getName()
+                        + "; inflation may have unexpected results.", e);
+            }
+            sCheckedField = true;
+        }
+        if (sLayoutInflaterFactory2Field != null) {
+            try {
+                sLayoutInflaterFactory2Field.set(inflater, factory);
+            } catch (IllegalAccessException e) {
+                Log.e(TAG, "forceSetFactory2 could not set the Factory2 on LayoutInflater "
+                        + inflater + "; inflation may have unexpected results.", e);
+            }
+        }
+    }
 }
diff --git a/v4/java/android/support/v4/view/LayoutInflaterCompat.java b/v4/java/android/support/v4/view/LayoutInflaterCompat.java
index 56ed8f3..d67a739 100644
--- a/v4/java/android/support/v4/view/LayoutInflaterCompat.java
+++ b/v4/java/android/support/v4/view/LayoutInflaterCompat.java
@@ -43,10 +43,19 @@
         }
     }
 
+    static class LayoutInflaterCompatImplV21 extends LayoutInflaterCompatImplV11 {
+        @Override
+        public void setFactory(LayoutInflater layoutInflater, LayoutInflaterFactory factory) {
+            LayoutInflaterCompatLollipop.setFactory(layoutInflater, factory);
+        }
+    }
+
     static final LayoutInflaterCompatImpl IMPL;
     static {
         final int version = Build.VERSION.SDK_INT;
-        if (version >= 11) {
+        if (version >= 21) {
+            IMPL = new LayoutInflaterCompatImplV21();
+        } else if (version >= 11) {
             IMPL = new LayoutInflaterCompatImplV11();
         } else {
             IMPL = new LayoutInflaterCompatImplBase();