package com.epson.memcardacc;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.media.ExifInterface;
import com.epson.memcardacc.ImageUtil;
import epson.print.EPImage;
import java.io.File;
import java.io.IOException;

public class BitmapCache {
    private static final String LOG_TAG = "BitmapCache";
    private static BitmapCache sBitmapCacheInstance;
    protected File mCacheDir;
    CacheHashMap<String, File> mCacheHashMap = new CacheHashMap<>();
    private CifsAccess mCifsAccess;
    protected File mTempolaryDownloadDir;
    private int mThumbnailHeight;
    private int mThumbnailWidth;

    public boolean setDirectory(File file, File file2) {
        mCacheDir = file;
        mTempolaryDownloadDir = file2;
        return initEnv();
    }

    private boolean initEnv() {
        if (!checkDirectory(mCacheDir) || !checkDirectory(mTempolaryDownloadDir)) {
            return false;
        }
        new File(mCacheDir, MemcardConfig.MAP_FILE_NAME).exists();
        return true;
    }

    public boolean checkSettings() {
        return checkDirectory(mCacheDir) && checkDirectory(mTempolaryDownloadDir);
    }

    private boolean checkDirectory(File file) {
        if (!file.exists()) {
            if (!file.mkdirs()) {
                return false;
            }
            return true;
        } else if (!file.isDirectory() || !file.canRead() || !file.canWrite()) {
            return false;
        } else {
            return true;
        }
    }

    public void setCifsAccess(CifsAccess cifsAccess) {
        mCifsAccess = cifsAccess;
    }

    private void deleteDir(File file) {
        if (file.exists()) {
            for (File delete : file.listFiles()) {
                delete.delete();
            }
            file.delete();
        }
    }

    public void clearCache() {
        deleteDir(mCacheDir);
        deleteDir(mTempolaryDownloadDir);
    }

    public Bitmap makeThumbnailBitmap(String str, String str2) {
        File file;
        try {
            file = new File(mTempolaryDownloadDir, str);
            try {
                if (mCifsAccess.readFromPrinterMemcard(str2, file.toString()) != 0) {
                    if (file.exists()) {
                        file.delete();
                    }
                    return null;
                }
                int exifRotation = getExifRotation(file);
                int i = mThumbnailWidth;
                int i2 = mThumbnailHeight;
                if (exifRotation == 90 || exifRotation == 270) {
                    i = mThumbnailHeight;
                    i2 = mThumbnailWidth;
                }
                Bitmap makeThumbNail = ImageUtil.makeThumbNail(file, new ImageUtil.WidthHeight(i, i2));
                if (exifRotation == 0) {
                    if (file.exists()) {
                        file.delete();
                    }
                    return makeThumbNail;
                }
                Bitmap rotateBitmap = rotateBitmap(makeThumbNail, exifRotation);
                makeThumbNail.recycle();
                if (file.exists()) {
                    file.delete();
                }
                return rotateBitmap;
            } catch (Exception unused) {
                if (file != null && file.exists()) {
                    file.delete();
                }
                return null;
            } catch (Throwable th) {
                th = th;
                if (file != null && file.exists()) {
                    file.delete();
                }
                throw th;
            }
        } catch (Exception unused2) {
            file = null;
            file.delete();
            return null;
        } catch (Throwable th2) {
            th = th2;
            file = null;
            file.delete();
            throw th;
        }
    }

    private static Bitmap rotateBitmap(Bitmap bitmap, int i) {
        if (bitmap == null) {
            return null;
        }
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        if (width <= 0 || height <= 0) {
            return null;
        }
        Matrix matrix = new Matrix();
        matrix.setRotate((float) i);
        return Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
    }

    private static int getExifRotation(File file) {
        try {
            int attributeInt = new ExifInterface(file.toString()).getAttributeInt(android.support.media.ExifInterface.TAG_ORIENTATION, 1);
            if (attributeInt == 3) {
                return EPImage.EPS_ROTATE_180;
            }
            if (attributeInt == 6) {
                return 90;
            }
            if (attributeInt != 8) {
                return 0;
            }
            return EPImage.EPS_ROTATE_270;
        } catch (IOException unused) {
            return 0;
        }
    }

    /* JADX WARNING: Removed duplicated region for block: B:18:0x003f A[SYNTHETIC, Splitter:B:18:0x003f] */
    /* JADX WARNING: Removed duplicated region for block: B:25:0x004f A[SYNTHETIC, Splitter:B:25:0x004f] */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    private void saveCache(java.lang.String r7, android.graphics.Bitmap r8) {
        /*
            r6 = this;
            if (r8 != 0) goto L_0x0003
            return
        L_0x0003:
            java.util.Locale r0 = java.util.Locale.US
            java.lang.String r1 = "%08x.bin"
            r2 = 1
            java.lang.Object[] r2 = new java.lang.Object[r2]
            r3 = 0
            long r4 = java.lang.System.currentTimeMillis()
            java.lang.Long r4 = java.lang.Long.valueOf(r4)
            r2[r3] = r4
            java.lang.String r0 = java.lang.String.format(r0, r1, r2)
            java.io.File r1 = new java.io.File
            java.io.File r2 = r6.mCacheDir
            r1.<init>(r2, r0)
            r0 = 0
            java.io.FileOutputStream r2 = new java.io.FileOutputStream     // Catch:{ FileNotFoundException -> 0x0039 }
            r2.<init>(r1)     // Catch:{ FileNotFoundException -> 0x0039 }
            android.graphics.Bitmap$CompressFormat r0 = android.graphics.Bitmap.CompressFormat.PNG     // Catch:{ FileNotFoundException -> 0x0034, all -> 0x0031 }
            r3 = 100
            r8.compress(r0, r3, r2)     // Catch:{ FileNotFoundException -> 0x0034, all -> 0x0031 }
            r2.close()     // Catch:{ IOException -> 0x0043 }
            goto L_0x0047
        L_0x0031:
            r7 = move-exception
            r0 = r2
            goto L_0x004d
        L_0x0034:
            r8 = move-exception
            r0 = r2
            goto L_0x003a
        L_0x0037:
            r7 = move-exception
            goto L_0x004d
        L_0x0039:
            r8 = move-exception
        L_0x003a:
            r8.printStackTrace()     // Catch:{ all -> 0x0037 }
            if (r0 == 0) goto L_0x0047
            r0.close()     // Catch:{ IOException -> 0x0043 }
            goto L_0x0047
        L_0x0043:
            r8 = move-exception
            r8.printStackTrace()
        L_0x0047:
            com.epson.memcardacc.CacheHashMap<java.lang.String, java.io.File> r8 = r6.mCacheHashMap
            r8.put(r7, r1)
            return
        L_0x004d:
            if (r0 == 0) goto L_0x0057
            r0.close()     // Catch:{ IOException -> 0x0053 }
            goto L_0x0057
        L_0x0053:
            r8 = move-exception
            r8.printStackTrace()
        L_0x0057:
            throw r7
        */
        throw new UnsupportedOperationException("Method not decompiled: com.epson.memcardacc.BitmapCache.saveCache(java.lang.String, android.graphics.Bitmap):void");
    }

    private Bitmap getBitmapFromCache(String str) {
        File file = (File) mCacheHashMap.get(str);
        if (file == null) {
            return null;
        }
        Bitmap decodeFile = BitmapFactory.decodeFile(file.toString());
        if (decodeFile == null) {
            mCacheHashMap.remove(str);
        }
        return decodeFile;
    }

    public Bitmap cacheOrMakeThumbnailBitmap(String str, String str2) {
        Bitmap bitmapFromCache = getBitmapFromCache(str2);
        if (bitmapFromCache != null) {
            return bitmapFromCache;
        }
        Bitmap makeThumbnailBitmap = makeThumbnailBitmap(str, str2);
        saveCache(str2, makeThumbnailBitmap);
        return makeThumbnailBitmap;
    }

    public static BitmapCache getInstance() {
        if (sBitmapCacheInstance == null) {
            sBitmapCacheInstance = new BitmapCache();
        }
        return sBitmapCacheInstance;
    }

    public void setSize(int i, int i2) {
        mThumbnailWidth = i;
        mThumbnailHeight = i2;
    }
}
