diff --git a/app/src/main/java/com/kunzisoft/keepass/activities/dialogs/SetMainCredentialDialogFragment.kt b/app/src/main/java/com/kunzisoft/keepass/activities/dialogs/SetMainCredentialDialogFragment.kt index dab99bae6..3724b9dcf 100644 --- a/app/src/main/java/com/kunzisoft/keepass/activities/dialogs/SetMainCredentialDialogFragment.kt +++ b/app/src/main/java/com/kunzisoft/keepass/activities/dialogs/SetMainCredentialDialogFragment.kt @@ -171,7 +171,7 @@ class SetMainCredentialDialogFragment : DatabaseDialogFragment() { hardwareKeySelectionView.error = if (!HardwareKeyResponseHelper.isHardwareKeyAvailable(requireActivity(), hardwareKey)) { // show hardware driver dialog if required - getString(R.string.warning_hardware_key_required) + getString(R.string.error_driver_required, hardwareKey.toString()) } else { null } @@ -236,7 +236,8 @@ class SetMainCredentialDialogFragment : DatabaseDialogFragment() { ) { // show hardware driver dialog if required error = true - hardwareKeySelectionView.error = getString(R.string.warning_hardware_key_required) + hardwareKeySelectionView.error = + getString(R.string.error_driver_required, hardwareKey.toString()) } if (!error) { mListener?.onAssignKeyDialogPositiveClick(retrieveMainCredential()) diff --git a/app/src/main/java/com/kunzisoft/keepass/activities/legacy/DatabaseActivity.kt b/app/src/main/java/com/kunzisoft/keepass/activities/legacy/DatabaseActivity.kt index 0bc31fb8c..d9194bd7c 100644 --- a/app/src/main/java/com/kunzisoft/keepass/activities/legacy/DatabaseActivity.kt +++ b/app/src/main/java/com/kunzisoft/keepass/activities/legacy/DatabaseActivity.kt @@ -6,9 +6,8 @@ import androidx.activity.viewModels import com.kunzisoft.keepass.activities.stylish.StylishActivity import com.kunzisoft.keepass.database.action.DatabaseTaskProvider import com.kunzisoft.keepass.database.element.Database -import com.kunzisoft.keepass.model.CipherEncryptDatabase import com.kunzisoft.keepass.database.element.MainCredential -import com.kunzisoft.keepass.hardware.HardwareKeyResponseHelper +import com.kunzisoft.keepass.model.CipherEncryptDatabase import com.kunzisoft.keepass.tasks.ActionRunnable import com.kunzisoft.keepass.viewmodels.DatabaseViewModel @@ -18,12 +17,10 @@ abstract class DatabaseActivity: StylishActivity(), DatabaseRetrieval { protected var mDatabaseTaskProvider: DatabaseTaskProvider? = null protected var mDatabase: Database? = null - private var mHardwareKeyResponseHelper = HardwareKeyResponseHelper(this) - override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - mDatabaseTaskProvider = DatabaseTaskProvider(this, mHardwareKeyResponseHelper) + mDatabaseTaskProvider = DatabaseTaskProvider(this) mDatabaseTaskProvider?.onDatabaseRetrieved = { database -> val databaseWasReloaded = database?.wasReloaded == true diff --git a/app/src/main/java/com/kunzisoft/keepass/database/action/DatabaseTaskProvider.kt b/app/src/main/java/com/kunzisoft/keepass/database/action/DatabaseTaskProvider.kt index 1e3f88986..cf6f3fe83 100644 --- a/app/src/main/java/com/kunzisoft/keepass/database/action/DatabaseTaskProvider.kt +++ b/app/src/main/java/com/kunzisoft/keepass/database/action/DatabaseTaskProvider.kt @@ -38,6 +38,7 @@ import com.kunzisoft.keepass.database.crypto.kdf.KdfEngine import com.kunzisoft.keepass.database.element.Database import com.kunzisoft.keepass.database.element.Entry import com.kunzisoft.keepass.database.element.Group +import com.kunzisoft.keepass.database.element.MainCredential import com.kunzisoft.keepass.database.element.database.CompressionAlgorithm import com.kunzisoft.keepass.database.element.node.Node import com.kunzisoft.keepass.database.element.node.NodeId @@ -46,7 +47,6 @@ import com.kunzisoft.keepass.database.exception.InvalidCredentialsDatabaseExcept import com.kunzisoft.keepass.hardware.HardwareKey import com.kunzisoft.keepass.hardware.HardwareKeyResponseHelper import com.kunzisoft.keepass.model.CipherEncryptDatabase -import com.kunzisoft.keepass.database.element.MainCredential import com.kunzisoft.keepass.model.SnapFileDatabaseInfo import com.kunzisoft.keepass.services.DatabaseTaskNotificationService import com.kunzisoft.keepass.services.DatabaseTaskNotificationService.Companion.ACTION_DATABASE_ASSIGN_PASSWORD_TASK @@ -95,7 +95,6 @@ import java.util.* class DatabaseTaskProvider { private var activity: FragmentActivity? = null - private var service: Service? = null private var context: Context var onDatabaseRetrieved: ((database: Database?) -> Unit)? = null @@ -114,44 +113,35 @@ class DatabaseTaskProvider { private var progressTaskDialogFragment: ProgressTaskDialogFragment? = null private var databaseChangedDialogFragment: DatabaseChangedDialogFragment? = null - // To manage hardware key challenge response - private var mHardwareKeyResponseHelper: HardwareKeyResponseHelper? = null - private var mChallengeResponseData: ByteArray? = null - private var mCancelChallengeResponse: Boolean = false - constructor(activity: FragmentActivity, - hardwareKeyResponseHelper: HardwareKeyResponseHelper? = null) { + constructor(activity: FragmentActivity) { this.activity = activity this.context = activity this.intentDatabaseTask = Intent(activity.applicationContext, DatabaseTaskNotificationService::class.java) - if (hardwareKeyResponseHelper != null) { - this.mHardwareKeyResponseHelper = hardwareKeyResponseHelper - this.mHardwareKeyResponseHelper?.buildHardwareKeyResponse { responseData, _ -> - // TODO Verify database - mChallengeResponseData = responseData ?: ByteArray(0) - respondToChallengeIfAllowed() - } - this.requestChallengeListener = object: DatabaseTaskNotificationService.RequestChallengeListener { - override fun onChallengeResponseRequested(hardwareKey: HardwareKey, seed: ByteArray?) { - if (HardwareKeyResponseHelper.isHardwareKeyAvailable(activity, hardwareKey)) { - mHardwareKeyResponseHelper?.launchChallengeForResponse(hardwareKey, seed) - } else { - throw InvalidCredentialsDatabaseException("Driver for $hardwareKey is required.") - } + // To manage hardware key challenge response + val hardwareKeyResponseHelper = HardwareKeyResponseHelper(activity) + hardwareKeyResponseHelper.buildHardwareKeyResponse { responseData, _ -> + // TODO Verify database + mChallengeResponseData = responseData ?: ByteArray(0) + respondToChallengeIfAllowed() + } + this.requestChallengeListener = object: DatabaseTaskNotificationService.RequestChallengeListener { + override fun onChallengeResponseRequested(hardwareKey: HardwareKey, seed: ByteArray?) { + if (HardwareKeyResponseHelper.isHardwareKeyAvailable(activity, hardwareKey)) { + hardwareKeyResponseHelper.launchChallengeForResponse(hardwareKey, seed) + } else { + throw InvalidCredentialsDatabaseException( + context.getString(R.string.error_driver_required, hardwareKey.toString()) + ) } } - } else { - // It's not a credential screen, cancel the challenge response - mCancelChallengeResponse = true - cancelChallengeResponseIfAllowed() } } constructor(service: Service) { - this.service = service this.context = service this.intentDatabaseTask = Intent(service.applicationContext, DatabaseTaskNotificationService::class.java) @@ -235,12 +225,9 @@ class DatabaseTaskProvider { } } - private fun cancelChallengeResponseIfAllowed() { - mBinder?.let { binder -> - if (mCancelChallengeResponse) { - binder.getService().cancelChallengeResponse() - } - } + // TODO Cancel challenge response + private fun cancelChallengeResponse() { + mBinder?.getService()?.cancelChallengeResponse() } private fun startDialog(titleId: Int?, @@ -299,7 +286,6 @@ class DatabaseTaskProvider { getService().checkAction() } respondToChallengeIfAllowed() - cancelChallengeResponseIfAllowed() } override fun onServiceDisconnected(name: ComponentName?) { @@ -398,7 +384,7 @@ class DatabaseTaskProvider { context.startService(intentDatabaseTask) } catch (e: Exception) { Log.e(TAG, "Unable to perform database action", e) - Toast.makeText(activity, R.string.error_start_database_action, Toast.LENGTH_LONG).show() + Toast.makeText(context, R.string.error_start_database_action, Toast.LENGTH_LONG).show() } } diff --git a/app/src/main/java/com/kunzisoft/keepass/hardware/HardwareKeyResponseHelper.kt b/app/src/main/java/com/kunzisoft/keepass/hardware/HardwareKeyResponseHelper.kt index 844811ced..0025737dc 100644 --- a/app/src/main/java/com/kunzisoft/keepass/hardware/HardwareKeyResponseHelper.kt +++ b/app/src/main/java/com/kunzisoft/keepass/hardware/HardwareKeyResponseHelper.kt @@ -93,14 +93,16 @@ class HardwareKeyResponseHelper { private const val YKDROID_SEED_KEY = "challenge" private const val EXTRA_BUNDLE_KEY = "EXTRA_BUNDLE_KEY" - fun isHardwareKeyAvailable(activity: FragmentActivity, - hardwareKey: HardwareKey, - showDialog: Boolean = true): Boolean { + fun isHardwareKeyAvailable( + activity: FragmentActivity, + hardwareKey: HardwareKey, + showDialog: Boolean = true + ): Boolean { return when (hardwareKey) { HardwareKey.FIDO2_SECRET -> { // TODO FIDO2 if (showDialog) - showHardwareKeyDriverNeeded(activity) + showHardwareKeyDriverNeeded(activity, hardwareKey) false } HardwareKey.CHALLENGE_RESPONSE_YUBIKEY -> { @@ -110,11 +112,17 @@ class HardwareKeyResponseHelper { } } - private fun showHardwareKeyDriverNeeded(activity: FragmentActivity) { + private fun showHardwareKeyDriverNeeded( + activity: FragmentActivity, + hardwareKey: HardwareKey + ) { activity.lifecycleScope.launch { val builder = AlertDialog.Builder(activity) - builder.setMessage(R.string.warning_hardware_key_required) - .setPositiveButton(android.R.string.ok) { _, _ -> + builder + .setMessage( + activity.getString(R.string.error_driver_required, hardwareKey.toString()) + ) + .setPositiveButton(R.string.download) { _, _ -> UriUtil.openExternalApp(activity, UriUtil.KEEPASSDX_PRO_PACKAGE) } .setNegativeButton(android.R.string.cancel) { _, _ -> } diff --git a/app/src/main/java/com/kunzisoft/keepass/tasks/ProgressTaskDialogFragment.kt b/app/src/main/java/com/kunzisoft/keepass/tasks/ProgressTaskDialogFragment.kt index 91f037367..fead3e950 100644 --- a/app/src/main/java/com/kunzisoft/keepass/tasks/ProgressTaskDialogFragment.kt +++ b/app/src/main/java/com/kunzisoft/keepass/tasks/ProgressTaskDialogFragment.kt @@ -31,7 +31,9 @@ import androidx.annotation.StringRes import androidx.appcompat.app.AlertDialog import androidx.core.view.isVisible import androidx.fragment.app.DialogFragment +import androidx.lifecycle.lifecycleScope import com.kunzisoft.keepass.R +import kotlinx.coroutines.launch open class ProgressTaskDialogFragment : DialogFragment() { @@ -89,7 +91,7 @@ open class ProgressTaskDialogFragment : DialogFragment() { } private fun updateView(textView: TextView?, @StringRes resId: Int) { - activity?.runOnUiThread { + activity?.lifecycleScope?.launch { if (resId == UNDEFINED) { textView?.visibility = View.GONE } else { @@ -99,6 +101,15 @@ open class ProgressTaskDialogFragment : DialogFragment() { } } + private fun updateCancelable() { + activity?.lifecycleScope?.launch { + cancelButton?.isVisible = cancellable != null + cancelButton?.setOnClickListener { + cancellable?.invoke() + } + } + } + fun updateTitle(@StringRes resId: Int) { this.title = resId updateView(titleView, title) @@ -116,10 +127,7 @@ open class ProgressTaskDialogFragment : DialogFragment() { fun setCancellable(cancellable: (() -> Unit)?) { this.cancellable = cancellable - cancelButton?.isVisible = cancellable != null - cancelButton?.setOnClickListener { - cancellable?.invoke() - } + updateCancelable() } companion object { diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index fa0ae2ed7..eca471b12 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -201,6 +201,7 @@ Response already provided. Unable to get the response from the challenge. Cancelled by user. + Driver for %1$s is required. Field name Field value Could not find file. Try reopening it from your file browser. @@ -364,7 +365,6 @@ Access to the file revoked by the file manager, close the database and reopen it from its location. You have not allowed the app to use an exact alarm. As a result, the features requiring a timer will not be done with an exact time. The hash of the file is not guaranteed because Android can change its data on the fly. Change the file extension to .bin for correct integrity. - Driver for this hardware is required. Permission Version %1$s Build %1$s