make LayoutInflater instances persistent per Context

Also implement LayoutInfater.cloneInContext() and support mutipe View
factories at once.

This is needed, so that androidx can repace all normal Views with
appcompat Views, which is needed for proper tint color support.
This commit is contained in:
Julian Winkler
2025-01-27 18:16:25 +01:00
parent cb64c13f2d
commit 9c454accca
4 changed files with 18 additions and 6 deletions

View File

@@ -407,7 +407,7 @@ public class Activity extends ContextThemeWrapper implements Window.Callback, La
}
public LayoutInflater getLayoutInflater() {
return new LayoutInflater(this);
return (LayoutInflater)getSystemService("layout_inflater");
}
public boolean isChangingConfigurations() {return false;}

View File

@@ -87,6 +87,7 @@ public class Context extends Object {
public static PackageManager package_manager;
public /*← FIXME?*/ static Application this_application;
private LayoutInflater layout_inflater = new LayoutInflater(this);
File data_dir = null;
File prefs_dir = null;
@@ -219,7 +220,7 @@ public class Context extends Object {
case "accessibility":
return new AccessibilityManager();
case "layout_inflater":
return new LayoutInflater(this);
return layout_inflater;
case "wifi":
return new WifiManager();
case "bluetooth":

View File

@@ -24,6 +24,8 @@ import android.util.Slog;
import android.util.Xml;
import java.io.FileReader;
import java.lang.reflect.Constructor;
import java.util.LinkedList;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;
@@ -46,6 +48,7 @@ public class LayoutInflater {
private Factory2 mFactory2;
private Context context;
private LinkedList<Factory2> factories = new LinkedList<>();
public LayoutInflater(Context context) {
this.context = context;
@@ -63,10 +66,11 @@ public class LayoutInflater {
public void setFactory2(Factory2 factory) {
this.mFactory2 = factory;
factories.addFirst(factory);
}
public static LayoutInflater from(Context context) {
return new LayoutInflater(context);
return (LayoutInflater)context.getSystemService("layout_inflater");
}
public final View createView(String name, String prefix, AttributeSet attrs) throws Exception {
@@ -121,7 +125,12 @@ public class LayoutInflater {
View view = null;
if (context instanceof Factory2) {
for (Factory2 factory : factories) {
view = factory.onCreateView(parent, name, context, attrs);
if (view != null)
break;
}
if (view == null && context instanceof Factory2) {
view = ((Factory2)context).onCreateView(parent, name, context, attrs);
}
if (view == null && -1 == name.indexOf('.')) {
@@ -332,7 +341,9 @@ public class LayoutInflater {
}
public LayoutInflater cloneInContext(Context context) {
return this;
LayoutInflater inflater = new LayoutInflater(context);
inflater.factories.addAll(factories);
return inflater;
}
public Context getContext() {

View File

@@ -282,7 +282,7 @@ public class ArrayAdapter<T> extends BaseAdapter /*implements Filterable*/ {
}
private void init(Context context, int resource, int textViewResourceId, List<T> objects) {
mContext = context;
mInflater = new LayoutInflater(context);
mInflater = (LayoutInflater)context.getSystemService("layout_inflater");
mResource = mDropDownResource = resource;
mObjects = objects;
mFieldId = textViewResourceId;