LayoutInflater_Delegate.java revision 9a4fe29c8d92014d2d9a848e9116b8cc9d0842f9
1/* 2 * Copyright (C) 2011 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package android.view; 18 19import com.android.layoutlib.bridge.android.BridgeInflater; 20import com.android.tools.layoutlib.annotations.LayoutlibDelegate; 21 22import org.xmlpull.v1.XmlPullParser; 23import org.xmlpull.v1.XmlPullParserException; 24 25import android.util.AttributeSet; 26 27import java.io.IOException; 28 29/** 30 * Delegate used to provide new implementation of a select few methods of {@link LayoutInflater} 31 * 32 * Through the layoutlib_create tool, the original methods of LayoutInflater have been replaced 33 * by calls to methods of the same name in this delegate class. 34 * 35 */ 36public class LayoutInflater_Delegate { 37 38 /** 39 * Recursive method used to descend down the xml hierarchy and instantiate 40 * views, instantiate their children, and then call onFinishInflate(). 41 */ 42 @LayoutlibDelegate 43 /*package*/ static void rInflate(LayoutInflater thisInflater, 44 XmlPullParser parser, View parent, final AttributeSet attrs, 45 boolean finishInflate) throws XmlPullParserException, IOException { 46 47 if (finishInflate == false) { 48 // this is a merge rInflate! 49 if (thisInflater instanceof BridgeInflater) { 50 ((BridgeInflater) thisInflater).setIsInMerge(true); 51 } 52 } 53 54 // ---- START DEFAULT IMPLEMENTATION. 55 56 final int depth = parser.getDepth(); 57 int type; 58 59 while (((type = parser.next()) != XmlPullParser.END_TAG || 60 parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) { 61 62 if (type != XmlPullParser.START_TAG) { 63 continue; 64 } 65 66 final String name = parser.getName(); 67 68 if (LayoutInflater.TAG_REQUEST_FOCUS.equals(name)) { 69 thisInflater.parseRequestFocus(parser, parent); 70 } else if (LayoutInflater.TAG_INCLUDE.equals(name)) { 71 if (parser.getDepth() == 0) { 72 throw new InflateException("<include /> cannot be the root element"); 73 } 74 thisInflater.parseInclude(parser, parent, attrs); 75 } else if (LayoutInflater.TAG_MERGE.equals(name)) { 76 throw new InflateException("<merge /> must be the root element"); 77 } else { 78 final View view = thisInflater.createViewFromTag(parent, name, attrs); 79 final ViewGroup viewGroup = (ViewGroup) parent; 80 final ViewGroup.LayoutParams params = viewGroup.generateLayoutParams(attrs); 81 thisInflater.rInflate(parser, view, attrs, true); 82 viewGroup.addView(view, params); 83 } 84 } 85 86 if (finishInflate) parent.onFinishInflate(); 87 88 // ---- END DEFAULT IMPLEMENTATION. 89 90 if (finishInflate == false) { 91 // this is a merge rInflate! 92 if (thisInflater instanceof BridgeInflater) { 93 ((BridgeInflater) thisInflater).setIsInMerge(false); 94 } 95 } 96 } 97} 98