京东到家.apk(点击下载) / bt.java


package com.xiaomi.push;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.text.TextUtils;
import com.huawei.hms.push.constant.RemoteMessageConst;
import com.igexin.push.e.b.d;
import com.xiaomi.mipush.sdk.Constants;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Locale;
import java.util.Map;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class bt {
    protected static final Map<String, bp> b = new HashMap();

    /* renamed from: c  reason: collision with root package name */
    protected static Context f2674c;
    protected static boolean f = false;
    private static bt l;
    private static a m;
    private static String n;
    private static String o;
    protected final Map<String, bq> a;
    protected b d;
    String e;
    private bs g;
    private String h;
    private long i;
    private final long j;
    private long k;

    public interface a {
        bt a(Context context, b bVar, String str);
    }

    public interface b {
        String a(String str);
    }

    private bt(Context context, bs bsVar, b bVar, String str, String str2, String str3) {
        this.a = new HashMap();
        this.h = "0";
        this.i = 0;
        this.j = 15;
        this.k = 0;
        this.e = "isp_prov_city_country_ip";
        this.d = bVar;
        this.g = new bu(this);
        this.h = str;
        n = str2 == null ? context.getPackageName() : str2;
        o = str3 == null ? j() : str3;
    }

    protected bt(Context context, b bVar, String str) {
        this(context, null, bVar, str, null, null);
    }

    public static synchronized bt a() {
        bt btVar;
        synchronized (bt.class) {
            if (l != null) {
                btVar = l;
            } else {
                throw new IllegalStateException("the host manager is not initialized yet.");
            }
        }
        return btVar;
    }

    private ArrayList<bp> a(ArrayList<String> arrayList) {
        JSONObject jSONObject;
        JSONObject jSONObject2;
        synchronized (this.a) {
            for (bq bqVar : this.a.values()) {
                bqVar.b();
            }
            loop1:
            while (true) {
                for (boolean z = false; !z; z = true) {
                    for (String str : this.a.keySet()) {
                        if (this.a.get(str).b.isEmpty()) {
                            this.a.remove(str);
                        }
                    }
                }
                break loop1;
            }
        }
        synchronized (this.a) {
            h();
            for (String str2 : this.a.keySet()) {
                if (!arrayList.contains(str2)) {
                    arrayList.add(str2);
                }
            }
        }
        synchronized (b) {
            for (Object obj : b.values().toArray()) {
                bp bpVar = (bp) obj;
                if (!bpVar.a()) {
                    b.remove(bpVar.f2673c);
                }
            }
        }
        if (!arrayList.contains("resolver.msg.xiaomi.net")) {
            arrayList.add("resolver.msg.xiaomi.net");
        }
        ArrayList<bp> arrayList2 = new ArrayList<>(arrayList.size());
        for (int i2 = 0; i2 < arrayList.size(); i2++) {
            arrayList2.add(null);
        }
        try {
            String str3 = ag.d(f2674c) ? "wifi" : "wap";
            String a2 = a(arrayList, str3, this.h, true);
            if (!TextUtils.isEmpty(a2)) {
                JSONObject jSONObject3 = new JSONObject(a2);
                com.xiaomi.channel.commonutils.logger.b.b(a2);
                if ("OK".equalsIgnoreCase(jSONObject3.getString("S"))) {
                    JSONObject jSONObject4 = jSONObject3.getJSONObject("R");
                    String string = jSONObject4.getString("province");
                    String string2 = jSONObject4.getString("city");
                    String string3 = jSONObject4.getString("isp");
                    String string4 = jSONObject4.getString("ip");
                    String string5 = jSONObject4.getString("country");
                    JSONObject jSONObject5 = jSONObject4.getJSONObject(str3);
                    com.xiaomi.channel.commonutils.logger.b.c("get bucket: net=" + string3 + ", hosts=" + jSONObject5.toString());
                    int i3 = 0;
                    while (i3 < arrayList.size()) {
                        String str4 = arrayList.get(i3);
                        JSONArray optJSONArray = jSONObject5.optJSONArray(str4);
                        if (optJSONArray == null) {
                            com.xiaomi.channel.commonutils.logger.b.a("no bucket found for ".concat(String.valueOf(str4)));
                            jSONObject = jSONObject5;
                        } else {
                            bp bpVar2 = new bp(str4);
                            int i4 = 0;
                            while (i4 < optJSONArray.length()) {
                                String string6 = optJSONArray.getString(i4);
                                if (!TextUtils.isEmpty(string6)) {
                                    jSONObject2 = jSONObject5;
                                    bpVar2.a(new by(string6, optJSONArray.length() - i4));
                                } else {
                                    jSONObject2 = jSONObject5;
                                }
                                i4++;
                                jSONObject5 = jSONObject2;
                            }
                            jSONObject = jSONObject5;
                            arrayList2.set(i3, bpVar2);
                            bpVar2.h = string5;
                            bpVar2.d = string;
                            bpVar2.f = string3;
                            bpVar2.g = string4;
                            bpVar2.e = string2;
                            if (jSONObject4.has("stat-percent")) {
                                bpVar2.j = jSONObject4.getDouble("stat-percent");
                            }
                            if (jSONObject4.has("stat-domain")) {
                                bpVar2.k = jSONObject4.getString("stat-domain");
                            }
                            if (jSONObject4.has(RemoteMessageConst.TTL)) {
                                bpVar2.a(((long) jSONObject4.getInt(RemoteMessageConst.TTL)) * 1000);
                            }
                            this.e = bpVar2.c();
                        }
                        i3++;
                        jSONObject5 = jSONObject;
                    }
                    JSONObject optJSONObject = jSONObject4.optJSONObject("reserved");
                    if (optJSONObject != null) {
                        long j2 = d.b;
                        if (jSONObject4.has("reserved-ttl")) {
                            j2 = ((long) jSONObject4.getInt("reserved-ttl")) * 1000;
                        }
                        Iterator<String> keys = optJSONObject.keys();
                        while (keys.hasNext()) {
                            String next = keys.next();
                            JSONArray optJSONArray2 = optJSONObject.optJSONArray(next);
                            if (optJSONArray2 == null) {
                                com.xiaomi.channel.commonutils.logger.b.a("no bucket found for ".concat(String.valueOf(next)));
                            } else {
                                bp bpVar3 = new bp(next);
                                bpVar3.a(j2);
                                for (int i5 = 0; i5 < optJSONArray2.length(); i5++) {
                                    String string7 = optJSONArray2.getString(i5);
                                    if (!TextUtils.isEmpty(string7)) {
                                        bpVar3.a(new by(string7, optJSONArray2.length() - i5));
                                    }
                                }
                                synchronized (b) {
                                    b.put(next, bpVar3);
                                }
                            }
                        }
                    }
                }
            }
        } catch (Exception e2) {
            com.xiaomi.channel.commonutils.logger.b.a("failed to get bucket " + e2.getMessage());
        }
        for (int i6 = 0; i6 < arrayList.size(); i6++) {
            bp bpVar4 = arrayList2.get(i6);
            if (bpVar4 != null) {
                a(arrayList.get(i6), bpVar4);
            }
        }
        f();
        return arrayList2;
    }

    public static synchronized void a(Context context, b bVar, String str, String str2, String str3) {
        synchronized (bt.class) {
            Context applicationContext = context.getApplicationContext();
            f2674c = applicationContext;
            if (applicationContext == null) {
                f2674c = context;
            }
            if (l == null) {
                if (m == null) {
                    l = new bt(context, null, bVar, str, str2, str3);
                    return;
                }
                l = m.a(context, bVar, str);
            }
        }
    }

    public static synchronized void a(a aVar) {
        synchronized (bt.class) {
            m = aVar;
            l = null;
        }
    }

    private void a(String str, bp bpVar) {
        if (TextUtils.isEmpty(str) || bpVar == null) {
            throw new IllegalArgumentException("the argument is invalid " + str + ", " + bpVar);
        }
        synchronized (this.a) {
            h();
            if (this.a.containsKey(str)) {
                this.a.get(str).a(bpVar);
            } else {
                bq bqVar = new bq(str);
                bqVar.a(bpVar);
                this.a.put(str, bqVar);
            }
        }
    }

    public static void a(String str, String str2) {
        bp bpVar = b.get(str);
        synchronized (b) {
            if (bpVar == null) {
                bp bpVar2 = new bp(str);
                bpVar2.a(d.b);
                bpVar2.b(str2);
                b.put(str, bpVar2);
            } else {
                bpVar.b(str2);
            }
        }
    }

    static String b() {
        NetworkInfo activeNetworkInfo;
        Context context = f2674c;
        if (context == null) {
            return "unknown";
        }
        try {
            ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService("connectivity");
            if (connectivityManager == null || (activeNetworkInfo = connectivityManager.getActiveNetworkInfo()) == null) {
                return "unknown";
            }
            if (activeNetworkInfo.getType() == 1) {
                return "WIFI-UNKNOWN";
            }
            return activeNetworkInfo.getTypeName() + Constants.ACCEPT_TIME_SEPARATOR_SERVER + activeNetworkInfo.getSubtypeName();
        } catch (Throwable unused) {
            return "unknown";
        }
    }

    public static bp c(String str) {
        bp bpVar;
        synchronized (b) {
            bpVar = b.get(str);
        }
        return bpVar;
    }

    private bp d(String str) {
        bq bqVar;
        bp a2;
        synchronized (this.a) {
            h();
            bqVar = this.a.get(str);
        }
        if (bqVar == null || (a2 = bqVar.a()) == null) {
            return null;
        }
        return a2;
    }

    private bp e(String str) {
        if (System.currentTimeMillis() - this.k <= this.i * 60 * 1000) {
            return null;
        }
        this.k = System.currentTimeMillis();
        ArrayList<String> arrayList = new ArrayList<>();
        arrayList.add(str);
        bp bpVar = a(arrayList).get(0);
        if (bpVar != null) {
            this.i = 0;
            return bpVar;
        }
        long j2 = this.i;
        if (j2 >= 15) {
            return null;
        }
        this.i = j2 + 1;
        return null;
    }

    private static String f(String str) {
        try {
            int length = str.length();
            byte[] bytes = str.getBytes("UTF-8");
            for (int i2 = 0; i2 < bytes.length; i2++) {
                byte b2 = bytes[i2];
                int i3 = b2 & 240;
                if (i3 != 240) {
                    bytes[i2] = (byte) (((b2 & 15) ^ ((byte) (((b2 >> 4) + length) & 15))) | i3);
                }
            }
            return new String(bytes);
        } catch (UnsupportedEncodingException unused) {
            return str;
        }
    }

    private static String g() {
        BufferedReader bufferedReader;
        Throwable th;
        Throwable th2;
        try {
            File file = new File(f2674c.getFilesDir(), i());
            if (file.isFile()) {
                bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
                try {
                    StringBuilder sb = new StringBuilder();
                    while (true) {
                        String readLine = bufferedReader.readLine();
                        if (readLine != null) {
                            sb.append(readLine);
                        } else {
                            String sb2 = sb.toString();
                            jd.a(bufferedReader);
                            return sb2;
                        }
                    }
                } catch (Throwable th3) {
                    th2 = th3;
                    try {
                        com.xiaomi.channel.commonutils.logger.b.a("load host exception " + th2.getMessage());
                        jd.a(bufferedReader);
                        return null;
                    } catch (Throwable th4) {
                        th = th4;
                        jd.a(bufferedReader);
                        throw th;
                    }
                }
            } else {
                jd.a((Closeable) null);
                return null;
            }
        } catch (Throwable th5) {
            bufferedReader = null;
            th = th5;
            jd.a(bufferedReader);
            throw th;
        }
    }

    private boolean h() {
        synchronized (this.a) {
            if (f) {
                return true;
            }
            f = true;
            this.a.clear();
            try {
                String g2 = g();
                if (!TextUtils.isEmpty(g2)) {
                    synchronized (this.a) {
                        this.a.clear();
                        JSONObject jSONObject = new JSONObject(g2);
                        if (jSONObject.optInt("ver") == 2) {
                            JSONArray optJSONArray = jSONObject.optJSONArray("data");
                            if (optJSONArray != null) {
                                for (int i2 = 0; i2 < optJSONArray.length(); i2++) {
                                    bq a2 = new bq().a(optJSONArray.getJSONObject(i2));
                                    this.a.put(a2.a, a2);
                                }
                            }
                            JSONArray optJSONArray2 = jSONObject.optJSONArray("reserved");
                            if (optJSONArray2 != null) {
                                for (int i3 = 0; i3 < optJSONArray2.length(); i3++) {
                                    JSONObject jSONObject2 = optJSONArray2.getJSONObject(i3);
                                    String optString = jSONObject2.optString("host");
                                    if (!TextUtils.isEmpty(optString)) {
                                        try {
                                            bp a3 = new bp(optString).a(jSONObject2);
                                            b.put(a3.f2673c, a3);
                                            com.xiaomi.channel.commonutils.logger.b.a("load local reserved host for " + a3.f2673c);
                                        } catch (JSONException unused) {
                                            com.xiaomi.channel.commonutils.logger.b.a("parse reserved host fail.");
                                        }
                                    }
                                }
                            }
                        } else {
                            throw new JSONException("Bad version");
                        }
                    }
                    com.xiaomi.channel.commonutils.logger.b.b("loading the new hosts succeed");
                    return true;
                }
            } catch (Throwable th) {
                com.xiaomi.channel.commonutils.logger.b.a("load bucket failure: " + th.getMessage());
            }
            return false;
        }
    }

    private static String i() {
        if ("com.xiaomi.xmsf".equals(n)) {
            return n;
        }
        return n + ":pushservice";
    }

    private static String j() {
        try {
            PackageInfo packageInfo = f2674c.getPackageManager().getPackageInfo(f2674c.getPackageName(), 16384);
            return packageInfo != null ? packageInfo.versionName : "0";
        } catch (Exception unused) {
            return "0";
        }
    }

    private JSONObject k() {
        JSONObject jSONObject;
        synchronized (this.a) {
            jSONObject = new JSONObject();
            jSONObject.put("ver", 2);
            JSONArray jSONArray = new JSONArray();
            for (bq bqVar : this.a.values()) {
                jSONArray.put(bqVar.c());
            }
            jSONObject.put("data", jSONArray);
            JSONArray jSONArray2 = new JSONArray();
            for (bp bpVar : b.values()) {
                jSONArray2.put(bpVar.d());
            }
            jSONObject.put("reserved", jSONArray2);
        }
        return jSONObject;
    }

    public final bp a(String str) {
        if (!TextUtils.isEmpty(str)) {
            return a(new URL(str).getHost(), true);
        }
        throw new IllegalArgumentException("the url is empty");
    }

    public final bp a(String str, boolean z) {
        bp e2;
        if (!TextUtils.isEmpty(str)) {
            bp d2 = d(str);
            return (d2 == null || !d2.a()) ? (!z || !ag.a(f2674c) || (e2 = e(str)) == null) ? new bv(this, str, d2) : e2 : d2;
        }
        throw new IllegalArgumentException("the host is empty");
    }

    /* access modifiers changed from: protected */
    public String a(ArrayList<String> arrayList, String str, String str2, boolean z) {
        ArrayList<String> arrayList2 = new ArrayList<>();
        ArrayList<af> arrayList3 = new ArrayList();
        arrayList3.add(new ad("type", str));
        if (str.equals("wap")) {
            arrayList3.add(new ad("conpt", f(ag.i(f2674c))));
        }
        if (z) {
            arrayList3.add(new ad("reserved", "1"));
        }
        arrayList3.add(new ad("uuid", str2));
        arrayList3.add(new ad("list", am.a(arrayList, ",")));
        arrayList3.add(new ad("countrycode", com.xiaomi.push.service.d.a(f2674c).b()));
        arrayList3.add(new ad("push_sdk_vc", "50001"));
        bp d2 = d("resolver.msg.xiaomi.net");
        String format = String.format(Locale.US, "https://%1$s/gslb/?ver=5.0", "resolver.msg.xiaomi.net");
        if (d2 == null) {
            arrayList2.add(format);
            synchronized (b) {
                bp bpVar = b.get("resolver.msg.xiaomi.net");
                if (bpVar != null) {
                    Iterator<String> it = bpVar.a(true).iterator();
                    while (it.hasNext()) {
                        arrayList2.add(String.format(Locale.US, "https://%1$s/gslb/?ver=5.0", it.next()));
                    }
                }
            }
        } else {
            arrayList2 = d2.a(format);
        }
        Iterator<String> it2 = arrayList2.iterator();
        IOException e2 = null;
        while (it2.hasNext()) {
            Uri.Builder buildUpon = Uri.parse(it2.next()).buildUpon();
            for (af afVar : arrayList3) {
                buildUpon.appendQueryParameter(afVar.a(), afVar.b());
            }
            try {
                return this.d == null ? ag.a(f2674c, new URL(buildUpon.toString())) : this.d.a(buildUpon.toString());
            } catch (IOException e3) {
                e2 = e3;
            }
        }
        if (e2 == null) {
            return null;
        }
        com.xiaomi.channel.commonutils.logger.b.a("network exception: " + e2.getMessage());
        throw e2;
    }

    public final bp b(String str) {
        return a(str, true);
    }

    public final void c() {
        synchronized (this.a) {
            this.a.clear();
        }
    }

    public final String d() {
        StringBuilder sb = new StringBuilder();
        synchronized (this.a) {
            for (Map.Entry<String, bq> entry : this.a.entrySet()) {
                sb.append(entry.getKey());
                sb.append(":\n");
                sb.append(entry.getValue().toString());
                sb.append("\n");
            }
        }
        return sb.toString();
    }

    public final void e() {
        ArrayList<String> arrayList;
        synchronized (this.a) {
            h();
            arrayList = new ArrayList<>(this.a.keySet());
            for (int size = arrayList.size() - 1; size >= 0; size--) {
                bq bqVar = this.a.get(arrayList.get(size));
                if (!(bqVar == null || bqVar.a() == null)) {
                    arrayList.remove(size);
                }
            }
        }
        ArrayList<bp> a2 = a(arrayList);
        for (int i2 = 0; i2 < arrayList.size(); i2++) {
            if (a2.get(i2) != null) {
                a(arrayList.get(i2), a2.get(i2));
            }
        }
    }

    public final void f() {
        synchronized (this.a) {
            try {
                BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(f2674c.openFileOutput(i(), 0)));
                String jSONObject = k().toString();
                if (!TextUtils.isEmpty(jSONObject)) {
                    bufferedWriter.write(jSONObject);
                }
                bufferedWriter.close();
            } catch (Exception e2) {
                com.xiaomi.channel.commonutils.logger.b.a("persist bucket failure: " + e2.getMessage());
            }
        }
    }
}