package F2;

import B2.m;
import F1.AbstractC0174c;
import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.Point;
import android.graphics.Rect;
import android.inputmethodservice.InputMethodService;
import android.util.Log;
import android.view.Display;
import android.view.DisplayCutout;
import android.view.WindowManager;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import kotlin.jvm.internal.o;

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

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public static final c f1480c = new c(0);

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public static final c f1481d = new c(1);

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public static final c f1482e = new c(2);

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public static final c f1483f = new c(3);

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public static final c f1484g = new c(4);

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

    public /* synthetic */ c(int i6) {
        this.f1485b = i6;
    }

    @Override // F2.b
    public Rect a(Activity activity) throws Exception {
        Object obj;
        a aVar = b.f1479a;
        switch (this.f1485b) {
            case 0:
                Rect 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 i6 = rect.bottom + dimensionPixelSize;
                    if (i6 == point.y) {
                        rect.bottom = i6;
                    } else {
                        int i7 = rect.right + dimensionPixelSize;
                        if (i7 == point.x) {
                            rect.right = i7;
                        }
                    }
                }
                return rect;
            case 1:
                Rect rect2 = 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]);
                        o.f(objInvoke, "null cannot be cast to non-null type android.graphics.Rect");
                        rect2.set((Rect) objInvoke);
                    } else {
                        Object objInvoke2 = obj2.getClass().getDeclaredMethod("getAppBounds", new Class[0]).invoke(obj2, new Object[0]);
                        o.f(objInvoke2, "null cannot be cast to non-null type android.graphics.Rect");
                        rect2.set((Rect) objInvoke2);
                    }
                    break;
                } catch (Exception e6) {
                    if (!(e6 instanceof NoSuchFieldException) && !(e6 instanceof NoSuchMethodException) && !(e6 instanceof IllegalAccessException) && !(e6 instanceof InvocationTargetException)) {
                        throw e6;
                    }
                    aVar.getClass();
                    Log.w(a.f1478b, e6);
                    activity.getWindowManager().getDefaultDisplay().getRectSize(rect2);
                }
                Display defaultDisplay2 = activity.getWindowManager().getDefaultDisplay();
                Point point2 = new Point();
                defaultDisplay2.getRealSize(point2);
                if (!activity.isInMultiWindowMode()) {
                    Resources resources2 = activity.getResources();
                    int identifier2 = resources2.getIdentifier("navigation_bar_height", "dimen", "android");
                    int dimensionPixelSize2 = identifier2 > 0 ? resources2.getDimensionPixelSize(identifier2) : 0;
                    int i8 = rect2.bottom + dimensionPixelSize2;
                    if (i8 == point2.y) {
                        rect2.bottom = i8;
                    } else {
                        int i9 = rect2.right + dimensionPixelSize2;
                        if (i9 == point2.x) {
                            rect2.right = i9;
                        } else if (rect2.left == dimensionPixelSize2) {
                            rect2.left = 0;
                        }
                    }
                }
                if ((rect2.width() < point2.x || rect2.height() < point2.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 = defaultDisplay2.getClass().getDeclaredMethod("getDisplayInfo", objNewInstance.getClass());
                        declaredMethod.setAccessible(true);
                        declaredMethod.invoke(defaultDisplay2, objNewInstance);
                        Field declaredField2 = objNewInstance.getClass().getDeclaredField("displayCutout");
                        declaredField2.setAccessible(true);
                        obj = declaredField2.get(objNewInstance);
                    } catch (Exception e7) {
                        if (!(e7 instanceof ClassNotFoundException) && !(e7 instanceof NoSuchMethodException) && !(e7 instanceof NoSuchFieldException) && !(e7 instanceof IllegalAccessException) && !(e7 instanceof InvocationTargetException) && !(e7 instanceof InstantiationException)) {
                            throw e7;
                        }
                        aVar.getClass();
                        Log.w(a.f1478b, e7);
                    }
                    DisplayCutout displayCutoutC = AbstractC0174c.w(obj) ? AbstractC0174c.c(obj) : null;
                    if (displayCutoutC != null) {
                        if (rect2.left == displayCutoutC.getSafeInsetLeft()) {
                            rect2.left = 0;
                        }
                        if (point2.x - rect2.right == displayCutoutC.getSafeInsetRight()) {
                            rect2.right = displayCutoutC.getSafeInsetRight() + rect2.right;
                        }
                        if (rect2.top == displayCutoutC.getSafeInsetTop()) {
                            rect2.top = 0;
                        }
                        if (point2.y - rect2.bottom == displayCutoutC.getSafeInsetBottom()) {
                            rect2.bottom = displayCutoutC.getSafeInsetBottom() + rect2.bottom;
                        }
                    }
                    break;
                }
                return rect2;
            default:
                Configuration configuration2 = activity.getResources().getConfiguration();
                try {
                    Field declaredField3 = Configuration.class.getDeclaredField("windowConfiguration");
                    declaredField3.setAccessible(true);
                    Object obj3 = declaredField3.get(configuration2);
                    Object objInvoke3 = obj3.getClass().getDeclaredMethod("getBounds", new Class[0]).invoke(obj3, new Object[0]);
                    o.f(objInvoke3, "null cannot be cast to non-null type android.graphics.Rect");
                    return new Rect((Rect) objInvoke3);
                } catch (Exception e8) {
                    if (!(e8 instanceof NoSuchFieldException) && !(e8 instanceof NoSuchMethodException) && !(e8 instanceof IllegalAccessException) && !(e8 instanceof InvocationTargetException)) {
                        throw e8;
                    }
                    aVar.getClass();
                    Log.w(a.f1478b, e8);
                    return f1481d.a(activity);
                }
        }
    }

    @Override // F2.g
    public m b(Activity activity, e densityCompatHelper) {
        o.h(densityCompatHelper, "densityCompatHelper");
        b.f1479a.getClass();
        return new m(new z2.b(a.a().a(activity)), densityCompatHelper.c(activity));
    }

    @Override // F2.e
    public float c(Context context) {
        return context.getResources().getDisplayMetrics().density;
    }

    @Override // F2.g
    public m d(Context context, e densityCompatHelper) {
        o.h(densityCompatHelper, "densityCompatHelper");
        Context baseContext = context;
        while (true) {
            if (!(baseContext instanceof ContextWrapper)) {
                baseContext = context;
                break;
            }
            if ((baseContext instanceof Activity) || (baseContext instanceof InputMethodService)) {
                break;
            }
            ContextWrapper contextWrapper = (ContextWrapper) baseContext;
            if (contextWrapper.getBaseContext() == null) {
                break;
            }
            baseContext = contextWrapper.getBaseContext();
            o.g(baseContext, "getBaseContext(...)");
        }
        if (baseContext instanceof Activity) {
            return b((Activity) baseContext, densityCompatHelper);
        }
        if (!(baseContext instanceof InputMethodService) && !(baseContext instanceof Application)) {
            throw new IllegalArgumentException("Must provide a UiContext or Application Context");
        }
        Object systemService = context.getSystemService("window");
        o.f(systemService, "null cannot be cast to non-null type android.view.WindowManager");
        Display defaultDisplay = ((WindowManager) systemService).getDefaultDisplay();
        o.g(defaultDisplay, "getDefaultDisplay(...)");
        Point point = new Point();
        defaultDisplay.getRealSize(point);
        return new m(new Rect(0, 0, point.x, point.y), densityCompatHelper.c(context));
    }
}
