package androidx.appcompat.widget;

import android.app.Activity;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.res.Resources;
import android.graphics.Rect;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.TextView;

/* JADX INFO: compiled from: TooltipPopup.java */
/* JADX INFO: loaded from: classes.dex */
class h1 {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    private final Context f927a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    private final View f928b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    private final TextView f929c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    private final WindowManager.LayoutParams f930d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    private final Rect f931e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    private final int[] f932f;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    private final int[] f933g;

    h1(Context context) {
        WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
        this.f930d = layoutParams;
        this.f931e = new Rect();
        this.f932f = new int[2];
        this.f933g = new int[2];
        this.f927a = context;
        View viewInflate = LayoutInflater.from(context).inflate(c.g.f3270s, (ViewGroup) null);
        this.f928b = viewInflate;
        this.f929c = (TextView) viewInflate.findViewById(c.f.f3244s);
        layoutParams.setTitle(h1.class.getSimpleName());
        layoutParams.packageName = context.getPackageName();
        layoutParams.type = 1002;
        layoutParams.width = -2;
        layoutParams.height = -2;
        layoutParams.format = -3;
        layoutParams.windowAnimations = c.i.f3284a;
        layoutParams.flags = 24;
    }

    private void a(View view, int i3, int i4, boolean z2, WindowManager.LayoutParams layoutParams) {
        int height;
        int i5;
        layoutParams.token = view.getApplicationWindowToken();
        int dimensionPixelOffset = this.f927a.getResources().getDimensionPixelOffset(c.d.f3197m);
        if (view.getWidth() < dimensionPixelOffset) {
            i3 = view.getWidth() / 2;
        }
        if (view.getHeight() >= dimensionPixelOffset) {
            int dimensionPixelOffset2 = this.f927a.getResources().getDimensionPixelOffset(c.d.f3196l);
            height = i4 + dimensionPixelOffset2;
            i5 = i4 - dimensionPixelOffset2;
        } else {
            height = view.getHeight();
            i5 = 0;
        }
        layoutParams.gravity = 49;
        int dimensionPixelOffset3 = this.f927a.getResources().getDimensionPixelOffset(z2 ? c.d.f3199o : c.d.f3198n);
        View viewB = b(view);
        if (viewB == null) {
            Log.e("TooltipPopup", "Cannot find app view");
            return;
        }
        viewB.getWindowVisibleDisplayFrame(this.f931e);
        Rect rect = this.f931e;
        if (rect.left < 0 && rect.top < 0) {
            Resources resources = this.f927a.getResources();
            int identifier = resources.getIdentifier("status_bar_height", "dimen", "android");
            int dimensionPixelSize = identifier != 0 ? resources.getDimensionPixelSize(identifier) : 0;
            DisplayMetrics displayMetrics = resources.getDisplayMetrics();
            this.f931e.set(0, dimensionPixelSize, displayMetrics.widthPixels, displayMetrics.heightPixels);
        }
        viewB.getLocationOnScreen(this.f933g);
        view.getLocationOnScreen(this.f932f);
        int[] iArr = this.f932f;
        int i6 = iArr[0];
        int[] iArr2 = this.f933g;
        iArr[0] = i6 - iArr2[0];
        iArr[1] = iArr[1] - iArr2[1];
        layoutParams.x = (iArr[0] + i3) - (viewB.getWidth() / 2);
        int iMakeMeasureSpec = View.MeasureSpec.makeMeasureSpec(0, 0);
        this.f928b.measure(iMakeMeasureSpec, iMakeMeasureSpec);
        int measuredHeight = this.f928b.getMeasuredHeight();
        int[] iArr3 = this.f932f;
        int i7 = ((iArr3[1] + i5) - dimensionPixelOffset3) - measuredHeight;
        int i8 = iArr3[1] + height + dimensionPixelOffset3;
        if (z2) {
            if (i7 >= 0) {
                layoutParams.y = i7;
                return;
            } else {
                layoutParams.y = i8;
                return;
            }
        }
        if (measuredHeight + i8 <= this.f931e.height()) {
            layoutParams.y = i8;
        } else {
            layoutParams.y = i7;
        }
    }

    private static View b(View view) {
        View rootView = view.getRootView();
        ViewGroup.LayoutParams layoutParams = rootView.getLayoutParams();
        if ((layoutParams instanceof WindowManager.LayoutParams) && ((WindowManager.LayoutParams) layoutParams).type == 2) {
            return rootView;
        }
        for (Context context = view.getContext(); context instanceof ContextWrapper; context = ((ContextWrapper) context).getBaseContext()) {
            if (context instanceof Activity) {
                return ((Activity) context).getWindow().getDecorView();
            }
        }
        return rootView;
    }

    void c() {
        if (d()) {
            ((WindowManager) this.f927a.getSystemService("window")).removeView(this.f928b);
        }
    }

    boolean d() {
        return this.f928b.getParent() != null;
    }

    void e(View view, int i3, int i4, boolean z2, CharSequence charSequence) {
        if (d()) {
            c();
        }
        this.f929c.setText(charSequence);
        a(view, i3, i4, z2, this.f930d);
        ((WindowManager) this.f927a.getSystemService("window")).addView(this.f928b, this.f930d);
    }
}
