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 | /** |
| 38 | * This class is used to instantiate layout XML file into its corresponding View |
| 39 | * objects. It is never be used directly -- use |
| 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; |
Gilles Debunne | 3030193 | 2010-06-16 18:32:00 -0700 | [diff] [blame] | 587 | return constructor.newInstance(args); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 588 | |
| 589 | } catch (NoSuchMethodException e) { |
| 590 | InflateException ie = new InflateException(attrs.getPositionDescription() |
| 591 | + ": Error inflating class " |
| 592 | + (prefix != null ? (prefix + name) : name)); |
| 593 | ie.initCause(e); |
| 594 | throw ie; |
| 595 | |
Gilles Debunne | 3030193 | 2010-06-16 18:32:00 -0700 | [diff] [blame] | 596 | } catch (ClassCastException e) { |
| 597 | // If loaded class is not a View subclass |
| 598 | InflateException ie = new InflateException(attrs.getPositionDescription() |
| 599 | + ": Class is not a View " |
| 600 | + (prefix != null ? (prefix + name) : name)); |
| 601 | ie.initCause(e); |
| 602 | throw ie; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 603 | } catch (ClassNotFoundException e) { |
| 604 | // If loadClass fails, we should propagate the exception. |
| 605 | throw e; |
| 606 | } catch (Exception e) { |
| 607 | InflateException ie = new InflateException(attrs.getPositionDescription() |
| 608 | + ": Error inflating class " |
Romain Guy | d03b880 | 2009-09-16 14:36:16 -0700 | [diff] [blame] | 609 | + (clazz == null ? "<unknown>" : clazz.getName())); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 610 | ie.initCause(e); |
| 611 | throw ie; |
| 612 | } |
| 613 | } |
| 614 | |
| 615 | /** |
Gilles Debunne | 3030193 | 2010-06-16 18:32:00 -0700 | [diff] [blame] | 616 | * 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] | 617 | */ |
| 618 | private void failNotAllowed(String name, String prefix, AttributeSet attrs) { |
Romain Guy | 9c1223a | 2011-05-17 14:25:49 -0700 | [diff] [blame] | 619 | throw new InflateException(attrs.getPositionDescription() |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 620 | + ": Class not allowed to be inflated " |
| 621 | + (prefix != null ? (prefix + name) : name)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 622 | } |
| 623 | |
| 624 | /** |
| 625 | * This routine is responsible for creating the correct subclass of View |
| 626 | * given the xml element name. Override it to handle custom view objects. If |
| 627 | * you override this in your subclass be sure to call through to |
| 628 | * super.onCreateView(name) for names you do not recognize. |
Philip Milne | c29f031 | 2012-02-22 16:34:51 -0800 | [diff] [blame^] | 629 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 630 | * @param name The fully qualified class name of the View to be create. |
| 631 | * @param attrs An AttributeSet of attributes to apply to the View. |
Philip Milne | c29f031 | 2012-02-22 16:34:51 -0800 | [diff] [blame^] | 632 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 633 | * @return View The View created. |
| 634 | */ |
| 635 | protected View onCreateView(String name, AttributeSet attrs) |
| 636 | throws ClassNotFoundException { |
| 637 | return createView(name, "android.view.", attrs); |
| 638 | } |
| 639 | |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 640 | /** |
| 641 | * Version of {@link #onCreateView(String, AttributeSet)} that also |
| 642 | * takes the future parent of the view being constructure. The default |
| 643 | * implementation simply calls {@link #onCreateView(String, AttributeSet)}. |
| 644 | * |
| 645 | * @param parent The future parent of the returned view. <em>Note that |
| 646 | * this may be null.</em> |
| 647 | * @param name The fully qualified class name of the View to be create. |
| 648 | * @param attrs An AttributeSet of attributes to apply to the View. |
| 649 | * |
| 650 | * @return View The View created. |
| 651 | */ |
| 652 | protected View onCreateView(View parent, String name, AttributeSet attrs) |
| 653 | throws ClassNotFoundException { |
| 654 | return onCreateView(name, attrs); |
| 655 | } |
| 656 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 657 | /* |
| 658 | * default visibility so the BridgeInflater can override it. |
| 659 | */ |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 660 | View createViewFromTag(View parent, String name, AttributeSet attrs) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 661 | if (name.equals("view")) { |
| 662 | name = attrs.getAttributeValue(null, "class"); |
| 663 | } |
| 664 | |
| 665 | if (DEBUG) System.out.println("******** Creating view: " + name); |
| 666 | |
| 667 | try { |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 668 | View view; |
| 669 | if (mFactory2 != null) view = mFactory2.onCreateView(parent, name, mContext, attrs); |
| 670 | else if (mFactory != null) view = mFactory.onCreateView(name, mContext, attrs); |
| 671 | else view = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 672 | |
Dianne Hackborn | 420829e | 2011-01-28 11:30:35 -0800 | [diff] [blame] | 673 | if (view == null && mPrivateFactory != null) { |
| 674 | view = mPrivateFactory.onCreateView(parent, name, mContext, attrs); |
| 675 | } |
Philip Milne | c29f031 | 2012-02-22 16:34:51 -0800 | [diff] [blame^] | 676 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 677 | if (view == null) { |
| 678 | if (-1 == name.indexOf('.')) { |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 679 | view = onCreateView(parent, name, attrs); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 680 | } else { |
| 681 | view = createView(name, null, attrs); |
| 682 | } |
| 683 | } |
| 684 | |
| 685 | if (DEBUG) System.out.println("Created view is: " + view); |
| 686 | return view; |
| 687 | |
| 688 | } catch (InflateException e) { |
| 689 | throw e; |
| 690 | |
| 691 | } catch (ClassNotFoundException e) { |
| 692 | InflateException ie = new InflateException(attrs.getPositionDescription() |
| 693 | + ": Error inflating class " + name); |
| 694 | ie.initCause(e); |
| 695 | throw ie; |
| 696 | |
| 697 | } catch (Exception e) { |
| 698 | InflateException ie = new InflateException(attrs.getPositionDescription() |
| 699 | + ": Error inflating class " + name); |
| 700 | ie.initCause(e); |
| 701 | throw ie; |
| 702 | } |
| 703 | } |
| 704 | |
| 705 | /** |
| 706 | * Recursive method used to descend down the xml hierarchy and instantiate |
| 707 | * views, instantiate their children, and then call onFinishInflate(). |
| 708 | */ |
Xavier Ducrohet | 7f9f99ea | 2011-08-11 10:16:17 -0700 | [diff] [blame] | 709 | void rInflate(XmlPullParser parser, View parent, final AttributeSet attrs, |
Romain Guy | 9295ada | 2010-06-15 11:33:24 -0700 | [diff] [blame] | 710 | boolean finishInflate) throws XmlPullParserException, IOException { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 711 | |
| 712 | final int depth = parser.getDepth(); |
| 713 | int type; |
| 714 | |
| 715 | while (((type = parser.next()) != XmlPullParser.END_TAG || |
| 716 | parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) { |
| 717 | |
| 718 | if (type != XmlPullParser.START_TAG) { |
| 719 | continue; |
| 720 | } |
| 721 | |
| 722 | final String name = parser.getName(); |
Philip Milne | c29f031 | 2012-02-22 16:34:51 -0800 | [diff] [blame^] | 723 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 724 | if (TAG_REQUEST_FOCUS.equals(name)) { |
| 725 | parseRequestFocus(parser, parent); |
| 726 | } else if (TAG_INCLUDE.equals(name)) { |
| 727 | if (parser.getDepth() == 0) { |
| 728 | throw new InflateException("<include /> cannot be the root element"); |
| 729 | } |
| 730 | parseInclude(parser, parent, attrs); |
| 731 | } else if (TAG_MERGE.equals(name)) { |
| 732 | throw new InflateException("<merge /> must be the root element"); |
Romain Guy | 9c1223a | 2011-05-17 14:25:49 -0700 | [diff] [blame] | 733 | } else if (TAG_1995.equals(name)) { |
| 734 | final View view = new BlinkLayout(mContext, attrs); |
| 735 | final ViewGroup viewGroup = (ViewGroup) parent; |
| 736 | final ViewGroup.LayoutParams params = viewGroup.generateLayoutParams(attrs); |
| 737 | rInflate(parser, view, attrs, true); |
Philip Milne | c29f031 | 2012-02-22 16:34:51 -0800 | [diff] [blame^] | 738 | viewGroup.addView(view, params); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 739 | } else { |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 740 | final View view = createViewFromTag(parent, name, attrs); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 741 | final ViewGroup viewGroup = (ViewGroup) parent; |
| 742 | final ViewGroup.LayoutParams params = viewGroup.generateLayoutParams(attrs); |
Romain Guy | 9295ada | 2010-06-15 11:33:24 -0700 | [diff] [blame] | 743 | rInflate(parser, view, attrs, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 744 | viewGroup.addView(view, params); |
| 745 | } |
| 746 | } |
| 747 | |
Romain Guy | 9295ada | 2010-06-15 11:33:24 -0700 | [diff] [blame] | 748 | if (finishInflate) parent.onFinishInflate(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 749 | } |
| 750 | |
| 751 | private void parseRequestFocus(XmlPullParser parser, View parent) |
| 752 | throws XmlPullParserException, IOException { |
| 753 | int type; |
| 754 | parent.requestFocus(); |
| 755 | final int currentDepth = parser.getDepth(); |
| 756 | while (((type = parser.next()) != XmlPullParser.END_TAG || |
| 757 | parser.getDepth() > currentDepth) && type != XmlPullParser.END_DOCUMENT) { |
| 758 | // Empty |
| 759 | } |
| 760 | } |
| 761 | |
| 762 | private void parseInclude(XmlPullParser parser, View parent, AttributeSet attrs) |
| 763 | throws XmlPullParserException, IOException { |
| 764 | |
| 765 | int type; |
| 766 | |
| 767 | if (parent instanceof ViewGroup) { |
| 768 | final int layout = attrs.getAttributeResourceValue(null, "layout", 0); |
| 769 | if (layout == 0) { |
| 770 | final String value = attrs.getAttributeValue(null, "layout"); |
| 771 | if (value == null) { |
| 772 | throw new InflateException("You must specifiy a layout in the" |
| 773 | + " include tag: <include layout=\"@layout/layoutID\" />"); |
| 774 | } else { |
| 775 | throw new InflateException("You must specifiy a valid layout " |
| 776 | + "reference. The layout ID " + value + " is not valid."); |
| 777 | } |
| 778 | } else { |
| 779 | final XmlResourceParser childParser = |
| 780 | getContext().getResources().getLayout(layout); |
| 781 | |
| 782 | try { |
| 783 | final AttributeSet childAttrs = Xml.asAttributeSet(childParser); |
| 784 | |
| 785 | while ((type = childParser.next()) != XmlPullParser.START_TAG && |
| 786 | type != XmlPullParser.END_DOCUMENT) { |
| 787 | // Empty. |
| 788 | } |
| 789 | |
| 790 | if (type != XmlPullParser.START_TAG) { |
| 791 | throw new InflateException(childParser.getPositionDescription() + |
| 792 | ": No start tag found!"); |
| 793 | } |
| 794 | |
| 795 | final String childName = childParser.getName(); |
| 796 | |
| 797 | if (TAG_MERGE.equals(childName)) { |
| 798 | // Inflate all children. |
Romain Guy | 9295ada | 2010-06-15 11:33:24 -0700 | [diff] [blame] | 799 | rInflate(childParser, parent, childAttrs, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 800 | } else { |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 801 | final View view = createViewFromTag(parent, childName, childAttrs); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 802 | final ViewGroup group = (ViewGroup) parent; |
| 803 | |
| 804 | // We try to load the layout params set in the <include /> tag. If |
| 805 | // they don't exist, we will rely on the layout params set in the |
| 806 | // included XML file. |
Philip Milne | c29f031 | 2012-02-22 16:34:51 -0800 | [diff] [blame^] | 807 | TypedArray ta = getContext().obtainStyledAttributes(attrs, |
| 808 | R.styleable.ViewGroup_Layout); |
| 809 | boolean definesBothWidthAndHeight = |
| 810 | ta.hasValue(R.styleable.ViewGroup_Layout_layout_width) && |
| 811 | ta.hasValue(R.styleable.ViewGroup_Layout_layout_height); |
| 812 | AttributeSet attributes = definesBothWidthAndHeight ? attrs : childAttrs; |
| 813 | view.setLayoutParams(group.generateLayoutParams(attributes)); |
| 814 | ta.recycle(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 815 | |
| 816 | // Inflate all children. |
Romain Guy | 9295ada | 2010-06-15 11:33:24 -0700 | [diff] [blame] | 817 | rInflate(childParser, view, childAttrs, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 818 | |
| 819 | // Attempt to override the included layout's android:id with the |
| 820 | // one set on the <include /> tag itself. |
| 821 | TypedArray a = mContext.obtainStyledAttributes(attrs, |
| 822 | com.android.internal.R.styleable.View, 0, 0); |
| 823 | int id = a.getResourceId(com.android.internal.R.styleable.View_id, View.NO_ID); |
| 824 | // While we're at it, let's try to override android:visibility. |
| 825 | int visibility = a.getInt(com.android.internal.R.styleable.View_visibility, -1); |
| 826 | a.recycle(); |
| 827 | |
| 828 | if (id != View.NO_ID) { |
| 829 | view.setId(id); |
| 830 | } |
| 831 | |
| 832 | switch (visibility) { |
| 833 | case 0: |
| 834 | view.setVisibility(View.VISIBLE); |
| 835 | break; |
| 836 | case 1: |
| 837 | view.setVisibility(View.INVISIBLE); |
| 838 | break; |
| 839 | case 2: |
| 840 | view.setVisibility(View.GONE); |
| 841 | break; |
| 842 | } |
| 843 | |
| 844 | group.addView(view); |
| 845 | } |
| 846 | } finally { |
| 847 | childParser.close(); |
| 848 | } |
| 849 | } |
| 850 | } else { |
| 851 | throw new InflateException("<include /> can only be used inside of a ViewGroup"); |
| 852 | } |
| 853 | |
| 854 | final int currentDepth = parser.getDepth(); |
| 855 | while (((type = parser.next()) != XmlPullParser.END_TAG || |
| 856 | parser.getDepth() > currentDepth) && type != XmlPullParser.END_DOCUMENT) { |
| 857 | // Empty |
| 858 | } |
Romain Guy | 9c1223a | 2011-05-17 14:25:49 -0700 | [diff] [blame] | 859 | } |
| 860 | |
| 861 | private static class BlinkLayout extends FrameLayout { |
| 862 | private static final int MESSAGE_BLINK = 0x42; |
| 863 | private static final int BLINK_DELAY = 500; |
| 864 | |
| 865 | private boolean mBlink; |
| 866 | private boolean mBlinkState; |
| 867 | private final Handler mHandler; |
| 868 | |
| 869 | public BlinkLayout(Context context, AttributeSet attrs) { |
| 870 | super(context, attrs); |
| 871 | mHandler = new Handler(new Handler.Callback() { |
| 872 | @Override |
| 873 | public boolean handleMessage(Message msg) { |
| 874 | if (msg.what == MESSAGE_BLINK) { |
| 875 | if (mBlink) { |
| 876 | mBlinkState = !mBlinkState; |
| 877 | makeBlink(); |
| 878 | } |
| 879 | invalidate(); |
| 880 | return true; |
| 881 | } |
| 882 | return false; |
| 883 | } |
| 884 | }); |
| 885 | } |
| 886 | |
| 887 | private void makeBlink() { |
| 888 | Message message = mHandler.obtainMessage(MESSAGE_BLINK); |
| 889 | mHandler.sendMessageDelayed(message, BLINK_DELAY); |
| 890 | } |
| 891 | |
| 892 | @Override |
| 893 | protected void onAttachedToWindow() { |
| 894 | super.onAttachedToWindow(); |
| 895 | |
| 896 | mBlink = true; |
| 897 | mBlinkState = true; |
| 898 | |
| 899 | makeBlink(); |
| 900 | } |
| 901 | |
| 902 | @Override |
| 903 | protected void onDetachedFromWindow() { |
| 904 | super.onDetachedFromWindow(); |
| 905 | |
| 906 | mBlink = false; |
| 907 | mBlinkState = true; |
| 908 | |
| 909 | mHandler.removeMessages(MESSAGE_BLINK); |
| 910 | } |
| 911 | |
| 912 | @Override |
| 913 | protected void dispatchDraw(Canvas canvas) { |
| 914 | if (mBlinkState) { |
| 915 | super.dispatchDraw(canvas); |
| 916 | } |
| 917 | } |
| 918 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 919 | } |