米游社.apk(点击下载) / cv.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.alibaba.security.common.utils.NetWorkUtils;
import com.google.common.base.Ascii;
import com.huawei.hms.push.constant.RemoteMessageConst;
import com.tencent.qcloud.core.util.IOUtils;
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.Locale;
import java.util.Map;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class cv {

    /* renamed from: a  reason: collision with root package name */
    public static Context f42710a;

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

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

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

    /* renamed from: b  reason: collision with root package name */
    public static final Map<String, cr> f42711b = new HashMap();

    /* renamed from: c  reason: collision with root package name */
    private static String f42712c;

    /* renamed from: d  reason: collision with root package name */
    private static String f42713d;

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

    /* renamed from: a  reason: collision with other field name */
    private cu f399a;

    /* renamed from: a  reason: collision with other field name */
    public b f400a;

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

    /* renamed from: a  reason: collision with other field name */
    public final Map<String, cs> f402a;

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

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

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

    public interface a {
        cv a(Context context, cu cuVar, b bVar, String str);
    }

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

    public cv(Context context, cu cuVar, b bVar, String str) {
        this(context, cuVar, bVar, str, null, null);
    }

    public cv(Context context, cu cuVar, b bVar, String str, String str2, String str3) {
        this.f402a = new HashMap();
        this.f401a = "0";
        this.f398a = 0;
        this.f403b = 15;
        this.f405c = 0;
        this.f404b = "isp_prov_city_country_ip";
        this.f400a = bVar;
        this.f399a = cuVar == null ? new cw(this) : cuVar;
        this.f401a = str;
        f42712c = str2 == null ? context.getPackageName() : str2;
        f42713d = str3 == null ? f() : str3;
    }

    public static synchronized cv a() {
        cv cvVar;
        synchronized (cv.class) {
            cvVar = f396a;
            if (cvVar == null) {
                throw new IllegalStateException("the host manager is not initialized yet.");
            }
        }
        return cvVar;
    }

    /* renamed from: a  reason: collision with other method in class */
    public static String m666a() {
        NetworkInfo activeNetworkInfo;
        Context context = f42710a;
        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 String a(String str) {
        try {
            int length = str.length();
            byte[] bytes = str.getBytes("UTF-8");
            for (int i10 = 0; i10 < bytes.length; i10++) {
                byte b10 = bytes[i10];
                int i11 = b10 & 240;
                if (i11 != 240) {
                    bytes[i10] = (byte) (((b10 & Ascii.SI) ^ ((byte) (((b10 >> 4) + length) & 15))) | i11);
                }
            }
            return new String(bytes);
        } catch (UnsupportedEncodingException unused) {
            return str;
        }
    }

    private ArrayList<cr> a(ArrayList<String> arrayList) {
        JSONObject jSONObject;
        JSONObject jSONObject2;
        m675d();
        synchronized (this.f402a) {
            m671a();
            for (String str : this.f402a.keySet()) {
                if (!arrayList.contains(str)) {
                    arrayList.add(str);
                }
            }
        }
        Map<String, cr> map = f42711b;
        synchronized (map) {
            for (Object obj : map.values().toArray()) {
                cr crVar = (cr) obj;
                if (!crVar.b()) {
                    f42711b.remove(crVar.f392b);
                }
            }
        }
        if (!arrayList.contains(b())) {
            arrayList.add(b());
        }
        ArrayList<cr> arrayList2 = new ArrayList<>(arrayList.size());
        for (int i10 = 0; i10 < arrayList.size(); i10++) {
            arrayList2.add(null);
        }
        try {
            String str2 = bj.e(f42710a) ? NetWorkUtils.NETWORK_WIFI : "wap";
            String a10 = a(arrayList, str2, this.f401a, true);
            if (!TextUtils.isEmpty(a10)) {
                JSONObject jSONObject3 = new JSONObject(a10);
                com.xiaomi.channel.commonutils.logger.b.b(a10);
                if ("OK".equalsIgnoreCase(jSONObject3.getString(w1.a.T4))) {
                    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(str2);
                    com.xiaomi.channel.commonutils.logger.b.c("get bucket: net=" + string3 + ", hosts=" + jSONObject5.toString());
                    int i11 = 0;
                    while (i11 < arrayList.size()) {
                        String str3 = arrayList.get(i11);
                        JSONArray optJSONArray = jSONObject5.optJSONArray(str3);
                        if (optJSONArray == null) {
                            com.xiaomi.channel.commonutils.logger.b.m510a("no bucket found for " + str3);
                            jSONObject = jSONObject5;
                        } else {
                            cr crVar2 = new cr(str3);
                            int i12 = 0;
                            while (i12 < optJSONArray.length()) {
                                String string6 = optJSONArray.getString(i12);
                                if (!TextUtils.isEmpty(string6)) {
                                    jSONObject2 = jSONObject5;
                                    crVar2.a(new da(string6, optJSONArray.length() - i12));
                                } else {
                                    jSONObject2 = jSONObject5;
                                }
                                i12++;
                                jSONObject5 = jSONObject2;
                            }
                            jSONObject = jSONObject5;
                            arrayList2.set(i11, crVar2);
                            crVar2.f42704g = string5;
                            crVar2.f42700c = string;
                            crVar2.f42702e = string3;
                            crVar2.f42703f = string4;
                            crVar2.f42701d = string2;
                            if (jSONObject4.has("stat-percent")) {
                                crVar2.a(jSONObject4.getDouble("stat-percent"));
                            }
                            if (jSONObject4.has("stat-domain")) {
                                crVar2.b(jSONObject4.getString("stat-domain"));
                            }
                            if (jSONObject4.has(RemoteMessageConst.TTL)) {
                                crVar2.a(((long) jSONObject4.getInt(RemoteMessageConst.TTL)) * 1000);
                            }
                            m670a(crVar2.a());
                        }
                        i11++;
                        jSONObject5 = jSONObject;
                    }
                    JSONObject optJSONObject = jSONObject4.optJSONObject("reserved");
                    if (optJSONObject != null) {
                        long j10 = 604800000;
                        if (jSONObject4.has("reserved-ttl")) {
                            j10 = ((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.m510a("no bucket found for " + next);
                            } else {
                                cr crVar3 = new cr(next);
                                crVar3.a(j10);
                                for (int i13 = 0; i13 < optJSONArray2.length(); i13++) {
                                    String string7 = optJSONArray2.getString(i13);
                                    if (!TextUtils.isEmpty(string7)) {
                                        crVar3.a(new da(string7, optJSONArray2.length() - i13));
                                    }
                                }
                                Map<String, cr> map2 = f42711b;
                                synchronized (map2) {
                                    if (this.f399a.a(next)) {
                                        map2.put(next, crVar3);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        } catch (Exception e10) {
            com.xiaomi.channel.commonutils.logger.b.m510a("failed to get bucket " + e10.getMessage());
        }
        for (int i14 = 0; i14 < arrayList.size(); i14++) {
            cr crVar4 = arrayList2.get(i14);
            if (crVar4 != null) {
                a(arrayList.get(i14), crVar4);
            }
        }
        m674c();
        return arrayList2;
    }

    public static synchronized void a(Context context, cu cuVar, b bVar, String str, String str2, String str3) {
        synchronized (cv.class) {
            Context applicationContext = context.getApplicationContext();
            f42710a = applicationContext;
            if (applicationContext == null) {
                f42710a = context;
            }
            if (f396a == null) {
                a aVar = f395a;
                if (aVar == null) {
                    f396a = new cv(context, cuVar, bVar, str, str2, str3);
                } else {
                    f396a = aVar.a(context, cuVar, bVar, str);
                }
            }
        }
    }

    public static synchronized void a(a aVar) {
        synchronized (cv.class) {
            f395a = aVar;
            f396a = null;
        }
    }

    public static void a(String str, String str2) {
        Map<String, cr> map = f42711b;
        cr crVar = map.get(str);
        synchronized (map) {
            if (crVar == null) {
                cr crVar2 = new cr(str);
                crVar2.a(604800000L);
                crVar2.m659a(str2);
                map.put(str, crVar2);
            } else {
                crVar.m659a(str2);
            }
        }
    }

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

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

    public cr a(String str, boolean z10) {
        cr e10;
        if (TextUtils.isEmpty(str)) {
            throw new IllegalArgumentException("the host is empty");
        } else if (!this.f399a.a(str)) {
            return null;
        } else {
            cr c10 = c(str);
            return (c10 == null || !c10.b()) ? (!z10 || !bj.b(f42710a) || (e10 = e(str)) == null) ? new cx(this, str, c10) : e10 : c10;
        }
    }

    public String a(ArrayList<String> arrayList, String str, String str2, boolean z10) {
        ArrayList<String> arrayList2 = new ArrayList<>();
        ArrayList<bi> arrayList3 = new ArrayList();
        arrayList3.add(new bg("type", str));
        if (str.equals("wap")) {
            arrayList3.add(new bg("conpt", a(bj.m610a(f42710a))));
        }
        if (z10) {
            arrayList3.add(new bg("reserved", "1"));
        }
        arrayList3.add(new bg("uuid", str2));
        arrayList3.add(new bg("list", bp.a(arrayList, ",")));
        arrayList3.add(new bg("countrycode", com.xiaomi.push.service.a.a(f42710a).b()));
        String b10 = b();
        cr c10 = c(b10);
        String format = String.format(Locale.US, "https://%1$s/gslb/?ver=4.0", b10);
        if (c10 == null) {
            arrayList2.add(format);
            Map<String, cr> map = f42711b;
            synchronized (map) {
                cr crVar = map.get(b10);
                if (crVar != null) {
                    Iterator<String> it2 = crVar.a(true).iterator();
                    while (it2.hasNext()) {
                        arrayList2.add(String.format(Locale.US, "https://%1$s/gslb/?ver=4.0", it2.next()));
                    }
                }
            }
        } else {
            arrayList2 = c10.a(format);
        }
        Iterator<String> it3 = arrayList2.iterator();
        IOException e10 = null;
        while (it3.hasNext()) {
            Uri.Builder buildUpon = Uri.parse(it3.next()).buildUpon();
            for (bi biVar : arrayList3) {
                buildUpon.appendQueryParameter(biVar.a(), biVar.b());
            }
            try {
                b bVar = this.f400a;
                return bVar == null ? bj.a(f42710a, new URL(buildUpon.toString())) : bVar.a(buildUpon.toString());
            } catch (IOException e11) {
                e10 = e11;
            }
        }
        if (e10 == null) {
            return null;
        }
        com.xiaomi.channel.commonutils.logger.b.m510a("network exception: " + e10.getMessage());
        throw e10;
    }

    /* renamed from: a  reason: collision with other method in class */
    public JSONObject m668a() {
        JSONObject jSONObject;
        synchronized (this.f402a) {
            jSONObject = new JSONObject();
            jSONObject.put("ver", 2);
            JSONArray jSONArray = new JSONArray();
            for (cs csVar : this.f402a.values()) {
                jSONArray.put(csVar.m663a());
            }
            jSONObject.put("data", jSONArray);
            JSONArray jSONArray2 = new JSONArray();
            for (cr crVar : f42711b.values()) {
                jSONArray2.put(crVar.m658a());
            }
            jSONObject.put("reserved", jSONArray2);
        }
        return jSONObject;
    }

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

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

    public void a(String str, cr crVar) {
        if (TextUtils.isEmpty(str) || crVar == null) {
            throw new IllegalArgumentException("the argument is invalid " + str + ", " + crVar);
        } else if (this.f399a.a(str)) {
            synchronized (this.f402a) {
                m671a();
                if (this.f402a.containsKey(str)) {
                    this.f402a.get(str).a(crVar);
                } else {
                    cs csVar = new cs(str);
                    csVar.a(crVar);
                    this.f402a.put(str, csVar);
                }
            }
        }
    }

    /* renamed from: a  reason: collision with other method in class */
    public boolean m671a() {
        synchronized (this.f402a) {
            if (f397a) {
                return true;
            }
            f397a = true;
            this.f402a.clear();
            try {
                String d10 = d();
                if (!TextUtils.isEmpty(d10)) {
                    m673b(d10);
                    com.xiaomi.channel.commonutils.logger.b.b("loading the new hosts succeed");
                    return true;
                }
            } catch (Throwable th2) {
                com.xiaomi.channel.commonutils.logger.b.m510a("load bucket failure: " + th2.getMessage());
            }
            return false;
        }
    }

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

    public String b() {
        String a10 = com.xiaomi.push.service.a.a(f42710a).a();
        return (TextUtils.isEmpty(a10) || PushChannelRegion.China.name().equals(a10)) ? "resolver.msg.xiaomi.net" : "resolver.msg.global.xiaomi.net";
    }

    /* renamed from: b  reason: collision with other method in class */
    public void m672b() {
        ArrayList<String> arrayList;
        synchronized (this.f402a) {
            m671a();
            arrayList = new ArrayList<>(this.f402a.keySet());
            for (int size = arrayList.size() - 1; size >= 0; size--) {
                cs csVar = this.f402a.get(arrayList.get(size));
                if (!(csVar == null || csVar.a() == null)) {
                    arrayList.remove(size);
                }
            }
        }
        ArrayList<cr> a10 = a(arrayList);
        for (int i10 = 0; i10 < arrayList.size(); i10++) {
            if (a10.get(i10) != null) {
                a(arrayList.get(i10), a10.get(i10));
            }
        }
    }

    /* renamed from: b  reason: collision with other method in class */
    public void m673b(String str) {
        synchronized (this.f402a) {
            this.f402a.clear();
            JSONObject jSONObject = new JSONObject(str);
            if (jSONObject.optInt("ver") == 2) {
                JSONArray optJSONArray = jSONObject.optJSONArray("data");
                if (optJSONArray != null) {
                    for (int i10 = 0; i10 < optJSONArray.length(); i10++) {
                        cs a10 = new cs().a(optJSONArray.getJSONObject(i10));
                        this.f402a.put(a10.m661a(), a10);
                    }
                }
                JSONArray optJSONArray2 = jSONObject.optJSONArray("reserved");
                if (optJSONArray2 != null) {
                    for (int i11 = 0; i11 < optJSONArray2.length(); i11++) {
                        JSONObject jSONObject2 = optJSONArray2.getJSONObject(i11);
                        String optString = jSONObject2.optString("host");
                        if (!TextUtils.isEmpty(optString)) {
                            try {
                                cr a11 = new cr(optString).a(jSONObject2);
                                f42711b.put(a11.f392b, a11);
                                com.xiaomi.channel.commonutils.logger.b.m510a("load local reserved host for " + a11.f392b);
                            } catch (JSONException unused) {
                                com.xiaomi.channel.commonutils.logger.b.m510a("parse reserved host fail.");
                            }
                        }
                    }
                }
            } else {
                throw new JSONException("Bad version");
            }
        }
    }

    public cr c(String str) {
        cs csVar;
        cr a10;
        synchronized (this.f402a) {
            m671a();
            csVar = this.f402a.get(str);
        }
        if (csVar == null || (a10 = csVar.a()) == null) {
            return null;
        }
        return a10;
    }

    public String c() {
        StringBuilder sb2 = new StringBuilder();
        synchronized (this.f402a) {
            for (Map.Entry<String, cs> entry : this.f402a.entrySet()) {
                sb2.append(entry.getKey());
                sb2.append(":\n");
                sb2.append(entry.getValue().toString());
                sb2.append(IOUtils.LINE_SEPARATOR_UNIX);
            }
        }
        return sb2.toString();
    }

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

    public cr d(String str) {
        cr crVar;
        Map<String, cr> map = f42711b;
        synchronized (map) {
            crVar = map.get(str);
        }
        return crVar;
    }

    public String d() {
        BufferedReader bufferedReader;
        Throwable th2;
        try {
            File file = new File(f42710a.getFilesDir(), e());
            if (file.isFile()) {
                bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
                try {
                    StringBuilder sb2 = new StringBuilder();
                    while (true) {
                        String readLine = bufferedReader.readLine();
                        if (readLine != null) {
                            sb2.append(readLine);
                        } else {
                            String sb3 = sb2.toString();
                            ab.a(bufferedReader);
                            return sb3;
                        }
                    }
                } catch (Throwable th3) {
                    th2 = th3;
                    try {
                        com.xiaomi.channel.commonutils.logger.b.m510a("load host exception " + th2.getMessage());
                        return null;
                    } finally {
                        ab.a(bufferedReader);
                    }
                }
            } else {
                ab.a((Closeable) null);
                return null;
            }
        } catch (Throwable th4) {
            th2 = th4;
            bufferedReader = null;
            com.xiaomi.channel.commonutils.logger.b.m510a("load host exception " + th2.getMessage());
            return null;
        }
    }

    /* renamed from: d  reason: collision with other method in class */
    public void m675d() {
        synchronized (this.f402a) {
            for (cs csVar : this.f402a.values()) {
                csVar.a(true);
            }
            while (true) {
                for (boolean z10 = false; !z10; z10 = true) {
                    for (String str : this.f402a.keySet()) {
                        if (this.f402a.get(str).m662a().isEmpty()) {
                            this.f402a.remove(str);
                        }
                    }
                }
            }
        }
    }

    public cr e(String str) {
        if (System.currentTimeMillis() - this.f405c <= this.f398a * 60 * 1000) {
            return null;
        }
        this.f405c = System.currentTimeMillis();
        ArrayList<String> arrayList = new ArrayList<>();
        arrayList.add(str);
        cr crVar = a(arrayList).get(0);
        if (crVar != null) {
            this.f398a = 0;
            return crVar;
        }
        long j10 = this.f398a;
        if (j10 >= 15) {
            return null;
        }
        this.f398a = j10 + 1;
        return null;
    }

    public String e() {
        if ("com.xiaomi.xmsf".equals(f42712c)) {
            return f42712c;
        }
        return f42712c + ":pushservice";
    }
}