package com.bytedance.sdk.openadsdk.core.duv.fm;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.os.Process;
import android.util.ArrayMap;
import com.bytedance.sdk.component.utils.sds;
import com.bytedance.sdk.openadsdk.core.gof;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.concurrent.atomic.AtomicBoolean;
import org.json.JSONException;
import org.json.JSONObject;

/* JADX INFO: loaded from: classes2.dex */
public class ro {
    private static volatile ro fm;
    private final ArrayList<String> ro = new ArrayList<>();

    /* JADX INFO: renamed from: lb, reason: collision with root package name */
    private final AtomicBoolean f7079lb = new AtomicBoolean(false);
    private long yz = System.currentTimeMillis();
    private long jnr = 0;
    private long ajl = 0;
    private String wsy = "";
    private String wu = "";

    /* JADX INFO: renamed from: ef, reason: collision with root package name */
    private String f7078ef = "";
    private boolean vt = false;
    private boolean ku = false;

    public static ro fm(Application application) {
        if (fm == null) {
            synchronized (ro.class) {
                try {
                    if (fm == null) {
                        ro roVar = new ro();
                        fm = roVar;
                        roVar.vt = fm((Context) application);
                        fm.ku = fm(application.getApplicationContext(), "android.permission.SYSTEM_ALERT_WINDOW") == 0;
                        fm.fm();
                    }
                } catch (Throwable th) {
                    throw th;
                }
            }
        }
        return fm;
    }

    public void ro(Activity activity) {
        String localClassName = activity.getLocalClassName();
        if (this.ro.contains(localClassName)) {
            this.ro.remove(localClassName);
        }
        if (this.ro.size() == 0) {
            this.yz = System.currentTimeMillis();
            this.f7079lb.set(true);
            this.wu = localClassName;
        }
    }

    private static int fm(Context context, String str) {
        try {
            return context.checkPermission(str, Process.myPid(), Process.myUid());
        } catch (Throwable unused) {
            return -1;
        }
    }

    private static boolean fm(Context context) {
        ApplicationInfo applicationInfo;
        return (context == null || (applicationInfo = context.getApplicationInfo()) == null || (applicationInfo.flags & 1) <= 0) ? false : true;
    }

    public void fm(Activity activity) {
        String localClassName = activity.getLocalClassName();
        if (this.ro.size() == 0) {
            this.wsy = localClassName;
            this.jnr = System.currentTimeMillis();
            this.ajl = System.currentTimeMillis() - this.yz;
            this.f7079lb.set(false);
        }
        if (!this.ro.contains(localClassName)) {
            this.ro.add(localClassName);
        }
        if (localClassName.contains("com.bytedance.sdk.openadsdk.activity.TTFullScreenExpressVideoActivity") || localClassName.contains("com.bytedance.sdk.openadsdk.activity.TTRewardExpressVideoActivity")) {
            return;
        }
        this.f7078ef = localClassName;
    }

    private void fm() {
        int size;
        try {
            Class<?> cls = Class.forName("android.app.ActivityThread");
            Method declaredMethod = cls.getDeclaredMethod("currentActivityThread", new Class[0]);
            declaredMethod.setAccessible(true);
            Object objInvoke = declaredMethod.invoke(null, new Object[0]);
            Field declaredField = cls.getDeclaredField("mActivities");
            declaredField.setAccessible(true);
            ArrayMap arrayMap = (ArrayMap) declaredField.get(objInvoke);
            if (arrayMap != null && (size = arrayMap.size()) > 0) {
                Class<?> cls2 = Class.forName("android.app.ActivityThread$ActivityClientRecord");
                Field declaredField2 = cls2.getDeclaredField("stopped");
                declaredField2.setAccessible(true);
                Field declaredField3 = cls2.getDeclaredField("activity");
                declaredField3.setAccessible(true);
                for (int i10 = 0; i10 < size; i10++) {
                    Object objValueAt = arrayMap.valueAt(i10);
                    if (!((Boolean) declaredField2.get(objValueAt)).booleanValue()) {
                        String localClassName = ((Activity) declaredField3.get(objValueAt)).getLocalClassName();
                        if (!this.ro.contains(localClassName)) {
                            this.ro.add(localClassName);
                        }
                    }
                }
                this.f7079lb.set(this.ro.size() <= 0);
            }
        } catch (Throwable unused) {
        }
    }

    public String fm(String str, long j10, int i10) {
        String string;
        long jCurrentTimeMillis = System.currentTimeMillis();
        long j11 = jCurrentTimeMillis - this.jnr;
        long j12 = jCurrentTimeMillis - j10;
        int i11 = j12 < 500 ? 1 : 0;
        if (this.f7079lb.get() && this.ku) {
            i11 |= 2;
        }
        if (!this.f7079lb.get() && this.ajl >= 5000 && j11 < 1000) {
            i11 = this.wu.equals(this.f7078ef) ? i11 | 4 : i11 | 8;
        }
        try {
            string = new JSONObject().put("rst", i11).put("adtag", str).put("bakdur", this.ajl).put("rit", i10).put("poptime", j11).put("unlocktime", j12).put("bakground", this.f7079lb).put("alert", this.ku).put("sys", this.vt).put("actsize", this.ro.size()).put("mutiproc", true ^ sds.fm(gof.fm())).toString();
        } catch (JSONException unused) {
            string = "";
        }
        this.wsy = "";
        this.ajl = 0L;
        this.jnr = 0L;
        this.yz = System.currentTimeMillis();
        return string;
    }
}
