嘿嘿连载.apk(点击下载) / ds.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.exifinterface.media.ExifInterface;
import com.meizu.cloud.pushsdk.constants.PushConstants;
import com.umeng.commonsdk.proguard.ap;
import com.umeng.commonsdk.proguard.e;
import com.umeng.umcrash.UMCustomLogInfoBuilder;
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;
import tv.danmaku.ijk.media.player.IjkMediaPlayer;

public class ds {

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

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

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

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

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

    /* renamed from: a  reason: collision with other field name */
    private dr f826a;

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

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

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

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

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

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

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

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

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

    protected ds(Context context, dr drVar, b bVar, String str, String str2, String str3) {
        this.f829a = new HashMap();
        this.f828a = PushConstants.PUSH_TYPE_NOTIFY;
        this.f825a = 0;
        this.f830b = 15;
        this.f832c = 0;
        this.f831b = "isp_prov_city_country_ip";
        this.f827a = bVar;
        this.f826a = drVar == null ? new dt(this) : drVar;
        this.f828a = str;
        c = str2 == null ? context.getPackageName() : str2;
        d = str3 == null ? f() : str3;
    }

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

    /* renamed from: a  reason: collision with other method in class */
    static String m3170a() {
        NetworkInfo activeNetworkInfo;
        Context context = f6667a;
        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) f6667a.getSystemService("wifi");
                if (!(wifiManager == null || wifiManager.getConnectionInfo() == null)) {
                    return "WIFI-" + wifiManager.getConnectionInfo().getSSID();
                }
                return "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 & ap.m) ^ ((byte) (((b2 >> 4) + length) & 15))) | i2);
                }
            }
            return new String(bytes);
        } catch (UnsupportedEncodingException unused) {
            return str;
        }
    }

    private ArrayList<Cdo> a(ArrayList<String> arrayList) {
        boolean z;
        JSONObject jSONObject;
        JSONObject jSONObject2;
        m3179d();
        synchronized (this.f829a) {
            m3175a();
            for (String str : this.f829a.keySet()) {
                if (!arrayList.contains(str)) {
                    arrayList.add(str);
                }
            }
        }
        boolean isEmpty = b.isEmpty();
        synchronized (b) {
            z = isEmpty;
            for (Object obj : b.values().toArray()) {
                Cdo doVar = (Cdo) obj;
                if (!doVar.b()) {
                    b.remove(doVar.f819b);
                    z = true;
                }
            }
        }
        if (!arrayList.contains(b())) {
            arrayList.add(b());
        }
        ArrayList<Cdo> arrayList2 = new ArrayList<>(arrayList.size());
        for (int i = 0; i < arrayList.size(); i++) {
            arrayList2.add(null);
        }
        try {
            String str2 = as.d(f6667a) ? "wifi" : "wap";
            String a2 = a(arrayList, str2, this.f828a, z);
            if (!TextUtils.isEmpty(a2)) {
                JSONObject jSONObject3 = new JSONObject(a2);
                com.xiaomi.channel.commonutils.logger.b.b(a2);
                if ("OK".equalsIgnoreCase(jSONObject3.getString(ExifInterface.LATITUDE_SOUTH))) {
                    JSONObject jSONObject4 = jSONObject3.getJSONObject("R");
                    String string = jSONObject4.getString("province");
                    String string2 = jSONObject4.getString("city");
                    String string3 = jSONObject4.getString("isp");
                    String string4 = jSONObject4.getString(IjkMediaPlayer.OnNativeInvokeListener.ARG_IP);
                    String string5 = jSONObject4.getString(e.N);
                    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.m3008a("no bucket found for " + str3);
                            jSONObject = jSONObject5;
                        } else {
                            Cdo doVar2 = new Cdo(str3);
                            int i3 = 0;
                            while (i3 < optJSONArray.length()) {
                                String string6 = optJSONArray.getString(i3);
                                if (!TextUtils.isEmpty(string6)) {
                                    jSONObject2 = jSONObject5;
                                    doVar2.a(new dx(string6, optJSONArray.length() - i3));
                                } else {
                                    jSONObject2 = jSONObject5;
                                }
                                i3++;
                                jSONObject5 = jSONObject2;
                            }
                            jSONObject = jSONObject5;
                            arrayList2.set(i2, doVar2);
                            doVar2.g = string5;
                            doVar2.c = string;
                            doVar2.e = string3;
                            doVar2.f = string4;
                            doVar2.d = string2;
                            if (jSONObject4.has("stat-percent")) {
                                doVar2.a(jSONObject4.getDouble("stat-percent"));
                            }
                            if (jSONObject4.has("stat-domain")) {
                                doVar2.b(jSONObject4.getString("stat-domain"));
                            }
                            if (jSONObject4.has("ttl")) {
                                doVar2.a(((long) jSONObject4.getInt("ttl")) * 1000);
                            }
                            m3174a(doVar2.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.m3008a("no bucket found for " + next);
                            } else {
                                Cdo doVar3 = new Cdo(next);
                                doVar3.a(j);
                                for (int i4 = 0; i4 < optJSONArray2.length(); i4++) {
                                    String string7 = optJSONArray2.getString(i4);
                                    if (!TextUtils.isEmpty(string7)) {
                                        doVar3.a(new dx(string7, optJSONArray2.length() - i4));
                                    }
                                }
                                synchronized (b) {
                                    if (this.f826a.a(next)) {
                                        b.put(next, doVar3);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        } catch (Exception e) {
            com.xiaomi.channel.commonutils.logger.b.m3008a("failed to get bucket " + e.getMessage());
        }
        for (int i5 = 0; i5 < arrayList.size(); i5++) {
            Cdo doVar4 = arrayList2.get(i5);
            if (doVar4 != null) {
                a(arrayList.get(i5), doVar4);
            }
        }
        m3178c();
        return arrayList2;
    }

    public static synchronized void a(Context context, dr drVar, b bVar, String str, String str2, String str3) {
        synchronized (ds.class) {
            f6667a = context.getApplicationContext();
            if (f6667a == null) {
                f6667a = context;
            }
            if (f823a == null) {
                if (f822a == null) {
                    f823a = new ds(context, drVar, bVar, str, str2, str3);
                } else {
                    f823a = f822a.a(context, drVar, bVar, str);
                }
            }
        }
    }

    public static synchronized void a(a aVar) {
        synchronized (ds.class) {
            f822a = aVar;
            f823a = null;
        }
    }

    public static void a(String str, String str2) {
        Cdo doVar = b.get(str);
        synchronized (b) {
            if (doVar == null) {
                Cdo doVar2 = new Cdo(str);
                doVar2.a(604800000L);
                doVar2.m3163a(str2);
                b.put(str, doVar2);
            } else {
                doVar.m3163a(str2);
            }
        }
    }

    private String f() {
        try {
            PackageInfo packageInfo = f6667a.getPackageManager().getPackageInfo(f6667a.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 Cdo m3171a(String str) {
        if (!TextUtils.isEmpty(str)) {
            return a(new URL(str).getHost(), true);
        }
        throw new IllegalArgumentException("the url is empty");
    }

    public Cdo a(String str, boolean z) {
        Cdo d2;
        if (TextUtils.isEmpty(str)) {
            throw new IllegalArgumentException("the host is empty");
        } else if (!this.f826a.a(str)) {
            return null;
        } else {
            Cdo c2 = c(str);
            return (c2 == null || !c2.b()) ? (!z || !as.b(f6667a) || (d2 = d(str)) == null) ? new du(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<ar> arrayList3 = new ArrayList();
        arrayList3.add(new ap("type", str));
        if (str.equals("wap")) {
            arrayList3.add(new ap("conpt", a(as.m3069a(f6667a))));
        }
        if (z) {
            arrayList3.add(new ap("reserved", "1"));
        }
        arrayList3.add(new ap("uuid", str2));
        arrayList3.add(new ap("list", ay.a(arrayList, Constants.ACCEPT_TIME_SEPARATOR_SP)));
        arrayList3.add(new ap("countrycode", com.xiaomi.push.service.a.a(f6667a).b()));
        Cdo 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) {
                Cdo doVar = b.get("resolver.msg.xiaomi.net");
                if (doVar != null) {
                    Iterator<String> it2 = doVar.a(true).iterator();
                    while (it2.hasNext()) {
                        arrayList2.add(String.format(Locale.US, "http://%1$s/gslb/?ver=4.0", it2.next()));
                    }
                }
            }
        } else {
            arrayList2 = c2.a(format);
        }
        Iterator<String> it3 = arrayList2.iterator();
        IOException e = null;
        while (it3.hasNext()) {
            Uri.Builder buildUpon = Uri.parse(it3.next()).buildUpon();
            for (ar arVar : arrayList3) {
                buildUpon.appendQueryParameter(arVar.a(), arVar.b());
            }
            try {
                return this.f827a == null ? as.a(f6667a, new URL(buildUpon.toString())) : this.f827a.a(buildUpon.toString());
            } catch (IOException e2) {
                e = e2;
            }
        }
        if (e == null) {
            return null;
        }
        com.xiaomi.channel.commonutils.logger.b.m3008a("network exception: " + e.getMessage());
        throw e;
    }

    /* access modifiers changed from: protected */
    /* renamed from: a  reason: collision with other method in class */
    public JSONObject m3172a() {
        JSONObject jSONObject;
        synchronized (this.f829a) {
            jSONObject = new JSONObject();
            jSONObject.put("ver", 2);
            JSONArray jSONArray = new JSONArray();
            for (dp dpVar : this.f829a.values()) {
                jSONArray.put(dpVar.m3167a());
            }
            jSONObject.put("data", jSONArray);
            JSONArray jSONArray2 = new JSONArray();
            for (Cdo doVar : b.values()) {
                jSONArray2.put(doVar.m3162a());
            }
            jSONObject.put("reserved", jSONArray2);
        }
        return jSONObject;
    }

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

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

    public void a(String str, Cdo doVar) {
        if (TextUtils.isEmpty(str) || doVar == null) {
            throw new IllegalArgumentException("the argument is invalid " + str + ", " + doVar);
        } else if (this.f826a.a(str)) {
            synchronized (this.f829a) {
                m3175a();
                if (this.f829a.containsKey(str)) {
                    this.f829a.get(str).a(doVar);
                } else {
                    dp dpVar = new dp(str);
                    dpVar.a(doVar);
                    this.f829a.put(str, dpVar);
                }
            }
        }
    }

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

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

    /* access modifiers changed from: protected */
    public String b() {
        String a2 = com.xiaomi.push.service.a.a(f6667a).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 m3176b() {
        ArrayList<String> arrayList;
        synchronized (this.f829a) {
            m3175a();
            arrayList = new ArrayList<>(this.f829a.keySet());
            for (int size = arrayList.size() - 1; size >= 0; size--) {
                dp dpVar = this.f829a.get(arrayList.get(size));
                if (!(dpVar == null || dpVar.a() == null)) {
                    arrayList.remove(size);
                }
            }
        }
        ArrayList<Cdo> 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 m3177b(String str) {
        synchronized (this.f829a) {
            this.f829a.clear();
            JSONObject jSONObject = new JSONObject(str);
            if (jSONObject.optInt("ver") == 2) {
                JSONArray optJSONArray = jSONObject.optJSONArray("data");
                for (int i = 0; i < optJSONArray.length(); i++) {
                    dp a2 = new dp().a(optJSONArray.getJSONObject(i));
                    this.f829a.put(a2.m3165a(), a2);
                }
                JSONArray optJSONArray2 = jSONObject.optJSONArray("reserved");
                for (int i2 = 0; i2 < optJSONArray2.length(); i2++) {
                    Cdo a3 = new Cdo("").a(optJSONArray2.getJSONObject(i2));
                    b.put(a3.f819b, a3);
                }
            } else {
                throw new JSONException("Bad version");
            }
        }
    }

    /* access modifiers changed from: protected */
    public Cdo c(String str) {
        dp dpVar;
        Cdo a2;
        synchronized (this.f829a) {
            m3175a();
            dpVar = this.f829a.get(str);
        }
        if (dpVar == null || (a2 = dpVar.a()) == null) {
            return null;
        }
        return a2;
    }

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

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

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

    /* access modifiers changed from: protected */
    public String d() {
        BufferedReader bufferedReader;
        Throwable th;
        try {
            File file = new File(f6667a.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.m3008a("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.m3008a("load host exception " + th.getMessage());
            return null;
        }
    }

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

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