blob: 1a07aee5d482c0eb2fee997009f4d9e1f1a3c48a [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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
17package android.view;
18
Alan Viverettee8489cd2015-02-03 14:40:45 -080019import com.android.internal.R;
Alan Viverette0810b632014-05-01 14:42:56 -070020
Gilles Debunne30301932010-06-16 18:32:00 -070021import org.xmlpull.v1.XmlPullParser;
22import org.xmlpull.v1.XmlPullParserException;
23
Tor Norbye7b9c9122013-05-30 16:48:33 -070024import android.annotation.LayoutRes;
Alan Viverettee8489cd2015-02-03 14:40:45 -080025import android.annotation.Nullable;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026import android.content.Context;
Alan Viverette0810b632014-05-01 14:42:56 -070027import android.content.res.Resources;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028import android.content.res.TypedArray;
29import android.content.res.XmlResourceParser;
Alan Viverettee8489cd2015-02-03 14:40:45 -080030import android.graphics.Canvas;
31import android.os.Handler;
32import android.os.Message;
33import android.os.Trace;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034import android.util.AttributeSet;
Alan Viverette0810b632014-05-01 14:42:56 -070035import android.util.Log;
Alan Viverettee8489cd2015-02-03 14:40:45 -080036import android.util.TypedValue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037import android.util.Xml;
Alan Viverettee8489cd2015-02-03 14:40:45 -080038import android.widget.FrameLayout;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040import java.io.IOException;
41import java.lang.reflect.Constructor;
42import java.util.HashMap;
43
44/**
Scott Main93dc6422012-02-24 12:04:06 -080045 * Instantiates a layout XML file into its corresponding {@link android.view.View}
46 * objects. It is never used directly. Instead, use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047 * {@link android.app.Activity#getLayoutInflater()} or
48 * {@link Context#getSystemService} to retrieve a standard LayoutInflater instance
49 * that is already hooked up to the current context and correctly configured
50 * for the device you are running on. For example:
51 *
52 * <pre>LayoutInflater inflater = (LayoutInflater)context.getSystemService
Christian Mehlmauerbd6fda12011-01-08 18:22:20 +010053 * (Context.LAYOUT_INFLATER_SERVICE);</pre>
Dave Burke579e1402012-10-18 20:41:55 -070054 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055 * <p>
56 * To create a new LayoutInflater with an additional {@link Factory} for your
57 * own views, you can use {@link #cloneInContext} to clone an existing
58 * ViewFactory, and then call {@link #setFactory} on it to include your
59 * Factory.
Dave Burke579e1402012-10-18 20:41:55 -070060 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061 * <p>
62 * For performance reasons, view inflation relies heavily on pre-processing of
63 * XML files that is done at build time. Therefore, it is not currently possible
64 * to use LayoutInflater with an XmlPullParser over a plain XML file at runtime;
65 * it only works with an XmlPullParser returned from a compiled resource
66 * (R.<em>something</em> file.)
Dave Burke579e1402012-10-18 20:41:55 -070067 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068 * @see Context#getSystemService
69 */
70public abstract class LayoutInflater {
Alan Viverette33e3cda2014-12-17 15:43:29 -080071
Alan Viverette0810b632014-05-01 14:42:56 -070072 private static final String TAG = LayoutInflater.class.getSimpleName();
73 private static final boolean DEBUG = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074
75 /**
76 * This field should be made private, so it is hidden from the SDK.
77 * {@hide}
78 */
79 protected final Context mContext;
80
81 // these are optional, set by the caller
82 private boolean mFactorySet;
83 private Factory mFactory;
Dianne Hackborn625ac272010-09-17 18:29:22 -070084 private Factory2 mFactory2;
Dianne Hackborn420829e2011-01-28 11:30:35 -080085 private Factory2 mPrivateFactory;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086 private Filter mFilter;
87
Xavier Ducrohet7f9f99ea2011-08-11 10:16:17 -070088 final Object[] mConstructorArgs = new Object[2];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089
Xavier Ducrohet7f9f99ea2011-08-11 10:16:17 -070090 static final Class<?>[] mConstructorSignature = new Class[] {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091 Context.class, AttributeSet.class};
92
Gilles Debunne30301932010-06-16 18:32:00 -070093 private static final HashMap<String, Constructor<? extends View>> sConstructorMap =
94 new HashMap<String, Constructor<? extends View>>();
Dave Burke579e1402012-10-18 20:41:55 -070095
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096 private HashMap<String, Boolean> mFilterMap;
97
Alan Viverette33e3cda2014-12-17 15:43:29 -080098 private TypedValue mTempValue;
99
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100 private static final String TAG_MERGE = "merge";
101 private static final String TAG_INCLUDE = "include";
Romain Guy9c1223a2011-05-17 14:25:49 -0700102 private static final String TAG_1995 = "blink";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103 private static final String TAG_REQUEST_FOCUS = "requestFocus";
Alan Viverette451a3412014-02-11 18:08:46 -0800104 private static final String TAG_TAG = "tag";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105
Alan Viverette33e3cda2014-12-17 15:43:29 -0800106 private static final String ATTR_LAYOUT = "layout";
107
Alan Viveretteef259e42014-01-24 17:20:12 -0800108 private static final int[] ATTRS_THEME = new int[] {
109 com.android.internal.R.attr.theme };
Alan Viverette24927f22014-01-07 17:28:48 -0800110
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800111 /**
112 * Hook to allow clients of the LayoutInflater to restrict the set of Views that are allowed
113 * to be inflated.
Dave Burke579e1402012-10-18 20:41:55 -0700114 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800115 */
116 public interface Filter {
117 /**
118 * Hook to allow clients of the LayoutInflater to restrict the set of Views
119 * that are allowed to be inflated.
Dave Burke579e1402012-10-18 20:41:55 -0700120 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800121 * @param clazz The class object for the View that is about to be inflated
Dave Burke579e1402012-10-18 20:41:55 -0700122 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800123 * @return True if this class is allowed to be inflated, or false otherwise
124 */
Gilles Debunnee6ac8b92010-06-17 10:55:04 -0700125 @SuppressWarnings("unchecked")
126 boolean onLoadClass(Class clazz);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800127 }
Dave Burke579e1402012-10-18 20:41:55 -0700128
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800129 public interface Factory {
130 /**
131 * Hook you can supply that is called when inflating from a LayoutInflater.
132 * You can use this to customize the tag names available in your XML
133 * layout files.
Dave Burke579e1402012-10-18 20:41:55 -0700134 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135 * <p>
136 * Note that it is good practice to prefix these custom names with your
137 * package (i.e., com.coolcompany.apps) to avoid conflicts with system
138 * names.
Dave Burke579e1402012-10-18 20:41:55 -0700139 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800140 * @param name Tag name to be inflated.
141 * @param context The context the view is being created in.
142 * @param attrs Inflation attributes as specified in XML file.
Dave Burke579e1402012-10-18 20:41:55 -0700143 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800144 * @return View Newly created view. Return null for the default
145 * behavior.
146 */
147 public View onCreateView(String name, Context context, AttributeSet attrs);
148 }
149
Dianne Hackborn625ac272010-09-17 18:29:22 -0700150 public interface Factory2 extends Factory {
151 /**
152 * Version of {@link #onCreateView(String, Context, AttributeSet)}
153 * that also supplies the parent that the view created view will be
154 * placed in.
155 *
156 * @param parent The parent that the created view will be placed
157 * in; <em>note that this may be null</em>.
158 * @param name Tag name to be inflated.
159 * @param context The context the view is being created in.
160 * @param attrs Inflation attributes as specified in XML file.
161 *
162 * @return View Newly created view. Return null for the default
163 * behavior.
164 */
165 public View onCreateView(View parent, String name, Context context, AttributeSet attrs);
166 }
167
168 private static class FactoryMerger implements Factory2 {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800169 private final Factory mF1, mF2;
Dianne Hackborn625ac272010-09-17 18:29:22 -0700170 private final Factory2 mF12, mF22;
Dave Burke579e1402012-10-18 20:41:55 -0700171
Dianne Hackborn625ac272010-09-17 18:29:22 -0700172 FactoryMerger(Factory f1, Factory2 f12, Factory f2, Factory2 f22) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800173 mF1 = f1;
174 mF2 = f2;
Dianne Hackborn625ac272010-09-17 18:29:22 -0700175 mF12 = f12;
176 mF22 = f22;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800177 }
Dave Burke579e1402012-10-18 20:41:55 -0700178
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800179 public View onCreateView(String name, Context context, AttributeSet attrs) {
180 View v = mF1.onCreateView(name, context, attrs);
181 if (v != null) return v;
182 return mF2.onCreateView(name, context, attrs);
183 }
Dianne Hackborn625ac272010-09-17 18:29:22 -0700184
185 public View onCreateView(View parent, String name, Context context, AttributeSet attrs) {
186 View v = mF12 != null ? mF12.onCreateView(parent, name, context, attrs)
187 : mF1.onCreateView(name, context, attrs);
188 if (v != null) return v;
189 return mF22 != null ? mF22.onCreateView(parent, name, context, attrs)
190 : mF2.onCreateView(name, context, attrs);
191 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800192 }
Dave Burke579e1402012-10-18 20:41:55 -0700193
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800194 /**
195 * Create a new LayoutInflater instance associated with a particular Context.
196 * Applications will almost always want to use
197 * {@link Context#getSystemService Context.getSystemService()} to retrieve
198 * the standard {@link Context#LAYOUT_INFLATER_SERVICE Context.INFLATER_SERVICE}.
Dave Burke579e1402012-10-18 20:41:55 -0700199 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800200 * @param context The Context in which this LayoutInflater will create its
201 * Views; most importantly, this supplies the theme from which the default
202 * values for their attributes are retrieved.
203 */
204 protected LayoutInflater(Context context) {
205 mContext = context;
206 }
207
208 /**
209 * Create a new LayoutInflater instance that is a copy of an existing
210 * LayoutInflater, optionally with its Context changed. For use in
211 * implementing {@link #cloneInContext}.
Dave Burke579e1402012-10-18 20:41:55 -0700212 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800213 * @param original The original LayoutInflater to copy.
214 * @param newContext The new Context to use.
215 */
216 protected LayoutInflater(LayoutInflater original, Context newContext) {
217 mContext = newContext;
218 mFactory = original.mFactory;
Dianne Hackborn625ac272010-09-17 18:29:22 -0700219 mFactory2 = original.mFactory2;
Dianne Hackborn420829e2011-01-28 11:30:35 -0800220 mPrivateFactory = original.mPrivateFactory;
Dan Sandler0c7bb332014-09-18 22:11:18 -0400221 setFilter(original.mFilter);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800222 }
Dave Burke579e1402012-10-18 20:41:55 -0700223
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800224 /**
225 * Obtains the LayoutInflater from the given context.
226 */
227 public static LayoutInflater from(Context context) {
228 LayoutInflater LayoutInflater =
229 (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
230 if (LayoutInflater == null) {
231 throw new AssertionError("LayoutInflater not found.");
232 }
233 return LayoutInflater;
234 }
235
236 /**
237 * Create a copy of the existing LayoutInflater object, with the copy
238 * pointing to a different Context than the original. This is used by
239 * {@link ContextThemeWrapper} to create a new LayoutInflater to go along
240 * with the new Context theme.
Dave Burke579e1402012-10-18 20:41:55 -0700241 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800242 * @param newContext The new Context to associate with the new LayoutInflater.
243 * May be the same as the original Context if desired.
Dave Burke579e1402012-10-18 20:41:55 -0700244 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800245 * @return Returns a brand spanking new LayoutInflater object associated with
246 * the given Context.
247 */
248 public abstract LayoutInflater cloneInContext(Context newContext);
Dave Burke579e1402012-10-18 20:41:55 -0700249
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800250 /**
251 * Return the context we are running in, for access to resources, class
252 * loader, etc.
253 */
254 public Context getContext() {
255 return mContext;
256 }
257
258 /**
Dianne Hackborn625ac272010-09-17 18:29:22 -0700259 * Return the current {@link Factory} (or null). This is called on each element
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800260 * name. If the factory returns a View, add that to the hierarchy. If it
261 * returns null, proceed to call onCreateView(name).
262 */
263 public final Factory getFactory() {
264 return mFactory;
265 }
266
267 /**
Dianne Hackborn625ac272010-09-17 18:29:22 -0700268 * Return the current {@link Factory2}. Returns null if no factory is set
269 * or the set factory does not implement the {@link Factory2} interface.
270 * This is called on each element
271 * name. If the factory returns a View, add that to the hierarchy. If it
272 * returns null, proceed to call onCreateView(name).
273 */
274 public final Factory2 getFactory2() {
275 return mFactory2;
276 }
277
278 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800279 * Attach a custom Factory interface for creating views while using
280 * this LayoutInflater. This must not be null, and can only be set once;
281 * after setting, you can not change the factory. This is
282 * called on each element name as the xml is parsed. If the factory returns
283 * a View, that is added to the hierarchy. If it returns null, the next
284 * factory default {@link #onCreateView} method is called.
Dave Burke579e1402012-10-18 20:41:55 -0700285 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800286 * <p>If you have an existing
287 * LayoutInflater and want to add your own factory to it, use
288 * {@link #cloneInContext} to clone the existing instance and then you
289 * can use this function (once) on the returned new instance. This will
290 * merge your own factory with whatever factory the original instance is
291 * using.
292 */
293 public void setFactory(Factory factory) {
294 if (mFactorySet) {
295 throw new IllegalStateException("A factory has already been set on this LayoutInflater");
296 }
297 if (factory == null) {
298 throw new NullPointerException("Given factory can not be null");
299 }
300 mFactorySet = true;
301 if (mFactory == null) {
302 mFactory = factory;
303 } else {
Dianne Hackborn625ac272010-09-17 18:29:22 -0700304 mFactory = new FactoryMerger(factory, null, mFactory, mFactory2);
305 }
306 }
307
308 /**
309 * Like {@link #setFactory}, but allows you to set a {@link Factory2}
310 * interface.
311 */
312 public void setFactory2(Factory2 factory) {
313 if (mFactorySet) {
314 throw new IllegalStateException("A factory has already been set on this LayoutInflater");
315 }
316 if (factory == null) {
317 throw new NullPointerException("Given factory can not be null");
318 }
319 mFactorySet = true;
320 if (mFactory == null) {
321 mFactory = mFactory2 = factory;
322 } else {
Adam Powell371a8092014-06-20 12:51:12 -0700323 mFactory = mFactory2 = new FactoryMerger(factory, factory, mFactory, mFactory2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800324 }
325 }
326
327 /**
Dianne Hackborn420829e2011-01-28 11:30:35 -0800328 * @hide for use by framework
329 */
330 public void setPrivateFactory(Factory2 factory) {
Adam Powell371a8092014-06-20 12:51:12 -0700331 if (mPrivateFactory == null) {
332 mPrivateFactory = factory;
333 } else {
334 mPrivateFactory = new FactoryMerger(factory, factory, mPrivateFactory, mPrivateFactory);
335 }
Dianne Hackborn420829e2011-01-28 11:30:35 -0800336 }
337
338 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800339 * @return The {@link Filter} currently used by this LayoutInflater to restrict the set of Views
340 * that are allowed to be inflated.
341 */
342 public Filter getFilter() {
343 return mFilter;
344 }
Dave Burke579e1402012-10-18 20:41:55 -0700345
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800346 /**
347 * Sets the {@link Filter} to by this LayoutInflater. If a view is attempted to be inflated
348 * which is not allowed by the {@link Filter}, the {@link #inflate(int, ViewGroup)} call will
349 * throw an {@link InflateException}. This filter will replace any previous filter set on this
350 * LayoutInflater.
Dave Burke579e1402012-10-18 20:41:55 -0700351 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800352 * @param filter The Filter which restricts the set of Views that are allowed to be inflated.
353 * This filter will replace any previous filter set on this LayoutInflater.
354 */
355 public void setFilter(Filter filter) {
356 mFilter = filter;
357 if (filter != null) {
358 mFilterMap = new HashMap<String, Boolean>();
359 }
360 }
361
362 /**
363 * Inflate a new view hierarchy from the specified xml resource. Throws
364 * {@link InflateException} if there is an error.
Dave Burke579e1402012-10-18 20:41:55 -0700365 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800366 * @param resource ID for an XML layout resource to load (e.g.,
367 * <code>R.layout.main_page</code>)
368 * @param root Optional view to be the parent of the generated hierarchy.
369 * @return The root View of the inflated hierarchy. If root was supplied,
370 * this is the root View; otherwise it is the root of the inflated
371 * XML file.
372 */
Tor Norbye7b9c9122013-05-30 16:48:33 -0700373 public View inflate(@LayoutRes int resource, @Nullable ViewGroup root) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800374 return inflate(resource, root, root != null);
375 }
376
377 /**
378 * Inflate a new view hierarchy from the specified xml node. Throws
379 * {@link InflateException} if there is an error. *
380 * <p>
381 * <em><strong>Important</strong></em>&nbsp;&nbsp;&nbsp;For performance
382 * reasons, view inflation relies heavily on pre-processing of XML files
383 * that is done at build time. Therefore, it is not currently possible to
384 * use LayoutInflater with an XmlPullParser over a plain XML file at runtime.
Dave Burke579e1402012-10-18 20:41:55 -0700385 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800386 * @param parser XML dom node containing the description of the view
387 * hierarchy.
388 * @param root Optional view to be the parent of the generated hierarchy.
389 * @return The root View of the inflated hierarchy. If root was supplied,
390 * this is the root View; otherwise it is the root of the inflated
391 * XML file.
392 */
Scott Kennedydb5fd422015-01-15 11:56:33 -0800393 public View inflate(XmlPullParser parser, @Nullable ViewGroup root) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800394 return inflate(parser, root, root != null);
395 }
396
397 /**
398 * Inflate a new view hierarchy from the specified xml resource. Throws
399 * {@link InflateException} if there is an error.
Dave Burke579e1402012-10-18 20:41:55 -0700400 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800401 * @param resource ID for an XML layout resource to load (e.g.,
402 * <code>R.layout.main_page</code>)
403 * @param root Optional view to be the parent of the generated hierarchy (if
404 * <em>attachToRoot</em> is true), or else simply an object that
405 * provides a set of LayoutParams values for root of the returned
406 * hierarchy (if <em>attachToRoot</em> is false.)
407 * @param attachToRoot Whether the inflated hierarchy should be attached to
408 * the root parameter? If false, root is only used to create the
409 * correct subclass of LayoutParams for the root view in the XML.
410 * @return The root View of the inflated hierarchy. If root was supplied and
411 * attachToRoot is true, this is root; otherwise it is the root of
412 * the inflated XML file.
413 */
Tor Norbye7b9c9122013-05-30 16:48:33 -0700414 public View inflate(@LayoutRes int resource, @Nullable ViewGroup root, boolean attachToRoot) {
Alan Viverette0810b632014-05-01 14:42:56 -0700415 final Resources res = getContext().getResources();
416 if (DEBUG) {
417 Log.d(TAG, "INFLATING from resource: \"" + res.getResourceName(resource) + "\" ("
418 + Integer.toHexString(resource) + ")");
419 }
420
421 final XmlResourceParser parser = res.getLayout(resource);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800422 try {
423 return inflate(parser, root, attachToRoot);
424 } finally {
425 parser.close();
426 }
427 }
428
429 /**
430 * Inflate a new view hierarchy from the specified XML node. Throws
431 * {@link InflateException} if there is an error.
432 * <p>
433 * <em><strong>Important</strong></em>&nbsp;&nbsp;&nbsp;For performance
434 * reasons, view inflation relies heavily on pre-processing of XML files
435 * that is done at build time. Therefore, it is not currently possible to
436 * use LayoutInflater with an XmlPullParser over a plain XML file at runtime.
Dave Burke579e1402012-10-18 20:41:55 -0700437 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800438 * @param parser XML dom node containing the description of the view
439 * hierarchy.
440 * @param root Optional view to be the parent of the generated hierarchy (if
441 * <em>attachToRoot</em> is true), or else simply an object that
442 * provides a set of LayoutParams values for root of the returned
443 * hierarchy (if <em>attachToRoot</em> is false.)
444 * @param attachToRoot Whether the inflated hierarchy should be attached to
445 * the root parameter? If false, root is only used to create the
446 * correct subclass of LayoutParams for the root view in the XML.
447 * @return The root View of the inflated hierarchy. If root was supplied and
448 * attachToRoot is true, this is root; otherwise it is the root of
449 * the inflated XML file.
450 */
Scott Kennedydb5fd422015-01-15 11:56:33 -0800451 public View inflate(XmlPullParser parser, @Nullable ViewGroup root, boolean attachToRoot) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800452 synchronized (mConstructorArgs) {
Romain Guy09f7b932013-04-10 11:42:44 -0700453 Trace.traceBegin(Trace.TRACE_TAG_VIEW, "inflate");
454
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800455 final AttributeSet attrs = Xml.asAttributeSet(parser);
Dianne Hackborn9dae48e2010-08-26 10:20:01 -0700456 Context lastContext = (Context)mConstructorArgs[0];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800457 mConstructorArgs[0] = mContext;
458 View result = root;
459
460 try {
461 // Look for the root node.
462 int type;
463 while ((type = parser.next()) != XmlPullParser.START_TAG &&
464 type != XmlPullParser.END_DOCUMENT) {
465 // Empty
466 }
467
468 if (type != XmlPullParser.START_TAG) {
469 throw new InflateException(parser.getPositionDescription()
470 + ": No start tag found!");
471 }
472
473 final String name = parser.getName();
Dave Burke579e1402012-10-18 20:41:55 -0700474
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800475 if (DEBUG) {
476 System.out.println("**************************");
477 System.out.println("Creating root view: "
478 + name);
479 System.out.println("**************************");
480 }
481
482 if (TAG_MERGE.equals(name)) {
483 if (root == null || !attachToRoot) {
484 throw new InflateException("<merge /> can be used only with a valid "
485 + "ViewGroup root and attachToRoot=true");
486 }
487
Alan Viverette24927f22014-01-07 17:28:48 -0800488 rInflate(parser, root, attrs, false, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800489 } else {
490 // Temp is the root view that was found in the xml
Alan Viverette24927f22014-01-07 17:28:48 -0800491 final View temp = createViewFromTag(root, name, attrs, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800492
493 ViewGroup.LayoutParams params = null;
494
495 if (root != null) {
496 if (DEBUG) {
497 System.out.println("Creating params from root: " +
498 root);
499 }
500 // Create layout params that match root, if supplied
501 params = root.generateLayoutParams(attrs);
502 if (!attachToRoot) {
503 // Set the layout params for temp if we are not
504 // attaching. (If we are, we use addView, below)
505 temp.setLayoutParams(params);
506 }
507 }
508
509 if (DEBUG) {
510 System.out.println("-----> start inflating children");
511 }
512 // Inflate all children under temp
Alan Viverette24927f22014-01-07 17:28:48 -0800513 rInflate(parser, temp, attrs, true, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800514 if (DEBUG) {
515 System.out.println("-----> done inflating children");
516 }
517
518 // We are supposed to attach all the views we found (int temp)
519 // to root. Do that now.
520 if (root != null && attachToRoot) {
521 root.addView(temp, params);
522 }
523
524 // Decide whether to return the root that was passed in or the
525 // top view found in xml.
526 if (root == null || !attachToRoot) {
527 result = temp;
528 }
529 }
530
531 } catch (XmlPullParserException e) {
532 InflateException ex = new InflateException(e.getMessage());
533 ex.initCause(e);
534 throw ex;
Alan Viverette93795052015-03-09 15:32:50 -0700535 } catch (Exception e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800536 InflateException ex = new InflateException(
537 parser.getPositionDescription()
Alan Viverette93795052015-03-09 15:32:50 -0700538 + ": " + e.getMessage());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800539 ex.initCause(e);
540 throw ex;
Dianne Hackborn9dae48e2010-08-26 10:20:01 -0700541 } finally {
542 // Don't retain static reference on context.
543 mConstructorArgs[0] = lastContext;
544 mConstructorArgs[1] = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800545 }
546
Romain Guy09f7b932013-04-10 11:42:44 -0700547 Trace.traceEnd(Trace.TRACE_TAG_VIEW);
548
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800549 return result;
550 }
551 }
552
553 /**
554 * Low-level function for instantiating a view by name. This attempts to
555 * instantiate a view class of the given <var>name</var> found in this
556 * LayoutInflater's ClassLoader.
Dave Burke579e1402012-10-18 20:41:55 -0700557 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800558 * <p>
559 * There are two things that can happen in an error case: either the
560 * exception describing the error will be thrown, or a null will be
561 * returned. You must deal with both possibilities -- the former will happen
562 * the first time createView() is called for a class of a particular name,
563 * the latter every time there-after for that class name.
Dave Burke579e1402012-10-18 20:41:55 -0700564 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800565 * @param name The full name of the class to be instantiated.
566 * @param attrs The XML attributes supplied for this instance.
Dave Burke579e1402012-10-18 20:41:55 -0700567 *
Gilles Debunne30301932010-06-16 18:32:00 -0700568 * @return View The newly instantiated view, or null.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800569 */
570 public final View createView(String name, String prefix, AttributeSet attrs)
571 throws ClassNotFoundException, InflateException {
Gilles Debunne30301932010-06-16 18:32:00 -0700572 Constructor<? extends View> constructor = sConstructorMap.get(name);
573 Class<? extends View> clazz = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800574
575 try {
Romain Guy09f7b932013-04-10 11:42:44 -0700576 Trace.traceBegin(Trace.TRACE_TAG_VIEW, name);
577
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800578 if (constructor == null) {
579 // Class not found in the cache, see if it's real, and try to add it
Romain Guyd03b8802009-09-16 14:36:16 -0700580 clazz = mContext.getClassLoader().loadClass(
Gilles Debunne30301932010-06-16 18:32:00 -0700581 prefix != null ? (prefix + name) : name).asSubclass(View.class);
Dave Burke579e1402012-10-18 20:41:55 -0700582
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800583 if (mFilter != null && clazz != null) {
584 boolean allowed = mFilter.onLoadClass(clazz);
585 if (!allowed) {
586 failNotAllowed(name, prefix, attrs);
587 }
588 }
589 constructor = clazz.getConstructor(mConstructorSignature);
590 sConstructorMap.put(name, constructor);
591 } else {
592 // If we have a filter, apply it to cached constructor
593 if (mFilter != null) {
594 // Have we seen this name before?
595 Boolean allowedState = mFilterMap.get(name);
596 if (allowedState == null) {
597 // New class -- remember whether it is allowed
Romain Guyd03b8802009-09-16 14:36:16 -0700598 clazz = mContext.getClassLoader().loadClass(
Gilles Debunne30301932010-06-16 18:32:00 -0700599 prefix != null ? (prefix + name) : name).asSubclass(View.class);
Dave Burke579e1402012-10-18 20:41:55 -0700600
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800601 boolean allowed = clazz != null && mFilter.onLoadClass(clazz);
602 mFilterMap.put(name, allowed);
603 if (!allowed) {
604 failNotAllowed(name, prefix, attrs);
605 }
606 } else if (allowedState.equals(Boolean.FALSE)) {
607 failNotAllowed(name, prefix, attrs);
608 }
609 }
610 }
611
612 Object[] args = mConstructorArgs;
613 args[1] = attrs;
Jeff Sharkeyb27b7a12012-04-02 21:07:29 -0700614
Jeff Haoe3abd2c2014-03-28 11:33:53 -0700615 constructor.setAccessible(true);
Jeff Sharkeyb27b7a12012-04-02 21:07:29 -0700616 final View view = constructor.newInstance(args);
617 if (view instanceof ViewStub) {
Alan Viverettea9ddb8d2014-09-17 18:14:32 -0700618 // Use the same context when inflating ViewStub later.
Jeff Sharkeyb27b7a12012-04-02 21:07:29 -0700619 final ViewStub viewStub = (ViewStub) view;
Alan Viverettea9ddb8d2014-09-17 18:14:32 -0700620 viewStub.setLayoutInflater(cloneInContext((Context) args[0]));
Jeff Sharkeyb27b7a12012-04-02 21:07:29 -0700621 }
622 return view;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800623
624 } catch (NoSuchMethodException e) {
625 InflateException ie = new InflateException(attrs.getPositionDescription()
626 + ": Error inflating class "
627 + (prefix != null ? (prefix + name) : name));
628 ie.initCause(e);
629 throw ie;
630
Gilles Debunne30301932010-06-16 18:32:00 -0700631 } catch (ClassCastException e) {
632 // If loaded class is not a View subclass
633 InflateException ie = new InflateException(attrs.getPositionDescription()
634 + ": Class is not a View "
635 + (prefix != null ? (prefix + name) : name));
636 ie.initCause(e);
637 throw ie;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800638 } catch (ClassNotFoundException e) {
639 // If loadClass fails, we should propagate the exception.
640 throw e;
641 } catch (Exception e) {
642 InflateException ie = new InflateException(attrs.getPositionDescription()
643 + ": Error inflating class "
Romain Guyd03b8802009-09-16 14:36:16 -0700644 + (clazz == null ? "<unknown>" : clazz.getName()));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800645 ie.initCause(e);
646 throw ie;
Romain Guy09f7b932013-04-10 11:42:44 -0700647 } finally {
648 Trace.traceEnd(Trace.TRACE_TAG_VIEW);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800649 }
650 }
651
652 /**
Gilles Debunne30301932010-06-16 18:32:00 -0700653 * Throw an exception because the specified class is not allowed to be inflated.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800654 */
655 private void failNotAllowed(String name, String prefix, AttributeSet attrs) {
Romain Guy9c1223a2011-05-17 14:25:49 -0700656 throw new InflateException(attrs.getPositionDescription()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800657 + ": Class not allowed to be inflated "
658 + (prefix != null ? (prefix + name) : name));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800659 }
660
661 /**
662 * This routine is responsible for creating the correct subclass of View
663 * given the xml element name. Override it to handle custom view objects. If
664 * you override this in your subclass be sure to call through to
665 * super.onCreateView(name) for names you do not recognize.
Dave Burke579e1402012-10-18 20:41:55 -0700666 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800667 * @param name The fully qualified class name of the View to be create.
668 * @param attrs An AttributeSet of attributes to apply to the View.
Dave Burke579e1402012-10-18 20:41:55 -0700669 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800670 * @return View The View created.
671 */
672 protected View onCreateView(String name, AttributeSet attrs)
673 throws ClassNotFoundException {
674 return createView(name, "android.view.", attrs);
675 }
676
Dianne Hackborn625ac272010-09-17 18:29:22 -0700677 /**
678 * Version of {@link #onCreateView(String, AttributeSet)} that also
Chet Haase430742f2013-04-12 11:18:36 -0700679 * takes the future parent of the view being constructed. The default
Dianne Hackborn625ac272010-09-17 18:29:22 -0700680 * implementation simply calls {@link #onCreateView(String, AttributeSet)}.
681 *
682 * @param parent The future parent of the returned view. <em>Note that
683 * this may be null.</em>
684 * @param name The fully qualified class name of the View to be create.
685 * @param attrs An AttributeSet of attributes to apply to the View.
686 *
687 * @return View The View created.
688 */
689 protected View onCreateView(View parent, String name, AttributeSet attrs)
690 throws ClassNotFoundException {
691 return onCreateView(name, attrs);
692 }
693
Alan Viverette24927f22014-01-07 17:28:48 -0800694 /**
695 * Creates a view from a tag name using the supplied attribute set.
696 * <p>
697 * If {@code inheritContext} is true and the parent is non-null, the view
698 * will be inflated in parent view's context. If the view specifies a
699 * &lt;theme&gt; attribute, the inflation context will be wrapped with the
700 * specified theme.
701 * <p>
702 * Note: Default visibility so the BridgeInflater can override it.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800703 */
Alan Viverette24927f22014-01-07 17:28:48 -0800704 View createViewFromTag(View parent, String name, AttributeSet attrs, boolean inheritContext) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800705 if (name.equals("view")) {
706 name = attrs.getAttributeValue(null, "class");
707 }
708
Alan Viverette24927f22014-01-07 17:28:48 -0800709 Context viewContext;
710 if (parent != null && inheritContext) {
711 viewContext = parent.getContext();
712 } else {
713 viewContext = mContext;
714 }
715
716 // Apply a theme wrapper, if requested.
Alan Viveretteef259e42014-01-24 17:20:12 -0800717 final TypedArray ta = viewContext.obtainStyledAttributes(attrs, ATTRS_THEME);
718 final int themeResId = ta.getResourceId(0, 0);
Alan Viverette24927f22014-01-07 17:28:48 -0800719 if (themeResId != 0) {
720 viewContext = new ContextThemeWrapper(viewContext, themeResId);
721 }
Alan Viveretteef259e42014-01-24 17:20:12 -0800722 ta.recycle();
Alan Viverette24927f22014-01-07 17:28:48 -0800723
724 if (name.equals(TAG_1995)) {
725 // Let's party like it's 1995!
726 return new BlinkLayout(viewContext, attrs);
727 }
728
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800729 if (DEBUG) System.out.println("******** Creating view: " + name);
730
731 try {
Dianne Hackborn625ac272010-09-17 18:29:22 -0700732 View view;
Alan Viverette24927f22014-01-07 17:28:48 -0800733 if (mFactory2 != null) {
734 view = mFactory2.onCreateView(parent, name, viewContext, attrs);
735 } else if (mFactory != null) {
736 view = mFactory.onCreateView(name, viewContext, attrs);
737 } else {
738 view = null;
739 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800740
Dianne Hackborn420829e2011-01-28 11:30:35 -0800741 if (view == null && mPrivateFactory != null) {
Alan Viverette24927f22014-01-07 17:28:48 -0800742 view = mPrivateFactory.onCreateView(parent, name, viewContext, attrs);
Dianne Hackborn420829e2011-01-28 11:30:35 -0800743 }
Alan Viverette24927f22014-01-07 17:28:48 -0800744
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800745 if (view == null) {
Alan Viverette24927f22014-01-07 17:28:48 -0800746 final Object lastContext = mConstructorArgs[0];
747 mConstructorArgs[0] = viewContext;
748 try {
749 if (-1 == name.indexOf('.')) {
750 view = onCreateView(parent, name, attrs);
751 } else {
752 view = createView(name, null, attrs);
753 }
754 } finally {
755 mConstructorArgs[0] = lastContext;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800756 }
757 }
758
759 if (DEBUG) System.out.println("Created view is: " + view);
760 return view;
761
762 } catch (InflateException e) {
763 throw e;
764
765 } catch (ClassNotFoundException e) {
766 InflateException ie = new InflateException(attrs.getPositionDescription()
767 + ": Error inflating class " + name);
768 ie.initCause(e);
769 throw ie;
770
771 } catch (Exception e) {
772 InflateException ie = new InflateException(attrs.getPositionDescription()
773 + ": Error inflating class " + name);
774 ie.initCause(e);
775 throw ie;
776 }
777 }
778
779 /**
780 * Recursive method used to descend down the xml hierarchy and instantiate
781 * views, instantiate their children, and then call onFinishInflate().
Alan Viverette24927f22014-01-07 17:28:48 -0800782 *
783 * @param inheritContext Whether the root view should be inflated in its
784 * parent's context. This should be true when called inflating
785 * child views recursively, or false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800786 */
Xavier Ducrohet7f9f99ea2011-08-11 10:16:17 -0700787 void rInflate(XmlPullParser parser, View parent, final AttributeSet attrs,
Alan Viverette24927f22014-01-07 17:28:48 -0800788 boolean finishInflate, boolean inheritContext) throws XmlPullParserException,
789 IOException {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800790
791 final int depth = parser.getDepth();
792 int type;
793
794 while (((type = parser.next()) != XmlPullParser.END_TAG ||
795 parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
796
797 if (type != XmlPullParser.START_TAG) {
798 continue;
799 }
800
801 final String name = parser.getName();
Dave Burke579e1402012-10-18 20:41:55 -0700802
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800803 if (TAG_REQUEST_FOCUS.equals(name)) {
804 parseRequestFocus(parser, parent);
Alan Viverette451a3412014-02-11 18:08:46 -0800805 } else if (TAG_TAG.equals(name)) {
806 parseViewTag(parser, parent, attrs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800807 } else if (TAG_INCLUDE.equals(name)) {
808 if (parser.getDepth() == 0) {
809 throw new InflateException("<include /> cannot be the root element");
810 }
Alan Viverette24927f22014-01-07 17:28:48 -0800811 parseInclude(parser, parent, attrs, inheritContext);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800812 } else if (TAG_MERGE.equals(name)) {
813 throw new InflateException("<merge /> must be the root element");
814 } else {
Alan Viverette24927f22014-01-07 17:28:48 -0800815 final View view = createViewFromTag(parent, name, attrs, inheritContext);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800816 final ViewGroup viewGroup = (ViewGroup) parent;
817 final ViewGroup.LayoutParams params = viewGroup.generateLayoutParams(attrs);
Alan Viverette24927f22014-01-07 17:28:48 -0800818 rInflate(parser, view, attrs, true, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800819 viewGroup.addView(view, params);
820 }
821 }
822
Romain Guy9295ada2010-06-15 11:33:24 -0700823 if (finishInflate) parent.onFinishInflate();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800824 }
825
Alan Viverette451a3412014-02-11 18:08:46 -0800826 /**
827 * Parses a <code>&lt;request-focus&gt;</code> element and requests focus on
828 * the containing View.
829 */
830 private void parseRequestFocus(XmlPullParser parser, View view)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800831 throws XmlPullParserException, IOException {
832 int type;
Alan Viverette451a3412014-02-11 18:08:46 -0800833 view.requestFocus();
834 final int currentDepth = parser.getDepth();
835 while (((type = parser.next()) != XmlPullParser.END_TAG ||
836 parser.getDepth() > currentDepth) && type != XmlPullParser.END_DOCUMENT) {
837 // Empty
838 }
839 }
840
841 /**
842 * Parses a <code>&lt;tag&gt;</code> element and sets a keyed tag on the
843 * containing View.
844 */
845 private void parseViewTag(XmlPullParser parser, View view, AttributeSet attrs)
846 throws XmlPullParserException, IOException {
847 int type;
848
Alan Viverette33e3cda2014-12-17 15:43:29 -0800849 final TypedArray ta = view.getContext().obtainStyledAttributes(
Alan Viverette451a3412014-02-11 18:08:46 -0800850 attrs, com.android.internal.R.styleable.ViewTag);
851 final int key = ta.getResourceId(com.android.internal.R.styleable.ViewTag_id, 0);
852 final CharSequence value = ta.getText(com.android.internal.R.styleable.ViewTag_value);
853 view.setTag(key, value);
854 ta.recycle();
855
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800856 final int currentDepth = parser.getDepth();
857 while (((type = parser.next()) != XmlPullParser.END_TAG ||
858 parser.getDepth() > currentDepth) && type != XmlPullParser.END_DOCUMENT) {
859 // Empty
860 }
861 }
862
Alan Viverette24927f22014-01-07 17:28:48 -0800863 private void parseInclude(XmlPullParser parser, View parent, AttributeSet attrs,
864 boolean inheritContext) throws XmlPullParserException, IOException {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800865 int type;
866
867 if (parent instanceof ViewGroup) {
Alan Viverette33e3cda2014-12-17 15:43:29 -0800868 Context context = inheritContext ? parent.getContext() : mContext;
869
870 // Apply a theme wrapper, if requested.
871 final TypedArray ta = context.obtainStyledAttributes(attrs, ATTRS_THEME);
872 final int themeResId = ta.getResourceId(0, 0);
873 if (themeResId != 0) {
874 context = new ContextThemeWrapper(context, themeResId);
875 }
876 ta.recycle();
877
878 // If the layout is pointing to a theme attribute, we have to
879 // massage the value to get a resource identifier out of it.
880 int layout = attrs.getAttributeResourceValue(null, ATTR_LAYOUT, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800881 if (layout == 0) {
Alan Viverette33e3cda2014-12-17 15:43:29 -0800882 final String value = attrs.getAttributeValue(null, ATTR_LAYOUT);
883 if (value == null || value.length() < 1) {
884 throw new InflateException("You must specify a layout in the"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800885 + " include tag: <include layout=\"@layout/layoutID\" />");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800886 }
Alan Viverette33e3cda2014-12-17 15:43:29 -0800887
888 layout = context.getResources().getIdentifier(value.substring(1), null, null);
889 }
890
891 // The layout might be referencing a theme attribute.
892 if (mTempValue == null) {
893 mTempValue = new TypedValue();
894 }
895 if (layout != 0 && context.getTheme().resolveAttribute(layout, mTempValue, true)) {
896 layout = mTempValue.resourceId;
897 }
898
899 if (layout == 0) {
900 final String value = attrs.getAttributeValue(null, ATTR_LAYOUT);
901 throw new InflateException("You must specify a valid layout "
902 + "reference. The layout ID " + value + " is not valid.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800903 } else {
904 final XmlResourceParser childParser =
905 getContext().getResources().getLayout(layout);
906
907 try {
908 final AttributeSet childAttrs = Xml.asAttributeSet(childParser);
909
910 while ((type = childParser.next()) != XmlPullParser.START_TAG &&
911 type != XmlPullParser.END_DOCUMENT) {
912 // Empty.
913 }
914
915 if (type != XmlPullParser.START_TAG) {
916 throw new InflateException(childParser.getPositionDescription() +
917 ": No start tag found!");
918 }
919
920 final String childName = childParser.getName();
921
922 if (TAG_MERGE.equals(childName)) {
923 // Inflate all children.
Alan Viverette24927f22014-01-07 17:28:48 -0800924 rInflate(childParser, parent, childAttrs, false, inheritContext);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800925 } else {
Alan Viverette24927f22014-01-07 17:28:48 -0800926 final View view = createViewFromTag(parent, childName, childAttrs,
927 inheritContext);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800928 final ViewGroup group = (ViewGroup) parent;
929
Alan Viverettee8489cd2015-02-03 14:40:45 -0800930 final TypedArray a = context.obtainStyledAttributes(
931 attrs, R.styleable.Include);
932 final int id = a.getResourceId(R.styleable.Include_id, View.NO_ID);
933 final int visibility = a.getInt(R.styleable.Include_visibility, -1);
934 final boolean hasWidth = a.hasValue(R.styleable.Include_layout_width);
935 final boolean hasHeight = a.hasValue(R.styleable.Include_layout_height);
936 a.recycle();
937
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800938 // We try to load the layout params set in the <include /> tag. If
939 // they don't exist, we will rely on the layout params set in the
940 // included XML file.
Dave Burke579e1402012-10-18 20:41:55 -0700941 // During a layoutparams generation, a runtime exception is thrown
942 // if either layout_width or layout_height is missing. We catch
943 // this exception and set localParams accordingly: true means we
944 // successfully loaded layout params from the <include /> tag,
945 // false means we need to rely on the included layout params.
946 ViewGroup.LayoutParams params = null;
Alan Viverettee8489cd2015-02-03 14:40:45 -0800947 if (hasWidth && hasHeight) {
948 try {
949 params = group.generateLayoutParams(attrs);
950 } catch (RuntimeException e) {
951 // Ignore, just fail over to child attrs.
Dave Burke579e1402012-10-18 20:41:55 -0700952 }
953 }
Alan Viverettee8489cd2015-02-03 14:40:45 -0800954 if (params == null) {
955 params = group.generateLayoutParams(childAttrs);
956 }
957 view.setLayoutParams(params);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800958
959 // Inflate all children.
Alan Viverette24927f22014-01-07 17:28:48 -0800960 rInflate(childParser, view, childAttrs, true, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800961
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800962 if (id != View.NO_ID) {
963 view.setId(id);
964 }
965
966 switch (visibility) {
967 case 0:
968 view.setVisibility(View.VISIBLE);
969 break;
970 case 1:
971 view.setVisibility(View.INVISIBLE);
972 break;
973 case 2:
974 view.setVisibility(View.GONE);
975 break;
976 }
977
978 group.addView(view);
979 }
980 } finally {
981 childParser.close();
982 }
983 }
984 } else {
985 throw new InflateException("<include /> can only be used inside of a ViewGroup");
986 }
987
988 final int currentDepth = parser.getDepth();
989 while (((type = parser.next()) != XmlPullParser.END_TAG ||
990 parser.getDepth() > currentDepth) && type != XmlPullParser.END_DOCUMENT) {
991 // Empty
992 }
Romain Guy9c1223a2011-05-17 14:25:49 -0700993 }
994
995 private static class BlinkLayout extends FrameLayout {
996 private static final int MESSAGE_BLINK = 0x42;
997 private static final int BLINK_DELAY = 500;
998
999 private boolean mBlink;
1000 private boolean mBlinkState;
1001 private final Handler mHandler;
1002
1003 public BlinkLayout(Context context, AttributeSet attrs) {
1004 super(context, attrs);
1005 mHandler = new Handler(new Handler.Callback() {
1006 @Override
1007 public boolean handleMessage(Message msg) {
1008 if (msg.what == MESSAGE_BLINK) {
1009 if (mBlink) {
1010 mBlinkState = !mBlinkState;
1011 makeBlink();
1012 }
1013 invalidate();
1014 return true;
1015 }
1016 return false;
1017 }
1018 });
1019 }
1020
1021 private void makeBlink() {
1022 Message message = mHandler.obtainMessage(MESSAGE_BLINK);
1023 mHandler.sendMessageDelayed(message, BLINK_DELAY);
1024 }
1025
1026 @Override
1027 protected void onAttachedToWindow() {
1028 super.onAttachedToWindow();
1029
1030 mBlink = true;
1031 mBlinkState = true;
1032
1033 makeBlink();
1034 }
1035
1036 @Override
1037 protected void onDetachedFromWindow() {
1038 super.onDetachedFromWindow();
1039
1040 mBlink = false;
1041 mBlinkState = true;
1042
1043 mHandler.removeMessages(MESSAGE_BLINK);
1044 }
1045
1046 @Override
1047 protected void dispatchDraw(Canvas canvas) {
1048 if (mBlinkState) {
1049 super.dispatchDraw(canvas);
1050 }
1051 }
1052 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001053}