Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ dependencies {
implementation("com.github.devnied.emvnfccard:library:3.0.1")
implementation("net.grey-panther:natural-comparator:1.1")

implementation("com.github.keemobile:kotpass:0.6.1")
implementation("com.squareup.okio:okio:3.4.0") //Needed for Meta for kotpass

implementation("androidx.datastore:datastore:1.0.0")

"githubImplementation"("com.squareup.retrofit2:retrofit:2.9.0")
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@
android:name=".ui.sync.ImportActivity"
android:exported="true"
android:theme="@style/AppTheme.NoActionBar">

<intent-filter>
<action android:name="android.intent.action.VIEW" />

<category android:name="android.intent.category.DEFAULT" />

<data android:mimeType="application/octet-stream" />
<data android:pathPattern=".*\\.keygo" />
<data android:mimeType="application/x-kdbx" />
<data android:scheme="content" />
</intent-filter>
</activity>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package de.davis.passwordmanager.backup

enum class BackupOperation {
IMPORT,
EXPORT
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package de.davis.passwordmanager.backup

import java.io.InputStream
import java.io.OutputStream

interface BackupResourceProvider {

suspend fun provideInputStream(): InputStream
suspend fun provideOutputStream(): OutputStream

suspend fun getFileName(): String
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package de.davis.passwordmanager.backup

sealed interface BackupResult {

open class Success : BackupResult
data class SuccessWithDuplicates(val duplicates: Int) : Success()
}
140 changes: 32 additions & 108 deletions app/src/main/java/de/davis/passwordmanager/backup/DataBackup.kt
Original file line number Diff line number Diff line change
@@ -1,128 +1,52 @@
package de.davis.passwordmanager.backup

import android.content.Context
import android.content.DialogInterface
import android.net.Uri
import android.widget.Toast
import androidx.annotation.IntDef
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import de.davis.passwordmanager.R
import de.davis.passwordmanager.dialog.LoadingDialog
import de.davis.passwordmanager.backup.listener.BackupListener
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import java.io.InputStream
import java.io.OutputStream
import javax.crypto.AEADBadTagException

abstract class DataBackup(private val backupListener: BackupListener = BackupListener.Empty) {

const val TYPE_EXPORT = 0
const val TYPE_IMPORT = 1
protected abstract suspend fun ProgressContext.runImport(inputStream: InputStream): BackupResult
protected abstract suspend fun ProgressContext.runExport(outputStream: OutputStream): BackupResult

@IntDef(TYPE_EXPORT, TYPE_IMPORT)
annotation class Type

abstract class DataBackup(val context: Context) {

private lateinit var loadingDialog: LoadingDialog

@Throws(Exception::class)
internal abstract suspend fun runExport(outputStream: OutputStream): Result

@Throws(Exception::class)
internal abstract suspend fun runImport(inputStream: InputStream): Result

open suspend fun execute(@Type type: Int, uri: Uri, onSyncedHandler: OnSyncedHandler? = null) {
val resolver = context.contentResolver
loadingDialog = LoadingDialog(context).apply {
setTitle(if (type == TYPE_EXPORT) R.string.export else R.string.import_str)
setMessage(R.string.wait_text)
}
val alertDialog = withContext(Dispatchers.Main) { loadingDialog.show() }

try {
withContext(Dispatchers.IO) {
val result: Result = when (type) {
TYPE_EXPORT -> resolver.openOutputStream(uri)?.use { runExport(it) }!!

TYPE_IMPORT -> resolver.openInputStream(uri)?.use { runImport(it) }!!

else -> Result.Error("Unexpected error occurred")
}

handleResult(result, onSyncedHandler)
private val progressContext = object : ProgressContext {
override suspend fun initiateProgress(maxCount: Int) {
withContext(Dispatchers.Main) {
backupListener.initiateProgress(maxCount)
}
} catch (e: Exception) {
if (e is NullPointerException) return
error(e)
} finally {
alertDialog.dismiss()
}
}

internal suspend fun error(exception: Exception) {
val msg = if (exception is AEADBadTagException)
context.getString(R.string.password_does_not_match)
else
exception.message

withContext(Dispatchers.Main) {
MaterialAlertDialogBuilder(context).apply {
setTitle(R.string.error_title)
setMessage(msg)
setPositiveButton(R.string.ok) { _: DialogInterface?, _: Int -> }
}.show()
override suspend fun madeProgress(progress: Int) {
withContext(Dispatchers.Main) {
backupListener.onProgressUpdated(progress)
}
}

exception.printStackTrace()
}

private suspend fun handleResult(result: Result, onSyncedHandler: OnSyncedHandler?) =
withContext(Dispatchers.Main) {
if (result is Result.Success) {
Toast.makeText(
context,
if (result.type == TYPE_EXPORT) R.string.backup_stored else R.string.backup_restored,
Toast.LENGTH_LONG
).show()
handleSyncHandler(onSyncedHandler, result)
return@withContext
}

MaterialAlertDialogBuilder(context).apply {
setPositiveButton(R.string.ok) { _: DialogInterface?, _: Int ->
handleSyncHandler(
onSyncedHandler,
result
)
}

if (result is Result.Error) {
setTitle(R.string.error_title)
setMessage(result.message)
} else if (result is Result.Duplicate) {
setTitle(R.string.warning)
setMessage(
context.resources.getQuantityString(
R.plurals.item_existed,
result.count,
result.count
)
)
open suspend fun execute(
backupOperation: BackupOperation,
backupResourceProvider: BackupResourceProvider
) {
backupListener.run {
runCatching {
withContext(Dispatchers.Main) { onStart(backupOperation) }

backupResourceProvider.run {
progressContext.run {
when (backupOperation) {
BackupOperation.IMPORT -> provideInputStream().use { runImport(it) }
BackupOperation.EXPORT -> provideOutputStream().use { runExport(it) }
}
}
}
}.show()
}

private fun handleSyncHandler(onSyncedHandler: OnSyncedHandler?, result: Result) {
onSyncedHandler?.onSynced(result)
}

internal suspend fun notifyUpdate(current: Int, max: Int) {
withContext(Dispatchers.Main) {
loadingDialog.updateProgress(current, max)
}.onSuccess {
withContext(Dispatchers.Main) { onSuccess(backupOperation, it) }
}.onFailure {
withContext(Dispatchers.Main) { onFailure(backupOperation, it) }
}
}
}

interface OnSyncedHandler {
fun onSynced(result: Result?)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package de.davis.passwordmanager.backup

interface PasswordProvider {

suspend operator fun invoke(
backupOperation: BackupOperation,
backupResourceProvider: BackupResourceProvider,
callback: suspend (password: String) -> Unit
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package de.davis.passwordmanager.backup

interface ProgressContext {
suspend fun initiateProgress(maxCount: Int)
suspend fun madeProgress(progress: Int)
}
7 changes: 0 additions & 7 deletions app/src/main/java/de/davis/passwordmanager/backup/Result.kt

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,73 +1,39 @@
package de.davis.passwordmanager.backup

import android.content.Context
import android.content.DialogInterface
import android.net.Uri
import android.text.InputType
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.content.res.AppCompatResources
import com.google.android.material.textfield.TextInputLayout
import de.davis.passwordmanager.R
import de.davis.passwordmanager.dialog.EditDialogBuilder
import de.davis.passwordmanager.ui.views.InformationView.Information
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import de.davis.passwordmanager.backup.listener.BackupListener
import java.io.InputStream
import java.io.OutputStream

abstract class SecureDataBackup(context: Context) : DataBackup(context) {
abstract class SecureDataBackup(
private val passwordProvider: PasswordProvider,
backupListener: BackupListener = BackupListener.Empty
) : DataBackup(backupListener) {

lateinit var password: String
private lateinit var password: String

private suspend fun requestPassword(
@Type type: Int,
uri: Uri,
onSyncedHandler: OnSyncedHandler?
) {
val information = Information().apply {
hint = context.getString(R.string.password)
inputType = InputType.TYPE_TEXT_VARIATION_PASSWORD
isSecret = true
}
withContext(Dispatchers.Main) {
EditDialogBuilder(context).apply {
setTitle(R.string.password)
setPositiveButton(R.string.yes) { _: DialogInterface?, _: Int -> }
setButtonListener(
DialogInterface.BUTTON_POSITIVE,
R.string.yes
) { dialog, _, password ->
/*
Needed for the error message that appears when the password (field) is empty.
otherwise the dialog would close itself
*/
protected abstract suspend fun ProgressContext.runImport(
inputStream: InputStream,
password: String
): BackupResult

val alertDialog = dialog as AlertDialog
if (password.isEmpty()) {
alertDialog.findViewById<TextInputLayout>(R.id.textInputLayout)?.error =
context.getString(R.string.is_not_filled_in)
return@setButtonListener
}
alertDialog.dismiss()
this@SecureDataBackup.password = password
CoroutineScope(Job() + Dispatchers.IO).launch {
super.execute(type, uri, onSyncedHandler)
}
}
withInformation(information)
withStartIcon(
AppCompatResources.getDrawable(
context,
R.drawable.ic_baseline_password_24
)
)
setCancelable(type == TYPE_IMPORT)
}.show()
}
}
protected abstract suspend fun ProgressContext.runExport(
outputStream: OutputStream,
password: String
): BackupResult

override suspend fun execute(@Type type: Int, uri: Uri, onSyncedHandler: OnSyncedHandler?) {
requestPassword(type, uri, onSyncedHandler)
final override suspend fun ProgressContext.runImport(inputStream: InputStream): BackupResult =
runImport(inputStream, password)

final override suspend fun ProgressContext.runExport(outputStream: OutputStream): BackupResult =
runExport(outputStream, password)

override suspend fun execute(
backupOperation: BackupOperation,
backupResourceProvider: BackupResourceProvider
) {
passwordProvider(backupOperation, backupResourceProvider) {
password = it
super.execute(backupOperation, backupResourceProvider)
}
}
}
Loading