Fing.apk(点击下载) / i.java


package com.overlook.android.fing.ui.mobiletools.speedtest;

import android.content.Context;
import android.net.wifi.WifiManager;
import android.os.PowerManager;
import android.text.TextUtils;
import android.util.Log;
import com.huawei.hms.support.api.entity.pay.HwPayConstant;
import com.overlook.android.fing.engine.e.h;
import com.overlook.android.fing.engine.e.l;
import com.overlook.android.fing.engine.j.a.e.t.b;
import com.overlook.android.fing.engine.j.a.e.t.c;
import com.overlook.android.fing.engine.j.d.u;
import com.overlook.android.fing.engine.j.d.x;
import com.overlook.android.fing.engine.j.g.d;
import com.overlook.android.fing.engine.j.j.b;
import com.overlook.android.fing.engine.l.q;
import com.overlook.android.fing.engine.l.t;
import com.overlook.android.fing.engine.l.v;
import com.overlook.android.fing.engine.model.internet.CarrierSubject;
import com.overlook.android.fing.engine.model.internet.IspInfo;
import com.overlook.android.fing.engine.model.internet.IspQuery;
import com.overlook.android.fing.engine.model.internet.IspSubject;
import com.overlook.android.fing.engine.model.internet.RatingSubject;
import com.overlook.android.fing.engine.model.internet.UserRating;
import com.overlook.android.fing.engine.model.net.CarrierInfo;
import com.overlook.android.fing.engine.model.net.GeoIpInfo;
import com.overlook.android.fing.engine.model.net.Ip4Address;
import com.overlook.android.fing.engine.model.net.IpAddress;
import com.overlook.android.fing.engine.model.net.NicInfo;
import com.overlook.android.fing.engine.model.net.p;
import com.overlook.android.fing.engine.model.speedtest.InternetSpeedInfo;
import com.overlook.android.fing.engine.model.speedtest.InternetSpeedServer;
import com.overlook.android.fing.engine.model.speedtest.InternetSpeedTestDevice;
import com.overlook.android.fing.engine.model.speedtest.InternetSpeedTestRecord;
import com.overlook.android.fing.engine.services.netbox.j0;
import com.overlook.android.fing.engine.services.netbox.l0;
import com.overlook.android.fing.engine.services.netbox.m0;
import com.overlook.android.fing.engine.services.wifi.WiFiChannel;
import com.overlook.android.fing.engine.services.wifi.WiFiConnectionInfo;
import com.overlook.android.fing.engine.services.wifi.WiFiSignal;
import com.overlook.android.fing.speedtest.NdtClient;
import com.overlook.android.fing.speedtest.NdtError;
import com.overlook.android.fing.speedtest.NdtState;
import com.overlook.android.fing.speedtest.NdtType;
import com.overlook.android.fing.speedtest.SpeedTestClient;
import com.overlook.android.fing.ui.service.FingAppService;
import com.overlook.android.fing.ui.speedtest.SpeedtestActivity;
import com.overlook.android.fing.ui.speedtest.j;
import com.overlook.android.fing.ui.speedtest.k;
import com.overlook.android.fing.ui.speedtest.m;
import e.f.a.a.a.r6;
import e.f.a.a.a.s6;
import h.c0;
import h.e0;
import h.i0;
import h.k0;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Random;
import java.util.UUID;
import okhttp3.internal.connection.e;
import org.json.JSONArray;
import org.json.JSONObject;
import org.snmp4j.smi.GenericAddress;

/* compiled from: MobileSpeedTester */
public class i implements b, h.b {
    private static final DecimalFormat F = new DecimalFormat("#.##");
    private boolean A;
    private List<Double> B;
    private List<Double> C;
    private List<String> D;
    private NicInfo E;

    /* renamed from: a  reason: collision with root package name */
    private final Object f17213a = new Object();
    private final FingAppService b;

    /* renamed from: c  reason: collision with root package name */
    private SpeedTestClient f17214c;

    /* renamed from: d  reason: collision with root package name */
    private j0 f17215d;

    /* renamed from: e  reason: collision with root package name */
    private g f17216e;

    /* renamed from: f  reason: collision with root package name */
    private c f17217f;

    /* renamed from: g  reason: collision with root package name */
    private InternetSpeedServer f17218g;

    /* renamed from: h  reason: collision with root package name */
    private s6 f17219h;

    /* renamed from: i  reason: collision with root package name */
    private com.overlook.android.fing.engine.k.c f17220i;
    private com.overlook.android.fing.engine.k.c j;
    private Random k;
    private String l;
    private Thread m;
    private a n;
    private Context o;
    private double p;
    private double q;
    private double t;
    private double u;
    private double v;
    private double w;
    private double x;
    private double y;
    private boolean z;

    /* compiled from: MobileSpeedTester */
    public static class a {

        /* renamed from: a  reason: collision with root package name */
        private Map<String, Long> f17221a = new HashMap();

        public void a(String str, long j) {
            this.f17221a.put(str, Long.valueOf(j));
        }

        public Map<String, Long> b() {
            return this.f17221a;
        }
    }

    public i(Context context, FingAppService fingAppService, a aVar) {
        l0 k2 = fingAppService.k();
        this.o = context;
        this.b = fingAppService;
        this.n = aVar;
        this.k = new Random();
        this.f17214c = NdtClient.getInstance();
        this.f17216e = new g();
        j0 j0Var = new j0("11.1.0");
        this.f17215d = j0Var;
        m0 m0Var = (m0) k2;
        j0Var.F(m0Var.C());
        this.f17215d.G(m0Var.v());
        this.f17215d.E(m0Var.x());
        this.f17219h = new r6(context);
        for (Map.Entry<String, Long> entry : this.n.b().entrySet()) {
            this.f17214c.setParam(entry.getKey(), entry.getValue().longValue());
        }
    }

    private void f(List<Double> list) {
        if (!list.isEmpty()) {
            double r = r(list);
            list.add(Double.valueOf((this.k.nextGaussian() * 0.005d * r) + r));
        }
    }

    private InternetSpeedInfo h() {
        InternetSpeedServer internetSpeedServer = new InternetSpeedServer(this.f17218g);
        InternetSpeedServer internetSpeedServer2 = new InternetSpeedServer(this.f17218g);
        internetSpeedServer.g(this.u);
        internetSpeedServer2.g(this.v);
        long currentTimeMillis = System.currentTimeMillis();
        double d2 = -1.0d;
        double d3 = this.z ? this.w : -1.0d;
        if (this.A) {
            d2 = this.x;
        }
        return new InternetSpeedInfo(currentTimeMillis, d3, d2, this.y, internetSpeedServer, internetSpeedServer2, this.D, this.E);
    }

    private NicInfo i(WiFiConnectionInfo wiFiConnectionInfo, CarrierInfo carrierInfo) {
        boolean z2 = false;
        boolean z3 = wiFiConnectionInfo != null;
        if (carrierInfo != null) {
            z2 = true;
        }
        NicInfo.b bVar = new NicInfo.b();
        bVar.f15719a = v.a();
        bVar.b = z3 ? NicInfo.e.TYPE_WIFI : z2 ? NicInfo.e.TYPE_CELLULAR : NicInfo.e.TYPE_UNKNOWN;
        bVar.f15720c = (z3 || z2) ? NicInfo.d.STATE_ACTIVE : NicInfo.d.STATE_INACTIVE;
        bVar.f15721d = null;
        if (z3) {
            bVar.f15722e = (long) wiFiConnectionInfo.i();
            bVar.f15723f = (long) wiFiConnectionInfo.i();
            bVar.j = wiFiConnectionInfo.d();
            bVar.f15726i = wiFiConnectionInfo.a();
            WiFiSignal c2 = wiFiConnectionInfo.c();
            if (c2 != null) {
                bVar.k = c2.c();
                WiFiChannel e2 = c2.e();
                if (e2 != null) {
                    bVar.l = e2.f();
                } else {
                    bVar.l = -1;
                }
            } else {
                bVar.k = -1;
                bVar.l = -1;
            }
        }
        bVar.m = carrierInfo;
        return bVar.a();
    }

    private RatingSubject j(GeoIpInfo geoIpInfo, WiFiConnectionInfo wiFiConnectionInfo, CarrierInfo carrierInfo) {
        if (wiFiConnectionInfo != null && geoIpInfo != null) {
            IspSubject ispSubject = new IspSubject(geoIpInfo.u(), geoIpInfo.y());
            if (geoIpInfo.B() != null) {
                ispSubject.h(geoIpInfo.B());
            }
            if (geoIpInfo.x() != null) {
                ispSubject.f(geoIpInfo.x());
            }
            RatingSubject ratingSubject = new RatingSubject();
            ratingSubject.e(ispSubject);
            return ratingSubject;
        } else if (carrierInfo == null || geoIpInfo == null) {
            return null;
        } else {
            CarrierSubject carrierSubject = new CarrierSubject();
            carrierSubject.d(geoIpInfo.u());
            carrierSubject.c(geoIpInfo.y());
            RatingSubject ratingSubject2 = new RatingSubject();
            ratingSubject2.d(carrierSubject);
            return ratingSubject2;
        }
    }

    /* JADX WARNING: Code restructure failed: missing block: B:21:0x007a, code lost:
        r2 = move-exception;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:22:0x007b, code lost:
        if (r0 != null) goto L_0x007d;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:24:?, code lost:
        r0.close();
     */
    /* JADX WARNING: Code restructure failed: missing block: B:25:0x0081, code lost:
        r0 = move-exception;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:26:0x0082, code lost:
        r1.addSuppressed(r0);
     */
    /* JADX WARNING: Code restructure failed: missing block: B:27:0x0085, code lost:
        throw r2;
     */
    private InternetSpeedServer k() {
        try {
            c0 c0Var = new c0(com.overlook.android.fing.engine.j.h.b.a());
            e0.a aVar = new e0.a();
            aVar.h("https://mlab-ns.appspot.com/ndt");
            i0 k2 = ((e) c0Var.A(aVar.b())).k();
            if (k2.x()) {
                k0 b2 = k2.b();
                if (b2 != null) {
                    JSONObject jSONObject = new JSONObject(b2.i());
                    String string = jSONObject.getString("fqdn");
                    String string2 = jSONObject.getString("city");
                    String string3 = jSONObject.getString(HwPayConstant.KEY_COUNTRY);
                    JSONArray jSONArray = jSONObject.getJSONArray(GenericAddress.TYPE_IP);
                    for (int i2 = 0; i2 < jSONArray.length(); i2++) {
                        Ip4Address A2 = Ip4Address.A(jSONArray.getString(i2));
                        if (A2 != null) {
                            InternetSpeedServer internetSpeedServer = new InternetSpeedServer(string2, string3, string, A2);
                            b2.close();
                            return internetSpeedServer;
                        }
                    }
                    b2.close();
                    return null;
                }
                throw new IOException("HTTP response body is empty!");
            }
            throw new IOException("HTTP response invalid (code=" + k2.i() + ",message=" + k2.y() + ")");
        } catch (Exception e2) {
            Log.e("fing:ist-mobile-engine", "Could not fetch test server from MLab", e2);
            return null;
        }
    }

    private void l(t tVar) {
        c cVar;
        g gVar;
        synchronized (this.f17213a) {
            cVar = this.f17217f;
            gVar = new g(this.f17216e);
        }
        if (cVar != null) {
            SpeedtestActivity speedtestActivity = (SpeedtestActivity) this.f17217f;
            speedtestActivity.runOnUiThread(new m(speedtestActivity, tVar, gVar));
        }
    }

    private void m(t tVar) {
        c cVar;
        synchronized (this.f17213a) {
            cVar = this.f17217f;
            b.EnumC0144b bVar = this.f17216e.f14931a;
        }
        if (cVar != null) {
            SpeedtestActivity speedtestActivity = (SpeedtestActivity) cVar;
            speedtestActivity.runOnUiThread(new j(speedtestActivity, tVar));
        }
    }

    private void n() {
        c cVar;
        g gVar;
        synchronized (this.f17213a) {
            cVar = this.f17217f;
            gVar = new g(this.f17216e);
        }
        if (cVar != null) {
            SpeedtestActivity speedtestActivity = (SpeedtestActivity) this.f17217f;
            speedtestActivity.runOnUiThread(new k(speedtestActivity, gVar));
        }
    }

    private void o(h hVar) {
        HashMap hashMap = new HashMap();
        hashMap.put("Error", hVar.name());
        e.f.a.a.b.j.j.x("Mobile_Speedtest_Failure", hashMap);
        synchronized (this.f17213a) {
            g gVar = this.f17216e;
            gVar.f14931a = b.EnumC0144b.READY;
            gVar.b = 100;
            gVar.o = hVar;
            gVar.f14936g = System.currentTimeMillis();
        }
    }

    private boolean p() {
        boolean z2;
        synchronized (this.f17213a) {
            z2 = this.f17216e.f14931a == b.EnumC0144b.RUNNING;
        }
        return z2;
    }

    /* JADX WARNING: Removed duplicated region for block: B:21:0x006a  */
    /* JADX WARNING: Removed duplicated region for block: B:44:0x00e1  */
    public static void q(i iVar) {
        WiFiConnectionInfo wiFiConnectionInfo;
        CarrierInfo carrierInfo;
        IspInfo ispInfo;
        boolean z2 = true;
        PowerManager.WakeLock b2 = q.b(iVar.o, 1);
        WifiManager.WifiLock d2 = q.d(iVar.o, 1);
        boolean z3 = false;
        try {
            synchronized (iVar.f17213a) {
                if (iVar.f17216e.f14931a != b.EnumC0144b.RUNNING) {
                    throw new MobileSpeedTestException(h.MSE_UNKNOWN_ERROR);
                }
            }
            d dVar = new d("fingdroid/11.1.0");
            dVar.f();
            h b3 = iVar.b.b();
            GeoIpInfo geoIpInfo = null;
            if (b3.t()) {
                wiFiConnectionInfo = b3.n();
                if (wiFiConnectionInfo == null || TextUtils.isEmpty(wiFiConnectionInfo.d()) || wiFiConnectionInfo.a() == null || wiFiConnectionInfo.a().m()) {
                    Log.v("fing:ist-mobile-engine", "<unknown ssid> or 02:00:00:00:00:00 bssid detected: asking user to enable service");
                    t tVar = new t();
                    iVar.m(tVar);
                    while (iVar.p() && !tVar.d(500)) {
                        while (iVar.p()) {
                            while (iVar.p()) {
                            }
                        }
                    }
                    if (tVar.a() == 0) {
                        iVar.v(200);
                        wiFiConnectionInfo = b3.n();
                    }
                }
                carrierInfo = null;
            } else if (b3.s()) {
                carrierInfo = b3.g(true);
                wiFiConnectionInfo = null;
            } else {
                carrierInfo = null;
                wiFiConnectionInfo = null;
            }
            Log.i("fing:ist-mobile-engine", "Carrier -> " + carrierInfo);
            Log.i("fing:ist-mobile-engine", "WIFI -> " + wiFiConnectionInfo);
            synchronized (iVar.f17213a) {
                g gVar = iVar.f17216e;
                gVar.p = carrierInfo;
                gVar.q = wiFiConnectionInfo;
            }
            if (wiFiConnectionInfo == null && carrierInfo == null) {
                throw new MobileSpeedTestException(h.MSE_NO_CONNECTIVITY);
            }
            if (wiFiConnectionInfo == null) {
                t tVar2 = new t();
                iVar.l(tVar2);
                while (iVar.p() && !tVar2.d(500)) {
                    while (iVar.p()) {
                        while (iVar.p()) {
                        }
                    }
                }
                if (tVar2.a() != 0) {
                    throw new MobileSpeedTestException(h.MSE_USER_TERMINATED);
                }
            }
            InternetSpeedServer k2 = iVar.k();
            iVar.f17218g = k2;
            if (k2 != null) {
                dVar.a(1500);
                if (dVar.d()) {
                    geoIpInfo = dVar.c();
                    ispInfo = iVar.s(geoIpInfo, carrierInfo != null);
                } else {
                    ispInfo = null;
                }
                synchronized (iVar.f17213a) {
                    g gVar2 = iVar.f17216e;
                    gVar2.n = geoIpInfo;
                    gVar2.m = ispInfo;
                }
                iVar.q = 0.0d;
                iVar.t = 0.0d;
                iVar.p = 0.0d;
                iVar.u = 0.0d;
                iVar.v = 0.0d;
                iVar.w = 0.0d;
                iVar.x = 0.0d;
                iVar.y = 0.0d;
                iVar.z = false;
                iVar.A = false;
                iVar.B = new ArrayList();
                iVar.C = new ArrayList();
                iVar.D = new ArrayList();
                iVar.f17220i = new com.overlook.android.fing.engine.k.c(0.1d, 0.0d);
                iVar.j = new com.overlook.android.fing.engine.k.c(0.1d, 0.0d);
                iVar.E = iVar.i(wiFiConnectionInfo, carrierInfo);
                iVar.l = UUID.randomUUID().toString();
                iVar.t(iVar.f17218g);
                iVar.v(200);
                iVar.w(NdtType.DOWN);
                iVar.v(200);
                iVar.w(NdtType.UP);
                synchronized (iVar.f17213a) {
                    g gVar3 = iVar.f17216e;
                    gVar3.f14931a = b.EnumC0144b.READY;
                    gVar3.b = 100;
                    if (gVar3.n == null && dVar.d()) {
                        geoIpInfo = dVar.c();
                        IspInfo s = iVar.s(geoIpInfo, carrierInfo != null);
                        g gVar4 = iVar.f17216e;
                        gVar4.n = geoIpInfo;
                        gVar4.m = s;
                    }
                }
                iVar.n();
                e.f.a.a.b.j.j.w("Mobile_Speedtest_Completed");
                InternetSpeedInfo h2 = iVar.h();
                InternetSpeedTestRecord u2 = iVar.u(h2, geoIpInfo);
                RatingSubject j2 = iVar.j(geoIpInfo, wiFiConnectionInfo, carrierInfo);
                if (j2 != null) {
                    InternetSpeedTestRecord c2 = ((r6) iVar.f17219h).c(j2);
                    if (c2 == null || c2.k() == null) {
                        u2.p(iVar.g(j2));
                    } else {
                        u2.p(c2.k());
                    }
                }
                WiFiConnectionInfo wiFiConnectionInfo2 = iVar.f17216e.q;
                if (!(wiFiConnectionInfo2 == null || wiFiConnectionInfo2.a() == null)) {
                    x f2 = iVar.b.f();
                    p y2 = f2.y(null, null, iVar.f17216e.q.a(), null, EnumSet.complementOf(x.v));
                    if (y2 != null) {
                        Log.v("fing:ist-mobile-engine", "Found network with BSSID " + iVar.f17216e.q.a() + ": storing speedtest result in network");
                        u o2 = f2.o(y2);
                        if (o2 != null) {
                            o2.v(h2);
                            o2.c();
                        }
                    } else {
                        Log.v("fing:ist-mobile-engine", "No network found with BSSID " + iVar.f17216e.q.a() + ": keeping speedtest result as local one");
                    }
                }
                ((r6) iVar.f17219h).f(u2);
                synchronized (iVar.f17213a) {
                    g gVar5 = iVar.f17216e;
                    gVar5.l = h2;
                    gVar5.r = u2;
                }
                iVar.n();
                q.v(b2);
                q.w(d2);
                return;
            }
            throw new MobileSpeedTestException(h.MSE_INITIALIZATION_ERROR);
        } catch (MobileSpeedTestException e2) {
            h a2 = e2.a();
            Objects.requireNonNull(a2);
            if (a2 == h.MSE_NDT_PROGRAMMING_ERROR || a2 == h.MSE_NDT_CONNECTION_ERROR || a2 == h.MSE_NDT_OS_ERROR || a2 == h.MSE_NDT_PROTOCOL_ERROR || a2 == h.MSE_NDT_SERVER_ERROR || a2 == h.MSE_NDT_SERVER_TIMEOUT || a2 == h.MSE_NDT_UNKNOWN_ERROR) {
                z3 = true;
            }
            if (z3) {
                iVar.v(1000);
                z2 = iVar.p();
            }
            if (z2) {
                Log.e("fing:ist-mobile-engine", "Exit with error: " + e2.a());
                iVar.o(e2.a());
                iVar.n();
            }
        } catch (Throwable th) {
            q.v(b2);
            q.w(d2);
            throw th;
        }
    }

    private double r(List<Double> list) {
        if (list.isEmpty()) {
            return 0.0d;
        }
        return list.get(list.size() - 1).doubleValue();
    }

    private IspInfo s(GeoIpInfo geoIpInfo, boolean z2) {
        if (geoIpInfo == null) {
            return null;
        }
        try {
            IspQuery ispQuery = new IspQuery(geoIpInfo.G(), geoIpInfo.y());
            ispQuery.l(geoIpInfo.B());
            ispQuery.k(geoIpInfo.x());
            ispQuery.i(z2);
            return this.b.K().k(ispQuery).d();
        } catch (Exception unused) {
            return null;
        }
    }

    private void t(InternetSpeedServer internetSpeedServer) throws MobileSpeedTestException {
        StringBuilder E2 = e.a.a.a.a.E("Start pinging host: '");
        E2.append(internetSpeedServer.d());
        E2.append("'");
        Log.i("fing:ist-mobile-engine", E2.toString());
        if (p()) {
            int i2 = 0;
            double d2 = 0.0d;
            if (com.overlook.android.fing.engine.j.j.b.a()) {
                int i3 = 0;
                while (true) {
                    Objects.requireNonNull(this.n);
                    if (i2 >= 10 || !p()) {
                        i2 = i3;
                    } else {
                        StringBuilder E3 = e.a.a.a.a.E("Ping #");
                        int i4 = i2 + 1;
                        E3.append(i4);
                        E3.append(" towards ");
                        E3.append(internetSpeedServer.d());
                        Log.d("fing:ist-mobile-engine", E3.toString());
                        IpAddress d3 = internetSpeedServer.d();
                        Objects.requireNonNull(this.n);
                        b.a b2 = com.overlook.android.fing.engine.j.j.b.b(d3, 3, -1);
                        if (b2.c() == b.EnumC0148b.ECHO_REPLY) {
                            d2 += b2.b();
                            i3++;
                        }
                        synchronized (this.f17213a) {
                            g gVar = this.f17216e;
                            Objects.requireNonNull(this.n);
                            gVar.f14934e = (int) ((((double) i4) / ((double) 10)) * 100.0d);
                        }
                        n();
                        Objects.requireNonNull(this.n);
                        if (i2 < 9) {
                            v(100);
                        }
                        i2 = i4;
                    }
                }
                i2 = i3;
            }
            this.y = i2 != 0 ? d2 / ((double) i2) : Double.NaN;
            this.p = 1.0d;
            synchronized (this.f17213a) {
                this.f17216e.l = h();
                this.f17216e.f14934e = (int) (this.p * 100.0d);
            }
            n();
            return;
        }
        throw new MobileSpeedTestException("Engine is not running");
    }

    private InternetSpeedTestRecord u(InternetSpeedInfo internetSpeedInfo, GeoIpInfo geoIpInfo) {
        InternetSpeedTestDevice internetSpeedTestDevice = new InternetSpeedTestDevice();
        FingAppService fingAppService = this.b;
        if (fingAppService != null) {
            com.overlook.android.fing.engine.f.b a2 = fingAppService.d().a(true);
            internetSpeedTestDevice.i(a2.b());
            internetSpeedTestDevice.h(a2.a());
            internetSpeedTestDevice.j(a2.c());
        }
        InternetSpeedTestRecord internetSpeedTestRecord = new InternetSpeedTestRecord(internetSpeedInfo.f(), internetSpeedInfo, geoIpInfo, internetSpeedTestDevice);
        try {
            FingAppService fingAppService2 = this.b;
            if (fingAppService2 != null) {
                m0 m0Var = (m0) fingAppService2.k();
                this.f17215d.F(m0Var.C());
                this.f17215d.G(m0Var.v());
                this.f17215d.E(m0Var.x());
                return this.f17215d.A(internetSpeedTestRecord);
            }
        } catch (Exception e2) {
            StringBuilder E2 = e.a.a.a.a.E("Failed to send speedtest record:");
            E2.append(e2.getMessage());
            Log.e("fing:ist-mobile-engine", E2.toString());
        }
        return internetSpeedTestRecord;
    }

    private void v(long j2) {
        if (p()) {
            try {
                Thread.sleep(j2);
            } catch (InterruptedException unused) {
            }
        }
    }

    /* JADX INFO: finally extract failed */
    /* JADX WARNING: Removed duplicated region for block: B:117:0x0268 A[LOOP:0: B:6:0x0028->B:117:0x0268, LOOP_END] */
    /* JADX WARNING: Removed duplicated region for block: B:128:0x0239 A[EDGE_INSN: B:128:0x0239->B:104:0x0239 ?: BREAK  , SYNTHETIC] */
    private void w(NdtType ndtType) throws MobileSpeedTestException {
        double d2;
        double d3;
        NdtError ndtError;
        long j2;
        double d4;
        double d5;
        if (p()) {
            try {
                NdtState start = this.f17214c.start(ndtType, this.f17218g.c(), this.l);
                if (!start.hasError()) {
                    long j3 = 75;
                    long currentTimeMillis = System.currentTimeMillis() - 75;
                    long j4 = 0;
                    long j5 = 0;
                    while (true) {
                        long j6 = currentTimeMillis + j3;
                        if (p()) {
                            long currentTimeMillis2 = j6 - System.currentTimeMillis();
                            if (currentTimeMillis2 > j4) {
                                try {
                                    Thread.sleep(currentTimeMillis2);
                                } catch (InterruptedException unused) {
                                }
                            }
                        }
                        long currentTimeMillis3 = System.currentTimeMillis();
                        NdtState poll = this.f17214c.poll();
                        boolean isFinished = poll.isFinished();
                        double progress = poll.getProgress();
                        double bps = poll.getBps();
                        double totalBps = poll.getTotalBps();
                        double bytes = poll.getBytes();
                        NdtError error = poll.getError();
                        NdtError ndtError2 = NdtError.OK;
                        boolean z2 = error != ndtError2 && progress > 0.95d;
                        Objects.requireNonNull(this.n);
                        if (!z2) {
                            ndtError = error;
                            d3 = totalBps;
                            d2 = bytes;
                        } else {
                            int ordinal = ndtType.ordinal();
                            if (ordinal == 2) {
                                double d6 = this.x;
                                double r = r(this.B);
                                totalBps = d6;
                                bytes = this.v;
                                bps = r;
                            } else if (ordinal == 4) {
                                double d7 = this.w;
                                double r2 = r(this.C);
                                bytes = this.u;
                                totalBps = d7;
                                bps = r2;
                            }
                            ndtError = ndtError2;
                            d3 = totalBps;
                            d2 = bytes;
                            progress = 1.0d;
                            isFinished = true;
                        }
                        if (ndtError == ndtError2) {
                            if (!isFinished && currentTimeMillis3 - j5 < 500) {
                                synchronized (this.f17213a) {
                                    if (this.f17216e.a()) {
                                        f(this.f17216e.f14937h);
                                        y();
                                    } else if (this.f17216e.b()) {
                                        f(this.f17216e.f14938i);
                                        y();
                                    }
                                    n();
                                }
                            } else if (p()) {
                                if (bps >= 1.0d || isFinished) {
                                    int ordinal2 = ndtType.ordinal();
                                    if (ordinal2 == 2) {
                                        d5 = d3;
                                        d4 = d2;
                                        this.q = progress;
                                        this.A = isFinished;
                                        this.x = d5;
                                        this.v = d4;
                                        List<Double> list = this.B;
                                        if (bps != 0.0d || !list.isEmpty()) {
                                            list.add(Double.valueOf(bps));
                                        }
                                        y();
                                    } else if (ordinal2 != 4) {
                                        d5 = d3;
                                        d4 = d2;
                                    } else {
                                        this.t = progress;
                                        this.z = isFinished;
                                        d5 = d3;
                                        this.w = d5;
                                        d4 = d2;
                                        this.u = d4;
                                        List<Double> list2 = this.C;
                                        if (bps != 0.0d || !list2.isEmpty()) {
                                            list2.add(Double.valueOf(bps));
                                        }
                                        y();
                                    }
                                    synchronized (this.f17213a) {
                                        g gVar = this.f17216e;
                                        j2 = currentTimeMillis3;
                                        gVar.f14934e = (int) (this.p * 100.0d);
                                        double d8 = this.t;
                                        gVar.f14932c = (int) (d8 * 100.0d);
                                        boolean z3 = this.A;
                                        gVar.f14933d = (int) (z3 ? 100.0d : this.q * 100.0d);
                                        gVar.b = (int) ((!z3 || !this.z) ? (d8 + this.q) * 50.0d : 100.0d);
                                        gVar.f14937h = new ArrayList(this.C);
                                        this.f17216e.f14938i = new ArrayList(this.B);
                                        this.f17216e.f14936g = System.currentTimeMillis();
                                        if (!this.f17216e.a() && !this.f17216e.b()) {
                                            this.f17216e.l = h();
                                        }
                                        n();
                                    }
                                    if (isFinished) {
                                        Log.i("fing:ist-mobile-engine", ndtType + "_TEST [100%] - FINISHED! Speed: " + e.e.a.a.a.a.w(d5) + " Mbps. Total bytes: " + F.format(d4) + ".");
                                        break;
                                    }
                                    Log.i("fing:ist-mobile-engine", ndtType + "_TEST [" + F.format(progress * 100.0d) + "%] - Sample: " + e.e.a.a.a.a.w(bps) + " Mbps");
                                } else {
                                    j2 = currentTimeMillis3;
                                }
                                j5 = j2;
                                if (p()) {
                                    break;
                                }
                                currentTimeMillis = j2;
                                j3 = 75;
                                j4 = 0;
                            }
                            j2 = currentTimeMillis3;
                            if (p()) {
                            }
                        } else {
                            this.D.add(ndtType.toString() + ": " + ndtError.getCode());
                            throw new MobileSpeedTestException(h.a(poll.getError()));
                        }
                    }
                    this.f17214c.stop();
                    synchronized (this.f17213a) {
                        this.f17216e.l = h();
                        if (ndtType.isUpload()) {
                            this.f17216e.f14933d = 100;
                            this.A = true;
                        } else {
                            this.f17216e.f14932c = 100;
                            this.z = true;
                        }
                    }
                    n();
                    return;
                }
                throw new MobileSpeedTestException(h.a(start.getError()));
            } catch (Throwable th) {
                this.f17214c.stop();
                throw th;
            }
        } else {
            throw new MobileSpeedTestException("Engine is not running");
        }
    }

    /* JADX WARNING: Exception block dominator not found, dom blocks: [] */
    /* JADX WARNING: Missing exception handler attribute for start block: B:22:0x008a */
    private void y() {
        synchronized (this.f17213a) {
            double d2 = 0.0d;
            if (this.f17216e.a()) {
                List<Double> list = this.f17216e.f14937h;
                double doubleValue = list.get(list.size() - 1).doubleValue() / 1000000.0d;
                if (doubleValue > 0.0d) {
                    this.f17220i.b(doubleValue);
                    List<Double> list2 = this.f17216e.j;
                    if (!list2.isEmpty()) {
                        d2 = this.f17220i.a();
                    }
                    list2.add(Double.valueOf(d2));
                }
            } else if (this.f17216e.b()) {
                List<Double> list3 = this.f17216e.f14938i;
                double doubleValue2 = list3.get(list3.size() - 1).doubleValue() / 1000000.0d;
                if (doubleValue2 > 0.0d) {
                    this.j.b(doubleValue2);
                    List<Double> list4 = this.f17216e.k;
                    if (!list4.isEmpty()) {
                        d2 = this.j.a();
                    }
                    list4.add(Double.valueOf(d2));
                }
            }
        }
    }

    @Override // com.overlook.android.fing.engine.e.h.b
    public void A(l lVar) {
    }

    @Override // com.overlook.android.fing.engine.j.a.e.t.b
    public void a() {
        synchronized (this.f17213a) {
            this.f17217f = null;
        }
    }

    @Override // com.overlook.android.fing.engine.j.a.e.t.b
    public void b() {
        Thread thread;
        synchronized (this.f17213a) {
            x();
            thread = this.m;
            this.m = null;
        }
        if (thread != null) {
            try {
                Log.v("fing:ist-mobile-engine", "Stopping running thread...");
                thread.interrupt();
                thread.join();
            } catch (InterruptedException unused) {
            }
        }
    }

    @Override // com.overlook.android.fing.engine.j.a.d
    public com.overlook.android.fing.engine.j.a.b c() {
        return null;
    }

    @Override // com.overlook.android.fing.engine.j.a.e.t.b
    public void d(c cVar) {
        synchronized (this.f17213a) {
            this.f17217f = cVar;
        }
    }

    @Override // com.overlook.android.fing.engine.e.h.b
    public void e(com.overlook.android.fing.engine.e.i iVar) {
        if (p()) {
            h b2 = this.b.b();
            CarrierInfo carrierInfo = null;
            WiFiConnectionInfo n2 = b2.t() ? b2.n() : null;
            boolean z2 = true;
            if (n2 == null && b2.s()) {
                carrierInfo = b2.g(true);
            }
            synchronized (this.f17213a) {
                if (n2 == null && carrierInfo == null) {
                    o(h.MSE_LOST_SIGNAL);
                    n();
                } else if (this.f17216e.q == null || n2 == null || n2.d() == null || n2.d().equals(this.f17216e.q.d())) {
                    g gVar = this.f17216e;
                    boolean z3 = (gVar.q == null || carrierInfo == null) ? false : true;
                    if (gVar.p == null || n2 == null) {
                        z2 = false;
                    }
                    if (z3 || z2) {
                        o(z3 ? h.MSE_WIFI_TO_CELLULAR : h.MSE_CELLULAR_TO_WIFI);
                        n();
                    }
                } else {
                    o(h.MSE_WIFI_CHANGED);
                    n();
                }
            }
        }
    }

    public UserRating g(RatingSubject ratingSubject) {
        UserRating.b bVar = new UserRating.b();
        bVar.n(0);
        bVar.k("overall");
        bVar.m(ratingSubject);
        bVar.l(0);
        return bVar.h();
    }

    @Override // com.overlook.android.fing.engine.j.a.e.t.b
    public void start() {
        Log.v("fing:ist-mobile-engine", "Starting mobile internet speedtest");
        this.b.b().x(this);
        synchronized (this.f17213a) {
            if (this.f17216e.f14931a == b.EnumC0144b.READY) {
                g gVar = new g();
                this.f17216e = gVar;
                gVar.f14931a = b.EnumC0144b.RUNNING;
                Thread thread = new Thread(new f(this));
                this.m = thread;
                n();
                thread.start();
            }
        }
    }

    public void x() {
        Log.v("fing:ist-mobile-engine", "Stopping internet speedtest...");
        synchronized (this.f17213a) {
            g gVar = this.f17216e;
            if (gVar.f14931a == b.EnumC0144b.RUNNING) {
                gVar.f14931a = b.EnumC0144b.STOPPING;
                n();
                synchronized (this.f17213a) {
                    this.f17213a.notifyAll();
                }
            }
        }
    }
}