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

216 lines
9.6 KiB
Kotlin
Raw Normal View History

2020-07-17 16:35:39 +02:00
package sushi.hardcore.droidfs
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
import android.text.Editable
import android.text.TextWatcher
import android.widget.AdapterView.OnItemClickListener
import android.widget.Toast
2020-07-28 22:25:10 +02:00
import androidx.appcompat.app.AppCompatActivity
2020-07-17 16:35:39 +02:00
import kotlinx.android.synthetic.main.activity_change_password.*
2020-10-15 17:43:13 +02:00
import kotlinx.android.synthetic.main.checkboxes_section.*
import kotlinx.android.synthetic.main.volume_path_section.*
2020-07-17 16:35:39 +02:00
import sushi.hardcore.droidfs.adapters.SavedVolumesAdapter
2021-06-07 14:12:40 +02:00
import sushi.hardcore.droidfs.util.PathUtils
import sushi.hardcore.droidfs.util.WidgetUtil
import sushi.hardcore.droidfs.util.Wiper
2020-07-27 16:20:52 +02:00
import sushi.hardcore.droidfs.widgets.ColoredAlertDialogBuilder
2020-08-25 14:10:46 +02:00
import java.io.File
2020-07-17 16:35:39 +02:00
import java.util.*
2020-10-22 22:14:22 +02:00
class ChangePasswordActivity : VolumeActionActivity() {
2020-10-15 17:43:13 +02:00
private lateinit var savedVolumesAdapter: SavedVolumesAdapter
2020-07-17 16:35:39 +02:00
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_change_password)
2021-06-07 16:34:50 +02:00
setupLayout()
2020-10-22 22:14:22 +02:00
setupFingerprintStuff()
savedVolumesAdapter = SavedVolumesAdapter(this, volumeDatabase)
2020-07-17 16:35:39 +02:00
if (savedVolumesAdapter.count > 0){
saved_path_listview.adapter = savedVolumesAdapter
saved_path_listview.onItemClickListener = OnItemClickListener { _, _, position, _ ->
val volume = savedVolumesAdapter.getItem(position)
currentVolumeName = volume.name
if (volume.isHidden){
switch_hidden_volume.isChecked = true
edit_volume_name.setText(currentVolumeName)
} else {
switch_hidden_volume.isChecked = false
edit_volume_path.setText(currentVolumeName)
}
2021-06-07 16:34:50 +02:00
onClickSwitchHiddenVolume()
2020-07-17 16:35:39 +02:00
}
} else {
WidgetUtil.hideWithPadding(saved_path_listview)
2020-07-17 16:35:39 +02:00
}
val textWatcher = object: TextWatcher{
2020-07-17 16:35:39 +02:00
override fun afterTextChanged(s: Editable?) {
}
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
}
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
if (volumeDatabase.isVolumeSaved(s.toString())){
2020-10-15 17:43:13 +02:00
checkbox_remember_path.isEnabled = false
checkbox_remember_path.isChecked = false
if (volumeDatabase.isHashSaved(s.toString())){
2020-10-15 17:43:13 +02:00
edit_old_password.text = null
edit_old_password.hint = getString(R.string.hash_saved_hint)
edit_old_password.isEnabled = false
} else {
edit_old_password.hint = null
edit_old_password.isEnabled = true
}
} else {
checkbox_remember_path.isEnabled = true
2020-07-17 16:35:39 +02:00
edit_old_password.hint = null
2020-07-28 22:25:10 +02:00
edit_old_password.isEnabled = true
2020-07-17 16:35:39 +02:00
}
}
}
edit_volume_path.addTextChangedListener(textWatcher)
edit_volume_name.addTextChangedListener(textWatcher)
2021-06-07 16:34:50 +02:00
edit_new_password_confirm.setOnEditorActionListener { _, _, _ ->
checkVolumePathThenChangePassword()
2020-07-17 16:35:39 +02:00
true
}
2021-06-07 16:34:50 +02:00
button_change_password.setOnClickListener {
checkVolumePathThenChangePassword()
}
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){
edit_volume_path.setText(path)
} 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.change_pwd_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 checkVolumePathThenChangePassword() {
loadVolumePath {
val volumeFile = File(currentVolumePath)
if (!GocryptfsVolume.isGocryptfsVolume(volumeFile)){
2020-10-14 19:04:19 +02:00
ColoredAlertDialogBuilder(this)
.setTitle(R.string.error)
.setMessage(R.string.error_not_a_volume)
.setPositiveButton(R.string.ok, null)
.show()
} else if (!volumeFile.canWrite()){
2020-08-25 14:10:46 +02:00
ColoredAlertDialogBuilder(this)
.setTitle(R.string.warning)
.setMessage(R.string.change_pwd_cant_write_error_msg)
.setPositiveButton(R.string.ok, null)
.show()
} else {
changePassword()
2020-08-25 14:10:46 +02:00
}
2020-07-17 16:35:39 +02:00
}
}
private fun changePassword(givenHash: ByteArray? = null){
2020-08-25 14:10:46 +02:00
val newPassword = edit_new_password.text.toString().toCharArray()
val newPasswordConfirm = edit_new_password_confirm.text.toString().toCharArray()
if (!newPassword.contentEquals(newPasswordConfirm)) {
Toast.makeText(this, R.string.passwords_mismatch, Toast.LENGTH_SHORT).show()
} else {
object : LoadingTask(this, R.string.loading_msg_change_password) {
override fun doTask(activity: AppCompatActivity) {
2020-07-28 22:25:10 +02:00
val oldPassword = edit_old_password.text.toString().toCharArray()
var returnedHash: ByteArray? = null
2020-10-22 22:14:22 +02:00
if (checkbox_save_password.isChecked) {
2020-07-28 22:25:10 +02:00
returnedHash = ByteArray(GocryptfsVolume.KeyLen)
2020-07-17 16:35:39 +02:00
}
2020-07-28 22:25:10 +02:00
var changePasswordImmediately = true
2020-08-25 14:10:46 +02:00
if (givenHash == null) {
var volume: Volume? = null
volumeDatabase.getVolumes().forEach { testVolume ->
if (testVolume.name == currentVolumeName){
volume = testVolume
}
}
volume?.let {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
it.hash?.let { hash ->
it.iv?.let { iv ->
currentVolumePath = if (it.isHidden){
PathUtils.pathJoin(filesDir.path, it.name)
} else {
it.name
}
stopTask {
loadPasswordHash(hash, iv, ::changePassword)
}
changePasswordImmediately = false
}
}
2020-07-28 22:25:10 +02:00
}
2020-07-17 16:35:39 +02:00
}
2020-07-28 22:25:10 +02:00
}
2020-08-25 14:10:46 +02:00
if (changePasswordImmediately) {
if (GocryptfsVolume.changePassword(currentVolumePath, oldPassword, givenHash, newPassword, returnedHash)) {
val volume = Volume(currentVolumeName, switch_hidden_volume.isChecked)
if (volumeDatabase.isHashSaved(currentVolumeName)) {
volumeDatabase.removeHash(volume)
2020-07-27 16:20:52 +02:00
}
2020-07-28 22:25:10 +02:00
if (checkbox_remember_path.isChecked) {
volumeDatabase.saveVolume(volume)
2020-07-17 16:35:39 +02:00
}
2020-10-22 22:14:22 +02:00
if (checkbox_save_password.isChecked && returnedHash != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
stopTask {
savePasswordHash(returnedHash) {
onPasswordChanged()
}
2020-10-15 17:43:13 +02:00
}
} else {
2020-07-28 22:25:10 +02:00
stopTask { onPasswordChanged() }
}
} else {
stopTask {
ColoredAlertDialogBuilder(activity)
.setTitle(R.string.error)
.setMessage(R.string.change_password_failed)
.setPositiveButton(R.string.ok, null)
.show()
}
2020-07-17 16:35:39 +02:00
}
}
2020-07-28 22:25:10 +02:00
Arrays.fill(oldPassword, 0.toChar())
2020-07-17 16:35:39 +02:00
}
2020-08-25 14:10:46 +02:00
override fun doFinally(activity: AppCompatActivity) {
Arrays.fill(newPassword, 0.toChar())
Arrays.fill(newPasswordConfirm, 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 onPasswordChanged(){
ColoredAlertDialogBuilder(this)
2020-07-17 16:35:39 +02:00
.setTitle(R.string.success_change_password)
.setMessage(R.string.success_change_password_msg)
.setCancelable(false)
.setPositiveButton(R.string.ok) { _, _ -> finish() }
.show()
}
override fun onDestroy() {
super.onDestroy()
Wiper.wipeEditText(edit_old_password)
Wiper.wipeEditText(edit_new_password)
Wiper.wipeEditText(edit_new_password_confirm)
}
}