package com.facebook.appevents; import android.content.Intent; import android.os.Bundle; import android.util.Log; import androidx.annotation.VisibleForTesting; import androidx.localbroadcastmanager.content.LocalBroadcastManager; import com.facebook.FacebookRequestError; import com.facebook.FacebookSdk; import com.facebook.GraphRequest; import com.facebook.GraphResponse; import com.facebook.LoggingBehavior; import com.facebook.appevents.AppEventsLogger; import com.facebook.appevents.cloudbridge.AppEventsCAPIManager; import com.facebook.appevents.cloudbridge.AppEventsConversionsAPITransformerWebRequests; import com.facebook.internal.FetchedAppSettings; import com.facebook.internal.FetchedAppSettingsManager; import com.facebook.internal.Logger; import com.facebook.internal.instrument.crashshield.CrashShieldHandler; import java.util.ArrayList; import java.util.Arrays; import java.util.Iterator; import java.util.List; import java.util.Set; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; import kotlin.jvm.internal.Intrinsics; import kotlin.jvm.internal.StringCompanionObject; import org.json.JSONArray; import org.json.JSONException; /* loaded from: classes2.dex */ public final class AppEventQueue { private static final int FLUSH_PERIOD_IN_SECONDS = 15; private static final int NO_CONNECTIVITY_ERROR_CODE = -1; private static ScheduledFuture scheduledFuture; public static final AppEventQueue INSTANCE = new AppEventQueue(); private static final String TAG = AppEventQueue.class.getName(); private static final int NUM_LOG_EVENTS_TO_TRY_TO_FLUSH_AFTER = 100; private static volatile AppEventCollection appEventCollection = new AppEventCollection(); private static final ScheduledExecutorService singleThreadExecutor = Executors.newSingleThreadScheduledExecutor(); private static final Runnable flushRunnable = new Runnable() { // from class: com.facebook.appevents.AppEventQueue$$ExternalSyntheticLambda5 @Override // java.lang.Runnable public final void run() { AppEventQueue.m453flushRunnable$lambda0(); } }; private AppEventQueue() { } /* JADX INFO: Access modifiers changed from: private */ /* renamed from: flushRunnable$lambda-0, reason: not valid java name */ public static final void m453flushRunnable$lambda0() { if (CrashShieldHandler.isObjectCrashing(AppEventQueue.class)) { return; } try { scheduledFuture = null; if (AppEventsLogger.Companion.getFlushBehavior() != AppEventsLogger.FlushBehavior.EXPLICIT_ONLY) { flushAndWait(FlushReason.TIMER); } } catch (Throwable th) { CrashShieldHandler.handleThrowable(th, AppEventQueue.class); } } public static final void persistToDisk() { if (CrashShieldHandler.isObjectCrashing(AppEventQueue.class)) { return; } try { singleThreadExecutor.execute(new Runnable() { // from class: com.facebook.appevents.AppEventQueue$$ExternalSyntheticLambda3 @Override // java.lang.Runnable public final void run() { AppEventQueue.m455persistToDisk$lambda1(); } }); } catch (Throwable th) { CrashShieldHandler.handleThrowable(th, AppEventQueue.class); } } /* JADX INFO: Access modifiers changed from: private */ /* renamed from: persistToDisk$lambda-1, reason: not valid java name */ public static final void m455persistToDisk$lambda1() { if (CrashShieldHandler.isObjectCrashing(AppEventQueue.class)) { return; } try { AppEventStore appEventStore = AppEventStore.INSTANCE; AppEventStore.persistEvents(appEventCollection); appEventCollection = new AppEventCollection(); } catch (Throwable th) { CrashShieldHandler.handleThrowable(th, AppEventQueue.class); } } public static final void flush(final FlushReason reason) { if (CrashShieldHandler.isObjectCrashing(AppEventQueue.class)) { return; } try { Intrinsics.checkNotNullParameter(reason, "reason"); singleThreadExecutor.execute(new Runnable() { // from class: com.facebook.appevents.AppEventQueue$$ExternalSyntheticLambda2 @Override // java.lang.Runnable public final void run() { AppEventQueue.m452flush$lambda2(FlushReason.this); } }); } catch (Throwable th) { CrashShieldHandler.handleThrowable(th, AppEventQueue.class); } } /* JADX INFO: Access modifiers changed from: private */ /* renamed from: flush$lambda-2, reason: not valid java name */ public static final void m452flush$lambda2(FlushReason reason) { if (CrashShieldHandler.isObjectCrashing(AppEventQueue.class)) { return; } try { Intrinsics.checkNotNullParameter(reason, "$reason"); flushAndWait(reason); } catch (Throwable th) { CrashShieldHandler.handleThrowable(th, AppEventQueue.class); } } public static final void add(final AccessTokenAppIdPair accessTokenAppId, final AppEvent appEvent) { if (CrashShieldHandler.isObjectCrashing(AppEventQueue.class)) { return; } try { Intrinsics.checkNotNullParameter(accessTokenAppId, "accessTokenAppId"); Intrinsics.checkNotNullParameter(appEvent, "appEvent"); singleThreadExecutor.execute(new Runnable() { // from class: com.facebook.appevents.AppEventQueue$$ExternalSyntheticLambda4 @Override // java.lang.Runnable public final void run() { AppEventQueue.m450add$lambda3(AccessTokenAppIdPair.this, appEvent); } }); } catch (Throwable th) { CrashShieldHandler.handleThrowable(th, AppEventQueue.class); } } /* JADX INFO: Access modifiers changed from: private */ /* renamed from: add$lambda-3, reason: not valid java name */ public static final void m450add$lambda3(AccessTokenAppIdPair accessTokenAppId, AppEvent appEvent) { if (CrashShieldHandler.isObjectCrashing(AppEventQueue.class)) { return; } try { Intrinsics.checkNotNullParameter(accessTokenAppId, "$accessTokenAppId"); Intrinsics.checkNotNullParameter(appEvent, "$appEvent"); appEventCollection.addEvent(accessTokenAppId, appEvent); if (AppEventsLogger.Companion.getFlushBehavior() != AppEventsLogger.FlushBehavior.EXPLICIT_ONLY && appEventCollection.getEventCount() > NUM_LOG_EVENTS_TO_TRY_TO_FLUSH_AFTER) { flushAndWait(FlushReason.EVENT_THRESHOLD); } else if (scheduledFuture == null) { scheduledFuture = singleThreadExecutor.schedule(flushRunnable, 15L, TimeUnit.SECONDS); } } catch (Throwable th) { CrashShieldHandler.handleThrowable(th, AppEventQueue.class); } } public static final Set getKeySet() { if (CrashShieldHandler.isObjectCrashing(AppEventQueue.class)) { return null; } try { return appEventCollection.keySet(); } catch (Throwable th) { CrashShieldHandler.handleThrowable(th, AppEventQueue.class); return null; } } public static final void flushAndWait(FlushReason reason) { if (CrashShieldHandler.isObjectCrashing(AppEventQueue.class)) { return; } try { Intrinsics.checkNotNullParameter(reason, "reason"); appEventCollection.addPersistedEvents(AppEventDiskStore.readAndClearStore()); try { FlushStatistics sendEventsToServer = sendEventsToServer(reason, appEventCollection); if (sendEventsToServer != null) { Intent intent = new Intent(AppEventsLogger.ACTION_APP_EVENTS_FLUSHED); intent.putExtra(AppEventsLogger.APP_EVENTS_EXTRA_NUM_EVENTS_FLUSHED, sendEventsToServer.getNumEvents()); intent.putExtra(AppEventsLogger.APP_EVENTS_EXTRA_FLUSH_RESULT, sendEventsToServer.getResult()); LocalBroadcastManager.getInstance(FacebookSdk.getApplicationContext()).sendBroadcast(intent); } } catch (Exception e) { Log.w(TAG, "Caught unexpected exception while flushing app events: ", e); } } catch (Throwable th) { CrashShieldHandler.handleThrowable(th, AppEventQueue.class); } } @VisibleForTesting(otherwise = 2) public static final FlushStatistics sendEventsToServer(FlushReason reason, AppEventCollection appEventCollection2) { if (CrashShieldHandler.isObjectCrashing(AppEventQueue.class)) { return null; } try { Intrinsics.checkNotNullParameter(reason, "reason"); Intrinsics.checkNotNullParameter(appEventCollection2, "appEventCollection"); FlushStatistics flushStatistics = new FlushStatistics(); List buildRequests = buildRequests(appEventCollection2, flushStatistics); if (!(!buildRequests.isEmpty())) { return null; } Logger.Companion companion = Logger.Companion; LoggingBehavior loggingBehavior = LoggingBehavior.APP_EVENTS; String TAG2 = TAG; Intrinsics.checkNotNullExpressionValue(TAG2, "TAG"); companion.log(loggingBehavior, TAG2, "Flushing %d events due to %s.", Integer.valueOf(flushStatistics.getNumEvents()), reason.toString()); Iterator it = buildRequests.iterator(); while (it.hasNext()) { it.next().executeAndWait(); } return flushStatistics; } catch (Throwable th) { CrashShieldHandler.handleThrowable(th, AppEventQueue.class); return null; } } public static final List buildRequests(AppEventCollection appEventCollection2, FlushStatistics flushResults) { if (CrashShieldHandler.isObjectCrashing(AppEventQueue.class)) { return null; } try { Intrinsics.checkNotNullParameter(appEventCollection2, "appEventCollection"); Intrinsics.checkNotNullParameter(flushResults, "flushResults"); boolean limitEventAndDataUsage = FacebookSdk.getLimitEventAndDataUsage(FacebookSdk.getApplicationContext()); ArrayList arrayList = new ArrayList(); for (AccessTokenAppIdPair accessTokenAppIdPair : appEventCollection2.keySet()) { SessionEventsState sessionEventsState = appEventCollection2.get(accessTokenAppIdPair); if (sessionEventsState != null) { GraphRequest buildRequestForSession = buildRequestForSession(accessTokenAppIdPair, sessionEventsState, limitEventAndDataUsage, flushResults); if (buildRequestForSession != null) { arrayList.add(buildRequestForSession); if (AppEventsCAPIManager.INSTANCE.isEnabled$facebook_core_release()) { AppEventsConversionsAPITransformerWebRequests.transformGraphRequestAndSendToCAPIGEndPoint(buildRequestForSession); } } } else { throw new IllegalStateException("Required value was null.".toString()); } } return arrayList; } catch (Throwable th) { CrashShieldHandler.handleThrowable(th, AppEventQueue.class); return null; } } public static final GraphRequest buildRequestForSession(final AccessTokenAppIdPair accessTokenAppId, final SessionEventsState appEvents, boolean z, final FlushStatistics flushState) { if (CrashShieldHandler.isObjectCrashing(AppEventQueue.class)) { return null; } try { Intrinsics.checkNotNullParameter(accessTokenAppId, "accessTokenAppId"); Intrinsics.checkNotNullParameter(appEvents, "appEvents"); Intrinsics.checkNotNullParameter(flushState, "flushState"); String applicationId = accessTokenAppId.getApplicationId(); FetchedAppSettings queryAppSettings = FetchedAppSettingsManager.queryAppSettings(applicationId, false); GraphRequest.Companion companion = GraphRequest.Companion; StringCompanionObject stringCompanionObject = StringCompanionObject.INSTANCE; String format = String.format("%s/activities", Arrays.copyOf(new Object[]{applicationId}, 1)); Intrinsics.checkNotNullExpressionValue(format, "java.lang.String.format(format, *args)"); final GraphRequest newPostRequest = companion.newPostRequest(null, format, null, null); newPostRequest.setForceApplicationRequest(true); Bundle parameters = newPostRequest.getParameters(); if (parameters == null) { parameters = new Bundle(); } parameters.putString("access_token", accessTokenAppId.getAccessTokenString()); String pushNotificationsRegistrationId = InternalAppEventsLogger.Companion.getPushNotificationsRegistrationId(); if (pushNotificationsRegistrationId != null) { parameters.putString("device_token", pushNotificationsRegistrationId); } String installReferrer = AppEventsLoggerImpl.Companion.getInstallReferrer(); if (installReferrer != null) { parameters.putString("install_referrer", installReferrer); } newPostRequest.setParameters(parameters); int populateRequest = appEvents.populateRequest(newPostRequest, FacebookSdk.getApplicationContext(), queryAppSettings != null ? queryAppSettings.supportsImplicitLogging() : false, z); if (populateRequest == 0) { return null; } flushState.setNumEvents(flushState.getNumEvents() + populateRequest); newPostRequest.setCallback(new GraphRequest.Callback() { // from class: com.facebook.appevents.AppEventQueue$$ExternalSyntheticLambda1 @Override // com.facebook.GraphRequest.Callback public final void onCompleted(GraphResponse graphResponse) { AppEventQueue.m451buildRequestForSession$lambda4(AccessTokenAppIdPair.this, newPostRequest, appEvents, flushState, graphResponse); } }); return newPostRequest; } catch (Throwable th) { CrashShieldHandler.handleThrowable(th, AppEventQueue.class); return null; } } /* JADX INFO: Access modifiers changed from: private */ /* renamed from: buildRequestForSession$lambda-4, reason: not valid java name */ public static final void m451buildRequestForSession$lambda4(AccessTokenAppIdPair accessTokenAppId, GraphRequest postRequest, SessionEventsState appEvents, FlushStatistics flushState, GraphResponse response) { if (CrashShieldHandler.isObjectCrashing(AppEventQueue.class)) { return; } try { Intrinsics.checkNotNullParameter(accessTokenAppId, "$accessTokenAppId"); Intrinsics.checkNotNullParameter(postRequest, "$postRequest"); Intrinsics.checkNotNullParameter(appEvents, "$appEvents"); Intrinsics.checkNotNullParameter(flushState, "$flushState"); Intrinsics.checkNotNullParameter(response, "response"); handleResponse(accessTokenAppId, postRequest, response, appEvents, flushState); } catch (Throwable th) { CrashShieldHandler.handleThrowable(th, AppEventQueue.class); } } public static final void handleResponse(final AccessTokenAppIdPair accessTokenAppId, GraphRequest request, GraphResponse response, final SessionEventsState appEvents, FlushStatistics flushState) { String str; if (CrashShieldHandler.isObjectCrashing(AppEventQueue.class)) { return; } try { Intrinsics.checkNotNullParameter(accessTokenAppId, "accessTokenAppId"); Intrinsics.checkNotNullParameter(request, "request"); Intrinsics.checkNotNullParameter(response, "response"); Intrinsics.checkNotNullParameter(appEvents, "appEvents"); Intrinsics.checkNotNullParameter(flushState, "flushState"); FacebookRequestError error = response.getError(); String str2 = "Success"; FlushResult flushResult = FlushResult.SUCCESS; boolean z = true; if (error != null) { if (error.getErrorCode() == -1) { str2 = "Failed: No Connectivity"; flushResult = FlushResult.NO_CONNECTIVITY; } else { StringCompanionObject stringCompanionObject = StringCompanionObject.INSTANCE; str2 = String.format("Failed:\n Response: %s\n Error %s", Arrays.copyOf(new Object[]{response.toString(), error.toString()}, 2)); Intrinsics.checkNotNullExpressionValue(str2, "java.lang.String.format(format, *args)"); flushResult = FlushResult.SERVER_ERROR; } } FacebookSdk facebookSdk = FacebookSdk.INSTANCE; if (FacebookSdk.isLoggingBehaviorEnabled(LoggingBehavior.APP_EVENTS)) { try { str = new JSONArray((String) request.getTag()).toString(2); Intrinsics.checkNotNullExpressionValue(str, "{\n val jsonArray = JSONArray(eventsJsonString)\n jsonArray.toString(2)\n }"); } catch (JSONException unused) { str = ""; } Logger.Companion companion = Logger.Companion; LoggingBehavior loggingBehavior = LoggingBehavior.APP_EVENTS; String TAG2 = TAG; Intrinsics.checkNotNullExpressionValue(TAG2, "TAG"); companion.log(loggingBehavior, TAG2, "Flush completed\nParams: %s\n Result: %s\n Events JSON: %s", String.valueOf(request.getGraphObject()), str2, str); } if (error == null) { z = false; } appEvents.clearInFlightAndStats(z); FlushResult flushResult2 = FlushResult.NO_CONNECTIVITY; if (flushResult == flushResult2) { FacebookSdk.getExecutor().execute(new Runnable() { // from class: com.facebook.appevents.AppEventQueue$$ExternalSyntheticLambda0 @Override // java.lang.Runnable public final void run() { AppEventQueue.m454handleResponse$lambda5(AccessTokenAppIdPair.this, appEvents); } }); } if (flushResult == FlushResult.SUCCESS || flushState.getResult() == flushResult2) { return; } flushState.setResult(flushResult); } catch (Throwable th) { CrashShieldHandler.handleThrowable(th, AppEventQueue.class); } } /* JADX INFO: Access modifiers changed from: private */ /* renamed from: handleResponse$lambda-5, reason: not valid java name */ public static final void m454handleResponse$lambda5(AccessTokenAppIdPair accessTokenAppId, SessionEventsState appEvents) { if (CrashShieldHandler.isObjectCrashing(AppEventQueue.class)) { return; } try { Intrinsics.checkNotNullParameter(accessTokenAppId, "$accessTokenAppId"); Intrinsics.checkNotNullParameter(appEvents, "$appEvents"); AppEventStore.persistEvents(accessTokenAppId, appEvents); } catch (Throwable th) { CrashShieldHandler.handleThrowable(th, AppEventQueue.class); } } }