万能钥匙.apk(点击下载) / dt.java


package com.xiaomi.push;

import android.app.ActivityManager;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.net.wifi.WifiManager;
import android.os.Process;
import android.text.TextUtils;
import com.appara.core.BLHttp;
import com.appara.feed.constant.TTParam;
import com.meizu.cloud.pushsdk.constants.PushConstants;
import com.xiaomi.mipush.sdk.Constants;
import com.xiaomi.push.service.module.PushChannelRegion;
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.List;
import java.util.Locale;
import java.util.Map;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class dt {

    /* renamed from: a  reason: collision with root package name */
    protected static Context f8488a;

    /* renamed from: a  reason: collision with other field name */
    private static a f220a;

    /* renamed from: a  reason: collision with other field name */
    private static dt f221a;

    /* renamed from: a  reason: collision with other field name */
    protected static boolean f222a = false;

    /* renamed from: b  reason: collision with root package name */
    protected static Map<String, dp> f8489b = new HashMap();
    private static String c;
    private static String d;

    /* renamed from: a  reason: collision with other field name */
    private long f223a;

    /* renamed from: a  reason: collision with other field name */
    private ds f224a;

    /* renamed from: a  reason: collision with other field name */
    protected b f225a;

    /* renamed from: a  reason: collision with other field name */
    private String f226a;

    /* renamed from: a  reason: collision with other field name */
    protected Map<String, dq> f227a;

    /* renamed from: b  reason: collision with other field name */
    private final long f228b;

    /* renamed from: b  reason: collision with other field name */
    private String f229b;

    /* renamed from: c  reason: collision with other field name */
    private long f230c;

    public interface a {
        dt a(Context context, ds dsVar, b bVar, String str);
    }

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

    protected dt(Context context, ds dsVar, b bVar, String str) {
        this(context, dsVar, bVar, str, null, null);
    }

    protected dt(Context context, ds dsVar, b bVar, String str, String str2, String str3) {
        this.f227a = new HashMap();
        this.f226a = "0";
        this.f223a = 0;
        this.f228b = 15;
        this.f230c = 0;
        this.f229b = "isp_prov_city_country_ip";
        this.f225a = bVar;
        this.f224a = dsVar == null ? new du(this) : dsVar;
        this.f226a = str;
        c = str2 == null ? context.getPackageName() : str2;
        d = str3 == null ? f() : str3;
    }

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

    /* renamed from: a  reason: collision with other method in class */
    static String m182a() {
        NetworkInfo activeNetworkInfo;
        Context context = f8488a;
        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) {
                WifiManager wifiManager = (WifiManager) f8488a.getSystemService("wifi");
                if (wifiManager == null || wifiManager.getConnectionInfo() == null) {
                    return "unknown";
                }
                return "WIFI-" + wifiManager.getConnectionInfo().getSSID();
            }
            return activeNetworkInfo.getTypeName() + Constants.ACCEPT_TIME_SEPARATOR_SERVER + activeNetworkInfo.getSubtypeName();
        } catch (Throwable unused) {
            return "unknown";
        }
    }

    static String a(String str) {
        try {
            int length = str.length();
            byte[] bytes = str.getBytes(BLHttp.SERVER_CHARSET);
            for (int i = 0; i < bytes.length; i++) {
                byte b2 = bytes[i];
                int i2 = b2 & 240;
                if (i2 != 240) {
                    bytes[i] = (byte) (((b2 & 15) ^ ((byte) (((b2 >> 4) + length) & 15))) | i2);
                }
            }
            return new String(bytes);
        } catch (UnsupportedEncodingException unused) {
            return str;
        }
    }

    private ArrayList<dp> a(ArrayList<String> arrayList) {
        boolean z;
        JSONObject jSONObject;
        JSONObject jSONObject2;
        m191d();
        synchronized (this.f227a) {
            m187a();
            for (String str : this.f227a.keySet()) {
                if (!arrayList.contains(str)) {
                    arrayList.add(str);
                }
            }
        }
        boolean isEmpty = f8489b.isEmpty();
        synchronized (f8489b) {
            z = isEmpty;
            for (Object obj : f8489b.values().toArray()) {
                dp dpVar = (dp) obj;
                if (!dpVar.b()) {
                    f8489b.remove(dpVar.f217b);
                    z = true;
                }
            }
        }
        if (!arrayList.contains(b())) {
            arrayList.add(b());
        }
        ArrayList<dp> arrayList2 = new ArrayList<>(arrayList.size());
        for (int i = 0; i < arrayList.size(); i++) {
            arrayList2.add(null);
        }
        try {
            String str2 = at.d(f8488a) ? "wifi" : "wap";
            String a2 = a(arrayList, str2, this.f226a, z);
            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(TTParam.KEY_isp);
                    String string4 = jSONObject4.getString(TTParam.KEY_ip);
                    String string5 = jSONObject4.getString("country");
                    JSONObject jSONObject5 = jSONObject4.getJSONObject(str2);
                    com.xiaomi.channel.commonutils.logger.b.c("get bucket: ip = " + string4 + " net = " + string3 + " hosts = " + jSONObject5.toString());
                    int i2 = 0;
                    while (i2 < arrayList.size()) {
                        String str3 = arrayList.get(i2);
                        JSONArray optJSONArray = jSONObject5.optJSONArray(str3);
                        if (optJSONArray == null) {
                            com.xiaomi.channel.commonutils.logger.b.m19a("no bucket found for ".concat(String.valueOf(str3)));
                            jSONObject = jSONObject5;
                        } else {
                            dp dpVar2 = new dp(str3);
                            int i3 = 0;
                            while (i3 < optJSONArray.length()) {
                                String string6 = optJSONArray.getString(i3);
                                if (!TextUtils.isEmpty(string6)) {
                                    jSONObject2 = jSONObject5;
                                    dpVar2.a(new dy(string6, optJSONArray.length() - i3));
                                } else {
                                    jSONObject2 = jSONObject5;
                                }
                                i3++;
                                jSONObject5 = jSONObject2;
                            }
                            jSONObject = jSONObject5;
                            arrayList2.set(i2, dpVar2);
                            dpVar2.g = string5;
                            dpVar2.c = string;
                            dpVar2.e = string3;
                            dpVar2.f = string4;
                            dpVar2.d = string2;
                            if (jSONObject4.has("stat-percent")) {
                                dpVar2.a(jSONObject4.getDouble("stat-percent"));
                            }
                            if (jSONObject4.has("stat-domain")) {
                                dpVar2.b(jSONObject4.getString("stat-domain"));
                            }
                            if (jSONObject4.has("ttl")) {
                                dpVar2.a(((long) jSONObject4.getInt("ttl")) * 1000);
                            }
                            m186a(dpVar2.a());
                        }
                        i2++;
                        jSONObject5 = jSONObject;
                    }
                    JSONObject optJSONObject = jSONObject4.optJSONObject("reserved");
                    if (optJSONObject != null) {
                        long j = 604800000;
                        if (jSONObject4.has("reserved-ttl")) {
                            j = ((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.m19a("no bucket found for ".concat(String.valueOf(next)));
                            } else {
                                dp dpVar3 = new dp(next);
                                dpVar3.a(j);
                                for (int i4 = 0; i4 < optJSONArray2.length(); i4++) {
                                    String string7 = optJSONArray2.getString(i4);
                                    if (!TextUtils.isEmpty(string7)) {
                                        dpVar3.a(new dy(string7, optJSONArray2.length() - i4));
                                    }
                                }
                                synchronized (f8489b) {
                                    if (this.f224a.a(next)) {
                                        f8489b.put(next, dpVar3);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        } catch (Exception e) {
            com.xiaomi.channel.commonutils.logger.b.m19a("failed to get bucket " + e.getMessage());
        }
        for (int i5 = 0; i5 < arrayList.size(); i5++) {
            dp dpVar4 = arrayList2.get(i5);
            if (dpVar4 != null) {
                a(arrayList.get(i5), dpVar4);
            }
        }
        m190c();
        return arrayList2;
    }

    public static synchronized void a(Context context, ds dsVar, b bVar, String str, String str2, String str3) {
        synchronized (dt.class) {
            Context applicationContext = context.getApplicationContext();
            f8488a = applicationContext;
            if (applicationContext == null) {
                f8488a = context;
            }
            if (f221a == null) {
                if (f220a == null) {
                    f221a = new dt(context, dsVar, bVar, str, str2, str3);
                    return;
                }
                f221a = f220a.a(context, dsVar, bVar, str);
            }
        }
    }

    public static synchronized void a(a aVar) {
        synchronized (dt.class) {
            f220a = aVar;
            f221a = null;
        }
    }

    public static void a(String str, String str2) {
        dp dpVar = f8489b.get(str);
        synchronized (f8489b) {
            if (dpVar == null) {
                dp dpVar2 = new dp(str);
                dpVar2.a(604800000L);
                dpVar2.m175a(str2);
                f8489b.put(str, dpVar2);
            } else {
                dpVar.m175a(str2);
            }
        }
    }

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

    /* renamed from: a  reason: collision with other method in class */
    public dp m183a(String str) {
        if (!TextUtils.isEmpty(str)) {
            return a(new URL(str).getHost(), true);
        }
        throw new IllegalArgumentException("the url is empty");
    }

    public dp a(String str, boolean z) {
        dp d2;
        if (TextUtils.isEmpty(str)) {
            throw new IllegalArgumentException("the host is empty");
        } else if (!this.f224a.a(str)) {
            return null;
        } else {
            dp c2 = c(str);
            return (c2 == null || !c2.b()) ? (!z || !at.b(f8488a) || (d2 = d(str)) == null) ? new dv(this, str, c2) : d2 : c2;
        }
    }

    /* access modifiers changed from: protected */
    public String a(ArrayList<String> arrayList, String str, String str2, boolean z) {
        ArrayList<String> arrayList2 = new ArrayList<>();
        ArrayList<as> arrayList3 = new ArrayList();
        arrayList3.add(new aq("type", str));
        if (str.equals("wap")) {
            arrayList3.add(new aq("conpt", a(at.m81a(f8488a))));
        }
        if (z) {
            arrayList3.add(new aq("reserved", "1"));
        }
        arrayList3.add(new aq("uuid", str2));
        arrayList3.add(new aq(TTParam.SOURCE_list, az.a(arrayList, Constants.ACCEPT_TIME_SEPARATOR_SP)));
        arrayList3.add(new aq("countrycode", com.xiaomi.push.service.a.a(f8488a).b()));
        dp c2 = c(b());
        String format = String.format(Locale.US, "http://%1$s/gslb/?ver=4.0", b());
        if (c2 == null) {
            arrayList2.add(format);
            synchronized (f8489b) {
                dp dpVar = f8489b.get("resolver.msg.xiaomi.net");
                if (dpVar != null) {
                    Iterator<String> it = dpVar.a(true).iterator();
                    while (it.hasNext()) {
                        arrayList2.add(String.format(Locale.US, "http://%1$s/gslb/?ver=4.0", it.next()));
                    }
                }
            }
        } else {
            arrayList2 = c2.a(format);
        }
        Iterator<String> it2 = arrayList2.iterator();
        IOException e = null;
        while (it2.hasNext()) {
            Uri.Builder buildUpon = Uri.parse(it2.next()).buildUpon();
            for (as asVar : arrayList3) {
                buildUpon.appendQueryParameter(asVar.a(), asVar.b());
            }
            try {
                return this.f225a == null ? at.a(f8488a, new URL(buildUpon.toString())) : this.f225a.a(buildUpon.toString());
            } catch (IOException e2) {
                e = e2;
            }
        }
        if (e == null) {
            return null;
        }
        com.xiaomi.channel.commonutils.logger.b.m19a("network exception: " + e.getMessage());
        throw e;
    }

    /* access modifiers changed from: protected */
    /* renamed from: a  reason: collision with other method in class */
    public JSONObject m184a() {
        JSONObject jSONObject;
        synchronized (this.f227a) {
            jSONObject = new JSONObject();
            jSONObject.put("ver", 2);
            JSONArray jSONArray = new JSONArray();
            for (dq dqVar : this.f227a.values()) {
                jSONArray.put(dqVar.m179a());
            }
            jSONObject.put("data", jSONArray);
            JSONArray jSONArray2 = new JSONArray();
            for (dp dpVar : f8489b.values()) {
                jSONArray2.put(dpVar.m174a());
            }
            jSONObject.put("reserved", jSONArray2);
        }
        return jSONObject;
    }

    /* renamed from: a  reason: collision with other method in class */
    public void m185a() {
        synchronized (this.f227a) {
            this.f227a.clear();
        }
    }

    /* renamed from: a  reason: collision with other method in class */
    public void m186a(String str) {
        this.f229b = str;
    }

    public void a(String str, dp dpVar) {
        if (TextUtils.isEmpty(str) || dpVar == null) {
            throw new IllegalArgumentException("the argument is invalid " + str + ", " + dpVar);
        } else if (this.f224a.a(str)) {
            synchronized (this.f227a) {
                m187a();
                if (this.f227a.containsKey(str)) {
                    this.f227a.get(str).a(dpVar);
                } else {
                    dq dqVar = new dq(str);
                    dqVar.a(dpVar);
                    this.f227a.put(str, dqVar);
                }
            }
        }
    }

    /* access modifiers changed from: protected */
    /* renamed from: a  reason: collision with other method in class */
    public boolean m187a() {
        synchronized (this.f227a) {
            if (f222a) {
                return true;
            }
            f222a = true;
            this.f227a.clear();
            try {
                String d2 = d();
                if (!TextUtils.isEmpty(d2)) {
                    m189b(d2);
                    com.xiaomi.channel.commonutils.logger.b.b("loading the new hosts succeed");
                    return true;
                }
            } catch (Throwable th) {
                com.xiaomi.channel.commonutils.logger.b.m19a("load bucket failure: " + th.getMessage());
            }
            return false;
        }
    }

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

    /* access modifiers changed from: protected */
    public String b() {
        String a2 = com.xiaomi.push.service.a.a(f8488a).a();
        return (TextUtils.isEmpty(a2) || PushChannelRegion.China.name().equals(a2)) ? "resolver.msg.xiaomi.net" : "resolver.msg.global.xiaomi.net";
    }

    /* renamed from: b  reason: collision with other method in class */
    public void m188b() {
        ArrayList<String> arrayList;
        synchronized (this.f227a) {
            m187a();
            arrayList = new ArrayList<>(this.f227a.keySet());
            for (int size = arrayList.size() - 1; size >= 0; size--) {
                dq dqVar = this.f227a.get(arrayList.get(size));
                if (!(dqVar == null || dqVar.a() == null)) {
                    arrayList.remove(size);
                }
            }
        }
        ArrayList<dp> a2 = a(arrayList);
        for (int i = 0; i < arrayList.size(); i++) {
            if (a2.get(i) != null) {
                a(arrayList.get(i), a2.get(i));
            }
        }
    }

    /* access modifiers changed from: protected */
    /* renamed from: b  reason: collision with other method in class */
    public void m189b(String str) {
        synchronized (this.f227a) {
            this.f227a.clear();
            JSONObject jSONObject = new JSONObject(str);
            if (jSONObject.optInt("ver") == 2) {
                JSONArray optJSONArray = jSONObject.optJSONArray("data");
                for (int i = 0; i < optJSONArray.length(); i++) {
                    dq a2 = new dq().a(optJSONArray.getJSONObject(i));
                    this.f227a.put(a2.m177a(), a2);
                }
                JSONArray optJSONArray2 = jSONObject.optJSONArray("reserved");
                for (int i2 = 0; i2 < optJSONArray2.length(); i2++) {
                    dp a3 = new dp("").a(optJSONArray2.getJSONObject(i2));
                    f8489b.put(a3.f217b, a3);
                }
            } else {
                throw new JSONException("Bad version");
            }
        }
    }

    /* access modifiers changed from: protected */
    public dp c(String str) {
        dq dqVar;
        dp a2;
        synchronized (this.f227a) {
            m187a();
            dqVar = this.f227a.get(str);
        }
        if (dqVar == null || (a2 = dqVar.a()) == null) {
            return null;
        }
        return a2;
    }

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

    /* renamed from: c  reason: collision with other method in class */
    public void m190c() {
        synchronized (this.f227a) {
            try {
                BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(f8488a.openFileOutput(e(), 0)));
                String jSONObject = m184a().toString();
                if (!TextUtils.isEmpty(jSONObject)) {
                    bufferedWriter.write(jSONObject);
                }
                bufferedWriter.close();
            } catch (Exception e) {
                com.xiaomi.channel.commonutils.logger.b.m19a("persist bucket failure: " + e.getMessage());
            }
        }
    }

    /* access modifiers changed from: protected */
    public dp d(String str) {
        if (System.currentTimeMillis() - this.f230c <= this.f223a * 60 * 1000) {
            return null;
        }
        this.f230c = System.currentTimeMillis();
        ArrayList<String> arrayList = new ArrayList<>();
        arrayList.add(str);
        dp dpVar = a(arrayList).get(0);
        if (dpVar != null) {
            this.f223a = 0;
            return dpVar;
        }
        long j = this.f223a;
        if (j >= 15) {
            return null;
        }
        this.f223a = j + 1;
        return null;
    }

    /* access modifiers changed from: protected */
    public String d() {
        BufferedReader bufferedReader;
        Throwable th;
        Throwable th2;
        try {
            File file = new File(f8488a.getFilesDir(), e());
            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();
                            z.a(bufferedReader);
                            return sb2;
                        }
                    }
                } catch (Throwable th3) {
                    th2 = th3;
                    try {
                        com.xiaomi.channel.commonutils.logger.b.m19a("load host exception " + th2.getMessage());
                        z.a(bufferedReader);
                        return null;
                    } catch (Throwable th4) {
                        th = th4;
                        z.a(bufferedReader);
                        throw th;
                    }
                }
            } else {
                z.a((Closeable) null);
                return null;
            }
        } catch (Throwable th5) {
            bufferedReader = null;
            th = th5;
            z.a(bufferedReader);
            throw th;
        }
    }

    /* renamed from: d  reason: collision with other method in class */
    public void m191d() {
        synchronized (this.f227a) {
            for (dq dqVar : this.f227a.values()) {
                dqVar.a(true);
            }
            while (true) {
                for (boolean z = false; !z; z = true) {
                    for (String str : this.f227a.keySet()) {
                        if (this.f227a.get(str).m178a().isEmpty()) {
                            this.f227a.remove(str);
                        }
                    }
                }
            }
        }
    }

    /* access modifiers changed from: protected */
    public String e() {
        List<ActivityManager.RunningAppProcessInfo> runningAppProcesses = ((ActivityManager) f8488a.getSystemService(PushConstants.INTENT_ACTIVITY_NAME)).getRunningAppProcesses();
        if (runningAppProcesses == null) {
            return "com.xiaomi";
        }
        for (ActivityManager.RunningAppProcessInfo runningAppProcessInfo : runningAppProcesses) {
            if (runningAppProcessInfo.pid == Process.myPid()) {
                return runningAppProcessInfo.processName;
            }
        }
        return "com.xiaomi";
    }
}