123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415 |
- 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 {
- 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 (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 (mTotalCacheSize > mCacheSizeLimit) {
- removeCacheFile(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 (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(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());
- }
- }
- }
|