Delete AutofillMatches when deleting files (#659)

This commit is contained in:
Fabian Henneke 2020-03-25 17:13:30 +01:00 committed by GitHub
parent ff1b1ba39b
commit fde8137b62
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 5 deletions

View file

@ -499,6 +499,8 @@ class PasswordStore : AppCompatActivity() {
MaterialAlertDialogBuilder(this) MaterialAlertDialogBuilder(this)
.setMessage(resources.getString(R.string.delete_dialog_text, item.longName)) .setMessage(resources.getString(R.string.delete_dialog_text, item.longName))
.setPositiveButton(resources.getString(R.string.dialog_yes)) { _, _ -> .setPositiveButton(resources.getString(R.string.dialog_yes)) { _, _ ->
val filesToDelete = FileUtils.listFiles(item.file, null, true)
AutofillMatcher.updateMatches(applicationContext, delete = filesToDelete)
item.file.deleteRecursively() item.file.deleteRecursively()
adapter.remove(position) adapter.remove(position)
it.remove() it.remove()
@ -665,7 +667,7 @@ class PasswordStore : AppCompatActivity() {
// TODO this should show a warning to the user // TODO this should show a warning to the user
Timber.tag(TAG).e("Something went wrong while moving.") Timber.tag(TAG).e("Something went wrong while moving.")
} else { } else {
AutofillMatcher.updateMatchesFor(this, sourceDestinationMap) AutofillMatcher.updateMatches(this, sourceDestinationMap)
commitChange(this.resources commitChange(this.resources
.getString( .getString(
R.string.git_commit_move_text, R.string.git_commit_move_text,

View file

@ -148,10 +148,11 @@ class AutofillMatcher {
/** /**
* Goes through all existing matches and updates their associated entries by using * Goes through all existing matches and updates their associated entries by using
* [sourceDestinationMap] as a lookup table. * [moveFromTo] as a lookup table and deleting the matches for files in [delete].
*/ */
fun updateMatchesFor(context: Context, sourceDestinationMap: Map<File, File>) { fun updateMatches(context: Context, moveFromTo: Map<File, File> = emptyMap(), delete: Collection<File> = emptyList()) {
val oldNewPathMap = sourceDestinationMap.mapValues { it.value.absolutePath } val deletePathList = delete.map { it.absolutePath }
val oldNewPathMap = moveFromTo.mapValues { it.value.absolutePath }
.mapKeys { it.key.absolutePath } .mapKeys { it.key.absolutePath }
for (prefs in listOf(context.autofillAppMatches, context.autofillWebMatches)) { for (prefs in listOf(context.autofillAppMatches, context.autofillWebMatches)) {
for ((key, value) in prefs.all) { for ((key, value) in prefs.all) {
@ -164,7 +165,10 @@ class AutofillMatcher {
// Delete all matches for file locations that are going to be overwritten, then // Delete all matches for file locations that are going to be overwritten, then
// transfer matches over to the files at their new locations. // transfer matches over to the files at their new locations.
val newMatches = val newMatches =
oldMatches.asSequence().minus(oldNewPathMap.values).map { match -> oldMatches.asSequence()
.minus(deletePathList)
.minus(oldNewPathMap.values)
.map { match ->
val newPath = oldNewPathMap[match] ?: return@map match val newPath = oldNewPathMap[match] ?: return@map match
d { "Updating match for $key: $match --> $newPath" } d { "Updating match for $key: $match --> $newPath" }
newPath newPath