apk(点击下载) / db.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 androidx.core.os.EnvironmentCompat;
import com.amap.api.services.district.DistrictSearchQuery;
import com.huawei.hms.push.constant.RemoteMessageConst;
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 db {
    protected static Context a;

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

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

    /* renamed from: a  reason: collision with other field name */
    protected static boolean f183a = false;
    protected static Map<String, cx> b = new HashMap();
    private static String c;

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

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

    /* renamed from: a  reason: collision with other field name */
    private da f185a;

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

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

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

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

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

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

    public interface a {
        db a(Context context, da daVar, b bVar, String str);
    }

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

    protected db(Context context, da daVar, b bVar, String str) {
        this(context, daVar, bVar, str, null, null);
    }

    protected db(Context context, da daVar, b bVar, String str, String str2, String str3) {
        this.f188a = new HashMap();
        this.f187a = PushConstants.PUSH_TYPE_NOTIFY;
        this.f184a = 0;
        this.f189b = 15;
        this.f191c = 0;
        this.f190b = "isp_prov_city_country_ip";
        this.f186a = bVar;
        this.f185a = daVar == null ? new dc(this) : daVar;
        this.f187a = str;
        c = str2 == null ? context.getPackageName() : str2;
        f1937d = str3 == null ? f() : str3;
    }

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

    /* renamed from: a  reason: collision with other method in class */
    static String m149a() {
        NetworkInfo activeNetworkInfo;
        Context context = a;
        if (context == null) {
            return EnvironmentCompat.MEDIA_UNKNOWN;
        }
        try {
            ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService("connectivity");
            if (connectivityManager == null || (activeNetworkInfo = connectivityManager.getActiveNetworkInfo()) == null) {
                return EnvironmentCompat.MEDIA_UNKNOWN;
            }
            if (activeNetworkInfo.getType() == 1) {
                WifiManager wifiManager = (WifiManager) a.getSystemService("wifi");
                if (!(wifiManager == null || wifiManager.getConnectionInfo() == null)) {
                    return "WIFI-" + wifiManager.getConnectionInfo().getSSID();
                }
                return EnvironmentCompat.MEDIA_UNKNOWN;
            }
            return activeNetworkInfo.getTypeName() + Constants.ACCEPT_TIME_SEPARATOR_SERVER + activeNetworkInfo.getSubtypeName();
        } catch (Throwable unused) {
        }
    }

    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 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<cx> a(ArrayList<String> arrayList) {
        JSONObject jSONObject;
        JSONObject jSONObject2;
        m158d();
        synchronized (this.f188a) {
            m154a();
            for (String str : this.f188a.keySet()) {
                if (!arrayList.contains(str)) {
                    arrayList.add(str);
                }
            }
        }
        boolean isEmpty = b.isEmpty();
        synchronized (b) {
            for (Object obj : b.values().toArray()) {
                cx cxVar = (cx) obj;
                if (!cxVar.b()) {
                    b.remove(cxVar.f178b);
                    isEmpty = true;
                }
            }
        }
        if (!arrayList.contains(b())) {
            arrayList.add(b());
        }
        ArrayList<cx> arrayList2 = new ArrayList<>(arrayList.size());
        for (int i = 0; i < arrayList.size(); i++) {
            arrayList2.add(null);
        }
        try {
            String str2 = az.d(a) ? "wifi" : "wap";
            String a2 = a(arrayList, str2, this.f187a, isEmpty);
            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(DistrictSearchQuery.KEYWORDS_PROVINCE);
                    String string2 = jSONObject4.getString(DistrictSearchQuery.KEYWORDS_CITY);
                    String string3 = jSONObject4.getString("isp");
                    String string4 = jSONObject4.getString("ip");
                    String string5 = jSONObject4.getString(DistrictSearchQuery.KEYWORDS_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.m4a("no bucket found for " + str3);
                            jSONObject = jSONObject5;
                        } else {
                            cx cxVar2 = new cx(str3);
                            int i3 = 0;
                            while (i3 < optJSONArray.length()) {
                                String string6 = optJSONArray.getString(i3);
                                if (!TextUtils.isEmpty(string6)) {
                                    jSONObject2 = jSONObject5;
                                    cxVar2.a(new dg(string6, optJSONArray.length() - i3));
                                } else {
                                    jSONObject2 = jSONObject5;
                                }
                                i3++;
                                jSONObject5 = jSONObject2;
                            }
                            jSONObject = jSONObject5;
                            arrayList2.set(i2, cxVar2);
                            cxVar2.g = string5;
                            cxVar2.c = string;
                            cxVar2.e = string3;
                            cxVar2.f = string4;
                            cxVar2.f1936d = string2;
                            if (jSONObject4.has("stat-percent")) {
                                cxVar2.a(jSONObject4.getDouble("stat-percent"));
                            }
                            if (jSONObject4.has("stat-domain")) {
                                cxVar2.b(jSONObject4.getString("stat-domain"));
                            }
                            if (jSONObject4.has(RemoteMessageConst.TTL)) {
                                cxVar2.a(((long) jSONObject4.getInt(RemoteMessageConst.TTL)) * 1000);
                            }
                            m153a(cxVar2.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 keys = optJSONObject.keys();
                        while (keys.hasNext()) {
                            String str4 = (String) keys.next();
                            JSONArray optJSONArray2 = optJSONObject.optJSONArray(str4);
                            if (optJSONArray2 == null) {
                                com.xiaomi.channel.commonutils.logger.b.m4a("no bucket found for " + str4);
                            } else {
                                cx cxVar3 = new cx(str4);
                                cxVar3.a(j);
                                for (int i4 = 0; i4 < optJSONArray2.length(); i4++) {
                                    String string7 = optJSONArray2.getString(i4);
                                    if (!TextUtils.isEmpty(string7)) {
                                        cxVar3.a(new dg(string7, optJSONArray2.length() - i4));
                                    }
                                }
                                synchronized (b) {
                                    if (this.f185a.a(str4)) {
                                        b.put(str4, cxVar3);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        } catch (Exception e) {
            com.xiaomi.channel.commonutils.logger.b.m4a("failed to get bucket " + e.getMessage());
        }
        for (int i5 = 0; i5 < arrayList.size(); i5++) {
            cx cxVar4 = arrayList2.get(i5);
            if (cxVar4 != null) {
                a(arrayList.get(i5), cxVar4);
            }
        }
        m157c();
        return arrayList2;
    }

    public static synchronized void a(Context context, da daVar, b bVar, String str, String str2, String str3) {
        synchronized (db.class) {
            Context applicationContext = context.getApplicationContext();
            a = applicationContext;
            if (applicationContext == null) {
                a = context;
            }
            if (f182a == null) {
                a aVar = f181a;
                if (aVar == null) {
                    f182a = new db(context, daVar, bVar, str, str2, str3);
                } else {
                    f182a = aVar.a(context, daVar, bVar, str);
                }
            }
        }
    }

    public static synchronized void a(a aVar) {
        synchronized (db.class) {
            f181a = aVar;
            f182a = null;
        }
    }

    public static void a(String str, String str2) {
        cx cxVar = b.get(str);
        synchronized (b) {
            if (cxVar == null) {
                cx cxVar2 = new cx(str);
                cxVar2.a(604800000L);
                cxVar2.m142a(str2);
                b.put(str, cxVar2);
            } else {
                cxVar.m142a(str2);
            }
        }
    }

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

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

    public cx a(String str, boolean z) {
        cx d2;
        if (TextUtils.isEmpty(str)) {
            throw new IllegalArgumentException("the host is empty");
        } else if (!this.f185a.a(str)) {
            return null;
        } else {
            cx c2 = c(str);
            return (c2 == null || !c2.b()) ? (!z || !az.b(a) || (d2 = d(str)) == null) ? new dd(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<ay> arrayList3 = new ArrayList();
        arrayList3.add(new aw("type", str));
        if (str.equals("wap")) {
            arrayList3.add(new aw("conpt", a(az.m75a(a))));
        }
        if (z) {
            arrayList3.add(new aw("reserved", "1"));
        }
        arrayList3.add(new aw("uuid", str2));
        arrayList3.add(new aw("list", bf.a(arrayList, Constants.ACCEPT_TIME_SEPARATOR_SP)));
        arrayList3.add(new aw("countrycode", com.xiaomi.push.service.a.a(a).b()));
        cx c2 = c(b());
        String format = String.format(Locale.US, "http://%1$s/gslb/?ver=4.0", b());
        if (c2 == null) {
            arrayList2.add(format);
            synchronized (b) {
                cx cxVar = b.get("resolver.msg.xiaomi.net");
                if (cxVar != null) {
                    Iterator<String> it = cxVar.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 (ay ayVar : arrayList3) {
                buildUpon.appendQueryParameter(ayVar.a(), ayVar.b());
            }
            try {
                b bVar = this.f186a;
                return bVar == null ? az.a(a, new URL(buildUpon.toString())) : bVar.a(buildUpon.toString());
            } catch (IOException e2) {
                e = e2;
            }
        }
        if (e == null) {
            return null;
        }
        com.xiaomi.channel.commonutils.logger.b.m4a("network exception: " + e.getMessage());
        throw e;
    }

    /* access modifiers changed from: protected */
    /* renamed from: a  reason: collision with other method in class */
    public JSONObject m151a() {
        JSONObject jSONObject;
        synchronized (this.f188a) {
            jSONObject = new JSONObject();
            jSONObject.put("ver", 2);
            JSONArray jSONArray = new JSONArray();
            for (cy cyVar : this.f188a.values()) {
                jSONArray.put(cyVar.m146a());
            }
            jSONObject.put("data", jSONArray);
            JSONArray jSONArray2 = new JSONArray();
            for (cx cxVar : b.values()) {
                jSONArray2.put(cxVar.m141a());
            }
            jSONObject.put("reserved", jSONArray2);
        }
        return jSONObject;
    }

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

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

    public void a(String str, cx cxVar) {
        if (TextUtils.isEmpty(str) || cxVar == null) {
            throw new IllegalArgumentException("the argument is invalid " + str + ", " + cxVar);
        } else if (this.f185a.a(str)) {
            synchronized (this.f188a) {
                m154a();
                if (this.f188a.containsKey(str)) {
                    this.f188a.get(str).a(cxVar);
                } else {
                    cy cyVar = new cy(str);
                    cyVar.a(cxVar);
                    this.f188a.put(str, cyVar);
                }
            }
        }
    }

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

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

    /* access modifiers changed from: protected */
    public String b() {
        String a2 = com.xiaomi.push.service.a.a(a).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 m155b() {
        ArrayList<String> arrayList;
        synchronized (this.f188a) {
            m154a();
            arrayList = new ArrayList<>(this.f188a.keySet());
            for (int size = arrayList.size() - 1; size >= 0; size--) {
                cy cyVar = this.f188a.get(arrayList.get(size));
                if (!(cyVar == null || cyVar.a() == null)) {
                    arrayList.remove(size);
                }
            }
        }
        ArrayList<cx> 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 m156b(String str) {
        synchronized (this.f188a) {
            this.f188a.clear();
            JSONObject jSONObject = new JSONObject(str);
            if (jSONObject.optInt("ver") == 2) {
                JSONArray optJSONArray = jSONObject.optJSONArray("data");
                for (int i = 0; i < optJSONArray.length(); i++) {
                    cy a2 = new cy().a(optJSONArray.getJSONObject(i));
                    this.f188a.put(a2.m144a(), a2);
                }
                JSONArray optJSONArray2 = jSONObject.optJSONArray("reserved");
                for (int i2 = 0; i2 < optJSONArray2.length(); i2++) {
                    cx a3 = new cx("").a(optJSONArray2.getJSONObject(i2));
                    b.put(a3.f178b, a3);
                }
            } else {
                throw new JSONException("Bad version");
            }
        }
    }

    /* access modifiers changed from: protected */
    public cx c(String str) {
        cy cyVar;
        cx a2;
        synchronized (this.f188a) {
            m154a();
            cyVar = this.f188a.get(str);
        }
        if (cyVar == null || (a2 = cyVar.a()) == null) {
            return null;
        }
        return a2;
    }

    public String c() {
        StringBuilder sb = new StringBuilder();
        synchronized (this.f188a) {
            for (Map.Entry<String, cy> entry : this.f188a.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 m157c() {
        synchronized (this.f188a) {
            try {
                BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(a.openFileOutput(e(), 0)));
                String jSONObject = m151a().toString();
                if (!TextUtils.isEmpty(jSONObject)) {
                    bufferedWriter.write(jSONObject);
                }
                bufferedWriter.close();
            } catch (Exception e) {
                com.xiaomi.channel.commonutils.logger.b.m4a("persist bucket failure: " + e.getMessage());
            }
        }
    }

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

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

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

    /* access modifiers changed from: protected */
    public String e() {
        List<ActivityManager.RunningAppProcessInfo> runningAppProcesses = ((ActivityManager) a.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";
    }
}