2 * ownCloud Android client application
4 * Copyright (C) 2012 Bartek Przybylski
5 * Copyright (C) 2015 ownCloud Inc.
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.
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.
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/>.
21 package com
.owncloud
.android
.utils
;
24 import java
.util
.ArrayList
;
25 import java
.util
.List
;
26 import java
.util
.Stack
;
28 import android
.os
.FileObserver
;
30 public class RecursiveFileObserver
extends FileObserver
{
32 public static int CHANGES_ONLY
= CLOSE_WRITE
| MOVE_SELF
| MOVED_FROM
;
34 List
<SingleFileObserver
> mObservers
;
38 public RecursiveFileObserver(String path
) {
39 this(path
, ALL_EVENTS
);
42 public RecursiveFileObserver(String path
, int mask
) {
49 public void startWatching() {
50 if (mObservers
!= null
) return;
51 mObservers
= new ArrayList
<SingleFileObserver
>();
52 Stack
<String
> stack
= new Stack
<String
>();
55 while (!stack
.empty()) {
56 String parent
= stack
.pop();
57 mObservers
.add(new SingleFileObserver(parent
, mMask
));
58 File path
= new File(parent
);
59 File
[] files
= path
.listFiles();
60 if (files
== null
) continue;
61 for (int i
= 0; i
< files
.length
; ++i
) {
62 if (files
[i
].isDirectory() && !files
[i
].getName().equals(".")
63 && !files
[i
].getName().equals("..")) {
64 stack
.push(files
[i
].getPath());
68 for (int i
= 0; i
< mObservers
.size(); i
++)
69 mObservers
.get(i
).startWatching();
73 public void stopWatching() {
74 if (mObservers
== null
) return;
76 for (int i
= 0; i
< mObservers
.size(); ++i
)
77 mObservers
.get(i
).stopWatching();
84 public void onEvent(int event
, String path
) {
88 private class SingleFileObserver
extends FileObserver
{
91 public SingleFileObserver(String path
, int mask
) {
97 public void onEvent(int event
, String path
) {
98 String newPath
= mPath
+ "/" + path
;
99 RecursiveFileObserver
.this.onEvent(event
, newPath
);