package com.singular.sdk.internal; import android.content.Context; import android.database.Cursor; import android.database.SQLException; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.provider.BaseColumns; import java.io.IOException; /* loaded from: classes4.dex */ public class SQLitePersistentQueue implements Queue { public static final SingularLog logger = SingularLog.getLogger(SQLitePersistentQueue.class.getSimpleName()); public SQLiteManager sqlite; public SQLitePersistentQueue(Context context) { this.sqlite = new SQLiteManager(new SQLiteHelper(context.getApplicationContext())); } @Override // com.singular.sdk.internal.Queue public synchronized void add(String str) { if (this.sqlite.insert(str) == -1) { throw new IOException("Failed to add element = " + str); } } @Override // com.singular.sdk.internal.Queue public synchronized String peek() { return this.sqlite.getHead(); } @Override // com.singular.sdk.internal.Queue public synchronized void remove() { this.sqlite.removeHead(); } public static class SQLiteManager { public final SQLiteHelper helper; public SQLiteManager(SQLiteHelper sQLiteHelper) { this.helper = sQLiteHelper; } /* JADX WARN: Removed duplicated region for block: B:18:0x0070 */ /* Code decompiled incorrectly, please refer to instructions dump. To view partially-correct add '--show-bad-code' argument */ public long insert(java.lang.String r9) { /* r8 = this; android.content.ContentValues r0 = new android.content.ContentValues r0.() java.lang.String r1 = "value" r0.put(r1, r9) r9 = 0 com.singular.sdk.internal.SQLitePersistentQueue$SQLiteHelper r1 = r8.helper // Catch: java.lang.Throwable -> L61 android.database.SQLException -> L65 android.database.sqlite.SQLiteDatabase r1 = r1.getWritableDatabase() // Catch: java.lang.Throwable -> L61 android.database.SQLException -> L65 java.lang.String r2 = "events" long r2 = r1.insert(r2, r9, r0) // Catch: java.lang.Throwable -> L59 android.database.SQLException -> L5b com.singular.sdk.internal.SingularLog r9 = com.singular.sdk.internal.SQLitePersistentQueue.access$000() // Catch: java.lang.Throwable -> L59 android.database.SQLException -> L5b java.lang.StringBuilder r0 = new java.lang.StringBuilder // Catch: java.lang.Throwable -> L59 android.database.SQLException -> L5b r0.() // Catch: java.lang.Throwable -> L59 android.database.SQLException -> L5b java.lang.String r4 = "insert() row = " r0.append(r4) // Catch: java.lang.Throwable -> L59 android.database.SQLException -> L5b r0.append(r2) // Catch: java.lang.Throwable -> L59 android.database.SQLException -> L5b java.lang.String r0 = r0.toString() // Catch: java.lang.Throwable -> L59 android.database.SQLException -> L5b r9.debug(r0) // Catch: java.lang.Throwable -> L59 android.database.SQLException -> L5b long r4 = r8.getCount(r1) // Catch: java.lang.Throwable -> L59 android.database.SQLException -> L5b r6 = 10000(0x2710, double:4.9407E-320) int r9 = (r4 > r6 ? 1 : (r4 == r6 ? 0 : -1)) if (r9 <= 0) goto L5d com.singular.sdk.internal.SingularLog r9 = com.singular.sdk.internal.SQLitePersistentQueue.access$000() // Catch: java.lang.Throwable -> L59 android.database.SQLException -> L5b java.lang.String r0 = "Pruning Queue; current size = %d; max size = %d" r6 = 2 java.lang.Object[] r6 = new java.lang.Object[r6] // Catch: java.lang.Throwable -> L59 android.database.SQLException -> L5b java.lang.Long r4 = java.lang.Long.valueOf(r4) // Catch: java.lang.Throwable -> L59 android.database.SQLException -> L5b r5 = 0 r6[r5] = r4 // Catch: java.lang.Throwable -> L59 android.database.SQLException -> L5b r4 = 10000(0x2710, float:1.4013E-41) java.lang.Integer r4 = java.lang.Integer.valueOf(r4) // Catch: java.lang.Throwable -> L59 android.database.SQLException -> L5b r5 = 1 r6[r5] = r4 // Catch: java.lang.Throwable -> L59 android.database.SQLException -> L5b r9.debug(r0, r6) // Catch: java.lang.Throwable -> L59 android.database.SQLException -> L5b r8.removeHead(r1) // Catch: java.lang.Throwable -> L59 android.database.SQLException -> L5b goto L5d L59: r9 = move-exception goto L6e L5b: r9 = move-exception goto L68 L5d: r1.close() return r2 L61: r0 = move-exception r1 = r9 r9 = r0 goto L6e L65: r0 = move-exception r1 = r9 r9 = r0 L68: java.io.IOException r0 = new java.io.IOException // Catch: java.lang.Throwable -> L59 r0.(r9) // Catch: java.lang.Throwable -> L59 throw r0 // Catch: java.lang.Throwable -> L59 L6e: if (r1 == 0) goto L73 r1.close() L73: throw r9 */ throw new UnsupportedOperationException("Method not decompiled: com.singular.sdk.internal.SQLitePersistentQueue.SQLiteManager.insert(java.lang.String):long"); } public String getHead() { Cursor cursor; Throwable th; SQLiteDatabase sQLiteDatabase; SQLException e; String str = null; try { try { sQLiteDatabase = this.helper.getReadableDatabase(); } catch (Throwable th2) { th = th2; } } catch (SQLException e2) { e = e2; } catch (Throwable th3) { cursor = null; th = th3; sQLiteDatabase = null; } try { long minId = getMinId(sQLiteDatabase); Cursor query = sQLiteDatabase.query("events", new String[]{"value"}, "_id = ?", new String[]{String.valueOf(minId)}, null, null, null); try { query.moveToFirst(); if (query.getCount() != 0) { str = query.getString(0); } SQLitePersistentQueue.logger.debug("getHead() _id = %d, value = %s", Long.valueOf(minId), str); query.close(); sQLiteDatabase.close(); return str; } catch (SQLException e3) { e = e3; throw new IOException(e); } } catch (SQLException e4) { e = e4; } catch (Throwable th4) { cursor = null; th = th4; if (cursor != null) { cursor.close(); } if (sQLiteDatabase != null) { sQLiteDatabase.close(); } throw th; } } public String removeHead() { SQLiteDatabase sQLiteDatabase = null; try { sQLiteDatabase = this.helper.getWritableDatabase(); return removeHead(sQLiteDatabase); } finally { if (sQLiteDatabase != null) { sQLiteDatabase.close(); } } } public final String removeHead(SQLiteDatabase sQLiteDatabase) { long minId; String[] strArr; Cursor query; Cursor cursor = null; try { try { minId = getMinId(sQLiteDatabase); strArr = new String[]{String.valueOf(minId)}; query = sQLiteDatabase.query("events", new String[]{"value"}, "_id = ?", strArr, null, null, null); } catch (SQLException e) { e = e; } } catch (Throwable th) { th = th; } try { query.moveToFirst(); if (query.getCount() != 0) { String string = query.getString(query.getColumnIndex("value")); sQLiteDatabase.delete("events", "_id = ?", strArr); SQLitePersistentQueue.logger.debug("removeHead() _id = %d", Long.valueOf(minId)); query.close(); return string; } query.close(); return null; } catch (SQLException e2) { e = e2; cursor = query; throw new IOException(e); } catch (Throwable th2) { th = th2; cursor = query; if (cursor != null) { cursor.close(); } throw th; } } public final long getMinId(SQLiteDatabase sQLiteDatabase) { Cursor cursor = null; try { try { cursor = sQLiteDatabase.rawQuery("SELECT MIN(_id) FROM events", null); cursor.moveToFirst(); long j = cursor.getLong(0); SQLitePersistentQueue.logger.debug("getMinId() id = %d", Long.valueOf(j)); cursor.close(); return j; } catch (SQLException e) { throw new IOException(e); } } catch (Throwable th) { if (cursor != null) { cursor.close(); } throw th; } } public final long getCount(SQLiteDatabase sQLiteDatabase) { Cursor cursor = null; try { try { cursor = sQLiteDatabase.rawQuery("SELECT COUNT(_id) FROM events", null); cursor.moveToFirst(); long j = cursor.getLong(0); SQLitePersistentQueue.logger.debug("getCount() = %d", Long.valueOf(j)); cursor.close(); return j; } catch (SQLException e) { throw new IOException(e); } } catch (Throwable th) { if (cursor != null) { cursor.close(); } throw th; } } } public static class SQLiteHelper extends SQLiteOpenHelper implements BaseColumns { @Override // android.database.sqlite.SQLiteOpenHelper public void onUpgrade(SQLiteDatabase sQLiteDatabase, int i, int i2) { } public SQLiteHelper(Context context) { super(context, "singular-1.db", (SQLiteDatabase.CursorFactory) null, 1); } @Override // android.database.sqlite.SQLiteOpenHelper public void onCreate(SQLiteDatabase sQLiteDatabase) { sQLiteDatabase.execSQL("CREATE TABLE events (_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,value TEXT )"); } } }