package com.baidu.ocr.sdk.utils; import android.content.Context; import com.baidu.ocr.sdk.OCR; import com.baidu.ocr.sdk.utils.HttpsClient; import java.io.FileInputStream; import java.io.FileOutputStream; import java.lang.Thread; import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashSet; import java.util.Set; import org.json.JSONException; import org.json.JSONObject; public class CrashReporterHandler implements Thread.UncaughtExceptionHandler { private static final String EMPTY_STR = ""; private static final String HTTP_HEADER_CONTENTTYPE = "application/x-www-form-urlencoded;charset=utf-8"; private static final String HTTP_URL = "https://verify.baidubce.com/verify/1.0/sdk/report"; private static final String REPORT_FILENAME = "bd_aip_crashreport_file"; private static final String REPORT_TIME_FORMATTER = "yyyy-MM-dd HH:mm:ss"; private static Thread.UncaughtExceptionHandler defaultHandler; private static CrashReporterHandler instance; private Context ctx; private Set<Class> sourceClassSet = new HashSet(); public CrashReporterHandler(Context context) { this.ctx = context; } private String createReport(String str) { JSONObject jSONObject = new JSONObject(); try { jSONObject.put("id", DeviceUtil.getDeviceId(this.ctx)); jSONObject.put("time", getCurrentTime()); jSONObject.put("system", DeviceUtil.getDeviceInfo(this.ctx)); jSONObject.put("sdkver", OCR.OCR_SDK_VERSION); jSONObject.put("appnm", getPackageName()); jSONObject.put("detail", str); return jSONObject.toString(); } catch (JSONException e2) { return e2.toString(); } } private String exDetailContent(Throwable th) { if (th.getCause() == null) { return genContent(th); } return genContent(th) + exDetailContent(th.getCause()); } private String genContent(Throwable th) { StringBuffer stringBuffer = new StringBuffer(); stringBuffer.append("!" + th.getMessage()); stringBuffer.append("|"); StackTraceElement[] stackTrace = th.getStackTrace(); for (StackTraceElement stackTraceElement : stackTrace) { stringBuffer.append(stackTraceElement.getClassName() + " [" + stackTraceElement.getMethodName() + ": " + stackTraceElement.getLineNumber() + "] "); stringBuffer.append("|"); } return stringBuffer.toString(); } private String getCurrentTime() { return new SimpleDateFormat(REPORT_TIME_FORMATTER).format(new Date()); } private String getPackageName() { return this.ctx.getPackageName(); } public static CrashReporterHandler init(Context context) { if (instance == null) { instance = new CrashReporterHandler(context); } if (defaultHandler == null) { defaultHandler = Thread.getDefaultUncaughtExceptionHandler(); } Thread.setDefaultUncaughtExceptionHandler(instance); instance.sendPreviousReport(); return instance; } private boolean isCauseBySource(Throwable th) { if (isStackCauseBySource(th.getStackTrace())) { return true; } if (th.getCause() == null) { return false; } return isCauseBySource(th.getCause()); } private boolean isStackCauseBySource(StackTraceElement[] stackTraceElementArr) { for (StackTraceElement stackTraceElement : stackTraceElementArr) { try { if (this.sourceClassSet.contains(Class.forName(stackTraceElement.getClassName()))) { return true; } } catch (ClassNotFoundException unused) { } } return false; } private String readFile() { StringBuffer stringBuffer = new StringBuffer(); try { FileInputStream openFileInput = this.ctx.openFileInput(REPORT_FILENAME); while (true) { int read = openFileInput.read(); if (read != -1) { stringBuffer.append((char) read); } else { openFileInput.close(); return stringBuffer.toString(); } } } catch (Exception unused) { return null; } } /* access modifiers changed from: private */ /* access modifiers changed from: public */ private boolean writeFile(String str) { try { FileOutputStream openFileOutput = this.ctx.openFileOutput(REPORT_FILENAME, 0); openFileOutput.write(str.getBytes("utf8")); openFileOutput.close(); return true; } catch (Exception unused) { return false; } } public CrashReporterHandler addSourceClass(Class cls) { this.sourceClassSet.add(cls); return instance; } public void postBody(String str) { HttpsClient httpsClient = new HttpsClient(); HttpsClient.RequestBody requestBody = new HttpsClient.RequestBody(); requestBody.setBody(str); HttpsClient.RequestInfo requestInfo = new HttpsClient.RequestInfo(HTTP_URL, requestBody); requestInfo.setHeader("Content-Type", HTTP_HEADER_CONTENTTYPE); requestInfo.build(); httpsClient.newCall(requestInfo).enqueue(new HttpsClient.Callback() { /* class com.baidu.ocr.sdk.utils.CrashReporterHandler.AnonymousClass1 */ @Override // com.baidu.ocr.sdk.utils.HttpsClient.Callback public void onFailure(Throwable th) { } @Override // com.baidu.ocr.sdk.utils.HttpsClient.Callback public void onResponse(String str) { CrashReporterHandler.this.writeFile(""); } }); } public void release() { this.ctx = null; } public void resolveException(Throwable th) { try { if (isCauseBySource(th)) { String createReport = createReport(exDetailContent(th)); if (!writeFile(createReport)) { postBody(createReport); } } } catch (Throwable th2) { String createReport2 = createReport(exDetailContent(th2)); if (!writeFile(createReport2)) { postBody(createReport2); } } } public void sendPreviousReport() { String readFile = readFile(); if (readFile != null && readFile != "") { postBody(readFile); } } public void uncaughtException(Thread thread, Throwable th) { try { resolveException(th); } catch (Throwable unused) { } defaultHandler.uncaughtException(thread, th); } }