package e4;

import android.app.Activity;
import android.content.Context;
import android.graphics.Rect;
import android.os.IBinder;
import androidx.window.layout.adapter.sidecar.DistinctElementSidecarCallback;
import androidx.window.layout.n;
import androidx.window.sidecar.SidecarDeviceState;
import androidx.window.sidecar.SidecarDisplayFeature;
import androidx.window.sidecar.SidecarInterface;
import androidx.window.sidecar.SidecarWindowLayoutInfo;
import e4.e;
import e4.f;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;
import nb.g;
import org.jetbrains.annotations.NotNull;

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

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

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final androidx.window.layout.adapter.sidecar.a f25457b;

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

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public final LinkedHashMap f25459d;

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

    public g(Context context) {
        nb.g.i(context, "context");
        SidecarInterface sidecarInterfaceB = e.b(context);
        androidx.window.layout.adapter.sidecar.a aVar = new androidx.window.layout.adapter.sidecar.a();
        this.f25456a = sidecarInterfaceB;
        this.f25457b = aVar;
        this.f25458c = new LinkedHashMap();
        this.f25459d = new LinkedHashMap();
    }

    public final SidecarInterface d() {
        return this.f25456a;
    }

    public final n e(Activity activity) {
        SidecarDeviceState sidecarDeviceState;
        IBinder iBinderA = e.a(activity);
        if (iBinderA == null) {
            return new n(hb.a.W());
        }
        SidecarInterface sidecarInterface = this.f25456a;
        SidecarWindowLayoutInfo windowLayoutInfo = sidecarInterface != null ? sidecarInterface.getWindowLayoutInfo(iBinderA) : null;
        if (sidecarInterface == null || (sidecarDeviceState = sidecarInterface.getDeviceState()) == null) {
            sidecarDeviceState = new SidecarDeviceState();
        }
        return this.f25457b.e(windowLayoutInfo, sidecarDeviceState);
    }

    /* JADX WARN: Multi-variable type inference failed */
    public final void f(Activity activity) {
        nb.g.i(activity, "activity");
        IBinder iBinderA = e.a(activity);
        if (iBinderA == null) {
            return;
        }
        SidecarInterface sidecarInterface = this.f25456a;
        if (sidecarInterface != null) {
            sidecarInterface.onWindowLayoutChangeListenerRemoved(iBinderA);
        }
        LinkedHashMap linkedHashMap = this.f25459d;
        p0.a aVar = (p0.a) linkedHashMap.get(activity);
        if (aVar != null) {
            if (activity instanceof g0.j) {
                ((g0.j) activity).removeOnConfigurationChangedListener(aVar);
            }
            linkedHashMap.remove(activity);
        }
        f fVar = this.f25460e;
        if (fVar != null) {
            fVar.b(activity);
        }
        LinkedHashMap linkedHashMap2 = this.f25458c;
        boolean z10 = linkedHashMap2.size() == 1;
        linkedHashMap2.remove(iBinderA);
        if (!z10 || sidecarInterface == null) {
            return;
        }
        sidecarInterface.onDeviceStateListenersChanged(true);
    }

    /* JADX WARN: Multi-variable type inference failed */
    public final void g(IBinder iBinder, final Activity activity) {
        LinkedHashMap linkedHashMap = this.f25458c;
        linkedHashMap.put(iBinder, activity);
        SidecarInterface sidecarInterface = this.f25456a;
        if (sidecarInterface != null) {
            sidecarInterface.onWindowLayoutChangeListenerAdded(iBinder);
        }
        if (linkedHashMap.size() == 1 && sidecarInterface != null) {
            sidecarInterface.onDeviceStateListenersChanged(false);
        }
        f fVar = this.f25460e;
        if (fVar != null) {
            fVar.a(activity, e(activity));
        }
        LinkedHashMap linkedHashMap2 = this.f25459d;
        if (linkedHashMap2.get(activity) == null && (activity instanceof g0.j)) {
            p0.a aVar = new p0.a() { // from class: e4.d
                @Override // p0.a
                public final void accept(Object obj) {
                    g gVar = this.f25451a;
                    nb.g.i(gVar, "this$0");
                    Activity activity2 = activity;
                    nb.g.i(activity2, "$activity");
                    f fVar2 = gVar.f25460e;
                    if (fVar2 != null) {
                        fVar2.a(activity2, gVar.e(activity2));
                    }
                }
            };
            linkedHashMap2.put(activity, aVar);
            ((g0.j) activity).addOnConfigurationChangedListener(aVar);
        }
    }

    public final void h(h hVar) {
        this.f25460e = new f(hVar);
        SidecarInterface sidecarInterface = this.f25456a;
        if (sidecarInterface != null) {
            sidecarInterface.setSidecarCallback(new DistinctElementSidecarCallback(this.f25457b, new SidecarInterface.SidecarCallback() { // from class: androidx.window.layout.adapter.sidecar.SidecarCompat$TranslatingCallback
                public void onDeviceStateChanged(@NotNull SidecarDeviceState sidecarDeviceState) {
                    SidecarInterface sidecarInterfaceD;
                    g.i(sidecarDeviceState, "newDeviceState");
                    Collection<Activity> collectionValues = this.f4460a.f25458c.values();
                    e4.g gVar = this.f4460a;
                    for (Activity activity : collectionValues) {
                        IBinder iBinderA = e.a(activity);
                        SidecarWindowLayoutInfo windowLayoutInfo = null;
                        if (iBinderA != null && (sidecarInterfaceD = gVar.d()) != null) {
                            windowLayoutInfo = sidecarInterfaceD.getWindowLayoutInfo(iBinderA);
                        }
                        f fVar = gVar.f25460e;
                        if (fVar != null) {
                            fVar.a(activity, gVar.f25457b.e(windowLayoutInfo, sidecarDeviceState));
                        }
                    }
                }

                public void onWindowLayoutChanged(@NotNull IBinder iBinder, @NotNull SidecarWindowLayoutInfo sidecarWindowLayoutInfo) {
                    SidecarDeviceState sidecarDeviceState;
                    g.i(iBinder, "windowToken");
                    g.i(sidecarWindowLayoutInfo, "newLayout");
                    Activity activity = (Activity) this.f4460a.f25458c.get(iBinder);
                    if (activity == null) {
                        return;
                    }
                    a aVar = this.f4460a.f25457b;
                    SidecarInterface sidecarInterfaceD = this.f4460a.d();
                    if (sidecarInterfaceD == null || (sidecarDeviceState = sidecarInterfaceD.getDeviceState()) == null) {
                        sidecarDeviceState = new SidecarDeviceState();
                    }
                    n nVarE = aVar.e(sidecarWindowLayoutInfo, sidecarDeviceState);
                    f fVar = this.f4460a.f25460e;
                    if (fVar != null) {
                        fVar.a(activity, nVarE);
                    }
                }
            }));
        }
    }

    public final boolean i() {
        Class<?> cls;
        Class<?> cls2;
        Class<?> cls3;
        Class<?> cls4;
        try {
            SidecarInterface sidecarInterface = this.f25456a;
            Method method = (sidecarInterface == null || (cls4 = sidecarInterface.getClass()) == null) ? null : cls4.getMethod("setSidecarCallback", SidecarInterface.SidecarCallback.class);
            Class<?> returnType = method != null ? method.getReturnType() : null;
            Class cls5 = Void.TYPE;
            if (!nb.g.c(returnType, cls5)) {
                throw new NoSuchMethodException("Illegal return type for 'setSidecarCallback': " + returnType);
            }
            SidecarInterface sidecarInterface2 = this.f25456a;
            if (sidecarInterface2 != null) {
                sidecarInterface2.getDeviceState();
            }
            SidecarInterface sidecarInterface3 = this.f25456a;
            if (sidecarInterface3 != null) {
                sidecarInterface3.onDeviceStateListenersChanged(true);
            }
            SidecarInterface sidecarInterface4 = this.f25456a;
            Method method2 = (sidecarInterface4 == null || (cls3 = sidecarInterface4.getClass()) == null) ? null : cls3.getMethod("getWindowLayoutInfo", IBinder.class);
            Class<?> returnType2 = method2 != null ? method2.getReturnType() : null;
            if (!nb.g.c(returnType2, SidecarWindowLayoutInfo.class)) {
                throw new NoSuchMethodException("Illegal return type for 'getWindowLayoutInfo': " + returnType2);
            }
            SidecarInterface sidecarInterface5 = this.f25456a;
            Method method3 = (sidecarInterface5 == null || (cls2 = sidecarInterface5.getClass()) == null) ? null : cls2.getMethod("onWindowLayoutChangeListenerAdded", IBinder.class);
            Class<?> returnType3 = method3 != null ? method3.getReturnType() : null;
            if (!nb.g.c(returnType3, cls5)) {
                throw new NoSuchMethodException("Illegal return type for 'onWindowLayoutChangeListenerAdded': " + returnType3);
            }
            SidecarInterface sidecarInterface6 = this.f25456a;
            Method method4 = (sidecarInterface6 == null || (cls = sidecarInterface6.getClass()) == null) ? null : cls.getMethod("onWindowLayoutChangeListenerRemoved", IBinder.class);
            Class<?> returnType4 = method4 != null ? method4.getReturnType() : null;
            if (!nb.g.c(returnType4, cls5)) {
                throw new NoSuchMethodException("Illegal return type for 'onWindowLayoutChangeListenerRemoved': " + returnType4);
            }
            SidecarDeviceState sidecarDeviceState = new SidecarDeviceState();
            try {
                sidecarDeviceState.posture = 3;
            } catch (NoSuchFieldError unused) {
                SidecarDeviceState.class.getMethod("setPosture", Integer.TYPE).invoke(sidecarDeviceState, 3);
                Object objInvoke = SidecarDeviceState.class.getMethod("getPosture", new Class[0]).invoke(sidecarDeviceState, new Object[0]);
                nb.g.g(objInvoke, "null cannot be cast to non-null type kotlin.Int");
                if (((Integer) objInvoke).intValue() != 3) {
                    throw new Exception("Invalid device posture getter/setter");
                }
            }
            SidecarDisplayFeature sidecarDisplayFeature = new SidecarDisplayFeature();
            Rect rect = sidecarDisplayFeature.getRect();
            nb.g.h(rect, "displayFeature.rect");
            sidecarDisplayFeature.setRect(rect);
            sidecarDisplayFeature.getType();
            sidecarDisplayFeature.setType(1);
            SidecarWindowLayoutInfo sidecarWindowLayoutInfo = new SidecarWindowLayoutInfo();
            try {
                List list = sidecarWindowLayoutInfo.displayFeatures;
            } catch (NoSuchFieldError unused2) {
                ArrayList arrayList = new ArrayList();
                arrayList.add(sidecarDisplayFeature);
                SidecarWindowLayoutInfo.class.getMethod("setDisplayFeatures", List.class).invoke(sidecarWindowLayoutInfo, arrayList);
                Object objInvoke2 = SidecarWindowLayoutInfo.class.getMethod("getDisplayFeatures", new Class[0]).invoke(sidecarWindowLayoutInfo, new Object[0]);
                nb.g.g(objInvoke2, "null cannot be cast to non-null type kotlin.collections.List<androidx.window.sidecar.SidecarDisplayFeature>");
                if (!nb.g.c(arrayList, (List) objInvoke2)) {
                    throw new Exception("Invalid display feature getter/setter");
                }
            }
            return true;
        } catch (Throwable unused3) {
            return false;
        }
    }
}
