1/* 2 * Copyright 2018 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package androidx.core.view; 18 19import android.content.Context; 20import android.os.Build; 21import android.util.AttributeSet; 22import android.util.Log; 23import android.view.LayoutInflater; 24import android.view.View; 25 26import androidx.annotation.NonNull; 27 28import java.lang.reflect.Field; 29 30/** 31 * Helper for accessing features in {@link LayoutInflater}. 32 */ 33public final class LayoutInflaterCompat { 34 private static final String TAG = "LayoutInflaterCompatHC"; 35 36 private static Field sLayoutInflaterFactory2Field; 37 private static boolean sCheckedField; 38 39 @SuppressWarnings("deprecation") 40 static class Factory2Wrapper implements LayoutInflater.Factory2 { 41 final LayoutInflaterFactory mDelegateFactory; 42 43 Factory2Wrapper(LayoutInflaterFactory delegateFactory) { 44 mDelegateFactory = delegateFactory; 45 } 46 47 @Override 48 public View onCreateView(String name, Context context, AttributeSet attrs) { 49 return mDelegateFactory.onCreateView(null, name, context, attrs); 50 } 51 52 @Override 53 public View onCreateView(View parent, String name, Context context, 54 AttributeSet attributeSet) { 55 return mDelegateFactory.onCreateView(parent, name, context, attributeSet); 56 } 57 58 @Override 59 public String toString() { 60 return getClass().getName() + "{" + mDelegateFactory + "}"; 61 } 62 } 63 64 /** 65 * For APIs < 21, there was a framework bug that prevented a LayoutInflater's 66 * Factory2 from being merged properly if set after a cloneInContext from a LayoutInflater 67 * that already had a Factory2 registered. We work around that bug here. If we can't we 68 * log an error. 69 */ 70 private static void forceSetFactory2(LayoutInflater inflater, LayoutInflater.Factory2 factory) { 71 if (!sCheckedField) { 72 try { 73 sLayoutInflaterFactory2Field = LayoutInflater.class.getDeclaredField("mFactory2"); 74 sLayoutInflaterFactory2Field.setAccessible(true); 75 } catch (NoSuchFieldException e) { 76 Log.e(TAG, "forceSetFactory2 Could not find field 'mFactory2' on class " 77 + LayoutInflater.class.getName() 78 + "; inflation may have unexpected results.", e); 79 } 80 sCheckedField = true; 81 } 82 if (sLayoutInflaterFactory2Field != null) { 83 try { 84 sLayoutInflaterFactory2Field.set(inflater, factory); 85 } catch (IllegalAccessException e) { 86 Log.e(TAG, "forceSetFactory2 could not set the Factory2 on LayoutInflater " 87 + inflater + "; inflation may have unexpected results.", e); 88 } 89 } 90 } 91 92 /* 93 * Hide the constructor. 94 */ 95 private LayoutInflaterCompat() { 96 } 97 98 /** 99 * Attach a custom Factory interface for creating views while using 100 * this LayoutInflater. This must not be null, and can only be set once; 101 * after setting, you can not change the factory. 102 * 103 * @see LayoutInflater#setFactory(android.view.LayoutInflater.Factory) 104 * 105 * @deprecated Use {@link #setFactory2(LayoutInflater, LayoutInflater.Factory2)} instead to set 106 * and {@link LayoutInflater#getFactory2()} to get the factory. 107 */ 108 @Deprecated 109 public static void setFactory( 110 @NonNull LayoutInflater inflater, @NonNull LayoutInflaterFactory factory) { 111 if (Build.VERSION.SDK_INT >= 21) { 112 inflater.setFactory2(factory != null ? new Factory2Wrapper(factory) : null); 113 } else { 114 final LayoutInflater.Factory2 factory2 = factory != null 115 ? new Factory2Wrapper(factory) : null; 116 inflater.setFactory2(factory2); 117 118 final LayoutInflater.Factory f = inflater.getFactory(); 119 if (f instanceof LayoutInflater.Factory2) { 120 // The merged factory is now set to getFactory(), but not getFactory2() (pre-v21). 121 // We will now try and force set the merged factory to mFactory2 122 forceSetFactory2(inflater, (LayoutInflater.Factory2) f); 123 } else { 124 // Else, we will force set the original wrapped Factory2 125 forceSetFactory2(inflater, factory2); 126 } 127 } 128 } 129 130 /** 131 * Attach a custom {@link LayoutInflater.Factory2} for creating views while using 132 * this {@link LayoutInflater}. This must not be null, and can only be set once; 133 * after setting, you can not change the factory. 134 * 135 * @see LayoutInflater#setFactory2(android.view.LayoutInflater.Factory2) 136 */ 137 public static void setFactory2( 138 @NonNull LayoutInflater inflater, @NonNull LayoutInflater.Factory2 factory) { 139 inflater.setFactory2(factory); 140 141 if (Build.VERSION.SDK_INT < 21) { 142 final LayoutInflater.Factory f = inflater.getFactory(); 143 if (f instanceof LayoutInflater.Factory2) { 144 // The merged factory is now set to getFactory(), but not getFactory2() (pre-v21). 145 // We will now try and force set the merged factory to mFactory2 146 forceSetFactory2(inflater, (LayoutInflater.Factory2) f); 147 } else { 148 // Else, we will force set the original wrapped Factory2 149 forceSetFactory2(inflater, factory); 150 } 151 } 152 } 153 154 /** 155 * Return the current {@link LayoutInflaterFactory} (or null). This is 156 * called on each element name. If the factory returns a View, add that 157 * to the hierarchy. If it returns null, proceed to call onCreateView(name). 158 * 159 * @return The {@link LayoutInflaterFactory} associated with the 160 * {@link LayoutInflater}. Will be {@code null} if the inflater does not 161 * have a {@link LayoutInflaterFactory} but a raw {@link LayoutInflater.Factory}. 162 * @see LayoutInflater#getFactory() 163 * 164 * @deprecated Use {@link #setFactory2(LayoutInflater, LayoutInflater.Factory2)} to set and 165 * {@link LayoutInflater#getFactory2()} to get the factory. 166 */ 167 @Deprecated 168 public static LayoutInflaterFactory getFactory(LayoutInflater inflater) { 169 LayoutInflater.Factory factory = inflater.getFactory(); 170 if (factory instanceof Factory2Wrapper) { 171 return ((Factory2Wrapper) factory).mDelegateFactory; 172 } 173 return null; 174 } 175} 176