RecursiveFileObserver.java 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /**
  2. * ownCloud Android client application
  3. *
  4. * Copyright (C) 2012 Bartek Przybylski
  5. * Copyright (C) 2015 ownCloud Inc.
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2,
  9. * as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. *
  19. */
  20. package com.owncloud.android.utils;
  21. import java.io.File;
  22. import java.util.ArrayList;
  23. import java.util.List;
  24. import java.util.Stack;
  25. import android.os.FileObserver;
  26. public class RecursiveFileObserver extends FileObserver {
  27. private final List<SingleFileObserver> mObservers = new ArrayList<>();
  28. private boolean watching = false;
  29. private String mPath;
  30. private int mMask;
  31. public RecursiveFileObserver(String path) {
  32. this(path, ALL_EVENTS);
  33. }
  34. RecursiveFileObserver(String path, int mask) {
  35. super(path, mask);
  36. mPath = path;
  37. mMask = mask;
  38. }
  39. @Override
  40. public void startWatching() {
  41. if (watching) {
  42. return;
  43. }
  44. watching = true;
  45. final Stack<String> stack = new Stack<String>();
  46. stack.push(mPath);
  47. while (!stack.empty()) {
  48. String parent = stack.pop();
  49. mObservers.add(new SingleFileObserver(parent, mMask));
  50. File path = new File(parent);
  51. File[] files = path.listFiles();
  52. if (files == null) {
  53. continue;
  54. }
  55. for (final File file : files) {
  56. if (file.isDirectory() && !file.getName().equals(".")
  57. && !file.getName().equals("..")) {
  58. stack.push(file.getPath());
  59. }
  60. }
  61. }
  62. for (int i = 0; i < mObservers.size(); i++) {
  63. mObservers.get(i).startWatching();
  64. }
  65. }
  66. @Override
  67. public void stopWatching() {
  68. if (!watching) {
  69. return;
  70. }
  71. for (int i = 0; i < mObservers.size(); ++i) {
  72. mObservers.get(i).stopWatching();
  73. }
  74. mObservers.clear();
  75. watching = false;
  76. }
  77. @Override
  78. public void onEvent(int event, String path) {
  79. }
  80. private class SingleFileObserver extends FileObserver {
  81. private String mPath;
  82. SingleFileObserver(String path, int mask) {
  83. super(path, mask);
  84. mPath = path;
  85. }
  86. @Override
  87. public void onEvent(int event, String path) {
  88. String newPath = mPath + "/" + path;
  89. RecursiveFileObserver.this.onEvent(event, newPath);
  90. }
  91. }
  92. }