package com.google.android.gms.internal.ads;

import android.app.Activity;
import android.app.Application;
import android.app.KeyguardManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.IntentFilter;
import android.graphics.Rect;
import android.os.Build;
import android.os.Bundle;
import android.os.PowerManager;
import android.os.SystemClock;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewParent;
import android.view.ViewTreeObserver;
import android.view.Window;
import android.view.WindowManager;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;

/* JADX INFO: loaded from: classes2.dex */
public final class lj implements View.OnAttachStateChangeListener, ViewTreeObserver.OnGlobalLayoutListener, ViewTreeObserver.OnScrollChangedListener, Application.ActivityLifecycleCallbacks {
    public static final long Q = ((Long) n7.y.f31093e.f31096c.b(ao.K1)).longValue();

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final Context f13718a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final Application f13719b;

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

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

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public WeakReference f13722e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public final WeakReference f13723f;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public final dh f13724g;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public final p7.b0 f13725h = new p7.b0(Q);

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    public boolean f13726i = false;

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    public int f13727j = -1;

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    public final HashSet f13728k = new HashSet();

    /* JADX INFO: renamed from: l, reason: collision with root package name */
    public final DisplayMetrics f13729l;

    /* JADX INFO: renamed from: m, reason: collision with root package name */
    public final Rect f13730m;

    @Nullable
    @VisibleForTesting
    BroadcastReceiver zza;

    public lj(Context context, View view) {
        Context applicationContext = context.getApplicationContext();
        this.f13718a = applicationContext;
        WindowManager windowManager = (WindowManager) applicationContext.getSystemService("window");
        this.f13720c = (PowerManager) applicationContext.getSystemService("power");
        this.f13721d = (KeyguardManager) applicationContext.getSystemService("keyguard");
        if (applicationContext instanceof Application) {
            Application application = (Application) applicationContext;
            this.f13719b = application;
            this.f13724g = new dh(application, this, 1);
        }
        this.f13729l = context.getResources().getDisplayMetrics();
        Rect rect = new Rect();
        this.f13730m = rect;
        rect.right = windowManager.getDefaultDisplay().getWidth();
        rect.bottom = windowManager.getDefaultDisplay().getHeight();
        WeakReference weakReference = this.f13723f;
        View view2 = weakReference != null ? (View) weakReference.get() : null;
        if (view2 != null) {
            view2.removeOnAttachStateChangeListener(this);
            f(view2);
        }
        this.f13723f = new WeakReference(view);
        if (view != null) {
            if (view.isAttachedToWindow()) {
                e(view);
            }
            view.addOnAttachStateChangeListener(this);
        }
    }

    public final Rect a(Rect rect) {
        float f10 = rect.left;
        float f11 = this.f13729l.density;
        return new Rect((int) (f10 / f11), (int) (rect.top / f11), (int) (rect.right / f11), (int) (rect.bottom / f11));
    }

    public final void b() {
        p7.r0.f31763l.post(new mv1(this, 9));
    }

    public final void c(Activity activity, int i10) {
        Window window;
        WeakReference weakReference = this.f13723f;
        if (weakReference == null || (window = activity.getWindow()) == null) {
            return;
        }
        View viewPeekDecorView = window.peekDecorView();
        View view = (View) weakReference.get();
        if (view == null || viewPeekDecorView == null || view.getRootView() != viewPeekDecorView.getRootView()) {
            return;
        }
        this.f13727j = i10;
    }

    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Removed duplicated region for block: B:51:0x012d  */
    /* JADX WARN: Removed duplicated region for block: B:53:0x0132  */
    /* JADX WARN: Type inference failed for: r0v10, types: [java.util.List] */
    /* JADX WARN: Type inference failed for: r0v11 */
    /* JADX WARN: Type inference failed for: r0v29, types: [java.util.List] */
    /* JADX WARN: Type inference failed for: r0v30, types: [java.util.ArrayList] */
    /* JADX WARN: Type inference failed for: r26v0, types: [boolean] */
    /* JADX WARN: Type inference failed for: r27v0, types: [java.util.List] */
    /* JADX WARN: Type inference failed for: r4v16 */
    /* JADX WARN: Type inference failed for: r4v3 */
    /* JADX WARN: Type inference failed for: r4v4 */
    /* JADX WARN: Type inference failed for: r4v5 */
    /* JADX WARN: Type inference failed for: r4v7, types: [boolean] */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final void d(int i10) {
        WeakReference weakReference;
        View view;
        boolean globalVisibleRect;
        boolean localVisibleRect;
        ?? EmptyList;
        ?? r42;
        boolean z10;
        boolean z11;
        boolean z12;
        boolean z13;
        int i11;
        HashSet hashSet = this.f13728k;
        if (hashSet.isEmpty() || (weakReference = this.f13723f) == null) {
            return;
        }
        View view2 = (View) weakReference.get();
        Rect rect = new Rect();
        Rect rect2 = new Rect();
        Rect rect3 = new Rect();
        Rect rect4 = new Rect();
        int[] iArr = new int[2];
        int[] iArr2 = new int[2];
        if (view2 != null) {
            globalVisibleRect = view2.getGlobalVisibleRect(rect2);
            localVisibleRect = view2.getLocalVisibleRect(rect3);
            view2.getHitRect(rect4);
            try {
                view2.getLocationOnScreen(iArr);
                view2.getLocationInWindow(iArr2);
            } catch (Exception unused) {
            }
            if (((Boolean) n7.y.f31093e.f31096c.b(ao.S5)).booleanValue()) {
                rect.left = iArr2[0];
                rect.top = iArr2[1];
            } else {
                rect.left = iArr[0];
                rect.top = iArr[1];
            }
            rect.right = view2.getWidth() + rect.left;
            rect.bottom = view2.getHeight() + rect.top;
            view = view2;
        } else {
            view = null;
            globalVisibleRect = false;
            localVisibleRect = false;
        }
        if (!((Boolean) n7.y.f31093e.f31096c.b(ao.N1)).booleanValue() || view == null) {
            EmptyList = Collections.emptyList();
        } else {
            try {
                EmptyList = new ArrayList();
                ViewParent parent = view.getParent();
                while (parent instanceof View) {
                    View view3 = (View) parent;
                    Rect rect5 = new Rect();
                    if (view3.isScrollContainer() && view3.getGlobalVisibleRect(rect5)) {
                        EmptyList.add(a(rect5));
                    }
                    parent = parent.getParent();
                }
            } catch (Exception e10) {
                m7.n.C.f30469h.d("PositionWatcher.getParentScrollViewRects", e10);
                EmptyList = Collections.emptyList();
            }
        }
        ?? r27 = EmptyList;
        int windowVisibility = view != null ? view.getWindowVisibility() : 8;
        int i12 = this.f13727j;
        if (i12 != -1) {
            windowVisibility = i12;
        }
        m7.n nVar = m7.n.C;
        p7.r0 r0Var = nVar.f30464c;
        long jA = p7.r0.a(view);
        tn tnVar = ao.Nb;
        n7.y yVar = n7.y.f31093e;
        boolean zBooleanValue = ((Boolean) yVar.f31096c.b(tnVar)).booleanValue();
        yn ynVar = yVar.f31096c;
        KeyguardManager keyguardManager = this.f13721d;
        PowerManager powerManager = this.f13720c;
        if (zBooleanValue) {
            if (view2 != null && p7.r0.o(view, powerManager, keyguardManager)) {
                if (globalVisibleRect) {
                    if (localVisibleRect) {
                        if (jA < ((Integer) ynVar.b(ao.Qb)).intValue() || windowVisibility != 0) {
                            z10 = false;
                        } else {
                            z10 = true;
                            windowVisibility = 0;
                        }
                        globalVisibleRect = true;
                        localVisibleRect = true;
                        r42 = z10;
                    }
                    r42 = 0;
                    globalVisibleRect = true;
                    localVisibleRect = false;
                }
                r42 = 0;
                globalVisibleRect = false;
            }
            r42 = 0;
        } else {
            if (view2 != null && p7.r0.o(view, powerManager, keyguardManager)) {
                if (globalVisibleRect) {
                    if (localVisibleRect) {
                        if (windowVisibility == 0) {
                        }
                        globalVisibleRect = true;
                        localVisibleRect = true;
                        r42 = z10;
                    }
                    r42 = 0;
                    globalVisibleRect = true;
                    localVisibleRect = false;
                }
                r42 = 0;
                globalVisibleRect = false;
            }
            r42 = 0;
        }
        if (((Boolean) ynVar.b(ao.Sb)).booleanValue()) {
            int i13 = true != p7.r0.o(view, powerManager, keyguardManager) ? 0 : 64;
            int i14 = true != globalVisibleRect ? 0 : 8;
            int i15 = true != localVisibleRect ? 0 : 16;
            if (windowVisibility == 0) {
                i11 = 128;
                z12 = localVisibleRect;
            } else {
                z12 = localVisibleRect;
                i11 = 0;
            }
            z11 = globalVisibleRect;
            p7.r0.i(view, (jA >= ((long) ((Integer) ynVar.b(ao.Qb)).intValue()) ? 32 : 0) | i13 | i14 | i15 | i11 | r42);
        } else {
            z11 = globalVisibleRect;
            z12 = localVisibleRect;
        }
        if (i10 == 1 && !this.f13725h.a() && r42 == this.f13726i) {
            return;
        }
        if (r42 != 0 || this.f13726i) {
            z13 = true;
        } else {
            z13 = true;
            if (i10 == 1) {
                return;
            }
        }
        nVar.f30472k.getClass();
        SystemClock.elapsedRealtime();
        powerManager.isScreenOn();
        boolean z14 = (view == null || !view.isAttachedToWindow()) ? false : z13;
        int windowVisibility2 = view != null ? view.getWindowVisibility() : 8;
        Rect rectA = a(this.f13730m);
        Rect rectA2 = a(rect);
        Rect rectA3 = a(rect2);
        Rect rectA4 = a(rect3);
        Rect rectA5 = a(rect4);
        float f10 = this.f13729l.density;
        jj jjVar = new jj(z14, windowVisibility2, rectA, rectA2, rectA3, z11, rectA4, z12, rectA5, r42, r27);
        Iterator it = hashSet.iterator();
        while (it.hasNext()) {
            ((kj) it.next()).p(jjVar);
        }
        this.f13726i = r42;
    }

    public final void e(View view) {
        ViewTreeObserver viewTreeObserver = view.getViewTreeObserver();
        if (viewTreeObserver.isAlive()) {
            this.f13722e = new WeakReference(viewTreeObserver);
            viewTreeObserver.addOnScrollChangedListener(this);
            viewTreeObserver.addOnGlobalLayoutListener(this);
        }
        if (this.zza == null) {
            IntentFilter intentFilter = new IntentFilter();
            intentFilter.addAction("android.intent.action.SCREEN_ON");
            intentFilter.addAction("android.intent.action.SCREEN_OFF");
            intentFilter.addAction("android.intent.action.USER_PRESENT");
            e.i0 i0Var = new e.i0(this);
            this.zza = i0Var;
            Context context = this.f13718a;
            p7.i0 i0Var2 = m7.n.C.f30487z;
            synchronized (i0Var2) {
                if (i0Var2.f31696c) {
                    ((Map) i0Var2.f31698e).put(i0Var, intentFilter);
                } else {
                    ao.a(context);
                    if (!((Boolean) n7.y.f31093e.f31096c.b(ao.f8938kc)).booleanValue() || Build.VERSION.SDK_INT < 33) {
                        context.registerReceiver(i0Var, intentFilter);
                    } else {
                        context.registerReceiver(i0Var, intentFilter, 4);
                    }
                }
            }
        }
        Application application = this.f13719b;
        if (application != null) {
            try {
                application.registerActivityLifecycleCallbacks(this.f13724g);
            } catch (Exception unused) {
            }
        }
    }

    public final void f(View view) {
        try {
            WeakReference weakReference = this.f13722e;
            if (weakReference != null) {
                ViewTreeObserver viewTreeObserver = (ViewTreeObserver) weakReference.get();
                if (viewTreeObserver != null && viewTreeObserver.isAlive()) {
                    viewTreeObserver.removeOnScrollChangedListener(this);
                    viewTreeObserver.removeGlobalOnLayoutListener(this);
                }
                this.f13722e = null;
            }
        } catch (Exception unused) {
        }
        try {
            ViewTreeObserver viewTreeObserver2 = view.getViewTreeObserver();
            if (viewTreeObserver2.isAlive()) {
                viewTreeObserver2.removeOnScrollChangedListener(this);
                viewTreeObserver2.removeGlobalOnLayoutListener(this);
            }
        } catch (Exception unused2) {
        }
        BroadcastReceiver broadcastReceiver = this.zza;
        if (broadcastReceiver != null) {
            try {
                m7.n.C.f30487z.c(this.f13718a, broadcastReceiver);
            } catch (IllegalStateException unused3) {
            } catch (Exception e10) {
                m7.n.C.f30469h.d("ActiveViewUnit.stopScreenStatusMonitoring", e10);
            }
            this.zza = null;
        }
        Application application = this.f13719b;
        if (application != null) {
            try {
                application.unregisterActivityLifecycleCallbacks(this.f13724g);
            } catch (Exception unused4) {
            }
        }
    }

    @Override // android.app.Application.ActivityLifecycleCallbacks
    public final void onActivityCreated(Activity activity, Bundle bundle) {
        c(activity, 0);
        d(3);
        b();
    }

    @Override // android.app.Application.ActivityLifecycleCallbacks
    public final void onActivityDestroyed(Activity activity) {
        d(3);
        b();
    }

    @Override // android.app.Application.ActivityLifecycleCallbacks
    public final void onActivityPaused(Activity activity) {
        c(activity, 4);
        d(3);
        b();
    }

    @Override // android.app.Application.ActivityLifecycleCallbacks
    public final void onActivityResumed(Activity activity) {
        c(activity, 0);
        d(3);
        b();
    }

    @Override // android.app.Application.ActivityLifecycleCallbacks
    public final void onActivitySaveInstanceState(Activity activity, Bundle bundle) {
        d(3);
        b();
    }

    @Override // android.app.Application.ActivityLifecycleCallbacks
    public final void onActivityStarted(Activity activity) {
        c(activity, 0);
        d(3);
        b();
    }

    @Override // android.app.Application.ActivityLifecycleCallbacks
    public final void onActivityStopped(Activity activity) {
        d(3);
        b();
    }

    @Override // android.view.ViewTreeObserver.OnGlobalLayoutListener
    public final void onGlobalLayout() {
        d(2);
        b();
    }

    @Override // android.view.ViewTreeObserver.OnScrollChangedListener
    public final void onScrollChanged() {
        d(1);
    }

    @Override // android.view.View.OnAttachStateChangeListener
    public final void onViewAttachedToWindow(View view) {
        this.f13727j = -1;
        e(view);
        d(3);
    }

    @Override // android.view.View.OnAttachStateChangeListener
    public final void onViewDetachedFromWindow(View view) {
        this.f13727j = -1;
        d(3);
        b();
        f(view);
    }
}
