230 lines
9.9 KiB
Java
230 lines
9.9 KiB
Java
package androidx.preference;
|
|
|
|
import android.content.Context;
|
|
import android.content.Intent;
|
|
import android.content.res.XmlResourceParser;
|
|
import android.util.AttributeSet;
|
|
import android.util.Xml;
|
|
import android.view.InflateException;
|
|
import androidx.annotation.NonNull;
|
|
import androidx.annotation.Nullable;
|
|
import java.io.IOException;
|
|
import java.lang.reflect.Constructor;
|
|
import java.util.HashMap;
|
|
import org.xmlpull.v1.XmlPullParser;
|
|
import org.xmlpull.v1.XmlPullParserException;
|
|
public class PreferenceInflater {
|
|
private static final HashMap<String, Constructor> CONSTRUCTOR_MAP = new HashMap<>();
|
|
private static final Class<?>[] CONSTRUCTOR_SIGNATURE = {Context.class, AttributeSet.class};
|
|
private static final String EXTRA_TAG_NAME = "extra";
|
|
private static final String INTENT_TAG_NAME = "intent";
|
|
private final Object[] mConstructorArgs = new Object[2];
|
|
private final Context mContext;
|
|
private String[] mDefaultPackages;
|
|
private PreferenceManager mPreferenceManager;
|
|
|
|
public PreferenceInflater(Context context, PreferenceManager preferenceManager) {
|
|
this.mContext = context;
|
|
init(preferenceManager);
|
|
}
|
|
|
|
/* JADX WARNING: Code restructure failed: missing block: B:24:0x0078, code lost:
|
|
r12 = move-exception;
|
|
*/
|
|
/* JADX WARNING: Code restructure failed: missing block: B:25:0x0079, code lost:
|
|
r0 = new android.view.InflateException(r13.getPositionDescription() + ": Error inflating class " + r11);
|
|
r0.initCause(r12);
|
|
*/
|
|
/* JADX WARNING: Code restructure failed: missing block: B:26:0x0097, code lost:
|
|
throw r0;
|
|
*/
|
|
/* JADX WARNING: Code restructure failed: missing block: B:27:0x0098, code lost:
|
|
r11 = move-exception;
|
|
*/
|
|
/* JADX WARNING: Code restructure failed: missing block: B:28:0x0099, code lost:
|
|
throw r11;
|
|
*/
|
|
/* JADX WARNING: Failed to process nested try/catch */
|
|
/* JADX WARNING: Removed duplicated region for block: B:24:0x0078 A[ExcHandler: Exception (r12v4 'e' java.lang.Exception A[CUSTOM_DECLARE]), Splitter:B:2:0x000d] */
|
|
private Preference createItem(@NonNull String str, @Nullable String[] strArr, AttributeSet attributeSet) throws ClassNotFoundException, InflateException {
|
|
Class<?> cls;
|
|
ClassNotFoundException e;
|
|
int i;
|
|
Constructor<?> constructor = CONSTRUCTOR_MAP.get(str);
|
|
if (constructor == null) {
|
|
try {
|
|
ClassLoader classLoader = this.mContext.getClassLoader();
|
|
if (strArr != null) {
|
|
if (strArr.length != 0) {
|
|
int length = strArr.length;
|
|
cls = null;
|
|
e = null;
|
|
i = 0;
|
|
while (i < length) {
|
|
cls = Class.forName(strArr[i] + str, false, classLoader);
|
|
break;
|
|
}
|
|
if (cls == null) {
|
|
if (e == null) {
|
|
throw new InflateException(attributeSet.getPositionDescription() + ": Error inflating class " + str);
|
|
}
|
|
throw e;
|
|
}
|
|
constructor = cls.getConstructor(CONSTRUCTOR_SIGNATURE);
|
|
constructor.setAccessible(true);
|
|
CONSTRUCTOR_MAP.put(str, constructor);
|
|
}
|
|
}
|
|
cls = Class.forName(str, false, classLoader);
|
|
constructor = cls.getConstructor(CONSTRUCTOR_SIGNATURE);
|
|
constructor.setAccessible(true);
|
|
CONSTRUCTOR_MAP.put(str, constructor);
|
|
} catch (ClassNotFoundException e2) {
|
|
e = e2;
|
|
i++;
|
|
} catch (Exception e3) {
|
|
}
|
|
}
|
|
Object[] objArr = this.mConstructorArgs;
|
|
objArr[1] = attributeSet;
|
|
return (Preference) constructor.newInstance(objArr);
|
|
}
|
|
|
|
private Preference createItemFromTag(String str, AttributeSet attributeSet) {
|
|
try {
|
|
return -1 == str.indexOf(46) ? onCreateItem(str, attributeSet) : createItem(str, null, attributeSet);
|
|
} catch (InflateException e) {
|
|
throw e;
|
|
} catch (ClassNotFoundException e2) {
|
|
InflateException inflateException = new InflateException(attributeSet.getPositionDescription() + ": Error inflating class (not found)" + str);
|
|
inflateException.initCause(e2);
|
|
throw inflateException;
|
|
} catch (Exception e3) {
|
|
InflateException inflateException2 = new InflateException(attributeSet.getPositionDescription() + ": Error inflating class " + str);
|
|
inflateException2.initCause(e3);
|
|
throw inflateException2;
|
|
}
|
|
}
|
|
|
|
private void init(PreferenceManager preferenceManager) {
|
|
this.mPreferenceManager = preferenceManager;
|
|
setDefaultPackages(new String[]{Preference.class.getPackage().getName() + ".", SwitchPreference.class.getPackage().getName() + "."});
|
|
}
|
|
|
|
@NonNull
|
|
private PreferenceGroup onMergeRoots(PreferenceGroup preferenceGroup, @NonNull PreferenceGroup preferenceGroup2) {
|
|
if (preferenceGroup != null) {
|
|
return preferenceGroup;
|
|
}
|
|
preferenceGroup2.onAttachedToHierarchy(this.mPreferenceManager);
|
|
return preferenceGroup2;
|
|
}
|
|
|
|
private void rInflate(XmlPullParser xmlPullParser, Preference preference, AttributeSet attributeSet) throws XmlPullParserException, IOException {
|
|
int depth = xmlPullParser.getDepth();
|
|
while (true) {
|
|
int next = xmlPullParser.next();
|
|
if ((next == 3 && xmlPullParser.getDepth() <= depth) || next == 1) {
|
|
return;
|
|
}
|
|
if (next == 2) {
|
|
String name = xmlPullParser.getName();
|
|
if (INTENT_TAG_NAME.equals(name)) {
|
|
try {
|
|
preference.setIntent(Intent.parseIntent(getContext().getResources(), xmlPullParser, attributeSet));
|
|
} catch (IOException e) {
|
|
XmlPullParserException xmlPullParserException = new XmlPullParserException("Error parsing preference");
|
|
xmlPullParserException.initCause(e);
|
|
throw xmlPullParserException;
|
|
}
|
|
} else if (EXTRA_TAG_NAME.equals(name)) {
|
|
getContext().getResources().parseBundleExtra(EXTRA_TAG_NAME, attributeSet, preference.getExtras());
|
|
try {
|
|
skipCurrentTag(xmlPullParser);
|
|
} catch (IOException e2) {
|
|
XmlPullParserException xmlPullParserException2 = new XmlPullParserException("Error parsing preference");
|
|
xmlPullParserException2.initCause(e2);
|
|
throw xmlPullParserException2;
|
|
}
|
|
} else {
|
|
Preference createItemFromTag = createItemFromTag(name, attributeSet);
|
|
((PreferenceGroup) preference).addItemFromInflater(createItemFromTag);
|
|
rInflate(xmlPullParser, createItemFromTag, attributeSet);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private static void skipCurrentTag(XmlPullParser xmlPullParser) throws XmlPullParserException, IOException {
|
|
int depth = xmlPullParser.getDepth();
|
|
while (true) {
|
|
int next = xmlPullParser.next();
|
|
if (next == 1) {
|
|
return;
|
|
}
|
|
if (next == 3 && xmlPullParser.getDepth() <= depth) {
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
public Context getContext() {
|
|
return this.mContext;
|
|
}
|
|
|
|
public String[] getDefaultPackages() {
|
|
return this.mDefaultPackages;
|
|
}
|
|
|
|
public Preference inflate(int i, @Nullable PreferenceGroup preferenceGroup) {
|
|
XmlResourceParser xml = getContext().getResources().getXml(i);
|
|
try {
|
|
return inflate(xml, preferenceGroup);
|
|
} finally {
|
|
xml.close();
|
|
}
|
|
}
|
|
|
|
public Preference inflate(XmlPullParser xmlPullParser, @Nullable PreferenceGroup preferenceGroup) {
|
|
int next;
|
|
PreferenceGroup onMergeRoots;
|
|
synchronized (this.mConstructorArgs) {
|
|
AttributeSet asAttributeSet = Xml.asAttributeSet(xmlPullParser);
|
|
this.mConstructorArgs[0] = this.mContext;
|
|
do {
|
|
try {
|
|
next = xmlPullParser.next();
|
|
if (next == 2) {
|
|
break;
|
|
}
|
|
} catch (InflateException e) {
|
|
throw e;
|
|
} catch (XmlPullParserException e2) {
|
|
InflateException inflateException = new InflateException(e2.getMessage());
|
|
inflateException.initCause(e2);
|
|
throw inflateException;
|
|
} catch (IOException e3) {
|
|
InflateException inflateException2 = new InflateException(xmlPullParser.getPositionDescription() + ": " + e3.getMessage());
|
|
inflateException2.initCause(e3);
|
|
throw inflateException2;
|
|
}
|
|
} while (next != 1);
|
|
if (next == 2) {
|
|
onMergeRoots = onMergeRoots(preferenceGroup, (PreferenceGroup) createItemFromTag(xmlPullParser.getName(), asAttributeSet));
|
|
rInflate(xmlPullParser, onMergeRoots, asAttributeSet);
|
|
} else {
|
|
throw new InflateException(xmlPullParser.getPositionDescription() + ": No start tag found!");
|
|
}
|
|
}
|
|
return onMergeRoots;
|
|
}
|
|
|
|
public Preference onCreateItem(String str, AttributeSet attributeSet) throws ClassNotFoundException {
|
|
return createItem(str, this.mDefaultPackages, attributeSet);
|
|
}
|
|
|
|
public void setDefaultPackages(String[] strArr) {
|
|
this.mDefaultPackages = strArr;
|
|
}
|
|
}
|