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
|
2020-08-25 14:10:46 +02:00
|
|
|
import android.widget.Toast
|
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
|
2021-03-17 15:49:59 +01:00
|
|
|
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()
|
2020-10-22 22:14:22 +02:00
|
|
|
setupFingerprintStuff()
|
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-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() {
|
2020-11-04 18:42:04 +01:00
|
|
|
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) {
|
2020-11-04 18:42:04 +01:00
|
|
|
val volumeFile = File(currentVolumePath)
|
2020-08-25 14:10:46 +02:00
|
|
|
var goodDirectory = false
|
2020-11-04 18:42:04 +01:00
|
|
|
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 {
|
2020-11-04 18:42:04 +01:00
|
|
|
val dirContent = volumeFile.list()
|
2020-08-25 14:10:46 +02:00
|
|
|
if (dirContent != null){
|
|
|
|
if (dirContent.isEmpty()) {
|
2020-11-04 18:42:04 +01:00
|
|
|
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-06-11 16:27:08 +02:00
|
|
|
if (GocryptfsVolume.createVolume(currentVolumePath, password, false, 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)
|
|
|
|
}
|
2020-11-04 18:42:04 +01:00
|
|
|
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) {
|
2021-03-17 15:49:59 +01:00
|
|
|
if (volumeDatabase.isVolumeSaved(currentVolumeName)) { //cleaning old saved path
|
2020-11-04 18:42:04 +01:00
|
|
|
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) { _, _ ->
|
2021-03-17 15:49:59 +01:00
|
|
|
val intent = Intent(this, ExplorerActivity::class.java)
|
2020-07-17 16:35:39 +02:00
|
|
|
intent.putExtra("sessionID", sessionID)
|
2020-11-04 18:42:04 +01:00
|
|
|
intent.putExtra("volume_name", File(currentVolumeName).name)
|
2020-07-17 16:35:39 +02:00
|
|
|
startActivity(intent)
|
2021-03-17 15:49:59 +01:00
|
|
|
isStartingExplorer = true
|
2020-07-17 16:35:39 +02:00
|
|
|
finish()
|
|
|
|
}
|
|
|
|
.show()
|
|
|
|
}
|
|
|
|
|
2020-10-23 13:21:14 +02:00
|
|
|
override fun onPause() {
|
|
|
|
super.onPause()
|
|
|
|
//Closing volume if leaving activity while showing dialog
|
2021-03-17 15:49:59 +01:00
|
|
|
if (sessionID != -1 && !isStartingExplorer) {
|
2020-10-23 13:21:14 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|