BitmapCache.java 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. package com.epson.memcardacc;
  2. import android.graphics.Bitmap;
  3. import android.graphics.BitmapFactory;
  4. import android.graphics.Matrix;
  5. import android.media.ExifInterface;
  6. import java.io.File;
  7. import java.io.IOException;
  8. import epson.print.EPImage;
  9. public class BitmapCache {
  10. private static final String LOG_TAG = "BitmapCache";
  11. private static BitmapCache sBitmapCacheInstance;
  12. protected File mCacheDir;
  13. CacheHashMap<String, File> mCacheHashMap = new CacheHashMap<>();
  14. private CifsAccess mCifsAccess;
  15. protected File mTempolaryDownloadDir;
  16. private int mThumbnailHeight;
  17. private int mThumbnailWidth;
  18. public boolean setDirectory(File file, File file2) {
  19. mCacheDir = file;
  20. mTempolaryDownloadDir = file2;
  21. return initEnv();
  22. }
  23. private boolean initEnv() {
  24. if (!checkDirectory(mCacheDir) || !checkDirectory(mTempolaryDownloadDir)) {
  25. return false;
  26. }
  27. new File(mCacheDir, MemcardConfig.MAP_FILE_NAME).exists();
  28. return true;
  29. }
  30. public boolean checkSettings() {
  31. return checkDirectory(mCacheDir) && checkDirectory(mTempolaryDownloadDir);
  32. }
  33. private boolean checkDirectory(File file) {
  34. if (!file.exists()) {
  35. if (!file.mkdirs()) {
  36. return false;
  37. }
  38. return true;
  39. } else if (!file.isDirectory() || !file.canRead() || !file.canWrite()) {
  40. return false;
  41. } else {
  42. return true;
  43. }
  44. }
  45. public void setCifsAccess(CifsAccess cifsAccess) {
  46. mCifsAccess = cifsAccess;
  47. }
  48. private void deleteDir(File file) {
  49. if (file.exists()) {
  50. for (File delete : file.listFiles()) {
  51. delete.delete();
  52. }
  53. file.delete();
  54. }
  55. }
  56. public void clearCache() {
  57. deleteDir(mCacheDir);
  58. deleteDir(mTempolaryDownloadDir);
  59. }
  60. public Bitmap makeThumbnailBitmap(String str, String str2) {
  61. File file;
  62. try {
  63. file = new File(mTempolaryDownloadDir, str);
  64. try {
  65. if (mCifsAccess.readFromPrinterMemcard(str2, file.toString()) != 0) {
  66. if (file.exists()) {
  67. file.delete();
  68. }
  69. return null;
  70. }
  71. int exifRotation = getExifRotation(file);
  72. int i = mThumbnailWidth;
  73. int i2 = mThumbnailHeight;
  74. if (exifRotation == 90 || exifRotation == 270) {
  75. i = mThumbnailHeight;
  76. i2 = mThumbnailWidth;
  77. }
  78. Bitmap makeThumbNail = ImageUtil.makeThumbNail(file, new ImageUtil.WidthHeight(i, i2));
  79. if (exifRotation == 0) {
  80. if (file.exists()) {
  81. file.delete();
  82. }
  83. return makeThumbNail;
  84. }
  85. Bitmap rotateBitmap = rotateBitmap(makeThumbNail, exifRotation);
  86. makeThumbNail.recycle();
  87. if (file.exists()) {
  88. file.delete();
  89. }
  90. return rotateBitmap;
  91. } catch (Exception unused) {
  92. if (file != null && file.exists()) {
  93. file.delete();
  94. }
  95. return null;
  96. } catch (Throwable th) {
  97. th = th;
  98. if (file != null && file.exists()) {
  99. file.delete();
  100. }
  101. throw th;
  102. }
  103. } catch (Exception unused2) {
  104. file = null;
  105. file.delete();
  106. return null;
  107. } catch (Throwable th2) {
  108. th = th2;
  109. file = null;
  110. file.delete();
  111. throw th;
  112. }
  113. }
  114. private static Bitmap rotateBitmap(Bitmap bitmap, int i) {
  115. if (bitmap == null) {
  116. return null;
  117. }
  118. int width = bitmap.getWidth();
  119. int height = bitmap.getHeight();
  120. if (width <= 0 || height <= 0) {
  121. return null;
  122. }
  123. Matrix matrix = new Matrix();
  124. matrix.setRotate((float) i);
  125. return Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
  126. }
  127. private static int getExifRotation(File file) {
  128. try {
  129. int attributeInt = new ExifInterface(file.toString()).getAttributeInt(android.support.media.ExifInterface.TAG_ORIENTATION, 1);
  130. if (attributeInt == 3) {
  131. return EPImage.EPS_ROTATE_180;
  132. }
  133. if (attributeInt == 6) {
  134. return 90;
  135. }
  136. if (attributeInt != 8) {
  137. return 0;
  138. }
  139. return EPImage.EPS_ROTATE_270;
  140. } catch (IOException unused) {
  141. return 0;
  142. }
  143. }
  144. /* JADX WARNING: Removed duplicated region for block: B:18:0x003f A[SYNTHETIC, Splitter:B:18:0x003f] */
  145. /* JADX WARNING: Removed duplicated region for block: B:25:0x004f A[SYNTHETIC, Splitter:B:25:0x004f] */
  146. /* Code decompiled incorrectly, please refer to instructions dump. */
  147. private void saveCache(java.lang.String r7, android.graphics.Bitmap r8) {
  148. /*
  149. r6 = this;
  150. if (r8 != 0) goto L_0x0003
  151. return
  152. L_0x0003:
  153. java.util.Locale r0 = java.util.Locale.US
  154. java.lang.String r1 = "%08x.bin"
  155. r2 = 1
  156. java.lang.Object[] r2 = new java.lang.Object[r2]
  157. r3 = 0
  158. long r4 = java.lang.System.currentTimeMillis()
  159. java.lang.Long r4 = java.lang.Long.valueOf(r4)
  160. r2[r3] = r4
  161. java.lang.String r0 = java.lang.String.format(r0, r1, r2)
  162. java.io.File r1 = new java.io.File
  163. java.io.File r2 = r6.mCacheDir
  164. r1.<init>(r2, r0)
  165. r0 = 0
  166. java.io.FileOutputStream r2 = new java.io.FileOutputStream // Catch:{ FileNotFoundException -> 0x0039 }
  167. r2.<init>(r1) // Catch:{ FileNotFoundException -> 0x0039 }
  168. android.graphics.Bitmap$CompressFormat r0 = android.graphics.Bitmap.CompressFormat.PNG // Catch:{ FileNotFoundException -> 0x0034, all -> 0x0031 }
  169. r3 = 100
  170. r8.compress(r0, r3, r2) // Catch:{ FileNotFoundException -> 0x0034, all -> 0x0031 }
  171. r2.close() // Catch:{ IOException -> 0x0043 }
  172. goto L_0x0047
  173. L_0x0031:
  174. r7 = move-exception
  175. r0 = r2
  176. goto L_0x004d
  177. L_0x0034:
  178. r8 = move-exception
  179. r0 = r2
  180. goto L_0x003a
  181. L_0x0037:
  182. r7 = move-exception
  183. goto L_0x004d
  184. L_0x0039:
  185. r8 = move-exception
  186. L_0x003a:
  187. r8.printStackTrace() // Catch:{ all -> 0x0037 }
  188. if (r0 == 0) goto L_0x0047
  189. r0.close() // Catch:{ IOException -> 0x0043 }
  190. goto L_0x0047
  191. L_0x0043:
  192. r8 = move-exception
  193. r8.printStackTrace()
  194. L_0x0047:
  195. com.epson.memcardacc.CacheHashMap<java.lang.String, java.io.File> r8 = r6.mCacheHashMap
  196. r8.put(r7, r1)
  197. return
  198. L_0x004d:
  199. if (r0 == 0) goto L_0x0057
  200. r0.close() // Catch:{ IOException -> 0x0053 }
  201. goto L_0x0057
  202. L_0x0053:
  203. r8 = move-exception
  204. r8.printStackTrace()
  205. L_0x0057:
  206. throw r7
  207. */
  208. throw new UnsupportedOperationException("Method not decompiled: com.epson.memcardacc.BitmapCache.saveCache(java.lang.String, android.graphics.Bitmap):void");
  209. }
  210. private Bitmap getBitmapFromCache(String str) {
  211. File file = (File) mCacheHashMap.get(str);
  212. if (file == null) {
  213. return null;
  214. }
  215. Bitmap decodeFile = BitmapFactory.decodeFile(file.toString());
  216. if (decodeFile == null) {
  217. mCacheHashMap.remove(str);
  218. }
  219. return decodeFile;
  220. }
  221. public Bitmap cacheOrMakeThumbnailBitmap(String str, String str2) {
  222. Bitmap bitmapFromCache = getBitmapFromCache(str2);
  223. if (bitmapFromCache != null) {
  224. return bitmapFromCache;
  225. }
  226. Bitmap makeThumbnailBitmap = makeThumbnailBitmap(str, str2);
  227. saveCache(str2, makeThumbnailBitmap);
  228. return makeThumbnailBitmap;
  229. }
  230. public static BitmapCache getInstance() {
  231. if (sBitmapCacheInstance == null) {
  232. sBitmapCacheInstance = new BitmapCache();
  233. }
  234. return sBitmapCacheInstance;
  235. }
  236. public void setSize(int i, int i2) {
  237. mThumbnailWidth = i;
  238. mThumbnailHeight = i2;
  239. }
  240. }