|
@@ -13,6 +13,21 @@ import io.reactivex.schedulers.Schedulers
|
|
|
import java.io.File
|
|
|
import java.lang.Exception
|
|
|
|
|
|
+/**
|
|
|
+ * 1. if info.shouldCopyFile == false,
|
|
|
+ * just call onCopySuccess() to send Event.Success(destination path)
|
|
|
+ * 2. if source path == destination path,
|
|
|
+ * just call onCopySuccess() to send Event.Success(destination path)
|
|
|
+ * 3. if the application has no permission,
|
|
|
+ * send Event.Failed(destination path, FileNotFoundException) and delete the destination file
|
|
|
+ * 4. if an IOException is thrown during copying,
|
|
|
+ * send Event.Failed(destination path, FileNotFoundException) and delete the destination file
|
|
|
+ * 5. otherwise, when CopyFileViewModel finishes copying file,
|
|
|
+ * call onCopySuccess() to send Event.Success(destination path)
|
|
|
+ *
|
|
|
+ * Finally, when CopyFileViewModel::onCleared() is called, delete the destination file if it is not complete copied
|
|
|
+*/
|
|
|
+
|
|
|
class CopyFileViewModel(applicationContext: Context,
|
|
|
private val info: CopyFileInfo,
|
|
|
private val eventManager: EventManager<Event> = EventManager()
|
|
@@ -49,6 +64,11 @@ class CopyFileViewModel(applicationContext: Context,
|
|
|
null
|
|
|
}
|
|
|
srcFilePath = source?.getFilePath()
|
|
|
+
|
|
|
+ if (isSrcFileEqualsDstFile()) {
|
|
|
+ onCopySuccess()
|
|
|
+ }
|
|
|
+
|
|
|
val fullFileSize = Math.max(1, source?.getFileSize() ?: 1)
|
|
|
val task = CopyFileTask()
|
|
|
task.setCopyMode(CopyFileTask.Mode.CREATE_NEW_FILE)
|
|
@@ -69,6 +89,15 @@ class CopyFileViewModel(applicationContext: Context,
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ // check the destination path is the same with the source path or not
|
|
|
+ private fun isSrcFileEqualsDstFile(): Boolean {
|
|
|
+ return try {
|
|
|
+ (!srcFilePath.isNullOrEmpty() && File(srcFilePath).canonicalPath == File(dstFilePath).canonicalPath)
|
|
|
+ } catch (e: Exception) {
|
|
|
+ false
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
private fun onCopyFailed(e: Throwable) {
|
|
|
if (!hasCompleteCopy) {
|
|
|
deleteDstFile()
|
|
@@ -87,11 +116,6 @@ class CopyFileViewModel(applicationContext: Context,
|
|
|
|
|
|
private fun deleteDstFile() {
|
|
|
dstFilePath?.apply {
|
|
|
- // do not delete the destination file if it is also the source file
|
|
|
- try {
|
|
|
- if (!srcFilePath.isNullOrEmpty() && File(srcFilePath).canonicalPath == File(dstFilePath).canonicalPath) return
|
|
|
- } catch (e: Exception) {}
|
|
|
-
|
|
|
val file = File(this)
|
|
|
if (file.exists()) {
|
|
|
file.delete()
|