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 | |
Romain Guy | 9c1223a | 2011-05-17 14:25:49 -0700 | [diff] [blame] | 19 | import android.graphics.Canvas; |
| 20 | import android.os.Handler; |
| 21 | import android.os.Message; |
| 22 | import android.widget.FrameLayout; |
Philip Milne | c29f031 | 2012-02-22 16:34:51 -0800 | [diff] [blame] | 23 | import com.android.internal.R; |
Gilles Debunne | 3030193 | 2010-06-16 18:32:00 -0700 | [diff] [blame] | 24 | import org.xmlpull.v1.XmlPullParser; |
| 25 | import org.xmlpull.v1.XmlPullParserException; |
| 26 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 27 | import android.content.Context; |
| 28 | import android.content.res.TypedArray; |
| 29 | import android.content.res.XmlResourceParser; |
| 30 | import android.util.AttributeSet; |
| 31 | import android.util.Xml; |
| 32 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 33 | import java.io.IOException; |
| 34 | import java.lang.reflect.Constructor; |
| 35 | import java.util.HashMap; |
| 36 | |
| 37 | /** |
Scott Main | 93dc642 | 2012-02-24 12:04:06 -0800 | [diff] [blame] | 38 | * Instantiates a layout XML file into its corresponding {@link android.view.View} |
| 39 | * objects. It is never used directly. Instead, use |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 40 | * {@link android.app.Activity#getLayoutInflater()} or |
| 41 | * {@link Context#getSystemService} to retrieve a standard LayoutInflater instance |
| 42 | * that is already hooked up to the current context and correctly configured |
| 43 | * for the device you are running on. For example: |
| 44 | * |
| 45 | * <pre>LayoutInflater inflater = (LayoutInflater)context.getSystemService |
Christian Mehlmauer | bd6fda1 | 2011-01-08 18:22:20 +0100 | [diff] [blame] | 46 | * (Context.LAYOUT_INFLATER_SERVICE);</pre> |
Philip Milne | c29f031 | 2012-02-22 16:34:51 -0800 | [diff] [blame] | 47 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 48 | * <p> |
| 49 | * To create a new LayoutInflater with an additional {@link Factory} for your |
| 50 | * own views, you can use {@link #cloneInContext} to clone an existing |
| 51 | * ViewFactory, and then call {@link #setFactory} on it to include your |
| 52 | * Factory. |
Philip Milne | c29f031 | 2012-02-22 16:34:51 -0800 | [diff] [blame] | 53 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 54 | * <p> |
| 55 | * For performance reasons, view inflation relies heavily on pre-processing of |
| 56 | * XML files that is done at build time. Therefore, it is not currently possible |
| 57 | * to use LayoutInflater with an XmlPullParser over a plain XML file at runtime; |
| 58 | * it only works with an XmlPullParser returned from a compiled resource |
| 59 | * (R.<em>something</em> file.) |
Philip Milne | c29f031 | 2012-02-22 16:34:51 -0800 | [diff] [blame] | 60 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 61 | * @see Context#getSystemService |
| 62 | */ |
| 63 | public abstract class LayoutInflater { |
| 64 | private final boolean DEBUG = false; |
| 65 | |
| 66 | /** |
| 67 | * This field should be made private, so it is hidden from the SDK. |
| 68 | * {@hide} |
| 69 | */ |
| 70 | protected final Context mContext; |
| 71 | |
| 72 | // these are optional, set by the caller |
| 73 | private boolean mFactorySet; |
| 74 | private Factory mFactory; |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 75 | private Factory2 mFactory2; |
Dianne Hackborn | 420829e | 2011-01-28 11:30:35 -0800 | [diff] [blame] | 76 | private Factory2 mPrivateFactory; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 77 | private Filter mFilter; |
| 78 | |
Xavier Ducrohet | 7f9f99ea | 2011-08-11 10:16:17 -0700 | [diff] [blame] | 79 | final Object[] mConstructorArgs = new Object[2]; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 80 | |
Xavier Ducrohet | 7f9f99ea | 2011-08-11 10:16:17 -0700 | [diff] [blame] | 81 | static final Class<?>[] mConstructorSignature = new Class[] { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 82 | Context.class, AttributeSet.class}; |
| 83 | |
Gilles Debunne | 3030193 | 2010-06-16 18:32:00 -0700 | [diff] [blame] | 84 | private static final HashMap<String, Constructor<? extends View>> sConstructorMap = |
| 85 | new HashMap<String, Constructor<? extends View>>(); |
Philip Milne | c29f031 | 2012-02-22 16:34:51 -0800 | [diff] [blame] | 86 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 87 | private HashMap<String, Boolean> mFilterMap; |
| 88 | |
| 89 | private static final String TAG_MERGE = "merge"; |
| 90 | private static final String TAG_INCLUDE = "include"; |
Romain Guy | 9c1223a | 2011-05-17 14:25:49 -0700 | [diff] [blame] | 91 | private static final String TAG_1995 = "blink"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 92 | private static final String TAG_REQUEST_FOCUS = "requestFocus"; |
| 93 | |
| 94 | /** |
| 95 | * Hook to allow clients of the LayoutInflater to restrict the set of Views that are allowed |
| 96 | * to be inflated. |
Philip Milne | c29f031 | 2012-02-22 16:34:51 -0800 | [diff] [blame] | 97 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 98 | */ |
| 99 | public interface Filter { |
| 100 | /** |
| 101 | * Hook to allow clients of the LayoutInflater to restrict the set of Views |
| 102 | * that are allowed to be inflated. |
Philip Milne | c29f031 | 2012-02-22 16:34:51 -0800 | [diff] [blame] | 103 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 104 | * @param clazz The class object for the View that is about to be inflated |
Philip Milne | c29f031 | 2012-02-22 16:34:51 -0800 | [diff] [blame] | 105 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 106 | * @return True if this class is allowed to be inflated, or false otherwise |
| 107 | */ |
Gilles Debunne | e6ac8b9 | 2010-06-17 10:55:04 -0700 | [diff] [blame] | 108 | @SuppressWarnings("unchecked") |
| 109 | boolean onLoadClass(Class clazz); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 110 | } |
Philip Milne | c29f031 | 2012-02-22 16:34:51 -0800 | [diff] [blame] | 111 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 112 | public interface Factory { |
| 113 | /** |
| 114 | * Hook you can supply that is called when inflating from a LayoutInflater. |
| 115 | * You can use this to customize the tag names available in your XML |
| 116 | * layout files. |
Philip Milne | c29f031 | 2012-02-22 16:34:51 -0800 | [diff] [blame] | 117 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 118 | * <p> |
| 119 | * Note that it is good practice to prefix these custom names with your |
| 120 | * package (i.e., com.coolcompany.apps) to avoid conflicts with system |
| 121 | * names. |
Philip Milne | c29f031 | 2012-02-22 16:34:51 -0800 | [diff] [blame] | 122 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 123 | * @param name Tag name to be inflated. |
| 124 | * @param context The context the view is being created in. |
| 125 | * @param attrs Inflation attributes as specified in XML file. |
Philip Milne | c29f031 | 2012-02-22 16:34:51 -0800 | [diff] [blame] | 126 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 127 | * @return View Newly created view. Return null for the default |
| 128 | * behavior. |
| 129 | */ |
| 130 | public View onCreateView(String name, Context context, AttributeSet attrs); |
| 131 | } |
| 132 | |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 133 | public interface Factory2 extends Factory { |
| 134 | /** |
| 135 | * Version of {@link #onCreateView(String, Context, AttributeSet)} |
| 136 | * that also supplies the parent that the view created view will be |
| 137 | * placed in. |
| 138 | * |
| 139 | * @param parent The parent that the created view will be placed |
| 140 | * in; <em>note that this may be null</em>. |
| 141 | * @param name Tag name to be inflated. |
| 142 | * @param context The context the view is being created in. |
| 143 | * @param attrs Inflation attributes as specified in XML file. |
| 144 | * |
| 145 | * @return View Newly created view. Return null for the default |
| 146 | * behavior. |
| 147 | */ |
| 148 | public View onCreateView(View parent, String name, Context context, AttributeSet attrs); |
| 149 | } |
| 150 | |
| 151 | private static class FactoryMerger implements Factory2 { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 152 | private final Factory mF1, mF2; |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 153 | private final Factory2 mF12, mF22; |
Philip Milne | c29f031 | 2012-02-22 16:34:51 -0800 | [diff] [blame] | 154 | |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 155 | FactoryMerger(Factory f1, Factory2 f12, Factory f2, Factory2 f22) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 156 | mF1 = f1; |
| 157 | mF2 = f2; |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 158 | mF12 = f12; |
| 159 | mF22 = f22; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 160 | } |
Philip Milne | c29f031 | 2012-02-22 16:34:51 -0800 | [diff] [blame] | 161 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 162 | public View onCreateView(String name, Context context, AttributeSet attrs) { |
| 163 | View v = mF1.onCreateView(name, context, attrs); |
| 164 | if (v != null) return v; |
| 165 | return mF2.onCreateView(name, context, attrs); |
| 166 | } |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 167 | |
| 168 | public View onCreateView(View parent, String name, Context context, AttributeSet attrs) { |
| 169 | View v = mF12 != null ? mF12.onCreateView(parent, name, context, attrs) |
| 170 | : mF1.onCreateView(name, context, attrs); |
| 171 | if (v != null) return v; |
| 172 | return mF22 != null ? mF22.onCreateView(parent, name, context, attrs) |
| 173 | : mF2.onCreateView(name, context, attrs); |
| 174 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 175 | } |
Philip Milne | c29f031 | 2012-02-22 16:34:51 -0800 | [diff] [blame] | 176 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 177 | /** |
| 178 | * Create a new LayoutInflater instance associated with a particular Context. |
| 179 | * Applications will almost always want to use |
| 180 | * {@link Context#getSystemService Context.getSystemService()} to retrieve |
| 181 | * the standard {@link Context#LAYOUT_INFLATER_SERVICE Context.INFLATER_SERVICE}. |
Philip Milne | c29f031 | 2012-02-22 16:34:51 -0800 | [diff] [blame] | 182 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 183 | * @param context The Context in which this LayoutInflater will create its |
| 184 | * Views; most importantly, this supplies the theme from which the default |
| 185 | * values for their attributes are retrieved. |
| 186 | */ |
| 187 | protected LayoutInflater(Context context) { |
| 188 | mContext = context; |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * Create a new LayoutInflater instance that is a copy of an existing |
| 193 | * LayoutInflater, optionally with its Context changed. For use in |
| 194 | * implementing {@link #cloneInContext}. |
Philip Milne | c29f031 | 2012-02-22 16:34:51 -0800 | [diff] [blame] | 195 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 196 | * @param original The original LayoutInflater to copy. |
| 197 | * @param newContext The new Context to use. |
| 198 | */ |
| 199 | protected LayoutInflater(LayoutInflater original, Context newContext) { |
| 200 | mContext = newContext; |
| 201 | mFactory = original.mFactory; |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 202 | mFactory2 = original.mFactory2; |
Dianne Hackborn | 420829e | 2011-01-28 11:30:35 -0800 | [diff] [blame] | 203 | mPrivateFactory = original.mPrivateFactory; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 204 | mFilter = original.mFilter; |
| 205 | } |
Philip Milne | c29f031 | 2012-02-22 16:34:51 -0800 | [diff] [blame] | 206 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 207 | /** |
| 208 | * Obtains the LayoutInflater from the given context. |
| 209 | */ |
| 210 | public static LayoutInflater from(Context context) { |
| 211 | LayoutInflater LayoutInflater = |
| 212 | (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); |
| 213 | if (LayoutInflater == null) { |
| 214 | throw new AssertionError("LayoutInflater not found."); |
| 215 | } |
| 216 | return LayoutInflater; |
| 217 | } |
| 218 | |
| 219 | /** |
| 220 | * Create a copy of the existing LayoutInflater object, with the copy |
| 221 | * pointing to a different Context than the original. This is used by |
| 222 | * {@link ContextThemeWrapper} to create a new LayoutInflater to go along |
| 223 | * with the new Context theme. |
Philip Milne | c29f031 | 2012-02-22 16:34:51 -0800 | [diff] [blame] | 224 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 225 | * @param newContext The new Context to associate with the new LayoutInflater. |
| 226 | * May be the same as the original Context if desired. |
Philip Milne | c29f031 | 2012-02-22 16:34:51 -0800 | [diff] [blame] | 227 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 228 | * @return Returns a brand spanking new LayoutInflater object associated with |
| 229 | * the given Context. |
| 230 | */ |
| 231 | public abstract LayoutInflater cloneInContext(Context newContext); |
Philip Milne | c29f031 | 2012-02-22 16:34:51 -0800 | [diff] [blame] | 232 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 233 | /** |
| 234 | * Return the context we are running in, for access to resources, class |
| 235 | * loader, etc. |
| 236 | */ |
| 237 | public Context getContext() { |
| 238 | return mContext; |
| 239 | } |
| 240 | |
| 241 | /** |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 242 | * 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] | 243 | * name. If the factory returns a View, add that to the hierarchy. If it |
| 244 | * returns null, proceed to call onCreateView(name). |
| 245 | */ |
| 246 | public final Factory getFactory() { |
| 247 | return mFactory; |
| 248 | } |
| 249 | |
| 250 | /** |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 251 | * Return the current {@link Factory2}. Returns null if no factory is set |
| 252 | * or the set factory does not implement the {@link Factory2} interface. |
| 253 | * This is called on each element |
| 254 | * name. If the factory returns a View, add that to the hierarchy. If it |
| 255 | * returns null, proceed to call onCreateView(name). |
| 256 | */ |
| 257 | public final Factory2 getFactory2() { |
| 258 | return mFactory2; |
| 259 | } |
| 260 | |
| 261 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 262 | * Attach a custom Factory interface for creating views while using |
| 263 | * this LayoutInflater. This must not be null, and can only be set once; |
| 264 | * after setting, you can not change the factory. This is |
| 265 | * called on each element name as the xml is parsed. If the factory returns |
| 266 | * a View, that is added to the hierarchy. If it returns null, the next |
| 267 | * factory default {@link #onCreateView} method is called. |
Philip Milne | c29f031 | 2012-02-22 16:34:51 -0800 | [diff] [blame] | 268 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 269 | * <p>If you have an existing |
| 270 | * LayoutInflater and want to add your own factory to it, use |
| 271 | * {@link #cloneInContext} to clone the existing instance and then you |
| 272 | * can use this function (once) on the returned new instance. This will |
| 273 | * merge your own factory with whatever factory the original instance is |
| 274 | * using. |
| 275 | */ |
| 276 | public void setFactory(Factory factory) { |
| 277 | if (mFactorySet) { |
| 278 | throw new IllegalStateException("A factory has already been set on this LayoutInflater"); |
| 279 | } |
| 280 | if (factory == null) { |
| 281 | throw new NullPointerException("Given factory can not be null"); |
| 282 | } |
| 283 | mFactorySet = true; |
| 284 | if (mFactory == null) { |
| 285 | mFactory = factory; |
| 286 | } else { |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 287 | mFactory = new FactoryMerger(factory, null, mFactory, mFactory2); |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | /** |
| 292 | * Like {@link #setFactory}, but allows you to set a {@link Factory2} |
| 293 | * interface. |
| 294 | */ |
| 295 | public void setFactory2(Factory2 factory) { |
| 296 | if (mFactorySet) { |
| 297 | throw new IllegalStateException("A factory has already been set on this LayoutInflater"); |
| 298 | } |
| 299 | if (factory == null) { |
| 300 | throw new NullPointerException("Given factory can not be null"); |
| 301 | } |
| 302 | mFactorySet = true; |
| 303 | if (mFactory == null) { |
| 304 | mFactory = mFactory2 = factory; |
| 305 | } else { |
| 306 | mFactory = new FactoryMerger(factory, factory, mFactory, mFactory2); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 307 | } |
| 308 | } |
| 309 | |
| 310 | /** |
Dianne Hackborn | 420829e | 2011-01-28 11:30:35 -0800 | [diff] [blame] | 311 | * @hide for use by framework |
| 312 | */ |
| 313 | public void setPrivateFactory(Factory2 factory) { |
| 314 | mPrivateFactory = factory; |
| 315 | } |
| 316 | |
| 317 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 318 | * @return The {@link Filter} currently used by this LayoutInflater to restrict the set of Views |
| 319 | * that are allowed to be inflated. |
| 320 | */ |
| 321 | public Filter getFilter() { |
| 322 | return mFilter; |
| 323 | } |
Philip Milne | c29f031 | 2012-02-22 16:34:51 -0800 | [diff] [blame] | 324 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 325 | /** |
| 326 | * Sets the {@link Filter} to by this LayoutInflater. If a view is attempted to be inflated |
| 327 | * which is not allowed by the {@link Filter}, the {@link #inflate(int, ViewGroup)} call will |
| 328 | * throw an {@link InflateException}. This filter will replace any previous filter set on this |
| 329 | * LayoutInflater. |
Philip Milne | c29f031 | 2012-02-22 16:34:51 -0800 | [diff] [blame] | 330 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 331 | * @param filter The Filter which restricts the set of Views that are allowed to be inflated. |
| 332 | * This filter will replace any previous filter set on this LayoutInflater. |
| 333 | */ |
| 334 | public void setFilter(Filter filter) { |
| 335 | mFilter = filter; |
| 336 | if (filter != null) { |
| 337 | mFilterMap = new HashMap<String, Boolean>(); |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | /** |
| 342 | * Inflate a new view hierarchy from the specified xml resource. Throws |
| 343 | * {@link InflateException} if there is an error. |
Philip Milne | c29f031 | 2012-02-22 16:34:51 -0800 | [diff] [blame] | 344 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 345 | * @param resource ID for an XML layout resource to load (e.g., |
| 346 | * <code>R.layout.main_page</code>) |
| 347 | * @param root Optional view to be the parent of the generated hierarchy. |
| 348 | * @return The root View of the inflated hierarchy. If root was supplied, |
| 349 | * this is the root View; otherwise it is the root of the inflated |
| 350 | * XML file. |
| 351 | */ |
| 352 | public View inflate(int resource, ViewGroup root) { |
| 353 | return inflate(resource, root, root != null); |
| 354 | } |
| 355 | |
| 356 | /** |
| 357 | * Inflate a new view hierarchy from the specified xml node. Throws |
| 358 | * {@link InflateException} if there is an error. * |
| 359 | * <p> |
| 360 | * <em><strong>Important</strong></em> For performance |
| 361 | * reasons, view inflation relies heavily on pre-processing of XML files |
| 362 | * that is done at build time. Therefore, it is not currently possible to |
| 363 | * use LayoutInflater with an XmlPullParser over a plain XML file at runtime. |
Philip Milne | c29f031 | 2012-02-22 16:34:51 -0800 | [diff] [blame] | 364 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 365 | * @param parser XML dom node containing the description of the view |
| 366 | * hierarchy. |
| 367 | * @param root Optional view to be the parent of the generated hierarchy. |
| 368 | * @return The root View of the inflated hierarchy. If root was supplied, |
| 369 | * this is the root View; otherwise it is the root of the inflated |
| 370 | * XML file. |
| 371 | */ |
| 372 | public View inflate(XmlPullParser parser, ViewGroup root) { |
| 373 | return inflate(parser, root, root != null); |
| 374 | } |
| 375 | |
| 376 | /** |
| 377 | * Inflate a new view hierarchy from the specified xml resource. Throws |
| 378 | * {@link InflateException} if there is an error. |
Philip Milne | c29f031 | 2012-02-22 16:34:51 -0800 | [diff] [blame] | 379 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 380 | * @param resource ID for an XML layout resource to load (e.g., |
| 381 | * <code>R.layout.main_page</code>) |
| 382 | * @param root Optional view to be the parent of the generated hierarchy (if |
| 383 | * <em>attachToRoot</em> is true), or else simply an object that |
| 384 | * provides a set of LayoutParams values for root of the returned |
| 385 | * hierarchy (if <em>attachToRoot</em> is false.) |
| 386 | * @param attachToRoot Whether the inflated hierarchy should be attached to |
| 387 | * the root parameter? If false, root is only used to create the |
| 388 | * correct subclass of LayoutParams for the root view in the XML. |
| 389 | * @return The root View of the inflated hierarchy. If root was supplied and |
| 390 | * attachToRoot is true, this is root; otherwise it is the root of |
| 391 | * the inflated XML file. |
| 392 | */ |
| 393 | public View inflate(int resource, ViewGroup root, boolean attachToRoot) { |
| 394 | if (DEBUG) System.out.println("INFLATING from resource: " + resource); |
| 395 | XmlResourceParser parser = getContext().getResources().getLayout(resource); |
| 396 | try { |
| 397 | return inflate(parser, root, attachToRoot); |
| 398 | } finally { |
| 399 | parser.close(); |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | /** |
| 404 | * Inflate a new view hierarchy from the specified XML node. Throws |
| 405 | * {@link InflateException} if there is an error. |
| 406 | * <p> |
| 407 | * <em><strong>Important</strong></em> For performance |
| 408 | * reasons, view inflation relies heavily on pre-processing of XML files |
| 409 | * that is done at build time. Therefore, it is not currently possible to |
| 410 | * use LayoutInflater with an XmlPullParser over a plain XML file at runtime. |
Philip Milne | c29f031 | 2012-02-22 16:34:51 -0800 | [diff] [blame] | 411 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 412 | * @param parser XML dom node containing the description of the view |
| 413 | * hierarchy. |
| 414 | * @param root Optional view to be the parent of the generated hierarchy (if |
| 415 | * <em>attachToRoot</em> is true), or else simply an object that |
| 416 | * provides a set of LayoutParams values for root of the returned |
| 417 | * hierarchy (if <em>attachToRoot</em> is false.) |
| 418 | * @param attachToRoot Whether the inflated hierarchy should be attached to |
| 419 | * the root parameter? If false, root is only used to create the |
| 420 | * correct subclass of LayoutParams for the root view in the XML. |
| 421 | * @return The root View of the inflated hierarchy. If root was supplied and |
| 422 | * attachToRoot is true, this is root; otherwise it is the root of |
| 423 | * the inflated XML file. |
| 424 | */ |
| 425 | public View inflate(XmlPullParser parser, ViewGroup root, boolean attachToRoot) { |
| 426 | synchronized (mConstructorArgs) { |
| 427 | final AttributeSet attrs = Xml.asAttributeSet(parser); |
Dianne Hackborn | 9dae48e | 2010-08-26 10:20:01 -0700 | [diff] [blame] | 428 | Context lastContext = (Context)mConstructorArgs[0]; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 429 | mConstructorArgs[0] = mContext; |
| 430 | View result = root; |
| 431 | |
| 432 | try { |
| 433 | // Look for the root node. |
| 434 | int type; |
| 435 | while ((type = parser.next()) != XmlPullParser.START_TAG && |
| 436 | type != XmlPullParser.END_DOCUMENT) { |
| 437 | // Empty |
| 438 | } |
| 439 | |
| 440 | if (type != XmlPullParser.START_TAG) { |
| 441 | throw new InflateException(parser.getPositionDescription() |
| 442 | + ": No start tag found!"); |
| 443 | } |
| 444 | |
| 445 | final String name = parser.getName(); |
Philip Milne | c29f031 | 2012-02-22 16:34:51 -0800 | [diff] [blame] | 446 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 447 | if (DEBUG) { |
| 448 | System.out.println("**************************"); |
| 449 | System.out.println("Creating root view: " |
| 450 | + name); |
| 451 | System.out.println("**************************"); |
| 452 | } |
| 453 | |
| 454 | if (TAG_MERGE.equals(name)) { |
| 455 | if (root == null || !attachToRoot) { |
| 456 | throw new InflateException("<merge /> can be used only with a valid " |
| 457 | + "ViewGroup root and attachToRoot=true"); |
| 458 | } |
| 459 | |
Romain Guy | 9295ada | 2010-06-15 11:33:24 -0700 | [diff] [blame] | 460 | rInflate(parser, root, attrs, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 461 | } else { |
| 462 | // Temp is the root view that was found in the xml |
Romain Guy | 9c1223a | 2011-05-17 14:25:49 -0700 | [diff] [blame] | 463 | View temp; |
| 464 | if (TAG_1995.equals(name)) { |
| 465 | temp = new BlinkLayout(mContext, attrs); |
| 466 | } else { |
| 467 | temp = createViewFromTag(root, name, attrs); |
| 468 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 469 | |
| 470 | ViewGroup.LayoutParams params = null; |
| 471 | |
| 472 | if (root != null) { |
| 473 | if (DEBUG) { |
| 474 | System.out.println("Creating params from root: " + |
| 475 | root); |
| 476 | } |
| 477 | // Create layout params that match root, if supplied |
| 478 | params = root.generateLayoutParams(attrs); |
| 479 | if (!attachToRoot) { |
| 480 | // Set the layout params for temp if we are not |
| 481 | // attaching. (If we are, we use addView, below) |
| 482 | temp.setLayoutParams(params); |
| 483 | } |
| 484 | } |
| 485 | |
| 486 | if (DEBUG) { |
| 487 | System.out.println("-----> start inflating children"); |
| 488 | } |
| 489 | // Inflate all children under temp |
Romain Guy | 9295ada | 2010-06-15 11:33:24 -0700 | [diff] [blame] | 490 | rInflate(parser, temp, attrs, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 491 | if (DEBUG) { |
| 492 | System.out.println("-----> done inflating children"); |
| 493 | } |
| 494 | |
| 495 | // We are supposed to attach all the views we found (int temp) |
| 496 | // to root. Do that now. |
| 497 | if (root != null && attachToRoot) { |
| 498 | root.addView(temp, params); |
| 499 | } |
| 500 | |
| 501 | // Decide whether to return the root that was passed in or the |
| 502 | // top view found in xml. |
| 503 | if (root == null || !attachToRoot) { |
| 504 | result = temp; |
| 505 | } |
| 506 | } |
| 507 | |
| 508 | } catch (XmlPullParserException e) { |
| 509 | InflateException ex = new InflateException(e.getMessage()); |
| 510 | ex.initCause(e); |
| 511 | throw ex; |
| 512 | } catch (IOException e) { |
| 513 | InflateException ex = new InflateException( |
| 514 | parser.getPositionDescription() |
| 515 | + ": " + e.getMessage()); |
| 516 | ex.initCause(e); |
| 517 | throw ex; |
Dianne Hackborn | 9dae48e | 2010-08-26 10:20:01 -0700 | [diff] [blame] | 518 | } finally { |
| 519 | // Don't retain static reference on context. |
| 520 | mConstructorArgs[0] = lastContext; |
| 521 | mConstructorArgs[1] = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | return result; |
| 525 | } |
| 526 | } |
| 527 | |
| 528 | /** |
| 529 | * Low-level function for instantiating a view by name. This attempts to |
| 530 | * instantiate a view class of the given <var>name</var> found in this |
| 531 | * LayoutInflater's ClassLoader. |
Philip Milne | c29f031 | 2012-02-22 16:34:51 -0800 | [diff] [blame] | 532 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 533 | * <p> |
| 534 | * There are two things that can happen in an error case: either the |
| 535 | * exception describing the error will be thrown, or a null will be |
| 536 | * returned. You must deal with both possibilities -- the former will happen |
| 537 | * the first time createView() is called for a class of a particular name, |
| 538 | * the latter every time there-after for that class name. |
Philip Milne | c29f031 | 2012-02-22 16:34:51 -0800 | [diff] [blame] | 539 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 540 | * @param name The full name of the class to be instantiated. |
| 541 | * @param attrs The XML attributes supplied for this instance. |
Philip Milne | c29f031 | 2012-02-22 16:34:51 -0800 | [diff] [blame] | 542 | * |
Gilles Debunne | 3030193 | 2010-06-16 18:32:00 -0700 | [diff] [blame] | 543 | * @return View The newly instantiated view, or null. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 544 | */ |
| 545 | public final View createView(String name, String prefix, AttributeSet attrs) |
| 546 | throws ClassNotFoundException, InflateException { |
Gilles Debunne | 3030193 | 2010-06-16 18:32:00 -0700 | [diff] [blame] | 547 | Constructor<? extends View> constructor = sConstructorMap.get(name); |
| 548 | Class<? extends View> clazz = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 549 | |
| 550 | try { |
| 551 | if (constructor == null) { |
| 552 | // 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] | 553 | clazz = mContext.getClassLoader().loadClass( |
Gilles Debunne | 3030193 | 2010-06-16 18:32:00 -0700 | [diff] [blame] | 554 | prefix != null ? (prefix + name) : name).asSubclass(View.class); |
Philip Milne | c29f031 | 2012-02-22 16:34:51 -0800 | [diff] [blame] | 555 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 556 | if (mFilter != null && clazz != null) { |
| 557 | boolean allowed = mFilter.onLoadClass(clazz); |
| 558 | if (!allowed) { |
| 559 | failNotAllowed(name, prefix, attrs); |
| 560 | } |
| 561 | } |
| 562 | constructor = clazz.getConstructor(mConstructorSignature); |
| 563 | sConstructorMap.put(name, constructor); |
| 564 | } else { |
| 565 | // If we have a filter, apply it to cached constructor |
| 566 | if (mFilter != null) { |
| 567 | // Have we seen this name before? |
| 568 | Boolean allowedState = mFilterMap.get(name); |
| 569 | if (allowedState == null) { |
| 570 | // New class -- remember whether it is allowed |
Romain Guy | d03b880 | 2009-09-16 14:36:16 -0700 | [diff] [blame] | 571 | clazz = mContext.getClassLoader().loadClass( |
Gilles Debunne | 3030193 | 2010-06-16 18:32:00 -0700 | [diff] [blame] | 572 | prefix != null ? (prefix + name) : name).asSubclass(View.class); |
Philip Milne | c29f031 | 2012-02-22 16:34:51 -0800 | [diff] [blame] | 573 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 574 | boolean allowed = clazz != null && mFilter.onLoadClass(clazz); |
| 575 | mFilterMap.put(name, allowed); |
| 576 | if (!allowed) { |
| 577 | failNotAllowed(name, prefix, attrs); |
| 578 | } |
| 579 | } else if (allowedState.equals(Boolean.FALSE)) { |
| 580 | failNotAllowed(name, prefix, attrs); |
| 581 | } |
| 582 | } |
| 583 | } |
| 584 | |
| 585 | Object[] args = mConstructorArgs; |
| 586 | args[1] = attrs; |
Jeff Sharkey | b27b7a1 | 2012-04-02 21:07:29 -0700 | [diff] [blame] | 587 | |
| 588 | final View view = constructor.newInstance(args); |
| 589 | if (view instanceof ViewStub) { |
| 590 | // always use ourselves when inflating ViewStub later |
| 591 | final ViewStub viewStub = (ViewStub) view; |
| 592 | viewStub.setLayoutInflater(this); |
| 593 | } |
| 594 | return view; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 595 | |
| 596 | } catch (NoSuchMethodException e) { |
| 597 | InflateException ie = new InflateException(attrs.getPositionDescription() |
| 598 | + ": Error inflating class " |
| 599 | + (prefix != null ? (prefix + name) : name)); |
| 600 | ie.initCause(e); |
| 601 | throw ie; |
| 602 | |
Gilles Debunne | 3030193 | 2010-06-16 18:32:00 -0700 | [diff] [blame] | 603 | } catch (ClassCastException e) { |
| 604 | // If loaded class is not a View subclass |
| 605 | InflateException ie = new InflateException(attrs.getPositionDescription() |
| 606 | + ": Class is not a View " |
| 607 | + (prefix != null ? (prefix + name) : name)); |
| 608 | ie.initCause(e); |
| 609 | throw ie; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 610 | } catch (ClassNotFoundException e) { |
| 611 | // If loadClass fails, we should propagate the exception. |
| 612 | throw e; |
| 613 | } catch (Exception e) { |
| 614 | InflateException ie = new InflateException(attrs.getPositionDescription() |
| 615 | + ": Error inflating class " |
Romain Guy | d03b880 | 2009-09-16 14:36:16 -0700 | [diff] [blame] | 616 | + (clazz == null ? "<unknown>" : clazz.getName())); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 617 | ie.initCause(e); |
| 618 | throw ie; |
| 619 | } |
| 620 | } |
| 621 | |
| 622 | /** |
Gilles Debunne | 3030193 | 2010-06-16 18:32:00 -0700 | [diff] [blame] | 623 | * 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] | 624 | */ |
| 625 | private void failNotAllowed(String name, String prefix, AttributeSet attrs) { |
Romain Guy | 9c1223a | 2011-05-17 14:25:49 -0700 | [diff] [blame] | 626 | throw new InflateException(attrs.getPositionDescription() |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 627 | + ": Class not allowed to be inflated " |
| 628 | + (prefix != null ? (prefix + name) : name)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 629 | } |
| 630 | |
| 631 | /** |
| 632 | * This routine is responsible for creating the correct subclass of View |
| 633 | * given the xml element name. Override it to handle custom view objects. If |
| 634 | * you override this in your subclass be sure to call through to |
| 635 | * super.onCreateView(name) for names you do not recognize. |
Philip Milne | c29f031 | 2012-02-22 16:34:51 -0800 | [diff] [blame] | 636 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 637 | * @param name The fully qualified class name of the View to be create. |
| 638 | * @param attrs An AttributeSet of attributes to apply to the View. |
Philip Milne | c29f031 | 2012-02-22 16:34:51 -0800 | [diff] [blame] | 639 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 640 | * @return View The View created. |
| 641 | */ |
| 642 | protected View onCreateView(String name, AttributeSet attrs) |
| 643 | throws ClassNotFoundException { |
| 644 | return createView(name, "android.view.", attrs); |
| 645 | } |
| 646 | |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 647 | /** |
| 648 | * Version of {@link #onCreateView(String, AttributeSet)} that also |
| 649 | * takes the future parent of the view being constructure. The default |
| 650 | * implementation simply calls {@link #onCreateView(String, AttributeSet)}. |
| 651 | * |
| 652 | * @param parent The future parent of the returned view. <em>Note that |
| 653 | * this may be null.</em> |
| 654 | * @param name The fully qualified class name of the View to be create. |
| 655 | * @param attrs An AttributeSet of attributes to apply to the View. |
| 656 | * |
| 657 | * @return View The View created. |
| 658 | */ |
| 659 | protected View onCreateView(View parent, String name, AttributeSet attrs) |
| 660 | throws ClassNotFoundException { |
| 661 | return onCreateView(name, attrs); |
| 662 | } |
| 663 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 664 | /* |
| 665 | * default visibility so the BridgeInflater can override it. |
| 666 | */ |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 667 | View createViewFromTag(View parent, String name, AttributeSet attrs) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 668 | if (name.equals("view")) { |
| 669 | name = attrs.getAttributeValue(null, "class"); |
| 670 | } |
| 671 | |
| 672 | if (DEBUG) System.out.println("******** Creating view: " + name); |
| 673 | |
| 674 | try { |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 675 | View view; |
| 676 | if (mFactory2 != null) view = mFactory2.onCreateView(parent, name, mContext, attrs); |
| 677 | else if (mFactory != null) view = mFactory.onCreateView(name, mContext, attrs); |
| 678 | else view = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 679 | |
Dianne Hackborn | 420829e | 2011-01-28 11:30:35 -0800 | [diff] [blame] | 680 | if (view == null && mPrivateFactory != null) { |
| 681 | view = mPrivateFactory.onCreateView(parent, name, mContext, attrs); |
| 682 | } |
Philip Milne | c29f031 | 2012-02-22 16:34:51 -0800 | [diff] [blame] | 683 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 684 | if (view == null) { |
| 685 | if (-1 == name.indexOf('.')) { |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 686 | view = onCreateView(parent, name, attrs); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 687 | } else { |
| 688 | view = createView(name, null, attrs); |
| 689 | } |
| 690 | } |
| 691 | |
| 692 | if (DEBUG) System.out.println("Created view is: " + view); |
| 693 | return view; |
| 694 | |
| 695 | } catch (InflateException e) { |
| 696 | throw e; |
| 697 | |
| 698 | } catch (ClassNotFoundException e) { |
| 699 | InflateException ie = new InflateException(attrs.getPositionDescription() |
| 700 | + ": Error inflating class " + name); |
| 701 | ie.initCause(e); |
| 702 | throw ie; |
| 703 | |
| 704 | } catch (Exception e) { |
| 705 | InflateException ie = new InflateException(attrs.getPositionDescription() |
| 706 | + ": Error inflating class " + name); |
| 707 | ie.initCause(e); |
| 708 | throw ie; |
| 709 | } |
| 710 | } |
| 711 | |
| 712 | /** |
| 713 | * Recursive method used to descend down the xml hierarchy and instantiate |
| 714 | * views, instantiate their children, and then call onFinishInflate(). |
| 715 | */ |
Xavier Ducrohet | 7f9f99ea | 2011-08-11 10:16:17 -0700 | [diff] [blame] | 716 | void rInflate(XmlPullParser parser, View parent, final AttributeSet attrs, |
Romain Guy | 9295ada | 2010-06-15 11:33:24 -0700 | [diff] [blame] | 717 | boolean finishInflate) throws XmlPullParserException, IOException { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 718 | |
| 719 | final int depth = parser.getDepth(); |
| 720 | int type; |
| 721 | |
| 722 | while (((type = parser.next()) != XmlPullParser.END_TAG || |
| 723 | parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) { |
| 724 | |
| 725 | if (type != XmlPullParser.START_TAG) { |
| 726 | continue; |
| 727 | } |
| 728 | |
| 729 | final String name = parser.getName(); |
Philip Milne | c29f031 | 2012-02-22 16:34:51 -0800 | [diff] [blame] | 730 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 731 | if (TAG_REQUEST_FOCUS.equals(name)) { |
| 732 | parseRequestFocus(parser, parent); |
| 733 | } else if (TAG_INCLUDE.equals(name)) { |
| 734 | if (parser.getDepth() == 0) { |
| 735 | throw new InflateException("<include /> cannot be the root element"); |
| 736 | } |
| 737 | parseInclude(parser, parent, attrs); |
| 738 | } else if (TAG_MERGE.equals(name)) { |
| 739 | throw new InflateException("<merge /> must be the root element"); |
Romain Guy | 9c1223a | 2011-05-17 14:25:49 -0700 | [diff] [blame] | 740 | } else if (TAG_1995.equals(name)) { |
| 741 | final View view = new BlinkLayout(mContext, attrs); |
| 742 | final ViewGroup viewGroup = (ViewGroup) parent; |
| 743 | final ViewGroup.LayoutParams params = viewGroup.generateLayoutParams(attrs); |
| 744 | rInflate(parser, view, attrs, true); |
Philip Milne | c29f031 | 2012-02-22 16:34:51 -0800 | [diff] [blame] | 745 | viewGroup.addView(view, params); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 746 | } else { |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 747 | final View view = createViewFromTag(parent, name, attrs); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 748 | final ViewGroup viewGroup = (ViewGroup) parent; |
| 749 | final ViewGroup.LayoutParams params = viewGroup.generateLayoutParams(attrs); |
Romain Guy | 9295ada | 2010-06-15 11:33:24 -0700 | [diff] [blame] | 750 | rInflate(parser, view, attrs, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 751 | viewGroup.addView(view, params); |
| 752 | } |
| 753 | } |
| 754 | |
Romain Guy | 9295ada | 2010-06-15 11:33:24 -0700 | [diff] [blame] | 755 | if (finishInflate) parent.onFinishInflate(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 756 | } |
| 757 | |
| 758 | private void parseRequestFocus(XmlPullParser parser, View parent) |
| 759 | throws XmlPullParserException, IOException { |
| 760 | int type; |
| 761 | parent.requestFocus(); |
| 762 | final int currentDepth = parser.getDepth(); |
| 763 | while (((type = parser.next()) != XmlPullParser.END_TAG || |
| 764 | parser.getDepth() > currentDepth) && type != XmlPullParser.END_DOCUMENT) { |
| 765 | // Empty |
| 766 | } |
| 767 | } |
| 768 | |
| 769 | private void parseInclude(XmlPullParser parser, View parent, AttributeSet attrs) |
| 770 | throws XmlPullParserException, IOException { |
| 771 | |
| 772 | int type; |
| 773 | |
| 774 | if (parent instanceof ViewGroup) { |
| 775 | final int layout = attrs.getAttributeResourceValue(null, "layout", 0); |
| 776 | if (layout == 0) { |
| 777 | final String value = attrs.getAttributeValue(null, "layout"); |
| 778 | if (value == null) { |
| 779 | throw new InflateException("You must specifiy a layout in the" |
| 780 | + " include tag: <include layout=\"@layout/layoutID\" />"); |
| 781 | } else { |
| 782 | throw new InflateException("You must specifiy a valid layout " |
| 783 | + "reference. The layout ID " + value + " is not valid."); |
| 784 | } |
| 785 | } else { |
| 786 | final XmlResourceParser childParser = |
| 787 | getContext().getResources().getLayout(layout); |
| 788 | |
| 789 | try { |
| 790 | final AttributeSet childAttrs = Xml.asAttributeSet(childParser); |
| 791 | |
| 792 | while ((type = childParser.next()) != XmlPullParser.START_TAG && |
| 793 | type != XmlPullParser.END_DOCUMENT) { |
| 794 | // Empty. |
| 795 | } |
| 796 | |
| 797 | if (type != XmlPullParser.START_TAG) { |
| 798 | throw new InflateException(childParser.getPositionDescription() + |
| 799 | ": No start tag found!"); |
| 800 | } |
| 801 | |
| 802 | final String childName = childParser.getName(); |
| 803 | |
| 804 | if (TAG_MERGE.equals(childName)) { |
| 805 | // Inflate all children. |
Romain Guy | 9295ada | 2010-06-15 11:33:24 -0700 | [diff] [blame] | 806 | rInflate(childParser, parent, childAttrs, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 807 | } else { |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 808 | final View view = createViewFromTag(parent, childName, childAttrs); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 809 | final ViewGroup group = (ViewGroup) parent; |
| 810 | |
| 811 | // We try to load the layout params set in the <include /> tag. If |
| 812 | // they don't exist, we will rely on the layout params set in the |
| 813 | // included XML file. |
Philip Milne | c29f031 | 2012-02-22 16:34:51 -0800 | [diff] [blame] | 814 | TypedArray ta = getContext().obtainStyledAttributes(attrs, |
| 815 | R.styleable.ViewGroup_Layout); |
| 816 | boolean definesBothWidthAndHeight = |
| 817 | ta.hasValue(R.styleable.ViewGroup_Layout_layout_width) && |
| 818 | ta.hasValue(R.styleable.ViewGroup_Layout_layout_height); |
| 819 | AttributeSet attributes = definesBothWidthAndHeight ? attrs : childAttrs; |
| 820 | view.setLayoutParams(group.generateLayoutParams(attributes)); |
| 821 | ta.recycle(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 822 | |
| 823 | // Inflate all children. |
Romain Guy | 9295ada | 2010-06-15 11:33:24 -0700 | [diff] [blame] | 824 | rInflate(childParser, view, childAttrs, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 825 | |
| 826 | // Attempt to override the included layout's android:id with the |
| 827 | // one set on the <include /> tag itself. |
| 828 | TypedArray a = mContext.obtainStyledAttributes(attrs, |
| 829 | com.android.internal.R.styleable.View, 0, 0); |
| 830 | int id = a.getResourceId(com.android.internal.R.styleable.View_id, View.NO_ID); |
| 831 | // While we're at it, let's try to override android:visibility. |
| 832 | int visibility = a.getInt(com.android.internal.R.styleable.View_visibility, -1); |
| 833 | a.recycle(); |
| 834 | |
| 835 | if (id != View.NO_ID) { |
| 836 | view.setId(id); |
| 837 | } |
| 838 | |
| 839 | switch (visibility) { |
| 840 | case 0: |
| 841 | view.setVisibility(View.VISIBLE); |
| 842 | break; |
| 843 | case 1: |
| 844 | view.setVisibility(View.INVISIBLE); |
| 845 | break; |
| 846 | case 2: |
| 847 | view.setVisibility(View.GONE); |
| 848 | break; |
| 849 | } |
| 850 | |
| 851 | group.addView(view); |
| 852 | } |
| 853 | } finally { |
| 854 | childParser.close(); |
| 855 | } |
| 856 | } |
| 857 | } else { |
| 858 | throw new InflateException("<include /> can only be used inside of a ViewGroup"); |
| 859 | } |
| 860 | |
| 861 | final int currentDepth = parser.getDepth(); |
| 862 | while (((type = parser.next()) != XmlPullParser.END_TAG || |
| 863 | parser.getDepth() > currentDepth) && type != XmlPullParser.END_DOCUMENT) { |
| 864 | // Empty |
| 865 | } |
Romain Guy | 9c1223a | 2011-05-17 14:25:49 -0700 | [diff] [blame] | 866 | } |
| 867 | |
| 868 | private static class BlinkLayout extends FrameLayout { |
| 869 | private static final int MESSAGE_BLINK = 0x42; |
| 870 | private static final int BLINK_DELAY = 500; |
| 871 | |
| 872 | private boolean mBlink; |
| 873 | private boolean mBlinkState; |
| 874 | private final Handler mHandler; |
| 875 | |
| 876 | public BlinkLayout(Context context, AttributeSet attrs) { |
| 877 | super(context, attrs); |
| 878 | mHandler = new Handler(new Handler.Callback() { |
| 879 | @Override |
| 880 | public boolean handleMessage(Message msg) { |
| 881 | if (msg.what == MESSAGE_BLINK) { |
| 882 | if (mBlink) { |
| 883 | mBlinkState = !mBlinkState; |
| 884 | makeBlink(); |
| 885 | } |
| 886 | invalidate(); |
| 887 | return true; |
| 888 | } |
| 889 | return false; |
| 890 | } |
| 891 | }); |
| 892 | } |
| 893 | |
| 894 | private void makeBlink() { |
| 895 | Message message = mHandler.obtainMessage(MESSAGE_BLINK); |
| 896 | mHandler.sendMessageDelayed(message, BLINK_DELAY); |
| 897 | } |
| 898 | |
| 899 | @Override |
| 900 | protected void onAttachedToWindow() { |
| 901 | super.onAttachedToWindow(); |
| 902 | |
| 903 | mBlink = true; |
| 904 | mBlinkState = true; |
| 905 | |
| 906 | makeBlink(); |
| 907 | } |
| 908 | |
| 909 | @Override |
| 910 | protected void onDetachedFromWindow() { |
| 911 | super.onDetachedFromWindow(); |
| 912 | |
| 913 | mBlink = false; |
| 914 | mBlinkState = true; |
| 915 | |
| 916 | mHandler.removeMessages(MESSAGE_BLINK); |
| 917 | } |
| 918 | |
| 919 | @Override |
| 920 | protected void dispatchDraw(Canvas canvas) { |
| 921 | if (mBlinkState) { |
| 922 | super.dispatchDraw(canvas); |
| 923 | } |
| 924 | } |
| 925 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 926 | } |