package X6FRs_4.KQgRr5e_0.LGe8pk_15;

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;
/* compiled from: TooltipPopup.java */
/* loaded from: classes.dex */
public class ViqFd_33 {
    public final Context KQgRr5e_0;

    /* renamed from: KQgRr5e_0  reason: collision with other field name */
    public final View f759KQgRr5e_0;

    /* renamed from: KQgRr5e_0  reason: collision with other field name */
    public final TextView f761KQgRr5e_0;

    /* renamed from: KQgRr5e_0  reason: collision with other field name */
    public final WindowManager.LayoutParams f760KQgRr5e_0 = new WindowManager.LayoutParams();

    /* renamed from: KQgRr5e_0  reason: collision with other field name */
    public final Rect f758KQgRr5e_0 = new Rect();

    /* renamed from: KQgRr5e_0  reason: collision with other field name */
    public final int[] f762KQgRr5e_0 = new int[2];
    public final int[] QxJ9d_1 = new int[2];

    public ViqFd_33(Context context) {
        this.KQgRr5e_0 = context;
        View inflate = LayoutInflater.from(context).inflate(X6FRs_4.KQgRr5e_0.IMuAHx_6.abc_tooltip, (ViewGroup) null);
        this.f759KQgRr5e_0 = inflate;
        this.f761KQgRr5e_0 = (TextView) inflate.findViewById(X6FRs_4.KQgRr5e_0.IFxWkV_5.message);
        this.f760KQgRr5e_0.setTitle(ViqFd_33.class.getSimpleName());
        this.f760KQgRr5e_0.packageName = this.KQgRr5e_0.getPackageName();
        WindowManager.LayoutParams layoutParams = this.f760KQgRr5e_0;
        layoutParams.type = 1002;
        layoutParams.width = -2;
        layoutParams.height = -2;
        layoutParams.format = -3;
        layoutParams.windowAnimations = X6FRs_4.KQgRr5e_0.Xgtqvk_8.Animation_AppCompat_Tooltip;
        layoutParams.flags = 24;
    }

    public void KQgRr5e_0(View view, int i, int i2, boolean z, CharSequence charSequence) {
        if (m431KQgRr5e_0()) {
            KQgRr5e_0();
        }
        this.f761KQgRr5e_0.setText(charSequence);
        KQgRr5e_0(view, i, i2, z, this.f760KQgRr5e_0);
        ((WindowManager) this.KQgRr5e_0.getSystemService("window")).addView(this.f759KQgRr5e_0, this.f760KQgRr5e_0);
    }

    public void KQgRr5e_0() {
        if (m431KQgRr5e_0()) {
            ((WindowManager) this.KQgRr5e_0.getSystemService("window")).removeView(this.f759KQgRr5e_0);
        }
    }

    /* renamed from: KQgRr5e_0  reason: collision with other method in class */
    public boolean m431KQgRr5e_0() {
        return this.f759KQgRr5e_0.getParent() != null;
    }

    public final void KQgRr5e_0(View view, int i, int i2, boolean z, WindowManager.LayoutParams layoutParams) {
        int height;
        int i3;
        layoutParams.token = view.getApplicationWindowToken();
        int dimensionPixelOffset = this.KQgRr5e_0.getResources().getDimensionPixelOffset(X6FRs_4.KQgRr5e_0.Mqv2r_3.tooltip_precise_anchor_threshold);
        if (view.getWidth() < dimensionPixelOffset) {
            i = view.getWidth() / 2;
        }
        if (view.getHeight() >= dimensionPixelOffset) {
            int dimensionPixelOffset2 = this.KQgRr5e_0.getResources().getDimensionPixelOffset(X6FRs_4.KQgRr5e_0.Mqv2r_3.tooltip_precise_anchor_extra_offset);
            height = i2 + dimensionPixelOffset2;
            i3 = i2 - dimensionPixelOffset2;
        } else {
            height = view.getHeight();
            i3 = 0;
        }
        layoutParams.gravity = 49;
        int dimensionPixelOffset3 = this.KQgRr5e_0.getResources().getDimensionPixelOffset(z ? X6FRs_4.KQgRr5e_0.Mqv2r_3.tooltip_y_offset_touch : X6FRs_4.KQgRr5e_0.Mqv2r_3.tooltip_y_offset_non_touch);
        View KQgRr5e_0 = KQgRr5e_0(view);
        if (KQgRr5e_0 == null) {
            Log.e("TooltipPopup", "Cannot find app view");
            return;
        }
        KQgRr5e_0.getWindowVisibleDisplayFrame(this.f758KQgRr5e_0);
        Rect rect = this.f758KQgRr5e_0;
        if (rect.left < 0 && rect.top < 0) {
            Resources resources = this.KQgRr5e_0.getResources();
            int identifier = resources.getIdentifier("status_bar_height", "dimen", "android");
            int dimensionPixelSize = identifier != 0 ? resources.getDimensionPixelSize(identifier) : 0;
            DisplayMetrics displayMetrics = resources.getDisplayMetrics();
            this.f758KQgRr5e_0.set(0, dimensionPixelSize, displayMetrics.widthPixels, displayMetrics.heightPixels);
        }
        KQgRr5e_0.getLocationOnScreen(this.QxJ9d_1);
        view.getLocationOnScreen(this.f762KQgRr5e_0);
        int[] iArr = this.f762KQgRr5e_0;
        int i4 = iArr[0];
        int[] iArr2 = this.QxJ9d_1;
        iArr[0] = i4 - iArr2[0];
        iArr[1] = iArr[1] - iArr2[1];
        layoutParams.x = (iArr[0] + i) - (KQgRr5e_0.getWidth() / 2);
        int makeMeasureSpec = View.MeasureSpec.makeMeasureSpec(0, 0);
        this.f759KQgRr5e_0.measure(makeMeasureSpec, makeMeasureSpec);
        int measuredHeight = this.f759KQgRr5e_0.getMeasuredHeight();
        int[] iArr3 = this.f762KQgRr5e_0;
        int i5 = ((iArr3[1] + i3) - dimensionPixelOffset3) - measuredHeight;
        int i6 = iArr3[1] + height + dimensionPixelOffset3;
        if (z) {
            if (i5 >= 0) {
                layoutParams.y = i5;
            } else {
                layoutParams.y = i6;
            }
        } else if (measuredHeight + i6 <= this.f758KQgRr5e_0.height()) {
            layoutParams.y = i6;
        } else {
            layoutParams.y = i5;
        }
    }

    public static View KQgRr5e_0(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;
    }
}
