package com.bytedance.sdk.component.adexpress.fm.ro;

import android.content.ContentValues;
import android.database.Cursor;
import android.text.TextUtils;
import android.util.LruCache;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicBoolean;
import tv.danmaku.ijk.media.player.IjkMediaPlayer;
import xyz.stream.download.config.InnerConstant;

/* JADX INFO: loaded from: classes.dex */
public class ajl {
    public static int fm = 20;
    private static volatile ajl ro;
    private volatile ConcurrentHashMap<String, com.bytedance.sdk.component.adexpress.fm.lb.lb> jnr;
    private final Object yz = new Object();
    private AtomicBoolean ajl = new AtomicBoolean(false);
    private LruCache<String, com.bytedance.sdk.component.adexpress.fm.lb.ro> wsy = new LruCache<String, com.bytedance.sdk.component.adexpress.fm.lb.ro>(fm) { // from class: com.bytedance.sdk.component.adexpress.fm.ro.ajl.1
        @Override // android.util.LruCache
        /* JADX INFO: renamed from: fm, reason: merged with bridge method [inline-methods] */
        public int sizeOf(String str, com.bytedance.sdk.component.adexpress.fm.lb.ro roVar) {
            return 1;
        }
    };

    /* JADX INFO: renamed from: lb, reason: collision with root package name */
    private Set<String> f6534lb = Collections.synchronizedSet(new HashSet());

    private ajl() {
    }

    public static void fm(int i10) {
        fm = i10;
    }

    public static String lb() {
        return "CREATE TABLE IF NOT EXISTS template_diff_new (_id INTEGER PRIMARY KEY AUTOINCREMENT,rit TEXT ,id TEXT UNIQUE,md5 TEXT ,url TEXT , data TEXT , version TEXT , update_time TEXT)";
    }

    private void yz(String str) {
        LruCache<String, com.bytedance.sdk.component.adexpress.fm.lb.ro> lruCache;
        if (TextUtils.isEmpty(str) || (lruCache = this.wsy) == null || lruCache.size() <= 0) {
            return;
        }
        synchronized (this.yz) {
            this.wsy.remove(str);
        }
    }

    public Set<String> ro(String str) {
        if (!TextUtils.isEmpty(str) && com.bytedance.sdk.component.adexpress.fm.fm.fm.fm().ro() != null) {
            HashSet hashSet = new HashSet();
            Cursor cursorFm = com.bytedance.sdk.component.adexpress.fm.fm.fm.fm().ro().fm("template_diff_new", null, "rit=?", new String[]{str}, null, null, null);
            if (cursorFm != null) {
                try {
                    if (cursorFm.moveToFirst()) {
                        do {
                            hashSet.add(cursorFm.getString(cursorFm.getColumnIndex(InnerConstant.Db.f36069id)));
                        } while (cursorFm.moveToNext());
                        cursorFm.close();
                        return hashSet;
                    }
                } catch (Exception unused) {
                } catch (Throwable th) {
                    cursorFm.close();
                    throw th;
                }
                cursorFm.close();
            }
        }
        return null;
    }

    public static ajl fm() {
        if (ro == null) {
            synchronized (ajl.class) {
                try {
                    if (ro == null) {
                        ro = new ajl();
                    }
                } catch (Throwable th) {
                    throw th;
                }
            }
        }
        return ro;
    }

    public void lb(String str) {
        com.bytedance.sdk.component.adexpress.fm.lb.lb lbVar;
        try {
            if (this.jnr == null || this.jnr.isEmpty() || (lbVar = this.jnr.get(str)) == null) {
                return;
            }
            if (!TextUtils.isEmpty(lbVar.fm()) && com.bytedance.sdk.component.adexpress.fm.fm.fm.fm().ajl() != null) {
                com.bytedance.sdk.component.adexpress.fm.fm.fm.fm().ajl();
            }
            this.jnr.remove(str);
        } catch (Throwable unused) {
        }
    }

    public com.bytedance.sdk.component.adexpress.fm.lb.ro fm(String str) {
        com.bytedance.sdk.component.adexpress.fm.lb.ro roVar;
        com.bytedance.sdk.component.adexpress.fm.lb.ro roVarFm;
        if (TextUtils.isEmpty(str) || com.bytedance.sdk.component.adexpress.fm.fm.fm.fm().ro() == null) {
            return null;
        }
        synchronized (this.yz) {
            roVar = this.wsy.get(String.valueOf(str));
        }
        if (roVar != null) {
            return roVar;
        }
        Cursor cursorFm = com.bytedance.sdk.component.adexpress.fm.fm.fm.fm().ro().fm("template_diff_new", null, "id=?", new String[]{str}, null, null, null);
        if (cursorFm != null) {
            try {
                if (cursorFm.moveToFirst()) {
                    do {
                        String string = cursorFm.getString(cursorFm.getColumnIndex("rit"));
                        String string2 = cursorFm.getString(cursorFm.getColumnIndex(InnerConstant.Db.f36069id));
                        String string3 = cursorFm.getString(cursorFm.getColumnIndex("md5"));
                        String string4 = cursorFm.getString(cursorFm.getColumnIndex(IjkMediaPlayer.OnNativeInvokeListener.ARG_URL));
                        String string5 = cursorFm.getString(cursorFm.getColumnIndex("data"));
                        String string6 = cursorFm.getString(cursorFm.getColumnIndex("version"));
                        roVarFm = new com.bytedance.sdk.component.adexpress.fm.lb.ro().fm(string).ro(string2).lb(string3).yz(string4).jnr(string5).ajl(string6).fm(Long.valueOf(cursorFm.getLong(cursorFm.getColumnIndex("update_time"))));
                        synchronized (this.yz) {
                            this.wsy.put(string2, roVarFm);
                        }
                        this.f6534lb.add(string2);
                    } while (cursorFm.moveToNext());
                    cursorFm.close();
                    return roVarFm;
                }
            } catch (Throwable unused) {
            }
            cursorFm.close();
        }
        return null;
    }

    public List<com.bytedance.sdk.component.adexpress.fm.lb.ro> ro() {
        if (com.bytedance.sdk.component.adexpress.fm.fm.fm.fm().ro() == null) {
            return null;
        }
        boolean z10 = this.ajl.get();
        this.ajl.set(true);
        ArrayList arrayList = new ArrayList();
        Cursor cursorFm = com.bytedance.sdk.component.adexpress.fm.fm.fm.fm().ro().fm("template_diff_new", null, null, null, null, null, null);
        if (cursorFm != null) {
            while (cursorFm.moveToNext()) {
                try {
                    String string = cursorFm.getString(cursorFm.getColumnIndex("rit"));
                    String string2 = cursorFm.getString(cursorFm.getColumnIndex(InnerConstant.Db.f36069id));
                    String string3 = cursorFm.getString(cursorFm.getColumnIndex("md5"));
                    String string4 = cursorFm.getString(cursorFm.getColumnIndex(IjkMediaPlayer.OnNativeInvokeListener.ARG_URL));
                    String string5 = cursorFm.getString(cursorFm.getColumnIndex("data"));
                    String string6 = cursorFm.getString(cursorFm.getColumnIndex("version"));
                    arrayList.add(new com.bytedance.sdk.component.adexpress.fm.lb.ro().fm(string).ro(string2).lb(string3).yz(string4).jnr(string5).ajl(string6).fm(Long.valueOf(cursorFm.getLong(cursorFm.getColumnIndex("update_time")))));
                    synchronized (this.yz) {
                        this.wsy.put(string2, (com.bytedance.sdk.component.adexpress.fm.lb.ro) arrayList.get(arrayList.size() - 1));
                    }
                    this.f6534lb.add(string2);
                    if (!z10 && com.bytedance.sdk.component.adexpress.fm.fm.fm.fm().ajl() != null) {
                        if (this.jnr == null) {
                            this.jnr = new ConcurrentHashMap<>();
                        }
                        if (string2 != null && !this.jnr.contains(string2)) {
                            this.jnr.put(string2, new com.bytedance.sdk.component.adexpress.fm.lb.lb(string, string2, string3));
                        }
                    }
                } catch (Throwable unused) {
                }
            }
            cursorFm.close();
        }
        return arrayList;
    }

    public void fm(com.bytedance.sdk.component.adexpress.fm.lb.ro roVar, boolean z10) {
        if (roVar == null || com.bytedance.sdk.component.adexpress.fm.fm.fm.fm().ro() == null || TextUtils.isEmpty(roVar.ro())) {
            return;
        }
        Cursor cursorFm = com.bytedance.sdk.component.adexpress.fm.fm.fm.fm().ro().fm("template_diff_new", null, "id=?", new String[]{roVar.ro()}, null, null, null);
        boolean z11 = cursorFm != null && cursorFm.getCount() > 0;
        if (cursorFm != null) {
            try {
                string = cursorFm.moveToFirst() ? cursorFm.getString(cursorFm.getColumnIndex("rit")) : null;
                cursorFm.close();
            } catch (Throwable unused) {
            }
        }
        ContentValues contentValues = new ContentValues();
        contentValues.put("rit", roVar.fm());
        contentValues.put(InnerConstant.Db.f36069id, roVar.ro());
        contentValues.put("md5", roVar.lb());
        contentValues.put(IjkMediaPlayer.OnNativeInvokeListener.ARG_URL, roVar.yz());
        contentValues.put("data", roVar.jnr());
        contentValues.put("version", roVar.ajl());
        contentValues.put("update_time", roVar.wsy());
        if (z11) {
            com.bytedance.sdk.component.adexpress.fm.fm.fm.fm().ro().fm("template_diff_new", contentValues, "id=?", new String[]{roVar.ro()});
        } else {
            com.bytedance.sdk.component.adexpress.fm.fm.fm.fm().ro().fm("template_diff_new", contentValues);
        }
        synchronized (this.yz) {
            this.wsy.put(roVar.ro(), roVar);
        }
        this.f6534lb.add(roVar.ro());
        if (z10) {
            return;
        }
        try {
            if (com.bytedance.sdk.component.adexpress.fm.fm.fm.fm().ajl() == null) {
                return;
            }
            if (this.jnr == null) {
                this.jnr = new ConcurrentHashMap<>();
            }
            com.bytedance.sdk.component.adexpress.fm.lb.lb lbVar = new com.bytedance.sdk.component.adexpress.fm.lb.lb(roVar.fm(), roVar.ro(), roVar.lb());
            this.jnr.put(roVar.ro(), lbVar);
            if (string != null) {
                com.bytedance.sdk.component.adexpress.fm.fm.fm.fm().ajl();
                lbVar.ro();
            }
            com.bytedance.sdk.component.adexpress.fm.fm.fm.fm().ajl();
            roVar.fm();
        } catch (Throwable unused2) {
        }
    }

    public void fm(Set<String> set) {
        if (set == null || set.isEmpty() || com.bytedance.sdk.component.adexpress.fm.fm.fm.fm().ro() == null) {
            return;
        }
        String[] strArr = (String[]) set.toArray(new String[set.size()]);
        if (strArr.length > 0) {
            for (int i10 = 0; i10 < strArr.length; i10++) {
                yz(strArr[i10]);
                com.bytedance.sdk.component.adexpress.fm.fm.fm.fm().ro().fm("template_diff_new", "id=?", new String[]{strArr[i10]});
                lb(strArr[i10]);
            }
        }
    }
}
