package a3;

import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.content.res.XmlResourceParser;
import android.os.Bundle;
import android.os.Handler;
import android.util.AttributeSet;
import android.util.Xml;
import android.view.InflateException;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.preference.Preference;
import androidx.preference.PreferenceGroup;
import androidx.preference.SwitchPreference;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.util.Collections;
import java.util.HashMap;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

/* JADX INFO: loaded from: classes.dex */
public final class a0 {

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public static final Class[] f240e = {Context.class, AttributeSet.class};

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public static final HashMap f241f = new HashMap();

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final Context f242a;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public final f0 f244c;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final Object[] f243b = new Object[2];

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public final String[] f245d = {Preference.class.getPackage().getName() + ".", SwitchPreference.class.getPackage().getName() + "."};

    public a0(Context context, f0 f0Var) {
        this.f242a = context;
        this.f244c = f0Var;
    }

    private Preference createItem(@NonNull String str, @Nullable String[] strArr, AttributeSet attributeSet) throws InflateException, ClassNotFoundException {
        Class<?> cls;
        HashMap map = f241f;
        Constructor<?> constructor = (Constructor) map.get(str);
        if (constructor == null) {
            try {
                try {
                    ClassLoader classLoader = this.f242a.getClassLoader();
                    if (strArr == null || strArr.length == 0) {
                        cls = Class.forName(str, false, classLoader);
                    } else {
                        cls = null;
                        ClassNotFoundException e10 = null;
                        for (String str2 : strArr) {
                            try {
                                cls = Class.forName(str2 + str, false, classLoader);
                                break;
                            } catch (ClassNotFoundException e11) {
                                e10 = e11;
                            }
                        }
                        if (cls == null) {
                            if (e10 != null) {
                                throw e10;
                            }
                            throw new InflateException(attributeSet.getPositionDescription() + ": Error inflating class " + str);
                        }
                    }
                    constructor = cls.getConstructor(f240e);
                    constructor.setAccessible(true);
                    map.put(str, constructor);
                } catch (Exception e12) {
                    InflateException inflateException = new InflateException(attributeSet.getPositionDescription() + ": Error inflating class " + str);
                    inflateException.initCause(e12);
                    throw inflateException;
                }
            } catch (ClassNotFoundException e13) {
                throw e13;
            }
        }
        Object[] objArr = this.f243b;
        objArr[1] = attributeSet;
        return (Preference) constructor.newInstance(objArr);
    }

    public final Preference a(String str, AttributeSet attributeSet) {
        try {
            return -1 == str.indexOf(46) ? createItem(str, this.f245d, attributeSet) : createItem(str, null, attributeSet);
        } catch (InflateException e10) {
            throw e10;
        } catch (ClassNotFoundException e11) {
            InflateException inflateException = new InflateException(attributeSet.getPositionDescription() + ": Error inflating class (not found)" + str);
            inflateException.initCause(e11);
            throw inflateException;
        } catch (Exception e12) {
            InflateException inflateException2 = new InflateException(attributeSet.getPositionDescription() + ": Error inflating class " + str);
            inflateException2.initCause(e12);
            throw inflateException2;
        }
    }

    public final void b(XmlPullParser xmlPullParser, Preference preference, AttributeSet attributeSet) throws XmlPullParserException, IOException {
        long jLongValue;
        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".equals(name)) {
                    try {
                        preference.setIntent(Intent.parseIntent(this.f242a.getResources(), xmlPullParser, attributeSet));
                    } catch (IOException e10) {
                        XmlPullParserException xmlPullParserException = new XmlPullParserException("Error parsing preference");
                        xmlPullParserException.initCause(e10);
                        throw xmlPullParserException;
                    }
                } else if ("extra".equals(name)) {
                    Resources resources = this.f242a.getResources();
                    if (preference.f3590m == null) {
                        preference.f3590m = new Bundle();
                    }
                    resources.parseBundleExtra("extra", attributeSet, preference.f3590m);
                    try {
                        int depth2 = xmlPullParser.getDepth();
                        while (true) {
                            int next2 = xmlPullParser.next();
                            if (next2 == 1 || (next2 == 3 && xmlPullParser.getDepth() <= depth2)) {
                                break;
                            }
                        }
                    } catch (IOException e11) {
                        XmlPullParserException xmlPullParserException2 = new XmlPullParserException("Error parsing preference");
                        xmlPullParserException2.initCause(e11);
                        throw xmlPullParserException2;
                    }
                } else {
                    Preference preferenceA = a(name, attributeSet);
                    PreferenceGroup preferenceGroup = (PreferenceGroup) preference;
                    if (!preferenceGroup.f3611p0.contains(preferenceA)) {
                        if (preferenceA.f3584j != null) {
                            PreferenceGroup parent = preferenceGroup;
                            while (parent.getParent() != null) {
                                parent = parent.getParent();
                            }
                            parent.findPreference(preferenceA.f3584j);
                        }
                        int i10 = preferenceA.f3574e;
                        if (i10 == Integer.MAX_VALUE) {
                            if (preferenceGroup.f3612q0) {
                                int i11 = preferenceGroup.f3613r0;
                                preferenceGroup.f3613r0 = i11 + 1;
                                if (i11 != i10) {
                                    preferenceA.f3574e = i11;
                                    m mVar = preferenceA.f3581h0;
                                    if (mVar != null) {
                                        z zVar = (z) mVar;
                                        Handler handler = zVar.f293e;
                                        e.s0 s0Var = zVar.f294f;
                                        handler.removeCallbacks(s0Var);
                                        handler.post(s0Var);
                                    }
                                }
                            }
                            if (preferenceA instanceof PreferenceGroup) {
                                ((PreferenceGroup) preferenceA).f3612q0 = preferenceGroup.f3612q0;
                            }
                        }
                        int iBinarySearch = Collections.binarySearch(preferenceGroup.f3611p0, preferenceA);
                        if (iBinarySearch < 0) {
                            iBinarySearch = (iBinarySearch * (-1)) - 1;
                        }
                        boolean zX = preferenceGroup.x();
                        if (preferenceA.W == zX) {
                            preferenceA.W = !zX;
                            preferenceA.k(preferenceA.x());
                            preferenceA.j();
                        }
                        synchronized (preferenceGroup) {
                            preferenceGroup.f3611p0.add(iBinarySearch, preferenceA);
                        }
                        f0 f0VarH = preferenceGroup.h();
                        String str = preferenceA.f3584j;
                        if (str == null || !preferenceGroup.f3610o0.containsKey(str)) {
                            synchronized (f0VarH) {
                                jLongValue = f0VarH.f250b;
                                f0VarH.f250b = 1 + jLongValue;
                            }
                        } else {
                            jLongValue = ((Long) preferenceGroup.f3610o0.get(str)).longValue();
                            preferenceGroup.f3610o0.remove(str);
                        }
                        preferenceA.f3568b = jLongValue;
                        preferenceA.f3570c = true;
                        try {
                            preferenceA.m(f0VarH);
                            preferenceA.f3570c = false;
                            preferenceA.assignParent(preferenceGroup);
                            if (preferenceGroup.f3614s0) {
                                preferenceA.l();
                            }
                            m mVar2 = preferenceGroup.f3581h0;
                            if (mVar2 != null) {
                                z zVar2 = (z) mVar2;
                                Handler handler2 = zVar2.f293e;
                                e.s0 s0Var2 = zVar2.f294f;
                                handler2.removeCallbacks(s0Var2);
                                handler2.post(s0Var2);
                            }
                        } catch (Throwable th) {
                            preferenceA.f3570c = false;
                            throw th;
                        }
                    }
                    b(xmlPullParser, preferenceA, attributeSet);
                }
            }
        }
    }

    @NonNull
    public Preference inflate(int i10, @Nullable PreferenceGroup preferenceGroup) {
        XmlResourceParser xml = this.f242a.getResources().getXml(i10);
        try {
            return inflate(xml, preferenceGroup);
        } finally {
            xml.close();
        }
    }

    @NonNull
    public Preference inflate(XmlPullParser xmlPullParser, @Nullable PreferenceGroup preferenceGroup) {
        int next;
        synchronized (this.f243b) {
            AttributeSet attributeSetAsAttributeSet = Xml.asAttributeSet(xmlPullParser);
            this.f243b[0] = this.f242a;
            do {
                try {
                    try {
                        try {
                            next = xmlPullParser.next();
                            if (next == 2) {
                                break;
                            }
                        } catch (IOException e10) {
                            InflateException inflateException = new InflateException(xmlPullParser.getPositionDescription() + ": " + e10.getMessage());
                            inflateException.initCause(e10);
                            throw inflateException;
                        }
                    } catch (InflateException e11) {
                        throw e11;
                    }
                } catch (XmlPullParserException e12) {
                    InflateException inflateException2 = new InflateException(e12.getMessage());
                    inflateException2.initCause(e12);
                    throw inflateException2;
                }
            } while (next != 1);
            if (next == 2) {
                PreferenceGroup preferenceGroup2 = (PreferenceGroup) a(xmlPullParser.getName(), attributeSetAsAttributeSet);
                if (preferenceGroup == null) {
                    preferenceGroup2.m(this.f244c);
                    preferenceGroup = preferenceGroup2;
                }
                b(xmlPullParser, preferenceGroup, attributeSetAsAttributeSet);
            } else {
                throw new InflateException(xmlPullParser.getPositionDescription() + ": No start tag found!");
            }
        }
        return preferenceGroup;
    }
}
