package coil.memory;

import android.graphics.Bitmap;
import coil.memory.MemoryCache;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import kotlin.jvm.internal.h;
import x4.AbstractC3251q;

/* JADX INFO: loaded from: classes.dex */
public final class RealWeakMemoryCache implements WeakMemoryCache {
    private static final int CLEAN_UP_INTERVAL = 10;
    public static final Companion Companion = new Companion(null);
    private final LinkedHashMap<MemoryCache.Key, ArrayList<InternalValue>> cache = new LinkedHashMap<>();
    private int operationsSinceCleanUp;

    public static final class Companion {
        public /* synthetic */ Companion(h hVar) {
            this();
        }

        private Companion() {
        }
    }

    public static final class InternalValue {
        private final WeakReference<Bitmap> bitmap;
        private final Map<String, Object> extras;
        private final int identityHashCode;
        private final int size;

        public InternalValue(int i6, WeakReference<Bitmap> weakReference, Map<String, ? extends Object> map, int i7) {
            this.identityHashCode = i6;
            this.bitmap = weakReference;
            this.extras = map;
            this.size = i7;
        }

        public final WeakReference<Bitmap> getBitmap() {
            return this.bitmap;
        }

        public final Map<String, Object> getExtras() {
            return this.extras;
        }

        public final int getIdentityHashCode() {
            return this.identityHashCode;
        }

        public final int getSize() {
            return this.size;
        }
    }

    private final void cleanUpIfNecessary() {
        int i6 = this.operationsSinceCleanUp;
        this.operationsSinceCleanUp = i6 + 1;
        if (i6 >= 10) {
            cleanUp$coil_base_release();
        }
    }

    public static /* synthetic */ void getCache$coil_base_release$annotations() {
    }

    public final void cleanUp$coil_base_release() {
        WeakReference<Bitmap> bitmap;
        this.operationsSinceCleanUp = 0;
        Iterator<ArrayList<InternalValue>> it = this.cache.values().iterator();
        while (it.hasNext()) {
            ArrayList<InternalValue> next = it.next();
            if (next.size() <= 1) {
                InternalValue internalValue = (InternalValue) AbstractC3251q.g0(next);
                if (((internalValue == null || (bitmap = internalValue.getBitmap()) == null) ? null : bitmap.get()) == null) {
                    it.remove();
                }
            } else {
                int size = next.size();
                int i6 = 0;
                for (int i7 = 0; i7 < size; i7++) {
                    int i8 = i7 - i6;
                    if (next.get(i8).getBitmap().get() == null) {
                        next.remove(i8);
                        i6++;
                    }
                }
                if (next.isEmpty()) {
                    it.remove();
                }
            }
        }
    }

    @Override // coil.memory.WeakMemoryCache
    public synchronized void clearMemory() {
        this.operationsSinceCleanUp = 0;
        this.cache.clear();
    }

    @Override // coil.memory.WeakMemoryCache
    public synchronized MemoryCache.Value get(MemoryCache.Key key) {
        try {
            ArrayList<InternalValue> arrayList = this.cache.get(key);
            MemoryCache.Value value = null;
            if (arrayList == null) {
                return null;
            }
            int size = arrayList.size();
            int i6 = 0;
            while (true) {
                if (i6 >= size) {
                    break;
                }
                InternalValue internalValue = arrayList.get(i6);
                Bitmap bitmap = internalValue.getBitmap().get();
                MemoryCache.Value value2 = bitmap != null ? new MemoryCache.Value(bitmap, internalValue.getExtras()) : null;
                if (value2 != null) {
                    value = value2;
                    break;
                }
                i6++;
            }
            cleanUpIfNecessary();
            return value;
        } catch (Throwable th) {
            throw th;
        }
    }

    public final LinkedHashMap<MemoryCache.Key, ArrayList<InternalValue>> getCache$coil_base_release() {
        return this.cache;
    }

    @Override // coil.memory.WeakMemoryCache
    public synchronized Set<MemoryCache.Key> getKeys() {
        return AbstractC3251q.H0(this.cache.keySet());
    }

    @Override // coil.memory.WeakMemoryCache
    public synchronized boolean remove(MemoryCache.Key key) {
        return this.cache.remove(key) != null;
    }

    @Override // coil.memory.WeakMemoryCache
    public synchronized void set(MemoryCache.Key key, Bitmap bitmap, Map<String, ? extends Object> map, int i6) {
        try {
            LinkedHashMap<MemoryCache.Key, ArrayList<InternalValue>> linkedHashMap = this.cache;
            ArrayList<InternalValue> arrayList = linkedHashMap.get(key);
            if (arrayList == null) {
                arrayList = new ArrayList<>();
                linkedHashMap.put(key, arrayList);
            }
            ArrayList<InternalValue> arrayList2 = arrayList;
            int iIdentityHashCode = System.identityHashCode(bitmap);
            InternalValue internalValue = new InternalValue(iIdentityHashCode, new WeakReference(bitmap), map, i6);
            int size = arrayList2.size();
            int i7 = 0;
            while (true) {
                if (i7 >= size) {
                    arrayList2.add(internalValue);
                    break;
                }
                InternalValue internalValue2 = arrayList2.get(i7);
                if (i6 < internalValue2.getSize()) {
                    i7++;
                } else if (internalValue2.getIdentityHashCode() == iIdentityHashCode && internalValue2.getBitmap().get() == bitmap) {
                    arrayList2.set(i7, internalValue);
                } else {
                    arrayList2.add(i7, internalValue);
                }
            }
            cleanUpIfNecessary();
        } catch (Throwable th) {
            throw th;
        }
    }

    @Override // coil.memory.WeakMemoryCache
    public synchronized void trimMemory(int i6) {
        if (i6 >= 10 && i6 != 20) {
            cleanUp$coil_base_release();
        }
    }
}
