ImageFindTask.java 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package epson.print.imgsel;
  2. import android.content.ContentResolver;
  3. import android.graphics.Bitmap;
  4. import android.os.AsyncTask;
  5. import epson.print.ImageItem;
  6. import epson.print.imgsel.ImageFinder;
  7. import java.lang.ref.WeakReference;
  8. import java.util.ArrayList;
  9. import java.util.List;
  10. class ImageFindTask extends AsyncTask<ImageFinder, List<ImageItem>, Void> implements ImageFinder.Canceller {
  11. private static int IMAGE_QUERY_LIMIT = 32;
  12. String mFolderPath;
  13. WeakReference<ImageGridFragment> mFragmentReference;
  14. public ImageFindTask(ImageGridFragment imageGridFragment, String str) {
  15. this.mFragmentReference = new WeakReference<>(imageGridFragment);
  16. this.mFolderPath = str;
  17. }
  18. protected Void doInBackground(ImageFinder... imageFinderArr) {
  19. ImageGridFragment imageGridFragment;
  20. List<ImageFileInfo> findImageInDirectory;
  21. if (imageFinderArr == null || imageFinderArr.length <= 0) {
  22. return null;
  23. }
  24. ImageFinder imageFinder = imageFinderArr[0];
  25. if (!(isCancelled() || imageFinder == null || (imageGridFragment = (ImageGridFragment) this.mFragmentReference.get()) == null || imageGridFragment.getActivity() == null || isCancelled() || (findImageInDirectory = imageFinder.findImageInDirectory(this.mFolderPath, getResolver(), this)) == null)) {
  26. ArrayList arrayList = new ArrayList();
  27. for (ImageFileInfo next : findImageInDirectory) {
  28. if (isCancelled()) {
  29. return null;
  30. }
  31. boolean isImageSelected = isImageSelected(next.mCanonicalPath);
  32. arrayList.add(new ImageItem((Bitmap) null, isImageSelected ? 1 : 0, next.mCanonicalPath, next.mMediaInfoId));
  33. if (arrayList.size() % IMAGE_QUERY_LIMIT == 0) {
  34. publishProgress(new List[]{arrayList});
  35. arrayList = new ArrayList();
  36. }
  37. }
  38. if (arrayList.size() > 0) {
  39. publishProgress(new List[]{arrayList});
  40. }
  41. }
  42. return null;
  43. }
  44. private ContentResolver getResolver() {
  45. FragmentActivity activity;
  46. ImageGridFragment imageGridFragment = (ImageGridFragment) this.mFragmentReference.get();
  47. if (imageGridFragment == null || (activity = imageGridFragment.getActivity()) == null) {
  48. return null;
  49. }
  50. return activity.getContentResolver();
  51. }
  52. private boolean isImageSelected(String str) {
  53. ImageGridFragment imageGridFragment = (ImageGridFragment) this.mFragmentReference.get();
  54. if (imageGridFragment == null) {
  55. return false;
  56. }
  57. return imageGridFragment.isImageSelected(str);
  58. }
  59. public boolean checkCanceled() {
  60. return isCancelled();
  61. }
  62. protected void onPreExecute() {
  63. ImageGridFragment imageGridFragment = (ImageGridFragment) this.mFragmentReference.get();
  64. if (imageGridFragment != null) {
  65. imageGridFragment.clearItem();
  66. }
  67. }
  68. protected void onProgressUpdate(List<ImageItem>... listArr) {
  69. ImageGridFragment imageGridFragment = (ImageGridFragment) this.mFragmentReference.get();
  70. if (imageGridFragment != null && listArr != null && listArr.length > 0) {
  71. imageGridFragment.addItem(listArr[0]);
  72. }
  73. }
  74. protected void onPostExecute(Void voidR) {
  75. ImageGridFragment imageGridFragment = (ImageGridFragment) this.mFragmentReference.get();
  76. if (imageGridFragment != null) {
  77. imageGridFragment.updateData();
  78. }
  79. }
  80. }