Fix volume copying

This commit is contained in:
Matéo Duparc 2023-05-02 14:24:59 +02:00
parent 481558bd56
commit f4f3239bb1
Signed by untrusted user: hardcoresushi
GPG Key ID: AFE384344A45E13A

View File

@ -46,9 +46,9 @@ class MainActivity : BaseActivity(), VolumeAdapter.Listener {
finish() finish()
} }
} }
private var changePasswordPosition: Int? = null private var selectedVolumePosition: Int? = null
private var changePassword = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { private var changePassword = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
changePasswordPosition?.let { unselect(it) } selectedVolumePosition?.let { unselect(it) }
} }
private val pickDirectory = registerForActivityResult(ActivityResultContracts.OpenDocumentTree()) { uri -> private val pickDirectory = registerForActivityResult(ActivityResultContracts.OpenDocumentTree()) { uri ->
if (uri != null) if (uri != null)
@ -147,6 +147,7 @@ class MainActivity : BaseActivity(), VolumeAdapter.Listener {
recreate() recreate()
} else { } else {
volumeAdapter.refresh() volumeAdapter.refresh()
invalidateOptionsMenu()
if (volumeAdapter.volumes.isNotEmpty()) { if (volumeAdapter.volumes.isNotEmpty()) {
binding.textNoVolumes.visibility = View.GONE binding.textNoVolumes.visibility = View.GONE
} }
@ -292,9 +293,9 @@ class MainActivity : BaseActivity(), VolumeAdapter.Listener {
true true
} }
R.id.change_password -> { R.id.change_password -> {
changePasswordPosition = volumeAdapter.selectedItems.elementAt(0) selectedVolumePosition = volumeAdapter.selectedItems.elementAt(0)
changePassword.launch(Intent(this, ChangePasswordActivity::class.java).apply { changePassword.launch(Intent(this, ChangePasswordActivity::class.java).apply {
putExtra("volume", volumeAdapter.volumes[changePasswordPosition!!]) putExtra("volume", volumeAdapter.volumes[selectedVolumePosition!!])
}) })
true true
} }
@ -304,24 +305,23 @@ class MainActivity : BaseActivity(), VolumeAdapter.Listener {
true true
} }
R.id.copy -> { R.id.copy -> {
val position = volumeAdapter.selectedItems.elementAt(0) selectedVolumePosition = volumeAdapter.selectedItems.elementAt(0)
val volume = volumeAdapter.volumes[position] val volume = volumeAdapter.volumes[selectedVolumePosition!!]
when { if (volume.isHidden) {
volume.isHidden -> { PathUtils.safePickDirectory(pickDirectory, this, theme)
PathUtils.safePickDirectory(pickDirectory, this, theme) } else {
} val hiddenVolumeFile = File(VolumeData.getHiddenVolumeFullPath(filesDir.path, volume.shortName))
File(filesDir, volume.shortName).exists() -> { if (hiddenVolumeFile.exists()) {
CustomAlertDialogBuilder(this, theme) CustomAlertDialogBuilder(this, theme)
.setTitle(R.string.error) .setTitle(R.string.error)
.setMessage(R.string.hidden_volume_already_exists) .setMessage(R.string.hidden_volume_already_exists)
.setPositiveButton(R.string.ok, null) .setPositiveButton(R.string.ok, null)
.show() .show()
} } else {
else -> { unselect(selectedVolumePosition!!)
unselect(position)
copyVolume( copyVolume(
DocumentFile.fromFile(File(volume.name)), DocumentFile.fromFile(File(volume.name)),
DocumentFile.fromFile(filesDir), DocumentFile.fromFile(hiddenVolumeFile.parentFile!!),
) { ) {
VolumeData(volume.shortName, true, volume.type, volume.encryptedHash, volume.iv) VolumeData(volume.shortName, true, volume.type, volume.encryptedHash, volume.iv)
} }
@ -379,9 +379,8 @@ class MainActivity : BaseActivity(), VolumeAdapter.Listener {
} }
private fun onDirectoryPicked(uri: Uri) { private fun onDirectoryPicked(uri: Uri) {
val position = volumeAdapter.selectedItems.elementAt(0) val volume = volumeAdapter.volumes[selectedVolumePosition!!]
val volume = volumeAdapter.volumes[position] unselect(selectedVolumePosition!!)
unselect(position)
val dstDocumentFile = DocumentFile.fromTreeUri(this, uri) val dstDocumentFile = DocumentFile.fromTreeUri(this, uri)
if (dstDocumentFile == null) { if (dstDocumentFile == null) {
CustomAlertDialogBuilder(this, theme) CustomAlertDialogBuilder(this, theme)
@ -409,6 +408,13 @@ class MainActivity : BaseActivity(), VolumeAdapter.Listener {
} }
} }
/**
* Copy a volume.
*
* @param srcDocumentFile [DocumentFile] of the volume to copy
* @param dstDocumentFile [DocumentFile] of the destination PARENT FOLDER
* @param getResultVolume A function that returns the [VolumeData] corresponding to the destination volume. Takes the [DocumentFile] of the newly created volume (not the parent folder).
*/
private fun copyVolume(srcDocumentFile: DocumentFile, dstDocumentFile: DocumentFile, getResultVolume: (DocumentFile) -> VolumeData?) { private fun copyVolume(srcDocumentFile: DocumentFile, dstDocumentFile: DocumentFile, getResultVolume: (DocumentFile) -> VolumeData?) {
lifecycleScope.launch { lifecycleScope.launch {
val result = fileOperationService.copyVolume(srcDocumentFile, dstDocumentFile) val result = fileOperationService.copyVolume(srcDocumentFile, dstDocumentFile)