2024-04-07 21:51:11 +02:00
|
|
|
/*
|
|
|
|
|
* portions Copyright (C) 2007 The Android Open Source Project
|
|
|
|
|
*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
2022-10-02 23:06:56 +02:00
|
|
|
package android.view;
|
|
|
|
|
|
|
|
|
|
import android.content.Context;
|
2023-11-08 21:29:46 +01:00
|
|
|
import android.content.res.TypedArray;
|
2023-06-23 18:35:00 +02:00
|
|
|
import android.content.res.XmlResourceParser;
|
2022-10-02 23:06:56 +02:00
|
|
|
import android.util.AttributeSet;
|
2023-09-06 17:42:24 +02:00
|
|
|
import android.util.Slog;
|
2022-10-02 23:06:56 +02:00
|
|
|
import android.util.Xml;
|
2023-06-22 11:45:46 +02:00
|
|
|
import java.io.FileReader;
|
|
|
|
|
import java.lang.reflect.Constructor;
|
2022-10-02 23:06:56 +02:00
|
|
|
import org.xmlpull.v1.XmlPullParser;
|
|
|
|
|
import org.xmlpull.v1.XmlPullParserException;
|
|
|
|
|
import org.xmlpull.v1.XmlPullParserFactory;
|
|
|
|
|
|
|
|
|
|
public class LayoutInflater {
|
2023-09-06 17:42:24 +02:00
|
|
|
private static final String TAG = "LayoutInflater";
|
2022-10-02 23:06:56 +02:00
|
|
|
|
|
|
|
|
public interface Factory {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public interface Factory2 {
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-17 10:46:24 +02:00
|
|
|
private Factory2 mFactory2;
|
2024-06-15 22:32:01 +02:00
|
|
|
private Context context;
|
|
|
|
|
|
|
|
|
|
public LayoutInflater(Context context) {
|
|
|
|
|
this.context = context;
|
|
|
|
|
}
|
2022-10-02 23:06:56 +02:00
|
|
|
|
|
|
|
|
public final LayoutInflater.Factory getFactory() {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-22 11:45:46 +02:00
|
|
|
public void setFactory2(Factory2 factory) {
|
2023-08-17 10:46:24 +02:00
|
|
|
this.mFactory2 = factory;
|
2022-10-02 23:06:56 +02:00
|
|
|
}
|
|
|
|
|
|
2023-06-22 11:45:46 +02:00
|
|
|
public static LayoutInflater from(Context context) {
|
2024-06-15 22:32:01 +02:00
|
|
|
return new LayoutInflater(context);
|
2022-10-02 23:06:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public final View createView(String name, String prefix, AttributeSet attrs) throws Exception {
|
2023-09-06 17:42:24 +02:00
|
|
|
Slog.v(TAG, ">>>>>>>>>>>>>>>>>>>>>>>>> createView(" + name + ", " + prefix + ", " + attrs + ");");
|
2022-10-02 23:06:56 +02:00
|
|
|
|
2023-07-14 18:02:04 +02:00
|
|
|
String view_class_name = prefix!=null ? prefix + name : name;
|
2022-10-02 23:06:56 +02:00
|
|
|
Class view_class = Class.forName(view_class_name);
|
|
|
|
|
|
2023-07-14 18:02:04 +02:00
|
|
|
Constructor constructor = view_class.getConstructor(Context.class, AttributeSet.class);
|
2022-10-02 23:06:56 +02:00
|
|
|
|
2024-06-15 22:32:01 +02:00
|
|
|
Context context = this.context;
|
2023-11-08 21:29:46 +01:00
|
|
|
final TypedArray ta = context.obtainStyledAttributes(attrs, new int[]{com.android.internal.R.attr.theme});
|
|
|
|
|
final int themeResId = ta.getResourceId(0, 0);
|
|
|
|
|
if (themeResId != 0) {
|
|
|
|
|
context = new ContextThemeWrapper(context, themeResId);
|
|
|
|
|
}
|
|
|
|
|
ta.recycle();
|
|
|
|
|
View view_instance = (View)constructor.newInstance(context, attrs);
|
2022-10-02 23:06:56 +02:00
|
|
|
|
|
|
|
|
return view_instance;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* taken as-is
|
|
|
|
|
*/
|
|
|
|
|
protected View onCreateView(String name, AttributeSet attrs) throws Exception {
|
|
|
|
|
try { // FIXME ugly
|
|
|
|
|
return createView(name, "android.view.", attrs);
|
2023-06-22 11:45:46 +02:00
|
|
|
} catch (java.lang.ClassNotFoundException e) {
|
2022-10-02 23:06:56 +02:00
|
|
|
return createView(name, "android.widget.", attrs);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* taken as-is
|
|
|
|
|
*/
|
|
|
|
|
protected View onCreateView(View parent, String name, AttributeSet attrs) throws Exception {
|
|
|
|
|
return onCreateView(name, attrs);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
View createViewFromTag(View parent, String name, AttributeSet attrs) throws Exception {
|
|
|
|
|
if (name.equals("view")) {
|
|
|
|
|
name = attrs.getAttributeValue(null, "class");
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-06 17:42:24 +02:00
|
|
|
Slog.v(TAG, "******** Creating view: " + name);
|
2022-10-02 23:06:56 +02:00
|
|
|
|
|
|
|
|
View view;
|
|
|
|
|
|
|
|
|
|
if (-1 == name.indexOf('.')) {
|
|
|
|
|
view = onCreateView(parent, name, attrs);
|
|
|
|
|
} else {
|
|
|
|
|
view = createView(name, null, attrs);
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-06 17:42:24 +02:00
|
|
|
Slog.v(TAG, "Created view is: " + view);
|
2022-10-02 23:06:56 +02:00
|
|
|
return view;
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-01 12:39:20 +02:00
|
|
|
public View inflate(int resource, ViewGroup root) {
|
2023-06-22 11:45:46 +02:00
|
|
|
return inflate(resource, root, root != null);
|
|
|
|
|
}
|
2022-10-02 23:06:56 +02:00
|
|
|
|
2023-09-01 12:39:20 +02:00
|
|
|
public View inflate(int layoutResID, ViewGroup root, boolean attachToRoot) {
|
2022-10-02 23:06:56 +02:00
|
|
|
|
2024-03-07 15:47:10 +01:00
|
|
|
Slog.v(TAG, "inflating view from id: " + String.format("%x", layoutResID));
|
2024-06-15 22:32:01 +02:00
|
|
|
XmlResourceParser xpp = context.getResources().getLayout(layoutResID);
|
2022-10-02 23:06:56 +02:00
|
|
|
|
2023-09-01 12:39:20 +02:00
|
|
|
try {
|
|
|
|
|
return inflate(xpp, root, attachToRoot);
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
|
}
|
2023-06-22 11:45:46 +02:00
|
|
|
}
|
2022-10-02 23:06:56 +02:00
|
|
|
|
|
|
|
|
public View inflate(XmlPullParser parser, ViewGroup root, boolean attachToRoot) throws Exception {
|
|
|
|
|
|
|
|
|
|
final AttributeSet attrs = Xml.asAttributeSet(parser);
|
|
|
|
|
|
|
|
|
|
View result = root;
|
|
|
|
|
|
|
|
|
|
// Look for the root node.
|
|
|
|
|
int type;
|
|
|
|
|
while ((type = parser.next()) != XmlPullParser.START_TAG &&
|
2023-06-22 11:45:46 +02:00
|
|
|
type != XmlPullParser.END_DOCUMENT) {
|
2022-10-02 23:06:56 +02:00
|
|
|
// Empty
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (type != XmlPullParser.START_TAG) {
|
|
|
|
|
throw new Exception(parser.getPositionDescription() + ": No start tag found!");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
final String name = parser.getName();
|
|
|
|
|
|
2023-09-06 17:42:24 +02:00
|
|
|
Slog.v(TAG, "**************************");
|
|
|
|
|
Slog.v(TAG, "Creating root view: " + name);
|
|
|
|
|
Slog.v(TAG, "**************************");
|
2022-10-02 23:06:56 +02:00
|
|
|
|
|
|
|
|
if (name.equals("merge")) {
|
|
|
|
|
if (root == null || !attachToRoot) {
|
|
|
|
|
throw new Exception("<merge /> can be used only with a valid ViewGroup root and attachToRoot=true");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
rInflate(parser, root, attrs, false);
|
|
|
|
|
} else {
|
|
|
|
|
// Temp is the root view that was found in the xml
|
|
|
|
|
View temp;
|
|
|
|
|
if (name.equals("blink")) {
|
|
|
|
|
throw new Exception("<blink> not supported atm");
|
|
|
|
|
/*temp = new BlinkLayout(mContext, attrs);*/
|
|
|
|
|
} else {
|
|
|
|
|
temp = createViewFromTag(root, name, attrs);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ViewGroup.LayoutParams params = null;
|
|
|
|
|
|
|
|
|
|
if (root != null) {
|
2023-09-06 17:42:24 +02:00
|
|
|
Slog.v(TAG, "Creating params from root: " + root);
|
2022-10-02 23:06:56 +02:00
|
|
|
|
|
|
|
|
// Create layout params that match root, if supplied
|
|
|
|
|
params = root.generateLayoutParams(attrs);
|
2024-02-09 16:03:12 +01:00
|
|
|
params.resolveLayoutDirection(root.getLayoutDirection());
|
2022-10-02 23:06:56 +02:00
|
|
|
if (!attachToRoot) {
|
|
|
|
|
// Set the layout params for temp if we are not
|
|
|
|
|
// attaching. (If we are, we use addView, below)
|
|
|
|
|
temp.setLayoutParams(params);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-06 17:42:24 +02:00
|
|
|
Slog.v(TAG, "-----> start inflating children");
|
2022-10-02 23:06:56 +02:00
|
|
|
|
|
|
|
|
// Inflate all children under temp
|
|
|
|
|
rInflate(parser, temp, attrs, true);
|
|
|
|
|
|
2023-09-06 17:42:24 +02:00
|
|
|
Slog.v(TAG, "-----> done inflating children");
|
2022-10-02 23:06:56 +02:00
|
|
|
|
|
|
|
|
// We are supposed to attach all the views we found (int temp)
|
|
|
|
|
// to root. Do that now.
|
|
|
|
|
if (root != null && attachToRoot) {
|
|
|
|
|
root.addView(temp, params);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Decide whether to return the root that was passed in or the
|
|
|
|
|
// top view found in xml.
|
|
|
|
|
if (root == null || !attachToRoot) {
|
|
|
|
|
result = temp;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void rInflate(XmlPullParser parser, View parent, final AttributeSet attrs, boolean finishInflate) throws Exception {
|
|
|
|
|
|
|
|
|
|
final int depth = parser.getDepth();
|
|
|
|
|
int type;
|
|
|
|
|
|
|
|
|
|
while (((type = parser.next()) != XmlPullParser.END_TAG ||
|
2023-06-22 11:45:46 +02:00
|
|
|
parser.getDepth() > depth) &&
|
|
|
|
|
type != XmlPullParser.END_DOCUMENT) {
|
2022-10-02 23:06:56 +02:00
|
|
|
|
|
|
|
|
if (type != XmlPullParser.START_TAG) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
final String name = parser.getName();
|
|
|
|
|
|
|
|
|
|
if (name.equals("requestFocus")) {
|
|
|
|
|
throw new Exception("<requestFocus /> not supported atm");
|
2023-06-22 11:45:46 +02:00
|
|
|
// parseRequestFocus(parser, parent);
|
2022-10-02 23:06:56 +02:00
|
|
|
} else if (name.equals("include")) {
|
2023-08-06 14:32:28 +02:00
|
|
|
if (parser.getDepth() == 0) {
|
2022-10-02 23:06:56 +02:00
|
|
|
throw new Exception("<include /> cannot be the root element");
|
|
|
|
|
}
|
2023-08-06 14:32:28 +02:00
|
|
|
parseInclude(parser, parent, attrs);
|
2022-10-02 23:06:56 +02:00
|
|
|
} else if (name.equals("merge")) {
|
|
|
|
|
throw new Exception("<merge /> must be the root element");
|
|
|
|
|
} else if (name.equals("blink")) {
|
|
|
|
|
throw new Exception("<blink> not supported atm");
|
|
|
|
|
/*final View view = new BlinkLayout(mContext, attrs);
|
|
|
|
|
final ViewGroup viewGroup = (ViewGroup) parent;
|
|
|
|
|
final ViewGroup.LayoutParams params = viewGroup.generateLayoutParams(attrs);
|
|
|
|
|
rInflate(parser, view, attrs, true);
|
|
|
|
|
viewGroup.addView(view, params);*/
|
|
|
|
|
} else {
|
|
|
|
|
final View view = createViewFromTag(parent, name, attrs);
|
2023-06-22 11:45:46 +02:00
|
|
|
final ViewGroup viewGroup = (ViewGroup)parent;
|
2024-06-24 18:44:31 +02:00
|
|
|
ViewGroup.LayoutParams params = null;
|
|
|
|
|
try {
|
|
|
|
|
params = viewGroup.generateLayoutParams(attrs);
|
|
|
|
|
} catch (RuntimeException e) {
|
|
|
|
|
params = viewGroup.generateDefaultLayoutParams();
|
|
|
|
|
}
|
2024-02-09 16:03:12 +01:00
|
|
|
params.resolveLayoutDirection(viewGroup.getLayoutDirection());
|
2022-10-02 23:06:56 +02:00
|
|
|
rInflate(parser, view, attrs, true);
|
|
|
|
|
viewGroup.addView(view, params);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-22 11:45:46 +02:00
|
|
|
if (finishInflate)
|
|
|
|
|
parent.onFinishInflate();
|
2022-10-02 23:06:56 +02:00
|
|
|
}
|
2023-08-06 14:32:28 +02:00
|
|
|
|
|
|
|
|
private void parseInclude(XmlPullParser parser, View parent, AttributeSet attrs) throws Exception {
|
2023-08-17 10:46:24 +02:00
|
|
|
int type;
|
|
|
|
|
|
2023-08-06 14:32:28 +02:00
|
|
|
int layout = attrs.getAttributeResourceValue(null, "layout", 0);
|
|
|
|
|
|
2024-06-15 22:32:01 +02:00
|
|
|
final XmlResourceParser childParser = context.getResources().getLayout(layout);
|
2023-08-17 10:46:24 +02:00
|
|
|
final AttributeSet childAttrs = Xml.asAttributeSet(childParser);
|
|
|
|
|
|
|
|
|
|
while ((type = childParser.next()) != XmlPullParser.START_TAG &&
|
|
|
|
|
type != XmlPullParser.END_DOCUMENT) {
|
|
|
|
|
// Empty.
|
|
|
|
|
}
|
|
|
|
|
if (type != XmlPullParser.START_TAG) {
|
|
|
|
|
throw new Exception(childParser.getPositionDescription() + ": No start tag found!");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
final String childName = childParser.getName();
|
|
|
|
|
if ("merge".equals(childName)) {
|
|
|
|
|
// The <merge> tag doesn't support android:theme, so
|
|
|
|
|
// nothing special to do here.
|
|
|
|
|
rInflate(childParser, parent, childAttrs, false);
|
|
|
|
|
} else {
|
|
|
|
|
final View view = createViewFromTag(parent, childName, childAttrs);
|
|
|
|
|
final ViewGroup group = (ViewGroup)parent;
|
|
|
|
|
ViewGroup.LayoutParams params = null;
|
|
|
|
|
try {
|
|
|
|
|
params = group.generateLayoutParams(attrs);
|
|
|
|
|
} catch (RuntimeException e) {
|
|
|
|
|
// Ignore, just fail over to child attrs.
|
|
|
|
|
}
|
|
|
|
|
if (params == null) {
|
|
|
|
|
params = group.generateLayoutParams(childAttrs);
|
|
|
|
|
}
|
2024-02-09 16:03:12 +01:00
|
|
|
params.resolveLayoutDirection(group.getLayoutDirection());
|
2023-08-17 10:46:24 +02:00
|
|
|
view.setLayoutParams(params);
|
|
|
|
|
// Inflate all children.
|
|
|
|
|
rInflate(childParser, view, childAttrs, true);
|
|
|
|
|
|
|
|
|
|
int id = attrs.getAttributeResourceValue("http://schemas.android.com/apk/res/android", "id", 0);
|
|
|
|
|
if (id != 0)
|
|
|
|
|
view.setId(id);
|
|
|
|
|
|
|
|
|
|
group.addView(view);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public LayoutInflater cloneInContext(Context context) {
|
|
|
|
|
return this;
|
2023-08-06 14:32:28 +02:00
|
|
|
}
|
2024-06-15 22:32:01 +02:00
|
|
|
|
|
|
|
|
public Context getContext() {
|
|
|
|
|
return context;
|
|
|
|
|
}
|
2022-10-02 23:06:56 +02:00
|
|
|
}
|