package com.lantern.conn.sdk.manager; import android.content.Context; import android.net.wifi.WifiInfo; import android.os.Handler; import android.os.Looper; import android.os.Message; import com.appara.feed.model.FeedItem; import com.huawei.hms.support.api.entity.core.JosStatusCodes; import com.lantern.conn.sdk.WkApplication; import com.lantern.conn.sdk.core.common.BLCallback; import com.lantern.conn.sdk.core.common.BLLog; import com.lantern.conn.sdk.core.common.f; import com.lantern.conn.sdk.core.model.WkAccessPoint; import java.io.IOException; import java.net.HttpURLConnection; import java.net.Inet4Address; import java.net.InetAddress; import java.net.URL; import java.net.UnknownHostException; import java.util.HashMap; import java.util.Iterator; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import org.json.JSONException; import org.json.JSONObject; /* compiled from: WkNetworkMonitor */ public class e { private static e e; /* renamed from: a reason: collision with root package name */ private HashMap<WkAccessPoint, Integer> f5509a = new HashMap<>(); /* renamed from: b reason: collision with root package name */ private HashMap<WkAccessPoint, Integer> f5510b = new HashMap<>(); private boolean c = true; private boolean d = false; private ExecutorService f = Executors.newFixedThreadPool(2); public static boolean a(int i) { return i == 256; } public static boolean b(int i) { return i == 1; } /* access modifiers changed from: private */ public static String d(int i) { return i == 0 ? "offline" : i == 256 ? "auth" : i == 1 ? "online" : "unknown"; } private e() { } public static e a() { if (e == null) { e = new e(); } return e; } public int a(WkAccessPoint wkAccessPoint) { int i; synchronized (this) { i = -1; if (wkAccessPoint != null) { BLLog.d(this.f5510b.toString(), new Object[0]); Iterator<WkAccessPoint> it = this.f5510b.keySet().iterator(); while (true) { if (!it.hasNext()) { break; } WkAccessPoint next = it.next(); BLLog.d(next.toString(), new Object[0]); BLLog.d(wkAccessPoint.toString(), new Object[0]); if (next.getSecurity() == wkAccessPoint.getSecurity() && next.getSSID().equals(wkAccessPoint.getSSID())) { i = this.f5510b.get(next).intValue(); break; } } } } return i; } /* access modifiers changed from: private */ /* access modifiers changed from: public */ private void c(WkAccessPoint wkAccessPoint, int i) { synchronized (this) { this.f5510b.put(wkAccessPoint, Integer.valueOf(i)); } } public int b(WkAccessPoint wkAccessPoint) { synchronized (this) { if (!this.f5509a.containsKey(wkAccessPoint)) { return -1; } return this.f5509a.get(wkAccessPoint).intValue(); } } public void a(WkAccessPoint wkAccessPoint, int i) { synchronized (this) { this.f5509a.put(wkAccessPoint, Integer.valueOf(i)); } } public void b() { synchronized (this) { if (this.f5509a != null) { this.f5509a.clear(); } if (this.f5510b != null) { this.f5510b.clear(); } } } public boolean b(WkAccessPoint wkAccessPoint, int i) { int b2; BLLog.i("Current ap:" + wkAccessPoint + " value:" + i); if (wkAccessPoint == null || (b2 = b(wkAccessPoint)) == i) { return false; } BLLog.i("status diff:".concat(String.valueOf(b2))); a(wkAccessPoint, i); return true; } public void a(BLCallback bLCallback) { b(bLCallback); } public void b(BLCallback bLCallback) { int intValue; WkAccessPoint a2 = a(WkApplication.getAppContext()); c cVar = new c(Looper.getMainLooper(), a2, bLCallback); BLLog.i("check network threads:".concat(String.valueOf(a2))); if (!this.c || !this.f5509a.containsKey(a2) || (intValue = this.f5509a.get(a2).intValue()) != 1) { cVar.sendEmptyMessageDelayed(400, 8000); this.f.execute(new b(a2, cVar)); this.f.execute(new a(a2, cVar)); return; } BLLog.i("found cache status online"); cVar.obtainMessage(FeedItem.TEMPLATE_MAX_COUNT, intValue, 0).sendToTarget(); } private static boolean a(byte[] bArr) { if (bArr[0] != 123 && bArr[0] != 91) { return false; } try { return new JSONObject(new String(bArr)).has("SJ_DOMAIN"); } catch (JSONException e2) { BLLog.e(e2); return false; } } public int c(WkAccessPoint wkAccessPoint) { InetAddress a2 = a("c.51y5.net"); if (a2 == null) { BLLog.e("lookupHost failed c.51y5.net"); return 0; } int a3 = a(a2); if (a3 == 204) { return 1; } if (a3 < 200 || a3 > 399) { return 0; } return 256; } public int d(WkAccessPoint wkAccessPoint) { f fVar = new f("http://check02.51y5.net/cp.a?time=" + System.currentTimeMillis()); fVar.a(JosStatusCodes.RTN_CODE_COMMON_ERROR, JosStatusCodes.RTN_CODE_COMMON_ERROR); byte[] b2 = fVar.b(); if (b2 == null || b2.length == 0) { BLLog.e("network error"); return 0; } else if (b2.length == 1 && b2[0] == 48) { BLLog.i("check successfully"); return 1; } else if (!this.d || !a(b2)) { return 256; } else { return 257; } } private InetAddress a(String str) { try { InetAddress[] allByName = InetAddress.getAllByName(str); for (InetAddress inetAddress : allByName) { if (inetAddress instanceof Inet4Address) { return inetAddress; } } return null; } catch (UnknownHostException unused) { return null; } catch (Exception unused2) { return null; } } /* JADX WARNING: Code restructure failed: missing block: B:22:0x0059, code lost: if (r1 != null) goto L_0x005b; */ /* JADX WARNING: Removed duplicated region for block: B:19:0x004a A[Catch:{ IOException -> 0x004b, Exception -> 0x003a, all -> 0x0038 }] */ /* JADX WARNING: Removed duplicated region for block: B:26:0x0062 */ private int a(InetAddress inetAddress) { Throwable th; IOException e2; Exception e3; BLLog.d("http://c.51y5.net/generate_204", new Object[0]); HttpURLConnection httpURLConnection = null; try { HttpURLConnection httpURLConnection2 = (HttpURLConnection) new URL("http://c.51y5.net/generate_204").openConnection(); try { httpURLConnection2.setInstanceFollowRedirects(false); httpURLConnection2.setConnectTimeout(JosStatusCodes.RTN_CODE_COMMON_ERROR); httpURLConnection2.setReadTimeout(JosStatusCodes.RTN_CODE_COMMON_ERROR); httpURLConnection2.setUseCaches(false); httpURLConnection2.getInputStream(); int responseCode = httpURLConnection2.getResponseCode(); if (httpURLConnection2 == null) { return responseCode; } httpURLConnection2.disconnect(); return responseCode; } catch (IOException e4) { e2 = e4; httpURLConnection = httpURLConnection2; BLLog.e("Probably not a portal: IOException ".concat(String.valueOf(e2))); } catch (Exception e5) { e3 = e5; httpURLConnection = httpURLConnection2; BLLog.e("Probably not a portal: exception ".concat(String.valueOf(e3))); if (httpURLConnection != null) { } return -1; } catch (Throwable th2) { th = th2; httpURLConnection = httpURLConnection2; if (httpURLConnection != null) { } throw th; } } catch (IOException e6) { e2 = e6; BLLog.e("Probably not a portal: IOException ".concat(String.valueOf(e2))); } catch (Exception e7) { e3 = e7; BLLog.e("Probably not a portal: exception ".concat(String.valueOf(e3))); if (httpURLConnection != null) { httpURLConnection.disconnect(); } return -1; } catch (Throwable th3) { th = th3; if (httpURLConnection != null) { httpURLConnection.disconnect(); } throw th; } } public static WkAccessPoint a(Context context) { WifiInfo c2 = com.lantern.conn.sdk.core.a.b.c(context); if (c2 == null) { return null; } String ssid = c2.getSSID(); String bssid = c2.getBSSID(); if (ssid != null && ssid.length() > 2 && ssid.startsWith("\"") && ssid.endsWith("\"")) { ssid = ssid.substring(1, ssid.length() - 1); } return new WkAccessPoint(ssid, bssid); } /* access modifiers changed from: package-private */ /* compiled from: WkNetworkMonitor */ public class b implements Runnable { /* renamed from: b reason: collision with root package name */ private WkAccessPoint f5514b; private Handler c; public b(WkAccessPoint wkAccessPoint, Handler handler) { this.f5514b = wkAccessPoint; this.c = handler; } public void run() { this.c.obtainMessage(200, e.this.d(this.f5514b), 0).sendToTarget(); } } /* access modifiers changed from: package-private */ /* compiled from: WkNetworkMonitor */ public class a implements Runnable { /* renamed from: b reason: collision with root package name */ private WkAccessPoint f5512b; private Handler c; public a(WkAccessPoint wkAccessPoint, Handler handler) { this.f5512b = wkAccessPoint; this.c = handler; } public void run() { this.c.obtainMessage(200, e.this.c(this.f5512b), 1).sendToTarget(); } } /* access modifiers changed from: package-private */ /* compiled from: WkNetworkMonitor */ public class c extends Handler { /* renamed from: b reason: collision with root package name */ private WkAccessPoint f5516b; private BLCallback c; private boolean d; private int[] e = new int[2]; private boolean a() { int i = 0; for (int i2 = 0; i2 < 2; i2++) { if (this.e[i2] != -1) { i++; } } return i == 2; } private boolean b() { int i = 0; for (int i2 = 0; i2 < 2; i2++) { if (this.e[i2] != -1) { i++; } } return i >= 2; } private int c() { int i = this.e[0]; for (int i2 = 1; i2 < 2; i2++) { i = Math.max(i, this.e[1]); } if (i < 0) { return 0; } return i; } public c(Looper looper, WkAccessPoint wkAccessPoint, BLCallback bLCallback) { super(looper); this.f5516b = wkAccessPoint; this.c = bLCallback; for (int i = 0; i < 2; i++) { this.e[i] = -1; } } public void handleMessage(Message message) { WkAccessPoint wkAccessPoint; int i = message.what; int i2 = message.arg1; int i3 = message.arg2; BLLog.i("what:%d, result:%d,src:%d", Integer.valueOf(i), Integer.valueOf(i2), Integer.valueOf(i3)); if (i != 200 && i != 400 && i != 300) { return; } if (i == 300) { this.c.run(1, e.d(i2), Integer.valueOf(i2)); } else if (i3 >= 0 && i3 <= 2) { if (i == 200) { this.e[message.arg2] = message.arg1; } if (!this.d) { if (i == 400) { BLLog.i("Checking ap %s timout", this.f5516b); this.d = true; int c2 = c(); e.this.c(this.f5516b, c2); this.c.run(1, e.d(c2), Integer.valueOf(c2)); } else if (b()) { this.d = true; removeMessages(400); int c3 = c(); if (e.this.c && (wkAccessPoint = this.f5516b) != null && c3 == 1 && e.this.b(wkAccessPoint, c3)) { d.b(c3); } e.this.c(this.f5516b, c3); this.c.run(1, e.d(c3), Integer.valueOf(c3)); } else if (a()) { this.d = true; removeMessages(400); int c4 = c(); e.this.c(this.f5516b, c4); this.c.run(1, e.d(c4), Integer.valueOf(c4)); } } } } } }