Android Open Source - Alfred4Android Recursive File Observer






From Project

Back to project page Alfred4Android.

License

The source code is released under:

Apache License

If you think the Android project Alfred4Android 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.toraleap.collimator.util;
//from   w  w w .j  a  v  a  2s.c  o  m
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

import android.os.FileObserver;
import android.util.Log;

/**
 * Enhanced FileObserver to support recursive directory monitoring basically.
 * @author    uestc.Mobius <mobius@toraleap.com>
 * @version  2011.0121
 */
public class RecursiveFileObserver extends FileObserver {
  /** Only modification events */
  public static int CHANGES_ONLY = CREATE | DELETE | CLOSE_WRITE | MOVE_SELF | MOVED_FROM | MOVED_TO;
  
  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.isEmpty()) {
      String parent = stack.pop();
      mObservers.add(new SingleFileObserver(parent, mMask));
      File path = new File(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
  public void stopWatching() {
    if (mObservers == null) return;
    
    for (SingleFileObserver sfo : mObservers) {
      sfo.stopWatching();
    }
    mObservers.clear();
    mObservers = null;
  }

  @Override
  public void onEvent(int event, String path) {
    switch (event)
    {
    case FileObserver.ACCESS:
      Log.i("RecursiveFileObserver", "ACCESS: " + path);
      break;
    case FileObserver.ATTRIB:
      Log.i("RecursiveFileObserver", "ATTRIB: " + path);
      break;
    case FileObserver.CLOSE_NOWRITE:
      Log.i("RecursiveFileObserver", "CLOSE_NOWRITE: " + path);
      break;
    case FileObserver.CLOSE_WRITE:
      Log.i("RecursiveFileObserver", "CLOSE_WRITE: " + path);
      break;
    case FileObserver.CREATE:
      Log.i("RecursiveFileObserver", "CREATE: " + path);
      break;
    case FileObserver.DELETE:
      Log.i("RecursiveFileObserver", "DELETE: " + path);
      break;
    case FileObserver.DELETE_SELF:
      Log.i("RecursiveFileObserver", "DELETE_SELF: " + path);
      break;
    case FileObserver.MODIFY:
      Log.i("RecursiveFileObserver", "MODIFY: " + path);
      break;
    case FileObserver.MOVE_SELF:
      Log.i("RecursiveFileObserver", "MOVE_SELF: " + path);
      break;
    case FileObserver.MOVED_FROM:
      Log.i("RecursiveFileObserver", "MOVED_FROM: " + path);
      break;
    case FileObserver.MOVED_TO:
      Log.i("RecursiveFileObserver", "MOVED_TO: " + path);
      break;
    case FileObserver.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
   */
  class SingleFileObserver extends FileObserver {
    String mPath;
    
    public SingleFileObserver(String path) {
      this(path, ALL_EVENTS);
      mPath = path;
    }
    
    public SingleFileObserver(String path, int mask) {
      super(path, mask);
      mPath = path;
    }

    @Override
    public void onEvent(int event, String path) {
      String newPath = mPath + "/" + path;
      RecursiveFileObserver.this.onEvent(event, newPath);
    }
  }
}




Java Source Code List

android.media.MediaMetadataRetriever.java
com.toraleap.collimator.AppSearchActivity.java
com.toraleap.collimator.GlobalContext.java
com.toraleap.collimator.HelpActivity.java
com.toraleap.collimator.PrefsActivity.java
com.toraleap.collimator.SearchActivity.java
com.toraleap.collimator.ShortcutActivity.java
com.toraleap.collimator.bll.FileScannerService.java
com.toraleap.collimator.bll.TagGenerator.java
com.toraleap.collimator.dal.DBColumns.java
com.toraleap.collimator.dal.DBHelper.java
com.toraleap.collimator.dal.DBOperation.java
com.toraleap.collimator.data.Expression.java
com.toraleap.collimator.data.IndexData.java
com.toraleap.collimator.data.IndexLoader.java
com.toraleap.collimator.data.Index.java
com.toraleap.collimator.data.Match.java
com.toraleap.collimator.data.Matcher.java
com.toraleap.collimator.data.Sorter.java
com.toraleap.collimator.ext.GlobalProvider.java
com.toraleap.collimator.ext.Playlist.java
com.toraleap.collimator.model.BaseTag.java
com.toraleap.collimator.ui.FloatingDialog.java
com.toraleap.collimator.ui.MatchAdapter.java
com.toraleap.collimator.util.DigestUtil.java
com.toraleap.collimator.util.FileInfo.java
com.toraleap.collimator.util.MimeTypeMap.java
com.toraleap.collimator.util.RecursiveFileObserver.java
com.toraleap.collimator.util.ShortcutHelper.java
com.toraleap.collimator.util.SoftCache.java
com.toraleap.collimator.util.ThumbnailUtil.java
com.toraleap.collimator.util.Unicode2Alpha.java