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

import android.app.Activity;
import android.app.KeyguardManager;
import android.content.Context;
import android.content.ContextWrapper;
import android.os.Handler;
import android.os.Looper;
import android.view.View;
import android.view.WindowManager;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Objects;
import java.util.WeakHashMap;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import org.json.JSONException;
import org.json.JSONObject;

/* JADX INFO: loaded from: classes2.dex */
public final class ig implements Runnable {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final /* synthetic */ int f12493a;

    /* JADX WARN: Multi-variable type inference failed */
    @Override // java.lang.Runnable
    public final void run() {
        CountDownLatch countDownLatch;
        HashSet hashSet;
        HashSet hashSet2;
        boolean z10;
        KeyguardManager keyguardManager;
        Iterator it;
        HashSet hashSet3;
        Activity activity;
        Boolean bool;
        String str;
        switch (this.f12493a) {
            case 0:
                try {
                    jg.f12842b = MessageDigest.getInstance("MD5");
                    countDownLatch = jg.f12845e;
                } catch (NoSuchAlgorithmException unused) {
                    countDownLatch = jg.f12845e;
                } catch (Throwable th) {
                    jg.f12845e.countDown();
                    throw th;
                }
                countDownLatch.countDown();
                return;
            case 2:
                Looper.myLooper().quit();
                return;
            case 6:
                tg1 tg1Var = tg1.f16881g;
                tg1Var.getClass();
                tg1Var.f16887b.clear();
                for (uf1 uf1Var : Collections.unmodifiableCollection(cg1.f9890c.f9892b)) {
                }
                tg1Var.f16891f = System.nanoTime();
                sg1 sg1Var = tg1Var.f16889d;
                sg1Var.getClass();
                cg1 cg1Var = cg1.f9890c;
                HashMap map = sg1Var.f16439c;
                HashMap map2 = sg1Var.f16443g;
                HashSet hashSet4 = sg1Var.f16442f;
                HashMap map3 = sg1Var.f16438b;
                HashMap map4 = sg1Var.f16437a;
                HashSet hashSet5 = sg1Var.f16440d;
                HashSet hashSet6 = sg1Var.f16444h;
                HashSet hashSet7 = sg1Var.f16441e;
                if (cg1Var != null) {
                    Iterator it2 = Collections.unmodifiableCollection(cg1Var.f9892b).iterator();
                    while (it2.hasNext()) {
                        uf1 uf1Var2 = (uf1) it2.next();
                        View view = (View) uf1Var2.f17229c.get();
                        if (!uf1Var2.f17231e || uf1Var2.f17232f) {
                            it = it2;
                            hashSet3 = hashSet6;
                            it2 = it;
                            hashSet6 = hashSet3;
                        } else {
                            String str2 = uf1Var2.f17233g;
                            if (view != null) {
                                Context context = view.getContext();
                                while (true) {
                                    if (!(context instanceof ContextWrapper)) {
                                        activity = null;
                                    } else if (context instanceof Activity) {
                                        activity = (Activity) context;
                                    } else {
                                        context = ((ContextWrapper) context).getBaseContext();
                                    }
                                }
                                boolean zIsInPictureInPictureMode = activity != null ? activity.isInPictureInPictureMode() : false;
                                if (zIsInPictureInPictureMode) {
                                    hashSet6.add(str2);
                                }
                                it = it2;
                                if (view.isAttachedToWindow()) {
                                    boolean zHasWindowFocus = view.hasWindowFocus();
                                    hashSet3 = hashSet6;
                                    WeakHashMap weakHashMap = sg1Var.f16445i;
                                    if (zHasWindowFocus) {
                                        weakHashMap.remove(view);
                                        bool = Boolean.FALSE;
                                    } else if (weakHashMap.containsKey(view)) {
                                        bool = (Boolean) weakHashMap.get(view);
                                    } else {
                                        bool = Boolean.FALSE;
                                        weakHashMap.put(view, bool);
                                    }
                                    if (!bool.booleanValue() || zIsInPictureInPictureMode) {
                                        HashSet hashSet8 = new HashSet();
                                        View view2 = view;
                                        while (true) {
                                            if (view2 == null) {
                                                hashSet5.addAll(hashSet8);
                                                str = null;
                                            } else {
                                                String strD = ni.d(view2);
                                                if (strD != null) {
                                                    str = strD;
                                                } else {
                                                    hashSet8.add(view2);
                                                    Object parent = view2.getParent();
                                                    view2 = parent instanceof View ? (View) parent : null;
                                                }
                                            }
                                        }
                                    } else {
                                        str = "noWindowFocus";
                                    }
                                } else {
                                    str = "notAttached";
                                    hashSet3 = hashSet6;
                                }
                                if (str == null) {
                                    hashSet7.add(str2);
                                    map4.put(view, str2);
                                    for (fg1 fg1Var : uf1Var2.f17228b.f11742a) {
                                        View view3 = (View) fg1Var.f11212a.get();
                                        if (view3 != null) {
                                            rg1 rg1Var = (rg1) map3.get(view3);
                                            if (rg1Var != null) {
                                                rg1Var.f16135b.add(str2);
                                            } else {
                                                map3.put(view3, new rg1(fg1Var, str2));
                                            }
                                        }
                                    }
                                } else if (str != "noWindowFocus") {
                                    hashSet4.add(str2);
                                    map.put(str2, view);
                                    map2.put(str2, str);
                                }
                                it2 = it;
                                hashSet6 = hashSet3;
                            } else {
                                hashSet4.add(str2);
                                map2.put(str2, "noAdView");
                            }
                        }
                    }
                }
                HashSet hashSet9 = hashSet6;
                long jNanoTime = System.nanoTime();
                uz0 uz0Var = tg1Var.f16888c;
                jc1 jc1Var = (jc1) uz0Var.f17460b;
                int size = hashSet4.size();
                uz0 uz0Var2 = tg1Var.f16890e;
                if (size > 0) {
                    Iterator it3 = hashSet4.iterator();
                    while (it3.hasNext()) {
                        String str3 = (String) it3.next();
                        JSONObject jSONObjectF = jc1Var.f(null);
                        View view4 = (View) map.get(str3);
                        Iterator it4 = it3;
                        jc1 jc1Var2 = (jc1) uz0Var.f17461c;
                        uz0 uz0Var3 = uz0Var;
                        String str4 = (String) map2.get(str3);
                        if (str4 != null) {
                            JSONObject jSONObjectF2 = jc1Var2.f(view4);
                            WindowManager windowManager = qg1.f15732a;
                            try {
                                jSONObjectF2.put("adSessionId", str3);
                            } catch (JSONException unused2) {
                                ni.g();
                            }
                            try {
                                jSONObjectF2.put("notVisibleReason", str4);
                            } catch (JSONException unused3) {
                                ni.g();
                            }
                            qg1.c(jSONObjectF, jSONObjectF2);
                            break;
                        }
                        qg1.d(jSONObjectF);
                        HashSet hashSet10 = new HashSet();
                        hashSet10.add(str3);
                        uz0Var2.getClass();
                        ((eb) uz0Var2.f17460b).c(new xg1(uz0Var2, hashSet10, jSONObjectF, jNanoTime, 0));
                        it3 = it4;
                        uz0Var = uz0Var3;
                    }
                }
                if (hashSet7.size() > 0) {
                    JSONObject jSONObjectF3 = jc1Var.f(null);
                    z10 = false;
                    hashSet = hashSet7;
                    hashSet2 = hashSet9;
                    tg1Var.c(null, jc1Var, jSONObjectF3, 1, false);
                    qg1.d(jSONObjectF3);
                    uz0Var2.getClass();
                    ((eb) uz0Var2.f17460b).c(new xg1(uz0Var2, hashSet, jSONObjectF3, jNanoTime, 1));
                } else {
                    hashSet = hashSet7;
                    hashSet2 = hashSet9;
                    z10 = false;
                    uz0Var2.getClass();
                    ((eb) uz0Var2.f17460b).c(new wg1(uz0Var2));
                }
                map4.clear();
                map3.clear();
                map.clear();
                hashSet5.clear();
                hashSet.clear();
                hashSet4.clear();
                map2.clear();
                sg1Var.f16446j = z10;
                hashSet2.clear();
                long jNanoTime2 = System.nanoTime() - tg1Var.f16891f;
                ArrayList arrayList = tg1Var.f16886a;
                if (arrayList.size() > 0) {
                    Iterator it5 = arrayList.iterator();
                    if (it5.hasNext()) {
                        com.google.android.gms.measurement.internal.u1.x(it5.next());
                        TimeUnit.NANOSECONDS.toMillis(jNanoTime2);
                        throw null;
                    }
                }
                jg1 jg1Var = jg1.f12851d;
                Context context2 = (Context) jg1Var.f12852a.get();
                if (context2 == null || (keyguardManager = (KeyguardManager) context2.getSystemService("keyguard")) == null) {
                    return;
                }
                boolean zIsDeviceLocked = keyguardManager.isDeviceLocked();
                jg1Var.a(jg1Var.f12853b, zIsDeviceLocked);
                jg1Var.f12854c = zIsDeviceLocked;
                return;
            case 7:
                Handler handler = tg1.f16883i;
                if (handler != null) {
                    handler.post(tg1.f16884j);
                    tg1.f16883i.postDelayed(tg1.f16885k, 200L);
                    return;
                }
                return;
            default:
                return;
        }
    }

    public /* synthetic */ ig(int i10, int i11) {
        this.f12493a = i10;
    }

    public ig(z30 z30Var) {
        this.f12493a = 2;
        Objects.requireNonNull(z30Var);
    }
}
