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.os.Process; import android.text.TextUtils; import androidx.exifinterface.media.ExifInterface; import com.alipay.sdk.cons.c; import com.amap.api.services.district.DistrictSearchQuery; import com.baidu.mobads.interfaces.utils.IXAdSystemUtils; import com.huawei.hms.push.constant.RemoteMessageConst; import com.umeng.analytics.pro.bz; 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 cu { /* renamed from: a reason: collision with root package name */ public static Context f25006a; /* renamed from: a reason: collision with other field name */ public static a f236a; /* renamed from: a reason: collision with other field name */ public static cu f237a; /* renamed from: a reason: collision with other field name */ public static boolean f238a = false; /* renamed from: b reason: collision with root package name */ public static final Map<String, cq> f25007b = new HashMap(); /* renamed from: a reason: collision with other field name */ public long f239a; /* renamed from: a reason: collision with other field name */ public ct f240a; /* renamed from: a reason: collision with other field name */ public b f241a; /* renamed from: a reason: collision with other field name */ public String f242a; /* renamed from: a reason: collision with other field name */ public final Map<String, cr> f243a; public long c; public interface a { cu a(Context context, ct ctVar, b bVar, String str); } public interface b { String a(String str); } public cu(Context context, ct ctVar, b bVar, String str) { this(context, ctVar, bVar, str, null, null); } public cu(Context context, ct ctVar, b bVar, String str, String str2, String str3) { this.f243a = new HashMap(); this.f242a = "0"; this.f239a = 0; this.c = 0; this.f241a = bVar; this.f240a = ctVar == null ? new cv(this) : ctVar; this.f242a = str; if (str2 == null) { context.getPackageName(); } if (str3 == null) { f(); } } public static synchronized cu a() { cu cuVar; synchronized (cu.class) { cuVar = f237a; if (cuVar == null) { throw new IllegalStateException("the host manager is not initialized yet."); } } return cuVar; } /* renamed from: a reason: collision with other method in class */ public static String m184a() { NetworkInfo activeNetworkInfo; Context context = f25006a; 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 i2 = b2 & 240; if (i2 != 240) { bytes[i] = (byte) (((b2 & bz.m) ^ ((byte) (((b2 >> 4) + length) & 15))) | i2); } } return new String(bytes); } catch (UnsupportedEncodingException unused) { return str; } } public static synchronized void a(Context context, ct ctVar, b bVar, String str, String str2, String str3) { synchronized (cu.class) { Context applicationContext = context.getApplicationContext(); f25006a = applicationContext; if (applicationContext == null) { f25006a = context; } if (f237a == null) { a aVar = f236a; if (aVar == null) { f237a = new cu(context, ctVar, bVar, str, str2, str3); } else { f237a = aVar.a(context, ctVar, bVar, str); } } } } public static synchronized void a(a aVar) { synchronized (cu.class) { f236a = aVar; f237a = null; } } public static void a(String str, String str2) { Map<String, cq> map = f25007b; cq cqVar = map.get(str); synchronized (map) { if (cqVar == null) { cq cqVar2 = new cq(str); cqVar2.a(604800000L); cqVar2.m177a(str2); map.put(str, cqVar2); } else { cqVar.m177a(str2); } } } /* renamed from: a reason: collision with other method in class */ public cq m185a(String str) { if (!TextUtils.isEmpty(str)) { return a(new URL(str).getHost(), true); } throw new IllegalArgumentException("the url is empty"); } public cq a(String str, boolean z) { cq e; if (TextUtils.isEmpty(str)) { throw new IllegalArgumentException("the host is empty"); } else if (!this.f240a.a(str)) { return null; } else { cq c2 = c(str); return (c2 == null || !c2.b()) ? (!z || !bi.b(f25006a) || (e = e(str)) == null) ? new cw(this, str, c2) : e : c2; } } public String a(ArrayList<String> arrayList, String str, String str2, boolean z) { ArrayList<String> arrayList2 = new ArrayList<>(); ArrayList<bh> arrayList3 = new ArrayList(); arrayList3.add(new bf("type", str)); if (str.equals("wap")) { arrayList3.add(new bf("conpt", a(bi.m133a(f25006a)))); } if (z) { arrayList3.add(new bf("reserved", "1")); } arrayList3.add(new bf("uuid", str2)); arrayList3.add(new bf("list", bo.a(arrayList, ","))); arrayList3.add(new bf("countrycode", com.xiaomi.push.service.a.a(f25006a).b())); String b2 = b(); cq c2 = c(b2); String format = String.format(Locale.US, "https://%1$s/gslb/?ver=4.0", b2); if (c2 == null) { arrayList2.add(format); Map<String, cq> map = f25007b; synchronized (map) { cq cqVar = map.get(b2); if (cqVar != null) { Iterator<String> it2 = cqVar.a(true).iterator(); while (it2.hasNext()) { arrayList2.add(String.format(Locale.US, "https://%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 (bh bhVar : arrayList3) { buildUpon.appendQueryParameter(bhVar.a(), bhVar.b()); } try { b bVar = this.f241a; return bVar == null ? bi.a(f25006a, new URL(buildUpon.toString())) : bVar.a(buildUpon.toString()); } catch (IOException e2) { e = e2; } } if (e == null) { return null; } com.xiaomi.channel.commonutils.logger.b.m44a("network exception: " + e.getMessage()); throw e; } public final ArrayList<cq> a(ArrayList<String> arrayList) { JSONObject jSONObject; JSONObject jSONObject2; m193d(); synchronized (this.f243a) { m189a(); for (String str : this.f243a.keySet()) { if (!arrayList.contains(str)) { arrayList.add(str); } } } Map<String, cq> map = f25007b; synchronized (map) { for (Object obj : map.values().toArray()) { cq cqVar = (cq) obj; if (!cqVar.b()) { f25007b.remove(cqVar.f233b); } } } if (!arrayList.contains(b())) { arrayList.add(b()); } ArrayList<cq> arrayList2 = new ArrayList<>(arrayList.size()); for (int i = 0; i < arrayList.size(); i++) { arrayList2.add(null); } try { String str2 = bi.e(f25006a) ? IXAdSystemUtils.NT_WIFI : "wap"; String a2 = a(arrayList, str2, this.f242a, true); 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(DistrictSearchQuery.KEYWORDS_PROVINCE); String string2 = jSONObject4.getString(DistrictSearchQuery.KEYWORDS_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 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.m44a("no bucket found for " + str3); jSONObject = jSONObject5; } else { cq cqVar2 = new cq(str3); int i3 = 0; while (i3 < optJSONArray.length()) { String string6 = optJSONArray.getString(i3); if (!TextUtils.isEmpty(string6)) { jSONObject2 = jSONObject5; cqVar2.a(new cz(string6, optJSONArray.length() - i3)); } else { jSONObject2 = jSONObject5; } i3++; jSONObject5 = jSONObject2; } jSONObject = jSONObject5; arrayList2.set(i2, cqVar2); cqVar2.g = string5; cqVar2.c = string; cqVar2.e = string3; cqVar2.f = string4; cqVar2.d = string2; if (jSONObject4.has("stat-percent")) { cqVar2.a(jSONObject4.getDouble("stat-percent")); } if (jSONObject4.has("stat-domain")) { cqVar2.b(jSONObject4.getString("stat-domain")); } if (jSONObject4.has(RemoteMessageConst.TTL)) { cqVar2.a(((long) jSONObject4.getInt(RemoteMessageConst.TTL)) * 1000); } m188a(cqVar2.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.m44a("no bucket found for " + next); } else { cq cqVar3 = new cq(next); cqVar3.a(j); for (int i4 = 0; i4 < optJSONArray2.length(); i4++) { String string7 = optJSONArray2.getString(i4); if (!TextUtils.isEmpty(string7)) { cqVar3.a(new cz(string7, optJSONArray2.length() - i4)); } } Map<String, cq> map2 = f25007b; synchronized (map2) { if (this.f240a.a(next)) { map2.put(next, cqVar3); } } } } } } } } catch (Exception e) { com.xiaomi.channel.commonutils.logger.b.m44a("failed to get bucket " + e.getMessage()); } for (int i5 = 0; i5 < arrayList.size(); i5++) { cq cqVar4 = arrayList2.get(i5); if (cqVar4 != null) { a(arrayList.get(i5), cqVar4); } } m192c(); return arrayList2; } /* renamed from: a reason: collision with other method in class */ public JSONObject m186a() { JSONObject jSONObject; synchronized (this.f243a) { jSONObject = new JSONObject(); jSONObject.put("ver", 2); JSONArray jSONArray = new JSONArray(); for (cr crVar : this.f243a.values()) { jSONArray.put(crVar.m181a()); } jSONObject.put("data", jSONArray); JSONArray jSONArray2 = new JSONArray(); for (cq cqVar : f25007b.values()) { jSONArray2.put(cqVar.m176a()); } jSONObject.put("reserved", jSONArray2); } return jSONObject; } /* renamed from: a reason: collision with other method in class */ public void m187a() { synchronized (this.f243a) { this.f243a.clear(); } } /* renamed from: a reason: collision with other method in class */ public void m188a(String str) { } public void a(String str, cq cqVar) { if (TextUtils.isEmpty(str) || cqVar == null) { throw new IllegalArgumentException("the argument is invalid " + str + ", " + cqVar); } else if (this.f240a.a(str)) { synchronized (this.f243a) { m189a(); if (this.f243a.containsKey(str)) { this.f243a.get(str).a(cqVar); } else { cr crVar = new cr(str); crVar.a(cqVar); this.f243a.put(str, crVar); } } } } /* renamed from: a reason: collision with other method in class */ public boolean m189a() { synchronized (this.f243a) { if (f238a) { return true; } f238a = true; this.f243a.clear(); try { String d = d(); if (!TextUtils.isEmpty(d)) { m191b(d); com.xiaomi.channel.commonutils.logger.b.b("loading the new hosts succeed"); return true; } } catch (Throwable th) { com.xiaomi.channel.commonutils.logger.b.m44a("load bucket failure: " + th.getMessage()); } return false; } } public cq b(String str) { return a(str, true); } public String b() { String a2 = com.xiaomi.push.service.a.a(f25006a).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 m190b() { ArrayList<String> arrayList; synchronized (this.f243a) { m189a(); arrayList = new ArrayList<>(this.f243a.keySet()); for (int size = arrayList.size() - 1; size >= 0; size--) { cr crVar = this.f243a.get(arrayList.get(size)); if (!(crVar == null || crVar.a() == null)) { arrayList.remove(size); } } } ArrayList<cq> a2 = a(arrayList); for (int i = 0; i < arrayList.size(); i++) { if (a2.get(i) != null) { a(arrayList.get(i), a2.get(i)); } } } /* renamed from: b reason: collision with other method in class */ public void m191b(String str) { synchronized (this.f243a) { this.f243a.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++) { cr crVar = new cr(); crVar.a(optJSONArray.getJSONObject(i)); this.f243a.put(crVar.m179a(), crVar); } } JSONArray optJSONArray2 = jSONObject.optJSONArray("reserved"); if (optJSONArray2 != null) { for (int i2 = 0; i2 < optJSONArray2.length(); i2++) { JSONObject jSONObject2 = optJSONArray2.getJSONObject(i2); String optString = jSONObject2.optString(c.f); if (!TextUtils.isEmpty(optString)) { try { cq cqVar = new cq(optString); cqVar.a(jSONObject2); f25007b.put(cqVar.f233b, cqVar); com.xiaomi.channel.commonutils.logger.b.m44a("load local reserved host for " + cqVar.f233b); } catch (JSONException unused) { com.xiaomi.channel.commonutils.logger.b.m44a("parse reserved host fail."); } } } } } else { throw new JSONException("Bad version"); } } } public cq c(String str) { cr crVar; cq a2; synchronized (this.f243a) { m189a(); crVar = this.f243a.get(str); } if (crVar == null || (a2 = crVar.a()) == null) { return null; } return a2; } public String c() { StringBuilder sb = new StringBuilder(); synchronized (this.f243a) { for (Map.Entry<String, cr> entry : this.f243a.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 m192c() { synchronized (this.f243a) { try { BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(f25006a.openFileOutput(e(), 0))); String jSONObject = m186a().toString(); if (!TextUtils.isEmpty(jSONObject)) { bufferedWriter.write(jSONObject); } bufferedWriter.close(); } catch (Exception e) { com.xiaomi.channel.commonutils.logger.b.m44a("persist bucket failure: " + e.getMessage()); } } } public cq d(String str) { cq cqVar; Map<String, cq> map = f25007b; synchronized (map) { cqVar = map.get(str); } return cqVar; } public String d() { BufferedReader bufferedReader; Throwable th; try { File file = new File(f25006a.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(); aa.a(bufferedReader); return sb2; } } } catch (Throwable th2) { th = th2; try { com.xiaomi.channel.commonutils.logger.b.m44a("load host exception " + th.getMessage()); return null; } finally { aa.a(bufferedReader); } } } else { aa.a((Closeable) null); return null; } } catch (Throwable th3) { th = th3; bufferedReader = null; com.xiaomi.channel.commonutils.logger.b.m44a("load host exception " + th.getMessage()); return null; } } /* renamed from: d reason: collision with other method in class */ public void m193d() { synchronized (this.f243a) { for (cr crVar : this.f243a.values()) { crVar.a(true); } while (true) { for (boolean z = false; !z; z = true) { for (String str : this.f243a.keySet()) { if (this.f243a.get(str).m180a().isEmpty()) { this.f243a.remove(str); } } } } } } public cq e(String str) { if (System.currentTimeMillis() - this.c <= this.f239a * 60 * 1000) { return null; } this.c = System.currentTimeMillis(); ArrayList<String> arrayList = new ArrayList<>(); arrayList.add(str); cq cqVar = a(arrayList).get(0); if (cqVar != null) { this.f239a = 0; return cqVar; } long j = this.f239a; if (j >= 15) { return null; } this.f239a = j + 1; return null; } public String e() { List<ActivityManager.RunningAppProcessInfo> runningAppProcesses = ((ActivityManager) f25006a.getSystemService("activity")).getRunningAppProcesses(); if (runningAppProcesses == null) { return "com.xiaomi"; } for (ActivityManager.RunningAppProcessInfo runningAppProcessInfo : runningAppProcesses) { if (runningAppProcessInfo.pid == Process.myPid()) { return runningAppProcessInfo.processName; } } return "com.xiaomi"; } public final String f() { try { PackageInfo packageInfo = f25006a.getPackageManager().getPackageInfo(f25006a.getPackageName(), 16384); return packageInfo != null ? packageInfo.versionName : "0"; } catch (Exception unused) { return "0"; } } }