Back to project page filemanager.
The source code is released under:
MIT License
If you think the Android project filemanager listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
/** * The MIT License (MIT)//ww w.java 2 s.co m * * Copyright 2014 Binkery Huang <binkery@gmail.com> * * Oct 23, 2014 */ package com.binkery.app.filemanager.fragments; import java.io.File; import android.app.ProgressDialog; import android.os.AsyncTask; import com.binkery.app.filemanager.R; import com.binkery.app.filemanager.utils.Logs; public class CopyTask extends AsyncTask<File, String, Integer> { private static final String TAG = CopyTask.class.getSimpleName(); private ProgressDialog dialog = null; private FileListFragment mFragment = null; public static final int OK = 0; public static final int FAILED_FILE_NO_EXIST = -1; public static final int FAILED_NO_SPACE = -2; public CopyTask(FileListFragment fragment) { mFragment = fragment; dialog = new ProgressDialog(fragment.getActivity()); } @Override protected void onPreExecute() { super.onPreExecute(); dialog.setMessage(mFragment.getString(R.string.copy)); dialog.setCancelable(false); dialog.show(); } @Override protected Integer doInBackground(File... params) { File from = params[0]; File curFolder = params[1]; long time = System.currentTimeMillis(); //TODO if(from == null || !from.exists()){ return FAILED_FILE_NO_EXIST; } Logs.i(TAG, "from " + from.getAbsolutePath() + ", to " + curFolder.getAbsolutePath()); long filesize = com.binkery.app.filemanager.utils.FileUtils.getFileSize(from); Logs.i(TAG, "file size = " + filesize); long freeSpace = com.binkery.app.filemanager.utils.FileUtils.getFreeSpace(curFolder); Logs.i(TAG, "free space = " + freeSpace); if(filesize > freeSpace){ return FAILED_NO_SPACE; } File targetFile = getNewFileName(curFolder,from.getName(),0); Logs.i(TAG, "new file name = " + targetFile); copyFileStream(from, targetFile); Logs.i(TAG, "using " + (System.currentTimeMillis() - time)); return OK; } private File getNewFileName(File folder, String name, int index) { String path = null; if (index == 0) { path = folder.getAbsoluteFile() + File.separator + name; } else { int point = name.lastIndexOf("."); if (point == -1 || point == 0) { path = folder.getAbsoluteFile() + File.separator + name + "-" + index; }else { String pre = name.substring(0, point); String suf = name.substring(point, name.length()); path = folder.getAbsolutePath() + File.separator + pre + "-" + index + suf; } } File file = new File(path); if (!file.exists()) { return file; } return getNewFileName(folder, name, index + 1); } @Override protected void onPostExecute(Integer result) { super.onPostExecute(result); dialog.dismiss(); mFragment.onCopyCompleted(result); } private long copyFileStream(File from, File to) { long count = 0; if (from.isFile()) { return com.binkery.app.filemanager.utils.FileUtils.copy(from, to); } else if (from.isDirectory()) { if (to.exists()) { return -1; } if (!to.mkdir()) { return -1; } File[] files = from.listFiles(); if (files == null || files.length == 0) { return 0; } for (File file : files) { String path = to.getAbsoluteFile() + File.separator + file.getName(); File target = new File(path); long result = copyFileStream(file, target); if (result == -1) { return -1; } count += result; } } return count; } }