package androidx.appcompat.widget;

import android.text.TextUtils;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.accessibility.AccessibilityManager;
import io.netty.handler.traffic.AbstractTrafficShapingHandler;

/* JADX INFO: compiled from: TooltipCompatHandler.java */
/* JADX INFO: loaded from: classes.dex */
class w0 implements View.OnLongClickListener, View.OnHoverListener, View.OnAttachStateChangeListener {
    private static w0 n;
    private static w0 o;

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

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    private final CharSequence f539f;

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

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    private final Runnable f541h = new a();

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    private final Runnable f542i = new b();

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    private int f543j;

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    private int f544k;
    private x0 l;
    private boolean m;

    /* JADX INFO: compiled from: TooltipCompatHandler.java */
    class a implements Runnable {
        a() {
        }

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

    /* JADX INFO: compiled from: TooltipCompatHandler.java */
    class b implements Runnable {
        b() {
        }

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

    private w0(View view, CharSequence charSequence) {
        this.f538b = view;
        this.f539f = charSequence;
        this.f540g = c.f.l.u.c(ViewConfiguration.get(view.getContext()));
        b();
        this.f538b.setOnLongClickListener(this);
        this.f538b.setOnHoverListener(this);
    }

    private void a() {
        this.f538b.removeCallbacks(this.f541h);
    }

    private void b() {
        this.f543j = Integer.MAX_VALUE;
        this.f544k = Integer.MAX_VALUE;
    }

    private void d() {
        this.f538b.postDelayed(this.f541h, ViewConfiguration.getLongPressTimeout());
    }

    private static void e(w0 w0Var) {
        w0 w0Var2 = n;
        if (w0Var2 != null) {
            w0Var2.a();
        }
        n = w0Var;
        if (w0Var != null) {
            w0Var.d();
        }
    }

    public static void f(View view, CharSequence charSequence) {
        w0 w0Var = n;
        if (w0Var != null && w0Var.f538b == view) {
            e(null);
        }
        if (!TextUtils.isEmpty(charSequence)) {
            new w0(view, charSequence);
            return;
        }
        w0 w0Var2 = o;
        if (w0Var2 != null && w0Var2.f538b == view) {
            w0Var2.c();
        }
        view.setOnLongClickListener(null);
        view.setLongClickable(false);
        view.setOnHoverListener(null);
    }

    private boolean h(MotionEvent motionEvent) {
        int x = (int) motionEvent.getX();
        int y = (int) motionEvent.getY();
        if (Math.abs(x - this.f543j) <= this.f540g && Math.abs(y - this.f544k) <= this.f540g) {
            return false;
        }
        this.f543j = x;
        this.f544k = y;
        return true;
    }

    void c() {
        if (o == this) {
            o = null;
            x0 x0Var = this.l;
            if (x0Var != null) {
                x0Var.c();
                this.l = null;
                b();
                this.f538b.removeOnAttachStateChangeListener(this);
            } else {
                Log.e("TooltipCompatHandler", "sActiveHandler.mPopup == null");
            }
        }
        if (n == this) {
            e(null);
        }
        this.f538b.removeCallbacks(this.f542i);
    }

    void g(boolean z) {
        long j2;
        int longPressTimeout;
        long j3;
        if (c.f.l.t.G(this.f538b)) {
            e(null);
            w0 w0Var = o;
            if (w0Var != null) {
                w0Var.c();
            }
            o = this;
            this.m = z;
            x0 x0Var = new x0(this.f538b.getContext());
            this.l = x0Var;
            x0Var.e(this.f538b, this.f543j, this.f544k, this.m, this.f539f);
            this.f538b.addOnAttachStateChangeListener(this);
            if (this.m) {
                j3 = 2500;
            } else {
                if ((c.f.l.t.A(this.f538b) & 1) == 1) {
                    j2 = 3000;
                    longPressTimeout = ViewConfiguration.getLongPressTimeout();
                } else {
                    j2 = AbstractTrafficShapingHandler.DEFAULT_MAX_TIME;
                    longPressTimeout = ViewConfiguration.getLongPressTimeout();
                }
                j3 = j2 - ((long) longPressTimeout);
            }
            this.f538b.removeCallbacks(this.f542i);
            this.f538b.postDelayed(this.f542i, j3);
        }
    }

    @Override // android.view.View.OnHoverListener
    public boolean onHover(View view, MotionEvent motionEvent) {
        if (this.l != null && this.m) {
            return false;
        }
        AccessibilityManager accessibilityManager = (AccessibilityManager) this.f538b.getContext().getSystemService("accessibility");
        if (accessibilityManager.isEnabled() && accessibilityManager.isTouchExplorationEnabled()) {
            return false;
        }
        int action = motionEvent.getAction();
        if (action != 7) {
            if (action == 10) {
                b();
                c();
            }
        } else if (this.f538b.isEnabled() && this.l == null && h(motionEvent)) {
            e(this);
        }
        return false;
    }

    @Override // android.view.View.OnLongClickListener
    public boolean onLongClick(View view) {
        this.f543j = view.getWidth() / 2;
        this.f544k = view.getHeight() / 2;
        g(true);
        return true;
    }

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

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