package com.facebook.internal.instrument.crashreport; import android.util.Log; import androidx.annotation.RestrictTo; import com.facebook.FacebookSdk; import com.facebook.GraphRequest; import com.facebook.GraphResponse; import com.facebook.internal.Utility; import com.facebook.internal.instrument.ExceptionAnalyzer; import com.facebook.internal.instrument.InstrumentData; import com.facebook.internal.instrument.InstrumentUtility; import com.facebook.internal.instrument.crashreport.CrashHandler; import java.io.File; import java.lang.Thread; import java.util.ArrayList; import java.util.Comparator; import java.util.Iterator; import java.util.List; import kotlin.collections.CollectionsKt___CollectionsKt; import kotlin.collections.IntIterator; import kotlin.jvm.internal.DefaultConstructorMarker; import kotlin.jvm.internal.Intrinsics; import kotlin.ranges.IntRange; import kotlin.ranges.RangesKt___RangesKt; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; @RestrictTo({RestrictTo.Scope.LIBRARY_GROUP}) /* loaded from: classes2.dex */ public final class CrashHandler implements Thread.UncaughtExceptionHandler { private static final int MAX_CRASH_REPORT_NUM = 5; private static CrashHandler instance; private final Thread.UncaughtExceptionHandler previousHandler; public static final Companion Companion = new Companion(null); private static final String TAG = CrashHandler.class.getCanonicalName(); public /* synthetic */ CrashHandler(Thread.UncaughtExceptionHandler uncaughtExceptionHandler, DefaultConstructorMarker defaultConstructorMarker) { this(uncaughtExceptionHandler); } public static final synchronized void enable() { synchronized (CrashHandler.class) { Companion.enable(); } } private CrashHandler(Thread.UncaughtExceptionHandler uncaughtExceptionHandler) { this.previousHandler = uncaughtExceptionHandler; } @Override // java.lang.Thread.UncaughtExceptionHandler public void uncaughtException(Thread t, Throwable e) { Intrinsics.checkNotNullParameter(t, "t"); Intrinsics.checkNotNullParameter(e, "e"); if (InstrumentUtility.isSDKRelatedException(e)) { ExceptionAnalyzer.execute(e); InstrumentData.Builder builder = InstrumentData.Builder.INSTANCE; InstrumentData.Builder.build(e, InstrumentData.Type.CrashReport).save(); } Thread.UncaughtExceptionHandler uncaughtExceptionHandler = this.previousHandler; if (uncaughtExceptionHandler == null) { return; } uncaughtExceptionHandler.uncaughtException(t, e); } public static final class Companion { public /* synthetic */ Companion(DefaultConstructorMarker defaultConstructorMarker) { this(); } private Companion() { } public final synchronized void enable() { try { if (FacebookSdk.getAutoLogAppEventsEnabled()) { sendExceptionReports(); } if (CrashHandler.instance != null) { Log.w(CrashHandler.TAG, "Already enabled!"); } else { CrashHandler.instance = new CrashHandler(Thread.getDefaultUncaughtExceptionHandler(), null); Thread.setDefaultUncaughtExceptionHandler(CrashHandler.instance); } } catch (Throwable th) { throw th; } } private final void sendExceptionReports() { final List sortedWith; IntRange until; if (Utility.isDataProcessingRestricted()) { return; } File[] listExceptionReportFiles = InstrumentUtility.listExceptionReportFiles(); ArrayList arrayList = new ArrayList(listExceptionReportFiles.length); for (File file : listExceptionReportFiles) { arrayList.add(InstrumentData.Builder.load(file)); } ArrayList arrayList2 = new ArrayList(); for (Object obj : arrayList) { if (((InstrumentData) obj).isValid()) { arrayList2.add(obj); } } sortedWith = CollectionsKt___CollectionsKt.sortedWith(arrayList2, new Comparator() { // from class: com.facebook.internal.instrument.crashreport.CrashHandler$Companion$$ExternalSyntheticLambda0 @Override // java.util.Comparator public final int compare(Object obj2, Object obj3) { int m603sendExceptionReports$lambda2; m603sendExceptionReports$lambda2 = CrashHandler.Companion.m603sendExceptionReports$lambda2((InstrumentData) obj2, (InstrumentData) obj3); return m603sendExceptionReports$lambda2; } }); JSONArray jSONArray = new JSONArray(); until = RangesKt___RangesKt.until(0, Math.min(sortedWith.size(), 5)); Iterator it = until.iterator(); while (it.hasNext()) { jSONArray.put(sortedWith.get(((IntIterator) it).nextInt())); } InstrumentUtility instrumentUtility = InstrumentUtility.INSTANCE; InstrumentUtility.sendReports("crash_reports", jSONArray, new GraphRequest.Callback() { // from class: com.facebook.internal.instrument.crashreport.CrashHandler$Companion$$ExternalSyntheticLambda1 @Override // com.facebook.GraphRequest.Callback public final void onCompleted(GraphResponse graphResponse) { CrashHandler.Companion.m604sendExceptionReports$lambda5(sortedWith, graphResponse); } }); } /* JADX INFO: Access modifiers changed from: private */ /* renamed from: sendExceptionReports$lambda-2, reason: not valid java name */ public static final int m603sendExceptionReports$lambda2(InstrumentData instrumentData, InstrumentData o2) { Intrinsics.checkNotNullExpressionValue(o2, "o2"); return instrumentData.compareTo(o2); } /* JADX INFO: Access modifiers changed from: private */ /* renamed from: sendExceptionReports$lambda-5, reason: not valid java name */ public static final void m604sendExceptionReports$lambda5(List validReports, GraphResponse response) { Intrinsics.checkNotNullParameter(validReports, "$validReports"); Intrinsics.checkNotNullParameter(response, "response"); try { if (response.getError() == null) { JSONObject jsonObject = response.getJsonObject(); if (Intrinsics.areEqual(jsonObject == null ? null : Boolean.valueOf(jsonObject.getBoolean("success")), Boolean.TRUE)) { Iterator it = validReports.iterator(); while (it.hasNext()) { ((InstrumentData) it.next()).clear(); } } } } catch (JSONException unused) { } } } }