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;

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

    /* JADX INFO: renamed from: l, reason: collision with root package name */
    private static w0 f212l;

    /* JADX INFO: renamed from: m, reason: collision with root package name */
    private static w0 f213m;
    private final View b;
    private final CharSequence d;
    private final int e;
    private final Runnable f = new a();

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

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

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

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

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    private boolean f218k;

    /* 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.b = view;
        this.d = charSequence;
        this.e = g.e.j.p.a(ViewConfiguration.get(view.getContext()));
        b();
        this.b.setOnLongClickListener(this);
        this.b.setOnHoverListener(this);
    }

    private void a() {
        this.b.removeCallbacks(this.f);
    }

    private void b() {
        this.f215h = Integer.MAX_VALUE;
        this.f216i = Integer.MAX_VALUE;
    }

    private void d() {
        this.b.postDelayed(this.f, ViewConfiguration.getLongPressTimeout());
    }

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

    public static void f(View view, CharSequence charSequence) {
        w0 w0Var = f212l;
        if (w0Var != null && w0Var.b == view) {
            e(null);
        }
        if (!TextUtils.isEmpty(charSequence)) {
            new w0(view, charSequence);
            return;
        }
        w0 w0Var2 = f213m;
        if (w0Var2 != null && w0Var2.b == 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.f215h) <= this.e && Math.abs(y - this.f216i) <= this.e) {
            return false;
        }
        this.f215h = x;
        this.f216i = y;
        return true;
    }

    void c() {
        if (f213m == this) {
            f213m = null;
            x0 x0Var = this.f217j;
            if (x0Var != null) {
                x0Var.c();
                this.f217j = null;
                b();
                this.b.removeOnAttachStateChangeListener(this);
            } else {
                Log.e("TooltipCompatHandler", "sActiveHandler.mPopup == null");
            }
        }
        if (f212l == this) {
            e(null);
        }
        this.b.removeCallbacks(this.f214g);
    }

    void g(boolean z) {
        long j2;
        int longPressTimeout;
        long j3;
        if (g.e.j.o.l(this.b)) {
            e(null);
            w0 w0Var = f213m;
            if (w0Var != null) {
                w0Var.c();
            }
            f213m = this;
            this.f218k = z;
            x0 x0Var = new x0(this.b.getContext());
            this.f217j = x0Var;
            x0Var.e(this.b, this.f215h, this.f216i, this.f218k, this.d);
            this.b.addOnAttachStateChangeListener(this);
            if (this.f218k) {
                j3 = 2500;
            } else {
                if ((g.e.j.o.i(this.b) & 1) == 1) {
                    j2 = 3000;
                    longPressTimeout = ViewConfiguration.getLongPressTimeout();
                } else {
                    j2 = 15000;
                    longPressTimeout = ViewConfiguration.getLongPressTimeout();
                }
                j3 = j2 - ((long) longPressTimeout);
            }
            this.b.removeCallbacks(this.f214g);
            this.b.postDelayed(this.f214g, j3);
        }
    }

    @Override // android.view.View.OnHoverListener
    public boolean onHover(View view, MotionEvent motionEvent) {
        if (this.f217j != null && this.f218k) {
            return false;
        }
        AccessibilityManager accessibilityManager = (AccessibilityManager) this.b.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.b.isEnabled() && this.f217j == null && h(motionEvent)) {
            e(this);
        }
        return false;
    }

    @Override // android.view.View.OnLongClickListener
    public boolean onLongClick(View view) {
        this.f215h = view.getWidth() / 2;
        this.f216i = 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();
    }
}
