package com.vinfast.companion.car.utils.location;

/* JADX INFO: compiled from: r8-map-id-ae6aab8d9a1033945587ffdd1bc6222159522c1d66e66a2c4cecb065134dd0a6 */
/* JADX INFO: loaded from: E:\ios\vf_dex\classes15.dex */
@kotlin.Metadata(d1 = {"\u0000@\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\b\u0003\n\u0002\u0010\t\n\u0000\n\u0002\u0010\u0002\n\u0002\b\n\n\u0002\u0010\u000e\n\u0002\b\u0003\n\u0002\u0018\u0002\n\u0000\n\u0002\u0010\u000b\n\u0002\b\u000f\n\u0002\u0018\u0002\n\u0002\b\u000e\u0018\u00002\u00020\u00012\u00020\u0002B\u000f\u0012\u0006\u0010\u0004\u001a\u00020\u0003¢\u0006\u0004\b\u0005\u0010\u0006J\u0017\u0010\n\u001a\u00020\t2\u0006\u0010\b\u001a\u00020\u0007H\u0003¢\u0006\u0004\b\n\u0010\u000bJ\u0017\u0010\f\u001a\u00020\t2\u0006\u0010\b\u001a\u00020\u0007H\u0003¢\u0006\u0004\b\f\u0010\u000bJ\u0017\u0010\r\u001a\u00020\t2\u0006\u0010\b\u001a\u00020\u0007H\u0003¢\u0006\u0004\b\r\u0010\u000bJ\u0017\u0010\u000e\u001a\u00020\t2\u0006\u0010\b\u001a\u00020\u0007H\u0003¢\u0006\u0004\b\u000e\u0010\u000bJ\u000f\u0010\u000f\u001a\u00020\tH\u0003¢\u0006\u0004\b\u000f\u0010\u0010J\u000f\u0010\u0011\u001a\u00020\tH\u0003¢\u0006\u0004\b\u0011\u0010\u0010J\u000f\u0010\u0012\u001a\u00020\tH\u0003¢\u0006\u0004\b\u0012\u0010\u0010J\u000f\u0010\u0013\u001a\u00020\tH\u0003¢\u0006\u0004\b\u0013\u0010\u0010J\u0017\u0010\u0016\u001a\u00020\t2\u0006\u0010\u0015\u001a\u00020\u0014H\u0003¢\u0006\u0004\b\u0016\u0010\u0017J\u001f\u0010\u001b\u001a\u00020\u001a*\u0004\u0018\u00010\u00182\b\u0010\u0019\u001a\u0004\u0018\u00010\u0018H\u0002¢\u0006\u0004\b\u001b\u0010\u001cJ\u0011\u0010\u001d\u001a\u0004\u0018\u00010\u0018H\u0017¢\u0006\u0004\b\u001d\u0010\u001eJ\u000f\u0010\u001f\u001a\u00020\tH\u0015¢\u0006\u0004\b\u001f\u0010\u0010J\u000f\u0010 \u001a\u00020\tH\u0015¢\u0006\u0004\b \u0010\u0010J\u0017\u0010\"\u001a\u00020\t2\u0006\u0010!\u001a\u00020\u0018H\u0016¢\u0006\u0004\b\"\u0010#R\u0014\u0010%\u001a\u00020\u00078\u0002X\u0082\u0004¢\u0006\u0006\n\u0004\b \u0010$R\u0014\u0010&\u001a\u00020\u00078\u0002X\u0082\u0004¢\u0006\u0006\n\u0004\b\u001f\u0010$R\u001c\u0010)\u001a\n '*\u0004\u0018\u00010\u00140\u00148\u0002X\u0082\u0004¢\u0006\u0006\n\u0004\b\u001d\u0010(R\u0014\u0010-\u001a\u00020*8\u0002X\u0082\u0004¢\u0006\u0006\n\u0004\b+\u0010,R\u0018\u00100\u001a\u0004\u0018\u00010\u00188\u0002@\u0002X\u0082\u000e¢\u0006\u0006\n\u0004\b.\u0010/R\u0018\u00102\u001a\u0004\u0018\u00010\u00188\u0002@\u0002X\u0082\u000e¢\u0006\u0006\n\u0004\b1\u0010/R\u0018\u00104\u001a\u0004\u0018\u00010\u00188\u0002@\u0002X\u0082\u000e¢\u0006\u0006\n\u0004\b3\u0010/R\u0014\u00107\u001a\u00020\u001a8BX\u0082\u0004¢\u0006\u0006\u001a\u0004\b5\u00106¨\u00068"}, d2 = {"Lcom/vinfast/companion/car/utils/location/FusionEngine;", "Lcom/vinfast/companion/car/utils/location/LocationEngine;", "Landroidx/core/location/LocationListenerCompat;", "Landroid/content/Context;", "context", "<init>", "(Landroid/content/Context;)V", "", "interval", "", "r", "(J)V", "s", "t", "q", "n", "()V", "o", "p", "m", "", "provider", "l", "(Ljava/lang/String;)V", "Landroid/location/Location;", "other", "", "v", "(Landroid/location/Location;Landroid/location/Location;)Z", "e", "()Landroid/location/Location;", "d", "c", "location", "onLocationChanged", "(Landroid/location/Location;)V", "J", "RECENT_UPDATE_THRESHOLD_IN_MILLIS", "RECENT_UPDATE_THRESHOLD_IN_NANOS", "kotlin.jvm.PlatformType", "Ljava/lang/String;", "TAG", "Landroid/location/LocationManager;", "f", "Landroid/location/LocationManager;", "locationManager", "g", "Landroid/location/Location;", "gpsLocation", "h", "networkLocation", "i", "fusedLocation", "u", "()Z", "supportsSystemFusedProvider", "car_release"}, k = 1, mv = {2, 2, 0}, xi = 48)
@kotlin.jvm.internal.SourceDebugExtension
public final class FusionEngine extends com.vinfast.companion.car.utils.location.LocationEngine implements androidx.core.location.LocationListenerCompat {

    /* JADX INFO: renamed from: c, reason: from kotlin metadata */
    private final long RECENT_UPDATE_THRESHOLD_IN_MILLIS;

    /* JADX INFO: renamed from: d, reason: from kotlin metadata */
    private final long RECENT_UPDATE_THRESHOLD_IN_NANOS;

    /* JADX INFO: renamed from: e, reason: from kotlin metadata */
    private final java.lang.String TAG;

    /* JADX INFO: renamed from: f, reason: from kotlin metadata */
    private final android.location.LocationManager locationManager;

    /* JADX INFO: renamed from: g, reason: from kotlin metadata */
    private android.location.Location gpsLocation;

    /* JADX INFO: renamed from: h, reason: from kotlin metadata */
    private android.location.Location networkLocation;

    /* JADX INFO: renamed from: i, reason: from kotlin metadata */
    private android.location.Location fusedLocation;

    /* JADX INFO: compiled from: r8-map-id-ae6aab8d9a1033945587ffdd1bc6222159522c1d66e66a2c4cecb065134dd0a6 */
    @kotlin.Metadata(k = 3, mv = {2, 2, 0}, xi = 48)
    public static final /* synthetic */ class WhenMappings {
        public static final /* synthetic */ int[] a;

        static {
            int[] iArr = new int[com.vinfast.companion.car.utils.location.Priority.values().length];
            try {
                iArr[com.vinfast.companion.car.utils.location.Priority.HIGH_ACCURACY.ordinal()] = 1;
            } catch (java.lang.NoSuchFieldError unused) {
            }
            try {
                iArr[com.vinfast.companion.car.utils.location.Priority.BALANCED_POWER_ACCURACY.ordinal()] = 2;
            } catch (java.lang.NoSuchFieldError unused2) {
            }
            try {
                iArr[com.vinfast.companion.car.utils.location.Priority.LOW_POWER.ordinal()] = 3;
            } catch (java.lang.NoSuchFieldError unused3) {
            }
            try {
                iArr[com.vinfast.companion.car.utils.location.Priority.NO_POWER.ordinal()] = 4;
            } catch (java.lang.NoSuchFieldError unused4) {
            }
            a = iArr;
        }
    }

    /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
    public FusionEngine(android.content.Context context) {
        super(context);
        kotlin.jvm.internal.Intrinsics.l(context, "context");
        this.RECENT_UPDATE_THRESHOLD_IN_MILLIS = 60000L;
        this.RECENT_UPDATE_THRESHOLD_IN_NANOS = 60000 * ((long) 1000000);
        this.TAG = com.vinfast.companion.car.utils.location.FusionEngine.class.getSimpleName();
        java.lang.Object systemService = context.getSystemService("location");
        kotlin.jvm.internal.Intrinsics.j(systemService, "null cannot be cast to non-null type android.location.LocationManager");
        this.locationManager = (android.location.LocationManager) systemService;
    }

    private final void l(java.lang.String provider) {
        android.location.Location lastKnownLocation = this.locationManager.getLastKnownLocation(provider);
        if (lastKnownLocation != null) {
            onLocationChanged(lastKnownLocation);
        }
    }

    private final void m() {
        l("fused");
    }

    private final void n() {
        l("gps");
    }

    private final void o() {
        l("network");
    }

    private final void p() {
        l("passive");
    }

    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Type inference failed for: r5v0 */
    /* JADX WARN: Type inference failed for: r5v2, types: [android.location.LocationListener] */
    private final void q(long interval) {
        com.vinfast.companion.car.utils.location.FusionEngine fusionEngine;
        try {
            fusionEngine = this;
            try {
                this.locationManager.requestLocationUpdates("fused", interval, 0.0f, (android.location.LocationListener) fusionEngine, f());
            } catch (java.lang.IllegalArgumentException e) {
                e = e;
                android.util.Log.e(fusionEngine.TAG, "Unable to register for passive updates.", e);
            }
        } catch (java.lang.IllegalArgumentException e2) {
            e = e2;
            fusionEngine = this;
        }
    }

    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Type inference failed for: r5v0 */
    /* JADX WARN: Type inference failed for: r5v2, types: [android.location.LocationListener] */
    private final void r(long interval) {
        com.vinfast.companion.car.utils.location.FusionEngine fusionEngine;
        try {
            fusionEngine = this;
            try {
                this.locationManager.requestLocationUpdates("gps", interval, 0.0f, (android.location.LocationListener) fusionEngine, f());
            } catch (java.lang.IllegalArgumentException e) {
                e = e;
                android.util.Log.e(fusionEngine.TAG, "Unable to register for GPS updates.", e);
            }
        } catch (java.lang.IllegalArgumentException e2) {
            e = e2;
            fusionEngine = this;
        }
    }

    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Type inference failed for: r5v0 */
    /* JADX WARN: Type inference failed for: r5v2, types: [android.location.LocationListener] */
    private final void s(long interval) {
        com.vinfast.companion.car.utils.location.FusionEngine fusionEngine;
        try {
            fusionEngine = this;
            try {
                this.locationManager.requestLocationUpdates("network", interval, 0.0f, (android.location.LocationListener) fusionEngine, f());
            } catch (java.lang.IllegalArgumentException e) {
                e = e;
                android.util.Log.e(fusionEngine.TAG, "Unable to register for network updates.", e);
            }
        } catch (java.lang.IllegalArgumentException e2) {
            e = e2;
            fusionEngine = this;
        }
    }

    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Type inference failed for: r5v0 */
    /* JADX WARN: Type inference failed for: r5v2, types: [android.location.LocationListener] */
    private final void t(long interval) {
        com.vinfast.companion.car.utils.location.FusionEngine fusionEngine;
        try {
            fusionEngine = this;
            try {
                this.locationManager.requestLocationUpdates("passive", interval, 0.0f, (android.location.LocationListener) fusionEngine, f());
            } catch (java.lang.IllegalArgumentException e) {
                e = e;
                android.util.Log.e(fusionEngine.TAG, "Unable to register for passive updates.", e);
            }
        } catch (java.lang.IllegalArgumentException e2) {
            e = e2;
            fusionEngine = this;
        }
    }

    private final boolean u() {
        return android.os.Build.VERSION.SDK_INT >= 31 && this.locationManager.getAllProviders().contains("fused");
    }

    private final boolean v(android.location.Location location, android.location.Location location2) {
        if (location == null) {
            return false;
        }
        if (location2 == null || location.getElapsedRealtimeNanos() > location2.getElapsedRealtimeNanos() + this.RECENT_UPDATE_THRESHOLD_IN_NANOS) {
            return true;
        }
        if (location.hasAccuracy()) {
            return !location2.hasAccuracy() || location.getAccuracy() < location2.getAccuracy();
        }
        return false;
    }

    /* JADX WARN: Multi-variable type inference failed */
    @Override // com.vinfast.companion.car.utils.location.LocationEngine
    protected void c() {
        this.locationManager.removeUpdates((android.location.LocationListener) this);
        this.gpsLocation = null;
        this.networkLocation = null;
        this.fusedLocation = null;
    }

    /* JADX INFO: Thrown type has an unknown type hierarchy: kotlin.NoWhenBranchMatchedException */
    @Override // com.vinfast.companion.car.utils.location.LocationEngine
    protected void d() throws kotlin.NoWhenBranchMatchedException {
        java.util.Iterator it = getRequests().iterator();
        long j = Long.MAX_VALUE;
        long j2 = Long.MAX_VALUE;
        long j3 = Long.MAX_VALUE;
        while (true) {
            boolean z = true;
            if (!it.hasNext()) {
                if (u() && j < Long.MAX_VALUE) {
                    try {
                        q(j);
                        m();
                    } catch (java.lang.SecurityException e) {
                        android.util.Log.e(this.TAG, "Permissions not granted for fused provider", e);
                    }
                }
                if (j < Long.MAX_VALUE) {
                    r(j);
                } else {
                    z = false;
                }
                if (j2 < Long.MAX_VALUE) {
                    s(j2);
                    if (z) {
                        android.location.Location lastKnownLocation = this.locationManager.getLastKnownLocation("gps");
                        android.location.Location lastKnownLocation2 = this.locationManager.getLastKnownLocation("network");
                        if (lastKnownLocation == null || lastKnownLocation2 == null) {
                            if (lastKnownLocation != null) {
                                n();
                            } else {
                                o();
                            }
                        } else if (v(lastKnownLocation, lastKnownLocation2)) {
                            n();
                        } else {
                            o();
                        }
                    } else {
                        o();
                    }
                }
                if (j3 < Long.MAX_VALUE) {
                    t(j3);
                    p();
                    return;
                }
                return;
            }
            com.vinfast.companion.car.utils.location.LocationRequest locationRequest = (com.vinfast.companion.car.utils.location.LocationRequest) it.next();
            com.vinfast.companion.car.utils.location.Priority priority = locationRequest.getPriority();
            long intervalMs = locationRequest.getIntervalMs();
            int i = com.vinfast.companion.car.utils.location.FusionEngine.WhenMappings.a[priority.ordinal()];
            if (i == 1) {
                if (intervalMs < j) {
                    j = intervalMs;
                }
                if (intervalMs < j2) {
                    j2 = intervalMs;
                }
            } else if (i == 2 || i == 3) {
                if (intervalMs < j2) {
                    j2 = intervalMs;
                }
            } else {
                if (i != 4) {
                    throw new kotlin.NoWhenBranchMatchedException();
                }
                if (intervalMs < j3) {
                    j3 = intervalMs;
                }
            }
        }
    }

    @Override // com.vinfast.companion.car.utils.location.LocationEngine
    public android.location.Location e() {
        if (u()) {
            try {
                return this.locationManager.getLastKnownLocation("fused");
            } catch (java.lang.SecurityException e) {
                android.util.Log.e(this.TAG, "Permissions not granted for fused provider", e);
            }
        }
        long jElapsedRealtimeNanos = android.os.SystemClock.elapsedRealtimeNanos() - this.RECENT_UPDATE_THRESHOLD_IN_NANOS;
        android.location.Location lastKnownLocation = null;
        long j = Long.MIN_VALUE;
        float f = Float.MAX_VALUE;
        for (java.lang.String str : this.locationManager.getAllProviders()) {
            try {
                lastKnownLocation = this.locationManager.getLastKnownLocation(str);
                if (lastKnownLocation != null) {
                    float accuracy = lastKnownLocation.getAccuracy();
                    long elapsedRealtimeNanos = lastKnownLocation.getElapsedRealtimeNanos();
                    if (elapsedRealtimeNanos > jElapsedRealtimeNanos && accuracy < f) {
                        f = accuracy;
                    } else if (elapsedRealtimeNanos >= jElapsedRealtimeNanos || f != Float.MAX_VALUE || elapsedRealtimeNanos <= j) {
                    }
                    j = elapsedRealtimeNanos;
                }
            } catch (java.lang.SecurityException e2) {
                android.util.Log.e(this.TAG, "Permissions not granted for provider: " + str, e2);
            }
        }
        return lastKnownLocation;
    }

    public void onLocationChanged(android.location.Location location) {
        kotlin.jvm.internal.Intrinsics.l(location, "location");
        if (kotlin.jvm.internal.Intrinsics.g("fused", location.getProvider())) {
            this.fusedLocation = location;
            java.util.Iterator it = getRequests().iterator();
            while (it.hasNext()) {
                ((com.vinfast.companion.car.utils.location.LocationRequest) it.next()).getListener().onLocationChanged(location);
            }
            return;
        }
        if (kotlin.jvm.internal.Intrinsics.g("gps", location.getProvider())) {
            this.gpsLocation = location;
            if (v(location, this.networkLocation) && this.fusedLocation == null) {
                java.util.Iterator it2 = getRequests().iterator();
                while (it2.hasNext()) {
                    ((com.vinfast.companion.car.utils.location.LocationRequest) it2.next()).getListener().onLocationChanged(location);
                }
                return;
            }
            return;
        }
        if (kotlin.jvm.internal.Intrinsics.g("network", location.getProvider())) {
            this.networkLocation = location;
            if (v(location, this.gpsLocation) && this.fusedLocation == null) {
                java.util.Iterator it3 = getRequests().iterator();
                while (it3.hasNext()) {
                    ((com.vinfast.companion.car.utils.location.LocationRequest) it3.next()).getListener().onLocationChanged(location);
                }
            }
        }
    }
}
