得物.apk(点击下载) / ct.java


package com.xiaomi.push;

import a.f;
import a30.h;
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 java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
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 ct {

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

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

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

    /* renamed from: a  reason: collision with other field name */
    public static boolean f285a = false;
    public static final Map<String, cp> b = new HashMap();

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

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

    /* renamed from: a  reason: collision with other field name */
    private cs f287a;

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

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

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

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

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

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

    public interface a {
        ct a(Context context, cs csVar, b bVar, String str);
    }

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

    public ct(Context context, cs csVar, b bVar, String str) {
        this(context, csVar, bVar, str, null, null);
    }

    public ct(Context context, cs csVar, b bVar, String str, String str2, String str3) {
        this.f290a = new HashMap();
        this.f289a = "0";
        this.f286a = 0;
        this.f291b = 15;
        this.f293c = 0;
        this.f292b = "isp_prov_city_country_ip";
        this.f288a = bVar;
        this.f287a = csVar == null ? new cu(this) : csVar;
        this.f289a = str;
        f30659c = str2 == null ? context.getPackageName() : str2;
        d = str3 == null ? g() : str3;
    }

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

    /* renamed from: a  reason: collision with other method in class */
    public static String m262a() {
        NetworkInfo activeNetworkInfo;
        Context context = f30658a;
        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() + "-" + 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 i = 0; i < bytes.length; i++) {
                byte b2 = bytes[i];
                int i3 = b2 & 240;
                if (i3 != 240) {
                    bytes[i] = (byte) (((b2 & 15) ^ ((byte) (((b2 >> 4) + length) & 15))) | i3);
                }
            }
            return new String(bytes);
        } catch (UnsupportedEncodingException unused) {
            return str;
        }
    }

    private ArrayList<cp> a(ArrayList<String> arrayList) {
        JSONObject jSONObject;
        JSONObject jSONObject2;
        m273e();
        synchronized (this.f290a) {
            m268a();
            for (String str : this.f290a.keySet()) {
                if (!arrayList.contains(str)) {
                    arrayList.add(str);
                }
            }
        }
        Map<String, cp> map = b;
        synchronized (map) {
            for (Object obj : map.values().toArray()) {
                cp cpVar = (cp) obj;
                if (!cpVar.b()) {
                    b.remove(cpVar.f280b);
                }
            }
        }
        if (!arrayList.contains(b())) {
            arrayList.add(b());
        }
        ArrayList<cp> arrayList2 = new ArrayList<>(arrayList.size());
        for (int i = 0; i < arrayList.size(); i++) {
            arrayList2.add(null);
        }
        try {
            String str2 = bh.e(f30658a) ? "wifi" : "wap";
            String a5 = a(arrayList, str2, this.f289a, true);
            if (!TextUtils.isEmpty(a5)) {
                JSONObject jSONObject3 = new JSONObject(a5);
                com.xiaomi.channel.commonutils.logger.b.b(a5);
                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(str2);
                    com.xiaomi.channel.commonutils.logger.b.c("get bucket: net=" + string3 + ", hosts=" + jSONObject5.toString());
                    int i3 = 0;
                    while (i3 < arrayList.size()) {
                        String str3 = arrayList.get(i3);
                        JSONArray optJSONArray = jSONObject5.optJSONArray(str3);
                        if (optJSONArray == null) {
                            com.xiaomi.channel.commonutils.logger.b.m107a("no bucket found for " + str3);
                            jSONObject = jSONObject5;
                        } else {
                            cp cpVar2 = new cp(str3);
                            int i12 = 0;
                            while (i12 < optJSONArray.length()) {
                                String string6 = optJSONArray.getString(i12);
                                if (!TextUtils.isEmpty(string6)) {
                                    jSONObject2 = jSONObject5;
                                    cpVar2.a(new cy(string6, optJSONArray.length() - i12));
                                } else {
                                    jSONObject2 = jSONObject5;
                                }
                                i12++;
                                jSONObject5 = jSONObject2;
                            }
                            jSONObject = jSONObject5;
                            arrayList2.set(i3, cpVar2);
                            cpVar2.f30654g = string5;
                            cpVar2.f30653c = string;
                            cpVar2.e = string3;
                            cpVar2.f = string4;
                            cpVar2.d = string2;
                            if (jSONObject4.has("stat-percent")) {
                                cpVar2.a(jSONObject4.getDouble("stat-percent"));
                            }
                            if (jSONObject4.has("stat-domain")) {
                                cpVar2.b(jSONObject4.getString("stat-domain"));
                            }
                            if (jSONObject4.has("ttl")) {
                                cpVar2.a(((long) jSONObject4.getInt("ttl")) * 1000);
                            }
                            m267a(cpVar2.a());
                        }
                        i3++;
                        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.m107a("no bucket found for " + next);
                            } else {
                                cp cpVar3 = new cp(next);
                                cpVar3.a(j);
                                for (int i13 = 0; i13 < optJSONArray2.length(); i13++) {
                                    String string7 = optJSONArray2.getString(i13);
                                    if (!TextUtils.isEmpty(string7)) {
                                        cpVar3.a(new cy(string7, optJSONArray2.length() - i13));
                                    }
                                }
                                Map<String, cp> map2 = b;
                                synchronized (map2) {
                                    if (this.f287a.a(next)) {
                                        map2.put(next, cpVar3);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        } catch (Exception e) {
            StringBuilder h12 = f.h("failed to get bucket ");
            h12.append(e.getMessage());
            com.xiaomi.channel.commonutils.logger.b.m107a(h12.toString());
        }
        for (int i14 = 0; i14 < arrayList.size(); i14++) {
            cp cpVar4 = arrayList2.get(i14);
            if (cpVar4 != null) {
                a(arrayList.get(i14), cpVar4);
            }
        }
        m271c();
        return arrayList2;
    }

    public static synchronized void a(Context context, cs csVar, b bVar, String str, String str2, String str3) {
        synchronized (ct.class) {
            Context applicationContext = context.getApplicationContext();
            f30658a = applicationContext;
            if (applicationContext == null) {
                f30658a = context;
            }
            if (f284a == null) {
                a aVar = f283a;
                if (aVar == null) {
                    f284a = new ct(context, csVar, bVar, str, str2, str3);
                } else {
                    f284a = aVar.a(context, csVar, bVar, str);
                }
            }
        }
    }

    public static synchronized void a(a aVar) {
        synchronized (ct.class) {
            f283a = aVar;
            f284a = null;
        }
    }

    public static void a(String str, String str2) {
        Map<String, cp> map = b;
        cp cpVar = map.get(str);
        synchronized (map) {
            if (cpVar == null) {
                cp cpVar2 = new cp(str);
                cpVar2.a(604800000L);
                cpVar2.m255a(str2);
                map.put(str, cpVar2);
            } else {
                cpVar.m255a(str2);
            }
        }
    }

    /* renamed from: a  reason: collision with other method in class */
    private byte[] m263a() {
        return bm.m216a(f30658a.getPackageName() + "_key_salt");
    }

    private String f() {
        return "host_fallbacks";
    }

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

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

    public cp a(String str, boolean z) {
        cp e;
        if (TextUtils.isEmpty(str)) {
            throw new IllegalArgumentException("the host is empty");
        } else if (!this.f287a.a(str)) {
            return null;
        } else {
            cp c5 = c(str);
            return (c5 == null || !c5.b()) ? (!z || !bh.b(f30658a) || (e = e(str)) == null) ? new cv(this, str, c5) : e : c5;
        }
    }

    public String a(ArrayList<String> arrayList, String str, String str2, boolean z) {
        ArrayList<String> arrayList2 = new ArrayList<>();
        ArrayList arrayList3 = new ArrayList();
        arrayList3.add(new be("type", str));
        if (str.equals("wap")) {
            arrayList3.add(new be("conpt", a(bh.m207a(f30658a))));
        }
        if (z) {
            arrayList3.add(new be("reserved", "1"));
        }
        arrayList3.add(new be("uuid", str2));
        arrayList3.add(new be("list", bn.a(arrayList, ",")));
        arrayList3.add(new be("countrycode", com.xiaomi.push.service.a.a(f30658a).b()));
        arrayList3.add(new be("push_sdk_vc", String.valueOf(50012)));
        String b2 = b();
        cp c5 = c(b2);
        String format = String.format(Locale.US, "https://%1$s/gslb/?ver=5.0", b2);
        if (c5 == null) {
            arrayList2.add(format);
            Map<String, cp> map = b;
            synchronized (map) {
                cp cpVar = map.get(b2);
                if (cpVar != null) {
                    Iterator<String> it2 = cpVar.a(true).iterator();
                    while (it2.hasNext()) {
                        arrayList2.add(String.format(Locale.US, "https://%1$s/gslb/?ver=5.0", it2.next()));
                    }
                }
            }
        } else {
            arrayList2 = c5.a(format);
        }
        Iterator<String> it3 = arrayList2.iterator();
        IOException e = null;
        while (it3.hasNext()) {
            Uri.Builder buildUpon = Uri.parse(it3.next()).buildUpon();
            Iterator it4 = arrayList3.iterator();
            while (it4.hasNext()) {
                bg bgVar = (bg) it4.next();
                buildUpon.appendQueryParameter(bgVar.a(), bgVar.b());
            }
            try {
                b bVar = this.f288a;
                return bVar == null ? bh.a(f30658a, new URL(buildUpon.toString())) : bVar.a(buildUpon.toString());
            } catch (IOException e6) {
                e = e6;
            }
        }
        if (e == null) {
            return null;
        }
        StringBuilder h12 = f.h("network exception: ");
        h12.append(e.getMessage());
        com.xiaomi.channel.commonutils.logger.b.m107a(h12.toString());
        throw e;
    }

    /* renamed from: a  reason: collision with other method in class */
    public JSONObject m265a() {
        JSONObject jSONObject;
        synchronized (this.f290a) {
            jSONObject = new JSONObject();
            jSONObject.put("ver", 2);
            JSONArray jSONArray = new JSONArray();
            for (cq cqVar : this.f290a.values()) {
                jSONArray.put(cqVar.m259a());
            }
            jSONObject.put("data", jSONArray);
            JSONArray jSONArray2 = new JSONArray();
            for (cp cpVar : b.values()) {
                jSONArray2.put(cpVar.m254a());
            }
            jSONObject.put("reserved", jSONArray2);
        }
        return jSONObject;
    }

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

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

    public void a(String str, cp cpVar) {
        if (TextUtils.isEmpty(str) || cpVar == null) {
            throw new IllegalArgumentException("the argument is invalid " + str + ", " + cpVar);
        } else if (this.f287a.a(str)) {
            synchronized (this.f290a) {
                m268a();
                if (this.f290a.containsKey(str)) {
                    this.f290a.get(str).a(cpVar);
                } else {
                    cq cqVar = new cq(str);
                    cqVar.a(cpVar);
                    this.f290a.put(str, cqVar);
                }
            }
        }
    }

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

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

    public String b() {
        return "resolver.msg.xiaomi.net";
    }

    /* renamed from: b  reason: collision with other method in class */
    public void m269b() {
        ArrayList<String> arrayList;
        synchronized (this.f290a) {
            m268a();
            arrayList = new ArrayList<>(this.f290a.keySet());
            for (int size = arrayList.size() - 1; size >= 0; size--) {
                cq cqVar = this.f290a.get(arrayList.get(size));
                if (!(cqVar == null || cqVar.a() == null)) {
                    arrayList.remove(size);
                }
            }
        }
        ArrayList<cp> a5 = a(arrayList);
        for (int i = 0; i < arrayList.size(); i++) {
            if (a5.get(i) != null) {
                a(arrayList.get(i), a5.get(i));
            }
        }
    }

    /* renamed from: b  reason: collision with other method in class */
    public void m270b(String str) {
        synchronized (this.f290a) {
            this.f290a.clear();
            JSONObject jSONObject = new JSONObject(str);
            if (jSONObject.optInt("ver") == 2) {
                JSONArray optJSONArray = jSONObject.optJSONArray("data");
                if (optJSONArray != null) {
                    for (int i = 0; i < optJSONArray.length(); i++) {
                        cq a5 = new cq().a(optJSONArray.getJSONObject(i));
                        this.f290a.put(a5.m257a(), a5);
                    }
                }
                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 {
                                cp a12 = new cp(optString).a(jSONObject2);
                                b.put(a12.f280b, a12);
                                com.xiaomi.channel.commonutils.logger.b.m107a("load local reserved host for " + a12.f280b);
                            } catch (JSONException unused) {
                                com.xiaomi.channel.commonutils.logger.b.m107a("parse reserved host fail.");
                            }
                        }
                    }
                }
            } else {
                throw new JSONException("Bad version");
            }
        }
    }

    public cp c(String str) {
        cq cqVar;
        cp a5;
        synchronized (this.f290a) {
            m268a();
            cqVar = this.f290a.get(str);
        }
        if (cqVar == null || (a5 = cqVar.a()) == null) {
            return null;
        }
        return a5;
    }

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

    /* renamed from: c  reason: collision with other method in class */
    public void m271c() {
        BufferedOutputStream bufferedOutputStream;
        FileOutputStream fileOutputStream;
        Throwable th2;
        Exception e;
        Exception e6;
        Throwable th3;
        synchronized (this.f290a) {
            BufferedOutputStream bufferedOutputStream2 = null;
            try {
                String jSONObject = m265a().toString();
                com.xiaomi.channel.commonutils.logger.b.b("persist host fallbacks = " + jSONObject);
                if (!TextUtils.isEmpty(jSONObject)) {
                    fileOutputStream = f30658a.openFileOutput(f(), 0);
                    try {
                        bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
                        try {
                            bufferedOutputStream.write(h.b(m263a(), jSONObject.getBytes(StandardCharsets.UTF_8)));
                            bufferedOutputStream.flush();
                            bufferedOutputStream2 = bufferedOutputStream;
                        } catch (Exception e12) {
                            e = e12;
                            try {
                                com.xiaomi.channel.commonutils.logger.b.m107a("persist bucket failure: " + e.getMessage());
                                x.a(bufferedOutputStream);
                                x.a(fileOutputStream);
                            } catch (Throwable th4) {
                                th2 = th4;
                                x.a(bufferedOutputStream);
                                x.a(fileOutputStream);
                                throw th2;
                            }
                        }
                    } catch (Exception e13) {
                        e6 = e13;
                        bufferedOutputStream = null;
                        e = e6;
                        com.xiaomi.channel.commonutils.logger.b.m107a("persist bucket failure: " + e.getMessage());
                        x.a(bufferedOutputStream);
                        x.a(fileOutputStream);
                    } catch (Throwable th5) {
                        th3 = th5;
                        bufferedOutputStream = null;
                        th2 = th3;
                        x.a(bufferedOutputStream);
                        x.a(fileOutputStream);
                        throw th2;
                    }
                } else {
                    fileOutputStream = null;
                }
                x.a(bufferedOutputStream2);
            } catch (Exception e14) {
                e6 = e14;
                fileOutputStream = null;
                bufferedOutputStream = null;
                e = e6;
                com.xiaomi.channel.commonutils.logger.b.m107a("persist bucket failure: " + e.getMessage());
                x.a(bufferedOutputStream);
                x.a(fileOutputStream);
            } catch (Throwable th6) {
                th3 = th6;
                fileOutputStream = null;
                bufferedOutputStream = null;
                th2 = th3;
                x.a(bufferedOutputStream);
                x.a(fileOutputStream);
                throw th2;
            }
            x.a(fileOutputStream);
        }
    }

    public cp d(String str) {
        cp cpVar;
        Map<String, cp> map = b;
        synchronized (map) {
            cpVar = map.get(str);
        }
        return cpVar;
    }

    public String d() {
        Throwable th2;
        InputStream inputStream;
        InputStream inputStream2;
        try {
            File file = new File(f30658a.getFilesDir(), f());
            if (file.isFile()) {
                inputStream = new FileInputStream(file);
                try {
                    inputStream2 = new BufferedInputStream(inputStream);
                } catch (Throwable th3) {
                    th2 = th3;
                    inputStream2 = null;
                    try {
                        com.xiaomi.channel.commonutils.logger.b.m107a("load host exception " + th2.getMessage());
                        return null;
                    } finally {
                        x.a((Closeable) inputStream2);
                        x.a((Closeable) inputStream);
                    }
                }
                try {
                    String str = new String(h.a(m263a(), x.a(inputStream2)), StandardCharsets.UTF_8);
                    com.xiaomi.channel.commonutils.logger.b.b("load host fallbacks = " + str);
                    x.a((Closeable) inputStream2);
                    x.a((Closeable) inputStream);
                    return str;
                } catch (Throwable th4) {
                    th2 = th4;
                    com.xiaomi.channel.commonutils.logger.b.m107a("load host exception " + th2.getMessage());
                    return null;
                }
            } else {
                x.a((Closeable) null);
                x.a((Closeable) null);
                return null;
            }
        } catch (Throwable th5) {
            th2 = th5;
            inputStream2 = null;
            inputStream = inputStream2;
            com.xiaomi.channel.commonutils.logger.b.m107a("load host exception " + th2.getMessage());
            return null;
        }
    }

    /* renamed from: d  reason: collision with other method in class */
    public void m272d() {
        String e = e();
        try {
            File file = new File(f30658a.getFilesDir(), e);
            if (file.exists()) {
                boolean delete = file.delete();
                StringBuilder sb2 = new StringBuilder();
                sb2.append("Delete old host fallbacks file ");
                sb2.append(e);
                sb2.append(delete ? " successful." : " failed.");
                com.xiaomi.channel.commonutils.logger.b.m107a(sb2.toString());
                return;
            }
            com.xiaomi.channel.commonutils.logger.b.b("Old host fallbacks file " + e + " does not exist.");
        } catch (Exception e6) {
            StringBuilder g3 = h.g("Delete old host fallbacks file ", e, " error: ");
            g3.append(e6.getMessage());
            com.xiaomi.channel.commonutils.logger.b.m107a(g3.toString());
        }
    }

    public cp e(String str) {
        if (System.currentTimeMillis() - this.f293c <= this.f286a * 60 * 1000) {
            return null;
        }
        this.f293c = System.currentTimeMillis();
        cp cpVar = a(a2.a.e(str)).get(0);
        if (cpVar != null) {
            this.f286a = 0;
            return cpVar;
        }
        long j = this.f286a;
        if (j >= 15) {
            return null;
        }
        this.f286a = j + 1;
        return null;
    }

    public String e() {
        return "com.xiaomi.xmsf".equals(f30659c) ? f30659c : f.g(new StringBuilder(), f30659c, ":pushservice");
    }

    /* renamed from: e  reason: collision with other method in class */
    public void m273e() {
        synchronized (this.f290a) {
            for (cq cqVar : this.f290a.values()) {
                cqVar.a(true);
            }
            while (true) {
                for (boolean z = false; !z; z = true) {
                    for (String str : this.f290a.keySet()) {
                        if (this.f290a.get(str).m258a().isEmpty()) {
                            this.f290a.remove(str);
                        }
                    }
                }
            }
        }
    }
}