package com.applovin.impl.sdk.network; import android.text.TextUtils; import com.applovin.impl.sdk.j; import com.applovin.impl.sdk.l; import com.applovin.impl.sdk.n; import com.applovin.impl.sj; import com.applovin.impl.yl; import java.io.ByteArrayInputStream; import java.io.File; import java.io.InputStream; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.concurrent.atomic.AtomicBoolean; import org.json.JSONArray; import org.json.JSONObject; /* loaded from: classes2.dex */ public class c extends yl { private final b h; private final n i; private AtomicBoolean j; private boolean k; public c(b bVar, j jVar) { super("PersistentPostbackQueueSaveTask", jVar); this.j = new AtomicBoolean(); this.k = false; this.h = bVar; this.i = jVar.J(); } public List a(int i) { ArrayList arrayList = new ArrayList(); l B = this.a.B(); if (!B.b("persistent_postback_cache.json", j.l())) { n.h("PersistentPostbackQueueSaveTask", "Postbacks queue file does not exist."); return arrayList; } File a = B.a("persistent_postback_cache.json", j.l()); String e = B.e(a); if (TextUtils.isEmpty(e)) { n.h("PersistentPostbackQueueSaveTask", "Postbacks queue file has no content."); return arrayList; } boolean z = false; try { JSONArray jSONArray = new JSONObject(e).getJSONArray("pb"); if (n.a()) { this.i.a("PersistentPostbackQueueSaveTask", "Deserializing " + jSONArray.length() + " postback(s)..."); } arrayList.ensureCapacity(Math.max(1, jSONArray.length())); Integer num = (Integer) this.a.a(sj.b3); for (int i2 = 0; i2 < jSONArray.length() && arrayList.size() < i; i2++) { try { d dVar = new d(jSONArray.getJSONObject(i2), this.a); if (dVar.c() < num.intValue()) { arrayList.add(dVar); } else { n.h("PersistentPostbackQueueSaveTask", "Skipping deserialization because maximum attempt count exceeded for postback: " + dVar); } } catch (Throwable th) { n.c("PersistentPostbackQueueSaveTask", "Unable to deserialize postback from json", th); this.a.E().a("PersistentPostbackQueueSaveTask", "deserializePostback", th); } } if (n.a()) { this.i.a("PersistentPostbackQueueSaveTask", "Successfully loaded postback queue with " + arrayList.size() + " postback(s)."); } } catch (Throwable th2) { try { n.c("PersistentPostbackQueueSaveTask", "Failed to load postback queue", th2); try { this.a.E().a("PersistentPostbackQueueSaveTask", "deserializePostbackQueue", th2); if (((Boolean) this.a.a(sj.g1)).booleanValue()) { B.a(a, "removeAfterDeserializationFail"); } } catch (Throwable th3) { th = th3; z = true; if (z && ((Boolean) this.a.a(sj.g1)).booleanValue()) { B.a(a, "removeAfterDeserializationFail"); } throw th; } } catch (Throwable th4) { th = th4; } } this.k = arrayList.isEmpty(); return arrayList; } @Override // java.lang.Runnable public void run() { a(this.h.d()); } private void a(List list) { if (!(this.k && list.isEmpty()) && this.j.compareAndSet(false, true)) { JSONArray jSONArray = new JSONArray(); Iterator it = list.iterator(); while (it.hasNext()) { d dVar = (d) it.next(); try { jSONArray.put(dVar.q()); } catch (Throwable th) { n.c("PersistentPostbackQueueSaveTask", "Unable to serialize postback to JSON: " + dVar, th); this.a.E().a("PersistentPostbackQueueSaveTask", "serializePostback", th); } } try { JSONObject jSONObject = new JSONObject(); jSONObject.put("pb", jSONArray); ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(jSONObject.toString().getBytes("UTF-8")); l B = this.a.B(); if (B.a((InputStream) byteArrayInputStream, B.a("persistent_postback_cache.json", j.l()), true)) { if (n.a()) { this.i.a("PersistentPostbackQueueSaveTask", "Exported postback queue to disk."); } } else if (n.a()) { this.i.b("PersistentPostbackQueueSaveTask", "Unable to export postback queue to disk."); } } catch (Throwable th2) { n.c("PersistentPostbackQueueSaveTask", "Unable to export postbacks to disk: " + jSONArray, th2); this.a.E().a("PersistentPostbackQueueSaveTask", "serializePostbackQueue", th2); } this.j.set(false); } } }