package xc;

import android.app.Activity;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.ContentProvider;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Trace;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.google.android.gms.measurement.internal.u1;
import ed.x;
import io.flutter.plugin.platform.r;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Objects;
import xyz.stream.api.a0;

/* JADX INFO: loaded from: classes.dex */
public final class i implements ad.d, bd.b, cd.a {

    @Nullable
    private e activityPluginBinding;

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

    @Nullable
    private BroadcastReceiver broadcastReceiver;

    @Nullable
    private f broadcastReceiverPluginBinding;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public final ad.b f34792c;

    @Nullable
    private ContentProvider contentProvider;

    @Nullable
    private g contentProviderPluginBinding;

    @Nullable
    private io.flutter.embedding.android.c exclusiveActivity;

    @Nullable
    private Service service;

    @Nullable
    private h servicePluginBinding;

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

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

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

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

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

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

    public i(@NonNull Context context, @NonNull c cVar, @NonNull io.flutter.embedding.engine.loader.f fVar, @Nullable l lVar) {
        this.f34791b = cVar;
        this.f34792c = new ad.b(context, cVar, cVar.f34763c, cVar.f34762b, cVar.f34778r.f28232a, new a0(fVar), lVar);
    }

    public final void a(ad.c cVar) {
        id.b.g("FlutterEngineConnectionRegistry#add ".concat(cVar.getClass().getSimpleName()));
        try {
            Class<?> cls = cVar.getClass();
            HashMap map = this.f34790a;
            if (map.containsKey(cls)) {
                cVar.toString();
                Objects.toString(this.f34791b);
                Trace.endSection();
                return;
            }
            cVar.toString();
            map.put(cVar.getClass(), cVar);
            cVar.onAttachedToEngine(this.f34792c);
            if (cVar instanceof bd.a) {
                bd.a aVar = (bd.a) cVar;
                this.f34793d.put(cVar.getClass(), aVar);
                if (g()) {
                    aVar.onAttachedToActivity(this.activityPluginBinding);
                }
            }
            Trace.endSection();
        } catch (Throwable th) {
            try {
                Trace.endSection();
            } catch (Throwable th2) {
                th.addSuppressed(th2);
            }
            throw th;
        }
    }

    @Override // cd.a
    public void attachToService(@NonNull Service service, @Nullable androidx.lifecycle.l lVar, boolean z10) {
        id.b.g("FlutterEngineConnectionRegistry#attachToService");
        try {
            f();
            this.service = service;
            this.servicePluginBinding = new h(service, lVar);
            Iterator it = this.f34795f.values().iterator();
            if (it.hasNext()) {
                u1.x(it.next());
                throw null;
            }
            Trace.endSection();
        } catch (Throwable th) {
            try {
                Trace.endSection();
            } catch (Throwable th2) {
                th.addSuppressed(th2);
            }
            throw th;
        }
    }

    public final void b(io.flutter.embedding.android.c cVar, androidx.lifecycle.l lVar) {
        id.b.g("FlutterEngineConnectionRegistry#attachToActivity");
        try {
            io.flutter.embedding.android.c cVar2 = this.exclusiveActivity;
            if (cVar2 != null) {
                ((io.flutter.embedding.android.i) cVar2).d();
            }
            f();
            this.exclusiveActivity = cVar;
            Activity activity = ((io.flutter.embedding.android.i) cVar).f27910a.getActivity();
            if (activity == null) {
                throw new AssertionError("FlutterActivityAndFragmentDelegate's getAppComponent should only be queried after onAttach, when the host's activity should always be non-null");
            }
            c(activity, lVar);
            Trace.endSection();
        } catch (Throwable th) {
            try {
                Trace.endSection();
            } catch (Throwable th2) {
                th.addSuppressed(th2);
            }
            throw th;
        }
    }

    public final void c(Activity activity, androidx.lifecycle.l lVar) {
        this.activityPluginBinding = new e(activity, lVar);
        boolean booleanExtra = activity.getIntent() != null ? activity.getIntent().getBooleanExtra("enable-software-rendering", false) : false;
        c cVar = this.f34791b;
        r rVar = cVar.f34778r;
        rVar.f28250s = booleanExtra;
        rVar.attach(activity, cVar.f34762b, cVar.f34763c);
        for (bd.a aVar : this.f34793d.values()) {
            if (this.f34794e) {
                aVar.onReattachedToActivityForConfigChanges(this.activityPluginBinding);
            } else {
                aVar.onAttachedToActivity(this.activityPluginBinding);
            }
        }
        this.f34794e = false;
    }

    public final void d() {
        if (g()) {
            id.b.g("FlutterEngineConnectionRegistry#detachFromActivity");
            try {
                Iterator it = this.f34793d.values().iterator();
                while (it.hasNext()) {
                    ((bd.a) it.next()).onDetachedFromActivity();
                }
                e();
                Trace.endSection();
            } catch (Throwable th) {
                try {
                    Trace.endSection();
                } catch (Throwable th2) {
                    th.addSuppressed(th2);
                }
                throw th;
            }
        }
    }

    public final void e() {
        this.f34791b.f34778r.l();
        this.exclusiveActivity = null;
        this.activityPluginBinding = null;
    }

    public final void f() {
        if (g()) {
            d();
            return;
        }
        Service service = this.service;
        if (service != null) {
            if (service != null) {
                id.b.g("FlutterEngineConnectionRegistry#detachFromService");
                try {
                    Iterator it = this.f34795f.values().iterator();
                    if (it.hasNext()) {
                        u1.x(it.next());
                        throw null;
                    }
                    this.service = null;
                    this.servicePluginBinding = null;
                    Trace.endSection();
                    return;
                } finally {
                }
            }
            return;
        }
        BroadcastReceiver broadcastReceiver = this.broadcastReceiver;
        if (broadcastReceiver != null) {
            if (broadcastReceiver != null) {
                id.b.g("FlutterEngineConnectionRegistry#detachFromBroadcastReceiver");
                try {
                    Iterator it2 = this.f34796g.values().iterator();
                    if (it2.hasNext()) {
                        u1.x(it2.next());
                        throw null;
                    }
                    Trace.endSection();
                    return;
                } finally {
                }
            }
            return;
        }
        ContentProvider contentProvider = this.contentProvider;
        if (contentProvider == null || contentProvider == null) {
            return;
        }
        id.b.g("FlutterEngineConnectionRegistry#detachFromContentProvider");
        try {
            Iterator it3 = this.f34797h.values().iterator();
            if (it3.hasNext()) {
                u1.x(it3.next());
                throw null;
            }
            Trace.endSection();
        } finally {
            try {
                Trace.endSection();
            } catch (Throwable th) {
                th.addSuppressed(th);
            }
        }
    }

    public final boolean g() {
        return this.exclusiveActivity != null;
    }

    @Override // ad.d
    public final ad.c get(Class cls) {
        return (ad.c) this.f34790a.get(cls);
    }

    public final void h(Intent intent) {
        if (g()) {
            id.b.g("FlutterEngineConnectionRegistry#onNewIntent");
            try {
                this.activityPluginBinding.onNewIntent(intent);
                Trace.endSection();
            } catch (Throwable th) {
                try {
                    Trace.endSection();
                } catch (Throwable th2) {
                    th.addSuppressed(th2);
                }
                throw th;
            }
        }
    }

    public final boolean i(int i10, String[] strArr, int[] iArr) {
        if (!g()) {
            return false;
        }
        id.b.g("FlutterEngineConnectionRegistry#onRequestPermissionsResult");
        try {
            Iterator it = this.activityPluginBinding.f34785c.iterator();
            while (true) {
                boolean z10 = false;
                while (it.hasNext()) {
                    if (((x) it.next()).a(i10, strArr, iArr) || z10) {
                        z10 = true;
                    }
                }
                Trace.endSection();
                return z10;
            }
        } catch (Throwable th) {
            try {
                Trace.endSection();
            } catch (Throwable th2) {
                th.addSuppressed(th2);
            }
            throw th;
        }
    }

    public final void j() {
        if (g()) {
            id.b.g("FlutterEngineConnectionRegistry#onSaveInstanceState");
            try {
                Iterator it = this.activityPluginBinding.f34789g.iterator();
                if (it.hasNext()) {
                    u1.x(it.next());
                    throw null;
                }
                Trace.endSection();
            } catch (Throwable th) {
                try {
                    Trace.endSection();
                } catch (Throwable th2) {
                    th.addSuppressed(th2);
                }
                throw th;
            }
        }
    }

    public final void k() {
        if (g()) {
            id.b.g("FlutterEngineConnectionRegistry#onUserLeaveHint");
            try {
                Iterator it = this.activityPluginBinding.f34788f.iterator();
                if (it.hasNext()) {
                    u1.x(it.next());
                    throw null;
                }
                Trace.endSection();
            } catch (Throwable th) {
                try {
                    Trace.endSection();
                } catch (Throwable th2) {
                    th.addSuppressed(th2);
                }
                throw th;
            }
        }
    }

    @Override // bd.b
    public boolean onActivityResult(int i10, int i11, @Nullable Intent intent) {
        if (!g()) {
            return false;
        }
        id.b.g("FlutterEngineConnectionRegistry#onActivityResult");
        try {
            boolean zOnActivityResult = this.activityPluginBinding.onActivityResult(i10, i11, intent);
            Trace.endSection();
            return zOnActivityResult;
        } catch (Throwable th) {
            try {
                Trace.endSection();
            } catch (Throwable th2) {
                th.addSuppressed(th2);
            }
            throw th;
        }
    }

    @Override // bd.b
    public void onRestoreInstanceState(@Nullable Bundle bundle) {
        if (g()) {
            id.b.g("FlutterEngineConnectionRegistry#onRestoreInstanceState");
            try {
                this.activityPluginBinding.onRestoreInstanceState(bundle);
                Trace.endSection();
            } catch (Throwable th) {
                try {
                    Trace.endSection();
                } catch (Throwable th2) {
                    th.addSuppressed(th2);
                }
                throw th;
            }
        }
    }
}
