package c.a.g;

import android.app.Activity;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.res.Resources;
import android.graphics.Rect;
import android.os.Build;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.accessibility.AccessibilityManager;
import com.ifpdos.multiuser.R;
import com.seewo.vcommons.constants.Platform;
import java.util.WeakHashMap;

/* JADX INFO: loaded from: classes.dex */
public class o0 implements View.OnLongClickListener, View.OnHoverListener, View.OnAttachStateChangeListener {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public static o0 f5033a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public static o0 f5034b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public final View f5035c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public final CharSequence f5036d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public final int f5037e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public final Runnable f5038f = new a();

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public final Runnable f5039g = new b();

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public int f5040h;

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    public int f5041i;

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    public p0 f5042j;
    public boolean k;

    public class a implements Runnable {
        public a() {
        }

        @Override // java.lang.Runnable
        public void run() {
            o0.this.d(false);
        }
    }

    public class b implements Runnable {
        public b() {
        }

        @Override // java.lang.Runnable
        public void run() {
            o0.this.b();
        }
    }

    public o0(View view, CharSequence charSequence) {
        this.f5035c = view;
        this.f5036d = charSequence;
        ViewConfiguration viewConfiguration = ViewConfiguration.get(view.getContext());
        int i2 = c.c.i.p.f5295a;
        this.f5037e = Build.VERSION.SDK_INT >= 28 ? viewConfiguration.getScaledHoverSlop() : viewConfiguration.getScaledTouchSlop() / 2;
        a();
        view.setOnLongClickListener(this);
        view.setOnHoverListener(this);
    }

    public static void c(o0 o0Var) {
        o0 o0Var2 = f5033a;
        if (o0Var2 != null) {
            o0Var2.f5035c.removeCallbacks(o0Var2.f5038f);
        }
        f5033a = o0Var;
        if (o0Var != null) {
            o0Var.f5035c.postDelayed(o0Var.f5038f, ViewConfiguration.getLongPressTimeout());
        }
    }

    public final void a() {
        this.f5040h = Platform.UNKNOWN;
        this.f5041i = Platform.UNKNOWN;
    }

    public void b() {
        if (f5034b == this) {
            f5034b = null;
            p0 p0Var = this.f5042j;
            if (p0Var != null) {
                p0Var.a();
                this.f5042j = null;
                a();
                this.f5035c.removeOnAttachStateChangeListener(this);
            } else {
                Log.e("TooltipCompatHandler", "sActiveHandler.mPopup == null");
            }
        }
        if (f5033a == this) {
            c(null);
        }
        this.f5035c.removeCallbacks(this.f5039g);
    }

    public void d(boolean z) {
        int height;
        int i2;
        long longPressTimeout;
        View view = this.f5035c;
        WeakHashMap<View, c.c.i.q> weakHashMap = c.c.i.k.f5288a;
        if (view.isAttachedToWindow()) {
            c(null);
            o0 o0Var = f5034b;
            if (o0Var != null) {
                o0Var.b();
            }
            f5034b = this;
            this.k = z;
            p0 p0Var = new p0(this.f5035c.getContext());
            this.f5042j = p0Var;
            View view2 = this.f5035c;
            int width = this.f5040h;
            int i3 = this.f5041i;
            boolean z2 = this.k;
            CharSequence charSequence = this.f5036d;
            if (p0Var.f5051b.getParent() != null) {
                p0Var.a();
            }
            p0Var.f5052c.setText(charSequence);
            WindowManager.LayoutParams layoutParams = p0Var.f5053d;
            layoutParams.token = view2.getApplicationWindowToken();
            int dimensionPixelOffset = p0Var.f5050a.getResources().getDimensionPixelOffset(R.dimen.tooltip_precise_anchor_threshold);
            if (view2.getWidth() < dimensionPixelOffset) {
                width = view2.getWidth() / 2;
            }
            if (view2.getHeight() >= dimensionPixelOffset) {
                int dimensionPixelOffset2 = p0Var.f5050a.getResources().getDimensionPixelOffset(R.dimen.tooltip_precise_anchor_extra_offset);
                height = i3 + dimensionPixelOffset2;
                i2 = i3 - dimensionPixelOffset2;
            } else {
                height = view2.getHeight();
                i2 = 0;
            }
            layoutParams.gravity = 49;
            int dimensionPixelOffset3 = p0Var.f5050a.getResources().getDimensionPixelOffset(z2 ? R.dimen.tooltip_y_offset_touch : R.dimen.tooltip_y_offset_non_touch);
            View rootView = view2.getRootView();
            ViewGroup.LayoutParams layoutParams2 = rootView.getLayoutParams();
            if (!(layoutParams2 instanceof WindowManager.LayoutParams) || ((WindowManager.LayoutParams) layoutParams2).type != 2) {
                Context context = view2.getContext();
                while (true) {
                    if (!(context instanceof ContextWrapper)) {
                        break;
                    }
                    if (context instanceof Activity) {
                        rootView = ((Activity) context).getWindow().getDecorView();
                        break;
                    }
                    context = ((ContextWrapper) context).getBaseContext();
                }
            }
            if (rootView == null) {
                Log.e("TooltipPopup", "Cannot find app view");
            } else {
                rootView.getWindowVisibleDisplayFrame(p0Var.f5054e);
                Rect rect = p0Var.f5054e;
                if (rect.left < 0 && rect.top < 0) {
                    Resources resources = p0Var.f5050a.getResources();
                    int identifier = resources.getIdentifier("status_bar_height", "dimen", "android");
                    int dimensionPixelSize = identifier != 0 ? resources.getDimensionPixelSize(identifier) : 0;
                    DisplayMetrics displayMetrics = resources.getDisplayMetrics();
                    p0Var.f5054e.set(0, dimensionPixelSize, displayMetrics.widthPixels, displayMetrics.heightPixels);
                }
                rootView.getLocationOnScreen(p0Var.f5056g);
                view2.getLocationOnScreen(p0Var.f5055f);
                int[] iArr = p0Var.f5055f;
                int i4 = iArr[0];
                int[] iArr2 = p0Var.f5056g;
                iArr[0] = i4 - iArr2[0];
                iArr[1] = iArr[1] - iArr2[1];
                layoutParams.x = (iArr[0] + width) - (rootView.getWidth() / 2);
                int iMakeMeasureSpec = View.MeasureSpec.makeMeasureSpec(0, 0);
                p0Var.f5051b.measure(iMakeMeasureSpec, iMakeMeasureSpec);
                int measuredHeight = p0Var.f5051b.getMeasuredHeight();
                int[] iArr3 = p0Var.f5055f;
                int i5 = ((iArr3[1] + i2) - dimensionPixelOffset3) - measuredHeight;
                int i6 = iArr3[1] + height + dimensionPixelOffset3;
                if (!z2 ? measuredHeight + i6 <= p0Var.f5054e.height() : i5 < 0) {
                    layoutParams.y = i5;
                } else {
                    layoutParams.y = i6;
                }
            }
            ((WindowManager) p0Var.f5050a.getSystemService("window")).addView(p0Var.f5051b, p0Var.f5053d);
            this.f5035c.addOnAttachStateChangeListener(this);
            if (this.k) {
                longPressTimeout = 2500;
            } else {
                longPressTimeout = ((this.f5035c.getWindowSystemUiVisibility() & 1) == 1 ? 3000L : 15000L) - ((long) ViewConfiguration.getLongPressTimeout());
            }
            this.f5035c.removeCallbacks(this.f5039g);
            this.f5035c.postDelayed(this.f5039g, longPressTimeout);
        }
    }

    @Override // android.view.View.OnHoverListener
    public boolean onHover(View view, MotionEvent motionEvent) {
        boolean z;
        if (this.f5042j != null && this.k) {
            return false;
        }
        AccessibilityManager accessibilityManager = (AccessibilityManager) this.f5035c.getContext().getSystemService("accessibility");
        if (accessibilityManager.isEnabled() && accessibilityManager.isTouchExplorationEnabled()) {
            return false;
        }
        int action = motionEvent.getAction();
        if (action != 7) {
            if (action == 10) {
                a();
                b();
            }
        } else if (this.f5035c.isEnabled() && this.f5042j == null) {
            int x = (int) motionEvent.getX();
            int y = (int) motionEvent.getY();
            if (Math.abs(x - this.f5040h) > this.f5037e || Math.abs(y - this.f5041i) > this.f5037e) {
                this.f5040h = x;
                this.f5041i = y;
                z = true;
            } else {
                z = false;
            }
            if (z) {
                c(this);
            }
        }
        return false;
    }

    @Override // android.view.View.OnLongClickListener
    public boolean onLongClick(View view) {
        this.f5040h = view.getWidth() / 2;
        this.f5041i = view.getHeight() / 2;
        d(true);
        return true;
    }

    @Override // android.view.View.OnAttachStateChangeListener
    public void onViewAttachedToWindow(View view) {
    }

    @Override // android.view.View.OnAttachStateChangeListener
    public void onViewDetachedFromWindow(View view) {
        b();
    }
}
