package com.bytedance.keva.adapter;

import X.19Y;
import X.8Kc;
import X.8e1;
import X.8e2;
import X.8eG;
import X.8eL;
import X.8r0;
import X.8r1;
import X.AnonymousClass4N2;
import X.C51664Ms;
import android.content.Context;
import android.content.SharedPreferences;
import com.bytedance.covode.number.Covode;
import com.bytedance.frameworks.apm.trace.MethodCollector;
import com.bytedance.ies.abmock.SettingsManager;
import com.bytedance.keva.Keva;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class KevaSpFastAdapter implements SharedPreferences {
    public static final Executor sApplyExecutor = Executors.newCachedThreadPool();
    public static final Executor sWriterExecutor = INVOKESTATIC_com_bytedance_keva_adapter_KevaSpFastAdapter_com_ss_android_ugc_aweme_lancet_thread_ThreadPoolLancet_newSingleThreadExecutor();
    public File mBackupFile;
    public final Map<SharedPreferences.OnSharedPreferenceChangeListener, Keva.OnChangeListener> mChangeListenerMap = new 19Y();
    public File mFile;
    public Keva mKeva;

    public class Editor implements SharedPreferences.Editor {
        public boolean mClear;
        public volatile Map<String, Object> mTempMap = new 19Y();

        static {
            Covode.recordClassIndex(35041);
        }

        public SharedPreferences.Editor clear() {
            this.mClear = true;
            return this;
        }

        public void apply() {
            commit();
        }

        public boolean commit() {
            doWriteKeva(this.mTempMap, this.mClear);
            if (KevaSpFastAdapter.this.mFile != null) {
                KevaSpFastAdapter.sWriterExecutor.execute(new Runnable() {
                    static {
                        Covode.recordClassIndex(35042);
                    }

                    public void run() {
                        KevaSpFastAdapter.this.doWriteSp(KevaSpFastAdapter.this.getAll());
                    }
                });
            }
            this.mTempMap = new 19Y();
            if (!this.mClear) {
                return true;
            }
            this.mClear = false;
            return true;
        }

        public Editor() {
        }

        public SharedPreferences.Editor remove(String str) {
            MethodCollector.i(1076);
            Map<String, Object> map = this.mTempMap;
            synchronized (map) {
                try {
                    map.put(str, this);
                } finally {
                    MethodCollector.o(1076);
                }
            }
            return this;
        }

        public SharedPreferences.Editor putBoolean(String str, boolean z) {
            MethodCollector.i(1072);
            Map<String, Object> map = this.mTempMap;
            synchronized (map) {
                try {
                    map.put(str, Boolean.valueOf(z));
                } finally {
                    MethodCollector.o(1072);
                }
            }
            return this;
        }

        public SharedPreferences.Editor putFloat(String str, float f) {
            MethodCollector.i(1067);
            Map<String, Object> map = this.mTempMap;
            synchronized (map) {
                try {
                    map.put(str, Float.valueOf(f));
                } finally {
                    MethodCollector.o(1067);
                }
            }
            return this;
        }

        public SharedPreferences.Editor putInt(String str, int i) {
            MethodCollector.i(916);
            Map<String, Object> map = this.mTempMap;
            synchronized (map) {
                try {
                    map.put(str, Integer.valueOf(i));
                } finally {
                    MethodCollector.o(916);
                }
            }
            return this;
        }

        public SharedPreferences.Editor putLong(String str, long j) {
            MethodCollector.i(918);
            Map<String, Object> map = this.mTempMap;
            synchronized (map) {
                try {
                    map.put(str, Long.valueOf(j));
                } finally {
                    MethodCollector.o(918);
                }
            }
            return this;
        }

        public SharedPreferences.Editor putString(String str, String str2) {
            MethodCollector.i(906);
            Map<String, Object> map = this.mTempMap;
            synchronized (map) {
                try {
                    map.put(str, str2);
                } finally {
                    MethodCollector.o(906);
                }
            }
            return this;
        }

        public SharedPreferences.Editor putStringSet(String str, Set<String> set) {
            MethodCollector.i(911);
            Map<String, Object> map = this.mTempMap;
            synchronized (map) {
                try {
                    map.put(str, set);
                } finally {
                    MethodCollector.o(911);
                }
            }
            return this;
        }

        private void doWriteKeva(Map<String, Object> map, boolean z) {
            MethodCollector.i(1121);
            if (z) {
                KevaSpFastAdapter.this.mKeva.clear();
            }
            synchronized (map) {
                try {
                    for (Map.Entry next : map.entrySet()) {
                        String str = (String) next.getKey();
                        Object value = next.getValue();
                        if (value == null) {
                            KevaSpFastAdapter.this.mKeva.erase(str);
                        }
                        if (value instanceof Integer) {
                            KevaSpFastAdapter.this.mKeva.storeInt(str, ((Integer) value).intValue());
                        } else if (value instanceof Boolean) {
                            KevaSpFastAdapter.this.mKeva.storeBoolean(str, ((Boolean) value).booleanValue());
                        } else if (value instanceof String) {
                            KevaSpFastAdapter.this.mKeva.storeString(str, (String) value);
                        } else if (value instanceof Long) {
                            KevaSpFastAdapter.this.mKeva.storeLong(str, ((Long) value).longValue());
                        } else if (value instanceof Float) {
                            KevaSpFastAdapter.this.mKeva.storeFloat(str, ((Float) value).floatValue());
                        } else if (value instanceof Set) {
                            KevaSpFastAdapter.this.mKeva.storeStringSet(str, (Set) value);
                        } else {
                            KevaSpFastAdapter.this.mKeva.erase(str);
                        }
                    }
                } finally {
                    MethodCollector.o(1121);
                }
            }
        }
    }

    public SharedPreferences.Editor edit() {
        return new Editor();
    }

    static {
        Covode.recordClassIndex(35039);
    }

    public static ExecutorService INVOKESTATIC_com_bytedance_keva_adapter_KevaSpFastAdapter_com_ss_android_ugc_aweme_lancet_thread_ThreadPoolLancet_newSingleThreadExecutor() {
        8e1 LIZ = 8e2.LIZ(8eG.FIXED);
        LIZ.LIZJ = 1;
        return 8eL.LIZ(LIZ.LIZ());
    }

    public Map<String, ?> getAll() {
        HashMap hashMap = new HashMap();
        for (Map.Entry entry : this.mKeva.getAll().entrySet()) {
            Object key = entry.getKey();
            Object value = entry.getValue();
            if (value != null) {
                if (value instanceof String[]) {
                    HashSet hashSet = new HashSet(16, 0.75f);
                    for (String add : (String[]) value) {
                        hashSet.add(add);
                    }
                    value = hashSet;
                }
                hashMap.put(key, value);
            }
        }
        return hashMap;
    }

    public boolean contains(String str) {
        return this.mKeva.contains(str);
    }

    public KevaSpFastAdapter(Keva keva) {
        this.mKeva = keva;
    }

    public void registerOnSharedPreferenceChangeListener(final SharedPreferences.OnSharedPreferenceChangeListener onSharedPreferenceChangeListener) {
        AnonymousClass1 r1 = new Keva.OnChangeListener() {
            static {
                Covode.recordClassIndex(35040);
            }

            public void onChanged(Keva keva, String str) {
                onSharedPreferenceChangeListener.onSharedPreferenceChanged(KevaSpFastAdapter.this, str);
            }
        };
        this.mKeva.registerChangeListener(r1);
        this.mChangeListenerMap.put(onSharedPreferenceChangeListener, r1);
    }

    public void unregisterOnSharedPreferenceChangeListener(SharedPreferences.OnSharedPreferenceChangeListener onSharedPreferenceChangeListener) {
        this.mKeva.unRegisterChangeListener(this.mChangeListenerMap.remove(onSharedPreferenceChangeListener));
    }

    public static File INVOKEVIRTUAL_com_bytedance_keva_adapter_KevaSpFastAdapter_com_ss_android_ugc_aweme_lancet_ContextLancet_getFilesDir(Context context) {
        if (C51664Ms.LIZJ != null && C51664Ms.LJ) {
            return C51664Ms.LIZJ;
        }
        File filesDir = context.getFilesDir();
        C51664Ms.LIZJ = filesDir;
        return filesDir;
    }

    public static FileOutputStream createFileOutputStream(File file) {
        FileOutputStream fileOutputStream;
        MethodCollector.i(180);
        FileOutputStream fileOutputStream2 = null;
        try {
            fileOutputStream = new FileOutputStream(file);
        } catch (FileNotFoundException unused) {
            if (!file.getParentFile().mkdir()) {
                MethodCollector.o(180);
                return null;
            }
            try {
                fileOutputStream = new FileOutputStream(file);
            } catch (FileNotFoundException unused2) {
            }
        }
        fileOutputStream2 = fileOutputStream;
        MethodCollector.o(180);
        return fileOutputStream2;
    }

    public static boolean INVOKEVIRTUAL_com_bytedance_keva_adapter_KevaSpFastAdapter_com_ss_android_ugc_aweme_storage_FileLancet_delete(File file) {
        8r1 r3;
        MethodCollector.i(176);
        try {
            if (8Kc.LIZIZ()) {
                r3 = (8r1) SettingsManager.LIZ().LIZ("storage_intercepter_key", 8r1.class, AnonymousClass4N2.LIZ);
            } else {
                r3 = AnonymousClass4N2.LIZ;
            }
            if (8r0.LIZ(file.getAbsolutePath(), r3)) {
                8r0.LIZ(file, new RuntimeException(), "exception_delete_log", 8r0.LIZ(r3));
            }
            if (8r0.LIZJ(file.getAbsolutePath(), r3)) {
                8r0.LIZ(file, new RuntimeException(), "exception_handle", 8r0.LIZ(r3));
                MethodCollector.o(176);
                return false;
            }
        } catch (Throwable unused) {
        }
        boolean delete = file.delete();
        MethodCollector.o(176);
        return delete;
    }

    /* JADX WARNING: Missing exception handler attribute for start block: B:31:0x0082 */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    public void doWriteSp(java.util.Map<java.lang.String, ?> r7) {
        /*
            r6 = this;
            r5 = 166(0xa6, float:2.33E-43)
            com.bytedance.frameworks.apm.trace.MethodCollector.i(r5)
            java.io.File r0 = r6.mBackupFile
            boolean r1 = r0.exists()
            java.io.File r0 = r6.mFile
            boolean r0 = r0.exists()
            if (r0 == 0) goto L_0x001c
            if (r1 != 0) goto L_0x0028
            java.io.File r1 = r6.mFile
            java.io.File r0 = r6.mBackupFile
            r1.renameTo(r0)
        L_0x001c:
            java.io.File r0 = r6.mFile
            java.io.FileOutputStream r4 = createFileOutputStream(r0)
            if (r4 != 0) goto L_0x002e
            com.bytedance.frameworks.apm.trace.MethodCollector.o(r5)
            return
        L_0x0028:
            java.io.File r0 = r6.mFile
            INVOKEVIRTUAL_com_bytedance_keva_adapter_KevaSpFastAdapter_com_ss_android_ugc_aweme_storage_FileLancet_delete(r0)
            goto L_0x001c
        L_0x002e:
            com.bytedance.keva.adapter.xml.XmlUtils.writeMapXml(r7, r4)     // Catch:{ IOException -> 0x0082, XmlPullParserException -> 0x0072, Exception -> 0x0048 }
            java.io.FileDescriptor r0 = r4.getFD()     // Catch:{ IOException -> 0x0082, XmlPullParserException -> 0x0072, Exception -> 0x0048 }
            r0.sync()     // Catch:{ IOException -> 0x0082, XmlPullParserException -> 0x0072, Exception -> 0x0048 }
            java.io.File r0 = r6.mBackupFile     // Catch:{ IOException -> 0x0082, XmlPullParserException -> 0x0072, Exception -> 0x0048 }
            INVOKEVIRTUAL_com_bytedance_keva_adapter_KevaSpFastAdapter_com_ss_android_ugc_aweme_storage_FileLancet_delete(r0)     // Catch:{ IOException -> 0x0082, XmlPullParserException -> 0x0072, Exception -> 0x0048 }
            r4.close()     // Catch:{ IOException -> 0x0044 }
            com.bytedance.frameworks.apm.trace.MethodCollector.o(r5)
            return
        L_0x0044:
            com.bytedance.frameworks.apm.trace.MethodCollector.o(r5)
            return
        L_0x0048:
            r3 = move-exception
            java.lang.RuntimeException r2 = new java.lang.RuntimeException     // Catch:{ all -> 0x0092 }
            java.lang.StringBuilder r1 = new java.lang.StringBuilder     // Catch:{ all -> 0x0092 }
            java.lang.String r0 = "fail to write "
            r1.<init>(r0)     // Catch:{ all -> 0x0092 }
            java.io.File r0 = r6.mFile     // Catch:{ all -> 0x0092 }
            java.lang.String r0 = r0.getName()     // Catch:{ all -> 0x0092 }
            r1.append(r0)     // Catch:{ all -> 0x0092 }
            java.lang.String r0 = ", dump: "
            r1.append(r0)     // Catch:{ all -> 0x0092 }
            java.util.Set r0 = r7.keySet()     // Catch:{ all -> 0x0092 }
            r1.append(r0)     // Catch:{ all -> 0x0092 }
            java.lang.String r0 = r1.toString()     // Catch:{ all -> 0x0092 }
            r2.<init>(r0, r3)     // Catch:{ all -> 0x0092 }
            com.bytedance.frameworks.apm.trace.MethodCollector.o(r5)     // Catch:{ all -> 0x0092 }
            throw r2     // Catch:{ all -> 0x0092 }
        L_0x0072:
            java.io.File r0 = r6.mFile     // Catch:{ all -> 0x0092 }
            INVOKEVIRTUAL_com_bytedance_keva_adapter_KevaSpFastAdapter_com_ss_android_ugc_aweme_storage_FileLancet_delete(r0)     // Catch:{ all -> 0x0092 }
            r4.close()     // Catch:{ IOException -> 0x007e }
            com.bytedance.frameworks.apm.trace.MethodCollector.o(r5)
            return
        L_0x007e:
            com.bytedance.frameworks.apm.trace.MethodCollector.o(r5)
            return
        L_0x0082:
            java.io.File r0 = r6.mFile     // Catch:{ all -> 0x0092 }
            INVOKEVIRTUAL_com_bytedance_keva_adapter_KevaSpFastAdapter_com_ss_android_ugc_aweme_storage_FileLancet_delete(r0)     // Catch:{ all -> 0x0092 }
            r4.close()     // Catch:{ IOException -> 0x008e }
            com.bytedance.frameworks.apm.trace.MethodCollector.o(r5)
            return
        L_0x008e:
            com.bytedance.frameworks.apm.trace.MethodCollector.o(r5)
            return
        L_0x0092:
            r0 = move-exception
            r4.close()     // Catch:{ IOException -> 0x0096 }
        L_0x0096:
            com.bytedance.frameworks.apm.trace.MethodCollector.o(r5)
            throw r0
        */
        throw new UnsupportedOperationException("Method not decompiled: com.bytedance.keva.adapter.KevaSpFastAdapter.doWriteSp(java.util.Map):void");
    }

    public boolean getBoolean(String str, boolean z) {
        return this.mKeva.getBoolean(str, z);
    }

    public float getFloat(String str, float f) {
        return this.mKeva.getFloat(str, f);
    }

    public int getInt(String str, int i) {
        return this.mKeva.getInt(str, i);
    }

    public long getLong(String str, long j) {
        return this.mKeva.getLong(str, j);
    }

    public String getString(String str, String str2) {
        String string = this.mKeva.getString(str, str2);
        if (string != null) {
            return string;
        }
        return str2;
    }

    public Set<String> getStringSet(String str, Set<String> set) {
        Set<String> stringSet = this.mKeva.getStringSet(str, set);
        if (stringSet != null) {
            return stringSet;
        }
        return set;
    }

    public static KevaSpFastAdapter getSharedPreferences(Context context, String str, int i, boolean z) {
        int i2;
        if (i == 4) {
            i2 = 1;
        } else {
            i2 = 0;
        }
        KevaSpFastAdapter kevaSpFastAdapter = new KevaSpFastAdapter(Keva.getRepoFromSp(context, str, i2));
        if (z) {
            File INVOKEVIRTUAL_com_bytedance_keva_adapter_KevaSpFastAdapter_com_ss_android_ugc_aweme_lancet_ContextLancet_getFilesDir = INVOKEVIRTUAL_com_bytedance_keva_adapter_KevaSpFastAdapter_com_ss_android_ugc_aweme_lancet_ContextLancet_getFilesDir(context);
            if (!INVOKEVIRTUAL_com_bytedance_keva_adapter_KevaSpFastAdapter_com_ss_android_ugc_aweme_lancet_ContextLancet_getFilesDir.exists()) {
                INVOKEVIRTUAL_com_bytedance_keva_adapter_KevaSpFastAdapter_com_ss_android_ugc_aweme_lancet_ContextLancet_getFilesDir.mkdirs();
            }
            String parent = INVOKEVIRTUAL_com_bytedance_keva_adapter_KevaSpFastAdapter_com_ss_android_ugc_aweme_lancet_ContextLancet_getFilesDir.getParent();
            kevaSpFastAdapter.mFile = new File(parent, "shared_prefs/" + str + ".xml");
            kevaSpFastAdapter.mBackupFile = new File(kevaSpFastAdapter.mFile.getPath() + ".bak");
        }
        return kevaSpFastAdapter;
    }
}
