package io.flutter.embedding.android;

import android.R;
import android.content.Context;
import android.graphics.Matrix;
import android.os.Build;
import android.util.TypedValue;
import android.view.InputDevice;
import android.view.MotionEvent;
import android.view.ViewConfiguration;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.HashMap;

/* JADX INFO: loaded from: classes.dex */
public final class b {

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public static final Matrix f27886f = new Matrix();

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final io.flutter.embedding.engine.renderer.j f27887a;

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

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public final boolean f27889c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public final HashMap f27890d = new HashMap();

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public int f27891e;

    public b(io.flutter.embedding.engine.renderer.j jVar, boolean z10) {
        this.f27887a = jVar;
        if (g0.f27906c == null) {
            g0.f27906c = new g0();
        }
        this.f27888b = g0.f27906c;
        this.f27889c = z10;
    }

    public static int b(int i10) {
        if (i10 == 0) {
            return 4;
        }
        if (i10 == 1) {
            return 6;
        }
        if (i10 == 5) {
            return 4;
        }
        if (i10 == 6) {
            return 6;
        }
        if (i10 == 2) {
            return 5;
        }
        if (i10 == 7) {
            return 3;
        }
        if (i10 == 3) {
            return 0;
        }
        return i10 == 8 ? 3 : -1;
    }

    public final void a(MotionEvent motionEvent, int i10, int i11, int i12, Matrix matrix, ByteBuffer byteBuffer, Context context) {
        int i13;
        long buttonState;
        long jIncrementAndGet;
        double min;
        double max;
        MotionEvent motionEvent2;
        int i14;
        double d10;
        double d11;
        double scaledVerticalScrollFactor;
        b bVar;
        float fC;
        InputDevice.MotionRange motionRange;
        int i15 = -1;
        if (i11 == -1) {
            return;
        }
        int pointerId = motionEvent.getPointerId(i10);
        int toolType = motionEvent.getToolType(i10);
        if (toolType == 1) {
            i13 = 0;
        } else if (toolType != 2) {
            i13 = 3;
            if (toolType == 3) {
                i13 = 1;
            } else if (toolType != 4) {
                i13 = 5;
            }
        } else {
            i13 = 2;
        }
        float[] fArr = {motionEvent.getX(i10), motionEvent.getY(i10)};
        matrix.mapPoints(fArr);
        HashMap map = this.f27890d;
        if (i13 == 1) {
            buttonState = motionEvent.getButtonState() & 31;
            if (buttonState == 0 && motionEvent.getSource() == 8194 && i11 == 4) {
                map.put(Integer.valueOf(pointerId), fArr);
            }
        } else {
            buttonState = i13 == 2 ? (motionEvent.getButtonState() >> 4) & 15 : 0L;
        }
        boolean zContainsKey = map.containsKey(Integer.valueOf(pointerId));
        if (zContainsKey) {
            int i16 = i11 == 4 ? 7 : i11 == 5 ? 8 : (i11 == 6 || i11 == 0) ? 9 : -1;
            if (i16 == -1) {
                return;
            } else {
                i15 = i16;
            }
        }
        if (this.f27889c) {
            g0 g0Var = this.f27888b;
            g0Var.getClass();
            jIncrementAndGet = f0.f27902b.incrementAndGet();
            g0Var.f27907a.put(jIncrementAndGet, MotionEvent.obtain(motionEvent));
            g0Var.f27908b.add(Long.valueOf(jIncrementAndGet));
        } else {
            jIncrementAndGet = 0;
        }
        int i17 = motionEvent.getActionMasked() == 8 ? 1 : 0;
        long eventTime = motionEvent.getEventTime() * 1000;
        byteBuffer.putLong(jIncrementAndGet);
        byteBuffer.putLong(eventTime);
        if (zContainsKey) {
            byteBuffer.putLong(i15);
            byteBuffer.putLong(4L);
        } else {
            byteBuffer.putLong(i11);
            byteBuffer.putLong(i13);
        }
        byteBuffer.putLong(i17);
        byteBuffer.putLong(pointerId);
        byteBuffer.putLong(0L);
        if (zContainsKey) {
            float[] fArr2 = (float[]) map.get(Integer.valueOf(pointerId));
            byteBuffer.putDouble(fArr2[0]);
            byteBuffer.putDouble(fArr2[1]);
        } else {
            byteBuffer.putDouble(fArr[0]);
            byteBuffer.putDouble(fArr[1]);
        }
        byteBuffer.putDouble(0.0d);
        byteBuffer.putDouble(0.0d);
        byteBuffer.putLong(buttonState);
        byteBuffer.putLong(0L);
        byteBuffer.putLong(0L);
        byteBuffer.putDouble(motionEvent.getPressure(i10));
        if (motionEvent.getDevice() == null || (motionRange = motionEvent.getDevice().getMotionRange(2)) == null) {
            min = 0.0d;
            max = 1.0d;
        } else {
            min = motionRange.getMin();
            max = motionRange.getMax();
        }
        byteBuffer.putDouble(min);
        byteBuffer.putDouble(max);
        if (i13 == 2) {
            motionEvent2 = motionEvent;
            i14 = i10;
            byteBuffer.putDouble(motionEvent2.getAxisValue(24, i14));
            byteBuffer.putDouble(0.0d);
        } else {
            motionEvent2 = motionEvent;
            i14 = i10;
            byteBuffer.putDouble(0.0d);
            byteBuffer.putDouble(0.0d);
        }
        byteBuffer.putDouble(motionEvent.getSize(i10));
        byteBuffer.putDouble(motionEvent.getToolMajor(i10));
        byteBuffer.putDouble(motionEvent.getToolMinor(i10));
        byteBuffer.putDouble(0.0d);
        byteBuffer.putDouble(0.0d);
        byteBuffer.putDouble(motionEvent2.getAxisValue(8, i14));
        if (i13 == 2) {
            byteBuffer.putDouble(motionEvent2.getAxisValue(25, i14));
        } else {
            byteBuffer.putDouble(0.0d);
        }
        byteBuffer.putLong(i12);
        if (i17 == 1) {
            if (context != null) {
                int i18 = Build.VERSION.SDK_INT;
                if (i18 >= 26) {
                    fC = ViewConfiguration.get(context).getScaledHorizontalScrollFactor();
                    bVar = this;
                } else {
                    bVar = this;
                    fC = bVar.c(context);
                }
                d11 = fC;
                scaledVerticalScrollFactor = i18 >= 26 ? ViewConfiguration.get(context).getScaledVerticalScrollFactor() : bVar.c(context);
            } else {
                d11 = 48.0d;
                scaledVerticalScrollFactor = 48.0d;
            }
            double d12 = d11 * ((double) (-motionEvent2.getAxisValue(10, i14)));
            double d13 = scaledVerticalScrollFactor * ((double) (-motionEvent2.getAxisValue(9, i14)));
            byteBuffer.putDouble(d12);
            byteBuffer.putDouble(d13);
        } else {
            byteBuffer.putDouble(0.0d);
            byteBuffer.putDouble(0.0d);
        }
        if (zContainsKey) {
            float[] fArr3 = (float[]) map.get(Integer.valueOf(pointerId));
            byteBuffer.putDouble(fArr[0] - fArr3[0]);
            byteBuffer.putDouble(fArr[1] - fArr3[1]);
            d10 = 0.0d;
        } else {
            d10 = 0.0d;
            byteBuffer.putDouble(0.0d);
            byteBuffer.putDouble(0.0d);
        }
        byteBuffer.putDouble(d10);
        byteBuffer.putDouble(d10);
        byteBuffer.putDouble(1.0d);
        byteBuffer.putDouble(d10);
        byteBuffer.putLong(0L);
        if (zContainsKey && i15 == 9) {
            map.remove(Integer.valueOf(pointerId));
        }
    }

    public final int c(Context context) {
        if (this.f27891e == 0) {
            TypedValue typedValue = new TypedValue();
            if (!context.getTheme().resolveAttribute(R.attr.listPreferredItemHeight, typedValue, true)) {
                return 48;
            }
            this.f27891e = (int) typedValue.getDimension(context.getResources().getDisplayMetrics());
        }
        return this.f27891e;
    }

    /* JADX WARN: Removed duplicated region for block: B:23:0x0037 A[PHI: r5
      0x0037: PHI (r5v6 char) = (r5v5 char), (r5v7 char) binds: [B:17:0x002e, B:21:0x0034] A[DONT_GENERATE, DONT_INLINE]] */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final void d(MotionEvent motionEvent, Matrix matrix) {
        int actionMasked = motionEvent.getActionMasked();
        int iB = b(motionEvent.getActionMasked());
        char c6 = 5;
        boolean z10 = actionMasked == 0 || actionMasked == 5;
        boolean z11 = !z10 && (actionMasked == 1 || actionMasked == 6);
        int toolType = motionEvent.getToolType(motionEvent.getActionIndex());
        if (toolType != 1) {
            char c10 = 2;
            if (toolType != 2) {
                c10 = 3;
                if (toolType == 3) {
                    c6 = 1;
                } else if (toolType == 4) {
                    c6 = c10;
                }
            }
        } else {
            c6 = 0;
        }
        int i10 = (z11 && c6 == 0) ? 1 : 0;
        int pointerCount = motionEvent.getPointerCount();
        ByteBuffer byteBufferAllocateDirect = ByteBuffer.allocateDirect((pointerCount + i10) * 288);
        byteBufferAllocateDirect.order(ByteOrder.LITTLE_ENDIAN);
        if (z10) {
            a(motionEvent, motionEvent.getActionIndex(), iB, 0, matrix, byteBufferAllocateDirect, null);
        } else if (z11) {
            for (int i11 = 0; i11 < pointerCount; i11++) {
                if (i11 != motionEvent.getActionIndex() && motionEvent.getToolType(i11) == 1) {
                    a(motionEvent, i11, 5, 1, matrix, byteBufferAllocateDirect, null);
                }
            }
            a(motionEvent, motionEvent.getActionIndex(), iB, 0, matrix, byteBufferAllocateDirect, null);
            if (i10 != 0) {
                a(motionEvent, motionEvent.getActionIndex(), 2, 0, matrix, byteBufferAllocateDirect, null);
            }
        } else {
            for (int i12 = 0; i12 < pointerCount; i12++) {
                a(motionEvent, i12, iB, 0, matrix, byteBufferAllocateDirect, null);
            }
        }
        if (byteBufferAllocateDirect.position() % 288 != 0) {
            throw new AssertionError("Packet position is not on field boundary");
        }
        this.f27887a.f28049a.dispatchPointerDataPacket(byteBufferAllocateDirect, byteBufferAllocateDirect.position());
    }
}
