The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 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 | |
| 17 | package android.view; |
| 18 | |
Tor Norbye | 7b9c912 | 2013-05-30 16:48:33 -0700 | [diff] [blame] | 19 | import android.annotation.LayoutRes; |
Ian Lake | 0c807f4 | 2018-09-12 14:03:27 -0700 | [diff] [blame] | 20 | import android.annotation.NonNull; |
Alan Viverette | e8489cd | 2015-02-03 14:40:45 -0800 | [diff] [blame] | 21 | import android.annotation.Nullable; |
Jeff Sharkey | d86b8fe | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 22 | import android.annotation.SystemService; |
Eric Holk | 25aa4ed | 2019-01-18 11:49:55 -0800 | [diff] [blame] | 23 | import android.annotation.TestApi; |
Mathew Inwood | a570dee | 2018-08-17 14:56:00 +0100 | [diff] [blame] | 24 | import android.annotation.UnsupportedAppUsage; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 25 | import android.content.Context; |
Eric Holk | 987c7a9 | 2019-01-17 10:51:17 -0800 | [diff] [blame] | 26 | import android.content.pm.ApplicationInfo; |
Alan Viverette | 0810b63 | 2014-05-01 14:42:56 -0700 | [diff] [blame] | 27 | import android.content.res.Resources; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 28 | import android.content.res.TypedArray; |
| 29 | import android.content.res.XmlResourceParser; |
Alan Viverette | e8489cd | 2015-02-03 14:40:45 -0800 | [diff] [blame] | 30 | import android.graphics.Canvas; |
Cătălin Tudor | 0c0e82a | 2018-10-18 18:32:49 +0100 | [diff] [blame] | 31 | import android.os.Build; |
Alan Viverette | e8489cd | 2015-02-03 14:40:45 -0800 | [diff] [blame] | 32 | import android.os.Handler; |
| 33 | import android.os.Message; |
Eric Holk | 928bbac | 2019-01-02 10:38:54 -0800 | [diff] [blame] | 34 | import android.os.SystemProperties; |
Alan Viverette | e8489cd | 2015-02-03 14:40:45 -0800 | [diff] [blame] | 35 | import android.os.Trace; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 36 | import android.util.AttributeSet; |
Alan Viverette | 0810b63 | 2014-05-01 14:42:56 -0700 | [diff] [blame] | 37 | import android.util.Log; |
Alan Viverette | e8489cd | 2015-02-03 14:40:45 -0800 | [diff] [blame] | 38 | import android.util.TypedValue; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 39 | import android.util.Xml; |
Alan Viverette | e8489cd | 2015-02-03 14:40:45 -0800 | [diff] [blame] | 40 | import android.widget.FrameLayout; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 41 | |
Aurimas Liutikas | 67e2ae8 | 2016-10-11 18:17:42 -0700 | [diff] [blame] | 42 | import com.android.internal.R; |
| 43 | |
Eric Holk | 928bbac | 2019-01-02 10:38:54 -0800 | [diff] [blame] | 44 | import dalvik.system.PathClassLoader; |
| 45 | import java.io.File; |
| 46 | import java.lang.reflect.Method; |
Aurimas Liutikas | 67e2ae8 | 2016-10-11 18:17:42 -0700 | [diff] [blame] | 47 | import org.xmlpull.v1.XmlPullParser; |
| 48 | import org.xmlpull.v1.XmlPullParserException; |
| 49 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 50 | import java.io.IOException; |
| 51 | import java.lang.reflect.Constructor; |
| 52 | import java.util.HashMap; |
| 53 | |
| 54 | /** |
Scott Main | 93dc642 | 2012-02-24 12:04:06 -0800 | [diff] [blame] | 55 | * Instantiates a layout XML file into its corresponding {@link android.view.View} |
| 56 | * objects. It is never used directly. Instead, use |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 57 | * {@link android.app.Activity#getLayoutInflater()} or |
| 58 | * {@link Context#getSystemService} to retrieve a standard LayoutInflater instance |
| 59 | * that is already hooked up to the current context and correctly configured |
Jeff Sharkey | d86b8fe | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 60 | * for the device you are running on. |
Aurimas Liutikas | 67e2ae8 | 2016-10-11 18:17:42 -0700 | [diff] [blame] | 61 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 62 | * <p> |
| 63 | * To create a new LayoutInflater with an additional {@link Factory} for your |
| 64 | * own views, you can use {@link #cloneInContext} to clone an existing |
| 65 | * ViewFactory, and then call {@link #setFactory} on it to include your |
| 66 | * Factory. |
Aurimas Liutikas | 67e2ae8 | 2016-10-11 18:17:42 -0700 | [diff] [blame] | 67 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 68 | * <p> |
| 69 | * For performance reasons, view inflation relies heavily on pre-processing of |
| 70 | * XML files that is done at build time. Therefore, it is not currently possible |
| 71 | * to use LayoutInflater with an XmlPullParser over a plain XML file at runtime; |
| 72 | * it only works with an XmlPullParser returned from a compiled resource |
| 73 | * (R.<em>something</em> file.) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 74 | */ |
Jeff Sharkey | d86b8fe | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 75 | @SystemService(Context.LAYOUT_INFLATER_SERVICE) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 76 | public abstract class LayoutInflater { |
Alan Viverette | 33e3cda | 2014-12-17 15:43:29 -0800 | [diff] [blame] | 77 | |
Alan Viverette | 0810b63 | 2014-05-01 14:42:56 -0700 | [diff] [blame] | 78 | private static final String TAG = LayoutInflater.class.getSimpleName(); |
| 79 | private static final boolean DEBUG = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 80 | |
Eric Holk | 928bbac | 2019-01-02 10:38:54 -0800 | [diff] [blame] | 81 | private static final String USE_PRECOMPILED_LAYOUT_SYSTEM_PROPERTY |
| 82 | = "view.precompiled_layout_enabled"; |
| 83 | private static final String COMPILED_VIEW_DEX_FILE_NAME = "/compiled_view.dex"; |
| 84 | |
Alan Viverette | 8f12481 | 2015-09-25 15:17:05 -0400 | [diff] [blame] | 85 | /** Empty stack trace used to avoid log spam in re-throw exceptions. */ |
| 86 | private static final StackTraceElement[] EMPTY_STACK_TRACE = new StackTraceElement[0]; |
| 87 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 88 | /** |
| 89 | * This field should be made private, so it is hidden from the SDK. |
| 90 | * {@hide} |
| 91 | */ |
Cătălin Tudor | 0c0e82a | 2018-10-18 18:32:49 +0100 | [diff] [blame] | 92 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 93 | protected final Context mContext; |
| 94 | |
| 95 | // these are optional, set by the caller |
Mathew Inwood | a570dee | 2018-08-17 14:56:00 +0100 | [diff] [blame] | 96 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 97 | private boolean mFactorySet; |
Mathew Inwood | a570dee | 2018-08-17 14:56:00 +0100 | [diff] [blame] | 98 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 99 | private Factory mFactory; |
Mathew Inwood | a570dee | 2018-08-17 14:56:00 +0100 | [diff] [blame] | 100 | @UnsupportedAppUsage |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 101 | private Factory2 mFactory2; |
Mathew Inwood | a570dee | 2018-08-17 14:56:00 +0100 | [diff] [blame] | 102 | @UnsupportedAppUsage |
Dianne Hackborn | 420829e | 2011-01-28 11:30:35 -0800 | [diff] [blame] | 103 | private Factory2 mPrivateFactory; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 104 | private Filter mFilter; |
| 105 | |
Eric Holk | 928bbac | 2019-01-02 10:38:54 -0800 | [diff] [blame] | 106 | // Indicates whether we should try to inflate layouts using a precompiled layout instead of |
| 107 | // inflating from the XML resource. |
| 108 | private boolean mUseCompiledView; |
| 109 | // This variable holds the classloader that will be used to look for precompiled layouts. The |
| 110 | // The classloader includes the generated compiled_view.dex file. |
| 111 | private ClassLoader mPrecompiledClassLoader; |
| 112 | |
Mathew Inwood | a570dee | 2018-08-17 14:56:00 +0100 | [diff] [blame] | 113 | @UnsupportedAppUsage |
Xavier Ducrohet | 7f9f99ea | 2011-08-11 10:16:17 -0700 | [diff] [blame] | 114 | final Object[] mConstructorArgs = new Object[2]; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 115 | |
Mathew Inwood | a570dee | 2018-08-17 14:56:00 +0100 | [diff] [blame] | 116 | @UnsupportedAppUsage |
Xavier Ducrohet | 7f9f99ea | 2011-08-11 10:16:17 -0700 | [diff] [blame] | 117 | static final Class<?>[] mConstructorSignature = new Class[] { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 118 | Context.class, AttributeSet.class}; |
| 119 | |
Mathew Inwood | a570dee | 2018-08-17 14:56:00 +0100 | [diff] [blame] | 120 | @UnsupportedAppUsage |
Gilles Debunne | 3030193 | 2010-06-16 18:32:00 -0700 | [diff] [blame] | 121 | private static final HashMap<String, Constructor<? extends View>> sConstructorMap = |
| 122 | new HashMap<String, Constructor<? extends View>>(); |
Aurimas Liutikas | 67e2ae8 | 2016-10-11 18:17:42 -0700 | [diff] [blame] | 123 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 124 | private HashMap<String, Boolean> mFilterMap; |
| 125 | |
Alan Viverette | 33e3cda | 2014-12-17 15:43:29 -0800 | [diff] [blame] | 126 | private TypedValue mTempValue; |
| 127 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 128 | private static final String TAG_MERGE = "merge"; |
| 129 | private static final String TAG_INCLUDE = "include"; |
Romain Guy | 9c1223a | 2011-05-17 14:25:49 -0700 | [diff] [blame] | 130 | private static final String TAG_1995 = "blink"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 131 | private static final String TAG_REQUEST_FOCUS = "requestFocus"; |
Alan Viverette | 451a341 | 2014-02-11 18:08:46 -0800 | [diff] [blame] | 132 | private static final String TAG_TAG = "tag"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 133 | |
Alan Viverette | 33e3cda | 2014-12-17 15:43:29 -0800 | [diff] [blame] | 134 | private static final String ATTR_LAYOUT = "layout"; |
| 135 | |
Mathew Inwood | a570dee | 2018-08-17 14:56:00 +0100 | [diff] [blame] | 136 | @UnsupportedAppUsage |
Alan Viverette | ef259e4 | 2014-01-24 17:20:12 -0800 | [diff] [blame] | 137 | private static final int[] ATTRS_THEME = new int[] { |
| 138 | com.android.internal.R.attr.theme }; |
Alan Viverette | 24927f2 | 2014-01-07 17:28:48 -0800 | [diff] [blame] | 139 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 140 | /** |
| 141 | * Hook to allow clients of the LayoutInflater to restrict the set of Views that are allowed |
| 142 | * to be inflated. |
Aurimas Liutikas | 67e2ae8 | 2016-10-11 18:17:42 -0700 | [diff] [blame] | 143 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 144 | */ |
| 145 | public interface Filter { |
| 146 | /** |
Aurimas Liutikas | 67e2ae8 | 2016-10-11 18:17:42 -0700 | [diff] [blame] | 147 | * Hook to allow clients of the LayoutInflater to restrict the set of Views |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 148 | * that are allowed to be inflated. |
Aurimas Liutikas | 67e2ae8 | 2016-10-11 18:17:42 -0700 | [diff] [blame] | 149 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 150 | * @param clazz The class object for the View that is about to be inflated |
Aurimas Liutikas | 67e2ae8 | 2016-10-11 18:17:42 -0700 | [diff] [blame] | 151 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 152 | * @return True if this class is allowed to be inflated, or false otherwise |
| 153 | */ |
Gilles Debunne | e6ac8b9 | 2010-06-17 10:55:04 -0700 | [diff] [blame] | 154 | @SuppressWarnings("unchecked") |
| 155 | boolean onLoadClass(Class clazz); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 156 | } |
Aurimas Liutikas | 67e2ae8 | 2016-10-11 18:17:42 -0700 | [diff] [blame] | 157 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 158 | public interface Factory { |
| 159 | /** |
| 160 | * Hook you can supply that is called when inflating from a LayoutInflater. |
| 161 | * You can use this to customize the tag names available in your XML |
| 162 | * layout files. |
Aurimas Liutikas | 67e2ae8 | 2016-10-11 18:17:42 -0700 | [diff] [blame] | 163 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 164 | * <p> |
| 165 | * Note that it is good practice to prefix these custom names with your |
| 166 | * package (i.e., com.coolcompany.apps) to avoid conflicts with system |
| 167 | * names. |
Aurimas Liutikas | 67e2ae8 | 2016-10-11 18:17:42 -0700 | [diff] [blame] | 168 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 169 | * @param name Tag name to be inflated. |
| 170 | * @param context The context the view is being created in. |
| 171 | * @param attrs Inflation attributes as specified in XML file. |
Aurimas Liutikas | 67e2ae8 | 2016-10-11 18:17:42 -0700 | [diff] [blame] | 172 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 173 | * @return View Newly created view. Return null for the default |
| 174 | * behavior. |
| 175 | */ |
Ian Lake | 0c807f4 | 2018-09-12 14:03:27 -0700 | [diff] [blame] | 176 | @Nullable |
| 177 | View onCreateView(@NonNull String name, @NonNull Context context, |
| 178 | @NonNull AttributeSet attrs); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 179 | } |
| 180 | |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 181 | public interface Factory2 extends Factory { |
| 182 | /** |
| 183 | * Version of {@link #onCreateView(String, Context, AttributeSet)} |
| 184 | * that also supplies the parent that the view created view will be |
| 185 | * placed in. |
| 186 | * |
| 187 | * @param parent The parent that the created view will be placed |
| 188 | * in; <em>note that this may be null</em>. |
| 189 | * @param name Tag name to be inflated. |
| 190 | * @param context The context the view is being created in. |
| 191 | * @param attrs Inflation attributes as specified in XML file. |
| 192 | * |
| 193 | * @return View Newly created view. Return null for the default |
| 194 | * behavior. |
| 195 | */ |
Ian Lake | 0c807f4 | 2018-09-12 14:03:27 -0700 | [diff] [blame] | 196 | @Nullable |
| 197 | View onCreateView(@Nullable View parent, @NonNull String name, |
| 198 | @NonNull Context context, @NonNull AttributeSet attrs); |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | private static class FactoryMerger implements Factory2 { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 202 | private final Factory mF1, mF2; |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 203 | private final Factory2 mF12, mF22; |
Aurimas Liutikas | 67e2ae8 | 2016-10-11 18:17:42 -0700 | [diff] [blame] | 204 | |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 205 | FactoryMerger(Factory f1, Factory2 f12, Factory f2, Factory2 f22) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 206 | mF1 = f1; |
| 207 | mF2 = f2; |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 208 | mF12 = f12; |
| 209 | mF22 = f22; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 210 | } |
Aurimas Liutikas | 67e2ae8 | 2016-10-11 18:17:42 -0700 | [diff] [blame] | 211 | |
Ian Lake | 0c807f4 | 2018-09-12 14:03:27 -0700 | [diff] [blame] | 212 | @Nullable |
| 213 | public View onCreateView(@NonNull String name, @NonNull Context context, |
| 214 | @NonNull AttributeSet attrs) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 215 | View v = mF1.onCreateView(name, context, attrs); |
| 216 | if (v != null) return v; |
| 217 | return mF2.onCreateView(name, context, attrs); |
| 218 | } |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 219 | |
Ian Lake | 0c807f4 | 2018-09-12 14:03:27 -0700 | [diff] [blame] | 220 | @Nullable |
| 221 | public View onCreateView(@Nullable View parent, @NonNull String name, |
| 222 | @NonNull Context context, @NonNull AttributeSet attrs) { |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 223 | View v = mF12 != null ? mF12.onCreateView(parent, name, context, attrs) |
| 224 | : mF1.onCreateView(name, context, attrs); |
| 225 | if (v != null) return v; |
| 226 | return mF22 != null ? mF22.onCreateView(parent, name, context, attrs) |
| 227 | : mF2.onCreateView(name, context, attrs); |
| 228 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 229 | } |
Aurimas Liutikas | 67e2ae8 | 2016-10-11 18:17:42 -0700 | [diff] [blame] | 230 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 231 | /** |
| 232 | * Create a new LayoutInflater instance associated with a particular Context. |
| 233 | * Applications will almost always want to use |
| 234 | * {@link Context#getSystemService Context.getSystemService()} to retrieve |
| 235 | * the standard {@link Context#LAYOUT_INFLATER_SERVICE Context.INFLATER_SERVICE}. |
Aurimas Liutikas | 67e2ae8 | 2016-10-11 18:17:42 -0700 | [diff] [blame] | 236 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 237 | * @param context The Context in which this LayoutInflater will create its |
| 238 | * Views; most importantly, this supplies the theme from which the default |
| 239 | * values for their attributes are retrieved. |
| 240 | */ |
| 241 | protected LayoutInflater(Context context) { |
| 242 | mContext = context; |
Eric Holk | 928bbac | 2019-01-02 10:38:54 -0800 | [diff] [blame] | 243 | initPrecompiledViews(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | /** |
| 247 | * Create a new LayoutInflater instance that is a copy of an existing |
| 248 | * LayoutInflater, optionally with its Context changed. For use in |
| 249 | * implementing {@link #cloneInContext}. |
Aurimas Liutikas | 67e2ae8 | 2016-10-11 18:17:42 -0700 | [diff] [blame] | 250 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 251 | * @param original The original LayoutInflater to copy. |
| 252 | * @param newContext The new Context to use. |
| 253 | */ |
| 254 | protected LayoutInflater(LayoutInflater original, Context newContext) { |
| 255 | mContext = newContext; |
| 256 | mFactory = original.mFactory; |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 257 | mFactory2 = original.mFactory2; |
Dianne Hackborn | 420829e | 2011-01-28 11:30:35 -0800 | [diff] [blame] | 258 | mPrivateFactory = original.mPrivateFactory; |
Dan Sandler | 0c7bb33 | 2014-09-18 22:11:18 -0400 | [diff] [blame] | 259 | setFilter(original.mFilter); |
Eric Holk | 928bbac | 2019-01-02 10:38:54 -0800 | [diff] [blame] | 260 | initPrecompiledViews(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 261 | } |
Aurimas Liutikas | 67e2ae8 | 2016-10-11 18:17:42 -0700 | [diff] [blame] | 262 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 263 | /** |
| 264 | * Obtains the LayoutInflater from the given context. |
| 265 | */ |
| 266 | public static LayoutInflater from(Context context) { |
| 267 | LayoutInflater LayoutInflater = |
| 268 | (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); |
| 269 | if (LayoutInflater == null) { |
| 270 | throw new AssertionError("LayoutInflater not found."); |
| 271 | } |
| 272 | return LayoutInflater; |
| 273 | } |
| 274 | |
| 275 | /** |
| 276 | * Create a copy of the existing LayoutInflater object, with the copy |
| 277 | * pointing to a different Context than the original. This is used by |
| 278 | * {@link ContextThemeWrapper} to create a new LayoutInflater to go along |
| 279 | * with the new Context theme. |
Aurimas Liutikas | 67e2ae8 | 2016-10-11 18:17:42 -0700 | [diff] [blame] | 280 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 281 | * @param newContext The new Context to associate with the new LayoutInflater. |
| 282 | * May be the same as the original Context if desired. |
Aurimas Liutikas | 67e2ae8 | 2016-10-11 18:17:42 -0700 | [diff] [blame] | 283 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 284 | * @return Returns a brand spanking new LayoutInflater object associated with |
| 285 | * the given Context. |
| 286 | */ |
| 287 | public abstract LayoutInflater cloneInContext(Context newContext); |
Aurimas Liutikas | 67e2ae8 | 2016-10-11 18:17:42 -0700 | [diff] [blame] | 288 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 289 | /** |
| 290 | * Return the context we are running in, for access to resources, class |
| 291 | * loader, etc. |
| 292 | */ |
| 293 | public Context getContext() { |
| 294 | return mContext; |
| 295 | } |
| 296 | |
| 297 | /** |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 298 | * Return the current {@link Factory} (or null). This is called on each element |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 299 | * name. If the factory returns a View, add that to the hierarchy. If it |
| 300 | * returns null, proceed to call onCreateView(name). |
| 301 | */ |
| 302 | public final Factory getFactory() { |
| 303 | return mFactory; |
| 304 | } |
| 305 | |
| 306 | /** |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 307 | * Return the current {@link Factory2}. Returns null if no factory is set |
| 308 | * or the set factory does not implement the {@link Factory2} interface. |
| 309 | * This is called on each element |
| 310 | * name. If the factory returns a View, add that to the hierarchy. If it |
| 311 | * returns null, proceed to call onCreateView(name). |
| 312 | */ |
| 313 | public final Factory2 getFactory2() { |
| 314 | return mFactory2; |
| 315 | } |
| 316 | |
| 317 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 318 | * Attach a custom Factory interface for creating views while using |
| 319 | * this LayoutInflater. This must not be null, and can only be set once; |
| 320 | * after setting, you can not change the factory. This is |
| 321 | * called on each element name as the xml is parsed. If the factory returns |
| 322 | * a View, that is added to the hierarchy. If it returns null, the next |
| 323 | * factory default {@link #onCreateView} method is called. |
Aurimas Liutikas | 67e2ae8 | 2016-10-11 18:17:42 -0700 | [diff] [blame] | 324 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 325 | * <p>If you have an existing |
| 326 | * LayoutInflater and want to add your own factory to it, use |
| 327 | * {@link #cloneInContext} to clone the existing instance and then you |
| 328 | * can use this function (once) on the returned new instance. This will |
| 329 | * merge your own factory with whatever factory the original instance is |
| 330 | * using. |
| 331 | */ |
| 332 | public void setFactory(Factory factory) { |
| 333 | if (mFactorySet) { |
| 334 | throw new IllegalStateException("A factory has already been set on this LayoutInflater"); |
| 335 | } |
| 336 | if (factory == null) { |
| 337 | throw new NullPointerException("Given factory can not be null"); |
| 338 | } |
| 339 | mFactorySet = true; |
| 340 | if (mFactory == null) { |
| 341 | mFactory = factory; |
| 342 | } else { |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 343 | mFactory = new FactoryMerger(factory, null, mFactory, mFactory2); |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | /** |
| 348 | * Like {@link #setFactory}, but allows you to set a {@link Factory2} |
| 349 | * interface. |
| 350 | */ |
| 351 | public void setFactory2(Factory2 factory) { |
| 352 | if (mFactorySet) { |
| 353 | throw new IllegalStateException("A factory has already been set on this LayoutInflater"); |
| 354 | } |
| 355 | if (factory == null) { |
| 356 | throw new NullPointerException("Given factory can not be null"); |
| 357 | } |
| 358 | mFactorySet = true; |
| 359 | if (mFactory == null) { |
| 360 | mFactory = mFactory2 = factory; |
| 361 | } else { |
Adam Powell | 371a809 | 2014-06-20 12:51:12 -0700 | [diff] [blame] | 362 | mFactory = mFactory2 = new FactoryMerger(factory, factory, mFactory, mFactory2); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 363 | } |
| 364 | } |
| 365 | |
| 366 | /** |
Dianne Hackborn | 420829e | 2011-01-28 11:30:35 -0800 | [diff] [blame] | 367 | * @hide for use by framework |
| 368 | */ |
Mathew Inwood | a570dee | 2018-08-17 14:56:00 +0100 | [diff] [blame] | 369 | @UnsupportedAppUsage |
Dianne Hackborn | 420829e | 2011-01-28 11:30:35 -0800 | [diff] [blame] | 370 | public void setPrivateFactory(Factory2 factory) { |
Adam Powell | 371a809 | 2014-06-20 12:51:12 -0700 | [diff] [blame] | 371 | if (mPrivateFactory == null) { |
| 372 | mPrivateFactory = factory; |
| 373 | } else { |
| 374 | mPrivateFactory = new FactoryMerger(factory, factory, mPrivateFactory, mPrivateFactory); |
| 375 | } |
Dianne Hackborn | 420829e | 2011-01-28 11:30:35 -0800 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 379 | * @return The {@link Filter} currently used by this LayoutInflater to restrict the set of Views |
| 380 | * that are allowed to be inflated. |
| 381 | */ |
| 382 | public Filter getFilter() { |
| 383 | return mFilter; |
| 384 | } |
Aurimas Liutikas | 67e2ae8 | 2016-10-11 18:17:42 -0700 | [diff] [blame] | 385 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 386 | /** |
| 387 | * Sets the {@link Filter} to by this LayoutInflater. If a view is attempted to be inflated |
| 388 | * which is not allowed by the {@link Filter}, the {@link #inflate(int, ViewGroup)} call will |
| 389 | * throw an {@link InflateException}. This filter will replace any previous filter set on this |
| 390 | * LayoutInflater. |
Aurimas Liutikas | 67e2ae8 | 2016-10-11 18:17:42 -0700 | [diff] [blame] | 391 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 392 | * @param filter The Filter which restricts the set of Views that are allowed to be inflated. |
| 393 | * This filter will replace any previous filter set on this LayoutInflater. |
| 394 | */ |
| 395 | public void setFilter(Filter filter) { |
| 396 | mFilter = filter; |
| 397 | if (filter != null) { |
| 398 | mFilterMap = new HashMap<String, Boolean>(); |
| 399 | } |
| 400 | } |
| 401 | |
Eric Holk | 928bbac | 2019-01-02 10:38:54 -0800 | [diff] [blame] | 402 | private void initPrecompiledViews() { |
Eric Holk | 25aa4ed | 2019-01-18 11:49:55 -0800 | [diff] [blame] | 403 | initPrecompiledViews( |
| 404 | SystemProperties.getBoolean(USE_PRECOMPILED_LAYOUT_SYSTEM_PROPERTY, false)); |
| 405 | } |
| 406 | |
| 407 | private void initPrecompiledViews(boolean enablePrecompiledViews) { |
| 408 | mUseCompiledView = enablePrecompiledViews; |
| 409 | |
Eric Holk | 987c7a9 | 2019-01-17 10:51:17 -0800 | [diff] [blame] | 410 | if (!mUseCompiledView) { |
Eric Holk | 25aa4ed | 2019-01-18 11:49:55 -0800 | [diff] [blame] | 411 | mPrecompiledClassLoader = null; |
Eric Holk | 987c7a9 | 2019-01-17 10:51:17 -0800 | [diff] [blame] | 412 | return; |
| 413 | } |
| 414 | |
| 415 | // Make sure the application allows code generation |
| 416 | ApplicationInfo appInfo = mContext.getApplicationInfo(); |
Victor Hsieh | fa9df0b | 2019-01-29 12:48:36 -0800 | [diff] [blame] | 417 | if (appInfo.isEmbeddedDexUsed() || appInfo.isPrivilegedApp()) { |
Eric Holk | 987c7a9 | 2019-01-17 10:51:17 -0800 | [diff] [blame] | 418 | mUseCompiledView = false; |
| 419 | return; |
| 420 | } |
| 421 | |
| 422 | // Try to load the precompiled layout file. |
Eric Holk | 928bbac | 2019-01-02 10:38:54 -0800 | [diff] [blame] | 423 | try { |
Eric Holk | 987c7a9 | 2019-01-17 10:51:17 -0800 | [diff] [blame] | 424 | mPrecompiledClassLoader = mContext.getClassLoader(); |
| 425 | String dexFile = mContext.getCodeCacheDir() + COMPILED_VIEW_DEX_FILE_NAME; |
| 426 | if (new File(dexFile).exists()) { |
| 427 | mPrecompiledClassLoader = new PathClassLoader(dexFile, mPrecompiledClassLoader); |
| 428 | } else { |
| 429 | // If the precompiled layout file doesn't exist, then disable precompiled |
| 430 | // layouts. |
| 431 | mUseCompiledView = false; |
Eric Holk | 928bbac | 2019-01-02 10:38:54 -0800 | [diff] [blame] | 432 | } |
| 433 | } catch (Throwable e) { |
| 434 | if (DEBUG) { |
| 435 | Log.e(TAG, "Failed to initialized precompiled views layouts", e); |
| 436 | } |
| 437 | mUseCompiledView = false; |
| 438 | } |
Eric Holk | 25aa4ed | 2019-01-18 11:49:55 -0800 | [diff] [blame] | 439 | if (!mUseCompiledView) { |
| 440 | mPrecompiledClassLoader = null; |
| 441 | } |
| 442 | } |
| 443 | |
| 444 | /** |
| 445 | * @hide for use by CTS tests |
| 446 | */ |
| 447 | @TestApi |
| 448 | public void setPrecompiledLayoutsEnabledForTesting(boolean enablePrecompiledLayouts) { |
| 449 | initPrecompiledViews(enablePrecompiledLayouts); |
Eric Holk | 928bbac | 2019-01-02 10:38:54 -0800 | [diff] [blame] | 450 | } |
| 451 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 452 | /** |
| 453 | * Inflate a new view hierarchy from the specified xml resource. Throws |
| 454 | * {@link InflateException} if there is an error. |
Aurimas Liutikas | 67e2ae8 | 2016-10-11 18:17:42 -0700 | [diff] [blame] | 455 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 456 | * @param resource ID for an XML layout resource to load (e.g., |
| 457 | * <code>R.layout.main_page</code>) |
| 458 | * @param root Optional view to be the parent of the generated hierarchy. |
| 459 | * @return The root View of the inflated hierarchy. If root was supplied, |
| 460 | * this is the root View; otherwise it is the root of the inflated |
| 461 | * XML file. |
| 462 | */ |
Tor Norbye | 7b9c912 | 2013-05-30 16:48:33 -0700 | [diff] [blame] | 463 | public View inflate(@LayoutRes int resource, @Nullable ViewGroup root) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 464 | return inflate(resource, root, root != null); |
| 465 | } |
| 466 | |
| 467 | /** |
| 468 | * Inflate a new view hierarchy from the specified xml node. Throws |
| 469 | * {@link InflateException} if there is an error. * |
| 470 | * <p> |
| 471 | * <em><strong>Important</strong></em> For performance |
| 472 | * reasons, view inflation relies heavily on pre-processing of XML files |
| 473 | * that is done at build time. Therefore, it is not currently possible to |
| 474 | * use LayoutInflater with an XmlPullParser over a plain XML file at runtime. |
Aurimas Liutikas | 67e2ae8 | 2016-10-11 18:17:42 -0700 | [diff] [blame] | 475 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 476 | * @param parser XML dom node containing the description of the view |
| 477 | * hierarchy. |
| 478 | * @param root Optional view to be the parent of the generated hierarchy. |
| 479 | * @return The root View of the inflated hierarchy. If root was supplied, |
| 480 | * this is the root View; otherwise it is the root of the inflated |
| 481 | * XML file. |
| 482 | */ |
Scott Kennedy | db5fd42 | 2015-01-15 11:56:33 -0800 | [diff] [blame] | 483 | public View inflate(XmlPullParser parser, @Nullable ViewGroup root) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 484 | return inflate(parser, root, root != null); |
| 485 | } |
| 486 | |
| 487 | /** |
| 488 | * Inflate a new view hierarchy from the specified xml resource. Throws |
| 489 | * {@link InflateException} if there is an error. |
Aurimas Liutikas | 67e2ae8 | 2016-10-11 18:17:42 -0700 | [diff] [blame] | 490 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 491 | * @param resource ID for an XML layout resource to load (e.g., |
| 492 | * <code>R.layout.main_page</code>) |
| 493 | * @param root Optional view to be the parent of the generated hierarchy (if |
| 494 | * <em>attachToRoot</em> is true), or else simply an object that |
| 495 | * provides a set of LayoutParams values for root of the returned |
| 496 | * hierarchy (if <em>attachToRoot</em> is false.) |
| 497 | * @param attachToRoot Whether the inflated hierarchy should be attached to |
| 498 | * the root parameter? If false, root is only used to create the |
| 499 | * correct subclass of LayoutParams for the root view in the XML. |
| 500 | * @return The root View of the inflated hierarchy. If root was supplied and |
| 501 | * attachToRoot is true, this is root; otherwise it is the root of |
| 502 | * the inflated XML file. |
| 503 | */ |
Tor Norbye | 7b9c912 | 2013-05-30 16:48:33 -0700 | [diff] [blame] | 504 | public View inflate(@LayoutRes int resource, @Nullable ViewGroup root, boolean attachToRoot) { |
Alan Viverette | 0810b63 | 2014-05-01 14:42:56 -0700 | [diff] [blame] | 505 | final Resources res = getContext().getResources(); |
| 506 | if (DEBUG) { |
| 507 | Log.d(TAG, "INFLATING from resource: \"" + res.getResourceName(resource) + "\" (" |
Eric Holk | 928bbac | 2019-01-02 10:38:54 -0800 | [diff] [blame] | 508 | + Integer.toHexString(resource) + ")"); |
Alan Viverette | 0810b63 | 2014-05-01 14:42:56 -0700 | [diff] [blame] | 509 | } |
| 510 | |
Eric Holk | 928bbac | 2019-01-02 10:38:54 -0800 | [diff] [blame] | 511 | View view = tryInflatePrecompiled(resource, res, root, attachToRoot); |
| 512 | if (view != null) { |
| 513 | return view; |
| 514 | } |
| 515 | XmlResourceParser parser = res.getLayout(resource); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 516 | try { |
| 517 | return inflate(parser, root, attachToRoot); |
| 518 | } finally { |
| 519 | parser.close(); |
| 520 | } |
| 521 | } |
| 522 | |
Eric Holk | 928bbac | 2019-01-02 10:38:54 -0800 | [diff] [blame] | 523 | private @Nullable |
| 524 | View tryInflatePrecompiled(@LayoutRes int resource, Resources res, @Nullable ViewGroup root, |
| 525 | boolean attachToRoot) { |
| 526 | if (!mUseCompiledView) { |
| 527 | return null; |
| 528 | } |
| 529 | |
| 530 | Trace.traceBegin(Trace.TRACE_TAG_VIEW, "inflate (precompiled)"); |
| 531 | |
| 532 | // Try to inflate using a precompiled layout. |
| 533 | String pkg = res.getResourcePackageName(resource); |
| 534 | String layout = res.getResourceEntryName(resource); |
| 535 | |
| 536 | try { |
| 537 | Class clazz = mPrecompiledClassLoader.loadClass("" + pkg + ".CompiledView"); |
| 538 | Method inflater = clazz.getMethod(layout, Context.class, int.class); |
| 539 | View view = (View) inflater.invoke(null, mContext, resource); |
| 540 | |
| 541 | if (view != null && root != null) { |
| 542 | // We were able to use the precompiled inflater, but now we need to do some work to |
| 543 | // attach the view to the root correctly. |
| 544 | XmlResourceParser parser = res.getLayout(resource); |
| 545 | try { |
| 546 | AttributeSet attrs = Xml.asAttributeSet(parser); |
| 547 | advanceToRootNode(parser); |
| 548 | ViewGroup.LayoutParams params = root.generateLayoutParams(attrs); |
| 549 | |
| 550 | if (attachToRoot) { |
| 551 | root.addView(view, params); |
| 552 | } else { |
| 553 | view.setLayoutParams(params); |
| 554 | } |
| 555 | } finally { |
| 556 | parser.close(); |
| 557 | } |
| 558 | } |
| 559 | |
| 560 | return view; |
| 561 | } catch (Throwable e) { |
| 562 | if (DEBUG) { |
| 563 | Log.e(TAG, "Failed to use precompiled view", e); |
| 564 | } |
| 565 | } finally { |
| 566 | Trace.traceEnd(Trace.TRACE_TAG_VIEW); |
| 567 | } |
| 568 | return null; |
| 569 | } |
| 570 | |
| 571 | /** |
| 572 | * Advances the given parser to the first START_TAG. Throws InflateException if no start tag is |
| 573 | * found. |
| 574 | */ |
| 575 | private void advanceToRootNode(XmlPullParser parser) |
| 576 | throws InflateException, IOException, XmlPullParserException { |
| 577 | // Look for the root node. |
| 578 | int type; |
| 579 | while ((type = parser.next()) != XmlPullParser.START_TAG && |
| 580 | type != XmlPullParser.END_DOCUMENT) { |
| 581 | // Empty |
| 582 | } |
| 583 | |
| 584 | if (type != XmlPullParser.START_TAG) { |
| 585 | throw new InflateException(parser.getPositionDescription() |
| 586 | + ": No start tag found!"); |
| 587 | } |
| 588 | } |
| 589 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 590 | /** |
| 591 | * Inflate a new view hierarchy from the specified XML node. Throws |
| 592 | * {@link InflateException} if there is an error. |
| 593 | * <p> |
| 594 | * <em><strong>Important</strong></em> For performance |
| 595 | * reasons, view inflation relies heavily on pre-processing of XML files |
| 596 | * that is done at build time. Therefore, it is not currently possible to |
| 597 | * use LayoutInflater with an XmlPullParser over a plain XML file at runtime. |
Aurimas Liutikas | 67e2ae8 | 2016-10-11 18:17:42 -0700 | [diff] [blame] | 598 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 599 | * @param parser XML dom node containing the description of the view |
| 600 | * hierarchy. |
| 601 | * @param root Optional view to be the parent of the generated hierarchy (if |
| 602 | * <em>attachToRoot</em> is true), or else simply an object that |
| 603 | * provides a set of LayoutParams values for root of the returned |
| 604 | * hierarchy (if <em>attachToRoot</em> is false.) |
| 605 | * @param attachToRoot Whether the inflated hierarchy should be attached to |
| 606 | * the root parameter? If false, root is only used to create the |
| 607 | * correct subclass of LayoutParams for the root view in the XML. |
| 608 | * @return The root View of the inflated hierarchy. If root was supplied and |
| 609 | * attachToRoot is true, this is root; otherwise it is the root of |
| 610 | * the inflated XML file. |
| 611 | */ |
Scott Kennedy | db5fd42 | 2015-01-15 11:56:33 -0800 | [diff] [blame] | 612 | public View inflate(XmlPullParser parser, @Nullable ViewGroup root, boolean attachToRoot) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 613 | synchronized (mConstructorArgs) { |
Romain Guy | 09f7b93 | 2013-04-10 11:42:44 -0700 | [diff] [blame] | 614 | Trace.traceBegin(Trace.TRACE_TAG_VIEW, "inflate"); |
| 615 | |
Alan Viverette | 6194d72 | 2015-03-20 15:49:06 -0700 | [diff] [blame] | 616 | final Context inflaterContext = mContext; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 617 | final AttributeSet attrs = Xml.asAttributeSet(parser); |
Alan Viverette | 6194d72 | 2015-03-20 15:49:06 -0700 | [diff] [blame] | 618 | Context lastContext = (Context) mConstructorArgs[0]; |
| 619 | mConstructorArgs[0] = inflaterContext; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 620 | View result = root; |
| 621 | |
| 622 | try { |
Eric Holk | 928bbac | 2019-01-02 10:38:54 -0800 | [diff] [blame] | 623 | advanceToRootNode(parser); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 624 | final String name = parser.getName(); |
Aurimas Liutikas | 67e2ae8 | 2016-10-11 18:17:42 -0700 | [diff] [blame] | 625 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 626 | if (DEBUG) { |
| 627 | System.out.println("**************************"); |
| 628 | System.out.println("Creating root view: " |
| 629 | + name); |
| 630 | System.out.println("**************************"); |
| 631 | } |
| 632 | |
| 633 | if (TAG_MERGE.equals(name)) { |
| 634 | if (root == null || !attachToRoot) { |
| 635 | throw new InflateException("<merge /> can be used only with a valid " |
| 636 | + "ViewGroup root and attachToRoot=true"); |
| 637 | } |
| 638 | |
Alan Viverette | 6194d72 | 2015-03-20 15:49:06 -0700 | [diff] [blame] | 639 | rInflate(parser, root, inflaterContext, attrs, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 640 | } else { |
| 641 | // Temp is the root view that was found in the xml |
Alan Viverette | 6194d72 | 2015-03-20 15:49:06 -0700 | [diff] [blame] | 642 | final View temp = createViewFromTag(root, name, inflaterContext, attrs); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 643 | |
| 644 | ViewGroup.LayoutParams params = null; |
| 645 | |
| 646 | if (root != null) { |
| 647 | if (DEBUG) { |
| 648 | System.out.println("Creating params from root: " + |
| 649 | root); |
| 650 | } |
| 651 | // Create layout params that match root, if supplied |
| 652 | params = root.generateLayoutParams(attrs); |
| 653 | if (!attachToRoot) { |
| 654 | // Set the layout params for temp if we are not |
| 655 | // attaching. (If we are, we use addView, below) |
| 656 | temp.setLayoutParams(params); |
| 657 | } |
| 658 | } |
| 659 | |
| 660 | if (DEBUG) { |
| 661 | System.out.println("-----> start inflating children"); |
| 662 | } |
Alan Viverette | 6194d72 | 2015-03-20 15:49:06 -0700 | [diff] [blame] | 663 | |
| 664 | // Inflate all children under temp against its context. |
| 665 | rInflateChildren(parser, temp, attrs, true); |
| 666 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 667 | if (DEBUG) { |
| 668 | System.out.println("-----> done inflating children"); |
| 669 | } |
| 670 | |
| 671 | // We are supposed to attach all the views we found (int temp) |
| 672 | // to root. Do that now. |
| 673 | if (root != null && attachToRoot) { |
| 674 | root.addView(temp, params); |
| 675 | } |
| 676 | |
| 677 | // Decide whether to return the root that was passed in or the |
| 678 | // top view found in xml. |
| 679 | if (root == null || !attachToRoot) { |
| 680 | result = temp; |
| 681 | } |
| 682 | } |
| 683 | |
| 684 | } catch (XmlPullParserException e) { |
Alan Viverette | 8f12481 | 2015-09-25 15:17:05 -0400 | [diff] [blame] | 685 | final InflateException ie = new InflateException(e.getMessage(), e); |
| 686 | ie.setStackTrace(EMPTY_STACK_TRACE); |
| 687 | throw ie; |
Alan Viverette | 9379505 | 2015-03-09 15:32:50 -0700 | [diff] [blame] | 688 | } catch (Exception e) { |
Alan Viverette | 8f12481 | 2015-09-25 15:17:05 -0400 | [diff] [blame] | 689 | final InflateException ie = new InflateException(parser.getPositionDescription() |
| 690 | + ": " + e.getMessage(), e); |
| 691 | ie.setStackTrace(EMPTY_STACK_TRACE); |
| 692 | throw ie; |
Dianne Hackborn | 9dae48e | 2010-08-26 10:20:01 -0700 | [diff] [blame] | 693 | } finally { |
| 694 | // Don't retain static reference on context. |
| 695 | mConstructorArgs[0] = lastContext; |
| 696 | mConstructorArgs[1] = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 697 | |
John Reck | 465e1a3 | 2015-10-19 12:46:09 -0700 | [diff] [blame] | 698 | Trace.traceEnd(Trace.TRACE_TAG_VIEW); |
| 699 | } |
Romain Guy | 09f7b93 | 2013-04-10 11:42:44 -0700 | [diff] [blame] | 700 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 701 | return result; |
| 702 | } |
| 703 | } |
| 704 | |
Mathew Inwood | 4985c5c | 2015-12-31 12:28:54 +0000 | [diff] [blame] | 705 | private static final ClassLoader BOOT_CLASS_LOADER = LayoutInflater.class.getClassLoader(); |
| 706 | |
| 707 | private final boolean verifyClassLoader(Constructor<? extends View> constructor) { |
| 708 | final ClassLoader constructorLoader = constructor.getDeclaringClass().getClassLoader(); |
| 709 | if (constructorLoader == BOOT_CLASS_LOADER) { |
| 710 | // fast path for boot class loader (most common case?) - always ok |
| 711 | return true; |
| 712 | } |
| 713 | // in all normal cases (no dynamic code loading), we will exit the following loop on the |
| 714 | // first iteration (i.e. when the declaring classloader is the contexts class loader). |
| 715 | ClassLoader cl = mContext.getClassLoader(); |
| 716 | do { |
| 717 | if (constructorLoader == cl) { |
| 718 | return true; |
| 719 | } |
| 720 | cl = cl.getParent(); |
| 721 | } while (cl != null); |
| 722 | return false; |
| 723 | } |
| 724 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 725 | /** |
| 726 | * Low-level function for instantiating a view by name. This attempts to |
| 727 | * instantiate a view class of the given <var>name</var> found in this |
| 728 | * LayoutInflater's ClassLoader. |
Aurimas Liutikas | 67e2ae8 | 2016-10-11 18:17:42 -0700 | [diff] [blame] | 729 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 730 | * <p> |
| 731 | * There are two things that can happen in an error case: either the |
| 732 | * exception describing the error will be thrown, or a null will be |
| 733 | * returned. You must deal with both possibilities -- the former will happen |
| 734 | * the first time createView() is called for a class of a particular name, |
| 735 | * the latter every time there-after for that class name. |
Aurimas Liutikas | 67e2ae8 | 2016-10-11 18:17:42 -0700 | [diff] [blame] | 736 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 737 | * @param name The full name of the class to be instantiated. |
| 738 | * @param attrs The XML attributes supplied for this instance. |
Aurimas Liutikas | 67e2ae8 | 2016-10-11 18:17:42 -0700 | [diff] [blame] | 739 | * |
Gilles Debunne | 3030193 | 2010-06-16 18:32:00 -0700 | [diff] [blame] | 740 | * @return View The newly instantiated view, or null. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 741 | */ |
| 742 | public final View createView(String name, String prefix, AttributeSet attrs) |
| 743 | throws ClassNotFoundException, InflateException { |
Gilles Debunne | 3030193 | 2010-06-16 18:32:00 -0700 | [diff] [blame] | 744 | Constructor<? extends View> constructor = sConstructorMap.get(name); |
Mathew Inwood | 4985c5c | 2015-12-31 12:28:54 +0000 | [diff] [blame] | 745 | if (constructor != null && !verifyClassLoader(constructor)) { |
| 746 | constructor = null; |
| 747 | sConstructorMap.remove(name); |
| 748 | } |
Gilles Debunne | 3030193 | 2010-06-16 18:32:00 -0700 | [diff] [blame] | 749 | Class<? extends View> clazz = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 750 | |
| 751 | try { |
Romain Guy | 09f7b93 | 2013-04-10 11:42:44 -0700 | [diff] [blame] | 752 | Trace.traceBegin(Trace.TRACE_TAG_VIEW, name); |
| 753 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 754 | if (constructor == null) { |
| 755 | // Class not found in the cache, see if it's real, and try to add it |
Romain Guy | d03b880 | 2009-09-16 14:36:16 -0700 | [diff] [blame] | 756 | clazz = mContext.getClassLoader().loadClass( |
Gilles Debunne | 3030193 | 2010-06-16 18:32:00 -0700 | [diff] [blame] | 757 | prefix != null ? (prefix + name) : name).asSubclass(View.class); |
Aurimas Liutikas | 67e2ae8 | 2016-10-11 18:17:42 -0700 | [diff] [blame] | 758 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 759 | if (mFilter != null && clazz != null) { |
| 760 | boolean allowed = mFilter.onLoadClass(clazz); |
| 761 | if (!allowed) { |
| 762 | failNotAllowed(name, prefix, attrs); |
| 763 | } |
| 764 | } |
| 765 | constructor = clazz.getConstructor(mConstructorSignature); |
Alan Viverette | 904de2e | 2015-05-04 10:32:57 -0700 | [diff] [blame] | 766 | constructor.setAccessible(true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 767 | sConstructorMap.put(name, constructor); |
| 768 | } else { |
| 769 | // If we have a filter, apply it to cached constructor |
| 770 | if (mFilter != null) { |
| 771 | // Have we seen this name before? |
| 772 | Boolean allowedState = mFilterMap.get(name); |
| 773 | if (allowedState == null) { |
| 774 | // New class -- remember whether it is allowed |
Romain Guy | d03b880 | 2009-09-16 14:36:16 -0700 | [diff] [blame] | 775 | clazz = mContext.getClassLoader().loadClass( |
Gilles Debunne | 3030193 | 2010-06-16 18:32:00 -0700 | [diff] [blame] | 776 | prefix != null ? (prefix + name) : name).asSubclass(View.class); |
Aurimas Liutikas | 67e2ae8 | 2016-10-11 18:17:42 -0700 | [diff] [blame] | 777 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 778 | boolean allowed = clazz != null && mFilter.onLoadClass(clazz); |
| 779 | mFilterMap.put(name, allowed); |
| 780 | if (!allowed) { |
| 781 | failNotAllowed(name, prefix, attrs); |
| 782 | } |
| 783 | } else if (allowedState.equals(Boolean.FALSE)) { |
| 784 | failNotAllowed(name, prefix, attrs); |
| 785 | } |
| 786 | } |
| 787 | } |
| 788 | |
Jason Monk | 9a376bc | 2017-05-10 09:52:10 -0400 | [diff] [blame] | 789 | Object lastContext = mConstructorArgs[0]; |
| 790 | if (mConstructorArgs[0] == null) { |
| 791 | // Fill in the context if not already within inflation. |
| 792 | mConstructorArgs[0] = mContext; |
| 793 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 794 | Object[] args = mConstructorArgs; |
| 795 | args[1] = attrs; |
Jeff Sharkey | b27b7a1 | 2012-04-02 21:07:29 -0700 | [diff] [blame] | 796 | |
| 797 | final View view = constructor.newInstance(args); |
| 798 | if (view instanceof ViewStub) { |
Alan Viverette | a9ddb8d | 2014-09-17 18:14:32 -0700 | [diff] [blame] | 799 | // Use the same context when inflating ViewStub later. |
Jeff Sharkey | b27b7a1 | 2012-04-02 21:07:29 -0700 | [diff] [blame] | 800 | final ViewStub viewStub = (ViewStub) view; |
Alan Viverette | a9ddb8d | 2014-09-17 18:14:32 -0700 | [diff] [blame] | 801 | viewStub.setLayoutInflater(cloneInContext((Context) args[0])); |
Jeff Sharkey | b27b7a1 | 2012-04-02 21:07:29 -0700 | [diff] [blame] | 802 | } |
Jason Monk | 9a376bc | 2017-05-10 09:52:10 -0400 | [diff] [blame] | 803 | mConstructorArgs[0] = lastContext; |
Jeff Sharkey | b27b7a1 | 2012-04-02 21:07:29 -0700 | [diff] [blame] | 804 | return view; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 805 | |
| 806 | } catch (NoSuchMethodException e) { |
Alan Viverette | 8f12481 | 2015-09-25 15:17:05 -0400 | [diff] [blame] | 807 | final InflateException ie = new InflateException(attrs.getPositionDescription() |
| 808 | + ": Error inflating class " + (prefix != null ? (prefix + name) : name), e); |
| 809 | ie.setStackTrace(EMPTY_STACK_TRACE); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 810 | throw ie; |
| 811 | |
Gilles Debunne | 3030193 | 2010-06-16 18:32:00 -0700 | [diff] [blame] | 812 | } catch (ClassCastException e) { |
| 813 | // If loaded class is not a View subclass |
Alan Viverette | 8f12481 | 2015-09-25 15:17:05 -0400 | [diff] [blame] | 814 | final InflateException ie = new InflateException(attrs.getPositionDescription() |
| 815 | + ": Class is not a View " + (prefix != null ? (prefix + name) : name), e); |
| 816 | ie.setStackTrace(EMPTY_STACK_TRACE); |
Gilles Debunne | 3030193 | 2010-06-16 18:32:00 -0700 | [diff] [blame] | 817 | throw ie; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 818 | } catch (ClassNotFoundException e) { |
| 819 | // If loadClass fails, we should propagate the exception. |
| 820 | throw e; |
| 821 | } catch (Exception e) { |
Alan Viverette | 8f12481 | 2015-09-25 15:17:05 -0400 | [diff] [blame] | 822 | final InflateException ie = new InflateException( |
| 823 | attrs.getPositionDescription() + ": Error inflating class " |
| 824 | + (clazz == null ? "<unknown>" : clazz.getName()), e); |
| 825 | ie.setStackTrace(EMPTY_STACK_TRACE); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 826 | throw ie; |
Romain Guy | 09f7b93 | 2013-04-10 11:42:44 -0700 | [diff] [blame] | 827 | } finally { |
| 828 | Trace.traceEnd(Trace.TRACE_TAG_VIEW); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 829 | } |
| 830 | } |
| 831 | |
| 832 | /** |
Gilles Debunne | 3030193 | 2010-06-16 18:32:00 -0700 | [diff] [blame] | 833 | * Throw an exception because the specified class is not allowed to be inflated. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 834 | */ |
| 835 | private void failNotAllowed(String name, String prefix, AttributeSet attrs) { |
Romain Guy | 9c1223a | 2011-05-17 14:25:49 -0700 | [diff] [blame] | 836 | throw new InflateException(attrs.getPositionDescription() |
Alan Viverette | 8f12481 | 2015-09-25 15:17:05 -0400 | [diff] [blame] | 837 | + ": Class not allowed to be inflated "+ (prefix != null ? (prefix + name) : name)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 838 | } |
| 839 | |
| 840 | /** |
| 841 | * This routine is responsible for creating the correct subclass of View |
| 842 | * given the xml element name. Override it to handle custom view objects. If |
| 843 | * you override this in your subclass be sure to call through to |
| 844 | * super.onCreateView(name) for names you do not recognize. |
Aurimas Liutikas | 67e2ae8 | 2016-10-11 18:17:42 -0700 | [diff] [blame] | 845 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 846 | * @param name The fully qualified class name of the View to be create. |
| 847 | * @param attrs An AttributeSet of attributes to apply to the View. |
Aurimas Liutikas | 67e2ae8 | 2016-10-11 18:17:42 -0700 | [diff] [blame] | 848 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 849 | * @return View The View created. |
| 850 | */ |
| 851 | protected View onCreateView(String name, AttributeSet attrs) |
| 852 | throws ClassNotFoundException { |
| 853 | return createView(name, "android.view.", attrs); |
| 854 | } |
| 855 | |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 856 | /** |
| 857 | * Version of {@link #onCreateView(String, AttributeSet)} that also |
Chet Haase | 430742f | 2013-04-12 11:18:36 -0700 | [diff] [blame] | 858 | * takes the future parent of the view being constructed. The default |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 859 | * implementation simply calls {@link #onCreateView(String, AttributeSet)}. |
| 860 | * |
| 861 | * @param parent The future parent of the returned view. <em>Note that |
| 862 | * this may be null.</em> |
| 863 | * @param name The fully qualified class name of the View to be create. |
| 864 | * @param attrs An AttributeSet of attributes to apply to the View. |
| 865 | * |
| 866 | * @return View The View created. |
| 867 | */ |
| 868 | protected View onCreateView(View parent, String name, AttributeSet attrs) |
| 869 | throws ClassNotFoundException { |
| 870 | return onCreateView(name, attrs); |
| 871 | } |
| 872 | |
Alan Viverette | 24927f2 | 2014-01-07 17:28:48 -0800 | [diff] [blame] | 873 | /** |
Alan Viverette | 6194d72 | 2015-03-20 15:49:06 -0700 | [diff] [blame] | 874 | * Convenience method for calling through to the five-arg createViewFromTag |
| 875 | * method. This method passes {@code false} for the {@code ignoreThemeAttr} |
| 876 | * argument and should be used for everything except {@code >include>} |
| 877 | * tag parsing. |
| 878 | */ |
Mathew Inwood | a570dee | 2018-08-17 14:56:00 +0100 | [diff] [blame] | 879 | @UnsupportedAppUsage |
Alan Viverette | 6194d72 | 2015-03-20 15:49:06 -0700 | [diff] [blame] | 880 | private View createViewFromTag(View parent, String name, Context context, AttributeSet attrs) { |
| 881 | return createViewFromTag(parent, name, context, attrs, false); |
| 882 | } |
| 883 | |
| 884 | /** |
Alan Viverette | 24927f2 | 2014-01-07 17:28:48 -0800 | [diff] [blame] | 885 | * Creates a view from a tag name using the supplied attribute set. |
| 886 | * <p> |
Alan Viverette | 6194d72 | 2015-03-20 15:49:06 -0700 | [diff] [blame] | 887 | * <strong>Note:</strong> Default visibility so the BridgeInflater can |
| 888 | * override it. |
| 889 | * |
| 890 | * @param parent the parent view, used to inflate layout params |
| 891 | * @param name the name of the XML tag used to define the view |
| 892 | * @param context the inflation context for the view, typically the |
| 893 | * {@code parent} or base layout inflater context |
| 894 | * @param attrs the attribute set for the XML tag used to define the view |
| 895 | * @param ignoreThemeAttr {@code true} to ignore the {@code android:theme} |
| 896 | * attribute (if set) for the view being inflated, |
| 897 | * {@code false} otherwise |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 898 | */ |
Mathew Inwood | a570dee | 2018-08-17 14:56:00 +0100 | [diff] [blame] | 899 | @UnsupportedAppUsage |
Alan Viverette | 6194d72 | 2015-03-20 15:49:06 -0700 | [diff] [blame] | 900 | View createViewFromTag(View parent, String name, Context context, AttributeSet attrs, |
| 901 | boolean ignoreThemeAttr) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 902 | if (name.equals("view")) { |
| 903 | name = attrs.getAttributeValue(null, "class"); |
| 904 | } |
| 905 | |
Alan Viverette | 6194d72 | 2015-03-20 15:49:06 -0700 | [diff] [blame] | 906 | // Apply a theme wrapper, if allowed and one is specified. |
| 907 | if (!ignoreThemeAttr) { |
| 908 | final TypedArray ta = context.obtainStyledAttributes(attrs, ATTRS_THEME); |
| 909 | final int themeResId = ta.getResourceId(0, 0); |
| 910 | if (themeResId != 0) { |
| 911 | context = new ContextThemeWrapper(context, themeResId); |
| 912 | } |
| 913 | ta.recycle(); |
Alan Viverette | 24927f2 | 2014-01-07 17:28:48 -0800 | [diff] [blame] | 914 | } |
| 915 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 916 | try { |
Eric Holk | 29d0cd6 | 2018-12-14 10:27:29 -0800 | [diff] [blame] | 917 | View view = tryCreateView(parent, name, context, attrs); |
Alan Viverette | 24927f2 | 2014-01-07 17:28:48 -0800 | [diff] [blame] | 918 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 919 | if (view == null) { |
Alan Viverette | 24927f2 | 2014-01-07 17:28:48 -0800 | [diff] [blame] | 920 | final Object lastContext = mConstructorArgs[0]; |
Alan Viverette | 6194d72 | 2015-03-20 15:49:06 -0700 | [diff] [blame] | 921 | mConstructorArgs[0] = context; |
Alan Viverette | 24927f2 | 2014-01-07 17:28:48 -0800 | [diff] [blame] | 922 | try { |
| 923 | if (-1 == name.indexOf('.')) { |
| 924 | view = onCreateView(parent, name, attrs); |
| 925 | } else { |
| 926 | view = createView(name, null, attrs); |
| 927 | } |
| 928 | } finally { |
| 929 | mConstructorArgs[0] = lastContext; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 930 | } |
| 931 | } |
| 932 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 933 | return view; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 934 | } catch (InflateException e) { |
| 935 | throw e; |
| 936 | |
| 937 | } catch (ClassNotFoundException e) { |
Alan Viverette | 6194d72 | 2015-03-20 15:49:06 -0700 | [diff] [blame] | 938 | final InflateException ie = new InflateException(attrs.getPositionDescription() |
Alan Viverette | 8f12481 | 2015-09-25 15:17:05 -0400 | [diff] [blame] | 939 | + ": Error inflating class " + name, e); |
| 940 | ie.setStackTrace(EMPTY_STACK_TRACE); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 941 | throw ie; |
| 942 | |
| 943 | } catch (Exception e) { |
Alan Viverette | 6194d72 | 2015-03-20 15:49:06 -0700 | [diff] [blame] | 944 | final InflateException ie = new InflateException(attrs.getPositionDescription() |
Alan Viverette | 8f12481 | 2015-09-25 15:17:05 -0400 | [diff] [blame] | 945 | + ": Error inflating class " + name, e); |
| 946 | ie.setStackTrace(EMPTY_STACK_TRACE); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 947 | throw ie; |
| 948 | } |
| 949 | } |
| 950 | |
| 951 | /** |
Eric Holk | 29d0cd6 | 2018-12-14 10:27:29 -0800 | [diff] [blame] | 952 | * Tries to create a view from a tag name using the supplied attribute set. |
| 953 | * |
| 954 | * This method gives the factory provided by {@link LayoutInflater#setFactory} and |
| 955 | * {@link LayoutInflater#setFactory2} a chance to create a view. However, it does not apply all |
| 956 | * of the general view creation logic, and thus may return {@code null} for some tags. This |
| 957 | * method is used by {@link LayoutInflater#inflate} in creating {@code View} objects. |
| 958 | * |
| 959 | * @hide for use by precompiled layouts. |
| 960 | * |
| 961 | * @param parent the parent view, used to inflate layout params |
| 962 | * @param name the name of the XML tag used to define the view |
| 963 | * @param context the inflation context for the view, typically the |
| 964 | * {@code parent} or base layout inflater context |
| 965 | * @param attrs the attribute set for the XML tag used to define the view |
| 966 | */ |
| 967 | @UnsupportedAppUsage(trackingBug = 122360734) |
| 968 | @Nullable |
| 969 | public final View tryCreateView(@Nullable View parent, @NonNull String name, |
| 970 | @NonNull Context context, |
| 971 | @NonNull AttributeSet attrs) { |
| 972 | if (name.equals(TAG_1995)) { |
| 973 | // Let's party like it's 1995! |
| 974 | return new BlinkLayout(context, attrs); |
| 975 | } |
| 976 | |
| 977 | View view; |
| 978 | if (mFactory2 != null) { |
| 979 | view = mFactory2.onCreateView(parent, name, context, attrs); |
| 980 | } else if (mFactory != null) { |
| 981 | view = mFactory.onCreateView(name, context, attrs); |
| 982 | } else { |
| 983 | view = null; |
| 984 | } |
| 985 | |
| 986 | if (view == null && mPrivateFactory != null) { |
| 987 | view = mPrivateFactory.onCreateView(parent, name, context, attrs); |
| 988 | } |
| 989 | |
| 990 | return view; |
| 991 | } |
| 992 | |
| 993 | /** |
Alan Viverette | 6194d72 | 2015-03-20 15:49:06 -0700 | [diff] [blame] | 994 | * Recursive method used to inflate internal (non-root) children. This |
| 995 | * method calls through to {@link #rInflate} using the parent context as |
| 996 | * the inflation context. |
| 997 | * <strong>Note:</strong> Default visibility so the BridgeInflater can |
| 998 | * call it. |
| 999 | */ |
| 1000 | final void rInflateChildren(XmlPullParser parser, View parent, AttributeSet attrs, |
| 1001 | boolean finishInflate) throws XmlPullParserException, IOException { |
| 1002 | rInflate(parser, parent, parent.getContext(), attrs, finishInflate); |
| 1003 | } |
| 1004 | |
| 1005 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1006 | * Recursive method used to descend down the xml hierarchy and instantiate |
| 1007 | * views, instantiate their children, and then call onFinishInflate(). |
Alan Viverette | 6194d72 | 2015-03-20 15:49:06 -0700 | [diff] [blame] | 1008 | * <p> |
| 1009 | * <strong>Note:</strong> Default visibility so the BridgeInflater can |
| 1010 | * override it. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1011 | */ |
Alan Viverette | 6194d72 | 2015-03-20 15:49:06 -0700 | [diff] [blame] | 1012 | void rInflate(XmlPullParser parser, View parent, Context context, |
| 1013 | AttributeSet attrs, boolean finishInflate) throws XmlPullParserException, IOException { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1014 | |
| 1015 | final int depth = parser.getDepth(); |
| 1016 | int type; |
Evan Rosky | 37df2db | 2017-01-24 16:35:52 -0800 | [diff] [blame] | 1017 | boolean pendingRequestFocus = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1018 | |
| 1019 | while (((type = parser.next()) != XmlPullParser.END_TAG || |
| 1020 | parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) { |
| 1021 | |
| 1022 | if (type != XmlPullParser.START_TAG) { |
| 1023 | continue; |
| 1024 | } |
| 1025 | |
| 1026 | final String name = parser.getName(); |
Aurimas Liutikas | 67e2ae8 | 2016-10-11 18:17:42 -0700 | [diff] [blame] | 1027 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1028 | if (TAG_REQUEST_FOCUS.equals(name)) { |
Evan Rosky | 37df2db | 2017-01-24 16:35:52 -0800 | [diff] [blame] | 1029 | pendingRequestFocus = true; |
| 1030 | consumeChildElements(parser); |
Alan Viverette | 451a341 | 2014-02-11 18:08:46 -0800 | [diff] [blame] | 1031 | } else if (TAG_TAG.equals(name)) { |
| 1032 | parseViewTag(parser, parent, attrs); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1033 | } else if (TAG_INCLUDE.equals(name)) { |
| 1034 | if (parser.getDepth() == 0) { |
| 1035 | throw new InflateException("<include /> cannot be the root element"); |
| 1036 | } |
Alan Viverette | 6194d72 | 2015-03-20 15:49:06 -0700 | [diff] [blame] | 1037 | parseInclude(parser, context, parent, attrs); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1038 | } else if (TAG_MERGE.equals(name)) { |
| 1039 | throw new InflateException("<merge /> must be the root element"); |
| 1040 | } else { |
Alan Viverette | 6194d72 | 2015-03-20 15:49:06 -0700 | [diff] [blame] | 1041 | final View view = createViewFromTag(parent, name, context, attrs); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1042 | final ViewGroup viewGroup = (ViewGroup) parent; |
| 1043 | final ViewGroup.LayoutParams params = viewGroup.generateLayoutParams(attrs); |
Alan Viverette | 6194d72 | 2015-03-20 15:49:06 -0700 | [diff] [blame] | 1044 | rInflateChildren(parser, view, attrs, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1045 | viewGroup.addView(view, params); |
| 1046 | } |
| 1047 | } |
| 1048 | |
Evan Rosky | 37df2db | 2017-01-24 16:35:52 -0800 | [diff] [blame] | 1049 | if (pendingRequestFocus) { |
| 1050 | parent.restoreDefaultFocus(); |
| 1051 | } |
| 1052 | |
Alan Viverette | 6194d72 | 2015-03-20 15:49:06 -0700 | [diff] [blame] | 1053 | if (finishInflate) { |
| 1054 | parent.onFinishInflate(); |
| 1055 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1056 | } |
| 1057 | |
Alan Viverette | 451a341 | 2014-02-11 18:08:46 -0800 | [diff] [blame] | 1058 | /** |
Alan Viverette | 451a341 | 2014-02-11 18:08:46 -0800 | [diff] [blame] | 1059 | * Parses a <code><tag></code> element and sets a keyed tag on the |
| 1060 | * containing View. |
| 1061 | */ |
| 1062 | private void parseViewTag(XmlPullParser parser, View view, AttributeSet attrs) |
| 1063 | throws XmlPullParserException, IOException { |
Alan Viverette | 6194d72 | 2015-03-20 15:49:06 -0700 | [diff] [blame] | 1064 | final Context context = view.getContext(); |
| 1065 | final TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.ViewTag); |
| 1066 | final int key = ta.getResourceId(R.styleable.ViewTag_id, 0); |
| 1067 | final CharSequence value = ta.getText(R.styleable.ViewTag_value); |
Alan Viverette | 451a341 | 2014-02-11 18:08:46 -0800 | [diff] [blame] | 1068 | view.setTag(key, value); |
| 1069 | ta.recycle(); |
| 1070 | |
Alan Viverette | 6194d72 | 2015-03-20 15:49:06 -0700 | [diff] [blame] | 1071 | consumeChildElements(parser); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1072 | } |
| 1073 | |
Mathew Inwood | a570dee | 2018-08-17 14:56:00 +0100 | [diff] [blame] | 1074 | @UnsupportedAppUsage |
Alan Viverette | 6194d72 | 2015-03-20 15:49:06 -0700 | [diff] [blame] | 1075 | private void parseInclude(XmlPullParser parser, Context context, View parent, |
| 1076 | AttributeSet attrs) throws XmlPullParserException, IOException { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1077 | int type; |
| 1078 | |
Eric Holk | bfc6870 | 2019-01-02 11:11:24 -0800 | [diff] [blame] | 1079 | if (!(parent instanceof ViewGroup)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1080 | throw new InflateException("<include /> can only be used inside of a ViewGroup"); |
| 1081 | } |
| 1082 | |
Eric Holk | bfc6870 | 2019-01-02 11:11:24 -0800 | [diff] [blame] | 1083 | // Apply a theme wrapper, if requested. This is sort of a weird |
| 1084 | // edge case, since developers think the <include> overwrites |
| 1085 | // values in the AttributeSet of the included View. So, if the |
| 1086 | // included View has a theme attribute, we'll need to ignore it. |
| 1087 | final TypedArray ta = context.obtainStyledAttributes(attrs, ATTRS_THEME); |
| 1088 | final int themeResId = ta.getResourceId(0, 0); |
| 1089 | final boolean hasThemeOverride = themeResId != 0; |
| 1090 | if (hasThemeOverride) { |
| 1091 | context = new ContextThemeWrapper(context, themeResId); |
| 1092 | } |
| 1093 | ta.recycle(); |
| 1094 | |
| 1095 | // If the layout is pointing to a theme attribute, we have to |
| 1096 | // massage the value to get a resource identifier out of it. |
| 1097 | int layout = attrs.getAttributeResourceValue(null, ATTR_LAYOUT, 0); |
| 1098 | if (layout == 0) { |
| 1099 | final String value = attrs.getAttributeValue(null, ATTR_LAYOUT); |
| 1100 | if (value == null || value.length() <= 0) { |
| 1101 | throw new InflateException("You must specify a layout in the" |
| 1102 | + " include tag: <include layout=\"@layout/layoutID\" />"); |
| 1103 | } |
| 1104 | |
| 1105 | // Attempt to resolve the "?attr/name" string to an attribute |
| 1106 | // within the default (e.g. application) package. |
| 1107 | layout = context.getResources().getIdentifier( |
| 1108 | value.substring(1), "attr", context.getPackageName()); |
| 1109 | |
| 1110 | } |
| 1111 | |
| 1112 | // The layout might be referencing a theme attribute. |
| 1113 | if (mTempValue == null) { |
| 1114 | mTempValue = new TypedValue(); |
| 1115 | } |
| 1116 | if (layout != 0 && context.getTheme().resolveAttribute(layout, mTempValue, true)) { |
| 1117 | layout = mTempValue.resourceId; |
| 1118 | } |
| 1119 | |
| 1120 | if (layout == 0) { |
| 1121 | final String value = attrs.getAttributeValue(null, ATTR_LAYOUT); |
| 1122 | throw new InflateException("You must specify a valid layout " |
| 1123 | + "reference. The layout ID " + value + " is not valid."); |
| 1124 | } |
| 1125 | |
Eric Holk | 928bbac | 2019-01-02 10:38:54 -0800 | [diff] [blame] | 1126 | final View precompiled = tryInflatePrecompiled(layout, context.getResources(), |
| 1127 | (ViewGroup) parent, /*attachToRoot=*/true); |
| 1128 | if (precompiled == null) { |
| 1129 | final XmlResourceParser childParser = context.getResources().getLayout(layout); |
Eric Holk | bfc6870 | 2019-01-02 11:11:24 -0800 | [diff] [blame] | 1130 | |
Eric Holk | 928bbac | 2019-01-02 10:38:54 -0800 | [diff] [blame] | 1131 | try { |
| 1132 | final AttributeSet childAttrs = Xml.asAttributeSet(childParser); |
Eric Holk | bfc6870 | 2019-01-02 11:11:24 -0800 | [diff] [blame] | 1133 | |
Eric Holk | 928bbac | 2019-01-02 10:38:54 -0800 | [diff] [blame] | 1134 | while ((type = childParser.next()) != XmlPullParser.START_TAG && |
| 1135 | type != XmlPullParser.END_DOCUMENT) { |
| 1136 | // Empty. |
| 1137 | } |
| 1138 | |
| 1139 | if (type != XmlPullParser.START_TAG) { |
| 1140 | throw new InflateException(childParser.getPositionDescription() + |
| 1141 | ": No start tag found!"); |
| 1142 | } |
| 1143 | |
| 1144 | final String childName = childParser.getName(); |
| 1145 | |
| 1146 | if (TAG_MERGE.equals(childName)) { |
| 1147 | // The <merge> tag doesn't support android:theme, so |
| 1148 | // nothing special to do here. |
| 1149 | rInflate(childParser, parent, context, childAttrs, false); |
| 1150 | } else { |
| 1151 | final View view = createViewFromTag(parent, childName, |
| 1152 | context, childAttrs, hasThemeOverride); |
| 1153 | final ViewGroup group = (ViewGroup) parent; |
| 1154 | |
| 1155 | final TypedArray a = context.obtainStyledAttributes( |
| 1156 | attrs, R.styleable.Include); |
| 1157 | final int id = a.getResourceId(R.styleable.Include_id, View.NO_ID); |
| 1158 | final int visibility = a.getInt(R.styleable.Include_visibility, -1); |
| 1159 | a.recycle(); |
| 1160 | |
| 1161 | // We try to load the layout params set in the <include /> tag. |
| 1162 | // If the parent can't generate layout params (ex. missing width |
| 1163 | // or height for the framework ViewGroups, though this is not |
| 1164 | // necessarily true of all ViewGroups) then we expect it to throw |
| 1165 | // a runtime exception. |
| 1166 | // We catch this exception and set localParams accordingly: true |
| 1167 | // means we successfully loaded layout params from the <include> |
| 1168 | // tag, false means we need to rely on the included layout params. |
| 1169 | ViewGroup.LayoutParams params = null; |
| 1170 | try { |
| 1171 | params = group.generateLayoutParams(attrs); |
| 1172 | } catch (RuntimeException e) { |
| 1173 | // Ignore, just fail over to child attrs. |
| 1174 | } |
| 1175 | if (params == null) { |
| 1176 | params = group.generateLayoutParams(childAttrs); |
| 1177 | } |
| 1178 | view.setLayoutParams(params); |
| 1179 | |
| 1180 | // Inflate all children. |
| 1181 | rInflateChildren(childParser, view, childAttrs, true); |
| 1182 | |
| 1183 | if (id != View.NO_ID) { |
| 1184 | view.setId(id); |
| 1185 | } |
| 1186 | |
| 1187 | switch (visibility) { |
| 1188 | case 0: |
| 1189 | view.setVisibility(View.VISIBLE); |
| 1190 | break; |
| 1191 | case 1: |
| 1192 | view.setVisibility(View.INVISIBLE); |
| 1193 | break; |
| 1194 | case 2: |
| 1195 | view.setVisibility(View.GONE); |
| 1196 | break; |
| 1197 | } |
| 1198 | |
| 1199 | group.addView(view); |
| 1200 | } |
| 1201 | } finally { |
| 1202 | childParser.close(); |
Eric Holk | bfc6870 | 2019-01-02 11:11:24 -0800 | [diff] [blame] | 1203 | } |
Eric Holk | bfc6870 | 2019-01-02 11:11:24 -0800 | [diff] [blame] | 1204 | } |
Alan Viverette | 6194d72 | 2015-03-20 15:49:06 -0700 | [diff] [blame] | 1205 | LayoutInflater.consumeChildElements(parser); |
| 1206 | } |
| 1207 | |
| 1208 | /** |
| 1209 | * <strong>Note:</strong> default visibility so that |
| 1210 | * LayoutInflater_Delegate can call it. |
| 1211 | */ |
| 1212 | final static void consumeChildElements(XmlPullParser parser) |
| 1213 | throws XmlPullParserException, IOException { |
| 1214 | int type; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1215 | final int currentDepth = parser.getDepth(); |
| 1216 | while (((type = parser.next()) != XmlPullParser.END_TAG || |
| 1217 | parser.getDepth() > currentDepth) && type != XmlPullParser.END_DOCUMENT) { |
| 1218 | // Empty |
| 1219 | } |
Romain Guy | 9c1223a | 2011-05-17 14:25:49 -0700 | [diff] [blame] | 1220 | } |
| 1221 | |
| 1222 | private static class BlinkLayout extends FrameLayout { |
| 1223 | private static final int MESSAGE_BLINK = 0x42; |
| 1224 | private static final int BLINK_DELAY = 500; |
| 1225 | |
| 1226 | private boolean mBlink; |
| 1227 | private boolean mBlinkState; |
| 1228 | private final Handler mHandler; |
| 1229 | |
| 1230 | public BlinkLayout(Context context, AttributeSet attrs) { |
| 1231 | super(context, attrs); |
| 1232 | mHandler = new Handler(new Handler.Callback() { |
| 1233 | @Override |
| 1234 | public boolean handleMessage(Message msg) { |
| 1235 | if (msg.what == MESSAGE_BLINK) { |
| 1236 | if (mBlink) { |
| 1237 | mBlinkState = !mBlinkState; |
| 1238 | makeBlink(); |
| 1239 | } |
| 1240 | invalidate(); |
| 1241 | return true; |
| 1242 | } |
| 1243 | return false; |
| 1244 | } |
| 1245 | }); |
| 1246 | } |
| 1247 | |
| 1248 | private void makeBlink() { |
| 1249 | Message message = mHandler.obtainMessage(MESSAGE_BLINK); |
| 1250 | mHandler.sendMessageDelayed(message, BLINK_DELAY); |
| 1251 | } |
| 1252 | |
| 1253 | @Override |
| 1254 | protected void onAttachedToWindow() { |
| 1255 | super.onAttachedToWindow(); |
| 1256 | |
| 1257 | mBlink = true; |
| 1258 | mBlinkState = true; |
| 1259 | |
| 1260 | makeBlink(); |
| 1261 | } |
| 1262 | |
| 1263 | @Override |
| 1264 | protected void onDetachedFromWindow() { |
| 1265 | super.onDetachedFromWindow(); |
| 1266 | |
| 1267 | mBlink = false; |
| 1268 | mBlinkState = true; |
| 1269 | |
| 1270 | mHandler.removeMessages(MESSAGE_BLINK); |
| 1271 | } |
| 1272 | |
| 1273 | @Override |
| 1274 | protected void dispatchDraw(Canvas canvas) { |
| 1275 | if (mBlinkState) { |
| 1276 | super.dispatchDraw(canvas); |
| 1277 | } |
| 1278 | } |
| 1279 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1280 | } |