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

216 lines
9.7 KiB
Kotlin
Raw Normal View History

2020-07-17 16:35:39 +02:00
package sushi.hardcore.droidfs
import android.content.Intent
2021-06-07 14:12:40 +02:00
import android.net.Uri
2020-07-17 16:35:39 +02:00
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.PathUtils
import sushi.hardcore.droidfs.util.Wiper
2020-07-27 16:20:52 +02:00
import sushi.hardcore.droidfs.widgets.ColoredAlertDialogBuilder
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) {
ColoredAlertDialogBuilder(this@CreateActivity)
.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-06-07 16:34:50 +02:00
ColoredAlertDialogBuilder(this)
.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 14:12:40 +02:00
override fun onDirectoryPicked(uri: Uri) {
if (PathUtils.isTreeUriOnPrimaryStorage(uri)){
val path = PathUtils.getFullPathFromTreeUri(uri, this)
if (path != null){
2021-06-11 20:23:54 +02:00
editVolumePath.setText(path)
2021-06-07 14:12:40 +02:00
} else {
ColoredAlertDialogBuilder(this)
.setTitle(R.string.error)
.setMessage(R.string.path_from_uri_null_error_msg)
.setPositiveButton(R.string.ok, null)
.show()
2020-07-17 16:35:39 +02:00
}
2021-06-07 14:12:40 +02:00
} else {
ColoredAlertDialogBuilder(this)
.setTitle(R.string.warning)
.setMessage(R.string.create_on_sdcard_error_msg)
.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 {
object: LoadingTask(this, R.string.loading_msg_create){
override fun doTask(activity: AppCompatActivity) {
val volumeFile = File(currentVolumePath)
2020-08-25 14:10:46 +02:00
var goodDirectory = false
if (!volumeFile.isDirectory) {
if (volumeFile.mkdirs()) {
2020-07-28 22:25:10 +02:00
goodDirectory = true
} else {
2020-08-25 14:10:46 +02:00
stopTask {
ColoredAlertDialogBuilder(activity)
.setTitle(R.string.warning)
.setMessage(R.string.create_cant_write_error_msg)
.setPositiveButton(R.string.ok, null)
.show()
}
2020-07-28 22:25:10 +02:00
}
} else {
val dirContent = volumeFile.list()
2020-08-25 14:10:46 +02:00
if (dirContent != null){
if (dirContent.isEmpty()) {
if (volumeFile.canWrite()){
2020-08-25 14:10:46 +02:00
goodDirectory = true
2020-07-28 22:25:10 +02:00
} else {
2020-08-25 14:10:46 +02:00
stopTask {
ColoredAlertDialogBuilder(activity)
.setTitle(R.string.warning)
.setMessage(R.string.create_cant_write_error_msg)
.setPositiveButton(R.string.ok, null)
.show()
2020-07-28 22:25:10 +02:00
}
2020-07-27 16:20:52 +02:00
}
2020-08-25 14:10:46 +02:00
} else {
stopTaskWithToast(R.string.dir_not_empty)
2020-07-28 22:25:10 +02:00
}
} else {
2020-08-25 14:10:46 +02:00
stopTaskWithToast(R.string.listdir_null_error_msg)
2020-07-27 16:20:52 +02:00
}
2020-08-25 14:10:46 +02:00
}
if (goodDirectory) {
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 {
ColoredAlertDialogBuilder(activity)
.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
}
2020-07-17 16:35:39 +02:00
}
2020-08-25 14:10:46 +02:00
override fun doFinally(activity: AppCompatActivity) {
Arrays.fill(password, 0.toChar())
Arrays.fill(passwordConfirm, 0.toChar())
}
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(){
ColoredAlertDialogBuilder(this)
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) {
GocryptfsVolume(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
}
}