DroidFS/app/src/main/java/sushi/hardcore/droidfs/util/Wiper.kt

98 lines
3.5 KiB
Kotlin
Raw Normal View History

2020-07-17 16:35:39 +02:00
package sushi.hardcore.droidfs.util
import android.content.Context
import android.net.Uri
import android.provider.OpenableColumns
import android.widget.EditText
2021-08-27 19:47:35 +02:00
import androidx.documentfile.provider.DocumentFile
2020-07-28 22:25:10 +02:00
import sushi.hardcore.droidfs.ConstValues
import sushi.hardcore.droidfs.R
2020-07-17 16:35:39 +02:00
import java.io.*
import java.lang.Exception
import java.lang.StringBuilder
import java.lang.UnsupportedOperationException
import java.util.*
import kotlin.math.ceil
object Wiper {
private const val buff_size = 4096
2020-07-28 22:25:10 +02:00
fun wipe(context: Context, uri: Uri): String? {
2020-07-17 16:35:39 +02:00
val cursor = context.contentResolver.query(uri, null, null, null, null)
cursor?.let {
val sizeIndex = cursor.getColumnIndex(OpenableColumns.SIZE)
cursor.moveToFirst()
val size = cursor.getLong(sizeIndex)
cursor.close()
try {
2021-08-27 19:47:35 +02:00
var os = context.contentResolver.openOutputStream(uri)!!
2020-07-17 16:35:39 +02:00
val buff = ByteArray(buff_size)
Arrays.fill(buff, 0.toByte())
val writes = ceil(size.toDouble() / buff_size).toInt()
2020-07-28 22:25:10 +02:00
for (i in 0 until ConstValues.wipe_passes) {
2020-07-17 16:35:39 +02:00
for (j in 0 until writes) {
2021-08-27 19:47:35 +02:00
os.write(buff)
2020-07-17 16:35:39 +02:00
}
2020-07-28 22:25:10 +02:00
if (i < ConstValues.wipe_passes - 1) {
2020-07-17 16:35:39 +02:00
//reopening to flush and seek
2021-08-27 19:47:35 +02:00
os.close()
os = context.contentResolver.openOutputStream(uri)!!
2020-07-17 16:35:39 +02:00
}
}
try {
context.contentResolver.delete(uri, null, null)
} catch (e: UnsupportedOperationException){
2021-08-27 19:47:35 +02:00
if (DocumentFile.fromSingleUri(context, uri)?.delete() != true) {
(os as FileOutputStream).channel.truncate(0) //truncate to 0 if cannot delete
}
2020-07-17 16:35:39 +02:00
}
2021-08-27 19:47:35 +02:00
os.close()
2020-07-28 22:25:10 +02:00
return null
2020-07-17 16:35:39 +02:00
} catch (e: Exception) {
2020-07-28 22:25:10 +02:00
return e.message
2020-07-17 16:35:39 +02:00
}
}
2020-07-28 22:25:10 +02:00
return context.getString(R.string.query_cursor_null_error_msg)
2020-07-17 16:35:39 +02:00
}
2020-07-28 22:25:10 +02:00
fun wipe(file: File): String? {
2020-07-17 16:35:39 +02:00
val size = file.length()
try {
var os = FileOutputStream(file)
val buff = ByteArray(buff_size)
Arrays.fill(buff, 0.toByte())
val writes = ceil(size.toDouble() / buff_size).toInt()
2020-07-28 22:25:10 +02:00
for (i in 0 until ConstValues.wipe_passes) {
2020-07-17 16:35:39 +02:00
for (j in 0 until writes) {
os.write(buff)
}
2020-07-28 22:25:10 +02:00
if (i < ConstValues.wipe_passes - 1) {
2020-07-17 16:35:39 +02:00
//reopening to flush and seek
os.close()
os = FileOutputStream(file)
}
}
try {
file.delete()
} catch (e: UnsupportedOperationException){
os.channel.truncate(0) //truncate to 0 if cannot delete
}
os.close()
2020-07-28 22:25:10 +02:00
return null
2020-07-17 16:35:39 +02:00
} catch (e: Exception) {
2020-07-28 22:25:10 +02:00
return e.message
2020-07-17 16:35:39 +02:00
}
}
2020-07-27 16:20:52 +02:00
private fun randomString(minSize: Int, maxSize: Int): String {
2020-07-17 16:35:39 +02:00
val r = Random()
val sb = StringBuilder()
val length = r.nextInt(maxSize-minSize)+minSize
for (i in 0..length){
sb.append((r.nextInt(94)+32).toChar())
}
return sb.toString()
}
fun wipeEditText(editText: EditText){
if (editText.text.isNotEmpty()){
editText.setText(randomString(editText.text.length, editText.text.length*3))
}
}
}