package cc.winboll.studio.regexputils.develop;

/*
 * 应用日志监听类
 * 源码提供：https://blog.csdn.net/wuxueshuan/article/details/121852698?app_version=5.15.2&code=app_1562916241&csdn_share_tail=%7B%22type%22%3A%22blog%22%2C%22rType%22%3A%22article%22%2C%22rId%22%3A%22121852698%22%2C%22source%22%3A%22weixin_38986226%22%7D&uLinkId=usr1mkqgl919blen&utm_source=app
 * 源码维护：ZhanGSKen@QQ.COM
 */
 
import android.os.FileObserver;
import android.util.Log;

public class LogListener extends FileObserver {
	public final static String TAG = "LogListener";

    public EventCallback callback;
    
    public String mLogPath;

    public LogListener(String path) {
		super(path);
        //Log.d(TAG, "LogListener(String path) path : " + path);
        mLogPath = path;
    }
    public void setEventCallback(EventCallback callback) {
        this.callback = callback;
    }

    @Override
    public void onEvent(int event, String path) {
        if (null != path && !"".equals(path)) {
            //String substring = path.substring(path.lastIndexOf(".") + 1);
            //Log.d(TAG, "path : " + path);
            //Log.d(TAG, "substring : " + substring);
            //Log.d(TAG, "event : " + event);

        } else {
            return;
        }

        int e = event & FileObserver.ALL_EVENTS;
        //Log.d(TAG, "event->e:" + e);
        switch (e) {
            case FileObserver.ACCESS:
                //Log.d(TAG, "文件操作___" + e + "__1打开文件后读取文件的操作");
                break;
            case FileObserver.MODIFY:
                //Log.d(TAG, "文件操作___" + e + "__2文件被修改");
                break;
            case FileObserver.ATTRIB:
                //Log.d(TAG, "文件操作___" + e + "__4属性变化");
                break;
            case FileObserver.CLOSE_WRITE:
                //Log.d(TAG, "文件操作___" + e + "__8文件写入或编辑后关闭");
				callback.onEvent(path);
                break;
            case FileObserver.CLOSE_NOWRITE:
                //录音时，最后一个有效回调是这个
                //Log.d(TAG, "文件操作___" + e + "__16只读文件被关闭");

                //callback.onEvent(path);


                break;
            case FileObserver.OPEN:
                //Log.d(TAG, "文件操作___" + e + "__32文件被打开");
                break;
            case FileObserver.MOVED_FROM:
                //Log.d(TAG, "文件操作___" + e + "__64移出事件");//试了重命名先MOVED_FROM再MOVED_TO
                break;
            case FileObserver.MOVED_TO:
                //Log.d(TAG, "文件操作___" + e + "__128移入事件");
                break;
            case FileObserver.CREATE:
                //Log.d(TAG, "文件操作___" + e + "__256新建文件");//把文件移动给自己先CREATE在DELETE
                break;
            case FileObserver.DELETE:
                //Log.d(TAG, "文件操作___" + e + "__512有删除文件");//把文件移出去DELETE
                break;
            case FileObserver.DELETE_SELF:
                //Log.d(TAG, "文件操作___" + e + "__1024监听的这个文件夹被删除");
                break;
            case FileObserver.MOVE_SELF:
                //Log.d(TAG, "文件操作___" + e + "__2048监听的这个文件夹被移走");
                break;
            case FileObserver.ALL_EVENTS:
                //Log.d(TAG, "文件操作___" + e + "__4095全部操作");
                break;
        }
    }

	public interface EventCallback {
        void onEvent(String path);
    }

}
