package M;

import android.content.Context;
import android.content.res.Resources;
import android.os.Build;
import android.view.InputDevice;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.ViewConfiguration;
import java.lang.reflect.Method;
import java.util.Map;
import java.util.Objects;

/* JADX INFO: renamed from: M.g, reason: case insensitive filesystem */
/* JADX INFO: loaded from: classes.dex */
public final class C0177g {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final Context f4110a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final InterfaceC0178h f4111b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public VelocityTracker f4112c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public float f4113d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public int f4114e = -1;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public int f4115f = -1;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public int f4116g = -1;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public final int[] f4117h = {Integer.MAX_VALUE, 0};

    public C0177g(Context context, InterfaceC0178h interfaceC0178h) {
        this.f4110a = context;
        this.f4111b = interfaceC0178h;
    }

    /* JADX WARN: Removed duplicated region for block: B:120:0x024a  */
    /* JADX WARN: Removed duplicated region for block: B:121:0x0252  */
    /* JADX WARN: Removed duplicated region for block: B:30:0x0081  */
    /* JADX WARN: Removed duplicated region for block: B:42:0x00aa  */
    /* JADX WARN: Removed duplicated region for block: B:87:0x0175  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final void a(MotionEvent motionEvent, int i) {
        int i5;
        int i8;
        int i9;
        int scaledMinimumFlingVelocity;
        int scaledMaximumFlingVelocity;
        boolean z8;
        float f3;
        float yVelocity;
        long j8;
        float f7;
        float fSqrt;
        float f8;
        int i10;
        float f9;
        int source = motionEvent.getSource();
        int deviceId = motionEvent.getDeviceId();
        int i11 = this.f4115f;
        int[] iArr = this.f4117h;
        if (i11 == source && this.f4116g == deviceId && this.f4114e == i) {
            z8 = false;
            i5 = 1;
            i8 = 20;
            i9 = 0;
        } else {
            Context context = this.f4110a;
            ViewConfiguration viewConfiguration = ViewConfiguration.get(context);
            int deviceId2 = motionEvent.getDeviceId();
            i5 = 1;
            int source2 = motionEvent.getSource();
            i8 = 20;
            int i12 = Build.VERSION.SDK_INT;
            i9 = 0;
            if (i12 >= 34) {
                Method method = K.f4070a;
                scaledMinimumFlingVelocity = AbstractC0189t.f(viewConfiguration, deviceId2, i, source2);
            } else {
                Method method2 = K.f4070a;
                InputDevice device = InputDevice.getDevice(deviceId2);
                if (device == null || device.getMotionRange(i, source2) == null) {
                    scaledMinimumFlingVelocity = Integer.MAX_VALUE;
                } else {
                    Resources resources = context.getResources();
                    int identifier = (source2 == 4194304 && i == 26) ? resources.getIdentifier("config_viewMinRotaryEncoderFlingVelocity", "dimen", "android") : -1;
                    Objects.requireNonNull(viewConfiguration);
                    if (identifier == -1) {
                        scaledMinimumFlingVelocity = viewConfiguration.getScaledMinimumFlingVelocity();
                    } else if (identifier == 0 || (scaledMinimumFlingVelocity = resources.getDimensionPixelSize(identifier)) < 0) {
                    }
                }
            }
            iArr[0] = scaledMinimumFlingVelocity;
            int deviceId3 = motionEvent.getDeviceId();
            int source3 = motionEvent.getSource();
            if (i12 >= 34) {
                scaledMaximumFlingVelocity = AbstractC0189t.e(viewConfiguration, deviceId3, i, source3);
            } else {
                InputDevice device2 = InputDevice.getDevice(deviceId3);
                if ((device2 == null || device2.getMotionRange(i, source3) == null) ? false : true) {
                    Resources resources2 = context.getResources();
                    int identifier2 = (source3 == 4194304 && i == 26) ? resources2.getIdentifier("config_viewMaxRotaryEncoderFlingVelocity", "dimen", "android") : -1;
                    Objects.requireNonNull(viewConfiguration);
                    if (identifier2 == -1) {
                        scaledMaximumFlingVelocity = viewConfiguration.getScaledMaximumFlingVelocity();
                    } else if (identifier2 == 0 || (scaledMaximumFlingVelocity = resources2.getDimensionPixelSize(identifier2)) < 0) {
                    }
                } else {
                    scaledMaximumFlingVelocity = Integer.MIN_VALUE;
                }
            }
            iArr[1] = scaledMaximumFlingVelocity;
            this.f4115f = source;
            this.f4116g = deviceId;
            this.f4114e = i;
            z8 = true;
        }
        if (iArr[i9] == Integer.MAX_VALUE) {
            VelocityTracker velocityTracker = this.f4112c;
            if (velocityTracker != null) {
                velocityTracker.recycle();
                this.f4112c = null;
                return;
            }
            return;
        }
        if (this.f4112c == null) {
            this.f4112c = VelocityTracker.obtain();
        }
        VelocityTracker velocityTracker2 = this.f4112c;
        Map map = AbstractC0190u.f4132a;
        velocityTracker2.addMovement(motionEvent);
        float f10 = 0.0f;
        if (Build.VERSION.SDK_INT < 34 && motionEvent.getSource() == 4194304) {
            Map map2 = AbstractC0190u.f4132a;
            if (!map2.containsKey(velocityTracker2)) {
                map2.put(velocityTracker2, new C0191v());
            }
            C0191v c0191v = (C0191v) map2.get(velocityTracker2);
            c0191v.getClass();
            long eventTime = motionEvent.getEventTime();
            int i13 = c0191v.f4136d;
            long[] jArr = c0191v.f4134b;
            if (i13 != 0 && eventTime - jArr[c0191v.f4137e] > 40) {
                c0191v.f4136d = i9;
                c0191v.f4135c = 0.0f;
            }
            int i14 = (c0191v.f4137e + 1) % 20;
            c0191v.f4137e = i14;
            int i15 = c0191v.f4136d;
            if (i15 != i8) {
                c0191v.f4136d = i15 + 1;
            }
            c0191v.f4133a[i14] = motionEvent.getAxisValue(26);
            jArr[c0191v.f4137e] = eventTime;
        }
        int i16 = 1000;
        velocityTracker2.computeCurrentVelocity(1000, Float.MAX_VALUE);
        C0191v c0191v2 = (C0191v) AbstractC0190u.f4132a.get(velocityTracker2);
        if (c0191v2 != null) {
            int i17 = c0191v2.f4136d;
            if (i17 < 2) {
                f7 = Float.MAX_VALUE;
                fSqrt = 0.0f;
                f3 = 0.0f;
                f9 = fSqrt * i16;
                c0191v2.f4135c = f9;
                if (f9 >= (-Math.abs(f7))) {
                    c0191v2.f4135c = -Math.abs(f7);
                } else if (c0191v2.f4135c > Math.abs(f7)) {
                    c0191v2.f4135c = Math.abs(f7);
                }
            } else {
                int i18 = c0191v2.f4137e;
                int i19 = ((i18 + 20) - (i17 - 1)) % 20;
                long[] jArr2 = c0191v2.f4134b;
                long j9 = jArr2[i18];
                while (true) {
                    j8 = jArr2[i19];
                    if (j9 - j8 <= 100) {
                        break;
                    }
                    c0191v2.f4136d--;
                    i19 = (i19 + 1) % 20;
                }
                int i20 = c0191v2.f4136d;
                if (i20 >= 2) {
                    float[] fArr = c0191v2.f4133a;
                    if (i20 == 2) {
                        int i21 = (i19 + 1) % 20;
                        if (j8 != jArr2[i21]) {
                            fSqrt = fArr[i21] / (r22 - j8);
                            f7 = Float.MAX_VALUE;
                            f3 = 0.0f;
                        }
                    } else {
                        f7 = Float.MAX_VALUE;
                        float fAbs = 0.0f;
                        int i22 = 0;
                        int i23 = 0;
                        while (true) {
                            if (i22 >= c0191v2.f4136d - 1) {
                                break;
                            }
                            int i24 = i22 + i19;
                            long j10 = jArr2[i24 % 20];
                            int i25 = (i24 + 1) % 20;
                            if (jArr2[i25] == j10) {
                                f8 = f10;
                                i10 = i5;
                            } else {
                                i23++;
                                f8 = f10;
                                float fSqrt2 = (fAbs < f10 ? -1.0f : 1.0f) * ((float) Math.sqrt(Math.abs(fAbs) * 2.0f));
                                float f11 = fArr[i25] / (jArr2[i25] - j10);
                                fAbs += Math.abs(f11) * (f11 - fSqrt2);
                                i10 = i5;
                                if (i23 == i10) {
                                    fAbs *= 0.5f;
                                }
                            }
                            i22 += i10;
                            i5 = i10;
                            f10 = f8;
                        }
                        f3 = f10;
                        fSqrt = (fAbs < f3 ? -1.0f : 1.0f) * ((float) Math.sqrt(Math.abs(fAbs) * 2.0f));
                        i16 = 1000;
                    }
                    f9 = fSqrt * i16;
                    c0191v2.f4135c = f9;
                    if (f9 >= (-Math.abs(f7))) {
                    }
                }
            }
        } else {
            f3 = 0.0f;
        }
        if (Build.VERSION.SDK_INT >= 34) {
            yVelocity = AbstractC0189t.b(velocityTracker2, i);
        } else if (i == 0) {
            yVelocity = velocityTracker2.getXVelocity();
        } else if (i == 1) {
            yVelocity = velocityTracker2.getYVelocity();
        } else {
            C0191v c0191v3 = (C0191v) AbstractC0190u.f4132a.get(velocityTracker2);
            yVelocity = (c0191v3 == null || i != 26) ? f3 : c0191v3.f4135c;
        }
        InterfaceC0178h interfaceC0178h = this.f4111b;
        float fL = interfaceC0178h.l() * yVelocity;
        float fSignum = Math.signum(fL);
        if (z8 || (fSignum != Math.signum(this.f4113d) && fSignum != f3)) {
            interfaceC0178h.n();
        }
        if (Math.abs(fL) < iArr[0]) {
            return;
        }
        float fMax = Math.max(-r1, Math.min(fL, iArr[1]));
        this.f4113d = interfaceC0178h.a(fMax) ? fMax : f3;
    }
}
