package d4;

import android.app.Activity;
import android.content.Context;
import android.content.ContextWrapper;
import android.graphics.Point;
import android.graphics.Rect;
import android.inputmethodservice.InputMethodService;
import android.os.Build;
import android.view.Display;
import android.view.WindowManager;
import androidx.core.view.f3;
import androidx.core.view.r2;
import androidx.window.extensions.layout.FoldingFeature;
import androidx.window.extensions.layout.WindowLayoutInfo;
import androidx.window.layout.e;
import androidx.window.layout.g;
import androidx.window.layout.h;
import androidx.window.layout.n;
import androidx.window.layout.o;
import androidx.window.layout.r;
import java.util.ArrayList;
import java.util.List;

/* JADX INFO: loaded from: classes.dex */
public abstract class c {
    public static h a(o oVar, FoldingFeature foldingFeature) {
        g gVar;
        e eVar;
        int type = foldingFeature.getType();
        if (type == 1) {
            gVar = g.f4472b;
        } else {
            if (type != 2) {
                return null;
            }
            gVar = g.f4473c;
        }
        int state = foldingFeature.getState();
        if (state == 1) {
            eVar = e.f4469b;
        } else {
            if (state != 2) {
                return null;
            }
            eVar = e.f4470c;
        }
        Rect bounds = foldingFeature.getBounds();
        nb.g.h(bounds, "oemFeature.bounds");
        androidx.window.core.b bVar = new androidx.window.core.b(bounds);
        Rect rectC = oVar.f4488a.c();
        if (bVar.a() == 0 && bVar.b() == 0) {
            return null;
        }
        if (bVar.b() != rectC.width() && bVar.a() != rectC.height()) {
            return null;
        }
        if (bVar.b() < rectC.width() && bVar.a() < rectC.height()) {
            return null;
        }
        if (bVar.b() == rectC.width() && bVar.a() == rectC.height()) {
            return null;
        }
        Rect bounds2 = foldingFeature.getBounds();
        nb.g.h(bounds2, "oemFeature.bounds");
        return new h(new androidx.window.core.b(bounds2), gVar, eVar);
    }

    public static n b(Context context, WindowLayoutInfo windowLayoutInfo) throws Exception {
        o oVar;
        nb.g.i(context, "context");
        nb.g.i(windowLayoutInfo, "info");
        int i10 = Build.VERSION.SDK_INT;
        if (i10 < 30) {
            if (i10 < 29 || !(context instanceof Activity)) {
                throw new UnsupportedOperationException("Display Features are only supported after Q. Display features for non-Activity contexts are not expected to be reported on devices running Q.");
            }
            r rVar = r.f4493b;
            return c(r.b((Activity) context), windowLayoutInfo);
        }
        r rVar2 = r.f4493b;
        if (i10 < 30) {
            Context baseContext = context;
            while (baseContext instanceof ContextWrapper) {
                boolean z10 = baseContext instanceof Activity;
                if (!z10 && !(baseContext instanceof InputMethodService)) {
                    ContextWrapper contextWrapper = (ContextWrapper) baseContext;
                    if (contextWrapper.getBaseContext() != null) {
                        baseContext = contextWrapper.getBaseContext();
                        nb.g.h(baseContext, "iterator.baseContext");
                    }
                }
                if (z10) {
                    oVar = r.b((Activity) context);
                } else {
                    if (!(baseContext instanceof InputMethodService)) {
                        throw new IllegalArgumentException(context + " is not a UiContext");
                    }
                    Object systemService = context.getSystemService("window");
                    nb.g.g(systemService, "null cannot be cast to non-null type android.view.WindowManager");
                    Display defaultDisplay = ((WindowManager) systemService).getDefaultDisplay();
                    nb.g.h(defaultDisplay, "wm.defaultDisplay");
                    Point point = new Point();
                    defaultDisplay.getRealSize(point);
                    Rect rect = new Rect(0, 0, point.x, point.y);
                    f3 f3VarB = new r2().f1814a.b();
                    nb.g.h(f3VarB, "Builder().build()");
                    oVar = new o(f3VarB, rect);
                }
            }
            throw new IllegalArgumentException("Context " + context + " is not a UiContext");
        }
        WindowManager windowManager = (WindowManager) context.getSystemService(WindowManager.class);
        f3 windowInsetsCompat = f3.toWindowInsetsCompat(windowManager.getCurrentWindowMetrics().getWindowInsets(), null);
        nb.g.h(windowInsetsCompat, "toWindowInsetsCompat(wm.…ndowMetrics.windowInsets)");
        Rect bounds = windowManager.getCurrentWindowMetrics().getBounds();
        nb.g.h(bounds, "wm.currentWindowMetrics.bounds");
        oVar = new o(windowInsetsCompat, bounds);
        return c(oVar, windowLayoutInfo);
    }

    public static n c(o oVar, WindowLayoutInfo windowLayoutInfo) {
        h hVarA;
        nb.g.i(windowLayoutInfo, "info");
        List<FoldingFeature> displayFeatures = windowLayoutInfo.getDisplayFeatures();
        nb.g.h(displayFeatures, "info.displayFeatures");
        ArrayList arrayList = new ArrayList();
        for (FoldingFeature foldingFeature : displayFeatures) {
            if (foldingFeature instanceof FoldingFeature) {
                nb.g.h(foldingFeature, "feature");
                hVarA = a(oVar, foldingFeature);
            } else {
                hVarA = null;
            }
            if (hVarA != null) {
                arrayList.add(hVarA);
            }
        }
        return new n(arrayList);
    }
}
