package io.flutter.plugin.platform;

import android.content.Context;
import android.content.MutableContextWrapper;
import android.graphics.Canvas;
import android.graphics.PorterDuff;
import android.hardware.display.DisplayManager;
import android.hardware.display.VirtualDisplay;
import android.os.Build;
import android.util.DisplayMetrics;
import android.util.SparseArray;
import android.view.MotionEvent;
import android.view.Surface;
import android.view.SurfaceView;
import android.view.View;
import android.widget.FrameLayout;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.google.android.gms.internal.measurement.s6;
import com.google.android.gms.measurement.internal.u1;
import com.google.firebase.messaging.Constants$ScionAnalytics$MessageType;
import io.flutter.embedding.android.FlutterView;
import io.flutter.embedding.android.g0;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import xyz.stream.utils.f0;
import xyz.stream.utils.j0;

/* JADX INFO: loaded from: classes.dex */
public final class r implements n {

    /* JADX INFO: renamed from: u, reason: collision with root package name */
    public static final Class[] f28231u = {SurfaceView.class};

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public io.flutter.embedding.android.b f28233b;

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

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

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public dd.x f28236e;

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

    @Nullable
    private io.flutter.plugin.editing.m textInputPlugin;

    @Nullable
    private io.flutter.view.p textureRegistry;

    /* JADX INFO: renamed from: m, reason: collision with root package name */
    public int f28244m = 0;

    /* JADX INFO: renamed from: n, reason: collision with root package name */
    public boolean f28245n = false;

    /* JADX INFO: renamed from: o, reason: collision with root package name */
    public boolean f28246o = true;

    /* JADX INFO: renamed from: s, reason: collision with root package name */
    public boolean f28250s = false;

    /* JADX INFO: renamed from: t, reason: collision with root package name */
    public final f0 f28251t = new f0(this, 23);

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final s6 f28232a = new s6(1);

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

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

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

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    public final SparseArray f28242k = new SparseArray();

    /* JADX INFO: renamed from: p, reason: collision with root package name */
    public final HashSet f28247p = new HashSet();

    /* JADX INFO: renamed from: q, reason: collision with root package name */
    public final HashSet f28248q = new HashSet();

    /* JADX INFO: renamed from: l, reason: collision with root package name */
    public final SparseArray f28243l = new SparseArray();

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    public final SparseArray f28240i = new SparseArray();

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    public final SparseArray f28241j = new SparseArray();

    public r() {
        if (g0.f27906c == null) {
            g0.f27906c = new g0();
        }
        this.f28249r = g0.f27906c;
    }

    public static void a(r rVar, dd.t tVar, boolean z10) {
        if (!z10) {
            io.flutter.plugin.editing.m mVar = rVar.textInputPlugin;
            if (mVar != null) {
                mVar.f(tVar.f25147a);
                return;
            }
            return;
        }
        dd.x xVar = rVar.f28236e;
        int i10 = tVar.f25147a;
        ed.u uVar = xVar.f25174a;
        if (uVar == null) {
            return;
        }
        uVar.invokeMethod("viewFocused", Integer.valueOf(i10));
    }

    public static void b(r rVar, int i10, boolean z10) {
        if (z10) {
            ed.u uVar = rVar.f28236e.f25174a;
            if (uVar == null) {
                return;
            }
            uVar.invokeMethod("viewFocused", Integer.valueOf(i10));
            return;
        }
        io.flutter.plugin.editing.m mVar = rVar.textInputPlugin;
        if (mVar != null) {
            mVar.f(i10);
        }
    }

    public static void c(r rVar, dd.t tVar) {
        rVar.getClass();
        int i10 = tVar.f25153g;
        if (i10 != 0 && i10 != 1) {
            throw new IllegalStateException(u.a.b(defpackage.e.q("Trying to create a view with unknown direction value: ", i10, "(view id: "), tVar.f25147a, ")"));
        }
    }

    public static void d(r rVar, c0 c0Var) {
        io.flutter.plugin.editing.m mVar = rVar.textInputPlugin;
        if (mVar == null) {
            return;
        }
        mVar.j();
        SingleViewPresentation singleViewPresentation = c0Var.f28200a;
        if (singleViewPresentation == null || singleViewPresentation.getView() == null) {
            return;
        }
        c0Var.f28200a.getView().onInputConnectionLocked();
    }

    public static void e(r rVar, c0 c0Var) {
        io.flutter.plugin.editing.m mVar = rVar.textInputPlugin;
        if (mVar == null) {
            return;
        }
        mVar.o();
        SingleViewPresentation singleViewPresentation = c0Var.f28200a;
        if (singleViewPresentation == null || singleViewPresentation.getView() == null) {
            return;
        }
        c0Var.f28200a.getView().onInputConnectionUnlocked();
    }

    /* JADX WARN: Removed duplicated region for block: B:8:0x0063  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public static long g(r rVar, i iVar, dd.t tVar) {
        c0 c0Var;
        rVar.getClass();
        n(20);
        l lVarQ = q(rVar.textureRegistry);
        int iT = rVar.t(tVar.f25149c);
        int iT2 = rVar.t(tVar.f25150d);
        Context context = rVar.f28234c;
        a aVar = rVar.f28237f;
        int i10 = tVar.f25147a;
        o oVar = new o(rVar, tVar, 1);
        z zVar = c0.f28199i;
        if (iT == 0 || iT2 == 0) {
            c0Var = null;
        } else {
            DisplayManager displayManager = (DisplayManager) context.getSystemService(Constants$ScionAnalytics$MessageType.DISPLAY_NOTIFICATION);
            DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
            lVarQ.i(iT, iT2);
            VirtualDisplay virtualDisplayCreateVirtualDisplay = displayManager.createVirtualDisplay(u1.g("flutter-vd#", i10), iT, iT2, displayMetrics.densityDpi, lVarQ.getSurface(), 0, c0.f28199i, null);
            if (virtualDisplayCreateVirtualDisplay != null) {
                c0Var = new c0(context, aVar, virtualDisplayCreateVirtualDisplay, iVar, lVarQ, oVar, i10);
            }
        }
        int i11 = tVar.f25147a;
        if (c0Var != null) {
            rVar.f28238g.put(Integer.valueOf(i11), c0Var);
            View view = iVar.getView();
            rVar.f28239h.put(view.getContext(), view);
            return lVarQ.getId();
        }
        throw new IllegalStateException("Failed creating virtual display for a " + tVar.f25148b + " with id: " + i11);
    }

    public static void n(int i10) {
        int i11 = Build.VERSION.SDK_INT;
        if (i11 < i10) {
            throw new IllegalStateException(defpackage.e.f("Trying to use platform views with API ", i11, ", required API level is: ", i10));
        }
    }

    public static l q(io.flutter.view.p pVar) {
        int i10 = Build.VERSION.SDK_INT;
        return i10 >= 29 ? new j0(((io.flutter.embedding.engine.renderer.j) pVar).c(), 26) : i10 >= 29 ? new c(((io.flutter.embedding.engine.renderer.j) pVar).b()) : new y(((io.flutter.embedding.engine.renderer.j) pVar).d());
    }

    public void attach(@Nullable Context context, @NonNull io.flutter.view.p pVar, @NonNull yc.d dVar) {
        if (this.f28234c != null) {
            throw new AssertionError("A PlatformViewsController can only be attached to a single output target.\nattach was called while the PlatformViewsController was already attached.");
        }
        this.f28234c = context;
        this.textureRegistry = pVar;
        dd.x xVar = new dd.x(dVar);
        this.f28236e = xVar;
        xVar.setPlatformViewsHandler(this.f28251t);
    }

    public boolean checkInputConnectionProxy(@Nullable View view) {
        if (view == null) {
            return false;
        }
        HashMap map = this.f28239h;
        if (!map.containsKey(view.getContext())) {
            return false;
        }
        View view2 = (View) map.get(view.getContext());
        if (view2 == view) {
            return true;
        }
        return view2.checkInputConnectionProxy(view);
    }

    @Override // io.flutter.plugin.platform.n
    @Nullable
    public View getPlatformViewById(int i10) {
        if (u(i10)) {
            return ((c0) this.f28238g.get(Integer.valueOf(i10))).b();
        }
        i iVar = (i) this.f28240i.get(i10);
        if (iVar == null) {
            return null;
        }
        return iVar.getView();
    }

    public final void h(io.flutter.plugin.editing.m mVar) {
        this.textInputPlugin = mVar;
    }

    public final long i(i iVar, dd.t tVar) {
        PlatformViewWrapper platformViewWrapper;
        long j10;
        n(23);
        int iT = t(tVar.f25149c);
        int iT2 = t(tVar.f25150d);
        int i10 = 0;
        if (this.f28250s) {
            platformViewWrapper = new PlatformViewWrapper(this.f28234c);
            j10 = -1;
        } else {
            l lVarQ = q(this.textureRegistry);
            PlatformViewWrapper platformViewWrapper2 = new PlatformViewWrapper(this.f28234c);
            platformViewWrapper2.f28185f = lVarQ;
            Surface surface = lVarQ.getSurface();
            if (surface != null) {
                Canvas canvasLockHardwareCanvas = surface.lockHardwareCanvas();
                try {
                    canvasLockHardwareCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
                } finally {
                    surface.unlockCanvasAndPost(canvasLockHardwareCanvas);
                }
            }
            long id2 = lVarQ.getId();
            platformViewWrapper = platformViewWrapper2;
            j10 = id2;
        }
        platformViewWrapper.setTouchProcessor(this.f28233b);
        l lVar = platformViewWrapper.f28185f;
        if (lVar != null) {
            lVar.i(iT, iT2);
        }
        FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(iT, iT2);
        int iT3 = t(tVar.f25151e);
        int iT4 = t(tVar.f25152f);
        layoutParams.topMargin = iT3;
        layoutParams.leftMargin = iT4;
        platformViewWrapper.setLayoutParams(layoutParams);
        View view = iVar.getView();
        view.setLayoutParams(new FrameLayout.LayoutParams(iT, iT2));
        view.setImportantForAccessibility(4);
        platformViewWrapper.addView(view);
        platformViewWrapper.setOnDescendantFocusChangeListener(new o(this, tVar, i10));
        this.f28235d.addView(platformViewWrapper);
        this.f28243l.append(tVar.f25147a, platformViewWrapper);
        FlutterView flutterView = this.f28235d;
        if (flutterView != null) {
            iVar.onFlutterViewAttached(flutterView);
        }
        return j10;
    }

    public final i j(dd.t tVar, boolean z10) {
        HashMap map = this.f28232a.f20410a;
        String str = tVar.f25148b;
        j jVar = (j) map.get(str);
        if (jVar == null) {
            throw new IllegalStateException("Trying to create a platform view of unregistered type: " + str);
        }
        Object objDecodeMessage = tVar.params != null ? jVar.getCreateArgsCodec().decodeMessage(tVar.params) : null;
        Context mutableContextWrapper = z10 ? new MutableContextWrapper(this.f28234c) : this.f28234c;
        int i10 = tVar.f25147a;
        i iVarCreate = jVar.create(mutableContextWrapper, i10, objDecodeMessage);
        View view = iVarCreate.getView();
        if (view == null) {
            throw new IllegalStateException("PlatformView#getView() returned null, but an Android view reference was expected.");
        }
        view.setLayoutDirection(tVar.f25153g);
        this.f28240i.put(i10, iVarCreate);
        FlutterView flutterView = this.f28235d;
        if (flutterView != null) {
            iVarCreate.onFlutterViewAttached(flutterView);
        }
        return iVarCreate;
    }

    public final void k() {
        int i10 = 0;
        while (true) {
            SparseArray sparseArray = this.f28242k;
            if (i10 >= sparseArray.size()) {
                return;
            }
            PlatformOverlayView platformOverlayView = (PlatformOverlayView) sparseArray.valueAt(i10);
            platformOverlayView.b();
            platformOverlayView.f27844a.close();
            i10++;
        }
    }

    public final void l() {
        dd.x xVar = this.f28236e;
        if (xVar != null) {
            xVar.setPlatformViewsHandler(null);
        }
        k();
        this.f28236e = null;
        this.f28234c = null;
        this.textureRegistry = null;
    }

    public final void m() {
        this.textInputPlugin = null;
    }

    public final void o(boolean z10) {
        int i10 = 0;
        while (true) {
            SparseArray sparseArray = this.f28242k;
            if (i10 >= sparseArray.size()) {
                break;
            }
            int iKeyAt = sparseArray.keyAt(i10);
            PlatformOverlayView platformOverlayView = (PlatformOverlayView) sparseArray.valueAt(i10);
            if (this.f28247p.contains(Integer.valueOf(iKeyAt))) {
                this.f28235d.d(platformOverlayView);
                z10 &= platformOverlayView.c();
            } else {
                if (!this.f28245n) {
                    platformOverlayView.b();
                }
                platformOverlayView.setVisibility(8);
                this.f28235d.removeView(platformOverlayView);
            }
            i10++;
        }
        int i11 = 0;
        while (true) {
            SparseArray sparseArray2 = this.f28241j;
            if (i11 >= sparseArray2.size()) {
                return;
            }
            int iKeyAt2 = sparseArray2.keyAt(i11);
            View view = (View) sparseArray2.get(iKeyAt2);
            if (!this.f28248q.contains(Integer.valueOf(iKeyAt2)) || (!z10 && this.f28246o)) {
                view.setVisibility(8);
            } else {
                view.setVisibility(0);
            }
            i11++;
        }
    }

    public final float p() {
        return this.f28234c.getResources().getDisplayMetrics().density;
    }

    public final void r() {
        for (c0 c0Var : this.f28238g.values()) {
            l lVar = c0Var.f28205f;
            int height = 0;
            int width = lVar != null ? lVar.getWidth() : 0;
            l lVar2 = c0Var.f28205f;
            if (lVar2 != null) {
                height = lVar2.getHeight();
            }
            int i10 = height;
            boolean zIsFocused = c0Var.b().isFocused();
            w wVarDetachState = c0Var.f28200a.detachState();
            c0Var.f28207h.setSurface(null);
            c0Var.f28207h.release();
            c0Var.f28207h = ((DisplayManager) c0Var.f28201b.getSystemService(Constants$ScionAnalytics$MessageType.DISPLAY_NOTIFICATION)).createVirtualDisplay("flutter-vd#" + c0Var.f28204e, width, i10, c0Var.f28203d, lVar2.getSurface(), 0, c0.f28199i, null);
            SingleViewPresentation singleViewPresentation = new SingleViewPresentation(c0Var.f28201b, c0Var.f28207h.getDisplay(), c0Var.f28202c, wVarDetachState, c0Var.f28206g, zIsFocused);
            singleViewPresentation.show();
            c0Var.f28200a.cancel();
            c0Var.f28200a = singleViewPresentation;
        }
    }

    public final MotionEvent s(float f10, dd.v vVar, boolean z10) {
        MotionEvent motionEventPop = this.f28249r.pop(new io.flutter.embedding.android.f0(vVar.f25173p));
        List<List> list = (List) vVar.f25164g;
        ArrayList arrayList = new ArrayList();
        for (List list2 : list) {
            MotionEvent.PointerCoords pointerCoords = new MotionEvent.PointerCoords();
            pointerCoords.orientation = (float) ((Double) list2.get(0)).doubleValue();
            pointerCoords.pressure = (float) ((Double) list2.get(1)).doubleValue();
            pointerCoords.size = (float) ((Double) list2.get(2)).doubleValue();
            double d10 = f10;
            pointerCoords.toolMajor = (float) (((Double) list2.get(3)).doubleValue() * d10);
            pointerCoords.toolMinor = (float) (((Double) list2.get(4)).doubleValue() * d10);
            pointerCoords.touchMajor = (float) (((Double) list2.get(5)).doubleValue() * d10);
            pointerCoords.touchMinor = (float) (((Double) list2.get(6)).doubleValue() * d10);
            pointerCoords.x = (float) (((Double) list2.get(7)).doubleValue() * d10);
            pointerCoords.y = (float) (((Double) list2.get(8)).doubleValue() * d10);
            arrayList.add(pointerCoords);
        }
        int i10 = vVar.f25162e;
        MotionEvent.PointerCoords[] pointerCoordsArr = (MotionEvent.PointerCoords[]) arrayList.toArray(new MotionEvent.PointerCoords[i10]);
        if (!z10 && motionEventPop != null) {
            if (pointerCoordsArr.length >= 1) {
                motionEventPop.offsetLocation(pointerCoordsArr[0].x - motionEventPop.getX(), pointerCoordsArr[0].y - motionEventPop.getY());
            }
            return motionEventPop;
        }
        List<List> list3 = (List) vVar.f25163f;
        ArrayList arrayList2 = new ArrayList();
        for (List list4 : list3) {
            MotionEvent.PointerProperties pointerProperties = new MotionEvent.PointerProperties();
            pointerProperties.id = ((Integer) list4.get(0)).intValue();
            pointerProperties.toolType = ((Integer) list4.get(1)).intValue();
            arrayList2.add(pointerProperties);
        }
        return MotionEvent.obtain(vVar.f25159b.longValue(), vVar.f25160c.longValue(), vVar.f25161d, vVar.f25162e, (MotionEvent.PointerProperties[]) arrayList2.toArray(new MotionEvent.PointerProperties[i10]), pointerCoordsArr, vVar.f25165h, vVar.f25166i, vVar.f25167j, vVar.f25168k, vVar.f25169l, vVar.f25170m, vVar.f25171n, vVar.f25172o);
    }

    public final int t(double d10) {
        return (int) Math.round(d10 * ((double) p()));
    }

    public final boolean u(int i10) {
        return this.f28238g.containsKey(Integer.valueOf(i10));
    }
}
