package com.oo.seex.oo_statistics_lib.utils.utdid.core;

import org.xmlpull.v1.XmlPullParserException;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.WeakHashMap;

public class TransactionXMLFile {
    public static final int MODE_WORLD_WRITEABLE = 2;
    public static final int MODE_WORLD_READABLE = 1;
    public static final int MODE_PRIVATE = 0;
    private final Object mSync = new Object();
    private File mPreferencesDir;
    private static final Object GLOBAL_COMMIT_LOCK = new Object();
    private HashMap<File, MySharedPreferencesImpl> sSharedPrefs = new HashMap();

    public TransactionXMLFile(String dir) {
        if (dir != null && dir.length() > 0) {
            this.mPreferencesDir = new File(dir);
        } else {
            throw new RuntimeException("Directory can not be empty");
        }
    }

    private File makeFilename(File base, String name) {
        if (name.indexOf(File.separatorChar) < 0) {
            return new File(base, name);
        } else {
            throw new IllegalArgumentException("File " + name + " contains a path separator");
        }
    }

    private File getPreferencesDir() {
        Object var1 = this.mSync;
        synchronized (this.mSync) {
            return this.mPreferencesDir;
        }
    }

    private File getSharedPrefsFile(String name) {
        return this.makeFilename(this.getPreferencesDir(), name + ".xml");
    }

    public MySharedPreferences getMySharedPreferences(String name, int mode) {
        File f = this.getSharedPrefsFile(name);
        Object str = GLOBAL_COMMIT_LOCK;
        TransactionXMLFile.MySharedPreferencesImpl sp;
        synchronized (GLOBAL_COMMIT_LOCK) {
            sp = (TransactionXMLFile.MySharedPreferencesImpl) this.sSharedPrefs.get(f);
            if (sp != null && !sp.hasFileChanged()) {
                return sp;
            }
        }

        str = null;
        File backup = makeBackupFile(f);
        if (backup.exists()) {
            f.delete();
            backup.renameTo(f);
        }

        if (f.exists() && !f.canRead()) {
            ;
        }

        HashMap map = null;
        if (f.exists() && f.canRead()) {
            FileInputStream str1;
            try {
                str1 = new FileInputStream(f);
                map = XmlUtils.readMapXml(str1);
                str1.close();
            } catch (XmlPullParserException var15) {
                try {
                    str1 = new FileInputStream(f);
                    byte[] e2 = new byte[str1.available()];
                    str1.read(e2);
                    new String(e2, 0, e2.length, "UTF-8");
                } catch (FileNotFoundException var13) {
                    var13.printStackTrace();
                } catch (IOException var14) {
                    var14.printStackTrace();
                }
            } catch (FileNotFoundException var16) {
                ;
            } catch (IOException var17) {
                ;
            }
        }

        Object e = GLOBAL_COMMIT_LOCK;
        synchronized (GLOBAL_COMMIT_LOCK) {
            if (sp != null) {
                sp.replace(map);
            } else {
                sp = (TransactionXMLFile.MySharedPreferencesImpl) this.sSharedPrefs.get(f);
                if (sp == null) {
                    sp = new TransactionXMLFile.MySharedPreferencesImpl(f, mode, map);
                    this.sSharedPrefs.put(f, sp);
                }
            }

            return sp;
        }
    }

    private static File makeBackupFile(File prefsFile) {
        return new File(prefsFile.getPath() + ".bak");
    }

    private static final class MySharedPreferencesImpl implements MySharedPreferences {
        private final File mFile;
        private final File mBackupFile;
        private final int mMode;
        private Map mMap;
        private boolean hasChange = false;
        private static final Object mContent = new Object();
        private WeakHashMap<OnSharedPreferenceChangeListener, Object> mListeners;

        MySharedPreferencesImpl(File file, int mode, Map initialContents) {
            this.mFile = file;
            this.mBackupFile = TransactionXMLFile.makeBackupFile(file);
            this.mMode = mode;
            this.mMap = (Map) (initialContents != null ? initialContents : new HashMap());
            this.mListeners = new WeakHashMap();
        }

        public boolean checkFile() {
            if (this.mFile != null) {
                File fTmp = new File(this.mFile.getAbsolutePath());
                if (fTmp.exists()) {
                    return true;
                }
            }

            return false;
        }

        public void setHasChange(boolean hasChange) {
            synchronized (this) {
                this.hasChange = hasChange;
            }
        }

        public boolean hasFileChanged() {
            synchronized (this) {
                return this.hasChange;
            }
        }

        public void replace(Map newContents) {
            if (newContents != null) {
                synchronized (this) {
                    this.mMap = newContents;
                }
            }

        }

        public void registerOnSharedPreferenceChangeListener(OnSharedPreferenceChangeListener listener) {
            synchronized (this) {
                this.mListeners.put(listener, mContent);
            }
        }

        public void unregisterOnSharedPreferenceChangeListener(OnSharedPreferenceChangeListener listener) {
            synchronized (this) {
                this.mListeners.remove(listener);
            }
        }

        public Map<String, ?> getAll() {
            synchronized (this) {
                return new HashMap(this.mMap);
            }
        }

        public String getString(String key, String defValue) {
            synchronized (this) {
                String v = (String) this.mMap.get(key);
                return v != null ? v : defValue;
            }
        }

        public int getInt(String key, int defValue) {
            synchronized (this) {
                Integer v = (Integer) this.mMap.get(key);
                return v != null ? v.intValue() : defValue;
            }
        }

        public long getLong(String key, long defValue) {
            synchronized (this) {
                Long v = (Long) this.mMap.get(key);
                return v != null ? v.longValue() : defValue;
            }
        }

        public float getFloat(String key, float defValue) {
            synchronized (this) {
                Float v = (Float) this.mMap.get(key);
                return v != null ? v.floatValue() : defValue;
            }
        }

        public boolean getBoolean(String key, boolean defValue) {
            synchronized (this) {
                Boolean v = (Boolean) this.mMap.get(key);
                return v != null ? v.booleanValue() : defValue;
            }
        }

        public boolean contains(String key) {
            synchronized (this) {
                return this.mMap.containsKey(key);
            }
        }

        public MyEditor edit() {
            return new TransactionXMLFile.MySharedPreferencesImpl.EditorImpl();
        }

        private FileOutputStream createFileOutputStream(File file) {
            FileOutputStream str = null;

            try {
                str = new FileOutputStream(file);
            } catch (FileNotFoundException var7) {
                File parent = file.getParentFile();
                if (!parent.mkdir()) {
                    return null;
                }

                try {
                    str = new FileOutputStream(file);
                } catch (FileNotFoundException var6) {
                    ;
                }
            }

            return str;
        }

        private boolean writeFileLocked() {
            if (this.mFile.exists()) {
                if (!this.mBackupFile.exists()) {
                    if (!this.mFile.renameTo(this.mBackupFile)) {
                        return false;
                    }
                } else {
                    this.mFile.delete();
                }
            }

            try {
                FileOutputStream e = this.createFileOutputStream(this.mFile);
                if (e == null) {
                    return false;
                }

                XmlUtils.writeMapXml(this.mMap, e);
                e.close();
                this.mBackupFile.delete();
                return true;
            } catch (XmlPullParserException var2) {
                ;
            } catch (IOException var3) {
                ;
            }

            if (this.mFile.exists() && !this.mFile.delete()) {
                ;
            }

            return false;
        }

        public final class EditorImpl implements MyEditor {
            private final Map<String, Object> mModified = new HashMap();
            private boolean mClear = false;

            public EditorImpl() {
            }

            public MyEditor putString(String key, String value) {
                synchronized (this) {
                    this.mModified.put(key, value);
                    return this;
                }
            }

            public MyEditor putInt(String key, int value) {
                synchronized (this) {
                    this.mModified.put(key, Integer.valueOf(value));
                    return this;
                }
            }

            public MyEditor putLong(String key, long value) {
                synchronized (this) {
                    this.mModified.put(key, Long.valueOf(value));
                    return this;
                }
            }

            public MyEditor putFloat(String key, float value) {
                synchronized (this) {
                    this.mModified.put(key, Float.valueOf(value));
                    return this;
                }
            }

            public MyEditor putBoolean(String key, boolean value) {
                synchronized (this) {
                    this.mModified.put(key, Boolean.valueOf(value));
                    return this;
                }
            }

            public MyEditor remove(String key) {
                synchronized (this) {
                    this.mModified.put(key, this);
                    return this;
                }
            }

            public MyEditor clear() {
                synchronized (this) {
                    this.mClear = true;
                    return this;
                }
            }

            public boolean commit() {
                ArrayList keysModified = null;
                HashSet listeners = null;
                boolean returnValue;
                boolean hasListeners;
                Iterator i$;
                synchronized (TransactionXMLFile.GLOBAL_COMMIT_LOCK) {
                    hasListeners = MySharedPreferencesImpl.this.mListeners.size() > 0;
                    if (hasListeners) {
                        keysModified = new ArrayList();
                        listeners = new HashSet(MySharedPreferencesImpl.this.mListeners.keySet());
                    }

                    synchronized (this) {
                        if (this.mClear) {
                            MySharedPreferencesImpl.this.mMap.clear();
                            this.mClear = false;
                        }

                        i$ = this.mModified.entrySet().iterator();

                        while (i$.hasNext()) {
                            Entry listener = (Entry) i$.next();
                            String k = (String) listener.getKey();
                            Object v = listener.getValue();
                            if (v == this) {
                                MySharedPreferencesImpl.this.mMap.remove(k);
                            } else {
                                MySharedPreferencesImpl.this.mMap.put(k, v);
                            }

                            if (hasListeners) {
                                keysModified.add(k);
                            }
                        }

                        this.mModified.clear();
                    }

                    returnValue = MySharedPreferencesImpl.this.writeFileLocked();
                    if (returnValue) {
                        MySharedPreferencesImpl.this.setHasChange(true);
                    }
                }

                if (hasListeners) {
                    for (int i = keysModified.size() - 1; i >= 0; --i) {
                        String key = (String) keysModified.get(i);
                        i$ = listeners.iterator();

                        while (i$.hasNext()) {
                            OnSharedPreferenceChangeListener var15 = (OnSharedPreferenceChangeListener) i$.next();
                            if (var15 != null) {
                                var15.onSharedPreferenceChanged(MySharedPreferencesImpl.this, key);
                            }
                        }
                    }
                }

                return returnValue;
            }
        }
    }
}
