FileLoader.java 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * Copyright (C) 2013 Paul Burke
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.ipaulpro.afilechooser;
  17. import android.content.Context;
  18. import android.os.FileObserver;
  19. import android.support.v4.content.AsyncTaskLoader;
  20. import com.ipaulpro.afilechooser.utils.FileUtils;
  21. import java.io.File;
  22. import java.util.ArrayList;
  23. import java.util.Arrays;
  24. import java.util.List;
  25. /**
  26. * Loader that returns a list of Files in a given file path.
  27. *
  28. * @version 2013-12-11
  29. * @author paulburke (ipaulpro)
  30. */
  31. public class FileLoader extends AsyncTaskLoader<List<File>> {
  32. private static final int FILE_OBSERVER_MASK = FileObserver.CREATE
  33. | FileObserver.DELETE | FileObserver.DELETE_SELF
  34. | FileObserver.MOVED_FROM | FileObserver.MOVED_TO
  35. | FileObserver.MODIFY | FileObserver.MOVE_SELF;
  36. private FileObserver mFileObserver;
  37. private List<File> mData;
  38. private String mPath;
  39. public FileLoader(Context context, String path) {
  40. super(context);
  41. this.mPath = path;
  42. }
  43. @Override
  44. public List<File> loadInBackground() {
  45. ArrayList<File> list = new ArrayList<File>();
  46. // Current directory File instance
  47. final File pathDir = new File(mPath);
  48. // List file in this directory with the directory filter
  49. final File[] dirs = pathDir.listFiles(FileUtils.sDirFilter);
  50. if (dirs != null) {
  51. // Sort the folders alphabetically
  52. Arrays.sort(dirs, FileUtils.sComparator);
  53. // Add each folder to the File list for the list adapter
  54. for (File dir : dirs)
  55. list.add(dir);
  56. }
  57. // List file in this directory with the file filter
  58. final File[] files = pathDir.listFiles(FileUtils.sFileFilter);
  59. if (files != null) {
  60. // Sort the files alphabetically
  61. Arrays.sort(files, FileUtils.sComparator);
  62. // Add each file to the File list for the list adapter
  63. for (File file : files)
  64. list.add(file);
  65. }
  66. return list;
  67. }
  68. @Override
  69. public void deliverResult(List<File> data) {
  70. if (isReset()) {
  71. onReleaseResources(data);
  72. return;
  73. }
  74. List<File> oldData = mData;
  75. mData = data;
  76. if (isStarted())
  77. super.deliverResult(data);
  78. if (oldData != null && oldData != data)
  79. onReleaseResources(oldData);
  80. }
  81. @Override
  82. protected void onStartLoading() {
  83. if (mData != null)
  84. deliverResult(mData);
  85. if (mFileObserver == null) {
  86. mFileObserver = new FileObserver(mPath, FILE_OBSERVER_MASK) {
  87. @Override
  88. public void onEvent(int event, String path) {
  89. onContentChanged();
  90. }
  91. };
  92. }
  93. mFileObserver.startWatching();
  94. if (takeContentChanged() || mData == null)
  95. forceLoad();
  96. }
  97. @Override
  98. protected void onStopLoading() {
  99. cancelLoad();
  100. }
  101. @Override
  102. protected void onReset() {
  103. onStopLoading();
  104. if (mData != null) {
  105. onReleaseResources(mData);
  106. mData = null;
  107. }
  108. }
  109. @Override
  110. public void onCanceled(List<File> data) {
  111. super.onCanceled(data);
  112. onReleaseResources(data);
  113. }
  114. protected void onReleaseResources(List<File> data) {
  115. if (mFileObserver != null) {
  116. mFileObserver.stopWatching();
  117. mFileObserver = null;
  118. }
  119. }
  120. }