package com.baidu.uaq.agent.android.crashes; import com.baidu.uaq.agent.android.UAQ; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStream; import java.lang.Thread; import java.net.HttpURLConnection; import java.net.URL; import java.util.Iterator; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.atomic.AtomicBoolean; /* compiled from: CrashReporter.java */ /* loaded from: classes.dex */ public class c { private static d f; private static ExecutorService h; private Thread.UncaughtExceptionHandler a; private boolean b; private static final com.baidu.uaq.agent.android.logging.a c = com.baidu.uaq.agent.android.logging.b.a(); private static final UAQ d = UAQ.getInstance(); private static final c e = new c(); private static final AtomicBoolean g = new AtomicBoolean(false); /* compiled from: CrashReporter.java */ private static class a implements Runnable { private final com.baidu.uaq.agent.android.crashes.b a; a(com.baidu.uaq.agent.android.crashes.b bVar) { this.a = bVar; } /* JADX WARN: Multi-variable type inference failed */ /* JADX WARN: Type inference failed for: r2v22 */ /* JADX WARN: Type inference failed for: r2v23 */ /* JADX WARN: Type inference failed for: r2v24, types: [java.io.InputStream] */ /* JADX WARN: Type inference failed for: r2v26, types: [java.io.InputStream] */ /* JADX WARN: Type inference failed for: r2v27 */ /* JADX WARN: Type inference failed for: r2v4, types: [java.lang.StringBuilder] */ /* JADX WARN: Type inference failed for: r2v5, types: [java.io.InputStream] */ /* JADX WARN: Type inference failed for: r2v6 */ /* JADX WARN: Type inference failed for: r2v7 */ /* JADX WARN: Type inference failed for: r2v8, types: [java.io.InputStream] */ @Override // java.lang.Runnable public void run() { HttpURLConnection httpURLConnection; String str = c.d.getConfig().isUseSsl() ? "https://" : "http://"; String str2 = ":" + c.d.getConfig().getCollectorPort(); ?? sb = new StringBuilder(); sb.append(str); sb.append(c.d.getConfig().getCollectorHost()); sb.append(str2); sb.append("/sdk_push_crash"); String sb2 = sb.toString(); InputStreamReader inputStreamReader = null; try { try { try { c.c.d("Crash url = " + sb2); httpURLConnection = (HttpURLConnection) new URL(sb2).openConnection(); } catch (Exception e) { e = e; sb = 0; } catch (Throwable th) { th = th; sb = 0; } if (httpURLConnection == null) { c.c.d("connection is null when send crash data!"); return; } httpURLConnection.setConnectTimeout(5000); httpURLConnection.setRequestMethod("POST"); httpURLConnection.setDoInput(true); httpURLConnection.setDoOutput(true); httpURLConnection.setRequestProperty("Content-Type", "application/json"); httpURLConnection.setRequestProperty("X-App-License-Key", c.d.getConfig().getAPIKey()); httpURLConnection.setRequestProperty("Content-Encoding", "gzip"); byte[] a = com.baidu.uaq.agent.android.util.b.a(this.a.c() + "\n", "utf-8"); httpURLConnection.setFixedLengthStreamingMode(a.length); httpURLConnection.setRequestProperty("Content-Length", String.valueOf(a.length)); OutputStream outputStream = httpURLConnection.getOutputStream(); outputStream.write(a); outputStream.flush(); outputStream.close(); if (httpURLConnection.getResponseCode() == 200) { c.c.d("Crash " + this.a.e().toString() + " successfully submitted."); sb = httpURLConnection.getInputStream(); try { InputStreamReader inputStreamReader2 = new InputStreamReader(sb); try { BufferedReader bufferedReader = new BufferedReader(inputStreamReader2); StringBuilder sb3 = new StringBuilder(); while (true) { String readLine = bufferedReader.readLine(); if (readLine == null) { break; } else { sb3.append(readLine); } } c.c.d("send crash success, response: " + sb3.toString()); c.f.b(this.a); inputStreamReader = inputStreamReader2; sb = sb; } catch (Exception e2) { e = e2; inputStreamReader = inputStreamReader2; c.c.a("Unable to report crash to UAQ, will try again later.", e); if (inputStreamReader != null) { try { inputStreamReader.close(); } catch (IOException e3) { e3.printStackTrace(); } } if (sb != 0) { sb.close(); } return; } catch (Throwable th2) { th = th2; inputStreamReader = inputStreamReader2; if (inputStreamReader != null) { try { inputStreamReader.close(); } catch (IOException e4) { e4.printStackTrace(); } } if (sb == 0) { throw th; } try { sb.close(); throw th; } catch (IOException e5) { e5.printStackTrace(); throw th; } } } catch (Exception e6) { e = e6; } } else { c.c.a("Something went wrong while submitting a crash (will try again later) - Response code " + httpURLConnection.getResponseCode()); sb = 0; } httpURLConnection.disconnect(); if (inputStreamReader != null) { try { inputStreamReader.close(); } catch (IOException e7) { e7.printStackTrace(); } } if (sb != 0) { sb.close(); } } catch (IOException e8) { e8.printStackTrace(); } } catch (Throwable th3) { th = th3; } } } /* compiled from: CrashReporter.java */ private class b implements Thread.UncaughtExceptionHandler { private final AtomicBoolean a; private void a(Thread thread, Throwable th) { if (c.this.a != null) { c.c.d("Chaining crash reporting duties to " + c.this.a.getClass().getSimpleName()); c.this.a.uncaughtException(thread, th); } } @Override // java.lang.Thread.UncaughtExceptionHandler public void uncaughtException(Thread thread, Throwable th) { if (!this.a.compareAndSet(false, true)) { com.baidu.uaq.agent.android.stats.a.b().a("Supportability/AgentHealth/Recursion/UncaughtExceptionHandler"); return; } if (!c.d.getConfig().isReportCrashes() || !c.e.b) { c.c.d("A crash has been detected but crash reporting is disabled!"); a(thread, th); return; } try { com.baidu.uaq.agent.android.stats.b bVar = new com.baidu.uaq.agent.android.stats.b(); bVar.a(); c.c.d("A crash has been detected in " + thread.getStackTrace()[0].getClassName() + " and will be reported ASAP."); com.baidu.uaq.agent.android.crashes.b bVar2 = new com.baidu.uaq.agent.android.crashes.b(th); c unused = c.e; c.f.a(bVar2); c.a(bVar2); c.c.d("Crash collection took " + bVar.b() + "ms"); a(thread, th); } catch (Throwable unused2) { a(thread, th); } } private b() { this.a = new AtomicBoolean(false); } } public c() { h = Executors.newCachedThreadPool(new com.baidu.uaq.agent.android.util.g("CrashReporter")); this.b = false; } public static void a(d dVar) { e.b = true; if (g.compareAndSet(false, true)) { f = dVar; h.submit(new Runnable() { // from class: com.baidu.uaq.agent.android.crashes.c.1 @Override // java.lang.Runnable public void run() { c.e.a(); } }); e.b(); } } private void b() { Thread.UncaughtExceptionHandler defaultUncaughtExceptionHandler = Thread.getDefaultUncaughtExceptionHandler(); if (defaultUncaughtExceptionHandler == null) { c.d("Installing UAQ crash handler."); } else { if (defaultUncaughtExceptionHandler instanceof b) { c.d("UAQ crash handler already installed."); return; } this.a = defaultUncaughtExceptionHandler; c.d("Installing UAQ crash handler and chaining " + this.a.getClass().getName()); } Thread.setDefaultUncaughtExceptionHandler(new b()); } /* JADX INFO: Access modifiers changed from: private */ public void a() { c.d("reportSavedCrashes, size=" + f.b()); Iterator it = f.a().iterator(); while (it.hasNext()) { a(it.next()); } } public static void a(com.baidu.uaq.agent.android.crashes.b bVar) { h.submit(new a(bVar)); } }