package com.bytedance.sdk.openadsdk.common;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.TextUtils;
import androidx.media3.exoplayer.rtsp.SessionDescription;
import com.bytedance.sdk.openadsdk.AdSlot;
import com.bytedance.sdk.openadsdk.CacheDirFactory;
import com.bytedance.sdk.openadsdk.core.model.ai;
import com.bytedance.sdk.openadsdk.core.model.lse;
import defpackage.e;
import e6.a;
import java.io.File;
import java.io.FileFilter;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;

/* JADX INFO: loaded from: classes2.dex */
public class ro {
    private static final HashMap<String, ro> fm = new HashMap<>();
    private static final HashMap<String, fm> ro = new HashMap<>();
    private final Map<lse, Long> ajl = Collections.synchronizedMap(new HashMap());
    private final String jnr;

    /* JADX INFO: renamed from: lb, reason: collision with root package name */
    @SuppressLint({"StaticFieldLeak"})
    private final Context f6859lb;
    private final fm yz;

    private ro(Context context, String str) {
        this.f6859lb = context;
        fm fmVar = ro.get(str);
        this.yz = fmVar == null ? new fm(str) : fmVar;
        this.jnr = str;
    }

    public void lb(String str) {
        this.yz.ajl(str);
    }

    public String ro(String str) {
        try {
            String strRo = this.yz.ro(str);
            if (TextUtils.isEmpty(strRo)) {
                return null;
            }
            return strRo;
        } catch (Throwable unused) {
            return null;
        }
    }

    public boolean yz(String str) {
        return this.yz.wsy(str);
    }

    public static ro fm(String str) {
        ro roVar;
        HashMap<String, ro> map = fm;
        ro roVar2 = map.get(str);
        if (roVar2 != null) {
            return roVar2;
        }
        synchronized (ro.class) {
            try {
                roVar = map.get(str);
                if (roVar == null) {
                    roVar = new ro(com.bytedance.sdk.openadsdk.core.gof.fm(), str);
                    map.put(str, roVar);
                }
            } catch (Throwable th) {
                throw th;
            }
        }
        return roVar;
    }

    private String ro() {
        return ((a) CacheDirFactory.getICacheDir(0)).b();
    }

    public static class fm {
        protected final String fm;
        private final HashSet<String> wsy = new HashSet<>();
        String ro = "material_data";

        /* JADX INFO: renamed from: lb, reason: collision with root package name */
        String f6860lb = "has_played";
        String yz = "create_time";
        String jnr = "in_use_process";
        String ajl = "req_id";

        public fm(String str) {
            this.fm = TextUtils.isEmpty(str) ? "" : str;
        }

        public void ajl(String str) {
            try {
                com.bytedance.sdk.openadsdk.gqi.yz.fm.fm(fm(str), this.jnr, com.bytedance.sdk.component.utils.sds.ro(com.bytedance.sdk.openadsdk.core.gof.fm()));
                this.wsy.add(str);
            } catch (Throwable unused) {
            }
        }

        public String fm(String str) {
            if (TextUtils.isEmpty(str)) {
                str = SessionDescription.SUPPORTED_SDP_VERSION;
            }
            return e.p(new StringBuilder(), this.fm, "_cache_", str);
        }

        public boolean jnr(String str) {
            try {
                return com.bytedance.sdk.openadsdk.gqi.yz.fm.fm(fm(str), this.f6860lb, true);
            } catch (Throwable unused) {
                return true;
            }
        }

        public String lb(String str) {
            try {
                return com.bytedance.sdk.openadsdk.gqi.yz.fm.ro(fm(str), this.ajl, (String) null);
            } catch (Throwable unused) {
                return null;
            }
        }

        public String ro(String str) {
            try {
                return com.bytedance.sdk.openadsdk.gqi.yz.fm.ro(fm(str), this.ro, (String) null);
            } catch (Throwable unused) {
                return null;
            }
        }

        public boolean wsy(String str) {
            try {
                String strFm = fm(str);
                String strRo = com.bytedance.sdk.openadsdk.gqi.yz.fm.ro(strFm, this.jnr, "");
                if (TextUtils.isEmpty(strRo)) {
                    return false;
                }
                if (strRo.equals(com.bytedance.sdk.component.utils.sds.ro(com.bytedance.sdk.openadsdk.core.gof.fm())) && !this.wsy.contains(str)) {
                    com.bytedance.sdk.openadsdk.gqi.yz.fm.fm(strFm, this.jnr, "");
                    return false;
                }
            } catch (Throwable unused) {
            }
            return true;
        }

        public void wu(String str) {
            try {
                com.bytedance.sdk.openadsdk.gqi.yz.fm.fm(fm(str));
                this.wsy.remove(str);
            } catch (Throwable unused) {
            }
        }

        public long yz(String str) {
            try {
                return com.bytedance.sdk.openadsdk.gqi.yz.fm.fm(fm(str), this.yz, 0L);
            } catch (Throwable unused) {
                return 0L;
            }
        }

        public void fm(AdSlot adSlot, String str, String str2) {
            try {
                String codeId = adSlot.getCodeId();
                String strFm = fm(codeId);
                com.bytedance.sdk.openadsdk.gqi.yz.fm.fm(strFm, this.f6860lb, Boolean.FALSE);
                com.bytedance.sdk.openadsdk.gqi.yz.fm.fm(strFm, this.yz, Long.valueOf(System.currentTimeMillis()));
                com.bytedance.sdk.openadsdk.gqi.yz.fm.fm(strFm, this.ro, str);
                com.bytedance.sdk.openadsdk.gqi.yz.fm.fm(strFm, this.jnr, "");
                com.bytedance.sdk.openadsdk.gqi.yz.fm.fm(strFm, this.ajl, str2);
                this.wsy.remove(codeId);
            } catch (Throwable unused) {
            }
        }
    }

    public static void fm(String str, fm fmVar) {
        ro.put(str, fmVar);
    }

    public void fm(String str, lse lseVar) {
        if (TextUtils.isEmpty(str)) {
            return;
        }
        String strLb = this.yz.lb(str);
        String strSs = lseVar != null ? lseVar.ss() : "";
        if (TextUtils.isEmpty(strLb) || TextUtils.isEmpty(strSs) || strLb.equals(strSs)) {
            this.yz.wu(str);
        }
    }

    public void fm() {
        File[] fileArrListFiles;
        try {
            boolean zWsy = com.bytedance.sdk.openadsdk.core.vt.wsy(this.jnr);
            File file = new File(this.f6859lb.getDataDir(), zWsy ? "files" : "shared_prefs");
            if (file.exists() && file.isDirectory() && (fileArrListFiles = file.listFiles(new FileFilter() { // from class: com.bytedance.sdk.openadsdk.common.ro.1
                @Override // java.io.FileFilter
                public boolean accept(File file2) {
                    if (file2 != null) {
                        return file2.getName().contains(ro.this.jnr);
                    }
                    return false;
                }
            })) != null) {
                for (File file2 : fileArrListFiles) {
                    if (zWsy) {
                        try {
                            com.bytedance.sdk.component.utils.wsy.lb(file2);
                        } catch (Throwable unused) {
                        }
                    } else {
                        this.f6859lb.deleteSharedPreferences(file2.getName().replace(".xml", ""));
                    }
                }
            }
        } catch (Throwable unused2) {
        }
        try {
            File file3 = new File(ro());
            if (file3.exists() && file3.isDirectory()) {
                com.bytedance.sdk.component.utils.wsy.lb(file3);
            }
        } catch (Throwable unused3) {
        }
    }

    public void fm(AdSlot adSlot, com.bytedance.sdk.openadsdk.core.model.fm fmVar) {
        lse lseVarWsy;
        if ((fmVar != null && fmVar.wsy() != null && fmVar.wsy().emt()) || fmVar == null || adSlot == null || !TextUtils.isEmpty(adSlot.getBidAdm()) || (lseVarWsy = fmVar.wsy()) == null || lseVarWsy.bh() == 2) {
            return;
        }
        try {
            this.yz.fm(adSlot, fmVar.duv(), fmVar.lb());
        } catch (Throwable unused) {
        }
    }

    public boolean fm(com.bytedance.sdk.openadsdk.core.model.fm fmVar, boolean z10) {
        if (fmVar == null || !fmVar.ajl()) {
            return false;
        }
        if (z10) {
            Iterator<lse> it = fmVar.jnr().iterator();
            while (it.hasNext()) {
                if (!fm(it.next())) {
                    it.remove();
                }
            }
        }
        return fmVar.ajl();
    }

    public String fm(String str, long j10) {
        long jYz = this.yz.yz(str);
        boolean zJnr = this.yz.jnr(str);
        if (System.currentTimeMillis() - jYz >= j10 || zJnr) {
            return null;
        }
        return ro(str);
    }

    private boolean fm(lse lseVar) {
        if (lseVar != null) {
            return ai.lb(lseVar) || lseVar.ex() != null;
        }
        return false;
    }
}
