package p028j;

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;
import p007c.C0488d;
import p007c.C0490f;
import p007c.C0491g;
import p007c.C0493i;

/* renamed from: j.o3 */
/* loaded from: classes.dex */
public class C1170o3 {

    /* renamed from: a */
    public final Context f2847a;

    /* renamed from: b */
    public final View f2848b;

    /* renamed from: c */
    public final TextView f2849c;

    /* renamed from: d */
    public final WindowManager.LayoutParams f2850d;

    /* renamed from: e */
    public final Rect f2851e;

    /* renamed from: f */
    public final int[] f2852f;

    /* renamed from: g */
    public final int[] f2853g;

    public C1170o3(Context context) {
        WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
        this.f2850d = layoutParams;
        this.f2851e = new Rect();
        this.f2852f = new int[2];
        this.f2853g = new int[2];
        this.f2847a = context;
        View inflate = LayoutInflater.from(context).inflate(C0491g.abc_tooltip, (ViewGroup) null);
        this.f2848b = inflate;
        this.f2849c = (TextView) inflate.findViewById(C0490f.message);
        layoutParams.setTitle(getClass().getSimpleName());
        layoutParams.packageName = context.getPackageName();
        layoutParams.type = 1002;
        layoutParams.width = -2;
        layoutParams.height = -2;
        layoutParams.format = -3;
        layoutParams.windowAnimations = C0493i.Animation_AppCompat_Tooltip;
        layoutParams.flags = 24;
    }

    /* renamed from: b */
    public static View m1963b(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;
    }

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

    /* renamed from: c */
    public void m1962c() {
        if (m1961d()) {
            ((WindowManager) this.f2847a.getSystemService("window")).removeView(this.f2848b);
        }
    }

    /* renamed from: d */
    public boolean m1961d() {
        return this.f2848b.getParent() != null;
    }

    /* renamed from: e */
    public void m1960e(View view, int i, int i2, boolean z, CharSequence charSequence) {
        if (m1961d()) {
            m1962c();
        }
        this.f2849c.setText(charSequence);
        m1964a(view, i, i2, z, this.f2850d);
        ((WindowManager) this.f2847a.getSystemService("window")).addView(this.f2848b, this.f2850d);
    }
}