package com.facebook.appevents; import android.content.Context; import android.os.Bundle; import com.facebook.GraphRequest; import com.facebook.internal.AppEventsLoggerUtility; import com.facebook.internal.AttributionIdentifiers; import com.facebook.internal.Utility; import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.util.List; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; /* loaded from: classes.dex */ class SessionEventsState { private List a = new ArrayList(); private List b = new ArrayList(); private int c; private AttributionIdentifiers d; private String e; public SessionEventsState(AttributionIdentifiers attributionIdentifiers, String str) { this.d = attributionIdentifiers; this.e = str; } public synchronized void a(AppEvent appEvent) { if (this.a.size() + this.b.size() >= 1000) { this.c++; } else { this.a.add(appEvent); } } public synchronized List b() { List list; list = this.a; this.a = new ArrayList(); return list; } public synchronized int a() { return this.a.size(); } public synchronized void a(boolean z) { if (z) { this.a.addAll(this.b); } this.b.clear(); this.c = 0; } public int a(GraphRequest graphRequest, Context context, boolean z, boolean z2) { synchronized (this) { int i = this.c; this.b.addAll(this.a); this.a.clear(); JSONArray jSONArray = new JSONArray(); for (AppEvent appEvent : this.b) { if (appEvent.d()) { if (z || !appEvent.a()) { jSONArray.put(appEvent.b()); } } else { Utility.a("Event with invalid checksum: %s", appEvent.toString()); } } if (jSONArray.length() == 0) { return 0; } a(graphRequest, context, i, jSONArray, z2); return jSONArray.length(); } } private void a(GraphRequest graphRequest, Context context, int i, JSONArray jSONArray, boolean z) { JSONObject jSONObject; try { jSONObject = AppEventsLoggerUtility.a(AppEventsLoggerUtility.GraphAPIActivityType.CUSTOM_APP_EVENTS, this.d, this.e, z, context); if (this.c > 0) { jSONObject.put("num_skipped_events", i); } } catch (JSONException unused) { jSONObject = new JSONObject(); } graphRequest.a(jSONObject); Bundle h = graphRequest.h(); if (h == null) { h = new Bundle(); } String jSONArray2 = jSONArray.toString(); if (jSONArray2 != null) { h.putByteArray("custom_events_file", a(jSONArray2)); graphRequest.a((Object) jSONArray2); } graphRequest.a(h); } private byte[] a(String str) { try { return str.getBytes("UTF-8"); } catch (UnsupportedEncodingException e) { Utility.a("Encoding exception: ", (Exception) e); return null; } } }