package com.bytedance.sdk.openadsdk.core.settings;

import androidx.annotation.NonNull;
import androidx.annotation.WorkerThread;
import com.bytedance.sdk.openadsdk.core.gof;
import com.bytedance.sdk.openadsdk.utils.dsz;
import java.io.File;
import java.io.FileWriter;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.json.JSONArray;
import org.json.JSONObject;

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

    @WorkerThread
    public static void fm(boolean z10) {
        File fileFm = fm();
        if (fileFm.exists()) {
            try {
                HashMap<String, ro> mapRo = ro(new JSONArray(new String(com.bytedance.sdk.component.utils.wsy.yz(fileFm))));
                if (mapRo.isEmpty()) {
                    return;
                }
                for (Map.Entry<String, ro> entry : mapRo.entrySet()) {
                    fm.put(entry.getKey(), entry.getValue());
                }
            } catch (Exception unused) {
            }
        }
    }

    @NonNull
    private static HashMap<String, ro> ro(JSONArray jSONArray) {
        HashMap<String, ro> map = new HashMap<>();
        for (int i10 = 0; i10 < jSONArray.length(); i10++) {
            try {
                ro roVarFm = fm(jSONArray.getJSONObject(i10));
                if (roVarFm != null) {
                    map.put(roVarFm.fm, roVarFm);
                }
            } catch (Exception unused) {
            }
        }
        return map;
    }

    private static ro ro(String str) {
        return new ro(str, 1);
    }

    public static void ro() {
        File fileFm = fm();
        if (fileFm.exists()) {
            fileFm.delete();
        }
    }

    @NonNull
    public static File fm() {
        return com.bytedance.sdk.component.utils.wsy.fm(gof.fm(), gof.fm().getFilesDir(), "tt_ads_conf_sr");
    }

    private static ro fm(JSONObject jSONObject) {
        if (jSONObject == null) {
            return null;
        }
        return new ro(jSONObject);
    }

    @NonNull
    public static ro fm(String str) {
        ro roVar = fm.get(str);
        return roVar == null ? ro(str) : roVar;
    }

    @WorkerThread
    public static void fm(JSONArray jSONArray) throws Throwable {
        FileWriter fileWriter;
        if (jSONArray == null) {
            return;
        }
        File fileFm = fm();
        File file = new File(fileFm.getParent(), fileFm.getName() + ".tmp");
        FileWriter fileWriter2 = null;
        try {
            if (file.exists()) {
                file.delete();
            }
            fileWriter = new FileWriter(file);
        } catch (Exception unused) {
        } catch (Throwable th) {
            th = th;
        }
        try {
            fileWriter.write(jSONArray.toString());
            file.renameTo(fileFm);
            if (file.exists()) {
                file.delete();
            }
            dsz.fm(fileWriter);
        } catch (Exception unused2) {
            fileWriter2 = fileWriter;
            if (file.exists()) {
                file.delete();
            }
            dsz.fm(fileWriter2);
        } catch (Throwable th2) {
            th = th2;
            fileWriter2 = fileWriter;
            if (file.exists()) {
                file.delete();
            }
            dsz.fm(fileWriter2);
            throw th;
        }
        HashMap<String, ro> mapRo = ro(jSONArray);
        if (mapRo.isEmpty()) {
            return;
        }
        for (Map.Entry<String, ro> entry : mapRo.entrySet()) {
            fm.put(entry.getKey(), entry.getValue());
        }
    }
}
