DroidFS/app/src/main/java/sushi/hardcore/droidfs/CreateActivity.kt

181 lines
8.1 KiB
Kotlin
Raw Normal View History

2020-07-17 16:35:39 +02:00
package sushi.hardcore.droidfs
import android.content.Intent
import android.os.Build
import android.os.Bundle
2021-10-17 13:46:10 +02:00
import android.view.View
import android.widget.*
2020-07-28 22:25:10 +02:00
import androidx.appcompat.app.AppCompatActivity
2021-06-11 20:23:54 +02:00
import sushi.hardcore.droidfs.databinding.ActivityCreateBinding
2020-07-17 16:35:39 +02:00
import sushi.hardcore.droidfs.explorers.ExplorerActivity
2021-06-07 14:12:40 +02:00
import sushi.hardcore.droidfs.util.Wiper
2021-11-09 11:12:09 +01:00
import sushi.hardcore.droidfs.widgets.CustomAlertDialogBuilder
2020-07-17 16:35:39 +02:00
import java.io.File
import java.util.*
2020-10-22 22:14:22 +02:00
class CreateActivity : VolumeActionActivity() {
2020-07-17 16:35:39 +02:00
private var sessionID = -1
private var isStartingExplorer = false
2021-06-11 20:23:54 +02:00
private lateinit var binding: ActivityCreateBinding
2020-07-17 16:35:39 +02:00
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
2021-06-11 20:23:54 +02:00
binding = ActivityCreateBinding.inflate(layoutInflater)
setContentView(binding.root)
2021-06-07 16:34:50 +02:00
setupLayout()
2021-10-19 13:52:06 +02:00
setupFingerprintStuff(mayDecrypt = false)
2021-06-11 20:23:54 +02:00
binding.editPasswordConfirm.setOnEditorActionListener { _, _, _ ->
2021-06-07 16:34:50 +02:00
createVolume()
2020-07-17 16:35:39 +02:00
true
}
2021-10-17 13:46:10 +02:00
binding.spinnerXchacha.adapter = ArrayAdapter(
this@CreateActivity,
android.R.layout.simple_spinner_item,
resources.getStringArray(R.array.encryption_cipher)
).apply {
setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
}
binding.spinnerXchacha.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
if (position == 1) {
2021-11-09 11:12:09 +01:00
CustomAlertDialogBuilder(this@CreateActivity, themeValue)
2021-10-17 13:46:10 +02:00
.setTitle(R.string.warning)
.setMessage(R.string.xchacha_warning)
.setPositiveButton(R.string.ok, null)
.show()
}
}
override fun onNothingSelected(parent: AdapterView<*>?) {}
}
2021-06-11 20:23:54 +02:00
binding.buttonCreate.setOnClickListener {
2021-06-07 16:34:50 +02:00
createVolume()
2020-11-04 18:54:16 +01:00
}
2020-07-17 16:35:39 +02:00
}
2021-06-07 16:34:50 +02:00
override fun onClickSwitchHiddenVolume() {
super.onClickSwitchHiddenVolume()
2021-06-11 20:23:54 +02:00
if (switchHiddenVolume.isChecked){
2021-11-09 11:12:09 +01:00
CustomAlertDialogBuilder(this, themeValue)
2021-06-07 16:34:50 +02:00
.setTitle(R.string.warning)
.setMessage(R.string.hidden_volume_warning)
.setPositiveButton(R.string.ok, null)
.show()
}
2020-07-17 16:35:39 +02:00
}
2021-06-07 16:34:50 +02:00
fun createVolume() {
loadVolumePath {
2021-06-11 20:23:54 +02:00
val password = binding.editPassword.text.toString().toCharArray()
val passwordConfirm = binding.editPasswordConfirm.text.toString().toCharArray()
2020-08-25 14:10:46 +02:00
if (!password.contentEquals(passwordConfirm)) {
Toast.makeText(this, R.string.passwords_mismatch, Toast.LENGTH_SHORT).show()
} else {
val volumeFile = File(currentVolumePath)
var goodDirectory = false
if (!volumeFile.isDirectory) {
if (volumeFile.mkdirs()) {
goodDirectory = true
} else {
errorDirectoryNotWritable(R.string.create_cant_write_error_msg)
}
} else {
val dirContent = volumeFile.list()
if (dirContent != null) {
if (dirContent.isEmpty()) {
if (volumeFile.canWrite()) {
2020-07-28 22:25:10 +02:00
goodDirectory = true
} else {
errorDirectoryNotWritable(R.string.create_cant_write_error_msg)
2020-07-28 22:25:10 +02:00
}
} else {
Toast.makeText(this, R.string.dir_not_empty, Toast.LENGTH_SHORT).show()
2020-08-25 14:10:46 +02:00
}
} else {
Toast.makeText(this, R.string.listdir_null_error_msg, Toast.LENGTH_SHORT).show()
}
}
if (goodDirectory) {
2021-11-09 11:12:09 +01:00
object: LoadingTask(this, themeValue, R.string.loading_msg_create) {
override fun doTask(activity: AppCompatActivity) {
2021-10-17 13:46:10 +02:00
val xchacha = when (binding.spinnerXchacha.selectedItemPosition) {
0 -> 0
1 -> 1
else -> -1
}
if (GocryptfsVolume.createVolume(currentVolumePath, password, false, xchacha, GocryptfsVolume.ScryptDefaultLogN, ConstValues.creator)) {
2020-08-25 14:10:46 +02:00
var returnedHash: ByteArray? = null
2021-06-11 20:23:54 +02:00
if (checkboxSavePassword.isChecked){
2020-08-25 14:10:46 +02:00
returnedHash = ByteArray(GocryptfsVolume.KeyLen)
}
sessionID = GocryptfsVolume.init(currentVolumePath, password, null, returnedHash)
2020-08-25 14:10:46 +02:00
if (sessionID != -1) {
2021-06-11 20:23:54 +02:00
if (checkboxRememberPath.isChecked) {
if (volumeDatabase.isVolumeSaved(currentVolumeName)) { //cleaning old saved path
volumeDatabase.removeVolume(Volume(currentVolumeName))
2020-08-25 14:10:46 +02:00
}
2021-06-11 20:23:54 +02:00
volumeDatabase.saveVolume(Volume(currentVolumeName, switchHiddenVolume.isChecked))
2020-08-25 14:10:46 +02:00
}
2021-06-11 20:23:54 +02:00
if (checkboxSavePassword.isChecked && returnedHash != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
2020-10-22 22:14:22 +02:00
stopTask {
savePasswordHash(returnedHash) {
startExplorer()
}
2020-10-15 17:43:13 +02:00
}
} else {
2020-08-25 14:10:46 +02:00
stopTask { startExplorer() }
}
} else {
stopTaskWithToast(R.string.open_volume_failed)
}
} else {
stopTask {
2021-11-09 11:12:09 +01:00
CustomAlertDialogBuilder(activity, themeValue)
2020-08-25 14:10:46 +02:00
.setTitle(R.string.error)
.setMessage(R.string.create_volume_failed)
.setPositiveButton(R.string.ok, null)
.show()
}
2020-07-28 22:25:10 +02:00
}
2020-07-27 16:20:52 +02:00
}
override fun doFinally(activity: AppCompatActivity) {
Arrays.fill(password, 0.toChar())
Arrays.fill(passwordConfirm, 0.toChar())
}
2020-08-25 14:10:46 +02:00
}
2020-07-17 16:35:39 +02:00
}
}
2020-07-28 22:25:10 +02:00
}
2020-07-17 16:35:39 +02:00
}
2020-07-27 16:20:52 +02:00
private fun startExplorer(){
2021-11-09 11:12:09 +01:00
CustomAlertDialogBuilder(this, themeValue)
2020-07-17 16:35:39 +02:00
.setTitle(R.string.success_volume_create)
.setMessage(R.string.success_volume_create_msg)
.setCancelable(false)
.setPositiveButton(R.string.ok) { _, _ ->
val intent = Intent(this, ExplorerActivity::class.java)
2020-07-17 16:35:39 +02:00
intent.putExtra("sessionID", sessionID)
intent.putExtra("volume_name", File(currentVolumeName).name)
2020-07-17 16:35:39 +02:00
startActivity(intent)
isStartingExplorer = true
2020-07-17 16:35:39 +02:00
finish()
}
.show()
}
override fun onPause() {
super.onPause()
//Closing volume if leaving activity while showing dialog
if (sessionID != -1 && !isStartingExplorer) {
2021-11-11 15:05:33 +01:00
GocryptfsVolume(applicationContext, sessionID).close()
finish()
}
}
2020-07-17 16:35:39 +02:00
override fun onDestroy() {
super.onDestroy()
2021-06-11 20:23:54 +02:00
Wiper.wipeEditText(binding.editPassword)
Wiper.wipeEditText(binding.editPasswordConfirm)
2020-07-17 16:35:39 +02:00
}
}