package com.ubtrobot.log; import android.annotation.SuppressLint; import android.content.Context; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.os.Build; import android.os.Process; import android.util.Log; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.PrintWriter; import java.io.StringWriter; import java.lang.Thread; import java.lang.reflect.Field; import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashMap; import java.util.Map; /* loaded from: classes2.dex */ public class CrashExceptionHandler implements Thread.UncaughtExceptionHandler { private static CrashExceptionHandler d; private Context a; private Thread.UncaughtExceptionHandler b; private Map c = new HashMap(); private CrashExceptionHandler(Context context) { c(context); } private boolean a(Throwable th, Thread thread) { if (th == null) { return true; } a(this.a); a(th); return true; } public static CrashExceptionHandler b(Context context) { if (d == null) { d = new CrashExceptionHandler(context); } return d; } private void c(Context context) { this.a = context; this.b = Thread.getDefaultUncaughtExceptionHandler(); } @Override // java.lang.Thread.UncaughtExceptionHandler public void uncaughtException(Thread thread, Throwable th) { Thread.UncaughtExceptionHandler uncaughtExceptionHandler; if (!a(th, thread) || (uncaughtExceptionHandler = this.b) == null) { this.b.uncaughtException(thread, th); return; } uncaughtExceptionHandler.uncaughtException(thread, th); Process.killProcess(Process.myPid()); System.exit(10); } @SuppressLint({"SimpleDateFormat"}) private void a(Throwable th) { StringBuilder sb; if (th == null) { return; } StringBuilder sb2 = new StringBuilder(); for (String str : this.c.keySet()) { String str2 = this.c.get(str); sb2.append(str); sb2.append("="); sb2.append(str2); sb2.append("\n"); } StringWriter stringWriter = new StringWriter(); PrintWriter printWriter = new PrintWriter(stringWriter); th.printStackTrace(printWriter); for (Throwable cause = th.getCause(); cause != null; cause = cause.getCause()) { cause.printStackTrace(printWriter); } printWriter.close(); sb2.append(stringWriter.toString()); FileOutputStream fileOutputStream = null; SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss"); long currentTimeMillis = System.currentTimeMillis(); String str3 = simpleDateFormat.format(new Date()) + "_" + currentTimeMillis + ".txt"; try { try { File a = ExternalOverFroyoUtils.a(this.a, "crash"); if (!a.exists()) { a.mkdirs(); } FileOutputStream fileOutputStream2 = new FileOutputStream(new File(a.getAbsolutePath() + File.separator + str3)); try { fileOutputStream2.write(sb2.toString().getBytes()); try { fileOutputStream2.close(); } catch (IOException e) { e = e; sb = new StringBuilder(); sb.append("关闭Crash文件流时出错:"); sb.append(e.getMessage()); Log.e("CrashExceptionHandler", sb.toString()); } } catch (FileNotFoundException e2) { e = e2; fileOutputStream = fileOutputStream2; Log.e("CrashExceptionHandler", "保存crash文件时出错:" + e.getMessage()); if (fileOutputStream != null) { try { fileOutputStream.close(); } catch (IOException e3) { e = e3; sb = new StringBuilder(); sb.append("关闭Crash文件流时出错:"); sb.append(e.getMessage()); Log.e("CrashExceptionHandler", sb.toString()); } } } catch (Exception e4) { e = e4; fileOutputStream = fileOutputStream2; Log.e("CrashExceptionHandler", "保存crash文件时出错:" + e.getMessage()); if (fileOutputStream != null) { try { fileOutputStream.close(); } catch (IOException e5) { e = e5; sb = new StringBuilder(); sb.append("关闭Crash文件流时出错:"); sb.append(e.getMessage()); Log.e("CrashExceptionHandler", sb.toString()); } } } catch (Throwable th2) { th = th2; fileOutputStream = fileOutputStream2; if (fileOutputStream != null) { try { fileOutputStream.close(); } catch (IOException e6) { Log.e("CrashExceptionHandler", "关闭Crash文件流时出错:" + e6.getMessage()); } } throw th; } } catch (FileNotFoundException e7) { e = e7; } catch (Exception e8) { e = e8; } } catch (Throwable th3) { th = th3; } } private void a(Context context) { try { PackageInfo packageInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 1); if (packageInfo != null) { String str = packageInfo.versionName == null ? "null" : packageInfo.versionName; String str2 = packageInfo.versionCode + ""; this.c.put("versionName", str); this.c.put("versionCode", str2); } } catch (PackageManager.NameNotFoundException unused) { Log.e("CrashExceptionHandler", "收集包信息时出错。"); } try { for (Field field : Build.class.getDeclaredFields()) { field.setAccessible(true); this.c.put(field.getName(), field.get(null).toString()); } } catch (Exception unused2) { Log.e("CrashExceptionHandler", "收集crash信息时出错。"); } } }