package epson.print.imgsel;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import java.io.File;
import java.io.Serializable;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class BitmapCache {
    private static final String JOURNAL_FILE_NAME = "journal.bin";
    private static final int JPEG_QUALITY = 90;
    private File mCacheDirectory;
    private LinkedHashMap<File, FileAttribute> mCacheFileInfoMap = new LinkedHashMap<>(32, 0.75f, true);
    private long mCacheSizeLimit = 10000000;
    private final Callable<Void> mCleanupCallable = new Callable<Void>() {
        public Void call() throws Exception {
            BitmapCache.this.trimToSize();
            return null;
        }
    };
    private final ExecutorService mExecutorService = new ThreadPoolExecutor(0, 1, 60, TimeUnit.SECONDS, new LinkedBlockingQueue());
    private long mTotalCacheSize;

    public interface DataWriter {
        boolean writeData(File file);
    }

    public boolean initialize(File file, int i) {
        mCacheSizeLimit = (long) i;
        return initialize(file);
    }

    public boolean initialize(File file) {
        if (!setCacheDirectory(file)) {
            return false;
        }
        restoreJournal();
        return true;
    }

    public boolean setCacheDirectory(File file) {
        mCacheDirectory = null;
        if (file.exists()) {
            if (!file.isDirectory() || !file.canRead()) {
                return false;
            }
        } else if (!file.mkdirs()) {
            return false;
        }
        mCacheDirectory = file;
        return true;
    }

    /* JADX WARNING: Can't wrap try/catch for region: R(4:24|(2:26|27)|28|29) */
    /* JADX WARNING: Code restructure failed: missing block: B:23:0x003d, code lost:
        if (r3 != null) goto L_0x002b;
     */
    /* JADX WARNING: Missing exception handler attribute for start block: B:28:0x0046 */
    /* JADX WARNING: Removed duplicated region for block: B:26:0x0043 A[SYNTHETIC, Splitter:B:26:0x0043] */
    /* JADX WARNING: Removed duplicated region for block: B:33:0x004a A[Catch:{ IOException -> 0x0047, ClassNotFoundException -> 0x0036, all -> 0x0033 }] */
    /* JADX WARNING: Removed duplicated region for block: B:36:0x004d A[Catch:{ IOException -> 0x0047, ClassNotFoundException -> 0x0036, all -> 0x0033 }] */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    private synchronized void restoreJournal() {
        /*
            r6 = this;
            monitor-enter(r6)
            java.io.File r0 = r6.getJournalFile()     // Catch:{ all -> 0x005b }
            boolean r1 = r0.exists()     // Catch:{ all -> 0x005b }
            if (r1 != 0) goto L_0x0010
            r6.clearCacheFileInfoMap()     // Catch:{ all -> 0x005b }
            monitor-exit(r6)
            return
        L_0x0010:
            r1 = 0
            r2 = 0
            java.io.ObjectInputStream r3 = new java.io.ObjectInputStream     // Catch:{ IOException -> 0x0047, ClassNotFoundException -> 0x0036, all -> 0x0033 }
            java.io.FileInputStream r4 = new java.io.FileInputStream     // Catch:{ IOException -> 0x0047, ClassNotFoundException -> 0x0036, all -> 0x0033 }
            r4.<init>(r0)     // Catch:{ IOException -> 0x0047, ClassNotFoundException -> 0x0036, all -> 0x0033 }
            r3.<init>(r4)     // Catch:{ IOException -> 0x0047, ClassNotFoundException -> 0x0036, all -> 0x0033 }
            java.lang.Object r1 = r3.readObject()     // Catch:{ IOException -> 0x0031, ClassNotFoundException -> 0x002f }
            java.util.LinkedHashMap r1 = (java.util.LinkedHashMap) r1     // Catch:{ IOException -> 0x0031, ClassNotFoundException -> 0x002f }
            r6.mCacheFileInfoMap = r1     // Catch:{ IOException -> 0x0031, ClassNotFoundException -> 0x002f }
            r6.calcTotalSizeAndDeleteNotExistEntry()     // Catch:{ IOException -> 0x0031, ClassNotFoundException -> 0x002f }
            r6.deleteNoEntryCacheFile()     // Catch:{ IOException -> 0x0031, ClassNotFoundException -> 0x002f }
            r2 = 1
        L_0x002b:
            r3.close()     // Catch:{ IOException -> 0x004b }
            goto L_0x004b
        L_0x002f:
            r1 = move-exception
            goto L_0x003a
        L_0x0031:
            goto L_0x0048
        L_0x0033:
            r0 = move-exception
            r3 = r1
            goto L_0x0041
        L_0x0036:
            r3 = move-exception
            r5 = r3
            r3 = r1
            r1 = r5
        L_0x003a:
            r1.printStackTrace()     // Catch:{ all -> 0x0040 }
            if (r3 == 0) goto L_0x004b
            goto L_0x002b
        L_0x0040:
            r0 = move-exception
        L_0x0041:
            if (r3 == 0) goto L_0x0046
            r3.close()     // Catch:{ IOException -> 0x0046 }
        L_0x0046:
            throw r0     // Catch:{ all -> 0x005b }
        L_0x0047:
            r3 = r1
        L_0x0048:
            if (r3 == 0) goto L_0x004b
            goto L_0x002b
        L_0x004b:
            if (r2 != 0) goto L_0x0059
            boolean r1 = r0.exists()     // Catch:{ all -> 0x005b }
            if (r1 == 0) goto L_0x0056
            r0.delete()     // Catch:{ all -> 0x005b }
        L_0x0056:
            r6.clearCacheFileInfoMap()     // Catch:{ all -> 0x005b }
        L_0x0059:
            monitor-exit(r6)
            return
        L_0x005b:
            r0 = move-exception
            monitor-exit(r6)
            throw r0
        */
        throw new UnsupportedOperationException("Method not decompiled: epson.print.imgsel.BitmapCache.restoreJournal():void");
    }

    private synchronized void calcTotalSizeAndDeleteNotExistEntry() {
        mTotalCacheSize = 0;
        Set<File> keySet = mCacheFileInfoMap.keySet();
        HashSet<File> hashSet = new HashSet<>();
        for (File next : keySet) {
            File cacheFile = getCacheFile(next.toString());
            if (!cacheFile.exists()) {
                hashSet.add(next);
            } else {
                mTotalCacheSize += cacheFile.length();
            }
        }
        for (File remove : hashSet) {
            mCacheFileInfoMap.remove(remove);
        }
        trimToSize();
    }

    private synchronized void deleteNoEntryCacheFile() {
        if (this.mCacheDirectory == null) {
            Set<File> keySet = mCacheFileInfoMap.keySet();
            HashSet hashSet = new HashSet(keySet.size());
            for (File file : keySet) {
                hashSet.add(getCacheFile(file.toString()));
            }
            for (File file2 : mCacheDirectory.listFiles()) {
                if (!hashSet.contains(file2)) {
                    file2.delete();
                }
            }
        }
    }

    public synchronized void trimToSize() {
        while (this.mTotalCacheSize > mCacheSizeLimit) {
            removeCacheFile(this.mCacheFileInfoMap.keySet().iterator().next());
        }
    }

    public synchronized void removeCacheFile(File file) {
        File cacheFile = getCacheFile(file.toString());
        long j = 0;
        if (cacheFile != null) {
            j = cacheFile.length();
            cacheFile.delete();
        }
        mTotalCacheSize -= j;
        mCacheFileInfoMap.remove(file);
    }

    private synchronized void clearCacheFileInfoMap() {
        mCacheFileInfoMap.clear();
        mTotalCacheSize = 0;
        if (this.mCacheDirectory != null) {
            for (File delete : mCacheDirectory.listFiles()) {
                delete.delete();
            }
        }
    }

    /* JADX WARNING: Can't wrap try/catch for region: R(4:15|(2:32|33)|34|35) */
    /* JADX WARNING: Missing exception handler attribute for start block: B:34:0x005c */
    /* JADX WARNING: Removed duplicated region for block: B:21:0x0047 A[SYNTHETIC, Splitter:B:21:0x0047] */
    /* JADX WARNING: Removed duplicated region for block: B:25:0x004c A[SYNTHETIC, Splitter:B:25:0x004c] */
    /* JADX WARNING: Removed duplicated region for block: B:32:0x0059 A[SYNTHETIC, Splitter:B:32:0x0059] */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    public synchronized void saveJournal() {
        /*
            r8 = this;
            monitor-enter(r8)
            java.io.File r0 = r8.getJournalFile()     // Catch:{ all -> 0x005d }
            r1 = 0
            r2 = 0
            java.io.ObjectOutputStream r3 = new java.io.ObjectOutputStream     // Catch:{ IOException -> 0x0025 }
            java.io.FileOutputStream r4 = new java.io.FileOutputStream     // Catch:{ IOException -> 0x0025 }
            r4.<init>(r0)     // Catch:{ IOException -> 0x0025 }
            r3.<init>(r4)     // Catch:{ IOException -> 0x0025 }
            java.util.LinkedHashMap<java.io.File, epson.print.imgsel.BitmapCache$FileAttribute> r1 = r8.mCacheFileInfoMap     // Catch:{ IOException -> 0x001e, all -> 0x001b }
            r3.writeObject(r1)     // Catch:{ IOException -> 0x001e, all -> 0x001b }
            r2 = 1
            r3.close()     // Catch:{ IOException -> 0x004a }
            goto L_0x004a
        L_0x001b:
            r0 = move-exception
            r1 = r3
            goto L_0x0057
        L_0x001e:
            r1 = move-exception
            r7 = r3
            r3 = r1
            r1 = r7
            goto L_0x0026
        L_0x0023:
            r0 = move-exception
            goto L_0x0057
        L_0x0025:
            r3 = move-exception
        L_0x0026:
            java.lang.String r4 = "BitmapCache"
            java.lang.StringBuilder r5 = new java.lang.StringBuilder     // Catch:{ all -> 0x0023 }
            r5.<init>()     // Catch:{ all -> 0x0023 }
            java.lang.String r6 = "in saveJournal() exception <"
            r5.append(r6)     // Catch:{ all -> 0x0023 }
            java.lang.String r3 = r3.toString()     // Catch:{ all -> 0x0023 }
            r5.append(r3)     // Catch:{ all -> 0x0023 }
            java.lang.String r3 = ">"
            r5.append(r3)     // Catch:{ all -> 0x0023 }
            java.lang.String r3 = r5.toString()     // Catch:{ all -> 0x0023 }
            android.util.Log.v(r4, r3)     // Catch:{ all -> 0x0023 }
            if (r1 == 0) goto L_0x004a
            r1.close()     // Catch:{ IOException -> 0x004a }
        L_0x004a:
            if (r2 != 0) goto L_0x0055
            boolean r1 = r0.exists()     // Catch:{ all -> 0x005d }
            if (r1 == 0) goto L_0x0055
            r0.delete()     // Catch:{ all -> 0x005d }
        L_0x0055:
            monitor-exit(r8)
            return
        L_0x0057:
            if (r1 == 0) goto L_0x005c
            r1.close()     // Catch:{ IOException -> 0x005c }
        L_0x005c:
            throw r0     // Catch:{ all -> 0x005d }
        L_0x005d:
            r0 = move-exception
            monitor-exit(r8)
            throw r0
        */
        throw new UnsupportedOperationException("Method not decompiled: epson.print.imgsel.BitmapCache.saveJournal():void");
    }

    private File getJournalFile() {
        return new File(this.mCacheDirectory, JOURNAL_FILE_NAME);
    }

    public synchronized Bitmap getBitmap(File file) {
        File file2 = getFile(file);
        if (file2 == null) {
            return null;
        }
        return BitmapFactory.decodeFile(file2.toString());
    }

    public synchronized File getFile(File file) {
        FileAttribute fileAttribute = mCacheFileInfoMap.get(file);
        if (fileAttribute == null) {
            return null;
        }
        if (fileAttribute.isSameAttribute(file)) {
            File cacheFile = getCacheFile(file.toString());
            if (cacheFile.exists()) {
                return cacheFile;
            }
            calcTotalSizeAndDeleteNotExistEntry();
            return null;
        }
        removeCacheFile(file);
        return null;
    }

    public void addBitmap(File file, Bitmap bitmap) {
        addFile(file, new BitmapDataWrite(bitmap));
    }

    static class BitmapDataWrite implements DataWriter {
        private Bitmap mBitmap;

        public BitmapDataWrite(Bitmap bitmap) {
            mBitmap = bitmap;
        }

        /* JADX WARNING: Removed duplicated region for block: B:15:0x0023 A[SYNTHETIC, Splitter:B:15:0x0023] */
        /* JADX WARNING: Removed duplicated region for block: B:21:0x002a A[SYNTHETIC, Splitter:B:21:0x002a] */
        /* JADX WARNING: Removed duplicated region for block: B:26:? A[RETURN, SYNTHETIC] */
        /* Code decompiled incorrectly, please refer to instructions dump. */
        public boolean writeData(java.io.File r5) {
            /*
                r4 = this;
                r0 = 0
                r1 = 0
                java.io.BufferedOutputStream r2 = new java.io.BufferedOutputStream     // Catch:{ IOException -> 0x0027, all -> 0x0020 }
                java.io.FileOutputStream r3 = new java.io.FileOutputStream     // Catch:{ IOException -> 0x0027, all -> 0x0020 }
                r3.<init>(r5)     // Catch:{ IOException -> 0x0027, all -> 0x0020 }
                r2.<init>(r3)     // Catch:{ IOException -> 0x0027, all -> 0x0020 }
                android.graphics.Bitmap r5 = r4.mBitmap     // Catch:{ IOException -> 0x001e, all -> 0x001b }
                android.graphics.Bitmap$CompressFormat r1 = android.graphics.Bitmap.CompressFormat.JPEG     // Catch:{ IOException -> 0x001e, all -> 0x001b }
                r3 = 90
                r5.compress(r1, r3, r2)     // Catch:{ IOException -> 0x001e, all -> 0x001b }
                r5 = 1
                r2.close()     // Catch:{ IOException -> 0x002d }
                r0 = 1
                goto L_0x002d
            L_0x001b:
                r5 = move-exception
                r1 = r2
                goto L_0x0021
            L_0x001e:
                r1 = r2
                goto L_0x0028
            L_0x0020:
                r5 = move-exception
            L_0x0021:
                if (r1 == 0) goto L_0x0026
                r1.close()     // Catch:{ IOException -> 0x0026 }
            L_0x0026:
                throw r5
            L_0x0027:
            L_0x0028:
                if (r1 == 0) goto L_0x002d
                r1.close()     // Catch:{ IOException -> 0x002d }
            L_0x002d:
                return r0
            */
            throw new UnsupportedOperationException("Method not decompiled: epson.print.imgsel.BitmapCache.BitmapDataWrite.writeData(java.io.File):boolean");
        }
    }

    public void addFile(File file, DataWriter dataWriter) {
        File cacheFile = getCacheFile(file.toString());
        if (cacheFile != null) {
            long j = 0;
            if (cacheFile.exists()) {
                j = cacheFile.length();
            }
            if (!dataWriter.writeData(cacheFile)) {
                if (cacheFile.exists()) {
                    cacheFile.delete();
                }
                mCacheFileInfoMap.remove(file);
                mTotalCacheSize -= j;
                return;
            }
            long length = cacheFile.length();
            mCacheFileInfoMap.put(file, new FileAttribute(file));
            mTotalCacheSize += length - j;
            trimToSize();
        }
    }

    private File getCacheFile(String str) {
        File file = mCacheDirectory;
        String uuid = UUID.nameUUIDFromBytes(str.getBytes()).toString();
        File file2 = mCacheDirectory;
        return new File(file2, uuid + ".jpg");
    }

    private static class FileAttribute implements Serializable {
        public long mFileSize;
        public long mModifiedTime;

        public FileAttribute(File file) {
            if (file != null) {
                mFileSize = file.length();
                mModifiedTime = file.lastModified();
            }
        }

        public boolean isSameAttribute(long j, long j2) {
            return mFileSize == j && mModifiedTime == j2;
        }

        public boolean isSameAttribute(File file) {
            if (file == null) {
                return false;
            }
            return isSameAttribute(file.length(), file.lastModified());
        }
    }
}
