package com.xiaomi.push; import android.content.Context; import android.content.pm.PackageInfo; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.net.Uri; import android.text.TextUtils; import com.alipay.sdk.m.l.c; import com.amap.api.services.district.DistrictSearchQuery; import com.huawei.hms.push.constant.RemoteMessageConst; import com.jingdong.sdk.baseinfo.BaseInfo; import com.taobao.weex.ui.component.WXBasicComponentType; import com.taobao.weex.ui.module.WXModalUIModule; import io.dcloud.common.util.ExifInterface; 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 cu { /* renamed from: a reason: collision with root package name */ protected static Context f7966a; /* renamed from: a reason: collision with other field name */ private static a f202a; /* renamed from: a reason: collision with other field name */ private static cu f203a; /* renamed from: a reason: collision with other field name */ protected static boolean f204a = false; /* renamed from: b reason: collision with root package name */ protected static final Map<String, cq> f7967b = new HashMap(); /* renamed from: c reason: collision with root package name */ private static String f7968c; /* renamed from: d reason: collision with root package name */ private static String f7969d; /* renamed from: a reason: collision with other field name */ private long f205a; /* renamed from: a reason: collision with other field name */ private ct f206a; /* renamed from: a reason: collision with other field name */ protected b f207a; /* renamed from: a reason: collision with other field name */ private String f208a; /* renamed from: a reason: collision with other field name */ protected final Map<String, cr> f209a; /* renamed from: b reason: collision with other field name */ private final long f210b; /* renamed from: b reason: collision with other field name */ private String f211b; /* renamed from: c reason: collision with other field name */ private long f212c; public interface a { cu a(Context context, ct ctVar, b bVar, String str); } public interface b { String a(String str); } protected cu(Context context, ct ctVar, b bVar, String str) { this(context, ctVar, bVar, str, null, null); } protected cu(Context context, ct ctVar, b bVar, String str, String str2, String str3) { this.f209a = new HashMap(); this.f208a = "0"; this.f205a = 0; this.f210b = 15; this.f212c = 0; this.f211b = "isp_prov_city_country_ip"; this.f207a = bVar; this.f206a = ctVar == null ? new cv(this) : ctVar; this.f208a = str; f7968c = str2 == null ? context.getPackageName() : str2; f7969d = str3 == null ? g() : str3; } public static synchronized cu a() { cu cuVar; synchronized (cu.class) { cuVar = f203a; 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 */ static String m168a() { NetworkInfo activeNetworkInfo; Context context = f7966a; 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"; } } 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<cq> a(ArrayList<String> arrayList) { JSONObject jSONObject; JSONObject jSONObject2; m179e(); synchronized (this.f209a) { m174a(); for (String str : this.f209a.keySet()) { if (!arrayList.contains(str)) { arrayList.add(str); } } } Map<String, cq> map = f7967b; synchronized (map) { for (Object obj : map.values().toArray()) { cq cqVar = (cq) obj; if (!cqVar.b()) { f7967b.remove(cqVar.f199b); } } } 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(f7966a) ? BaseInfo.NETWORK_TYPE_WIFI : "wap"; String a2 = a(arrayList, str2, this.f208a, true); if (!TextUtils.isEmpty(a2)) { JSONObject jSONObject3 = new JSONObject(a2); com.xiaomi.channel.commonutils.logger.b.b(a2); if (WXModalUIModule.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(DistrictSearchQuery.KEYWORDS_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.m12a("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.f7963g = string5; cqVar2.f7959c = string; cqVar2.f7961e = string3; cqVar2.f7962f = string4; cqVar2.f7960d = 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); } m173a(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.m12a("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 = f7967b; synchronized (map2) { if (this.f206a.a(next)) { map2.put(next, cqVar3); } } } } } } } } catch (Exception e2) { com.xiaomi.channel.commonutils.logger.b.m12a("failed to get bucket " + e2.getMessage()); } for (int i5 = 0; i5 < arrayList.size(); i5++) { cq cqVar4 = arrayList2.get(i5); if (cqVar4 != null) { a(arrayList.get(i5), cqVar4); } } m177c(); return arrayList2; } public static synchronized void a(Context context, ct ctVar, b bVar, String str, String str2, String str3) { synchronized (cu.class) { Context applicationContext = context.getApplicationContext(); f7966a = applicationContext; if (applicationContext == null) { f7966a = context; } if (f203a == null) { a aVar = f202a; if (aVar == null) { f203a = new cu(context, ctVar, bVar, str, str2, str3); } else { f203a = aVar.a(context, ctVar, bVar, str); } } } } public static synchronized void a(a aVar) { synchronized (cu.class) { f202a = aVar; f203a = null; } } public static void a(String str, String str2) { Map<String, cq> map = f7967b; cq cqVar = map.get(str); synchronized (map) { if (cqVar == null) { cq cqVar2 = new cq(str); cqVar2.a(604800000L); cqVar2.m161a(str2); map.put(str, cqVar2); } else { cqVar.m161a(str2); } } } /* renamed from: a reason: collision with other method in class */ private byte[] m169a() { return bn.m122a(f7966a.getPackageName() + "_key_salt"); } private String f() { return "host_fallbacks"; } private String g() { try { PackageInfo packageInfo = f7966a.getPackageManager().getPackageInfo(f7966a.getPackageName(), 16384); return packageInfo != null ? packageInfo.versionName : "0"; } catch (Exception unused) { return "0"; } } /* renamed from: a reason: collision with other method in class */ public cq m170a(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 e2; if (TextUtils.isEmpty(str)) { throw new IllegalArgumentException("the host is empty"); } else if (!this.f206a.a(str)) { return null; } else { cq c2 = c(str); return (c2 == null || !c2.b()) ? (!z || !bi.b(f7966a) || (e2 = e(str)) == null) ? new cw(this, str, c2) : e2 : c2; } } /* access modifiers changed from: protected */ 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.m113a(f7966a)))); } if (z) { arrayList3.add(new bf("reserved", "1")); } arrayList3.add(new bf("uuid", str2)); arrayList3.add(new bf(WXBasicComponentType.LIST, bo.a(arrayList, ","))); arrayList3.add(new bf("countrycode", com.xiaomi.push.service.a.a(f7966a).b())); arrayList3.add(new bf("push_sdk_vc", String.valueOf((int) BuildConfig.VERSION_CODE))); String b2 = b(); cq c2 = c(b2); String format = String.format(Locale.US, "https://%1$s/gslb/?ver=5.0", b2); if (c2 == null) { arrayList2.add(format); Map<String, cq> map = f7967b; synchronized (map) { cq cqVar = map.get(b2); if (cqVar != null) { Iterator<String> it = cqVar.a(true).iterator(); while (it.hasNext()) { arrayList2.add(String.format(Locale.US, "https://%1$s/gslb/?ver=5.0", it.next())); } } } } else { arrayList2 = c2.a(format); } Iterator<String> it2 = arrayList2.iterator(); IOException e2 = null; while (it2.hasNext()) { Uri.Builder buildUpon = Uri.parse(it2.next()).buildUpon(); for (bh bhVar : arrayList3) { buildUpon.appendQueryParameter(bhVar.a(), bhVar.b()); } try { b bVar = this.f207a; return bVar == null ? bi.a(f7966a, new URL(buildUpon.toString())) : bVar.a(buildUpon.toString()); } catch (IOException e3) { e2 = e3; } } if (e2 == null) { return null; } com.xiaomi.channel.commonutils.logger.b.m12a("network exception: " + e2.getMessage()); throw e2; } /* access modifiers changed from: protected */ /* renamed from: a reason: collision with other method in class */ public JSONObject m171a() { JSONObject jSONObject; synchronized (this.f209a) { jSONObject = new JSONObject(); jSONObject.put("ver", 2); JSONArray jSONArray = new JSONArray(); for (cr crVar : this.f209a.values()) { jSONArray.put(crVar.m165a()); } jSONObject.put("data", jSONArray); JSONArray jSONArray2 = new JSONArray(); for (cq cqVar : f7967b.values()) { jSONArray2.put(cqVar.m160a()); } jSONObject.put("reserved", jSONArray2); } return jSONObject; } /* renamed from: a reason: collision with other method in class */ public void m172a() { synchronized (this.f209a) { this.f209a.clear(); } } /* renamed from: a reason: collision with other method in class */ public void m173a(String str) { this.f211b = 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.f206a.a(str)) { synchronized (this.f209a) { m174a(); if (this.f209a.containsKey(str)) { this.f209a.get(str).a(cqVar); } else { cr crVar = new cr(str); crVar.a(cqVar); this.f209a.put(str, crVar); } } } } /* access modifiers changed from: protected */ /* renamed from: a reason: collision with other method in class */ public boolean m174a() { synchronized (this.f209a) { if (f204a) { return true; } f204a = true; this.f209a.clear(); try { String d2 = d(); if (!TextUtils.isEmpty(d2)) { m176b(d2); com.xiaomi.channel.commonutils.logger.b.b("loading the new hosts succeed"); return true; } } catch (Throwable th) { com.xiaomi.channel.commonutils.logger.b.m12a("load bucket failure: " + th.getMessage()); } return false; } } public cq b(String str) { return a(str, true); } /* access modifiers changed from: protected */ public String b() { return "resolver.msg.xiaomi.net"; } /* renamed from: b reason: collision with other method in class */ public void m175b() { ArrayList<String> arrayList; synchronized (this.f209a) { m174a(); arrayList = new ArrayList<>(this.f209a.keySet()); for (int size = arrayList.size() - 1; size >= 0; size--) { cr crVar = this.f209a.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)); } } } /* access modifiers changed from: protected */ /* renamed from: b reason: collision with other method in class */ public void m176b(String str) { synchronized (this.f209a) { this.f209a.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 a2 = new cr().a(optJSONArray.getJSONObject(i)); this.f209a.put(a2.m163a(), a2); } } 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.f2245f); if (!TextUtils.isEmpty(optString)) { try { cq a3 = new cq(optString).a(jSONObject2); f7967b.put(a3.f199b, a3); com.xiaomi.channel.commonutils.logger.b.m12a("load local reserved host for " + a3.f199b); } catch (JSONException unused) { com.xiaomi.channel.commonutils.logger.b.m12a("parse reserved host fail."); } } } } } else { throw new JSONException("Bad version"); } } } /* access modifiers changed from: protected */ public cq c(String str) { cr crVar; cq a2; synchronized (this.f209a) { m174a(); crVar = this.f209a.get(str); } if (crVar == null || (a2 = crVar.a()) == null) { return null; } return a2; } public String c() { StringBuilder sb = new StringBuilder(); synchronized (this.f209a) { for (Map.Entry<String, cr> entry : this.f209a.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 m177c() { BufferedOutputStream bufferedOutputStream; FileOutputStream fileOutputStream; Throwable th; Exception e2; Exception e3; Throwable th2; synchronized (this.f209a) { BufferedOutputStream bufferedOutputStream2 = null; try { String jSONObject = m171a().toString(); com.xiaomi.channel.commonutils.logger.b.b("persist host fallbacks = " + jSONObject); if (!TextUtils.isEmpty(jSONObject)) { fileOutputStream = f7966a.openFileOutput(f(), 0); try { bufferedOutputStream = new BufferedOutputStream(fileOutputStream); try { bufferedOutputStream.write(h.b(m169a(), jSONObject.getBytes(StandardCharsets.UTF_8))); bufferedOutputStream.flush(); bufferedOutputStream2 = bufferedOutputStream; } catch (Exception e4) { e2 = e4; try { com.xiaomi.channel.commonutils.logger.b.m12a("persist bucket failure: " + e2.getMessage()); y.a(bufferedOutputStream); y.a(fileOutputStream); } catch (Throwable th3) { th = th3; y.a(bufferedOutputStream); y.a(fileOutputStream); throw th; } } } catch (Exception e5) { e3 = e5; bufferedOutputStream = null; e2 = e3; com.xiaomi.channel.commonutils.logger.b.m12a("persist bucket failure: " + e2.getMessage()); y.a(bufferedOutputStream); y.a(fileOutputStream); } catch (Throwable th4) { th2 = th4; bufferedOutputStream = null; th = th2; y.a(bufferedOutputStream); y.a(fileOutputStream); throw th; } } else { fileOutputStream = null; } y.a(bufferedOutputStream2); } catch (Exception e6) { e3 = e6; fileOutputStream = null; bufferedOutputStream = null; e2 = e3; com.xiaomi.channel.commonutils.logger.b.m12a("persist bucket failure: " + e2.getMessage()); y.a(bufferedOutputStream); y.a(fileOutputStream); } catch (Throwable th5) { th2 = th5; fileOutputStream = null; bufferedOutputStream = null; th = th2; y.a(bufferedOutputStream); y.a(fileOutputStream); throw th; } y.a(fileOutputStream); } } public cq d(String str) { cq cqVar; Map<String, cq> map = f7967b; synchronized (map) { cqVar = map.get(str); } return cqVar; } /* access modifiers changed from: protected */ public String d() { Throwable th; InputStream inputStream; InputStream inputStream2; try { File file = new File(f7966a.getFilesDir(), f()); if (file.isFile()) { inputStream = new FileInputStream(file); try { inputStream2 = new BufferedInputStream(inputStream); } catch (Throwable th2) { th = th2; inputStream2 = null; try { com.xiaomi.channel.commonutils.logger.b.m12a("load host exception " + th.getMessage()); return null; } finally { y.a((Closeable) inputStream2); y.a((Closeable) inputStream); } } try { String str = new String(h.a(m169a(), y.a(inputStream2)), StandardCharsets.UTF_8); com.xiaomi.channel.commonutils.logger.b.b("load host fallbacks = " + str); y.a((Closeable) inputStream2); y.a((Closeable) inputStream); return str; } catch (Throwable th3) { th = th3; com.xiaomi.channel.commonutils.logger.b.m12a("load host exception " + th.getMessage()); return null; } } else { y.a((Closeable) null); y.a((Closeable) null); return null; } } catch (Throwable th4) { th = th4; inputStream2 = null; inputStream = inputStream2; com.xiaomi.channel.commonutils.logger.b.m12a("load host exception " + th.getMessage()); return null; } } /* renamed from: d reason: collision with other method in class */ public void m178d() { String e2 = e(); try { File file = new File(f7966a.getFilesDir(), e2); if (file.exists()) { boolean delete = file.delete(); StringBuilder sb = new StringBuilder(); sb.append("Delete old host fallbacks file "); sb.append(e2); sb.append(delete ? " successful." : " failed."); com.xiaomi.channel.commonutils.logger.b.m12a(sb.toString()); return; } com.xiaomi.channel.commonutils.logger.b.b("Old host fallbacks file " + e2 + " does not exist."); } catch (Exception e3) { com.xiaomi.channel.commonutils.logger.b.m12a("Delete old host fallbacks file " + e2 + " error: " + e3.getMessage()); } } /* access modifiers changed from: protected */ public cq e(String str) { if (System.currentTimeMillis() - this.f212c <= this.f205a * 60 * 1000) { return null; } this.f212c = System.currentTimeMillis(); ArrayList<String> arrayList = new ArrayList<>(); arrayList.add(str); cq cqVar = a(arrayList).get(0); if (cqVar != null) { this.f205a = 0; return cqVar; } long j = this.f205a; if (j >= 15) { return null; } this.f205a = j + 1; return null; } /* access modifiers changed from: protected */ public String e() { if ("com.xiaomi.xmsf".equals(f7968c)) { return f7968c; } return f7968c + ":pushservice"; } /* renamed from: e reason: collision with other method in class */ public void m179e() { synchronized (this.f209a) { for (cr crVar : this.f209a.values()) { crVar.a(true); } while (true) { for (boolean z = false; !z; z = true) { for (String str : this.f209a.keySet()) { if (this.f209a.get(str).m164a().isEmpty()) { this.f209a.remove(str); } } } } } } }