package com.bytedance.sdk.openadsdk.gqi.yz;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;
import androidx.annotation.Nullable;
import com.bytedance.sdk.component.ro;
import com.bytedance.sdk.component.utils.sds;
import com.bytedance.sdk.component.utils.wey;
import com.bytedance.sdk.openadsdk.core.gof;
import com.bytedance.sdk.openadsdk.core.vt;
import java.lang.ref.SoftReference;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import u.a;

/* JADX INFO: loaded from: classes2.dex */
public class fm {
    public static HashSet<String> fm = new HashSet<String>() { // from class: com.bytedance.sdk.openadsdk.gqi.yz.fm.1
        {
            add("app_id");
            add("extra_data");
            add("sdk_app_sha1");
            add("uuid");
            add("android_system_ua");
            add("sdk_local_web_ua");
            add("sdk_local_rom_info");
        }
    };
    private static SoftReference<ConcurrentHashMap<String, Map<String, Object>>> ro;

    private static String fm(Context context, String str, String str2) {
        if (fm.contains(str2)) {
            str = "pag_sp_bad_par";
        }
        if (sds.fm(context)) {
            return str;
        }
        StringBuilder sbD = a.d(str, "_");
        sbD.append(sds.lb(context));
        return sbD.toString();
    }

    private static void lb(String str) {
        ConcurrentHashMap<String, Map<String, Object>> concurrentHashMap;
        Map<String, Object> map;
        SoftReference<ConcurrentHashMap<String, Map<String, Object>>> softReference = ro;
        if (softReference == null || (concurrentHashMap = softReference.get()) == null || (map = concurrentHashMap.get(ro(str))) == null) {
            return;
        }
        map.clear();
    }

    private static String ro(String str) {
        return TextUtils.isEmpty(str) ? "tt_sp" : str;
    }

    public static String ro(String str, String str2, String str3) {
        return fm() ? str3 : fm(gof.fm(), ro(str), str2, str3);
    }

    private static void ro(Context context, String str, String str2) {
        ConcurrentHashMap<String, Map<String, Object>> concurrentHashMap;
        Map<String, Object> map;
        try {
            String strFm = fm(context, str, str2);
            if (vt.wsy(strFm)) {
                ro.fm(context, strFm).ro().remove(str2).apply();
                return;
            }
            SharedPreferences sharedPreferencesFm = fm(context, strFm);
            if (sharedPreferencesFm == null) {
                return;
            }
            SharedPreferences.Editor editorEdit = sharedPreferencesFm.edit();
            editorEdit.remove(str2);
            editorEdit.apply();
            SoftReference<ConcurrentHashMap<String, Map<String, Object>>> softReference = ro;
            if (softReference != null && (concurrentHashMap = softReference.get()) != null && (map = concurrentHashMap.get(ro(strFm))) != null && map.size() != 0) {
                map.remove(str2);
            }
        } catch (Throwable unused) {
        }
    }

    private static boolean fm() {
        return gof.fm() == null;
    }

    public static void fm(String str, String str2, Boolean bool) {
        if (fm()) {
            return;
        }
        fm(ro(str), str2, bool);
    }

    public static void fm(String str, String str2, Long l10) {
        if (fm()) {
            return;
        }
        fm(ro(str), str2, l10);
    }

    public static void fm(String str, String str2, String str3) {
        if (fm()) {
            return;
        }
        fm(ro(str), str2, str3);
    }

    public static void fm(String str, String str2, Integer num) {
        if (fm()) {
            return;
        }
        fm(ro(str), str2, num);
    }

    private static void ro(Context context, String str) {
        String strFm = fm(gof.fm(), str, "");
        if (vt.wsy(strFm)) {
            ro.fm(context, strFm).ro().clear().apply();
            lb(strFm);
            return;
        }
        SharedPreferences sharedPreferencesFm = fm(context, strFm);
        if (sharedPreferencesFm == null) {
            return;
        }
        SharedPreferences.Editor editorEdit = sharedPreferencesFm.edit();
        editorEdit.clear();
        editorEdit.apply();
        lb(strFm);
    }

    public static int fm(String str, String str2, int i10) {
        return fm() ? i10 : fm(gof.fm(), ro(str), str2, i10);
    }

    private static int fm(Context context, String str, String str2, int i10) {
        String strFm = fm(context, str, str2);
        if (vt.wsy(strFm)) {
            return ro.fm(context, strFm).fm(str2, i10);
        }
        SharedPreferences sharedPreferencesFm = fm(context, strFm);
        return sharedPreferencesFm == null ? i10 : sharedPreferencesFm.getInt(str2, i10);
    }

    public static boolean fm(String str, String str2, boolean z10) {
        return fm() ? z10 : fm(gof.fm(), ro(str), str2, z10);
    }

    private static Object ro(String str, String str2) {
        ConcurrentHashMap<String, Map<String, Object>> concurrentHashMap;
        Map<String, Object> map;
        SoftReference<ConcurrentHashMap<String, Map<String, Object>>> softReference = ro;
        if (softReference == null || (concurrentHashMap = softReference.get()) == null || (map = concurrentHashMap.get(ro(str))) == null) {
            return null;
        }
        return map.get(str2);
    }

    private static boolean fm(Context context, String str, String str2, boolean z10) {
        String strFm = fm(context, str, str2);
        if (vt.wsy(strFm)) {
            return ro.fm(context, strFm).fm(str2, z10);
        }
        SharedPreferences sharedPreferencesFm = fm(context, strFm);
        return sharedPreferencesFm == null ? z10 : sharedPreferencesFm.getBoolean(str2, z10);
    }

    private static void ro(String str, String str2, Object obj) {
        SoftReference<ConcurrentHashMap<String, Map<String, Object>>> softReference = ro;
        if (softReference == null || softReference.get() == null) {
            ro = new SoftReference<>(new ConcurrentHashMap());
        }
        String strRo = ro(str);
        ConcurrentHashMap<String, Map<String, Object>> concurrentHashMap = ro.get();
        if (concurrentHashMap == null) {
            return;
        }
        Map<String, Object> map = concurrentHashMap.get(strRo);
        if (map == null) {
            map = new HashMap<>();
            concurrentHashMap.put(strRo, map);
        }
        map.put(str2, obj);
    }

    public static long fm(String str, String str2, long j10) {
        return fm() ? j10 : fm(gof.fm(), ro(str), str2, j10);
    }

    private static long fm(Context context, String str, String str2, long j10) {
        String strFm = fm(context, str, str2);
        if (vt.wsy(strFm)) {
            return ro.fm(context, strFm).fm(str2, j10);
        }
        SharedPreferences sharedPreferencesFm = fm(context, strFm);
        return sharedPreferencesFm == null ? j10 : sharedPreferencesFm.getLong(str2, j10);
    }

    private static String fm(Context context, String str, String str2, String str3) {
        String strFm = fm(context, str, str2);
        if (vt.wsy(strFm)) {
            return ro.fm(context, strFm).fm(str2, str3);
        }
        SharedPreferences sharedPreferencesFm = fm(context, strFm);
        return sharedPreferencesFm == null ? str3 : sharedPreferencesFm.getString(str2, str3);
    }

    public static void fm(String str, String str2) {
        if (fm()) {
            return;
        }
        try {
            ro(gof.fm(), ro(str), str2);
        } catch (Throwable unused) {
        }
    }

    public static void fm(String str) {
        if (fm()) {
            return;
        }
        try {
            ro(gof.fm(), ro(str));
        } catch (Throwable unused) {
        }
    }

    private static <T> void fm(String str, String str2, T t10) {
        String strFm = fm(gof.fm(), str, str2);
        if (vt.wsy(strFm)) {
            if (t10.equals(ro(strFm, str2))) {
                return;
            }
            ro.lb lbVarRo = ro.fm(gof.fm(), ro(strFm)).ro();
            fm(lbVarRo, str2, (Object) t10);
            lbVarRo.apply();
            ro(strFm, str2, t10);
            return;
        }
        SharedPreferences sharedPreferencesFm = fm(gof.fm(), ro(strFm));
        if (sharedPreferencesFm == null || t10.equals(ro(strFm, str2))) {
            return;
        }
        SharedPreferences.Editor editorEdit = sharedPreferencesFm.edit();
        fm(editorEdit, str2, t10);
        editorEdit.apply();
        ro(strFm, str2, t10);
    }

    @Nullable
    public static SharedPreferences fm(Context context, String str) {
        if (context == null) {
            return null;
        }
        try {
            return context.getSharedPreferences(ro(str), 0);
        } catch (Throwable th) {
            wey.lb("TTAD.TTSaveHelper", "getSharedPreferences error ", th.getMessage());
            return null;
        }
    }

    /* JADX WARN: Multi-variable type inference failed */
    private static <T> void fm(ro.lb lbVar, String str, T t10) {
        if (t10 instanceof Integer) {
            lbVar.putInt(str, ((Integer) t10).intValue());
        }
        if (t10 instanceof Long) {
            lbVar.putLong(str, ((Long) t10).longValue());
        }
        if (t10 instanceof Float) {
            lbVar.putFloat(str, ((Float) t10).floatValue());
        }
        if (t10 instanceof Boolean) {
            lbVar.putBoolean(str, ((Boolean) t10).booleanValue());
        }
        if (t10 instanceof String) {
            lbVar.putString(str, (String) t10);
        }
    }

    /* JADX WARN: Multi-variable type inference failed */
    private static <T> void fm(SharedPreferences.Editor editor, String str, T t10) {
        if (t10 instanceof Integer) {
            editor.putInt(str, ((Integer) t10).intValue());
        }
        if (t10 instanceof Long) {
            editor.putLong(str, ((Long) t10).longValue());
        }
        if (t10 instanceof Float) {
            editor.putFloat(str, ((Float) t10).floatValue());
        }
        if (t10 instanceof Boolean) {
            editor.putBoolean(str, ((Boolean) t10).booleanValue());
        }
        if (t10 instanceof String) {
            editor.putString(str, (String) t10);
        }
    }
}
