码迷,mamicode.com
首页 > 其他好文 > 详细

文件管理器源码分析(二)

时间:2016-05-07 07:55:04      阅读:136      评论:0      收藏:0      [点我收藏+]

标签:

/**
 * 文件操作工具,执行文件的创建、移、粘贴、重命、删除等
 * 任务异步执IOperationProgressListener
 拷贝和删除等操作,比较费时,采用异步执行的方式~
 http://blog.csdn.net/xufenghappy6/article/details/7343899
 异步执+通知 比较流行的,比步等待很多时候好。
 */
public class FileOperationHelper {
    private static final String LOG_TAG = "FileOperation";
    private ArrayList<FileInfo> mCurFileNameList = new ArrayList<FileInfo>();
    private boolean mMoving;
    private IOperationProgressListener mOperationListener;
    private FilenameFilter mFilter;
    public interface IOperationProgressListener {
        void onFinish();
        void onFileChanged(String path);
    }
    public FileOperationHelper(IOperationProgressListener l) {
        mOperationListener = l;
    }
    public void setFilenameFilter(FilenameFilter f) {
        mFilter = f;
    }
    //根据路径文件名,创文件
    public boolean CreateFolder(String path, String name) {
        Log.v(LOG_TAG, "CreateFolder >>> " + path + "," + name);
        File f = new File(Util.makePath(path, name));
        if (f.exists())
            return false;
        return f.mkdir();
    }
    //若干个文件文件集合拷当前文件集合中mCurFileNameList”可以操作使用
    public void Copy(ArrayList<FileInfo> files) {
        copyFileList(files);
    }
    //贴,把当前文件集合中mCurFileNameList”的文件到目路径下
    public boolean Paste(String path) {
        if (mCurFileNameList.size() == 0)
            return false;
        final String _path = path;
        //异步执
        asnycExecute(new Runnable() {
            @Override
            public void run() {
                for (FileInfo f : mCurFileNameList) {
                    CopyFile(f, _path);
                }
                //通知操作
                mOperationListener.onFileChanged(Environment
                        .getExternalStorageDirectory()
                        .getAbsolutePath());
                //之后需要清空mCurFileNameList
                clear();
            }
        });
        return true;
    }
    //可以mCurFileNameList元素
    public boolean canPaste() {
        return mCurFileNameList.size() != 0;
    }
    //,标记正在移文件集合
    public void StartMove(ArrayList<FileInfo> files) {
        if (mMoving)
            return;
        mMoving = true;
        copyFileList(files);
    }
    //
    public boolean isMoveState() {
        return mMoving;
    }
    /**
     * 能否移走,假pathC:/a/b”,f.filePathC:/a/b/c/d.png”,能移走
     * 感覺太靠譜,爲啥不能移動到文件的上層目錄呢?
     * @param path
     * @return
     */
    public boolean canMove(String path) {
        for (FileInfo f : mCurFileNameList) {
            if (!f.IsDir)
                continue;
            if (Util.containsPath(f.filePath, path))
                return false;
        }
        return true;
    }
    //清空当前文件集合
    public void clear() {
        synchronized(mCurFileNameList) {
            mCurFileNameList.clear();
        }
    }
    //停止移动文件是异步执,结束后通知
    public boolean EndMove(String path) {
        if (!mMoving)
            return false;
        mMoving = false;
        if (TextUtils.isEmpty(path))
            return false;
        final String _path = path;
        asnycExecute(new Runnable() {
            @Override
            public void run() {
                    for (FileInfo f : mCurFileNameList) {
                        MoveFile(f, _path);
                    }
                    mOperationListener.onFileChanged(Environment
                            .getExternalStorageDirectory()
                            .getAbsolutePath());
                    clear();
                }
        });
        return true;
    }
    public ArrayList<FileInfo> getFileList() {
        return mCurFileNameList;
    }
    //异步执
    //android的类AsyncTask线进行了包装提供方式来使后台线程和UI线进行讯:后台线异步务,把操作果通知UI线程。
    //可以参考http://blog.csdn.net/xufenghappy6/article/details/7343899
    private void asnycExecute(Runnable r) {
        final Runnable _r = r;
        new AsyncTask() {
            @Override
            protected Object doInBackground(Object... params) {
                synchronized(mCurFileNameList) {
                    _r.run();
                }
                if (mOperationListener != null) {
                    mOperationListener.onFinish();
                }
                return null;
            }
        }.execute();
    }
    //个路径是否被
    public boolean isFileSelected(String path) {
        synchronized(mCurFileNameList) {
            for (FileInfo f : mCurFileNameList) {
                if (f.filePath.equalsIgnoreCase(path))
                    return true;
            }
        }
        return false;
    }
    //文件重命
    public boolean Rename(FileInfo f, String newName) {
        if (f == null || newName == null) {
            Log.e(LOG_TAG, "Rename: null parameter");
            return false;
        }
        File file = new File(f.filePath);
        String newPath = Util.makePath(Util.getPathFromFilepath(f.filePath), newName);
        final boolean needScan = file.isFile();
        try {
            boolean ret = file.renameTo(new File(newPath));
            if (ret) {
                if (needScan) {
                    mOperationListener.onFileChanged(f.filePath);
                }
                mOperationListener.onFileChanged(newPath);
            }
            return ret;
        } catch (SecurityException e) {
            Log.e(LOG_TAG, "Fail to rename file," + e.toString());
        }
        return false;
    }
    //除若干文件copy文件集合异步执除操作,删除完成后通知
    public boolean Delete(ArrayList<FileInfo> files) {
        copyFileList(files);
        asnycExecute(new Runnable() {
            @Override
            public void run() {
                for (FileInfo f : mCurFileNameList) {
                    DeleteFile(f);
                }
                mOperationListener.onFileChanged(Environment
                        .getExternalStorageDirectory()
                        .getAbsolutePath());
                clear();
            }
        });
        return true;
    }
    //1个文件
    protected void DeleteFile(FileInfo f) {
        if (f == null) {
            Log.e(LOG_TAG, "DeleteFile: null parameter");
            return;
        }
        File file = new File(f.filePath);
        boolean directory = file.isDirectory();
        if (directory) {
            for (File child : file.listFiles(mFilter)) {
                if (Util.isNormalFile(child.getAbsolutePath())) {
                    DeleteFile(Util.GetFileInfo(child, mFilter, true));
                }
            }
        }
        file.delete();
        Log.v(LOG_TAG, "DeleteFile >>> " + f.filePath);
    }
    //1个文件的贝,如果文件是递归Copy
    private void CopyFile(FileInfo f, String dest) {
        if (f == null || dest == null) {
            Log.e(LOG_TAG, "CopyFile: null parameter");
            return;
        }
        File file = new File(f.filePath);
        if (file.isDirectory()) {
            // directory exists in destination, rename it
            String destPath = Util.makePath(dest, f.fileName);
            File destFile = new File(destPath);
            int i = 1;
            while (destFile.exists()) {
                destPath = Util.makePath(dest, f.fileName + " " + i++);
                destFile = new File(destPath);
            }
            for (File child : file.listFiles(mFilter)) {
                if (!child.isHidden() && Util.isNormalFile(child.getAbsolutePath())) {
                    CopyFile(Util.GetFileInfo(child, mFilter, Settings.instance().getShowDotAndHiddenFiles()), destPath);
                }
            }
        } else {
            String destFile = Util.copyFile(f.filePath, dest);
        }
        Log.v(LOG_TAG, "CopyFile >>> " + f.filePath + "," + dest);
    }
    //动文件重命名的方式动的
    private boolean MoveFile(FileInfo f, String dest) {
        Log.v(LOG_TAG, "MoveFile >>> " + f.filePath + "," + dest);
        if (f == null || dest == null) {
            Log.e(LOG_TAG, "CopyFile: null parameter");
            return false;
        }
        File file = new File(f.filePath);
        String newPath = Util.makePath(dest, f.fileName);
        try {
            return file.renameTo(new File(newPath));
        } catch (SecurityException e) {
            Log.e(LOG_TAG, "Fail to move file," + e.toString());
        }
        return false;
    }
    //文件集合copymCurFileNameList~
    private void copyFileList(ArrayList<FileInfo> files) {
        synchronized(mCurFileNameList) {
            mCurFileNameList.clear();
            for (FileInfo f : files) {
                mCurFileNameList.add(f);
            }
        }
    }
}


文件管理器源码分析(二)

标签:

原文地址:http://blog.csdn.net/jjz_bobo/article/details/51332648

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!