package com.bytedance.sdk.component;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.HandlerThread;
import android.text.TextUtils;
import android.util.ArrayMap;
import androidx.annotation.GuardedBy;
import androidx.annotation.RequiresApi;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Objects;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import org.jetbrains.annotations.NotNull;

/* JADX INFO: loaded from: classes.dex */
public class ro {
    private static ArrayMap<File, ro> dsz = null;

    @GuardedBy("TTPropHelper.class")
    private static ArrayMap<String, File> duv = null;
    protected static fm fm = null;
    private static boolean ro = false;
    private volatile boolean ajl;

    /* JADX INFO: renamed from: ef, reason: collision with root package name */
    @GuardedBy("mWriteLock")
    private long f6656ef;

    @GuardedBy("mLoadLock")
    private Properties jnr;
    private final File ku;

    /* JADX INFO: renamed from: lb, reason: collision with root package name */
    private final Object f6657lb;
    private final File vt;

    @GuardedBy("mLoadLock")
    private int wsy;

    @GuardedBy("this")
    private long wu;
    private final Object yz;

    public interface fm {
        HandlerThread fm(String str, int i10);

        ExecutorService fm();
    }

    /* JADX INFO: renamed from: com.bytedance.sdk.component.ro$ro, reason: collision with other inner class name */
    public static class C0037ro {
        final long fm;
        boolean jnr;

        /* JADX INFO: renamed from: lb, reason: collision with root package name */
        final CountDownLatch f6660lb;
        final Properties ro;

        @GuardedBy("mWritingToDiskLock")
        volatile boolean yz;

        public void fm(boolean z10, boolean z11) {
            this.jnr = z10;
            this.yz = z11;
            this.f6660lb.countDown();
        }

        private C0037ro(long j10, Properties properties) {
            this.f6660lb = new CountDownLatch(1);
            this.yz = false;
            this.jnr = false;
            this.fm = j10;
            this.ro = properties;
        }
    }

    private ro(File file) {
        Object obj = new Object();
        this.f6657lb = obj;
        this.yz = new Object();
        this.jnr = new Properties();
        this.ajl = false;
        this.wsy = 0;
        this.vt = file;
        this.ku = fm(file);
        synchronized (obj) {
            this.ajl = false;
        }
        fm fmVar = fm;
        if (fmVar == null || fmVar.fm() == null) {
            new Thread("TTPropHelper") { // from class: com.bytedance.sdk.component.ro.1
                @Override // java.lang.Thread, java.lang.Runnable
                public void run() {
                    ro.this.fm();
                }
            }.start();
        } else {
            fm.fm().execute(new Runnable() { // from class: com.bytedance.sdk.component.ro.2
                @Override // java.lang.Runnable
                public void run() {
                    ro.this.fm();
                }
            });
        }
    }

    public static /* synthetic */ long ajl(ro roVar) {
        long j10 = roVar.wu;
        roVar.wu = 1 + j10;
        return j10;
    }

    public static /* synthetic */ int ef(ro roVar) {
        int i10 = roVar.wsy;
        roVar.wsy = i10 - 1;
        return i10;
    }

    public static void fm(@NotNull fm fmVar) {
        fm = fmVar;
    }

    public static /* synthetic */ int jnr(ro roVar) {
        int i10 = roVar.wsy;
        roVar.wsy = i10 + 1;
        return i10;
    }

    private void yz() {
        while (!this.ajl) {
            try {
                this.f6657lb.wait();
            } catch (InterruptedException unused) {
            }
        }
    }

    public lb ro() {
        return new lb();
    }

    public class lb implements SharedPreferences.Editor {
        private final Object ro = new Object();

        /* JADX INFO: renamed from: lb, reason: collision with root package name */
        @GuardedBy("mEditorLock")
        private final Map<String, Object> f6659lb = new HashMap();

        @GuardedBy("mEditorLock")
        private boolean yz = false;

        public lb() {
        }

        private C0037ro ro() {
            Properties properties;
            long j10;
            Object obj;
            boolean z10;
            synchronized (ro.this.f6657lb) {
                try {
                    if (ro.this.wsy > 0) {
                        Properties properties2 = new Properties();
                        properties2.putAll(ro.this.jnr);
                        ro.this.jnr = properties2;
                    }
                    properties = ro.this.jnr;
                    ro.jnr(ro.this);
                    synchronized (this.ro) {
                        try {
                            boolean z11 = false;
                            if (this.yz) {
                                if (properties.isEmpty()) {
                                    z10 = false;
                                } else {
                                    properties.clear();
                                    z10 = true;
                                }
                                this.yz = false;
                                z11 = z10;
                            }
                            for (Map.Entry<String, Object> entry : this.f6659lb.entrySet()) {
                                String key = entry.getKey();
                                Object value = entry.getValue();
                                if (value == this || value == null) {
                                    if (properties.containsKey(key)) {
                                        properties.remove(key);
                                        z11 = true;
                                    }
                                } else if (!properties.containsKey(key) || (obj = properties.get(key)) == null || !obj.equals(String.valueOf(value))) {
                                    properties.put(key, String.valueOf(value));
                                    z11 = true;
                                }
                            }
                            this.f6659lb.clear();
                            if (z11) {
                                ro.ajl(ro.this);
                            }
                            j10 = ro.this.wu;
                        } finally {
                        }
                    }
                } catch (Throwable th) {
                    throw th;
                }
            }
            return new C0037ro(j10, properties);
        }

        @Override // android.content.SharedPreferences.Editor
        public void apply() {
            ro.this.fm(ro(), false);
        }

        @Override // android.content.SharedPreferences.Editor
        public boolean commit() {
            if (ro.ro) {
                System.currentTimeMillis();
            }
            C0037ro c0037roRo = ro();
            ro.this.fm(c0037roRo, true);
            try {
                c0037roRo.f6660lb.await();
                if (ro.ro) {
                    ro.this.vt.getName();
                    System.currentTimeMillis();
                }
                return c0037roRo.yz;
            } catch (InterruptedException unused) {
                if (!ro.ro) {
                    return false;
                }
                ro.this.vt.getName();
                long j10 = c0037roRo.fm;
                System.currentTimeMillis();
                return false;
            } catch (Throwable th) {
                if (ro.ro) {
                    ro.this.vt.getName();
                    long j11 = c0037roRo.fm;
                    System.currentTimeMillis();
                }
                throw th;
            }
        }

        public lb fm(String str, Set<String> set) {
            synchronized (this.ro) {
                this.f6659lb.put(str, set == null ? null : new HashSet(set));
            }
            return this;
        }

        @Override // android.content.SharedPreferences.Editor
        public /* synthetic */ SharedPreferences.Editor putStringSet(String str, Set set) {
            return fm(str, (Set<String>) set);
        }

        @Override // android.content.SharedPreferences.Editor
        /* JADX INFO: renamed from: fm, reason: merged with bridge method [inline-methods] */
        public lb putInt(String str, int i10) {
            synchronized (this.ro) {
                this.f6659lb.put(str, Integer.valueOf(i10));
            }
            return this;
        }

        @Override // android.content.SharedPreferences.Editor
        /* JADX INFO: renamed from: fm, reason: merged with bridge method [inline-methods] */
        public lb putLong(String str, long j10) {
            synchronized (this.ro) {
                this.f6659lb.put(str, Long.valueOf(j10));
            }
            return this;
        }

        @Override // android.content.SharedPreferences.Editor
        /* JADX INFO: renamed from: fm, reason: merged with bridge method [inline-methods] */
        public lb putFloat(String str, float f10) {
            synchronized (this.ro) {
                this.f6659lb.put(str, Float.valueOf(f10));
            }
            return this;
        }

        @Override // android.content.SharedPreferences.Editor
        /* JADX INFO: renamed from: fm, reason: merged with bridge method [inline-methods] */
        public lb putString(String str, String str2) {
            synchronized (this.ro) {
                this.f6659lb.put(str, str2);
            }
            return this;
        }

        @Override // android.content.SharedPreferences.Editor
        /* JADX INFO: renamed from: fm, reason: merged with bridge method [inline-methods] */
        public lb putBoolean(String str, boolean z10) {
            synchronized (this.ro) {
                this.f6659lb.put(str, Boolean.valueOf(z10));
            }
            return this;
        }

        @Override // android.content.SharedPreferences.Editor
        /* JADX INFO: renamed from: fm, reason: merged with bridge method [inline-methods] */
        public lb remove(String str) {
            synchronized (this.ro) {
                this.f6659lb.put(str, this);
            }
            return this;
        }

        @Override // android.content.SharedPreferences.Editor
        /* JADX INFO: renamed from: fm, reason: merged with bridge method [inline-methods] */
        public lb clear() {
            synchronized (this.ro) {
                this.yz = true;
            }
            return this;
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    /* JADX WARN: Code restructure failed: missing block: B:21:0x0032, code lost:
    
        if (r0 == false) goto L35;
     */
    @GuardedBy("mWriteLock")
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public void ro(C0037ro c0037ro, boolean z10) {
        FileOutputStream fileOutputStream;
        Throwable th;
        if (ro) {
            System.currentTimeMillis();
        }
        boolean zExists = this.vt.exists();
        if (ro) {
            System.currentTimeMillis();
        }
        if (zExists) {
            if (this.f6656ef < c0037ro.fm) {
                if (!z10) {
                    synchronized (this.f6657lb) {
                        boolean z11 = this.wu == c0037ro.fm;
                    }
                }
                boolean zExists2 = this.ku.exists();
                if (ro) {
                    System.currentTimeMillis();
                }
                if (!zExists2) {
                    if (!this.vt.renameTo(this.ku)) {
                        Objects.toString(this.vt);
                        Objects.toString(this.ku);
                        c0037ro.fm(false, false);
                        return;
                    }
                } else {
                    this.vt.delete();
                }
            }
            c0037ro.fm(false, true);
            return;
        }
        try {
            synchronized (this.yz) {
                FileOutputStream fileOutputStream2 = null;
                try {
                    try {
                        fileOutputStream = new FileOutputStream(this.vt);
                    } catch (Exception unused) {
                    }
                } catch (Throwable th2) {
                    fileOutputStream = fileOutputStream2;
                    th = th2;
                }
                try {
                    if (ro) {
                        System.currentTimeMillis();
                    }
                    c0037ro.ro.store(fileOutputStream, (String) null);
                    if (ro) {
                        Objects.toString(c0037ro.ro);
                        this.vt.getAbsolutePath();
                    }
                    try {
                        fileOutputStream.close();
                    } catch (Throwable th3) {
                        th = th3;
                        th.getMessage();
                    }
                } catch (Exception unused2) {
                    fileOutputStream2 = fileOutputStream;
                    c0037ro.fm(false, false);
                    if (fileOutputStream2 != null) {
                        try {
                            fileOutputStream2.close();
                        } catch (Throwable th4) {
                            th = th4;
                            th.getMessage();
                        }
                    }
                } catch (Throwable th5) {
                    th = th5;
                    if (fileOutputStream != null) {
                        try {
                            fileOutputStream.close();
                        } catch (Throwable th6) {
                            th6.getMessage();
                        }
                    }
                    throw th;
                }
            }
            if (ro) {
                System.currentTimeMillis();
            }
            this.ku.delete();
            if (ro) {
                System.currentTimeMillis();
            }
            this.f6656ef = c0037ro.fm;
            c0037ro.fm(true, true);
        } catch (Throwable unused3) {
            if (this.vt.exists() && !this.vt.delete()) {
                Objects.toString(this.vt);
            }
            c0037ro.fm(false, false);
        }
    }

    @RequiresApi(api = 19)
    public static ro fm(@NotNull Context context, String str) {
        if (TextUtils.isEmpty(str)) {
            str = "tt_prop";
        }
        synchronized (ro.class) {
            try {
                if (duv == null) {
                    duv = new ArrayMap<>();
                }
                File file = duv.get(str);
                if (file == null) {
                    file = new File(context.getFilesDir(), str);
                    duv.put(str, file);
                }
                if (dsz == null) {
                    dsz = new ArrayMap<>();
                }
                ro roVar = dsz.get(file);
                if (roVar != null) {
                    return roVar;
                }
                ro roVar2 = new ro(file);
                dsz.put(file, roVar2);
                return roVar2;
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    public static File fm(File file) {
        return new File(file.getPath() + ".bak");
    }

    /* JADX WARN: Removed duplicated region for block: B:35:0x0068  */
    /* JADX WARN: Unsupported multi-entry loop pattern (BACK_EDGE: B:29:0x005b -> B:54:0x0064). Please report as a decompilation issue!!! */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public void fm() {
        synchronized (this.f6657lb) {
            try {
                if (this.ajl) {
                    return;
                }
                if (this.ku.exists()) {
                    this.vt.delete();
                    this.ku.renameTo(this.vt);
                }
                if (ro) {
                    this.vt.getAbsolutePath();
                    this.vt.exists();
                }
                Properties properties = null;
                FileInputStream fileInputStream = null;
                if (this.vt.exists()) {
                    Properties properties2 = new Properties();
                    try {
                        try {
                            FileInputStream fileInputStream2 = new FileInputStream(this.vt);
                            try {
                                properties2.load(fileInputStream2);
                                if (ro) {
                                    properties2.size();
                                    this.vt.getAbsolutePath();
                                }
                                fileInputStream2.close();
                            } catch (Throwable unused) {
                                fileInputStream = fileInputStream2;
                                if (fileInputStream != null) {
                                    fileInputStream.close();
                                }
                                properties = properties2;
                                synchronized (this.f6657lb) {
                                }
                            }
                        } catch (Throwable th) {
                            th.getMessage();
                        }
                    } catch (Throwable unused2) {
                    }
                    properties = properties2;
                }
                synchronized (this.f6657lb) {
                    if (properties != null) {
                        try {
                            if (!properties.isEmpty()) {
                                this.jnr = properties;
                            }
                        } catch (Throwable th2) {
                            throw th2;
                        }
                    }
                    this.ajl = true;
                    this.f6657lb.notifyAll();
                }
            } catch (Throwable th3) {
                throw th3;
            }
        }
    }

    public String fm(String str, String str2) {
        String property;
        if (TextUtils.isEmpty(str)) {
            return str2;
        }
        synchronized (this.f6657lb) {
            yz();
            property = this.jnr.getProperty(str, str2);
        }
        return property;
    }

    public int fm(String str, int i10) {
        int i11;
        if (TextUtils.isEmpty(str)) {
            return i10;
        }
        synchronized (this.f6657lb) {
            try {
                try {
                    yz();
                    i11 = Integer.parseInt(this.jnr.getProperty(str, String.valueOf(i10)));
                } catch (NumberFormatException e10) {
                    e10.getMessage();
                    return i10;
                }
            } catch (Throwable th) {
                throw th;
            }
        }
        return i11;
    }

    public long fm(String str, long j10) {
        long j11;
        if (TextUtils.isEmpty(str)) {
            return j10;
        }
        synchronized (this.f6657lb) {
            try {
                try {
                    yz();
                    j11 = Long.parseLong(this.jnr.getProperty(str, String.valueOf(j10)));
                } catch (NumberFormatException e10) {
                    e10.getMessage();
                    return j10;
                }
            } catch (Throwable th) {
                throw th;
            }
        }
        return j11;
    }

    public boolean fm(String str, boolean z10) {
        boolean z11;
        if (TextUtils.isEmpty(str)) {
            return z10;
        }
        synchronized (this.f6657lb) {
            try {
                try {
                    yz();
                    z11 = Boolean.parseBoolean(this.jnr.getProperty(str, String.valueOf(z10)));
                } catch (NumberFormatException e10) {
                    e10.getMessage();
                    return z10;
                }
            } catch (Throwable th) {
                throw th;
            }
        }
        return z11;
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void fm(final C0037ro c0037ro, final boolean z10) {
        boolean z11;
        Runnable runnable = new Runnable() { // from class: com.bytedance.sdk.component.ro.3
            @Override // java.lang.Runnable
            public void run() {
                synchronized (ro.this.yz) {
                    try {
                        ro.this.ro(c0037ro, z10);
                    } catch (OutOfMemoryError unused) {
                    }
                }
                synchronized (ro.this.f6657lb) {
                    ro.ef(ro.this);
                }
            }
        };
        if (z10) {
            synchronized (this.f6657lb) {
                z11 = this.wsy == 1;
            }
            if (z11) {
                runnable.run();
                return;
            }
        }
        com.bytedance.sdk.component.lb.fm(runnable, true ^ z10);
    }
}
