publicclassRecursiveFileObserverextendsFileObserver{ |
/** Only modification events */ |
publicstaticint CHANGES_ONLY = CREATE | DELETE | CLOSE_WRITE | MOVE_SELF | MOVED_FROM | MOVED_TO; |
List<SingleFileObserver> mObservers; |
String mPath; |
int mMask; |
publicRecursiveFileObserver(String path){ |
this(path, ALL_EVENTS); |
} |
publicRecursiveFileObserver(String path,int mask){ |
super(path, mask); |
mPath = path; |
mMask = mask; |
} |
@Override |
publicvoid startWatching(){ |
if(mObservers !=null)return; |
mObservers =newArrayList<SingleFileObserver>(); |
Stack<String> stack =newStack<String>(); |
stack.push(mPath); |
while(!stack.isEmpty()){ |
String parent = stack.pop(); |
mObservers.add(newSingleFileObserver(parent, mMask)); |
File path =newFile(parent); |
File[] files = path.listFiles(); |
if(null== files)continue; |
for(File f : files) |
{ |
if(f.isDirectory()&&!f.getName().equals(".")&&!f.getName().equals("..")){ |
stack.push(f.getPath()); |
} |
} |
} |
for(SingleFileObserver sfo : mObservers){ |
sfo.startWatching(); |
} |
} |
@Override |
publicvoid stopWatching(){ |
if(mObservers ==null)return; |
for(SingleFileObserver sfo : mObservers){ |
sfo.stopWatching(); |
} |
mObservers.clear(); |
mObservers =null; |
} |
@Override |
publicvoid onEvent(int event,String path){ |
switch(event) |
{ |
caseFileObserver.ACCESS: |
Log.i("RecursiveFileObserver","ACCESS: "+ path); |
break; |
caseFileObserver.ATTRIB: |
Log.i("RecursiveFileObserver","ATTRIB: "+ path); |
break; |
caseFileObserver.CLOSE_NOWRITE: |
Log.i("RecursiveFileObserver","CLOSE_NOWRITE: "+ path); |
break; |
caseFileObserver.CLOSE_WRITE: |
Log.i("RecursiveFileObserver","CLOSE_WRITE: "+ path); |
break; |
caseFileObserver.CREATE: |
Log.i("RecursiveFileObserver","CREATE: "+ path); |
break; |
caseFileObserver.DELETE: |
Log.i("RecursiveFileObserver","DELETE: "+ path); |
break; |
caseFileObserver.DELETE_SELF: |
Log.i("RecursiveFileObserver","DELETE_SELF: "+ path); |
break; |
caseFileObserver.MODIFY: |
Log.i("RecursiveFileObserver","MODIFY: "+ path); |
break; |
caseFileObserver.MOVE_SELF: |
Log.i("RecursiveFileObserver","MOVE_SELF: "+ path); |
break; |
caseFileObserver.MOVED_FROM: |
Log.i("RecursiveFileObserver","MOVED_FROM: "+ path); |
break; |
caseFileObserver.MOVED_TO: |
Log.i("RecursiveFileObserver","MOVED_TO: "+ path); |
break; |
caseFileObserver.OPEN: |
Log.i("RecursiveFileObserver","OPEN: "+ path); |
break; |
default: |
Log.i("RecursiveFileObserver","DEFAULT("+ event +"): "+ path); |
break; |
} |
} |
/** |
* Monitor single directory and dispatch all events to its parent, with full path. |
* @author uestc.Mobius <mobius@toraleap.com> |
* @version 2011.0121 |
*/ |
classSingleFileObserverextendsFileObserver{ |
String mPath; |
publicSingleFileObserver(String path){ |
this(path, ALL_EVENTS); |
mPath = path; |
} |
publicSingleFileObserver(String path,int mask){ |
super(path, mask); |
mPath = path; |
} |
@Override |
publicvoid onEvent(int event,String path){ |
String newPath = mPath +"/"+ path; |
RecursiveFileObserver.this.onEvent(event, newPath); |
} |
} |
} 注意: |