• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 package android.view;
18 
19 import com.android.ide.common.rendering.api.ILayoutLog;
20 import com.android.layoutlib.bridge.Bridge;
21 import com.android.tools.layoutlib.annotations.LayoutlibDelegate;
22 
23 import org.xmlpull.v1.XmlPullParser;
24 import org.xmlpull.v1.XmlPullParserException;
25 
26 import android.content.Context;
27 import android.content.res.TypedArray;
28 import android.content.res.XmlResourceParser;
29 import android.util.AttributeSet;
30 import android.util.TypedValue;
31 import android.util.Xml;
32 
33 import java.io.IOException;
34 
35 /**
36  * Delegate used to provide new implementation of a select few methods of {@link LayoutInflater}
37  *
38  * Through the layoutlib_create tool, the original  methods of LayoutInflater have been replaced
39  * by calls to methods of the same name in this delegate class.
40  *
41  */
42 public class LayoutInflater_Delegate {
43     private static final String TAG_MERGE = "merge";
44 
45     private static final String ATTR_LAYOUT = "layout";
46 
47     private static final int[] ATTRS_THEME = new int[] {
48             com.android.internal.R.attr.theme };
49 
50     public static boolean sIsInInclude = false;
51 
52     /**
53      * Recursive method used to descend down the xml hierarchy and instantiate
54      * views, instantiate their children, and then call onFinishInflate().
55      *
56      * This implementation just records the merge status before calling the default implementation.
57      */
58     @LayoutlibDelegate
rInflate(LayoutInflater thisInflater, XmlPullParser parser, View parent, Context context, AttributeSet attrs, boolean finishInflate)59     /* package */ static void rInflate(LayoutInflater thisInflater, XmlPullParser parser,
60             View parent, Context context, AttributeSet attrs, boolean finishInflate)
61             throws XmlPullParserException, IOException {
62 
63         if (!finishInflate) {
64             // this is a merge rInflate!
65             if (thisInflater instanceof BridgeInflater) {
66                 ((BridgeInflater) thisInflater).setIsInMerge(true);
67             }
68         }
69 
70         // ---- START DEFAULT IMPLEMENTATION.
71 
72         thisInflater.rInflate_Original(parser, parent, context, attrs, finishInflate);
73 
74         // ---- END DEFAULT IMPLEMENTATION.
75 
76         if (!finishInflate) {
77             // this is a merge rInflate!
78             if (thisInflater instanceof BridgeInflater) {
79                 ((BridgeInflater) thisInflater).setIsInMerge(false);
80             }
81         }
82     }
83 
84     @LayoutlibDelegate
parseInclude(LayoutInflater thisInflater, XmlPullParser parser, Context context, View parent, AttributeSet attrs)85     public static void parseInclude(LayoutInflater thisInflater, XmlPullParser parser,
86             Context context, View parent, AttributeSet attrs)
87             throws XmlPullParserException, IOException {
88         int type;
89 
90         if (parent instanceof ViewGroup) {
91             // Apply a theme wrapper, if requested. This is sort of a weird
92             // edge case, since developers think the <include> overwrites
93             // values in the AttributeSet of the included View. So, if the
94             // included View has a theme attribute, we'll need to ignore it.
95             final TypedArray ta = context.obtainStyledAttributes(attrs, ATTRS_THEME);
96             final int themeResId = ta.getResourceId(0, 0);
97             final boolean hasThemeOverride = themeResId != 0;
98             if (hasThemeOverride) {
99                 context = new ContextThemeWrapper(context, themeResId);
100             }
101             ta.recycle();
102 
103             // If the layout is pointing to a theme attribute, we have to
104             // massage the value to get a resource identifier out of it.
105             int layout = attrs.getAttributeResourceValue(null, ATTR_LAYOUT, 0);
106             if (layout == 0) {
107                 final String value = attrs.getAttributeValue(null, ATTR_LAYOUT);
108                 if (value == null || value.isEmpty()) {
109                     Bridge.getLog().error(ILayoutLog.TAG_BROKEN, "You must specify a layout in the"
110                             + " include tag: <include layout=\"@layout/layoutID\" />", null, null);
111                     LayoutInflater.consumeChildElements(parser);
112                     return;
113                 }
114 
115                 // Attempt to resolve the "?attr/name" string to an identifier.
116                 layout = context.getResources().getIdentifier(value.substring(1), null, null);
117             }
118 
119             // The layout might be referencing a theme attribute.
120             // ---- START CHANGES
121             if (layout != 0) {
122                 final TypedValue tempValue = new TypedValue();
123                 if (context.getTheme().resolveAttribute(layout, tempValue, true)) {
124                     layout = tempValue.resourceId;
125                 }
126             }
127             // ---- END CHANGES
128 
129             if (layout == 0) {
130                 final String value = attrs.getAttributeValue(null, ATTR_LAYOUT);
131                 if (value == null) {
132                     Bridge.getLog().error(ILayoutLog.TAG_BROKEN, "You must specify a layout in the"
133                             + " include tag: <include layout=\"@layout/layoutID\" />", null, null);
134                 } else {
135                     Bridge.getLog().error(ILayoutLog.TAG_BROKEN, "You must specify a valid layout "
136                             + "reference. The layout ID " + value + " is not valid.", null, null);
137                 }
138             } else {
139                 try (XmlResourceParser childParser = thisInflater.getContext().getResources()
140                         .getLayout(layout)) {
141                     final AttributeSet childAttrs = Xml.asAttributeSet(childParser);
142 
143                     while ((type = childParser.next()) != XmlPullParser.START_TAG &&
144                             type != XmlPullParser.END_DOCUMENT) {
145                         // Empty.
146                     }
147 
148                     if (type != XmlPullParser.START_TAG) {
149                         Bridge.getLog().error(ILayoutLog.TAG_BROKEN,
150                                 childParser.getPositionDescription() + ": No start tag found!",
151                                 null, null);
152                         LayoutInflater.consumeChildElements(parser);
153                         return;
154                     }
155 
156                     final String childName = childParser.getName();
157 
158                     if (TAG_MERGE.equals(childName)) {
159                         // Inflate all children.
160                         thisInflater.rInflate(childParser, parent, context, childAttrs, false);
161                     } else {
162                         final View view = thisInflater.createViewFromTag(parent, childName,
163                                 context, childAttrs, hasThemeOverride);
164                         final ViewGroup group = (ViewGroup) parent;
165 
166                         final TypedArray a = context.obtainStyledAttributes(
167                                 attrs, com.android.internal.R.styleable.Include);
168                         final int id = a.getResourceId(
169                                 com.android.internal.R.styleable.Include_id, View.NO_ID);
170                         final int visibility = a.getInt(
171                                 com.android.internal.R.styleable.Include_visibility, -1);
172                         a.recycle();
173 
174                         // We try to load the layout params set in the <include /> tag. If
175                         // they don't exist, we will rely on the layout params set in the
176                         // included XML file.
177                         // During a layoutparams generation, a runtime exception is thrown
178                         // if either layout_width or layout_height is missing. We catch
179                         // this exception and set localParams accordingly: true means we
180                         // successfully loaded layout params from the <include /> tag,
181                         // false means we need to rely on the included layout params.
182                         ViewGroup.LayoutParams params = null;
183                         try {
184                             // ---- START CHANGES
185                             sIsInInclude = true;
186                             // ---- END CHANGES
187 
188                             params = group.generateLayoutParams(attrs);
189                         } catch (RuntimeException ignored) {
190                             // Ignore, just fail over to child attrs.
191                         } finally {
192                             // ---- START CHANGES
193                             sIsInInclude = false;
194                             // ---- END CHANGES
195                         }
196                         if (params == null) {
197                             params = group.generateLayoutParams(childAttrs);
198                         }
199                         view.setLayoutParams(params);
200 
201                         // Inflate all children.
202                         thisInflater.rInflateChildren(childParser, view, childAttrs, true);
203 
204                         if (id != View.NO_ID) {
205                             view.setId(id);
206                         }
207 
208                         switch (visibility) {
209                             case 0:
210                                 view.setVisibility(View.VISIBLE);
211                                 break;
212                             case 1:
213                                 view.setVisibility(View.INVISIBLE);
214                                 break;
215                             case 2:
216                                 view.setVisibility(View.GONE);
217                                 break;
218                         }
219 
220                         group.addView(view);
221                     }
222                 }
223             }
224         } else {
225             Bridge.getLog().error(ILayoutLog.TAG_BROKEN,
226                     "<include /> can only be used inside of a ViewGroup",
227                     null, null);
228         }
229 
230         LayoutInflater.consumeChildElements(parser);
231     }
232 }
233