aac34eb34d40347dd199b02479a582d166631d23
1 /* ownCloud Android client application
2 * Copyright (C) 2012 Bartek Przybylski
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 package com
.owncloud
.android
.utils
;
22 import java
.util
.ArrayList
;
23 import java
.util
.List
;
24 import java
.util
.Stack
;
26 import android
.os
.FileObserver
;
28 public class RecursiveFileObserver
extends FileObserver
{
30 public static int CHANGES_ONLY
= CLOSE_WRITE
| MOVE_SELF
| MOVED_FROM
;
32 List
<SingleFileObserver
> mObservers
;
36 public RecursiveFileObserver(String path
) {
37 this(path
, ALL_EVENTS
);
40 public RecursiveFileObserver(String path
, int mask
) {
47 public void startWatching() {
48 if (mObservers
!= null
) return;
49 mObservers
= new ArrayList
<SingleFileObserver
>();
50 Stack
<String
> stack
= new Stack
<String
>();
53 while (!stack
.empty()) {
54 String parent
= stack
.pop();
55 mObservers
.add(new SingleFileObserver(parent
, mMask
));
56 File path
= new File(parent
);
57 File
[] files
= path
.listFiles();
58 if (files
== null
) continue;
59 for (int i
= 0; i
< files
.length
; ++i
) {
60 if (files
[i
].isDirectory() && !files
[i
].getName().equals(".")
61 && !files
[i
].getName().equals("..")) {
62 stack
.push(files
[i
].getPath());
66 for (int i
= 0; i
< mObservers
.size(); i
++)
67 mObservers
.get(i
).startWatching();
71 public void stopWatching() {
72 if (mObservers
== null
) return;
74 for (int i
= 0; i
< mObservers
.size(); ++i
)
75 mObservers
.get(i
).stopWatching();
82 public void onEvent(int event
, String path
) {
86 private class SingleFileObserver
extends FileObserver
{
89 public SingleFileObserver(String path
, int mask
) {
95 public void onEvent(int event
, String path
) {
96 String newPath
= mPath
+ "/" + path
;
97 RecursiveFileObserver
.this.onEvent(event
, newPath
);