package o; import android.content.ContentValues; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabaseLockedException; import android.os.SystemClock; import android.util.Base64; import com.google.android.gms.measurement.api.AppMeasurementSdk; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.ListIterator; import java.util.Map; import java.util.Set; import o.AbstractC5394bsH; import o.C17797zni; import o.Vsa; import o.qmH; @fZH /* renamed from: o.zni, reason: case insensitive filesystem */ /* loaded from: classes.dex */ public final class C17797zni implements siG, qmH { static final C1332aAy e = new C1332aAy("proto"); final mAI a; final C1172XcZ b; private final InterfaceC6203cLt c; private final InterfaceC6203cLt d; /* JADX INFO: Access modifiers changed from: package-private */ /* renamed from: o.zni$LWm */ /* loaded from: classes.dex */ public interface LWm { T e(); } /* JADX INFO: Access modifiers changed from: package-private */ /* renamed from: o.zni$RVV */ /* loaded from: classes.dex */ public interface RVV { U b(T t); } /* JADX INFO: Access modifiers changed from: package-private */ @InterfaceC13391fZD public C17797zni(InterfaceC6203cLt interfaceC6203cLt, InterfaceC6203cLt interfaceC6203cLt2, mAI mai, C1172XcZ c1172XcZ) { this.b = c1172XcZ; this.c = interfaceC6203cLt; this.d = interfaceC6203cLt2; this.a = mai; } @Override // o.siG public final FFX b(final AbstractC3038apv abstractC3038apv, final AbstractC5394bsH abstractC5394bsH) { new Object[]{abstractC3038apv.c(), abstractC5394bsH.b(), abstractC3038apv.a()}; long longValue = ((Long) d(new RVV(this, abstractC3038apv, abstractC5394bsH) { // from class: o.AYD private final AbstractC5394bsH a; private final C17797zni c; private final AbstractC3038apv e; @Override // o.C17797zni.RVV public final Object b(Object obj) { long insert; C17797zni c17797zni = this.c; AbstractC3038apv abstractC3038apv2 = this.e; AbstractC5394bsH abstractC5394bsH2 = this.a; SQLiteDatabase sQLiteDatabase = (SQLiteDatabase) obj; if (((SQLiteDatabase) c17797zni.e(new C11513egk(c17797zni.b), zoN.e)).compileStatement("PRAGMA page_count").simpleQueryForLong() * ((SQLiteDatabase) c17797zni.e(new C11513egk(c17797zni.b), zoN.e)).compileStatement("PRAGMA page_size").simpleQueryForLong() >= c17797zni.a.e()) { return -1L; } Long Hl_ = C17797zni.Hl_(sQLiteDatabase, abstractC3038apv2); if (Hl_ != null) { insert = Hl_.longValue(); } else { ContentValues contentValues = new ContentValues(); contentValues.put("backend_name", abstractC3038apv2.a()); contentValues.put("priority", Integer.valueOf(C0647Lgu.e(abstractC3038apv2.c()))); contentValues.put("next_request_ms", (Integer) 0); if (abstractC3038apv2.d() != null) { contentValues.put("extras", Base64.encodeToString(abstractC3038apv2.d(), 0)); } insert = sQLiteDatabase.insert("transport_contexts", null, contentValues); } int b = c17797zni.a.b(); byte[] bArr = abstractC5394bsH2.c().a; boolean z = bArr.length <= b; ContentValues contentValues2 = new ContentValues(); contentValues2.put("context_id", Long.valueOf(insert)); contentValues2.put("transport_name", abstractC5394bsH2.b()); contentValues2.put("timestamp_ms", Long.valueOf(abstractC5394bsH2.d())); contentValues2.put("uptime_ms", Long.valueOf(abstractC5394bsH2.j())); contentValues2.put("payload_encoding", abstractC5394bsH2.c().c.e); contentValues2.put("code", abstractC5394bsH2.e()); contentValues2.put("num_attempts", (Integer) 0); contentValues2.put("inline", Boolean.valueOf(z)); contentValues2.put("payload", z ? bArr : new byte[0]); long insert2 = sQLiteDatabase.insert("events", null, contentValues2); if (!z) { int ceil = (int) Math.ceil(bArr.length / b); for (int i = 1; i <= ceil; i++) { byte[] copyOfRange = Arrays.copyOfRange(bArr, (i - 1) * b, Math.min(i * b, bArr.length)); ContentValues contentValues3 = new ContentValues(); contentValues3.put("event_id", Long.valueOf(insert2)); contentValues3.put("sequence_num", Integer.valueOf(i)); contentValues3.put("bytes", copyOfRange); sQLiteDatabase.insert("event_payloads", null, contentValues3); } } for (Map.Entry entry : abstractC5394bsH2.h().entrySet()) { ContentValues contentValues4 = new ContentValues(); contentValues4.put("event_id", Long.valueOf(insert2)); contentValues4.put("name", entry.getKey()); contentValues4.put(AppMeasurementSdk.ConditionalUserProperty.VALUE, entry.getValue()); sQLiteDatabase.insert("event_metadata", null, contentValues4); } return Long.valueOf(insert2); } /* JADX INFO: Access modifiers changed from: package-private */ { this.c = this; this.e = abstractC3038apv; this.a = abstractC5394bsH; } })).longValue(); if (longValue < 1) { return null; } return new BSu(longValue, abstractC3038apv, abstractC5394bsH); } /* JADX INFO: Access modifiers changed from: package-private */ public static Long Hl_(SQLiteDatabase sQLiteDatabase, AbstractC3038apv abstractC3038apv) { StringBuilder sb = new StringBuilder("backend_name = ? and priority = ?"); ArrayList arrayList = new ArrayList(Arrays.asList(abstractC3038apv.a(), String.valueOf(C0647Lgu.e(abstractC3038apv.c())))); if (abstractC3038apv.d() != null) { sb.append(" and extras = ?"); arrayList.add(Base64.encodeToString(abstractC3038apv.d(), 0)); } return (Long) Hm_(sQLiteDatabase.query("transport_contexts", new String[]{"_id"}, sb.toString(), (String[]) arrayList.toArray(new String[0]), null, null, null), Ece.b); } @Override // o.siG public final void c(Iterable iterable) { if (iterable.iterator().hasNext()) { StringBuilder sb = new StringBuilder("UPDATE events SET num_attempts = num_attempts + 1 WHERE _id in "); sb.append(b(iterable)); final String obj = sb.toString(); d(new RVV(obj) { // from class: o.pzJ private final String d; @Override // o.C17797zni.RVV public final Object b(Object obj2) { SQLiteDatabase sQLiteDatabase = (SQLiteDatabase) obj2; sQLiteDatabase.compileStatement(this.d).execute(); sQLiteDatabase.compileStatement("DELETE FROM events WHERE num_attempts >= 10").execute(); return null; } /* JADX INFO: Access modifiers changed from: package-private */ { this.d = obj; } }); } } @Override // o.siG public final void a(Iterable iterable) { if (iterable.iterator().hasNext()) { StringBuilder sb = new StringBuilder("DELETE FROM events WHERE _id in "); sb.append(b(iterable)); ((SQLiteDatabase) e(new C11513egk(this.b), zoN.e)).compileStatement(sb.toString()).execute(); } } private static String b(Iterable iterable) { StringBuilder sb = new StringBuilder("("); Iterator it = iterable.iterator(); while (it.hasNext()) { sb.append(it.next().a()); if (it.hasNext()) { sb.append(','); } } sb.append(')'); return sb.toString(); } @Override // o.siG public final long d(AbstractC3038apv abstractC3038apv) { return ((Long) Hm_(((SQLiteDatabase) e(new C11513egk(this.b), zoN.e)).rawQuery("SELECT next_request_ms FROM transport_contexts WHERE backend_name = ? and priority = ?", new String[]{abstractC3038apv.a(), String.valueOf(C0647Lgu.e(abstractC3038apv.c()))}), nwA.c)).longValue(); } @Override // o.siG public final int d() { final long a = this.c.a() - this.a.d(); return ((Integer) d(new RVV(a) { // from class: o.oNN private final long a; @Override // o.C17797zni.RVV public final Object b(Object obj) { return Integer.valueOf(((SQLiteDatabase) obj).delete("events", "timestamp_ms < ?", new String[]{String.valueOf(this.a)})); } /* JADX INFO: Access modifiers changed from: package-private */ { this.a = a; } })).intValue(); } @Override // java.io.Closeable, java.lang.AutoCloseable public final void close() { this.b.close(); } /* JADX INFO: Access modifiers changed from: package-private */ public final T e(LWm lWm, RVV rvv) { long a = this.d.a(); while (true) { try { return lWm.e(); } catch (SQLiteDatabaseLockedException e2) { if (this.d.a() >= this.a.a() + a) { return rvv.b(e2); } SystemClock.sleep(50L); } } } @Override // o.qmH public final T d(qmH.LWm lWm) { final SQLiteDatabase sQLiteDatabase = (SQLiteDatabase) e(new C11513egk(this.b), zoN.e); e(new LWm(sQLiteDatabase) { // from class: o.RQQ private final SQLiteDatabase a; @Override // o.C17797zni.LWm public final Object e() { this.a.beginTransaction(); return null; } /* JADX INFO: Access modifiers changed from: package-private */ { this.a = sQLiteDatabase; } }, nIL.d); try { T a = lWm.a(); sQLiteDatabase.setTransactionSuccessful(); return a; } finally { sQLiteDatabase.endTransaction(); } } private T d(RVV rvv) { SQLiteDatabase sQLiteDatabase = (SQLiteDatabase) e(new C11513egk(this.b), zoN.e); sQLiteDatabase.beginTransaction(); try { T b = rvv.b(sQLiteDatabase); sQLiteDatabase.setTransactionSuccessful(); return b; } finally { sQLiteDatabase.endTransaction(); } } /* renamed from: o.zni$IeS */ /* loaded from: classes.dex */ static class IeS { final String a; final String d; /* JADX INFO: Access modifiers changed from: package-private */ public /* synthetic */ IeS(String str, String str2, byte b) { this(str, str2); } private IeS(String str, String str2) { this.a = str; this.d = str2; } } /* JADX INFO: Access modifiers changed from: package-private */ public static T Hm_(Cursor cursor, RVV rvv) { try { return rvv.b(cursor); } finally { cursor.close(); } } @Override // o.siG public final boolean c(final AbstractC3038apv abstractC3038apv) { return ((Boolean) d(new RVV(this, abstractC3038apv) { // from class: o.WME private final AbstractC3038apv d; private final C17797zni e; @Override // o.C17797zni.RVV public final Object b(Object obj) { C17797zni c17797zni = this.e; Long Hl_ = C17797zni.Hl_((SQLiteDatabase) obj, this.d); if (Hl_ == null) { return Boolean.FALSE; } return (Boolean) C17797zni.Hm_(((SQLiteDatabase) c17797zni.e(new C11513egk(c17797zni.b), zoN.e)).rawQuery("SELECT 1 FROM events WHERE context_id = ? LIMIT 1", new String[]{Hl_.toString()}), dHN.e); } /* JADX INFO: Access modifiers changed from: package-private */ { this.e = this; this.d = abstractC3038apv; } })).booleanValue(); } @Override // o.siG public final Iterable b() { return (Iterable) d(nDF.b); } @Override // o.siG public final Iterable b(final AbstractC3038apv abstractC3038apv) { return (Iterable) d(new RVV(this, abstractC3038apv) { // from class: o.LDE private final C17797zni a; private final AbstractC3038apv d; @Override // o.C17797zni.RVV public final Object b(Object obj) { final C17797zni c17797zni = this.a; final AbstractC3038apv abstractC3038apv2 = this.d; SQLiteDatabase sQLiteDatabase = (SQLiteDatabase) obj; final ArrayList arrayList = new ArrayList(); Long Hl_ = C17797zni.Hl_(sQLiteDatabase, abstractC3038apv2); if (Hl_ != null) { C17797zni.Hm_(sQLiteDatabase.query("events", new String[]{"_id", "transport_name", "timestamp_ms", "uptime_ms", "payload_encoding", "payload", "code", "inline"}, "context_id = ?", new String[]{Hl_.toString()}, null, null, null, String.valueOf(c17797zni.a.c())), new C17797zni.RVV(c17797zni, arrayList, abstractC3038apv2) { // from class: o.Piu private final List b; private final C17797zni c; private final AbstractC3038apv e; @Override // o.C17797zni.RVV public final Object b(Object obj2) { C17797zni c17797zni2 = this.c; List list = this.b; AbstractC3038apv abstractC3038apv3 = this.e; Cursor cursor = (Cursor) obj2; while (cursor.moveToNext()) { long j = cursor.getLong(0); boolean z = cursor.getInt(7) != 0; Vsa.Sts sts = new Vsa.Sts(); sts.c = new HashMap(); AbstractC5394bsH.RVV d = sts.e(cursor.getString(1)).e(cursor.getLong(2)).d(cursor.getLong(3)); if (z) { String string = cursor.getString(4); d.c(new sNO(string == null ? C17797zni.e : new C1332aAy(string), cursor.getBlob(5))); } else { String string2 = cursor.getString(4); d.c(new sNO(string2 == null ? C17797zni.e : new C1332aAy(string2), (byte[]) C17797zni.Hm_(((SQLiteDatabase) c17797zni2.e(new C11513egk(c17797zni2.b), zoN.e)).query("event_payloads", new String[]{"bytes"}, "event_id = ?", new String[]{String.valueOf(j)}, null, null, "sequence_num"), Lbs.b))); } if (!cursor.isNull(6)) { d.a(Integer.valueOf(cursor.getInt(6))); } list.add(new BSu(j, abstractC3038apv3, d.b())); } return null; } /* JADX INFO: Access modifiers changed from: package-private */ { this.c = c17797zni; this.b = arrayList; this.e = abstractC3038apv2; } }); } final HashMap hashMap = new HashMap(); StringBuilder sb = new StringBuilder("event_id IN ("); for (int i = 0; i < arrayList.size(); i++) { sb.append(((FFX) arrayList.get(i)).a()); if (i < arrayList.size() - 1) { sb.append(','); } } sb.append(')'); C17797zni.Hm_(sQLiteDatabase.query("event_metadata", new String[]{"event_id", "name", AppMeasurementSdk.ConditionalUserProperty.VALUE}, sb.toString(), null, null, null, null), new C17797zni.RVV(hashMap) { // from class: o.obs private final Map e; @Override // o.C17797zni.RVV public final Object b(Object obj2) { Map map = this.e; Cursor cursor = (Cursor) obj2; while (cursor.moveToNext()) { byte b = 0; long j = cursor.getLong(0); Set set = (Set) map.get(Long.valueOf(j)); if (set == null) { set = new HashSet(); map.put(Long.valueOf(j), set); } set.add(new C17797zni.IeS(cursor.getString(1), cursor.getString(2), b)); } return null; } /* JADX INFO: Access modifiers changed from: package-private */ { this.e = hashMap; } }); ListIterator listIterator = arrayList.listIterator(); while (listIterator.hasNext()) { FFX ffx = (FFX) listIterator.next(); if (hashMap.containsKey(Long.valueOf(ffx.a()))) { AbstractC5394bsH.RVV i2 = ffx.d().i(); for (C17797zni.IeS ieS : (Set) hashMap.get(Long.valueOf(ffx.a()))) { i2.a(ieS.a, ieS.d); } listIterator.set(new BSu(ffx.a(), ffx.e(), i2.b())); } } return arrayList; } /* JADX INFO: Access modifiers changed from: package-private */ { this.a = this; this.d = abstractC3038apv; } }); } @Override // o.siG public final void c(final AbstractC3038apv abstractC3038apv, final long j) { d(new RVV(j, abstractC3038apv) { // from class: o.wgR private final long c; private final AbstractC3038apv e; @Override // o.C17797zni.RVV public final Object b(Object obj) { long j2 = this.c; AbstractC3038apv abstractC3038apv2 = this.e; SQLiteDatabase sQLiteDatabase = (SQLiteDatabase) obj; ContentValues contentValues = new ContentValues(); contentValues.put("next_request_ms", Long.valueOf(j2)); if (sQLiteDatabase.update("transport_contexts", contentValues, "backend_name = ? and priority = ?", new String[]{abstractC3038apv2.a(), String.valueOf(C0647Lgu.e(abstractC3038apv2.c()))}) <= 0) { contentValues.put("backend_name", abstractC3038apv2.a()); contentValues.put("priority", Integer.valueOf(C0647Lgu.e(abstractC3038apv2.c()))); sQLiteDatabase.insert("transport_contexts", null, contentValues); } return null; } /* JADX INFO: Access modifiers changed from: package-private */ { this.c = j; this.e = abstractC3038apv; } }); } }