package com.bytedance.sdk.openadsdk.utils;

import android.content.Context;
import android.text.TextUtils;
import androidx.media3.exoplayer.rtsp.SessionDescription;
import com.bytedance.sdk.openadsdk.AdSlot;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.lang.Thread;
import java.util.Properties;

/* JADX INFO: loaded from: classes2.dex */
public class hxv implements Thread.UncaughtExceptionHandler {
    public static volatile boolean fm;

    /* JADX INFO: renamed from: lb, reason: collision with root package name */
    private String f7543lb;
    private final Thread.UncaughtExceptionHandler ro = Thread.getDefaultUncaughtExceptionHandler();

    private hxv() {
        Thread.setDefaultUncaughtExceptionHandler(this);
        ro();
    }

    public static hxv fm() {
        return new hxv();
    }

    private void lb() {
        try {
            com.bytedance.sdk.openadsdk.component.wsy.fm.fm(com.bytedance.sdk.openadsdk.core.gof.fm());
        } catch (Throwable unused) {
        }
        try {
            if (ro.lb()) {
                com.bytedance.sdk.openadsdk.component.reward.gof.fm(com.bytedance.sdk.openadsdk.core.gof.fm()).jnr();
            } else {
                com.bytedance.sdk.openadsdk.component.reward.qhl.fm(com.bytedance.sdk.openadsdk.core.gof.fm()).fm();
            }
        } catch (Throwable unused2) {
        }
        try {
            if (ro.lb()) {
                com.bytedance.sdk.openadsdk.component.reward.wsy.fm(com.bytedance.sdk.openadsdk.core.gof.fm()).jnr();
            } else {
                com.bytedance.sdk.openadsdk.component.reward.ajl.fm(com.bytedance.sdk.openadsdk.core.gof.fm()).fm();
            }
        } catch (Throwable unused3) {
        }
        try {
            com.bytedance.sdk.openadsdk.core.settings.ku.ro().fm();
        } catch (Throwable unused4) {
        }
        try {
            com.bytedance.sdk.component.adexpress.fm.ro.ro.ro();
        } catch (Throwable unused5) {
        }
    }

    private void ro() {
        Context contextFm = com.bytedance.sdk.openadsdk.core.gof.fm();
        if (contextFm == null) {
            return;
        }
        try {
            File file = new File(contextFm.getFilesDir(), "TTCache");
            file.mkdirs();
            this.f7543lb = file.getPath();
        } catch (Throwable unused) {
        }
    }

    @Override // java.lang.Thread.UncaughtExceptionHandler
    public void uncaughtException(Thread thread, Throwable th) {
        fm = true;
        com.bytedance.sdk.component.utils.wu.fm = true;
        com.bytedance.sdk.component.wu.ro.jnr.f6707lb = true;
        try {
            StringWriter stringWriter = new StringWriter();
            th.printStackTrace(new PrintWriter(stringWriter));
            String string = stringWriter.toString();
            if (string != null) {
                if (string.contains(AdSlot.class.getPackage().getName())) {
                    fm(thread, th);
                }
            }
        } catch (Throwable unused) {
        }
        Thread.UncaughtExceptionHandler uncaughtExceptionHandler = this.ro;
        if (uncaughtExceptionHandler == null || uncaughtExceptionHandler == this) {
            return;
        }
        uncaughtExceptionHandler.uncaughtException(thread, th);
    }

    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Removed duplicated region for block: B:100:? A[RETURN, SYNTHETIC] */
    /* JADX WARN: Removed duplicated region for block: B:77:0x00d1 A[EXC_TOP_SPLITTER, SYNTHETIC] */
    /* JADX WARN: Removed duplicated region for block: B:81:0x00e4 A[EXC_TOP_SPLITTER, SYNTHETIC] */
    /* JADX WARN: Removed duplicated region for block: B:85:0x00d6 A[EXC_TOP_SPLITTER, SYNTHETIC] */
    /* JADX WARN: Removed duplicated region for block: B:91:0x00e9 A[EXC_TOP_SPLITTER, SYNTHETIC] */
    /* JADX WARN: Removed duplicated region for block: B:99:? A[RETURN, SYNTHETIC] */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    private void fm(Thread thread, Throwable th) {
        FileOutputStream fileOutputStream;
        Object[] objArr;
        int i10;
        FileInputStream fileInputStream = null;
        fileOutputStream = null;
        fileOutputStream = null;
        FileOutputStream fileOutputStream2 = null;
        FileInputStream fileInputStream2 = null;
        fileInputStream = null;
        try {
            if (TextUtils.isEmpty(this.f7543lb)) {
                ro();
            }
            if (TextUtils.isEmpty(this.f7543lb)) {
                return;
            }
            File file = new File(this.f7543lb, "tt_crash_count.properties");
            if (file.exists() && file.isFile() && file.canRead()) {
                Properties properties = new Properties();
                FileInputStream fileInputStream3 = new FileInputStream(file);
                try {
                    properties.load(fileInputStream3);
                    String property = properties.getProperty("crash_count", SessionDescription.SUPPORTED_SDP_VERSION);
                    String property2 = properties.getProperty("crash_last_time", SessionDescription.SUPPORTED_SDP_VERSION);
                    int iIntValue = Integer.valueOf(property).intValue();
                    if (System.currentTimeMillis() - Long.valueOf(property2).longValue() < 300000) {
                        i10 = iIntValue + 1;
                        objArr = false;
                    } else {
                        objArr = true;
                        i10 = 1;
                    }
                    boolean z10 = i10 >= 3;
                    int i11 = z10 ? 0 : i10;
                    if (z10) {
                        try {
                            file.delete();
                        } catch (Throwable unused) {
                        }
                    } else {
                        properties.setProperty("crash_count", String.valueOf(i11));
                        if (objArr != false) {
                            properties.setProperty("crash_last_time", String.valueOf(System.currentTimeMillis()));
                        }
                        FileOutputStream fileOutputStream3 = new FileOutputStream(file);
                        try {
                            properties.store(fileOutputStream3, "tt_crash_info");
                            fileOutputStream2 = fileOutputStream3;
                        } catch (Throwable th2) {
                            fileOutputStream = fileOutputStream3;
                            th = th2;
                            fileInputStream = fileInputStream3;
                            com.bytedance.sdk.component.utils.wey.fm("TTCrashHandler", "crash count error", th);
                            if (fileInputStream != null) {
                            }
                            if (fileOutputStream == null) {
                            }
                        }
                    }
                    if (z10) {
                        lb();
                    }
                    fileOutputStream = fileOutputStream2;
                    fileInputStream2 = fileInputStream3;
                    if (fileInputStream2 != null) {
                    }
                    if (fileOutputStream == null) {
                    }
                } catch (Throwable th3) {
                    th = th3;
                    fileOutputStream = fileOutputStream2;
                }
            } else {
                Properties properties2 = new Properties();
                properties2.setProperty("crash_count", "1");
                properties2.setProperty("crash_last_time", String.valueOf(System.currentTimeMillis()));
                fileOutputStream = new FileOutputStream(file);
                try {
                    properties2.store(fileOutputStream, "tt_crash_info");
                    if (fileInputStream2 != null) {
                        try {
                            fileInputStream2.close();
                        } catch (Throwable unused2) {
                        }
                    }
                    if (fileOutputStream == null) {
                        try {
                            fileOutputStream.close();
                        } catch (Throwable unused3) {
                        }
                    }
                } catch (Throwable th4) {
                    th = th4;
                    try {
                        com.bytedance.sdk.component.utils.wey.fm("TTCrashHandler", "crash count error", th);
                        if (fileInputStream != null) {
                            try {
                                fileInputStream.close();
                            } catch (Throwable unused4) {
                            }
                        }
                        if (fileOutputStream == null) {
                            try {
                                fileOutputStream.close();
                            } catch (Throwable unused5) {
                            }
                        }
                    } finally {
                    }
                }
            }
        } catch (Throwable th5) {
            th = th5;
            fileOutputStream = null;
        }
    }
}
