Delete file & folder on wipe

This commit is contained in:
Matéo Duparc 2021-08-27 19:47:35 +02:00
parent 5cc9abfd76
commit c2c1e4b1e9
Signed by: hardcoresushi
GPG Key ID: 007F84120107191E
4 changed files with 21 additions and 14 deletions

View File

@ -425,14 +425,14 @@ open class BaseExplorerActivity : BaseActivity() {
}
}
fun importDirectory(sourceUri: Uri, callback: (String?, List<Uri>) -> Unit) {
fun importDirectory(sourceUri: Uri, callback: (String?, List<Uri>, DocumentFile) -> Unit) {
val tree = DocumentFile.fromTreeUri(this, sourceUri)!! //non-null after Lollipop
val operation = OperationFile.fromExplorerElement(ExplorerElement(tree.name!!, 0, parentPath = currentDirectoryPath))
checkPathOverwrite(arrayListOf(operation), currentDirectoryPath) { checkedOperation ->
checkedOperation?.let {
fileOperationService.importDirectory(checkedOperation[0].dstPath!!, tree) { failedItem, uris ->
runOnUiThread {
callback(failedItem, uris)
callback(failedItem, uris, tree)
}
}
}

View File

@ -9,6 +9,7 @@ import android.view.WindowManager
import android.widget.EditText
import android.widget.Toast
import androidx.activity.result.contract.ActivityResultContracts
import androidx.documentfile.provider.DocumentFile
import sushi.hardcore.droidfs.CameraActivity
import sushi.hardcore.droidfs.GocryptfsVolume
import sushi.hardcore.droidfs.OpenActivity
@ -121,7 +122,7 @@ class ExplorerActivity : BaseExplorerActivity() {
}
}
private fun onImportComplete(failedItem: String?, uris: List<Uri>) {
private fun onImportComplete(failedItem: String?, urisToWipe: List<Uri>, rootFile: DocumentFile? = null) {
if (failedItem == null){
ColoredAlertDialogBuilder(this)
.setTitle(R.string.success_import)
@ -130,9 +131,10 @@ class ExplorerActivity : BaseExplorerActivity() {
${getString(R.string.ask_for_wipe)}
""".trimIndent())
.setPositiveButton(R.string.yes) { _, _ ->
fileOperationService.wipeUris(uris) { errorMsg ->
fileOperationService.wipeUris(urisToWipe) { errorMsg ->
runOnUiThread {
if (errorMsg == null){
rootFile?.delete()
Toast.makeText(this, R.string.wipe_successful, Toast.LENGTH_SHORT).show()
} else {
ColoredAlertDialogBuilder(this)

View File

@ -288,15 +288,17 @@ class FileOperationService : Service() {
return@Thread
}
updateNotificationProgress(notification, 0, dstDirs.size)
// create destination folders so the new files can use them
for (mkdir in dstDirs) {
for (dir in dstDirs) {
if (notifications[notification.notificationId]!!) {
cancelNotification(notification)
return@Thread
}
gocryptfsVolume.mkdir(mkdir)
if (!gocryptfsVolume.mkdir(dir)) {
cancelNotification(notification)
callback(dir, srcUris)
break
}
}
importFilesFromUris(dstFiles, srcUris, notification) { failedItem ->

View File

@ -4,6 +4,7 @@ import android.content.Context
import android.net.Uri
import android.provider.OpenableColumns
import android.widget.EditText
import androidx.documentfile.provider.DocumentFile
import sushi.hardcore.droidfs.ConstValues
import sushi.hardcore.droidfs.R
import java.io.*
@ -23,26 +24,28 @@ object Wiper {
val size = cursor.getLong(sizeIndex)
cursor.close()
try {
var os = context.contentResolver.openOutputStream(uri)
var os = context.contentResolver.openOutputStream(uri)!!
val buff = ByteArray(buff_size)
Arrays.fill(buff, 0.toByte())
val writes = ceil(size.toDouble() / buff_size).toInt()
for (i in 0 until ConstValues.wipe_passes) {
for (j in 0 until writes) {
os!!.write(buff)
os.write(buff)
}
if (i < ConstValues.wipe_passes - 1) {
//reopening to flush and seek
os!!.close()
os = context.contentResolver.openOutputStream(uri)
os.close()
os = context.contentResolver.openOutputStream(uri)!!
}
}
try {
context.contentResolver.delete(uri, null, null)
} catch (e: UnsupportedOperationException){
if (DocumentFile.fromSingleUri(context, uri)?.delete() != true) {
(os as FileOutputStream).channel.truncate(0) //truncate to 0 if cannot delete
}
os!!.close()
}
os.close()
return null
} catch (e: Exception) {
return e.message