package com.ifpdos.debugmenu.action;

import android.content.Context;
import android.content.res.TypedArray;
import android.content.res.XmlResourceParser;
import android.net.wifi.WifiManager;
import android.util.AttributeSet;
import android.util.Log;
import android.util.Xml;
import com.ifpdos.debugmenu.R;
import com.ifpdos.debugmenu.action.base.ItemSelectAction;
import com.ifpdos.debugmenu.entity.CountryCodeModel;
import d.b.a.a.a;
import d.i.a.a.b;
import d.i.f.c.c;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.xmlpull.v1.XmlPullParserException;

/* JADX INFO: loaded from: classes.dex */
public class WifiCountryAction extends ItemSelectAction {
    private static final String TAG = "WifiCountryAction";
    private List<CountryCodeModel> list;
    private Context mContext;
    private final List<String> mEntries;
    private WifiManager wifiManager;

    public WifiCountryAction(Context context) {
        XmlResourceParser xml;
        AttributeSet attributeSetAsAttributeSet;
        String name;
        super(context);
        this.mContext = context;
        ArrayList arrayList = new ArrayList();
        try {
            xml = context.getResources().getXml(R.xml.country_code);
            try {
                attributeSetAsAttributeSet = Xml.asAttributeSet(xml);
                for (int next = xml.next(); next != 1 && next != 2; next = xml.next()) {
                }
                name = xml.getName();
            } catch (Throwable th) {
                if (xml != null) {
                    try {
                        xml.close();
                    } catch (Throwable th2) {
                        th.addSuppressed(th2);
                    }
                }
                throw th;
            }
        } catch (IOException | XmlPullParserException e2) {
            e2.printStackTrace();
        }
        if (!"country-code".equals(name)) {
            throw new RuntimeException("XML document must start with <country-code> tag; found" + name + " at " + xml.getPositionDescription());
        }
        int depth = xml.getDepth();
        while (true) {
            int next2 = xml.next();
            if (next2 == 1 || (next2 == 3 && xml.getDepth() <= depth)) {
                break;
            }
            if (next2 != 3 && next2 != 4) {
                String name2 = xml.getName();
                TypedArray typedArrayObtainStyledAttributes = context.obtainStyledAttributes(attributeSetAsAttributeSet, R.styleable.a);
                if ("country-item".equals(name2)) {
                    CountryCodeModel countryCodeModel = new CountryCodeModel();
                    countryCodeModel.setTwoCode(typedArrayObtainStyledAttributes.getString(6));
                    countryCodeModel.setThreeCode(typedArrayObtainStyledAttributes.getString(5));
                    countryCodeModel.setDigitalCode(typedArrayObtainStyledAttributes.getString(2));
                    countryCodeModel.setIsoCode(typedArrayObtainStyledAttributes.getString(3));
                    countryCodeModel.setCountryCN(typedArrayObtainStyledAttributes.getString(0));
                    countryCodeModel.setReserved(typedArrayObtainStyledAttributes.getString(4));
                    int resourceId = typedArrayObtainStyledAttributes.getResourceId(1, 0);
                    if (resourceId != 0) {
                        countryCodeModel.setCountryCurrent(context.getString(resourceId));
                        arrayList.add(countryCodeModel);
                    } else {
                        Log.e("WifiCountry", "nodeName=" + name2 + " failed");
                    }
                }
                typedArrayObtainStyledAttributes.recycle();
            }
        }
        xml.close();
        arrayList.toString();
        int i2 = b.a;
        this.list = arrayList;
        this.mEntries = new ArrayList();
        for (int i3 = 0; i3 < this.list.size(); i3++) {
            CountryCodeModel countryCodeModel2 = this.list.get(i3);
            this.mEntries.add(countryCodeModel2.getCountryCurrent() + "(" + countryCodeModel2.getTwoCode() + ")");
        }
    }

    private String getWifiCountryCode() {
        return c.e("persist.sys.wifi.countrycode");
    }

    private void setWifiCountryCode(String str) {
        c.l("persist.sys.wifi.countrycode", str);
        if (this.wifiManager == null) {
            this.wifiManager = (WifiManager) this.mContext.getApplicationContext().getSystemService("wifi");
        }
        d.i.f.g.e.b.d(this.wifiManager).b("setCountryCode", str);
    }

    @Override // com.ifpdos.debugmenu.action.base.ItemSelectAction
    public List<String> getEntryList(Context context) {
        return this.mEntries;
    }

    @Override // com.ifpdos.debugmenu.action.base.ItemSelectAction
    public int getSelectIndex(Context context) {
        String str;
        String wifiCountryCode = getWifiCountryCode();
        int i2 = 0;
        while (true) {
            if (i2 >= this.list.size()) {
                str = "";
                break;
            }
            CountryCodeModel countryCodeModel = this.list.get(i2);
            if (countryCodeModel.getTwoCode().equals(wifiCountryCode)) {
                str = countryCodeModel.getCountryCurrent() + "(" + countryCodeModel.getTwoCode() + ")";
                break;
            }
            i2++;
        }
        StringBuilder sbB = a.B("getSelectIndex:");
        sbB.append(this.mEntries.indexOf(str));
        Log.i(TAG, sbB.toString());
        return this.mEntries.indexOf(str);
    }

    @Override // com.ifpdos.debugmenu.action.base.ItemSelectAction
    public void onSelect(Context context, int i2) {
        String twoCode;
        String string = this.mEntries.get(i2).toString();
        a.O("countryName:", string, TAG);
        int i3 = 0;
        while (true) {
            if (i3 >= this.list.size()) {
                twoCode = "";
                break;
            }
            CountryCodeModel countryCodeModel = this.list.get(i3);
            if ((countryCodeModel.getCountryCurrent() + "(" + countryCodeModel.getTwoCode() + ")").equals(string)) {
                twoCode = countryCodeModel.getTwoCode();
                break;
            }
            i3++;
        }
        a.O("setWifiCountryCode:", string, TAG);
        setWifiCountryCode(twoCode);
    }
}
