package I0;

import D.AbstractC0046q;
import P.c0;
import P.d0;
import P.e0;
import P.f0;
import P.g0;
import P.s0;
import android.app.Activity;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.Point;
import android.graphics.Rect;
import android.os.Build;
import android.util.Log;
import android.view.Display;
import android.view.DisplayCutout;
import android.view.WindowManager;
import e4.C0562f;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import r4.AbstractC0983h;

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

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public static final /* synthetic */ int f909b = 0;

    static {
        new ArrayList(new C0562f(new Integer[]{1, 2, 4, 8, 16, 32, 64, 128}, true));
    }

    public static m a(Activity activity) throws Exception {
        Rect rect;
        s0 s0VarB;
        int i = Build.VERSION.SDK_INT;
        if (i >= 30) {
            rect = ((WindowManager) activity.getSystemService(WindowManager.class)).getCurrentWindowMetrics().getBounds();
            AbstractC0983h.d("wm.currentWindowMetrics.bounds", rect);
        } else {
            if (i >= 29) {
                Configuration configuration = activity.getResources().getConfiguration();
                try {
                    Field declaredField = Configuration.class.getDeclaredField("windowConfiguration");
                    declaredField.setAccessible(true);
                    Object obj = declaredField.get(configuration);
                    Object objInvoke = obj.getClass().getDeclaredMethod("getBounds", new Class[0]).invoke(obj, new Object[0]);
                    AbstractC0983h.c("null cannot be cast to non-null type android.graphics.Rect", objInvoke);
                    rect = new Rect((Rect) objInvoke);
                } catch (IllegalAccessException e2) {
                    Log.w("o", e2);
                    rect = b(activity);
                } catch (NoSuchFieldException e6) {
                    Log.w("o", e6);
                    rect = b(activity);
                } catch (NoSuchMethodException e7) {
                    Log.w("o", e7);
                    rect = b(activity);
                } catch (InvocationTargetException e8) {
                    Log.w("o", e8);
                    rect = b(activity);
                }
            } else if (i >= 28) {
                rect = b(activity);
            } else {
                rect = new Rect();
                Display defaultDisplay = activity.getWindowManager().getDefaultDisplay();
                defaultDisplay.getRectSize(rect);
                if (!activity.isInMultiWindowMode()) {
                    Point point = new Point();
                    defaultDisplay.getRealSize(point);
                    Resources resources = activity.getResources();
                    int identifier = resources.getIdentifier("navigation_bar_height", "dimen", "android");
                    int dimensionPixelSize = identifier > 0 ? resources.getDimensionPixelSize(identifier) : 0;
                    int i5 = rect.bottom + dimensionPixelSize;
                    if (i5 == point.y) {
                        rect.bottom = i5;
                    } else {
                        int i6 = rect.right + dimensionPixelSize;
                        if (i6 == point.x) {
                            rect.right = i6;
                        }
                    }
                }
            }
        }
        int i7 = Build.VERSION.SDK_INT;
        if (i7 < 30) {
            s0VarB = (i7 >= 34 ? new g0() : i7 >= 31 ? new f0() : i7 >= 30 ? new e0() : i7 >= 29 ? new d0() : new c0()).b();
            AbstractC0983h.d("{\n            WindowInse…ilder().build()\n        }", s0VarB);
        } else {
            if (i7 < 30) {
                throw new Exception("Incompatible SDK version");
            }
            s0VarB = M0.a.f2144a.a(activity);
        }
        return new m(new F0.b(rect), s0VarB);
    }

    public static Rect b(Activity activity) {
        Object obj;
        Rect rect = new Rect();
        Configuration configuration = activity.getResources().getConfiguration();
        try {
            Field declaredField = Configuration.class.getDeclaredField("windowConfiguration");
            declaredField.setAccessible(true);
            Object obj2 = declaredField.get(configuration);
            if (activity.isInMultiWindowMode()) {
                Object objInvoke = obj2.getClass().getDeclaredMethod("getBounds", new Class[0]).invoke(obj2, new Object[0]);
                AbstractC0983h.c("null cannot be cast to non-null type android.graphics.Rect", objInvoke);
                rect.set((Rect) objInvoke);
            } else {
                Object objInvoke2 = obj2.getClass().getDeclaredMethod("getAppBounds", new Class[0]).invoke(obj2, new Object[0]);
                AbstractC0983h.c("null cannot be cast to non-null type android.graphics.Rect", objInvoke2);
                rect.set((Rect) objInvoke2);
            }
        } catch (IllegalAccessException e2) {
            Log.w("o", e2);
            activity.getWindowManager().getDefaultDisplay().getRectSize(rect);
        } catch (NoSuchFieldException e6) {
            Log.w("o", e6);
            activity.getWindowManager().getDefaultDisplay().getRectSize(rect);
        } catch (NoSuchMethodException e7) {
            Log.w("o", e7);
            activity.getWindowManager().getDefaultDisplay().getRectSize(rect);
        } catch (InvocationTargetException e8) {
            Log.w("o", e8);
            activity.getWindowManager().getDefaultDisplay().getRectSize(rect);
        }
        Display defaultDisplay = activity.getWindowManager().getDefaultDisplay();
        Point point = new Point();
        AbstractC0983h.d("currentDisplay", defaultDisplay);
        defaultDisplay.getRealSize(point);
        if (!activity.isInMultiWindowMode()) {
            Resources resources = activity.getResources();
            int identifier = resources.getIdentifier("navigation_bar_height", "dimen", "android");
            int dimensionPixelSize = identifier > 0 ? resources.getDimensionPixelSize(identifier) : 0;
            int i = rect.bottom + dimensionPixelSize;
            if (i == point.y) {
                rect.bottom = i;
            } else {
                int i5 = rect.right + dimensionPixelSize;
                if (i5 == point.x) {
                    rect.right = i5;
                } else if (rect.left == dimensionPixelSize) {
                    rect.left = 0;
                }
            }
        }
        if ((rect.width() < point.x || rect.height() < point.y) && !activity.isInMultiWindowMode()) {
            try {
                Constructor<?> constructor = Class.forName("android.view.DisplayInfo").getConstructor(new Class[0]);
                constructor.setAccessible(true);
                Object objNewInstance = constructor.newInstance(new Object[0]);
                Method declaredMethod = defaultDisplay.getClass().getDeclaredMethod("getDisplayInfo", objNewInstance.getClass());
                declaredMethod.setAccessible(true);
                declaredMethod.invoke(defaultDisplay, objNewInstance);
                Field declaredField2 = objNewInstance.getClass().getDeclaredField("displayCutout");
                declaredField2.setAccessible(true);
                obj = declaredField2.get(objNewInstance);
            } catch (ClassNotFoundException e9) {
                Log.w("o", e9);
            } catch (IllegalAccessException e10) {
                Log.w("o", e10);
            } catch (InstantiationException e11) {
                Log.w("o", e11);
            } catch (NoSuchFieldException e12) {
                Log.w("o", e12);
            } catch (NoSuchMethodException e13) {
                Log.w("o", e13);
            } catch (InvocationTargetException e14) {
                Log.w("o", e14);
            }
            DisplayCutout displayCutoutL = AbstractC0046q.u(obj) ? AbstractC0046q.l(obj) : null;
            if (displayCutoutL != null) {
                if (rect.left == displayCutoutL.getSafeInsetLeft()) {
                    rect.left = 0;
                }
                if (point.x - rect.right == displayCutoutL.getSafeInsetRight()) {
                    rect.right = displayCutoutL.getSafeInsetRight() + rect.right;
                }
                if (rect.top == displayCutoutL.getSafeInsetTop()) {
                    rect.top = 0;
                }
                if (point.y - rect.bottom == displayCutoutL.getSafeInsetBottom()) {
                    rect.bottom = displayCutoutL.getSafeInsetBottom() + rect.bottom;
                }
            }
        }
        return rect;
    }
}
