package android.support.v7.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 {
    private static w0 k;
    private static w0 l;

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

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

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

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

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    private final Runnable f1160f = new b();
    private int g;
    private int h;
    private x0 i;
    private boolean j;

    /* 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.f1156b = view;
        this.f1157c = charSequence;
        this.f1158d = a.b.c.g.q.a(ViewConfiguration.get(view.getContext()));
        b();
        this.f1156b.setOnLongClickListener(this);
        this.f1156b.setOnHoverListener(this);
    }

    private void a() {
        this.f1156b.removeCallbacks(this.f1159e);
    }

    private void b() {
        this.g = Integer.MAX_VALUE;
        this.h = Integer.MAX_VALUE;
    }

    private void d() {
        this.f1156b.postDelayed(this.f1159e, ViewConfiguration.getLongPressTimeout());
    }

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

    public static void f(View view, CharSequence charSequence) {
        w0 w0Var = k;
        if (w0Var != null && w0Var.f1156b == view) {
            e(null);
        }
        if (!TextUtils.isEmpty(charSequence)) {
            new w0(view, charSequence);
            return;
        }
        w0 w0Var2 = l;
        if (w0Var2 != null && w0Var2.f1156b == 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.g) <= this.f1158d && Math.abs(y - this.h) <= this.f1158d) {
            return false;
        }
        this.g = x;
        this.h = y;
        return true;
    }

    void c() {
        if (l == this) {
            l = null;
            x0 x0Var = this.i;
            if (x0Var != null) {
                x0Var.c();
                this.i = null;
                b();
                this.f1156b.removeOnAttachStateChangeListener(this);
            } else {
                Log.e("TooltipCompatHandler", "sActiveHandler.mPopup == null");
            }
        }
        if (k == this) {
            e(null);
        }
        this.f1156b.removeCallbacks(this.f1160f);
    }

    void g(boolean z) {
        long j;
        int longPressTimeout;
        long j2;
        if (a.b.c.g.p.l(this.f1156b)) {
            e(null);
            w0 w0Var = l;
            if (w0Var != null) {
                w0Var.c();
            }
            l = this;
            this.j = z;
            x0 x0Var = new x0(this.f1156b.getContext());
            this.i = x0Var;
            x0Var.e(this.f1156b, this.g, this.h, this.j, this.f1157c);
            this.f1156b.addOnAttachStateChangeListener(this);
            if (this.j) {
                j2 = 2500;
            } else {
                if ((a.b.c.g.p.i(this.f1156b) & 1) == 1) {
                    j = 3000;
                    longPressTimeout = ViewConfiguration.getLongPressTimeout();
                } else {
                    j = 15000;
                    longPressTimeout = ViewConfiguration.getLongPressTimeout();
                }
                j2 = j - ((long) longPressTimeout);
            }
            this.f1156b.removeCallbacks(this.f1160f);
            this.f1156b.postDelayed(this.f1160f, j2);
        }
    }

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

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