RustAssistant/app/src/main/java/com/coldmint/rust/pro/RecyclingStationActivity.kt

194 lines
8.2 KiB
Kotlin
Raw Normal View History

2022-04-04 14:03:10 +00:00
package com.coldmint.rust.pro
import com.coldmint.rust.pro.base.BaseActivity
import android.os.Bundle
2022-08-14 02:43:53 +00:00
import android.view.LayoutInflater
2022-04-04 14:03:10 +00:00
import android.view.Menu
import androidx.recyclerview.widget.LinearLayoutManager
import com.coldmint.rust.pro.tool.AppSettings
import android.view.MenuItem
import android.view.View
import android.widget.PopupMenu
import androidx.core.view.isVisible
import com.coldmint.rust.core.tool.FileOperator
import com.coldmint.rust.pro.adapters.FileAdapter
import com.coldmint.rust.pro.databinding.ActivityRecyclingStationBinding
import java.io.File
import java.util.ArrayList
class RecyclingStationActivity : BaseActivity<ActivityRecyclingStationBinding>() {
private lateinit var mWorkFolder: File
private var working = false
private var mDayNum = 0
override fun whenCreateActivity(savedInstanceState: Bundle?, canUseView: Boolean) {
if (canUseView) {
setReturnButton()
2022-07-02 10:11:56 +00:00
title = getString(R.string.enable_the_recovery_station)
2022-04-04 14:03:10 +00:00
viewBinding.backupList.layoutManager =
LinearLayoutManager(this@RecyclingStationActivity)
2022-08-14 02:43:53 +00:00
val workFolderPath = AppSettings.getValue(
2022-04-04 14:03:10 +00:00
AppSettings.Setting.RecoveryStationFolder,
this@RecyclingStationActivity.filesDir.absolutePath + "/backup/"
)
mDayNum = Integer.valueOf(
2022-08-14 02:43:53 +00:00
AppSettings.getValue(
2022-04-04 14:03:10 +00:00
AppSettings.Setting.RecoveryStationFileSaveDays,
7
)
)
mWorkFolder = File(workFolderPath)
if (!mWorkFolder.exists()) {
mWorkFolder.mkdirs()
}
loadFiles()
}
}
private fun loadFiles() {
Thread {
val files = mWorkFolder.listFiles()
val fileArrayList: MutableList<File?> = ArrayList()
val nowTime = System.currentTimeMillis()
//先设置为多少秒
val targetDifferences = (mDayNum * 86400000).toLong()
for (file in files) {
val last = file.lastModified()
val differences = nowTime - last
if (differences > targetDifferences) {
FileOperator.delete_files(file)
} else {
fileArrayList.add(file)
}
}
if (fileArrayList.size > 0) {
val fileAdapter = FileAdapter(this@RecyclingStationActivity, fileArrayList)
fileAdapter.setItemEvent { i, fileItemBinding, viewHolder, file ->
fileItemBinding.more.setOnClickListener {
if (file == null) {
return@setOnClickListener
}
2022-07-02 10:11:56 +00:00
val popupMenu =
PopupMenu(this@RecyclingStationActivity, fileItemBinding.more)
2022-04-04 14:03:10 +00:00
popupMenu.menu.add(R.string.recovery_file)
popupMenu.menu.add(R.string.delete_title)
popupMenu.setOnMenuItemClickListener(PopupMenu.OnMenuItemClickListener { item ->
if (working) {
return@OnMenuItemClickListener true
}
val title = item.title.toString()
if (title == getString(R.string.delete_title)) {
Thread {
working = true
FileOperator.delete_files(file)
runOnUiThread {
working = false
loadFiles()
}
}.start()
} else if (title == getString(R.string.recovery_file)) {
Thread {
working = true
val removePath: String
val modDirectory =
2022-08-14 02:43:53 +00:00
AppSettings.getValue(AppSettings.Setting.ModFolder, "")
2022-04-04 14:03:10 +00:00
val removeFile: File
if (file.isDirectory) {
removePath = modDirectory + file.name + "/"
removeFile = File(removePath)
if (!removeFile.exists()) {
removeFile.mkdirs()
}
} else {
removePath = modDirectory + file.name
removeFile = File(removePath)
}
FileOperator.removeFiles(file, removeFile)
runOnUiThread {
working = false
loadFiles()
}
}.start()
}
true
})
popupMenu.show()
}
}
runOnUiThread {
viewBinding.progressBar.isVisible = false
viewBinding.backupError.isVisible = false
viewBinding.backupList.isVisible = true
viewBinding.backupList.adapter = fileAdapter
}
} else {
runOnUiThread {
viewBinding.progressBar.isVisible = false
viewBinding.backupError.isVisible = true
viewBinding.backupList.isVisible = false
viewBinding.backupError.setText(R.string.not_find_mod)
}
}
}.start()
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
val id = item.itemId
when (id) {
R.id.clear_recovery_station -> {
Thread {
runOnUiThread {
viewBinding.progressBar.isVisible = true
viewBinding.backupError.isVisible = true
viewBinding.backupList.isVisible = false
viewBinding.backupError.setText(R.string.del_moding)
}
FileOperator.delete_files(mWorkFolder)
runOnUiThread {
viewBinding.progressBar.isVisible = false
viewBinding.backupError.isVisible = true
viewBinding.backupList.isVisible = false
viewBinding.backupError.setText(R.string.not_find_mod)
}
}.start()
return true
}
R.id.restore_all -> {
Thread {
runOnUiThread {
viewBinding.progressBar.isVisible = true
viewBinding.backupError.isVisible = true
viewBinding.backupList.isVisible = false
viewBinding.backupError.setText(R.string.restoreing)
}
FileOperator.removeFiles(
mWorkFolder,
2022-08-14 02:43:53 +00:00
File(AppSettings.getValue(AppSettings.Setting.ModFolder, ""))
2022-04-04 14:03:10 +00:00
)
runOnUiThread {
viewBinding.progressBar.isVisible = false
viewBinding.backupError.isVisible = true
viewBinding.backupList.isVisible = false
viewBinding.backupError.setText(R.string.not_find_mod)
}
}.start()
return true
}
}
return super.onOptionsItemSelected(item)
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
val inflater = menuInflater
inflater.inflate(R.menu.menu_recovery_station, menu)
return true
}
2022-08-14 02:43:53 +00:00
override fun getViewBindingObject(layoutInflater: LayoutInflater): ActivityRecyclingStationBinding {
2022-04-04 14:03:10 +00:00
return ActivityRecyclingStationBinding.inflate(layoutInflater)
}
}