package coil.memory;

import Z0.o;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import coil.EventListener;
import coil.ImageLoader;
import coil.decode.DataSource;
import coil.decode.DecodeUtils;
import coil.intercept.EngineInterceptor;
import coil.intercept.Interceptor;
import coil.memory.MemoryCache;
import coil.request.ImageRequest;
import coil.request.Options;
import coil.request.RequestService;
import coil.request.SuccessResult;
import coil.size.Dimension;
import coil.size.Scale;
import coil.size.Size;
import coil.size.Sizes;
import coil.transform.Transformation;
import coil.util.Bitmaps;
import coil.util.Logger;
import coil.util.Requests;
import coil.util.Utils;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import kotlin.jvm.internal.h;
import x4.AbstractC3229F;

/* JADX INFO: loaded from: classes.dex */
public final class MemoryCacheService {
    public static final Companion Companion = new Companion(null);
    public static final String EXTRA_DISK_CACHE_KEY = "coil#disk_cache_key";
    public static final String EXTRA_IS_SAMPLED = "coil#is_sampled";
    public static final String EXTRA_TRANSFORMATION_INDEX = "coil#transformation_";
    public static final String EXTRA_TRANSFORMATION_SIZE = "coil#transformation_size";
    private static final String TAG = "MemoryCacheService";
    private final ImageLoader imageLoader;
    private final Logger logger;
    private final RequestService requestService;

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

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

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

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

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

        private Companion() {
        }
    }

    public MemoryCacheService(ImageLoader imageLoader, RequestService requestService, Logger logger) {
        this.imageLoader = imageLoader;
        this.requestService = requestService;
        this.logger = logger;
    }

    private final String getDiskCacheKey(MemoryCache.Value value) {
        Object obj = value.getExtras().get(EXTRA_DISK_CACHE_KEY);
        if (obj instanceof String) {
            return (String) obj;
        }
        return null;
    }

    private final boolean isSampled(MemoryCache.Value value) {
        Object obj = value.getExtras().get(EXTRA_IS_SAMPLED);
        Boolean bool = obj instanceof Boolean ? (Boolean) obj : null;
        if (bool != null) {
            return bool.booleanValue();
        }
        return false;
    }

    private final boolean isSizeValid(ImageRequest imageRequest, MemoryCache.Key key, MemoryCache.Value value, Size size, Scale scale) {
        boolean z6;
        String str;
        boolean zIsSampled = isSampled(value);
        if (Sizes.isOriginal(size)) {
            if (!zIsSampled) {
                return true;
            }
            Logger logger = this.logger;
            if (logger != null && logger.getLevel() <= 3) {
                logger.log(TAG, 3, imageRequest.getData() + ": Requested original size, but cached image is sampled.", null);
            }
            return false;
        }
        String str2 = key.getExtras().get(EXTRA_TRANSFORMATION_SIZE);
        if (str2 != null) {
            return str2.equals(size.toString());
        }
        int width = value.getBitmap().getWidth();
        int height = value.getBitmap().getHeight();
        Dimension width2 = size.getWidth();
        int i6 = width2 instanceof Dimension.Pixels ? ((Dimension.Pixels) width2).px : Integer.MAX_VALUE;
        Dimension height2 = size.getHeight();
        int i7 = height2 instanceof Dimension.Pixels ? ((Dimension.Pixels) height2).px : Integer.MAX_VALUE;
        double dComputeSizeMultiplier = DecodeUtils.computeSizeMultiplier(width, height, i6, i7, scale);
        boolean allowInexactSize = Requests.getAllowInexactSize(imageRequest);
        if (!allowInexactSize) {
            z6 = false;
            str = TAG;
            if ((!Utils.isMinOrMax(i6) && Math.abs(i6 - width) > 1) || (!Utils.isMinOrMax(i7) && Math.abs(i7 - height) > 1)) {
            }
            return true;
        }
        double d6 = dComputeSizeMultiplier > 1.0d ? 1.0d : dComputeSizeMultiplier;
        z6 = false;
        str = TAG;
        if (Math.abs(((double) i6) - (((double) width) * d6)) <= 1.0d || Math.abs(((double) i7) - (d6 * ((double) height))) <= 1.0d) {
            return true;
        }
        if (dComputeSizeMultiplier != 1.0d && !allowInexactSize) {
            Logger logger2 = this.logger;
            if (logger2 != null && logger2.getLevel() <= 3) {
                logger2.log(str, 3, imageRequest.getData() + ": Cached image's request size (" + width + ", " + height + ") does not exactly match the requested size (" + size.getWidth() + ", " + size.getHeight() + ", " + scale + ").", null);
            }
            return z6;
        }
        String str3 = str;
        if (dComputeSizeMultiplier > 1.0d && zIsSampled) {
            Logger logger3 = this.logger;
            if (logger3 != null && logger3.getLevel() <= 3) {
                logger3.log(str3, 3, imageRequest.getData() + ": Cached image's request size (" + width + ", " + height + ") is smaller than the requested size (" + size.getWidth() + ", " + size.getHeight() + ", " + scale + ").", null);
            }
            return z6;
        }
        return true;
    }

    public final MemoryCache.Value getCacheValue(ImageRequest imageRequest, MemoryCache.Key key, Size size, Scale scale) {
        if (!imageRequest.getMemoryCachePolicy().getReadEnabled()) {
            return null;
        }
        MemoryCache memoryCache = this.imageLoader.getMemoryCache();
        MemoryCache.Value value = memoryCache != null ? memoryCache.get(key) : null;
        if (value == null || !isCacheValueValid$coil_base_release(imageRequest, key, value, size, scale)) {
            return null;
        }
        return value;
    }

    public final boolean isCacheValueValid$coil_base_release(ImageRequest imageRequest, MemoryCache.Key key, MemoryCache.Value value, Size size, Scale scale) {
        if (this.requestService.isConfigValidForHardware(imageRequest, Bitmaps.getSafeConfig(value.getBitmap()))) {
            return isSizeValid(imageRequest, key, value, size, scale);
        }
        Logger logger = this.logger;
        if (logger == null || logger.getLevel() > 3) {
            return false;
        }
        logger.log(TAG, 3, imageRequest.getData() + ": Cached bitmap is hardware-backed, which is incompatible with the request.", null);
        return false;
    }

    public final MemoryCache.Key newCacheKey(ImageRequest imageRequest, Object obj, Options options, EventListener eventListener) {
        MemoryCache.Key memoryCacheKey = imageRequest.getMemoryCacheKey();
        if (memoryCacheKey != null) {
            return memoryCacheKey;
        }
        eventListener.keyStart(imageRequest, obj);
        String strKey = this.imageLoader.getComponents().key(obj, options);
        eventListener.keyEnd(imageRequest, strKey);
        if (strKey == null) {
            return null;
        }
        List<Transformation> transformations = imageRequest.getTransformations();
        Map<String, String> mapMemoryCacheKeys = imageRequest.getParameters().memoryCacheKeys();
        if (transformations.isEmpty() && mapMemoryCacheKeys.isEmpty()) {
            return new MemoryCache.Key(strKey, null, 2, null);
        }
        LinkedHashMap linkedHashMapI = AbstractC3229F.I(mapMemoryCacheKeys);
        if (!transformations.isEmpty()) {
            List<Transformation> transformations2 = imageRequest.getTransformations();
            int size = transformations2.size();
            for (int i6 = 0; i6 < size; i6++) {
                linkedHashMapI.put(o.l(EXTRA_TRANSFORMATION_INDEX, i6), transformations2.get(i6).getCacheKey());
            }
            linkedHashMapI.put(EXTRA_TRANSFORMATION_SIZE, options.getSize().toString());
        }
        return new MemoryCache.Key(strKey, linkedHashMapI);
    }

    public final SuccessResult newResult(Interceptor.Chain chain, ImageRequest imageRequest, MemoryCache.Key key, MemoryCache.Value value) {
        return new SuccessResult(new BitmapDrawable(imageRequest.getContext().getResources(), value.getBitmap()), imageRequest, DataSource.MEMORY_CACHE, key, getDiskCacheKey(value), isSampled(value), Utils.isPlaceholderCached(chain));
    }

    public final boolean setCacheValue(MemoryCache.Key key, ImageRequest imageRequest, EngineInterceptor.ExecuteResult executeResult) {
        MemoryCache memoryCache;
        Bitmap bitmap;
        if (imageRequest.getMemoryCachePolicy().getWriteEnabled() && (memoryCache = this.imageLoader.getMemoryCache()) != null && key != null) {
            Drawable drawable = executeResult.getDrawable();
            BitmapDrawable bitmapDrawable = drawable instanceof BitmapDrawable ? (BitmapDrawable) drawable : null;
            if (bitmapDrawable != null && (bitmap = bitmapDrawable.getBitmap()) != null) {
                LinkedHashMap linkedHashMap = new LinkedHashMap();
                linkedHashMap.put(EXTRA_IS_SAMPLED, Boolean.valueOf(executeResult.isSampled()));
                String diskCacheKey = executeResult.getDiskCacheKey();
                if (diskCacheKey != null) {
                    linkedHashMap.put(EXTRA_DISK_CACHE_KEY, diskCacheKey);
                }
                memoryCache.set(key, new MemoryCache.Value(bitmap, linkedHashMap));
                return true;
            }
        }
        return false;
    }
}
