Android Open Source - Metascan-Online Recursive File Observer






From Project

Back to project page Metascan-Online.

License

The source code is released under:

Apache License

If you think the Android project Metascan-Online listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.opswat.metascan;
/*from w w w. j  a v  a2s  . c om*/
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
 
import android.os.FileObserver;
import android.util.Log;
 
public class RecursiveFileObserver extends FileObserver {
 
    public static int CHANGES_ONLY = CLOSE_WRITE | MOVE_SELF | MOVED_FROM;
    private static final String TAG = "MainActivity.java";
  public String path;
    List<SingleFileObserver> mObservers;
    String mPath;
    int mMask;
    
    public RecursiveFileObserver(String path) {
        this(path, ALL_EVENTS);
    }
    
    public RecursiveFileObserver(String path, int mask) {
        super(path, mask);
        mPath = path;
        mMask = mask;
    }
 
    @Override
    public void startWatching() {
        if (mObservers != null) return;
        mObservers = new ArrayList<SingleFileObserver>();
        Stack<String> stack = new Stack<String>();
        stack.push(mPath);
        
        while (!stack.empty()) {
            String parent = stack.pop();
            mObservers.add(new SingleFileObserver(parent, mMask));
            File path = new File(parent);
            File[] files = path.listFiles();
            if (files == null) continue;
            for (int i = 0; i < files.length; ++i) {
                if (files[i].isDirectory() && !files[i].getName().equals(".")
                    && !files[i].getName().equals("..")) {
                    stack.push(files[i].getPath());
                }
            }
        }
        for (int i = 0; i < mObservers.size(); i++)
            mObservers.get(i).startWatching();
    }
    
    @Override
    public void stopWatching() {
        if (mObservers == null) return;
        
        for (int i = 0; i < mObservers.size(); ++i)
            mObservers.get(i).stopWatching();
 
        mObservers.clear();
        mObservers = null;
    }
    
    @Override
    public void onEvent(int event, String path) {
        
    }
    
    private class SingleFileObserver extends FileObserver {
        private String msPath;
 
        public SingleFileObserver(String path, int mask) {
            super(path, mask);
            msPath = path;
        }
        
        @Override
        public void onEvent(int event, String path) {
          if((event & FileObserver.CREATE)!=0){
            
            String newPath = msPath + "/" + path;
            PostDataAsyncTask task = new PostDataAsyncTask(MainActivity.context);
         task.execute(newPath);
            Log.v(TAG, msPath + "/" + path);
              RecursiveFileObserver.this.onEvent(event, newPath);
          }
          else if((event & FileObserver.MODIFY)!=0){
            String newPath = msPath + "/" + path;
            PostDataAsyncTask task = new PostDataAsyncTask(MainActivity.context);
         task.execute(newPath);
            Log.v(TAG, msPath + "/" + path);
              RecursiveFileObserver.this.onEvent(event, newPath);
          }
        } 
        
    }
}




Java Source Code List

com.opswat.metascan.DBTable.java
com.opswat.metascan.DirectoryChooserDialog.java
com.opswat.metascan.DownloadListener.java
com.opswat.metascan.FolderScan.java
com.opswat.metascan.HttpPostReceive.java
com.opswat.metascan.MainActivity.java
com.opswat.metascan.MySQLiteDB.java
com.opswat.metascan.ObserverService.java
com.opswat.metascan.ParseJSON.java
com.opswat.metascan.PostDataAsyncTask.java
com.opswat.metascan.RecursiveFileObserver.java
com.opswat.metascan.Results.java
com.opswat.metascan.Scan.java
com.opswat.metascan.Settings.java
com.opswat.metascan.TabAdapter.java
processing.files.SelectActivity.java
processing.files.SelectMode.java