package io.flutter.embedding.android;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Trace;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.window.BackEvent;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import com.google.android.gms.measurement.internal.u1;
import io.flutter.embedding.engine.FlutterJNI;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Objects;
import java.util.Set;

/* JADX INFO: loaded from: classes.dex */
public final class i implements c {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public h f27910a;

    @Nullable
    @VisibleForTesting
    ViewTreeObserver.OnPreDrawListener activePreDrawListener;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public boolean f27911b;

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

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

    @Nullable
    private xc.l engineGroup;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public Integer f27915f;

    @Nullable
    private xc.c flutterEngine;

    @Nullable
    @VisibleForTesting
    FlutterView flutterView;

    @Nullable
    private io.flutter.plugin.platform.h platformPlugin;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public final f f27916g = new f(this, 0);

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

    public i(@NonNull h hVar, @Nullable xc.l lVar) {
        this.f27910a = hVar;
        this.engineGroup = lVar;
    }

    public final void a(xc.k kVar) {
        String strV = this.f27910a.v();
        if (strV == null || strV.isEmpty()) {
            strV = wc.b.a().f34274a.f27990c.f27976b;
        }
        yc.a aVar = new yc.a(strV, this.f27910a.g());
        String initialRoute = this.f27910a.getInitialRoute();
        if (initialRoute == null && (initialRoute = f(this.f27910a.getActivity().getIntent())) == null) {
            initialRoute = "/";
        }
        kVar.d(aVar);
        kVar.f(initialRoute);
        kVar.e(this.f27910a.getDartEntrypointArgs());
    }

    public final void b() {
        e();
        xc.c cVar = this.flutterEngine;
        if (cVar != null) {
            cVar.f34770j.f25105a.invokeMethod("cancelBackGesture", null);
        }
    }

    public final void c() {
        e();
        xc.c cVar = this.flutterEngine;
        if (cVar != null) {
            cVar.f34770j.f25105a.invokeMethod("commitBackGesture", null);
        }
    }

    public final void d() {
        if (!this.f27910a.s()) {
            this.f27910a.m();
            return;
        }
        throw new AssertionError("The internal FlutterEngine created by " + this.f27910a + " has been attached to by another activity. To persist a FlutterEngine beyond the ownership of this activity, explicitly create a FlutterEngine");
    }

    public final void e() {
        if (this.f27910a == null) {
            throw new IllegalStateException("Cannot execute method on a destroyed FlutterActivityAndFragmentDelegate.");
        }
    }

    public final String f(Intent intent) {
        Uri data;
        if (!this.f27910a.shouldHandleDeeplinking() || (data = intent.getData()) == null) {
            return null;
        }
        return data.toString();
    }

    public final void g(int i10, int i11, Intent intent) {
        e();
        if (this.flutterEngine != null) {
            Objects.toString(intent);
            this.flutterEngine.f34764d.onActivityResult(i10, i11, intent);
        }
    }

    @Nullable
    public xc.c getFlutterEngine() {
        return this.flutterEngine;
    }

    public final void h() {
        e();
        if (this.flutterEngine == null) {
            String cachedEngineId = this.f27910a.getCachedEngineId();
            if (cachedEngineId != null) {
                if (xc.d.f34781b == null) {
                    xc.d.f34781b = new xc.d();
                }
                xc.c cVar = xc.d.f34781b.get(cachedEngineId);
                this.flutterEngine = cVar;
                this.f27911b = true;
                if (cVar == null) {
                    throw new IllegalStateException(u1.k("The requested cached FlutterEngine did not exist in the FlutterEngineCache: '", cachedEngineId, "'"));
                }
            } else {
                h hVar = this.f27910a;
                xc.c cVarProvideFlutterEngine = hVar.provideFlutterEngine(hVar.getContext());
                this.flutterEngine = cVarProvideFlutterEngine;
                if (cVarProvideFlutterEngine != null) {
                    this.f27911b = true;
                } else {
                    String cachedEngineGroupId = this.f27910a.getCachedEngineGroupId();
                    if (cachedEngineGroupId != null) {
                        if (xc.m.f34804b == null) {
                            synchronized (xc.m.class) {
                                try {
                                    if (xc.m.f34804b == null) {
                                        xc.m.f34804b = new xc.m();
                                    }
                                } finally {
                                }
                            }
                        }
                        xc.l lVar = xc.m.f34804b.get(cachedEngineGroupId);
                        if (lVar == null) {
                            throw new IllegalStateException(u1.k("The requested cached FlutterEngineGroup did not exist in the FlutterEngineGroupCache: '", cachedEngineGroupId, "'"));
                        }
                        xc.k kVar = new xc.k(this.f27910a.getContext());
                        a(kVar);
                        this.flutterEngine = lVar.a(kVar);
                        this.f27911b = false;
                    } else {
                        xc.l lVar2 = this.engineGroup;
                        if (lVar2 == null) {
                            Context context = this.f27910a.getContext();
                            xyz.stream.utils.a0 a0VarX = this.f27910a.x();
                            lVar2 = new xc.l(context, (String[]) ((Set) a0VarX.f36098a).toArray(new String[((Set) a0VarX.f36098a).size()]));
                        }
                        xc.k kVar2 = new xc.k(this.f27910a.getContext());
                        kVar2.f34801b = false;
                        kVar2.f34802c = this.f27910a.f();
                        a(kVar2);
                        this.flutterEngine = lVar2.a(kVar2);
                        this.f27911b = false;
                    }
                }
            }
        }
        if (this.f27910a.r()) {
            this.flutterEngine.f34764d.b(this, this.f27910a.getLifecycle());
        }
        h hVar2 = this.f27910a;
        this.platformPlugin = hVar2.providePlatformPlugin(hVar2.getActivity(), this.flutterEngine);
        this.f27910a.a(this.flutterEngine);
        this.f27914e = true;
    }

    public final void i() {
        e();
        xc.c cVar = this.flutterEngine;
        if (cVar != null) {
            cVar.f34769i.f25137a.invokeMethod("popRoute", null);
        }
    }

    public final void j() {
        e();
        if (this.activePreDrawListener != null) {
            this.flutterView.getViewTreeObserver().removeOnPreDrawListener(this.activePreDrawListener);
            this.activePreDrawListener = null;
        }
        FlutterView flutterView = this.flutterView;
        if (flutterView != null) {
            flutterView.g();
            this.flutterView.f27856a.remove(this.f27916g);
        }
    }

    public final void k() {
        if (this.f27914e) {
            e();
            this.f27910a.b(this.flutterEngine);
            if (this.f27910a.r()) {
                if (this.f27910a.getActivity().isChangingConfigurations()) {
                    xc.i iVar = this.flutterEngine.f34764d;
                    if (iVar.g()) {
                        id.b.g("FlutterEngineConnectionRegistry#detachFromActivityForConfigChanges");
                        try {
                            iVar.f34794e = true;
                            Iterator it = iVar.f34793d.values().iterator();
                            while (it.hasNext()) {
                                ((bd.a) it.next()).onDetachedFromActivityForConfigChanges();
                            }
                            iVar.e();
                            Trace.endSection();
                        } finally {
                        }
                    }
                } else {
                    this.flutterEngine.f34764d.d();
                }
            }
            io.flutter.plugin.platform.h hVar = this.platformPlugin;
            if (hVar != null) {
                hVar.f28217b.setPlatformMessageHandler(null);
                this.platformPlugin = null;
            }
            this.f27910a.t();
            xc.c cVar = this.flutterEngine;
            if (cVar != null) {
                cVar.f34767g.a();
            }
            if (this.f27910a.s()) {
                xc.c cVar2 = this.flutterEngine;
                Iterator it2 = cVar2.f34779s.iterator();
                while (it2.hasNext()) {
                    ((xc.b) it2.next()).a();
                }
                xc.i iVar2 = cVar2.f34764d;
                iVar2.f();
                HashMap map = iVar2.f34790a;
                for (Class cls : new HashSet(map.keySet())) {
                    ad.c cVar3 = (ad.c) map.get(cls);
                    if (cVar3 != null) {
                        id.b.g("FlutterEngineConnectionRegistry#remove ".concat(cls.getSimpleName()));
                        try {
                            if (cVar3 instanceof bd.a) {
                                if (iVar2.g()) {
                                    ((bd.a) cVar3).onDetachedFromActivity();
                                }
                                iVar2.f34793d.remove(cls);
                            }
                            cVar3.onDetachedFromEngine(iVar2.f34792c);
                            map.remove(cls);
                            Trace.endSection();
                        } finally {
                        }
                    }
                }
                map.clear();
                while (true) {
                    io.flutter.plugin.platform.r rVar = cVar2.f34778r;
                    SparseArray sparseArray = rVar.f28240i;
                    if (sparseArray.size() <= 0) {
                        break;
                    }
                    rVar.f28251t.h(sparseArray.keyAt(0));
                }
                cVar2.f34763c.f36937a.setPlatformMessageHandler(null);
                FlutterJNI flutterJNI = cVar2.f34761a;
                flutterJNI.removeEngineLifecycleListener(cVar2.f34780t);
                flutterJNI.setDeferredComponentManager(null);
                flutterJNI.detachFromNativeAndReleaseResources();
                wc.b.a().deferredComponentManager();
                if (this.f27910a.getCachedEngineId() != null) {
                    if (xc.d.f34781b == null) {
                        xc.d.f34781b = new xc.d();
                    }
                    xc.d.f34781b.put(this.f27910a.getCachedEngineId(), null);
                }
                this.flutterEngine = null;
            }
            this.f27914e = false;
        }
    }

    public final void l(Intent intent) {
        e();
        xc.c cVar = this.flutterEngine;
        if (cVar != null) {
            cVar.f34764d.h(intent);
            String strF = f(intent);
            if (strF == null || strF.isEmpty()) {
                return;
            }
            dd.n nVar = this.flutterEngine.f34769i;
            nVar.getClass();
            HashMap map = new HashMap();
            map.put("location", strF);
            nVar.f25137a.invokeMethod("pushRouteInformation", map);
        }
    }

    public final void m() {
        e();
        this.f27910a.t();
        xc.c cVar = this.flutterEngine;
        if (cVar != null) {
            cVar.f34767g.b();
        }
    }

    public final void n() {
        e();
        if (this.flutterEngine != null) {
            io.flutter.plugin.platform.h hVar = this.platformPlugin;
            if (hVar != null) {
                hVar.d();
            }
            this.flutterEngine.f34778r.r();
        }
    }

    public final void o(int i10, String[] strArr, int[] iArr) {
        e();
        if (this.flutterEngine != null) {
            Arrays.toString(strArr);
            Arrays.toString(iArr);
            this.flutterEngine.f34764d.i(i10, strArr, iArr);
        }
    }

    @NonNull
    public View onCreateView(LayoutInflater layoutInflater, @Nullable ViewGroup viewGroup, @Nullable Bundle bundle, int i10, boolean z10) {
        e();
        RenderMode renderModeY = this.f27910a.y();
        RenderMode renderMode = RenderMode.f27880a;
        if (renderModeY == renderMode) {
            FlutterSurfaceView flutterSurfaceView = new FlutterSurfaceView(this.f27910a.getContext(), this.f27910a.C() == TransparencyMode.f27884b);
            this.f27910a.p();
            this.flutterView = new FlutterView(this.f27910a.getContext(), flutterSurfaceView);
        } else {
            FlutterTextureView flutterTextureView = new FlutterTextureView(this.f27910a.getContext());
            flutterTextureView.setOpaque(this.f27910a.C() == TransparencyMode.f27883a);
            this.f27910a.E();
            this.flutterView = new FlutterView(this.f27910a.getContext(), flutterTextureView);
        }
        this.flutterView.f27856a.add(this.f27916g);
        this.f27910a.k();
        this.flutterView.e(this.flutterEngine);
        this.flutterView.setId(i10);
        if (z10) {
            FlutterView flutterView = this.flutterView;
            if (this.f27910a.y() != renderMode) {
                throw new IllegalArgumentException("Cannot delay the first Android view draw when the render mode is not set to `RenderMode.surface`.");
            }
            if (this.activePreDrawListener != null) {
                flutterView.getViewTreeObserver().removeOnPreDrawListener(this.activePreDrawListener);
            }
            this.activePreDrawListener = new g(this, flutterView);
            flutterView.getViewTreeObserver().addOnPreDrawListener(this.activePreDrawListener);
        }
        return this.flutterView;
    }

    public void onRestoreInstanceState(@Nullable Bundle bundle) {
        byte[] byteArray;
        Bundle bundle2;
        e();
        if (bundle != null) {
            bundle2 = bundle.getBundle("plugins");
            byteArray = bundle.getByteArray("framework");
        } else {
            byteArray = null;
            bundle2 = null;
        }
        if (this.f27910a.f()) {
            dd.b0 b0Var = this.flutterEngine.f34771k;
            b0Var.f25101e = true;
            ed.t tVar = b0Var.f25100d;
            if (tVar != null) {
                tVar.success(dd.b0.a(byteArray));
                b0Var.f25100d = null;
                b0Var.f25098b = byteArray;
            } else if (b0Var.f25102f) {
                b0Var.f25099c.invokeMethod("push", dd.b0.a(byteArray), new dd.a0(b0Var, byteArray, 0));
            } else {
                b0Var.f25098b = byteArray;
            }
        }
        if (this.f27910a.r()) {
            this.flutterEngine.f34764d.onRestoreInstanceState(bundle2);
        }
    }

    public void onSaveInstanceState(@Nullable Bundle bundle) {
        e();
        if (this.f27910a.f()) {
            bundle.putByteArray("framework", this.flutterEngine.f34771k.getRestorationData());
        }
        if (this.f27910a.r()) {
            Bundle bundle2 = new Bundle();
            this.flutterEngine.f34764d.j();
            bundle.putBundle("plugins", bundle2);
        }
        if (this.f27910a.getCachedEngineId() == null || this.f27910a.s()) {
            return;
        }
        bundle.putBoolean("enableOnBackInvokedCallbackState", this.f27910a.o());
    }

    public final void p() {
        e();
        this.f27910a.t();
        xc.c cVar = this.flutterEngine;
        if (cVar != null) {
            cVar.f34767g.d();
        }
    }

    public final void q() {
        e();
        if (this.f27910a.getCachedEngineId() == null && !this.flutterEngine.f34763c.f36941e) {
            String initialRoute = this.f27910a.getInitialRoute();
            if (initialRoute == null && (initialRoute = f(this.f27910a.getActivity().getIntent())) == null) {
                initialRoute = "/";
            }
            String dartEntrypointLibraryUri = this.f27910a.getDartEntrypointLibraryUri();
            this.f27910a.g();
            this.flutterEngine.f34769i.f25137a.invokeMethod("setInitialRoute", initialRoute);
            String strV = this.f27910a.v();
            if (strV == null || strV.isEmpty()) {
                strV = wc.b.a().f34274a.f27990c.f27976b;
            }
            this.flutterEngine.f34763c.executeDartEntrypoint(dartEntrypointLibraryUri == null ? new yc.a(strV, this.f27910a.g()) : new yc.a(strV, dartEntrypointLibraryUri, this.f27910a.g()), this.f27910a.getDartEntrypointArgs());
        }
        Integer num = this.f27915f;
        if (num != null) {
            this.flutterView.setVisibility(num.intValue());
        }
    }

    public final void r() {
        e();
        this.f27910a.t();
        xc.c cVar = this.flutterEngine;
        if (cVar != null) {
            cVar.f34767g.c();
        }
        this.f27915f = Integer.valueOf(this.flutterView.getVisibility());
        this.flutterView.setVisibility(8);
        xc.c cVar2 = this.flutterEngine;
        if (cVar2 != null) {
            cVar2.f34762b.e(40);
        }
    }

    public final void s(int i10) {
        e();
        xc.c cVar = this.flutterEngine;
        if (cVar != null) {
            if (this.f27913d && i10 >= 10) {
                FlutterJNI flutterJNI = cVar.f34763c.f36937a;
                if (flutterJNI.isAttached()) {
                    flutterJNI.notifyLowMemoryWarning();
                }
                e.y yVar = this.flutterEngine.f34776p;
                yVar.getClass();
                HashMap map = new HashMap(1);
                map.put("type", "memoryPressure");
                ((ed.e) yVar.f25365a).send(map);
            }
            this.flutterEngine.f34762b.e(i10);
            io.flutter.plugin.platform.r rVar = this.flutterEngine.f34778r;
            if (i10 < 40) {
                rVar.getClass();
                return;
            }
            Iterator it = rVar.f28238g.values().iterator();
            while (it.hasNext()) {
                ((io.flutter.plugin.platform.c0) it.next()).f28207h.setSurface(null);
            }
        }
    }

    public final void t() {
        e();
        xc.c cVar = this.flutterEngine;
        if (cVar != null) {
            cVar.f34764d.k();
        }
    }

    public final void u(boolean z10) {
        e();
        this.f27910a.t();
        xc.c cVar = this.flutterEngine;
        if (cVar != null) {
            io.flutter.embedding.engine.systemchannels.a aVar = cVar.f34767g;
            if (z10) {
                aVar.e(aVar.f28112a, true);
            } else {
                aVar.e(aVar.f28112a, false);
            }
        }
    }

    public final void v() {
        this.f27910a = null;
        this.flutterEngine = null;
        this.flutterView = null;
        this.platformPlugin = null;
    }

    public final void w(BackEvent backEvent) {
        e();
        xc.c cVar = this.flutterEngine;
        if (cVar != null) {
            dd.d dVar = cVar.f34770j;
            dVar.getClass();
            dVar.f25105a.invokeMethod("startBackGesture", dd.d.a(backEvent));
        }
    }

    public final void x(BackEvent backEvent) {
        e();
        xc.c cVar = this.flutterEngine;
        if (cVar != null) {
            dd.d dVar = cVar.f34770j;
            dVar.getClass();
            dVar.f25105a.invokeMethod("updateBackGestureProgress", dd.d.a(backEvent));
        }
    }
}
