I am trying to make a wiping app in kotlin for android. It has just one button that makes all the process. But when user clicks the button, screen freezes and my setText or setProgress functions are not working. I think it is because it thinks it must finish the process and make UI changes. How can I setText or setProgress in the middle of the process?
My Code:
// findViewById
wipe_button = findViewById(R.id.wipe_button)
progress_bar = findViewById(R.id.progressBar)
main_textview = findViewById(R.id.main_textview)
// Getting available memory
freeMemory = getAvailableInternalMemorySizeInMB()
// progressBar Settings
progress_bar.max = 100
progress_bar.setProgress(0)
// OnClickListener
wipe_button.setOnClickListener {
checkPermissions()
if (isPermissionGranted) {
main_textview.setText("Started Wiping...")
progress_bar.setProgress(0)
fillStorage()
progress_bar.setProgress(10)
main_textview.setText("Created Filling Text. Copying...")
copyFiles()
progress_bar.setProgress(100)
main_textview.setText("Finished Deleting.")
Handler().postDelayed(Runnable {
main_textview.setText("Click The Button To Start Wiping")
progress_bar.setProgress(0)
}, 5000)
} else {
if (requestNumber >= 2) {
Toast.makeText(this, "Please Give Storage Permission To The App In Settings", Toast.LENGTH_SHORT).show()
} else {
requestPermissions()
}
}
}
And Button Functions:
private fun fillStorage() {
val file = File(getExternalFilesDir(null), "fill-1.txt")
val fileWriter = FileWriter(file, true)
var fileSizeInMB = file.length() / 1048576
var fileSizeInKB = (file.length()).toInt() / 1024
while (fileSizeInMB < 4) {
fileWriter.append("00000000")
fileWriter.flush()
fileSizeInMB = (file.length() / 1048576)
fileSizeInKB = ((file.length().toInt()) / 1024)
when (fileSizeInKB) {
409 -> progress_bar.setProgress(1)
818 -> progress_bar.setProgress(2)
1227 -> progress_bar.setProgress(3)
1636 -> progress_bar.setProgress(4)
2045 -> progress_bar.setProgress(5)
2454 -> progress_bar.setProgress(6)
2863 -> progress_bar.setProgress(7)
3272 -> progress_bar.setProgress(8)
3681 -> progress_bar.setProgress(9)
4096 -> progress_bar.setProgress(10)
}
}
}
private fun copyFiles() {
val dir = File("/storage/emulated/0/Android/data/com.cemalmertozkan.wiped/files")
var dirSize = File(Environment.getExternalStorageDirectory().absolutePath).length() / 1048576
freeMemory = getAvailableInternalMemorySizeInMB()
var fillingSizeInMB = freeMemory - 100
val file = File(getExternalFilesDir(null), "fill-1.txt")
var onePercentProgressInMb = fillingSizeInMB / 80
var fileNumber = 1
while (freeMemory > 100) {
fileNumber = fileNumber 1
var fileNew = File(getExternalFilesDir(null), "fill-" fileNumber ".txt")
file.copyTo(fileNew, true)
freeMemory = getAvailableInternalMemorySizeInMB()
var completeNum : Int = (dirSize % onePercentProgressInMb).toInt()
var copiedFilesInGB = dirSize % 1024
main_textview.setText("Copied " copiedFilesInGB "GB Of Files.")
progress_bar.setProgress(completeNum 10)
}
progress_bar.setProgress(90)
main_textview.setText("Completed Copying. Deleting...")
dir.deleteRecursively()
}
CodePudding user response:
You need to run the fillStorage() and copyFiles() functions in a coroutine which will run these tasks in a non-UI thread hence your screen will not freeze.
Please go through this - Android Coroutine Guide
