package epson.print.imgsel;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import epson.common.Constants;
import epson.print.IprintApplication;
import epson.print.Util.EPLog;
import java.io.File;

public class AltThumbnailCache extends File {
    public static final String CACHE_DIR_NAME = "thumbnail";
    private static final boolean DEFAULT_USE_MEDIASTORE_THUMBNAIL = true;
    private static final String TAG = "ThumbnailCache";
    private static final int TARGET_SIZE_MICRO_THUMBNAIL = 96;
    static boolean bUseMediaStoreThumbnail = true;
    private static final long serialVersionUID = 1;
    long modified = -1;
    File realObject = null;

    public static void initThumbnailCache(Context context) {
        initCacheDirectory();
        bUseMediaStoreThumbnail = loadUseMediaStoreValue(context);
    }

    public static void setThumbnailCreateMethod(Context context, boolean z) {
        bUseMediaStoreThumbnail = z;
        saveUseMediaStoreValue(context, bUseMediaStoreThumbnail);
        deleteCacheDirectory();
        initCacheDirectory();
    }

    protected static boolean loadUseMediaStoreValue(Context context) {
        return getThumbnailCacheSharedPreferences(context).getBoolean(Constants.USE_MEDIASTORE_THUMBNAIL, true);
    }

    protected static void saveUseMediaStoreValue(Context context, boolean z) {
        SharedPreferences.Editor edit = getThumbnailCacheSharedPreferences(context).edit();
        edit.putBoolean(Constants.USE_MEDIASTORE_THUMBNAIL, z);
        edit.commit();
    }

    protected static SharedPreferences getThumbnailCacheSharedPreferences(Context context) {
        return context.getSharedPreferences(Constants.PREFS_PHOTO, 0);
    }

    public static boolean isUseMediaStoreThumbnail() {
        return bUseMediaStoreThumbnail;
    }

    public AltThumbnailCache(String str) {
        super(str);
        if (!exists()) {
            return;
        }
        if (isDirectory()) {
            for (File file : listFiles()) {
                if (!file.isDirectory()) {
                    this.realObject = file;
                    try {
                        this.modified = Long.parseLong(file.getName());
                    } catch (NumberFormatException e) {
                        EPLog.e(TAG, e.getMessage());
                    }
                }
            }
        } else if (isFile()) {
            delete();
        }
    }

    public boolean setLastModified(long j) {
        delete();
        this.modified = j;
        this.realObject = new File(getPath(), String.valueOf(j));
        return true;
    }

    public long lastModified() {
        return this.modified;
    }

    public String getCachePath() {
        if (!exists()) {
            mkdir();
        }
        return this.realObject.getPath();
    }

    static File getCacheDirectory() {
        return new File(IprintApplication.getInstance().getExternalCacheDir(), CACHE_DIR_NAME);
    }

    public static void initCacheDirectory() {
        File cacheDirectory = getCacheDirectory();
        if (!cacheDirectory.exists()) {
            cacheDirectory.mkdirs();
        }
    }

    public static void deleteCacheDirectory() {
        File cacheDirectory = getCacheDirectory();
        if (cacheDirectory.exists()) {
            deleteDirectory(cacheDirectory);
        }
    }

    public static void deleteDirectory(File file) {
        if (file.exists()) {
            if (!file.isDirectory()) {
                file.delete();
                return;
            }
            for (File file2 : file.listFiles()) {
                if (file2.isDirectory()) {
                    deleteDirectory(file2);
                }
                if (file2.exists()) {
                    file2.delete();
                }
            }
        }
    }

    /* JADX WARNING: Removed duplicated region for block: B:14:0x004e A[Catch:{ OutOfMemoryError -> 0x0073, Exception -> 0x0071 }] */
    /* JADX WARNING: Removed duplicated region for block: B:18:0x005f  */
    /* JADX WARNING: Removed duplicated region for block: B:32:0x007e  */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    public static android.graphics.Bitmap createThumbnail(java.lang.String r8, android.content.ContentResolver r9, long r10) throws java.io.IOException {
        /*
            java.io.File r0 = new java.io.File
            r0.<init>(r8)
            java.io.File r1 = new java.io.File
            java.io.File r2 = getCacheDirectory()
            int r3 = r8.hashCode()
            java.lang.String r3 = java.lang.String.valueOf(r3)
            r1.<init>(r2, r3)
            java.lang.String r1 = r1.toString()
            epson.print.imgsel.AltThumbnailCache r2 = new epson.print.imgsel.AltThumbnailCache
            r2.<init>(r1)
            r1 = 0
            boolean r3 = r2.exists()     // Catch:{ OutOfMemoryError -> 0x0078, Exception -> 0x0076 }
            if (r3 == 0) goto L_0x0049
            long r3 = r2.lastModified()     // Catch:{ OutOfMemoryError -> 0x0078, Exception -> 0x0076 }
            long r5 = r0.lastModified()     // Catch:{ OutOfMemoryError -> 0x0078, Exception -> 0x0076 }
            int r7 = (r3 > r5 ? 1 : (r3 == r5 ? 0 : -1))
            if (r7 != 0) goto L_0x0046
            android.graphics.BitmapFactory$Options r3 = new android.graphics.BitmapFactory$Options     // Catch:{ OutOfMemoryError -> 0x0078, Exception -> 0x0076 }
            r3.<init>()     // Catch:{ OutOfMemoryError -> 0x0078, Exception -> 0x0076 }
            android.graphics.Bitmap$Config r4 = android.graphics.Bitmap.Config.RGB_565     // Catch:{ OutOfMemoryError -> 0x0078, Exception -> 0x0076 }
            r3.inPreferredConfig = r4     // Catch:{ OutOfMemoryError -> 0x0078, Exception -> 0x0076 }
            java.lang.String r4 = r2.getCachePath()     // Catch:{ OutOfMemoryError -> 0x0078, Exception -> 0x0076 }
            android.graphics.Bitmap r3 = android.graphics.BitmapFactory.decodeFile(r4, r3)     // Catch:{ OutOfMemoryError -> 0x0078, Exception -> 0x0076 }
            if (r3 == 0) goto L_0x004a
            return r3
        L_0x0046:
            r2.delete()     // Catch:{ OutOfMemoryError -> 0x0078, Exception -> 0x0076 }
        L_0x0049:
            r3 = r1
        L_0x004a:
            boolean r4 = bUseMediaStoreThumbnail     // Catch:{ OutOfMemoryError -> 0x0073, Exception -> 0x0071 }
            if (r4 == 0) goto L_0x005f
            r4 = 3
            android.graphics.Bitmap r1 = android.provider.MediaStore.Images.Thumbnails.getThumbnail(r9, r10, r4, r1)     // Catch:{ OutOfMemoryError -> 0x0073, Exception -> 0x0071 }
            if (r1 == 0) goto L_0x007c
            epson.print.EPImageUtil r9 = new epson.print.EPImageUtil     // Catch:{ OutOfMemoryError -> 0x0078, Exception -> 0x0076 }
            r9.<init>()     // Catch:{ OutOfMemoryError -> 0x0078, Exception -> 0x0076 }
            android.graphics.Bitmap r1 = r9.check_rotate(r8, r1)     // Catch:{ OutOfMemoryError -> 0x0078, Exception -> 0x0076 }
            goto L_0x007c
        L_0x005f:
            r9 = 96
            android.graphics.Bitmap r1 = createThumbnail(r8, r9)     // Catch:{ OutOfMemoryError -> 0x0073, Exception -> 0x0071 }
            if (r1 == 0) goto L_0x007c
            epson.print.EPImageUtil r9 = new epson.print.EPImageUtil     // Catch:{ OutOfMemoryError -> 0x0078, Exception -> 0x0076 }
            r9.<init>()     // Catch:{ OutOfMemoryError -> 0x0078, Exception -> 0x0076 }
            android.graphics.Bitmap r1 = r9.check_rotate(r8, r1)     // Catch:{ OutOfMemoryError -> 0x0078, Exception -> 0x0076 }
            goto L_0x007c
        L_0x0071:
            r1 = r3
            goto L_0x007c
        L_0x0073:
            r8 = move-exception
            r1 = r3
            goto L_0x0079
        L_0x0076:
            goto L_0x007c
        L_0x0078:
            r8 = move-exception
        L_0x0079:
            r8.printStackTrace()
        L_0x007c:
            if (r1 == 0) goto L_0x009b
            long r8 = r0.lastModified()
            r2.setLastModified(r8)
            java.io.FileOutputStream r8 = new java.io.FileOutputStream
            java.lang.String r9 = r2.getCachePath()
            r8.<init>(r9)
            android.graphics.Bitmap$CompressFormat r9 = android.graphics.Bitmap.CompressFormat.JPEG
            r10 = 80
            r1.compress(r9, r10, r8)
            r8.flush()
            r8.close()
        L_0x009b:
            return r1
        */
        throw new UnsupportedOperationException("Method not decompiled: epson.print.imgsel.AltThumbnailCache.createThumbnail(java.lang.String, android.content.ContentResolver, long):android.graphics.Bitmap");
    }

    public static Bitmap createThumbnail(String str, int i) {
        Bitmap bitmap;
        try {
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            BitmapFactory.decodeFile(str, options);
            if (options.outWidth != 0) {
                if (options.outHeight != 0) {
                    int min = Math.min(options.outWidth / i, options.outHeight / i);
                    BitmapFactory.Options options2 = new BitmapFactory.Options();
                    options2.inPreferredConfig = Bitmap.Config.RGB_565;
                    options2.inSampleSize = min;
                    bitmap = BitmapFactory.decodeFile(str, options2);
                    if (bitmap == null) {
                        return null;
                    }
                    try {
                        int min2 = Math.min(bitmap.getWidth(), bitmap.getHeight());
                        float f = ((float) i) / ((float) min2);
                        Matrix matrix = new Matrix();
                        matrix.postScale(f, f);
                        return Bitmap.createBitmap(bitmap, (bitmap.getWidth() - min2) / 2, (bitmap.getHeight() - min2) / 2, min2, min2, matrix, true);
                    } catch (OutOfMemoryError e) {
                        e = e;
                        e.printStackTrace();
                        return bitmap;
                    }
                }
            }
            return null;
        } catch (OutOfMemoryError e2) {
            e = e2;
            bitmap = null;
            e.printStackTrace();
            return bitmap;
        }
    }
}
