package com.bytedance.sdk.openadsdk.hlt;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;
import com.bytedance.sdk.openadsdk.hlt.ro;
import java.util.Iterator;
import org.json.JSONObject;

/* JADX INFO: loaded from: classes2.dex */
public class lb {
    private SharedPreferences fm;

    /* JADX INFO: renamed from: lb, reason: collision with root package name */
    private final String f7424lb;
    private final Context ro;

    public lb(Context context, String str) {
        this.ro = context;
        this.f7424lb = str;
    }

    public SharedPreferences fm() {
        Context context;
        SharedPreferences sharedPreferences = this.fm;
        if (sharedPreferences != null) {
            return sharedPreferences;
        }
        if (TextUtils.isEmpty(this.f7424lb) || (context = this.ro) == null) {
            return null;
        }
        try {
            this.fm = context.getSharedPreferences(this.f7424lb, 0);
        } catch (Throwable th) {
            th.getMessage();
        }
        return this.fm;
    }

    public long ro(String str, long j10) {
        try {
            SharedPreferences sharedPreferencesFm = fm();
            if (sharedPreferencesFm != null && sharedPreferencesFm.contains(str)) {
                return sharedPreferencesFm.getLong(str, j10);
            }
            return j10;
        } catch (Throwable th) {
            th.getMessage();
            return j10;
        }
    }

    public void fm(JSONObject jSONObject) {
        try {
            SharedPreferences sharedPreferencesFm = fm();
            if (sharedPreferencesFm != null) {
                SharedPreferences.Editor editorEdit = sharedPreferencesFm.edit();
                Iterator<String> itKeys = jSONObject.keys();
                while (itKeys.hasNext()) {
                    String next = itKeys.next();
                    try {
                        if (!TextUtils.isEmpty(next)) {
                            Object obj = jSONObject.get(next);
                            if (obj instanceof Integer) {
                                editorEdit.putInt(next, ((Integer) obj).intValue());
                            } else if (obj instanceof Long) {
                                editorEdit.putLong(next, ((Long) obj).longValue());
                            } else if (obj instanceof String) {
                                editorEdit.putString(next, (String) obj);
                            } else if (obj instanceof Boolean) {
                                editorEdit.putBoolean(next, ((Boolean) obj).booleanValue());
                            } else if (obj instanceof Float) {
                                editorEdit.putFloat(next, ((Float) obj).floatValue());
                            } else if (obj instanceof Double) {
                                editorEdit.putFloat(next, Double.valueOf(((Double) obj).doubleValue()).floatValue());
                            } else {
                                editorEdit.putString(next, String.valueOf(obj));
                            }
                        }
                    } catch (Throwable th) {
                        th.getMessage();
                    }
                }
                editorEdit.apply();
            }
        } catch (Throwable th2) {
            th2.getMessage();
        }
    }

    public void ro() {
        SharedPreferences sharedPreferencesFm = fm();
        if (sharedPreferencesFm != null) {
            SharedPreferences.Editor editorEdit = sharedPreferencesFm.edit();
            editorEdit.clear();
            editorEdit.commit();
        }
    }

    public void fm(String str, long j10) {
        try {
            SharedPreferences sharedPreferencesFm = fm();
            if (sharedPreferencesFm != null) {
                SharedPreferences.Editor editorEdit = sharedPreferencesFm.edit();
                editorEdit.putLong(str, j10);
                editorEdit.apply();
            }
        } catch (Throwable th) {
            th.getMessage();
        }
    }

    public int fm(String str, int i10) {
        try {
            SharedPreferences sharedPreferencesFm = fm();
            if (sharedPreferencesFm != null && sharedPreferencesFm.contains(str)) {
                return sharedPreferencesFm.getInt(str, i10);
            }
            return i10;
        } catch (Throwable th) {
            th.getMessage();
            return i10;
        }
    }

    public String fm(String str, String str2) {
        try {
            SharedPreferences sharedPreferencesFm = fm();
            if (sharedPreferencesFm != null && sharedPreferencesFm.contains(str)) {
                return sharedPreferencesFm.getString(str, str2);
            }
            return str2;
        } catch (Throwable th) {
            th.getMessage();
            return str2;
        }
    }

    public boolean fm(String str, boolean z10) {
        try {
            SharedPreferences sharedPreferencesFm = fm();
            if (sharedPreferencesFm != null && sharedPreferencesFm.contains(str)) {
                return sharedPreferencesFm.getBoolean(str, z10);
            }
            return z10;
        } catch (Throwable th) {
            th.getMessage();
            return z10;
        }
    }

    public <T> T fm(String str, T t10, ro.fm<T> fmVar) {
        String string;
        if (str != null && !str.isEmpty()) {
            try {
                SharedPreferences sharedPreferencesFm = fm();
                if (sharedPreferencesFm != null && sharedPreferencesFm.contains(str) && (string = sharedPreferencesFm.getString(str, null)) != null && fmVar != null) {
                    T tRo = fmVar.ro(string);
                    if (tRo != null) {
                        return tRo;
                    }
                }
            } catch (Throwable th) {
                th.getMessage();
            }
        }
        return t10;
    }
}
