package bi;

import android.content.Context;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.Display;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.WindowManager;
import ei.k;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/* loaded from: classes4.dex */
public abstract class i extends bi.a {

    /* renamed from: u, reason: collision with root package name */
    private static boolean f6908u;

    /* renamed from: v, reason: collision with root package name */
    private static int f6909v = com.bumptech.glide.h.f12526a;

    /* renamed from: p, reason: collision with root package name */
    protected final View f6910p;

    /* renamed from: q, reason: collision with root package name */
    private final a f6911q;

    /* renamed from: r, reason: collision with root package name */
    private View.OnAttachStateChangeListener f6912r;

    /* renamed from: s, reason: collision with root package name */
    private boolean f6913s;

    /* renamed from: t, reason: collision with root package name */
    private boolean f6914t;

    /* loaded from: classes4.dex */
    static final class a {

        /* renamed from: e, reason: collision with root package name */
        static Integer f6915e;

        /* renamed from: a, reason: collision with root package name */
        private final View f6916a;

        /* renamed from: b, reason: collision with root package name */
        private final List f6917b = new ArrayList();

        /* renamed from: c, reason: collision with root package name */
        boolean f6918c;

        /* renamed from: d, reason: collision with root package name */
        private ViewTreeObserverOnPreDrawListenerC0181a f6919d;

        /* JADX INFO: Access modifiers changed from: private */
        /* renamed from: bi.i$a$a, reason: collision with other inner class name */
        /* loaded from: classes4.dex */
        public static final class ViewTreeObserverOnPreDrawListenerC0181a implements ViewTreeObserver.OnPreDrawListener {

            /* renamed from: p, reason: collision with root package name */
            private final WeakReference f6920p;

            ViewTreeObserverOnPreDrawListenerC0181a(a aVar) {
                this.f6920p = new WeakReference(aVar);
            }

            @Override // android.view.ViewTreeObserver.OnPreDrawListener
            public boolean onPreDraw() {
                if (Log.isLoggable("ViewTarget", 2)) {
                    Log.v("ViewTarget", "OnGlobalLayoutListener called attachStateListener=" + this);
                }
                a aVar = (a) this.f6920p.get();
                if (aVar == null) {
                    return true;
                }
                aVar.a();
                return true;
            }
        }

        a(View view) {
            this.f6916a = view;
        }

        private static int c(Context context) {
            if (f6915e == null) {
                Display defaultDisplay = ((WindowManager) k.d((WindowManager) context.getSystemService("window"))).getDefaultDisplay();
                Point point = new Point();
                defaultDisplay.getSize(point);
                f6915e = Integer.valueOf(Math.max(point.x, point.y));
            }
            return f6915e.intValue();
        }

        private int e(int i10, int i11, int i12) {
            int i13 = i11 - i12;
            if (i13 > 0) {
                return i13;
            }
            if (this.f6918c && this.f6916a.isLayoutRequested()) {
                return 0;
            }
            int i14 = i10 - i12;
            if (i14 > 0) {
                return i14;
            }
            if (this.f6916a.isLayoutRequested() || i11 != -2) {
                return 0;
            }
            if (Log.isLoggable("ViewTarget", 4)) {
                Log.i("ViewTarget", "Glide treats LayoutParams.WRAP_CONTENT as a request for an image the size of this device's screen dimensions. If you want to load the original image and are ok with the corresponding memory cost and OOMs (depending on the input size), use override(Target.SIZE_ORIGINAL). Otherwise, use LayoutParams.MATCH_PARENT, set layout_width and layout_height to fixed dimension, or use .override() with fixed dimensions.");
            }
            return c(this.f6916a.getContext());
        }

        private int f() {
            int paddingTop = this.f6916a.getPaddingTop() + this.f6916a.getPaddingBottom();
            ViewGroup.LayoutParams layoutParams = this.f6916a.getLayoutParams();
            return e(this.f6916a.getHeight(), layoutParams != null ? layoutParams.height : 0, paddingTop);
        }

        private int g() {
            int paddingLeft = this.f6916a.getPaddingLeft() + this.f6916a.getPaddingRight();
            ViewGroup.LayoutParams layoutParams = this.f6916a.getLayoutParams();
            return e(this.f6916a.getWidth(), layoutParams != null ? layoutParams.width : 0, paddingLeft);
        }

        private boolean h(int i10) {
            return i10 > 0 || i10 == Integer.MIN_VALUE;
        }

        private boolean i(int i10, int i11) {
            return h(i10) && h(i11);
        }

        private void j(int i10, int i11) {
            Iterator it = new ArrayList(this.f6917b).iterator();
            while (it.hasNext()) {
                ((g) it.next()).e(i10, i11);
            }
        }

        void a() {
            if (this.f6917b.isEmpty()) {
                return;
            }
            int g10 = g();
            int f10 = f();
            if (i(g10, f10)) {
                j(g10, f10);
                b();
            }
        }

        void b() {
            ViewTreeObserver viewTreeObserver = this.f6916a.getViewTreeObserver();
            if (viewTreeObserver.isAlive()) {
                viewTreeObserver.removeOnPreDrawListener(this.f6919d);
            }
            this.f6919d = null;
            this.f6917b.clear();
        }

        void d(g gVar) {
            int g10 = g();
            int f10 = f();
            if (i(g10, f10)) {
                gVar.e(g10, f10);
                return;
            }
            if (!this.f6917b.contains(gVar)) {
                this.f6917b.add(gVar);
            }
            if (this.f6919d == null) {
                ViewTreeObserver viewTreeObserver = this.f6916a.getViewTreeObserver();
                ViewTreeObserverOnPreDrawListenerC0181a viewTreeObserverOnPreDrawListenerC0181a = new ViewTreeObserverOnPreDrawListenerC0181a(this);
                this.f6919d = viewTreeObserverOnPreDrawListenerC0181a;
                viewTreeObserver.addOnPreDrawListener(viewTreeObserverOnPreDrawListenerC0181a);
            }
        }

        void k(g gVar) {
            this.f6917b.remove(gVar);
        }
    }

    public i(View view) {
        this.f6910p = (View) k.d(view);
        this.f6911q = new a(view);
    }

    private Object i() {
        return this.f6910p.getTag(f6909v);
    }

    private void j() {
        View.OnAttachStateChangeListener onAttachStateChangeListener = this.f6912r;
        if (onAttachStateChangeListener == null || this.f6914t) {
            return;
        }
        this.f6910p.addOnAttachStateChangeListener(onAttachStateChangeListener);
        this.f6914t = true;
    }

    private void k() {
        View.OnAttachStateChangeListener onAttachStateChangeListener = this.f6912r;
        if (onAttachStateChangeListener == null || !this.f6914t) {
            return;
        }
        this.f6910p.removeOnAttachStateChangeListener(onAttachStateChangeListener);
        this.f6914t = false;
    }

    private void l(Object obj) {
        f6908u = true;
        this.f6910p.setTag(f6909v, obj);
    }

    @Override // bi.h
    public void a(g gVar) {
        this.f6911q.k(gVar);
    }

    @Override // bi.h
    public void b(com.bumptech.glide.request.c cVar) {
        l(cVar);
    }

    @Override // bi.h
    public void c(g gVar) {
        this.f6911q.d(gVar);
    }

    @Override // bi.a, bi.h
    public void e(Drawable drawable) {
        super.e(drawable);
        j();
    }

    @Override // bi.h
    public com.bumptech.glide.request.c f() {
        Object i10 = i();
        if (i10 == null) {
            return null;
        }
        if (i10 instanceof com.bumptech.glide.request.c) {
            return (com.bumptech.glide.request.c) i10;
        }
        throw new IllegalArgumentException("You must not call setTag() on a view Glide is targeting");
    }

    @Override // bi.a, bi.h
    public void g(Drawable drawable) {
        super.g(drawable);
        this.f6911q.b();
        if (this.f6913s) {
            return;
        }
        k();
    }

    public String toString() {
        return "Target for: " + this.f6910p;
    }
}
