MediaStoreImageFinder.java 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. package epson.print.imgsel;
  2. import android.content.ContentResolver;
  3. import android.database.Cursor;
  4. import android.provider.MediaStore;
  5. import epson.print.Util.EPLog;
  6. import epson.print.imgsel.ImageFinder;
  7. import epson.print.imgsel.ImageFolderInfo;
  8. import java.io.File;
  9. import java.io.IOException;
  10. import java.util.ArrayList;
  11. import java.util.Collection;
  12. import java.util.Collections;
  13. import java.util.HashMap;
  14. import java.util.List;
  15. import java.util.Map;
  16. public abstract class MediaStoreImageFinder implements ImageFinder {
  17. private static final String TAG = "MediaStoreImageFinder";
  18. /* access modifiers changed from: protected */
  19. public abstract boolean myCheckFile(File file);
  20. public Collection<ImageFolderInfo> getFolderPhotoList(ImageFinder.Canceller canceller, ContentResolver contentResolver) {
  21. try {
  22. Cursor myQuery = myQuery(contentResolver);
  23. if (myQuery == null) {
  24. return null;
  25. }
  26. try {
  27. return getImageDirInfo(myQuery, canceller);
  28. } catch (Exception unused) {
  29. return null;
  30. } finally {
  31. myQuery.close();
  32. }
  33. } catch (Exception unused2) {
  34. return null;
  35. }
  36. }
  37. private List<ImageFolderInfo> getImageDirInfo(Cursor cursor, ImageFinder.Canceller canceller) {
  38. EPLog.d(TAG, "Enter getImageDirInfo");
  39. long currentTimeMillis = System.currentTimeMillis();
  40. int columnIndexOrThrow = cursor.getColumnIndexOrThrow("_data");
  41. int columnIndexOrThrow2 = cursor.getColumnIndexOrThrow("_id");
  42. if (!cursor.moveToFirst()) {
  43. return null;
  44. }
  45. HashMap hashMap = new HashMap();
  46. int i = 0;
  47. while (!canceller.checkCanceled()) {
  48. String string = cursor.getString(columnIndexOrThrow);
  49. if (string != null) {
  50. long j = cursor.getLong(columnIndexOrThrow2);
  51. try {
  52. File absoluteFile = new File(string).getAbsoluteFile();
  53. String parent = absoluteFile.getParent();
  54. if (parent != null) {
  55. if (myCheckFile(absoluteFile)) {
  56. ImageFolderInfo imageFolderInfo = (ImageFolderInfo) hashMap.get(parent);
  57. if (imageFolderInfo == null) {
  58. imageFolderInfo = new ImageFolderInfo(parent);
  59. hashMap.put(parent, imageFolderInfo);
  60. }
  61. imageFolderInfo.addImageLastIfPossible(j, absoluteFile.toString(), i);
  62. }
  63. i++;
  64. } else {
  65. throw new IOException();
  66. }
  67. } catch (IOException e) {
  68. e.printStackTrace();
  69. }
  70. }
  71. if (!cursor.moveToNext()) {
  72. long currentTimeMillis2 = System.currentTimeMillis();
  73. EPLog.d(TAG, "Leave getImageDirInfo time = " + (currentTimeMillis2 - currentTimeMillis) + " msec");
  74. return convertTempMapToSortedList(hashMap);
  75. }
  76. }
  77. return null;
  78. }
  79. private List<ImageFolderInfo> convertTempMapToSortedList(Map<String, ImageFolderInfo> map) {
  80. if (map == null) {
  81. return null;
  82. }
  83. ArrayList arrayList = new ArrayList(map.values());
  84. Collections.sort(arrayList, new ImageFolderInfo.DatabaseOrderComparator());
  85. return arrayList;
  86. }
  87. public List<ImageFileInfo> findImageInDirectory(String str, ContentResolver contentResolver, ImageFinder.Canceller canceller) {
  88. try {
  89. Cursor myQuery = myQuery(contentResolver);
  90. if (myQuery == null) {
  91. return null;
  92. }
  93. try {
  94. return getImageFileInfoList(str, myQuery, canceller);
  95. } catch (Exception unused) {
  96. return null;
  97. } finally {
  98. myQuery.close();
  99. }
  100. } catch (Exception unused2) {
  101. return null;
  102. }
  103. }
  104. private List<ImageFileInfo> getImageFileInfoList(String str, Cursor cursor, ImageFinder.Canceller canceller) {
  105. if (str == null || cursor == null || canceller == null) {
  106. return null;
  107. }
  108. ArrayList arrayList = new ArrayList();
  109. int columnIndexOrThrow = cursor.getColumnIndexOrThrow("_data");
  110. int columnIndexOrThrow2 = cursor.getColumnIndexOrThrow("_id");
  111. if (!cursor.moveToFirst()) {
  112. return null;
  113. }
  114. while (!canceller.checkCanceled()) {
  115. String string = cursor.getString(columnIndexOrThrow);
  116. if (string != null) {
  117. File file = new File(string);
  118. if (str.equals(file.getParent()) && myCheckFile(file)) {
  119. arrayList.add(new ImageFileInfo(file.toString(), cursor.getLong(columnIndexOrThrow2)));
  120. }
  121. }
  122. if (!cursor.moveToNext()) {
  123. return arrayList;
  124. }
  125. }
  126. return null;
  127. }
  128. private Cursor myQuery(ContentResolver contentResolver) {
  129. if (contentResolver == null) {
  130. return null;
  131. }
  132. return contentResolver.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new String[]{"_id", "_data"}, (String) null, (String[]) null, "date_modified DESC");
  133. }
  134. }