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