BitmapCache.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. package epson.print.imgsel;
  2. import android.graphics.Bitmap;
  3. import android.graphics.BitmapFactory;
  4. import java.io.File;
  5. import java.io.Serializable;
  6. import java.util.HashSet;
  7. import java.util.LinkedHashMap;
  8. import java.util.Set;
  9. import java.util.UUID;
  10. import java.util.concurrent.Callable;
  11. import java.util.concurrent.ExecutorService;
  12. import java.util.concurrent.LinkedBlockingQueue;
  13. import java.util.concurrent.ThreadPoolExecutor;
  14. import java.util.concurrent.TimeUnit;
  15. public class BitmapCache {
  16. private static final String JOURNAL_FILE_NAME = "journal.bin";
  17. private static final int JPEG_QUALITY = 90;
  18. private File mCacheDirectory;
  19. private LinkedHashMap<File, FileAttribute> mCacheFileInfoMap = new LinkedHashMap<>(32, 0.75f, true);
  20. private long mCacheSizeLimit = 10000000;
  21. private final Callable<Void> mCleanupCallable = new Callable<Void>() {
  22. public Void call() throws Exception {
  23. trimToSize();
  24. return null;
  25. }
  26. };
  27. private final ExecutorService mExecutorService = new ThreadPoolExecutor(0, 1, 60, TimeUnit.SECONDS, new LinkedBlockingQueue());
  28. private long mTotalCacheSize;
  29. public interface DataWriter {
  30. boolean writeData(File file);
  31. }
  32. public boolean initialize(File file, int i) {
  33. mCacheSizeLimit = (long) i;
  34. return initialize(file);
  35. }
  36. public boolean initialize(File file) {
  37. if (!setCacheDirectory(file)) {
  38. return false;
  39. }
  40. restoreJournal();
  41. return true;
  42. }
  43. public boolean setCacheDirectory(File file) {
  44. mCacheDirectory = null;
  45. if (file.exists()) {
  46. if (!file.isDirectory() || !file.canRead()) {
  47. return false;
  48. }
  49. } else if (!file.mkdirs()) {
  50. return false;
  51. }
  52. mCacheDirectory = file;
  53. return true;
  54. }
  55. /* JADX WARNING: Can't wrap try/catch for region: R(4:24|(2:26|27)|28|29) */
  56. /* JADX WARNING: Code restructure failed: missing block: B:23:0x003d, code lost:
  57. if (r3 != null) goto L_0x002b;
  58. */
  59. /* JADX WARNING: Missing exception handler attribute for start block: B:28:0x0046 */
  60. /* JADX WARNING: Removed duplicated region for block: B:26:0x0043 A[SYNTHETIC, Splitter:B:26:0x0043] */
  61. /* JADX WARNING: Removed duplicated region for block: B:33:0x004a A[Catch:{ IOException -> 0x0047, ClassNotFoundException -> 0x0036, all -> 0x0033 }] */
  62. /* JADX WARNING: Removed duplicated region for block: B:36:0x004d A[Catch:{ IOException -> 0x0047, ClassNotFoundException -> 0x0036, all -> 0x0033 }] */
  63. /* Code decompiled incorrectly, please refer to instructions dump. */
  64. private synchronized void restoreJournal() {
  65. /*
  66. r6 = this;
  67. monitor-enter(r6)
  68. java.io.File r0 = r6.getJournalFile() // Catch:{ all -> 0x005b }
  69. boolean r1 = r0.exists() // Catch:{ all -> 0x005b }
  70. if (r1 != 0) goto L_0x0010
  71. r6.clearCacheFileInfoMap() // Catch:{ all -> 0x005b }
  72. monitor-exit(r6)
  73. return
  74. L_0x0010:
  75. r1 = 0
  76. r2 = 0
  77. java.io.ObjectInputStream r3 = new java.io.ObjectInputStream // Catch:{ IOException -> 0x0047, ClassNotFoundException -> 0x0036, all -> 0x0033 }
  78. java.io.FileInputStream r4 = new java.io.FileInputStream // Catch:{ IOException -> 0x0047, ClassNotFoundException -> 0x0036, all -> 0x0033 }
  79. r4.<init>(r0) // Catch:{ IOException -> 0x0047, ClassNotFoundException -> 0x0036, all -> 0x0033 }
  80. r3.<init>(r4) // Catch:{ IOException -> 0x0047, ClassNotFoundException -> 0x0036, all -> 0x0033 }
  81. java.lang.Object r1 = r3.readObject() // Catch:{ IOException -> 0x0031, ClassNotFoundException -> 0x002f }
  82. java.util.LinkedHashMap r1 = (java.util.LinkedHashMap) r1 // Catch:{ IOException -> 0x0031, ClassNotFoundException -> 0x002f }
  83. r6.mCacheFileInfoMap = r1 // Catch:{ IOException -> 0x0031, ClassNotFoundException -> 0x002f }
  84. r6.calcTotalSizeAndDeleteNotExistEntry() // Catch:{ IOException -> 0x0031, ClassNotFoundException -> 0x002f }
  85. r6.deleteNoEntryCacheFile() // Catch:{ IOException -> 0x0031, ClassNotFoundException -> 0x002f }
  86. r2 = 1
  87. L_0x002b:
  88. r3.close() // Catch:{ IOException -> 0x004b }
  89. goto L_0x004b
  90. L_0x002f:
  91. r1 = move-exception
  92. goto L_0x003a
  93. L_0x0031:
  94. goto L_0x0048
  95. L_0x0033:
  96. r0 = move-exception
  97. r3 = r1
  98. goto L_0x0041
  99. L_0x0036:
  100. r3 = move-exception
  101. r5 = r3
  102. r3 = r1
  103. r1 = r5
  104. L_0x003a:
  105. r1.printStackTrace() // Catch:{ all -> 0x0040 }
  106. if (r3 == 0) goto L_0x004b
  107. goto L_0x002b
  108. L_0x0040:
  109. r0 = move-exception
  110. L_0x0041:
  111. if (r3 == 0) goto L_0x0046
  112. r3.close() // Catch:{ IOException -> 0x0046 }
  113. L_0x0046:
  114. throw r0 // Catch:{ all -> 0x005b }
  115. L_0x0047:
  116. r3 = r1
  117. L_0x0048:
  118. if (r3 == 0) goto L_0x004b
  119. goto L_0x002b
  120. L_0x004b:
  121. if (r2 != 0) goto L_0x0059
  122. boolean r1 = r0.exists() // Catch:{ all -> 0x005b }
  123. if (r1 == 0) goto L_0x0056
  124. r0.delete() // Catch:{ all -> 0x005b }
  125. L_0x0056:
  126. r6.clearCacheFileInfoMap() // Catch:{ all -> 0x005b }
  127. L_0x0059:
  128. monitor-exit(r6)
  129. return
  130. L_0x005b:
  131. r0 = move-exception
  132. monitor-exit(r6)
  133. throw r0
  134. */
  135. throw new UnsupportedOperationException("Method not decompiled: epson.print.imgsel.BitmapCache.restoreJournal():void");
  136. }
  137. private synchronized void calcTotalSizeAndDeleteNotExistEntry() {
  138. mTotalCacheSize = 0;
  139. Set<File> keySet = mCacheFileInfoMap.keySet();
  140. HashSet<File> hashSet = new HashSet<>();
  141. for (File next : keySet) {
  142. File cacheFile = getCacheFile(next.toString());
  143. if (!cacheFile.exists()) {
  144. hashSet.add(next);
  145. } else {
  146. mTotalCacheSize += cacheFile.length();
  147. }
  148. }
  149. for (File remove : hashSet) {
  150. mCacheFileInfoMap.remove(remove);
  151. }
  152. trimToSize();
  153. }
  154. private synchronized void deleteNoEntryCacheFile() {
  155. if (mCacheDirectory == null) {
  156. Set<File> keySet = mCacheFileInfoMap.keySet();
  157. HashSet hashSet = new HashSet(keySet.size());
  158. for (File file : keySet) {
  159. hashSet.add(getCacheFile(file.toString()));
  160. }
  161. for (File file2 : mCacheDirectory.listFiles()) {
  162. if (!hashSet.contains(file2)) {
  163. file2.delete();
  164. }
  165. }
  166. }
  167. }
  168. public synchronized void trimToSize() {
  169. while (mTotalCacheSize > mCacheSizeLimit) {
  170. removeCacheFile(mCacheFileInfoMap.keySet().iterator().next());
  171. }
  172. }
  173. public synchronized void removeCacheFile(File file) {
  174. File cacheFile = getCacheFile(file.toString());
  175. long j = 0;
  176. if (cacheFile != null) {
  177. j = cacheFile.length();
  178. cacheFile.delete();
  179. }
  180. mTotalCacheSize -= j;
  181. mCacheFileInfoMap.remove(file);
  182. }
  183. private synchronized void clearCacheFileInfoMap() {
  184. mCacheFileInfoMap.clear();
  185. mTotalCacheSize = 0;
  186. if (mCacheDirectory != null) {
  187. for (File delete : mCacheDirectory.listFiles()) {
  188. delete.delete();
  189. }
  190. }
  191. }
  192. /* JADX WARNING: Can't wrap try/catch for region: R(4:15|(2:32|33)|34|35) */
  193. /* JADX WARNING: Missing exception handler attribute for start block: B:34:0x005c */
  194. /* JADX WARNING: Removed duplicated region for block: B:21:0x0047 A[SYNTHETIC, Splitter:B:21:0x0047] */
  195. /* JADX WARNING: Removed duplicated region for block: B:25:0x004c A[SYNTHETIC, Splitter:B:25:0x004c] */
  196. /* JADX WARNING: Removed duplicated region for block: B:32:0x0059 A[SYNTHETIC, Splitter:B:32:0x0059] */
  197. /* Code decompiled incorrectly, please refer to instructions dump. */
  198. public synchronized void saveJournal() {
  199. /*
  200. r8 = this;
  201. monitor-enter(r8)
  202. java.io.File r0 = r8.getJournalFile() // Catch:{ all -> 0x005d }
  203. r1 = 0
  204. r2 = 0
  205. java.io.ObjectOutputStream r3 = new java.io.ObjectOutputStream // Catch:{ IOException -> 0x0025 }
  206. java.io.FileOutputStream r4 = new java.io.FileOutputStream // Catch:{ IOException -> 0x0025 }
  207. r4.<init>(r0) // Catch:{ IOException -> 0x0025 }
  208. r3.<init>(r4) // Catch:{ IOException -> 0x0025 }
  209. java.util.LinkedHashMap<java.io.File, epson.print.imgsel.BitmapCache$FileAttribute> r1 = r8.mCacheFileInfoMap // Catch:{ IOException -> 0x001e, all -> 0x001b }
  210. r3.writeObject(r1) // Catch:{ IOException -> 0x001e, all -> 0x001b }
  211. r2 = 1
  212. r3.close() // Catch:{ IOException -> 0x004a }
  213. goto L_0x004a
  214. L_0x001b:
  215. r0 = move-exception
  216. r1 = r3
  217. goto L_0x0057
  218. L_0x001e:
  219. r1 = move-exception
  220. r7 = r3
  221. r3 = r1
  222. r1 = r7
  223. goto L_0x0026
  224. L_0x0023:
  225. r0 = move-exception
  226. goto L_0x0057
  227. L_0x0025:
  228. r3 = move-exception
  229. L_0x0026:
  230. java.lang.String r4 = "BitmapCache"
  231. java.lang.StringBuilder r5 = new java.lang.StringBuilder // Catch:{ all -> 0x0023 }
  232. r5.<init>() // Catch:{ all -> 0x0023 }
  233. java.lang.String r6 = "in saveJournal() exception <"
  234. r5.append(r6) // Catch:{ all -> 0x0023 }
  235. java.lang.String r3 = r3.toString() // Catch:{ all -> 0x0023 }
  236. r5.append(r3) // Catch:{ all -> 0x0023 }
  237. java.lang.String r3 = ">"
  238. r5.append(r3) // Catch:{ all -> 0x0023 }
  239. java.lang.String r3 = r5.toString() // Catch:{ all -> 0x0023 }
  240. android.util.Log.v(r4, r3) // Catch:{ all -> 0x0023 }
  241. if (r1 == 0) goto L_0x004a
  242. r1.close() // Catch:{ IOException -> 0x004a }
  243. L_0x004a:
  244. if (r2 != 0) goto L_0x0055
  245. boolean r1 = r0.exists() // Catch:{ all -> 0x005d }
  246. if (r1 == 0) goto L_0x0055
  247. r0.delete() // Catch:{ all -> 0x005d }
  248. L_0x0055:
  249. monitor-exit(r8)
  250. return
  251. L_0x0057:
  252. if (r1 == 0) goto L_0x005c
  253. r1.close() // Catch:{ IOException -> 0x005c }
  254. L_0x005c:
  255. throw r0 // Catch:{ all -> 0x005d }
  256. L_0x005d:
  257. r0 = move-exception
  258. monitor-exit(r8)
  259. throw r0
  260. */
  261. throw new UnsupportedOperationException("Method not decompiled: epson.print.imgsel.BitmapCache.saveJournal():void");
  262. }
  263. private File getJournalFile() {
  264. return new File(mCacheDirectory, JOURNAL_FILE_NAME);
  265. }
  266. public synchronized Bitmap getBitmap(File file) {
  267. File file2 = getFile(file);
  268. if (file2 == null) {
  269. return null;
  270. }
  271. return BitmapFactory.decodeFile(file2.toString());
  272. }
  273. public synchronized File getFile(File file) {
  274. FileAttribute fileAttribute = mCacheFileInfoMap.get(file);
  275. if (fileAttribute == null) {
  276. return null;
  277. }
  278. if (fileAttribute.isSameAttribute(file)) {
  279. File cacheFile = getCacheFile(file.toString());
  280. if (cacheFile.exists()) {
  281. return cacheFile;
  282. }
  283. calcTotalSizeAndDeleteNotExistEntry();
  284. return null;
  285. }
  286. removeCacheFile(file);
  287. return null;
  288. }
  289. public void addBitmap(File file, Bitmap bitmap) {
  290. addFile(file, new BitmapDataWrite(bitmap));
  291. }
  292. static class BitmapDataWrite implements DataWriter {
  293. private Bitmap mBitmap;
  294. public BitmapDataWrite(Bitmap bitmap) {
  295. mBitmap = bitmap;
  296. }
  297. /* JADX WARNING: Removed duplicated region for block: B:15:0x0023 A[SYNTHETIC, Splitter:B:15:0x0023] */
  298. /* JADX WARNING: Removed duplicated region for block: B:21:0x002a A[SYNTHETIC, Splitter:B:21:0x002a] */
  299. /* JADX WARNING: Removed duplicated region for block: B:26:? A[RETURN, SYNTHETIC] */
  300. /* Code decompiled incorrectly, please refer to instructions dump. */
  301. public boolean writeData(java.io.File r5) {
  302. /*
  303. r4 = this;
  304. r0 = 0
  305. r1 = 0
  306. java.io.BufferedOutputStream r2 = new java.io.BufferedOutputStream // Catch:{ IOException -> 0x0027, all -> 0x0020 }
  307. java.io.FileOutputStream r3 = new java.io.FileOutputStream // Catch:{ IOException -> 0x0027, all -> 0x0020 }
  308. r3.<init>(r5) // Catch:{ IOException -> 0x0027, all -> 0x0020 }
  309. r2.<init>(r3) // Catch:{ IOException -> 0x0027, all -> 0x0020 }
  310. android.graphics.Bitmap r5 = r4.mBitmap // Catch:{ IOException -> 0x001e, all -> 0x001b }
  311. android.graphics.Bitmap$CompressFormat r1 = android.graphics.Bitmap.CompressFormat.JPEG // Catch:{ IOException -> 0x001e, all -> 0x001b }
  312. r3 = 90
  313. r5.compress(r1, r3, r2) // Catch:{ IOException -> 0x001e, all -> 0x001b }
  314. r5 = 1
  315. r2.close() // Catch:{ IOException -> 0x002d }
  316. r0 = 1
  317. goto L_0x002d
  318. L_0x001b:
  319. r5 = move-exception
  320. r1 = r2
  321. goto L_0x0021
  322. L_0x001e:
  323. r1 = r2
  324. goto L_0x0028
  325. L_0x0020:
  326. r5 = move-exception
  327. L_0x0021:
  328. if (r1 == 0) goto L_0x0026
  329. r1.close() // Catch:{ IOException -> 0x0026 }
  330. L_0x0026:
  331. throw r5
  332. L_0x0027:
  333. L_0x0028:
  334. if (r1 == 0) goto L_0x002d
  335. r1.close() // Catch:{ IOException -> 0x002d }
  336. L_0x002d:
  337. return r0
  338. */
  339. throw new UnsupportedOperationException("Method not decompiled: epson.print.imgsel.BitmapCache.BitmapDataWrite.writeData(java.io.File):boolean");
  340. }
  341. }
  342. public void addFile(File file, DataWriter dataWriter) {
  343. File cacheFile = getCacheFile(file.toString());
  344. if (cacheFile != null) {
  345. long j = 0;
  346. if (cacheFile.exists()) {
  347. j = cacheFile.length();
  348. }
  349. if (!dataWriter.writeData(cacheFile)) {
  350. if (cacheFile.exists()) {
  351. cacheFile.delete();
  352. }
  353. mCacheFileInfoMap.remove(file);
  354. mTotalCacheSize -= j;
  355. return;
  356. }
  357. long length = cacheFile.length();
  358. mCacheFileInfoMap.put(file, new FileAttribute(file));
  359. mTotalCacheSize += length - j;
  360. trimToSize();
  361. }
  362. }
  363. private File getCacheFile(String str) {
  364. File file = mCacheDirectory;
  365. String uuid = UUID.nameUUIDFromBytes(str.getBytes()).toString();
  366. File file2 = mCacheDirectory;
  367. return new File(file2, uuid + ".jpg");
  368. }
  369. private static class FileAttribute implements Serializable {
  370. public long mFileSize;
  371. public long mModifiedTime;
  372. public FileAttribute(File file) {
  373. if (file != null) {
  374. mFileSize = file.length();
  375. mModifiedTime = file.lastModified();
  376. }
  377. }
  378. public boolean isSameAttribute(long j, long j2) {
  379. return mFileSize == j && mModifiedTime == j2;
  380. }
  381. public boolean isSameAttribute(File file) {
  382. if (file == null) {
  383. return false;
  384. }
  385. return isSameAttribute(file.length(), file.lastModified());
  386. }
  387. }
  388. }