package com.unity3d.services; import com.unity3d.ads.core.configuration.AlternativeFlowReader; import com.unity3d.ads.core.domain.SendDiagnosticEvent; import com.unity3d.ads.core.extensions.ExceptionExtensionsKt; import com.unity3d.services.core.log.DeviceLog; import com.unity3d.services.core.request.metrics.Metric; import com.unity3d.services.core.request.metrics.SDKMetricsSender; import java.io.PrintWriter; import java.io.StringWriter; import kotlin.coroutines.CoroutineContext; import kotlin.jvm.functions.Function2; import kotlin.jvm.internal.DefaultConstructorMarker; import kotlin.jvm.internal.Intrinsics; import kotlin.sequences.Sequence; import kotlin.sequences.SequencesKt___SequencesKt; import kotlin.text.StringsKt__StringsKt; import kotlinx.coroutines.BuildersKt__Builders_commonKt; import kotlinx.coroutines.CoroutineDispatcher; import kotlinx.coroutines.CoroutineExceptionHandler; import kotlinx.coroutines.CoroutineName; import kotlinx.coroutines.CoroutineScope; import kotlinx.coroutines.CoroutineScopeKt; /* loaded from: classes4.dex */ public final class SDKErrorHandler implements CoroutineExceptionHandler { public static final Companion Companion = new Companion(null); public static final String UNITY_PACKAGE = "com.unity3d"; public static final String UNKNOWN_FILE = "unknown"; private final AlternativeFlowReader alternativeFlowReader; private final CoroutineDispatcher ioDispatcher; private final CoroutineExceptionHandler.Key key; private final CoroutineScope scope; private final SDKMetricsSender sdkMetricsSender; private final SendDiagnosticEvent sendDiagnosticEvent; @Override // kotlin.coroutines.CoroutineContext.Element public CoroutineExceptionHandler.Key getKey() { return this.key; } public SDKErrorHandler(CoroutineDispatcher ioDispatcher, AlternativeFlowReader alternativeFlowReader, SendDiagnosticEvent sendDiagnosticEvent, SDKMetricsSender sdkMetricsSender) { Intrinsics.checkNotNullParameter(ioDispatcher, "ioDispatcher"); Intrinsics.checkNotNullParameter(alternativeFlowReader, "alternativeFlowReader"); Intrinsics.checkNotNullParameter(sendDiagnosticEvent, "sendDiagnosticEvent"); Intrinsics.checkNotNullParameter(sdkMetricsSender, "sdkMetricsSender"); this.ioDispatcher = ioDispatcher; this.alternativeFlowReader = alternativeFlowReader; this.sendDiagnosticEvent = sendDiagnosticEvent; this.sdkMetricsSender = sdkMetricsSender; this.scope = CoroutineScopeKt.plus(CoroutineScopeKt.CoroutineScope(ioDispatcher), new CoroutineName("SDKErrorHandler")); this.key = CoroutineExceptionHandler.Key; } @Override // kotlin.coroutines.CoroutineContext public R fold(R r, Function2 function2) { return (R) CoroutineExceptionHandler.DefaultImpls.fold(this, r, function2); } @Override // kotlin.coroutines.CoroutineContext.Element, kotlin.coroutines.CoroutineContext public E get(CoroutineContext.Key key) { return (E) CoroutineExceptionHandler.DefaultImpls.get(this, key); } @Override // kotlin.coroutines.CoroutineContext public CoroutineContext minusKey(CoroutineContext.Key key) { return CoroutineExceptionHandler.DefaultImpls.minusKey(this, key); } @Override // kotlin.coroutines.CoroutineContext public CoroutineContext plus(CoroutineContext coroutineContext) { return CoroutineExceptionHandler.DefaultImpls.plus(this, coroutineContext); } @Override // kotlinx.coroutines.CoroutineExceptionHandler public void handleException(CoroutineContext context, Throwable exception) { String str; Intrinsics.checkNotNullParameter(context, "context"); Intrinsics.checkNotNullParameter(exception, "exception"); String retrieveCoroutineName = retrieveCoroutineName(context); if (exception instanceof NullPointerException) { str = "native_exception_npe"; } else if (exception instanceof OutOfMemoryError) { str = "native_exception_oom"; } else if (exception instanceof IllegalStateException) { str = "native_exception_ise"; } else if (exception instanceof SecurityException) { str = "native_exception_se"; } else { str = exception instanceof RuntimeException ? "native_exception_re" : "native_exception"; } String str2 = str; boolean invoke = this.alternativeFlowReader.invoke(); String retrieveUnityCrashValue = ExceptionExtensionsKt.retrieveUnityCrashValue(exception); DeviceLog.error("Unity Ads SDK encountered an exception: " + retrieveUnityCrashValue); if (invoke) { sendDiagnostic(str2, retrieveUnityCrashValue, retrieveCoroutineName, getShortenedStackTrace(exception, 15)); } else { sendMetric(new Metric(str2, retrieveUnityCrashValue, null, 4, null)); } } private final void sendDiagnostic(String str, String str2, String str3, String str4) { BuildersKt__Builders_commonKt.launch$default(this.scope, null, null, new SDKErrorHandler$sendDiagnostic$1(this, str, str2, str4, str3, null), 3, null); } private final void sendMetric(Metric metric) { this.sdkMetricsSender.sendMetric(metric); } private final String retrieveCoroutineName(CoroutineContext coroutineContext) { String name; CoroutineName coroutineName = (CoroutineName) coroutineContext.get(CoroutineName.Key); return (coroutineName == null || (name = coroutineName.getName()) == null) ? "unknown" : name; } private final String getShortenedStackTrace(Throwable th, int i) { CharSequence trim; Sequence take; String joinToString$default; try { StringWriter stringWriter = new StringWriter(); th.printStackTrace(new PrintWriter(stringWriter)); String stringWriter2 = stringWriter.toString(); Intrinsics.checkNotNullExpressionValue(stringWriter2, "writer.toString()"); trim = StringsKt__StringsKt.trim(stringWriter2); take = SequencesKt___SequencesKt.take(StringsKt__StringsKt.lineSequence(trim.toString()), i); joinToString$default = SequencesKt___SequencesKt.joinToString$default(take, "\n", null, null, 0, null, null, 62, null); return joinToString$default; } catch (Throwable unused) { return ""; } } public static final class Companion { public /* synthetic */ Companion(DefaultConstructorMarker defaultConstructorMarker) { this(); } private Companion() { } } }