package com.google.firebase.crashlytics.internal.analytics; import android.os.Bundle; import com.google.firebase.analytics.connector.AnalyticsConnector; import com.google.firebase.crashlytics.internal.Logger; import com.google.firebase.crashlytics.internal.analytics.AnalyticsReceiver; import org.json.JSONException; import org.json.JSONObject; /* loaded from: classes2.dex */ public class AnalyticsConnectorReceiver implements AnalyticsConnector.AnalyticsConnectorListener, AnalyticsReceiver { public static final String APP_EXCEPTION_EVENT_NAME = "_ae"; private static final String BREADCRUMB_PARAMS_KEY = "parameters"; private static final String BREADCRUMB_PREFIX = "$A$:"; static final String CRASHLYTICS_ORIGIN = "clx"; public static final String EVENT_NAME_KEY = "name"; private static final String EVENT_ORIGIN_KEY = "_o"; private static final String EVENT_PARAMS_KEY = "params"; static final String LEGACY_CRASH_ORIGIN = "crash"; private final AnalyticsConnector analyticsConnector; private AnalyticsConnector.AnalyticsConnectorHandle analyticsConnectorHandle; private final BreadcrumbHandler breadcrumbHandler; private AnalyticsReceiver.CrashlyticsOriginEventListener crashOriginEventListener; /* loaded from: classes2.dex */ public interface BreadcrumbHandler { void dropBreadcrumb(String str); } public AnalyticsConnectorReceiver(AnalyticsConnector analyticsConnector, BreadcrumbHandler breadcrumbHandler) { this.analyticsConnector = analyticsConnector; this.breadcrumbHandler = breadcrumbHandler; } @Override // com.google.firebase.crashlytics.internal.analytics.AnalyticsReceiver public boolean register() { AnalyticsConnector analyticsConnector = this.analyticsConnector; if (analyticsConnector == null) { Logger.getLogger().d("Firebase Analytics is not present; you will not see automatic logging of events before a crash occurs."); return false; } AnalyticsConnector.AnalyticsConnectorHandle registerAnalyticsConnectorListener = analyticsConnector.registerAnalyticsConnectorListener(CRASHLYTICS_ORIGIN, this); this.analyticsConnectorHandle = registerAnalyticsConnectorListener; if (registerAnalyticsConnectorListener == null) { Logger.getLogger().d("Could not register AnalyticsConnectorListener with Crashlytics origin."); AnalyticsConnector.AnalyticsConnectorHandle registerAnalyticsConnectorListener2 = this.analyticsConnector.registerAnalyticsConnectorListener("crash", this); this.analyticsConnectorHandle = registerAnalyticsConnectorListener2; if (registerAnalyticsConnectorListener2 != null) { Logger.getLogger().w("A new version of the Google Analytics for Firebase SDK is now available. For improved performance and compatibility with Crashlytics, please update to the latest version."); } } return this.analyticsConnectorHandle != null; } @Override // com.google.firebase.crashlytics.internal.analytics.AnalyticsReceiver public void unregister() { AnalyticsConnector.AnalyticsConnectorHandle analyticsConnectorHandle = this.analyticsConnectorHandle; if (analyticsConnectorHandle != null) { analyticsConnectorHandle.unregister(); } } @Override // com.google.firebase.analytics.connector.AnalyticsConnector.AnalyticsConnectorListener public void onMessageTriggered(int i, Bundle bundle) { Logger logger = Logger.getLogger(); StringBuilder sb = new StringBuilder("AnalyticsConnectorReceiver received message: "); sb.append(i); sb.append(" "); sb.append(bundle); logger.d(sb.toString()); if (bundle == null) { return; } Bundle bundle2 = bundle.getBundle(EVENT_PARAMS_KEY); if (bundle2 == null) { bundle2 = new Bundle(); } if (CRASHLYTICS_ORIGIN.equals(bundle2.getString(EVENT_ORIGIN_KEY))) { dispatchCrashlyticsOriginEvent(i, bundle); return; } String string = bundle.getString("name"); if (string != null) { dispatchBreadcrumbEvent(string, bundle2); } } private void dispatchCrashlyticsOriginEvent(int i, Bundle bundle) { AnalyticsReceiver.CrashlyticsOriginEventListener crashlyticsOriginEventListener = this.crashOriginEventListener; if (crashlyticsOriginEventListener != null) { crashlyticsOriginEventListener.onCrashlyticsOriginEvent(i, bundle); } } private void dispatchBreadcrumbEvent(String str, Bundle bundle) { try { StringBuilder sb = new StringBuilder(BREADCRUMB_PREFIX); sb.append(serializeEvent(str, bundle)); this.breadcrumbHandler.dropBreadcrumb(sb.toString()); } catch (JSONException unused) { Logger.getLogger().w("Unable to serialize Firebase Analytics event."); } } private static String serializeEvent(String str, Bundle bundle) throws JSONException { JSONObject jSONObject = new JSONObject(); JSONObject jSONObject2 = new JSONObject(); for (String str2 : bundle.keySet()) { jSONObject2.put(str2, bundle.get(str2)); } jSONObject.put("name", str); jSONObject.put(BREADCRUMB_PARAMS_KEY, jSONObject2); return jSONObject.toString(); } @Override // com.google.firebase.crashlytics.internal.analytics.AnalyticsReceiver public void setCrashlyticsOriginEventListener(AnalyticsReceiver.CrashlyticsOriginEventListener crashlyticsOriginEventListener) { this.crashOriginEventListener = crashlyticsOriginEventListener; } @Override // com.google.firebase.crashlytics.internal.analytics.AnalyticsReceiver public AnalyticsReceiver.CrashlyticsOriginEventListener getCrashlyticsOriginEventListener() { return this.crashOriginEventListener; } }