package com.ironsource;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.SystemClock;
import android.provider.BaseColumns;
/* loaded from: classes4.dex */
public class t9 extends SQLiteOpenHelper implements ue {
    private static t9 f = null;
    private static final String g = " TEXT";
    private static final String h = " INTEGER";
    private static final String i = ",";

    /* renamed from: a  reason: collision with root package name */
    private final u9 f3095a;
    private final int b;
    private final int c;
    private final String d;
    private final String e;

    /* loaded from: classes4.dex */
    static abstract class a implements BaseColumns {

        /* renamed from: a  reason: collision with root package name */
        public static final String f3096a = "events";
        public static final int b = 4;
        public static final String c = "eventid";
        public static final String d = "timestamp";
        public static final String e = "type";
        public static final String f = "data";

        a() {
        }
    }

    public t9(Context context, String str, int i2) {
        super(context, str, (SQLiteDatabase.CursorFactory) null, i2);
        this.f3095a = new u9();
        this.b = 4;
        this.c = 400;
        this.d = "DROP TABLE IF EXISTS events";
        this.e = "CREATE TABLE events (_id INTEGER PRIMARY KEY,eventid INTEGER,timestamp INTEGER,type TEXT,data TEXT )";
    }

    private ContentValues a(ob obVar, String str) {
        if (obVar != null) {
            ContentValues contentValues = new ContentValues(4);
            contentValues.put("eventid", Integer.valueOf(obVar.c()));
            contentValues.put("timestamp", Long.valueOf(obVar.d()));
            contentValues.put("type", str);
            contentValues.put("data", obVar.a());
            return contentValues;
        }
        return null;
    }

    private synchronized SQLiteDatabase a(boolean z) throws Throwable {
        int i2 = 0;
        while (true) {
            try {
                if (z) {
                    return this.f3095a.a(true, this);
                }
                return this.f3095a.a(false, this);
            } catch (Throwable th) {
                l9.d().a(th);
                i2++;
                if (i2 >= 4) {
                    throw th;
                }
                SystemClock.sleep(i2 * 400);
            }
        }
    }

    public static synchronized t9 a(Context context, String str, int i2) {
        t9 t9Var;
        synchronized (t9.class) {
            if (f == null) {
                f = new t9(context, str, i2);
            }
            t9Var = f;
        }
        return t9Var;
    }

    /* JADX WARN: Code restructure failed: missing block: B:26:0x0070, code lost:
        if (r1.isOpen() != false) goto L46;
     */
    /* JADX WARN: Code restructure failed: missing block: B:39:0x0096, code lost:
        if (r1.isOpen() != false) goto L46;
     */
    /* JADX WARN: Code restructure failed: missing block: B:40:0x0098, code lost:
        r1.close();
     */
    @Override // com.ironsource.ue
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct add '--show-bad-code' argument
    */
    public synchronized java.util.ArrayList<com.ironsource.ob> a(java.lang.String r12) {
        /*
            r11 = this;
            monitor-enter(r11)
            java.util.ArrayList r0 = new java.util.ArrayList     // Catch: java.lang.Throwable -> Lb5
            r0.<init>()     // Catch: java.lang.Throwable -> Lb5
            r1 = 0
            r2 = 0
            android.database.sqlite.SQLiteDatabase r1 = r11.a(r1)     // Catch: java.lang.Throwable -> L75
            java.lang.String r6 = "type = ?"
            java.lang.String[] r7 = new java.lang.String[]{r12}     // Catch: java.lang.Throwable -> L73
            java.lang.String r10 = "timestamp ASC"
            java.lang.String r4 = "events"
            r5 = 0
            r8 = 0
            r9 = 0
            r3 = r1
            android.database.Cursor r2 = r3.query(r4, r5, r6, r7, r8, r9, r10)     // Catch: java.lang.Throwable -> L73
            int r12 = r2.getCount()     // Catch: java.lang.Throwable -> L73
            if (r12 <= 0) goto L63
            r2.moveToFirst()     // Catch: java.lang.Throwable -> L73
        L27:
            boolean r12 = r2.isAfterLast()     // Catch: java.lang.Throwable -> L73
            if (r12 != 0) goto L60
            java.lang.String r12 = "eventid"
            int r12 = r2.getColumnIndex(r12)     // Catch: java.lang.Throwable -> L73
            if (r12 >= 0) goto L36
            goto L27
        L36:
            int r12 = r2.getInt(r12)     // Catch: java.lang.Throwable -> L73
            java.lang.String r3 = "timestamp"
            int r3 = r2.getColumnIndex(r3)     // Catch: java.lang.Throwable -> L73
            if (r3 >= 0) goto L43
            goto L27
        L43:
            long r3 = r2.getLong(r3)     // Catch: java.lang.Throwable -> L73
            java.lang.String r5 = "data"
            int r5 = r2.getColumnIndex(r5)     // Catch: java.lang.Throwable -> L73
            if (r5 >= 0) goto L50
            goto L27
        L50:
            java.lang.String r5 = r2.getString(r5)     // Catch: java.lang.Throwable -> L73
            com.ironsource.ob r6 = new com.ironsource.ob     // Catch: java.lang.Throwable -> L73
            r6.<init>(r12, r3, r5)     // Catch: java.lang.Throwable -> L73
            r0.add(r6)     // Catch: java.lang.Throwable -> L73
            r2.moveToNext()     // Catch: java.lang.Throwable -> L73
            goto L27
        L60:
            r2.close()     // Catch: java.lang.Throwable -> L73
        L63:
            boolean r12 = r2.isClosed()     // Catch: java.lang.Throwable -> Lb5
            if (r12 != 0) goto L6c
            r2.close()     // Catch: java.lang.Throwable -> Lb5
        L6c:
            boolean r12 = r1.isOpen()     // Catch: java.lang.Throwable -> Lb5
            if (r12 == 0) goto L9b
            goto L98
        L73:
            r12 = move-exception
            goto L77
        L75:
            r12 = move-exception
            r1 = r2
        L77:
            com.ironsource.l9 r3 = com.ironsource.l9.d()     // Catch: java.lang.Throwable -> L9d
            r3.a(r12)     // Catch: java.lang.Throwable -> L9d
            java.lang.String r3 = "IronSource"
            java.lang.String r4 = "Exception while loading events: "
            android.util.Log.e(r3, r4, r12)     // Catch: java.lang.Throwable -> L9d
            if (r2 == 0) goto L90
            boolean r12 = r2.isClosed()     // Catch: java.lang.Throwable -> Lb5
            if (r12 != 0) goto L90
            r2.close()     // Catch: java.lang.Throwable -> Lb5
        L90:
            if (r1 == 0) goto L9b
            boolean r12 = r1.isOpen()     // Catch: java.lang.Throwable -> Lb5
            if (r12 == 0) goto L9b
        L98:
            r1.close()     // Catch: java.lang.Throwable -> Lb5
        L9b:
            monitor-exit(r11)
            return r0
        L9d:
            r12 = move-exception
            if (r2 == 0) goto La9
            boolean r0 = r2.isClosed()     // Catch: java.lang.Throwable -> Lb5
            if (r0 != 0) goto La9
            r2.close()     // Catch: java.lang.Throwable -> Lb5
        La9:
            if (r1 == 0) goto Lb4
            boolean r0 = r1.isOpen()     // Catch: java.lang.Throwable -> Lb5
            if (r0 == 0) goto Lb4
            r1.close()     // Catch: java.lang.Throwable -> Lb5
        Lb4:
            throw r12     // Catch: java.lang.Throwable -> Lb5
        Lb5:
            r12 = move-exception
            monitor-exit(r11)
            throw r12
        */
        throw new UnsupportedOperationException("Method not decompiled: com.ironsource.t9.a(java.lang.String):java.util.ArrayList");
    }

    /* JADX WARN: Code restructure failed: missing block: B:19:0x0034, code lost:
        if (r0.isOpen() != false) goto L30;
     */
    @Override // com.ironsource.ue
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct add '--show-bad-code' argument
    */
    public synchronized void a(java.util.List<com.ironsource.ob> r5, java.lang.String r6) {
        /*
            r4 = this;
            monitor-enter(r4)
            if (r5 == 0) goto L67
            boolean r0 = r5.isEmpty()     // Catch: java.lang.Throwable -> L64
            if (r0 == 0) goto La
            goto L67
        La:
            r0 = 1
            r1 = 0
            android.database.sqlite.SQLiteDatabase r0 = r4.a(r0)     // Catch: java.lang.Throwable -> L3a
            java.util.Iterator r5 = r5.iterator()     // Catch: java.lang.Throwable -> L37
        L14:
            boolean r2 = r5.hasNext()     // Catch: java.lang.Throwable -> L37
            if (r2 == 0) goto L2e
            java.lang.Object r2 = r5.next()     // Catch: java.lang.Throwable -> L37
            com.ironsource.ob r2 = (com.ironsource.ob) r2     // Catch: java.lang.Throwable -> L37
            android.content.ContentValues r2 = r4.a(r2, r6)     // Catch: java.lang.Throwable -> L37
            if (r0 == 0) goto L14
            if (r2 == 0) goto L14
            java.lang.String r3 = "events"
            r0.insert(r3, r1, r2)     // Catch: java.lang.Throwable -> L37
            goto L14
        L2e:
            if (r0 == 0) goto L55
            boolean r5 = r0.isOpen()     // Catch: java.lang.Throwable -> L64
            if (r5 == 0) goto L55
            goto L52
        L37:
            r5 = move-exception
            r1 = r0
            goto L3b
        L3a:
            r5 = move-exception
        L3b:
            com.ironsource.l9 r6 = com.ironsource.l9.d()     // Catch: java.lang.Throwable -> L57
            r6.a(r5)     // Catch: java.lang.Throwable -> L57
            java.lang.String r6 = "IronSource"
            java.lang.String r0 = "Exception while saving events: "
            android.util.Log.e(r6, r0, r5)     // Catch: java.lang.Throwable -> L57
            if (r1 == 0) goto L55
            boolean r5 = r1.isOpen()     // Catch: java.lang.Throwable -> L64
            if (r5 == 0) goto L55
            r0 = r1
        L52:
            r0.close()     // Catch: java.lang.Throwable -> L64
        L55:
            monitor-exit(r4)
            return
        L57:
            r5 = move-exception
            if (r1 == 0) goto L63
            boolean r6 = r1.isOpen()     // Catch: java.lang.Throwable -> L64
            if (r6 == 0) goto L63
            r1.close()     // Catch: java.lang.Throwable -> L64
        L63:
            throw r5     // Catch: java.lang.Throwable -> L64
        L64:
            r5 = move-exception
            monitor-exit(r4)
            throw r5
        L67:
            monitor-exit(r4)
            return
        */
        throw new UnsupportedOperationException("Method not decompiled: com.ironsource.t9.a(java.util.List, java.lang.String):void");
    }

    /* JADX WARN: Code restructure failed: missing block: B:17:0x0030, code lost:
        if (r1.isOpen() != false) goto L14;
     */
    /* JADX WARN: Code restructure failed: missing block: B:18:0x0032, code lost:
        r1.close();
     */
    /* JADX WARN: Code restructure failed: missing block: B:8:0x0015, code lost:
        if (r1.isOpen() != false) goto L14;
     */
    @Override // com.ironsource.ue
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct add '--show-bad-code' argument
    */
    public synchronized void b(java.lang.String r4) {
        /*
            r3 = this;
            monitor-enter(r3)
            java.lang.String r0 = "type = ?"
            java.lang.String[] r4 = new java.lang.String[]{r4}     // Catch: java.lang.Throwable -> L44
            r1 = 1
            android.database.sqlite.SQLiteDatabase r1 = r3.a(r1)     // Catch: java.lang.Throwable -> L1a
            java.lang.String r2 = "events"
            r1.delete(r2, r0, r4)     // Catch: java.lang.Throwable -> L18
            boolean r4 = r1.isOpen()     // Catch: java.lang.Throwable -> L44
            if (r4 == 0) goto L35
            goto L32
        L18:
            r4 = move-exception
            goto L1c
        L1a:
            r4 = move-exception
            r1 = 0
        L1c:
            com.ironsource.l9 r0 = com.ironsource.l9.d()     // Catch: java.lang.Throwable -> L37
            r0.a(r4)     // Catch: java.lang.Throwable -> L37
            java.lang.String r0 = "IronSource"
            java.lang.String r2 = "Exception while clearing events: "
            android.util.Log.e(r0, r2, r4)     // Catch: java.lang.Throwable -> L37
            if (r1 == 0) goto L35
            boolean r4 = r1.isOpen()     // Catch: java.lang.Throwable -> L44
            if (r4 == 0) goto L35
        L32:
            r1.close()     // Catch: java.lang.Throwable -> L44
        L35:
            monitor-exit(r3)
            return
        L37:
            r4 = move-exception
            if (r1 == 0) goto L43
            boolean r0 = r1.isOpen()     // Catch: java.lang.Throwable -> L44
            if (r0 == 0) goto L43
            r1.close()     // Catch: java.lang.Throwable -> L44
        L43:
            throw r4     // Catch: java.lang.Throwable -> L44
        L44:
            r4 = move-exception
            monitor-exit(r3)
            throw r4
        */
        throw new UnsupportedOperationException("Method not decompiled: com.ironsource.t9.b(java.lang.String):void");
    }

    @Override // android.database.sqlite.SQLiteOpenHelper
    public void onCreate(SQLiteDatabase sQLiteDatabase) {
        sQLiteDatabase.execSQL("CREATE TABLE events (_id INTEGER PRIMARY KEY,eventid INTEGER,timestamp INTEGER,type TEXT,data TEXT )");
    }

    @Override // android.database.sqlite.SQLiteOpenHelper
    public void onUpgrade(SQLiteDatabase sQLiteDatabase, int i2, int i3) {
        sQLiteDatabase.execSQL("DROP TABLE IF EXISTS events");
        onCreate(sQLiteDatabase);
    }
}
