package com.facebook.internal.instrument; import androidx.annotation.RestrictTo; import com.facebook.FacebookSdk; import com.facebook.GraphRequest; import com.facebook.internal.Utility; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.FilenameFilter; import java.util.Arrays; import java.util.Iterator; import kotlin.jvm.internal.Intrinsics; import kotlin.jvm.internal.StringCompanionObject; import kotlin.text.Charsets; import kotlin.text.Regex; import kotlin.text.StringsKt__StringsJVMKt; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; @RestrictTo({RestrictTo.Scope.LIBRARY_GROUP}) /* loaded from: classes2.dex */ public final class InstrumentUtility { public static final String ANALYSIS_REPORT_PREFIX = "analysis_log_"; public static final String ANR_REPORT_PREFIX = "anr_log_"; private static final String CODELESS_PREFIX = "com.facebook.appevents.codeless"; public static final String CRASH_REPORT_PREFIX = "crash_log_"; public static final String CRASH_SHIELD_PREFIX = "shield_log_"; public static final String ERROR_REPORT_PREFIX = "error_log_"; private static final String FBSDK_PREFIX = "com.facebook"; public static final InstrumentUtility INSTANCE = new InstrumentUtility(); private static final String INSTRUMENT_DIR = "instrument"; private static final String METASDK_PREFIX = "com.meta"; private static final String SUGGESTED_EVENTS_PREFIX = "com.facebook.appevents.suggestedevents"; public static final String THREAD_CHECK_PREFIX = "thread_check_log_"; private InstrumentUtility() { } public static final String getCause(Throwable th) { if (th == null) { return null; } if (th.getCause() == null) { return th.toString(); } return String.valueOf(th.getCause()); } public static final String getStackTrace(Throwable th) { Throwable th2 = null; if (th == null) { return null; } JSONArray jSONArray = new JSONArray(); while (th != null && th != th2) { StackTraceElement[] stackTrace = th.getStackTrace(); Intrinsics.checkNotNullExpressionValue(stackTrace, "t.stackTrace"); int length = stackTrace.length; int i = 0; while (i < length) { StackTraceElement stackTraceElement = stackTrace[i]; i++; jSONArray.put(stackTraceElement.toString()); } th2 = th; th = th.getCause(); } return jSONArray.toString(); } public static final String getStackTrace(Thread thread) { Intrinsics.checkNotNullParameter(thread, "thread"); StackTraceElement[] stackTrace = thread.getStackTrace(); JSONArray jSONArray = new JSONArray(); Intrinsics.checkNotNullExpressionValue(stackTrace, "stackTrace"); int length = stackTrace.length; int i = 0; while (i < length) { StackTraceElement stackTraceElement = stackTrace[i]; i++; jSONArray.put(stackTraceElement.toString()); } return jSONArray.toString(); } public static final boolean isSDKRelatedException(Throwable th) { if (th == null) { return false; } Throwable th2 = null; while (th != null && th != th2) { StackTraceElement[] stackTrace = th.getStackTrace(); Intrinsics.checkNotNullExpressionValue(stackTrace, "t.stackTrace"); int length = stackTrace.length; int i = 0; while (i < length) { StackTraceElement element = stackTrace[i]; i++; Intrinsics.checkNotNullExpressionValue(element, "element"); if (isFromFbOrMeta(element)) { return true; } } th2 = th; th = th.getCause(); } return false; } public static final boolean isSDKRelatedThread(Thread thread) { StackTraceElement[] stackTrace; if (thread != null && (stackTrace = thread.getStackTrace()) != null) { for (StackTraceElement element : stackTrace) { Intrinsics.checkNotNullExpressionValue(element, "element"); if (isFromFbOrMeta(element)) { String className = element.getClassName(); Intrinsics.checkNotNullExpressionValue(className, "element.className"); if (!StringsKt__StringsJVMKt.startsWith$default(className, CODELESS_PREFIX, false, 2, null)) { String className2 = element.getClassName(); Intrinsics.checkNotNullExpressionValue(className2, "element.className"); if (!StringsKt__StringsJVMKt.startsWith$default(className2, SUGGESTED_EVENTS_PREFIX, false, 2, null)) { return true; } } String methodName = element.getMethodName(); Intrinsics.checkNotNullExpressionValue(methodName, "element.methodName"); if (StringsKt__StringsJVMKt.startsWith$default(methodName, "onClick", false, 2, null)) { continue; } else { String methodName2 = element.getMethodName(); Intrinsics.checkNotNullExpressionValue(methodName2, "element.methodName"); if (StringsKt__StringsJVMKt.startsWith$default(methodName2, "onItemClick", false, 2, null)) { continue; } else { String methodName3 = element.getMethodName(); Intrinsics.checkNotNullExpressionValue(methodName3, "element.methodName"); if (!StringsKt__StringsJVMKt.startsWith$default(methodName3, "onTouch", false, 2, null)) { return true; } } } } } } return false; } public static final File[] listAnrReportFiles() { File instrumentReportDir = getInstrumentReportDir(); if (instrumentReportDir == null) { return new File[0]; } File[] listFiles = instrumentReportDir.listFiles(new FilenameFilter() { // from class: com.facebook.internal.instrument.InstrumentUtility$$ExternalSyntheticLambda2 @Override // java.io.FilenameFilter public final boolean accept(File file, String str) { boolean m596listAnrReportFiles$lambda1; m596listAnrReportFiles$lambda1 = InstrumentUtility.m596listAnrReportFiles$lambda1(file, str); return m596listAnrReportFiles$lambda1; } }); return listFiles == null ? new File[0] : listFiles; } /* JADX INFO: Access modifiers changed from: private */ /* renamed from: listAnrReportFiles$lambda-1, reason: not valid java name */ public static final boolean m596listAnrReportFiles$lambda1(File file, String name) { Intrinsics.checkNotNullExpressionValue(name, "name"); StringCompanionObject stringCompanionObject = StringCompanionObject.INSTANCE; String format = String.format("^%s[0-9]+.json$", Arrays.copyOf(new Object[]{ANR_REPORT_PREFIX}, 1)); Intrinsics.checkNotNullExpressionValue(format, "java.lang.String.format(format, *args)"); return new Regex(format).matches(name); } public static final File[] listExceptionAnalysisReportFiles() { File instrumentReportDir = getInstrumentReportDir(); if (instrumentReportDir == null) { return new File[0]; } File[] listFiles = instrumentReportDir.listFiles(new FilenameFilter() { // from class: com.facebook.internal.instrument.InstrumentUtility$$ExternalSyntheticLambda1 @Override // java.io.FilenameFilter public final boolean accept(File file, String str) { boolean m597listExceptionAnalysisReportFiles$lambda2; m597listExceptionAnalysisReportFiles$lambda2 = InstrumentUtility.m597listExceptionAnalysisReportFiles$lambda2(file, str); return m597listExceptionAnalysisReportFiles$lambda2; } }); return listFiles == null ? new File[0] : listFiles; } /* JADX INFO: Access modifiers changed from: private */ /* renamed from: listExceptionAnalysisReportFiles$lambda-2, reason: not valid java name */ public static final boolean m597listExceptionAnalysisReportFiles$lambda2(File file, String name) { Intrinsics.checkNotNullExpressionValue(name, "name"); StringCompanionObject stringCompanionObject = StringCompanionObject.INSTANCE; String format = String.format("^%s[0-9]+.json$", Arrays.copyOf(new Object[]{ANALYSIS_REPORT_PREFIX}, 1)); Intrinsics.checkNotNullExpressionValue(format, "java.lang.String.format(format, *args)"); return new Regex(format).matches(name); } public static final File[] listExceptionReportFiles() { File instrumentReportDir = getInstrumentReportDir(); if (instrumentReportDir == null) { return new File[0]; } File[] listFiles = instrumentReportDir.listFiles(new FilenameFilter() { // from class: com.facebook.internal.instrument.InstrumentUtility$$ExternalSyntheticLambda0 @Override // java.io.FilenameFilter public final boolean accept(File file, String str) { boolean m598listExceptionReportFiles$lambda3; m598listExceptionReportFiles$lambda3 = InstrumentUtility.m598listExceptionReportFiles$lambda3(file, str); return m598listExceptionReportFiles$lambda3; } }); return listFiles == null ? new File[0] : listFiles; } /* JADX INFO: Access modifiers changed from: private */ /* renamed from: listExceptionReportFiles$lambda-3, reason: not valid java name */ public static final boolean m598listExceptionReportFiles$lambda3(File file, String name) { Intrinsics.checkNotNullExpressionValue(name, "name"); StringCompanionObject stringCompanionObject = StringCompanionObject.INSTANCE; String format = String.format("^(%s|%s|%s)[0-9]+.json$", Arrays.copyOf(new Object[]{CRASH_REPORT_PREFIX, CRASH_SHIELD_PREFIX, THREAD_CHECK_PREFIX}, 3)); Intrinsics.checkNotNullExpressionValue(format, "java.lang.String.format(format, *args)"); return new Regex(format).matches(name); } public static final JSONObject readFile(String str, boolean z) { File instrumentReportDir = getInstrumentReportDir(); if (instrumentReportDir != null && str != null) { try { return new JSONObject(Utility.readStreamToString(new FileInputStream(new File(instrumentReportDir, str)))); } catch (Exception unused) { if (z) { deleteFile(str); } } } return null; } public static final void writeFile(String str, String str2) { File instrumentReportDir = getInstrumentReportDir(); if (instrumentReportDir == null || str == null || str2 == null) { return; } try { FileOutputStream fileOutputStream = new FileOutputStream(new File(instrumentReportDir, str)); byte[] bytes = str2.getBytes(Charsets.UTF_8); Intrinsics.checkNotNullExpressionValue(bytes, "(this as java.lang.String).getBytes(charset)"); fileOutputStream.write(bytes); fileOutputStream.close(); } catch (Exception unused) { } } public static final boolean deleteFile(String str) { File instrumentReportDir = getInstrumentReportDir(); if (instrumentReportDir == null || str == null) { return false; } return new File(instrumentReportDir, str).delete(); } public static final void sendReports(String str, JSONArray reports, GraphRequest.Callback callback) { Intrinsics.checkNotNullParameter(reports, "reports"); if (reports.length() == 0) { return; } JSONObject jSONObject = new JSONObject(); try { jSONObject.put(str, reports.toString()); JSONObject dataProcessingOptions = Utility.getDataProcessingOptions(); if (dataProcessingOptions != null) { Iterator keys = dataProcessingOptions.keys(); while (keys.hasNext()) { String next = keys.next(); jSONObject.put(next, dataProcessingOptions.get(next)); } } GraphRequest.Companion companion = GraphRequest.Companion; StringCompanionObject stringCompanionObject = StringCompanionObject.INSTANCE; String format = String.format("%s/instruments", Arrays.copyOf(new Object[]{FacebookSdk.getApplicationId()}, 1)); Intrinsics.checkNotNullExpressionValue(format, "java.lang.String.format(format, *args)"); companion.newPostRequest(null, format, jSONObject, callback).executeAsync(); } catch (JSONException unused) { } } public static final File getInstrumentReportDir() { File file = new File(FacebookSdk.getApplicationContext().getCacheDir(), INSTRUMENT_DIR); if (file.exists() || file.mkdirs()) { return file; } return null; } public static final boolean isFromFbOrMeta(StackTraceElement element) { Intrinsics.checkNotNullParameter(element, "element"); String className = element.getClassName(); Intrinsics.checkNotNullExpressionValue(className, "element.className"); if (!StringsKt__StringsJVMKt.startsWith$default(className, "com.facebook", false, 2, null)) { String className2 = element.getClassName(); Intrinsics.checkNotNullExpressionValue(className2, "element.className"); if (!StringsKt__StringsJVMKt.startsWith$default(className2, METASDK_PREFIX, false, 2, null)) { return false; } } return true; } }