diff --git a/app/src/main/java/com/kunzisoft/keepass/view/OtpDisplayView.kt b/app/src/main/java/com/kunzisoft/keepass/view/OtpDisplayView.kt
index 715760711..1c17d8b5f 100644
--- a/app/src/main/java/com/kunzisoft/keepass/view/OtpDisplayView.kt
+++ b/app/src/main/java/com/kunzisoft/keepass/view/OtpDisplayView.kt
@@ -1,3 +1,22 @@
+/*
+ * Copyright 2026 Jeremy Jamet / Kunzisoft.
+ *
+ * This file is part of KeePassDX.
+ *
+ * KeePassDX is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * KeePassDX is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with KeePassDX. If not, see .
+ *
+ */
package com.kunzisoft.keepass.view
import android.content.Context
@@ -15,6 +34,10 @@ import com.kunzisoft.keepass.otp.OtpElement
import com.kunzisoft.keepass.settings.PreferencesUtil
import com.kunzisoft.keepass.timeout.ClipboardHelper
+/**
+ * View to display an OTP token and its progress.
+ * It supports protection (masking) and copying to clipboard.
+ */
class OtpDisplayView @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
@@ -36,6 +59,9 @@ class OtpDisplayView @JvmOverloads constructor(
override var onRevealChanged: ((isRevealed: Boolean) -> Unit)? = null
+ /**
+ * Callback when the OTP is updated.
+ */
var onOtpUpdated: ((OtpElement?) -> Unit)? = null
@@ -45,6 +71,9 @@ class OtpDisplayView @JvmOverloads constructor(
otpTokenView = findViewById(R.id.otp_token)
}
+ /**
+ * Copy the current OTP token to the clipboard.
+ */
fun copyTokenToClipboard() {
otpElement?.token?.let { token ->
try {
@@ -59,10 +88,16 @@ class OtpDisplayView @JvmOverloads constructor(
}
}
+ /**
+ * Hide the token view (invisible).
+ */
fun hideToken() {
otpTokenView.visibility = INVISIBLE
}
+ /**
+ * Reveal the token if it's protected.
+ */
fun revealToken() {
if (mProtected) {
reveal()
@@ -70,6 +105,9 @@ class OtpDisplayView @JvmOverloads constructor(
}
}
+ /**
+ * Mask the token if it's protected.
+ */
fun maskToken() {
if (mProtected) {
mask()
@@ -77,6 +115,10 @@ class OtpDisplayView @JvmOverloads constructor(
}
}
+ /**
+ * Set the OTP model and update the view.
+ * @param otpModel The OTP model to display.
+ */
fun setOtpModel(otpModel: OtpModel?) {
mRevealed = mShowOTP
otpModel?.let { model ->
@@ -111,6 +153,9 @@ class OtpDisplayView @JvmOverloads constructor(
}
}
+ /**
+ * Update the token view with the current OTP element.
+ */
private fun populateOtpToken() {
otpElement?.let { otpElement ->
otpTokenView.apply {
@@ -122,8 +167,7 @@ class OtpDisplayView @JvmOverloads constructor(
text = String(otpElement.tokenFormatted)
setTextSize(
unit = TypedValue.COMPLEX_UNIT_PX,
- defaultSize = otpTokenView.
- textSize,
+ defaultSize = otpTokenView.textSize,
mPrefSizeMultiplier
)
textDirection = TEXT_DIRECTION_LTR
@@ -131,10 +175,18 @@ class OtpDisplayView @JvmOverloads constructor(
}
}
+ /**
+ * Set the text color of the OTP token.
+ * @param color The color to set.
+ */
fun setTokenTextColor(color: Int) {
otpTokenView.setTextColor(color)
}
+ /**
+ * Set the progress indicator color.
+ * @param color The color to set.
+ */
fun setProgressIndicatorColor(color: Int) {
otpProgress.setProgressColor(color)
}
@@ -172,6 +224,9 @@ class OtpDisplayView @JvmOverloads constructor(
}
}
+ /**
+ * Clear the data and stop any pending updates.
+ */
fun clearData() {
otpElement = null
otpProgress.clearData()
diff --git a/app/src/main/java/com/kunzisoft/keepass/view/OtpProgressView.kt b/app/src/main/java/com/kunzisoft/keepass/view/OtpProgressView.kt
index 05ddaa794..3360337b6 100644
--- a/app/src/main/java/com/kunzisoft/keepass/view/OtpProgressView.kt
+++ b/app/src/main/java/com/kunzisoft/keepass/view/OtpProgressView.kt
@@ -1,3 +1,22 @@
+/*
+ * Copyright 2026 Jeremy Jamet / Kunzisoft.
+ *
+ * This file is part of KeePassDX.
+ *
+ * KeePassDX is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * KeePassDX is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with KeePassDX. If not, see .
+ *
+ */
package com.kunzisoft.keepass.view
import android.content.Context
@@ -9,7 +28,18 @@ import com.google.android.material.progressindicator.CircularProgressIndicator
import com.kunzisoft.keepass.R
import com.kunzisoft.keepass.otp.OtpElement
import com.kunzisoft.keepass.otp.OtpType
+import kotlinx.coroutines.CoroutineScope
+import kotlinx.coroutines.Dispatchers
+import kotlinx.coroutines.Job
+import kotlinx.coroutines.SupervisorJob
+import kotlinx.coroutines.cancel
+import kotlinx.coroutines.delay
+import kotlinx.coroutines.isActive
+import kotlinx.coroutines.launch
+/**
+ * View to display the progress of an OTP.
+ */
class OtpProgressView @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
@@ -18,47 +48,65 @@ class OtpProgressView @JvmOverloads constructor(
private val otpCircularProgress: CircularProgressIndicator
private val otpTextProgress: TextView
- private var otpRunnable: OtpRunnable = OtpRunnable(this)
+ private var currentOtpElement: OtpElement? = null
+ private var updateJob: Job? = null
+ private var viewScope: CoroutineScope? = null
+
+ /**
+ * Callback when the OTP is updated.
+ */
var onOtpUpdated: ((OtpElement?) -> Unit)? = null
- private class OtpRunnable(val view: OtpProgressView?): Runnable {
- var action: (() -> Unit)? = null
- override fun run() {
- action?.invoke()
- postDelayed()
- }
- fun postDelayed() {
- view?.postDelayed(this, 1000)
- }
- }
-
init {
LayoutInflater.from(context).inflate(R.layout.view_otp_progress, this, true)
otpCircularProgress = findViewById(R.id.otp_circular_progress)
otpTextProgress = findViewById(R.id.otp_text_progress)
}
+ private fun getScope(): CoroutineScope {
+ return viewScope ?: CoroutineScope(Dispatchers.Main + SupervisorJob()).also { viewScope = it }
+ }
+
+ /**
+ * Set the OTP element to display and start the progress update if it's a TOTP.
+ *
+ * @param otpElement The OTP element to display.
+ */
fun setOtpElement(otpElement: OtpElement?) {
- removeCallbacks(otpRunnable)
- otpRunnable = OtpRunnable(this)
- otpElement?.let { otpElement ->
- if (otpElement.token.isNotEmpty()) {
- // Execute runnable to show progress
- otpRunnable.action = {
- this@OtpProgressView.onOtpUpdated?.invoke(otpElement)
- populateOtpView(otpElement)
- }
- this@OtpProgressView.onOtpUpdated?.invoke(otpElement)
- if (otpElement.type == OtpType.TOTP) {
- otpRunnable.postDelayed()
- }
- populateOtpView(otpElement)
- visibility = VISIBLE
- } else {
- visibility = GONE
+ this.currentOtpElement = otpElement
+ stopUpdateJob()
+ if (otpElement != null && otpElement.token.isNotEmpty()) {
+ visibility = VISIBLE
+ onOtpUpdated?.invoke(otpElement)
+ populateOtpView(otpElement)
+ if (otpElement.type == OtpType.TOTP) {
+ startUpdateJob()
}
- } ?: run { visibility = GONE }
+ } else {
+ visibility = GONE
+ }
+ }
+
+ private fun startUpdateJob() {
+ val otpElement = currentOtpElement ?: return
+ if (updateJob?.isActive == true) return
+
+ updateJob = getScope().launch {
+ while (isActive) {
+ val now = System.currentTimeMillis()
+ val delayTime = 1000 - (now % 1000)
+ delay(delayTime)
+
+ onOtpUpdated?.invoke(otpElement)
+ populateOtpView(otpElement)
+ }
+ }
+ }
+
+ private fun stopUpdateJob() {
+ updateJob?.cancel()
+ updateJob = null
}
private fun populateOtpView(otpElement: OtpElement) {
@@ -83,25 +131,37 @@ class OtpProgressView @JvmOverloads constructor(
}
}
}
-
+
+ /**
+ * Set the color of the progress indicator and the text.
+ *
+ * @param color The color to set.
+ */
fun setProgressColor(color: Int) {
otpTextProgress.setTextColor(color)
otpCircularProgress.setIndicatorColor(color)
}
+ override fun onAttachedToWindow() {
+ super.onAttachedToWindow()
+ if (currentOtpElement?.type == OtpType.TOTP) {
+ startUpdateJob()
+ }
+ }
+
override fun onDetachedFromWindow() {
super.onDetachedFromWindow()
- removeCallbacks(otpRunnable)
- clearData()
+ stopUpdateJob()
+ viewScope?.cancel()
+ viewScope = null
}
+ /**
+ * Clear the data and stop any pending updates.
+ */
fun clearData() {
- removeCallbacks(otpRunnable)
- otpRunnable = OtpRunnable(this)
+ currentOtpElement = null
+ stopUpdateJob()
visibility = GONE
}
-
- companion object {
- private val TAG = OtpProgressView::class.java.name
- }
}
diff --git a/app/src/main/java/com/kunzisoft/keepass/view/OtpTextFieldView.kt b/app/src/main/java/com/kunzisoft/keepass/view/OtpTextFieldView.kt
index 537b4b294..d23b2902e 100644
--- a/app/src/main/java/com/kunzisoft/keepass/view/OtpTextFieldView.kt
+++ b/app/src/main/java/com/kunzisoft/keepass/view/OtpTextFieldView.kt
@@ -28,7 +28,10 @@ import com.kunzisoft.keepass.model.OtpModel
import com.kunzisoft.keepass.otp.OtpElement
import com.kunzisoft.keepass.settings.PreferencesUtil
-
+/**
+ * Custom text field view for displaying and managing OTP (One-Time Password) fields.
+ * Extends [TextFieldView] to include an [OtpProgressView] for time-based tokens.
+ */
open class OtpTextFieldView @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
@@ -42,20 +45,26 @@ open class OtpTextFieldView @JvmOverloads constructor(
private val otpProgressView = OtpProgressView(context).apply {
layoutParams = LayoutParams(
LayoutParams.WRAP_CONTENT,
- LayoutParams.WRAP_CONTENT)
+ LayoutParams.WRAP_CONTENT
+ )
}
- override var onRevealChanged: ((isRevealed: Boolean) -> Unit)? = null
-
init {
buildViews()
containerView.addView(otpProgressView)
}
+ /**
+ * Sets the listener to be notified when the OTP token is updated.
+ * @param onOtpUpdated The callback function.
+ */
fun setOnOtpUpdatedListener(onOtpUpdated: ((OtpElement?) -> Unit)? = null) {
otpProgressView.onOtpUpdated = onOtpUpdated
}
+ /**
+ * Initializes the views and layout rules for the OTP progress indicator.
+ */
private fun buildViews() {
otpProgressView.apply {
id = otpProgressViewId
@@ -66,6 +75,10 @@ open class OtpTextFieldView @JvmOverloads constructor(
}
}
+ /**
+ * Sets the OTP model to be displayed and configures the protection settings.
+ * @param otpModel The OTP model containing the secret and configuration.
+ */
fun setOtpModel(otpModel: OtpModel) {
clearData()
val otpElement = OtpElement(otpModel)
@@ -84,16 +97,14 @@ open class OtpTextFieldView @JvmOverloads constructor(
otpProgressView.apply {
setOtpElement(otpElement)
isVisible = true
- setProtection(
- isProtected = !mShowOTP,
- isRevealedByDefault = isRevealed(),
- needUserVerificationToReveal = false
- )
}
textDirection = TEXT_DIRECTION_LTR
}
}
+ /**
+ * Clears all OTP data and stops progress updates.
+ */
fun clearData() {
otpProgressView.clearData()
}
diff --git a/app/src/main/res/layout/view_otp_display.xml b/app/src/main/res/layout/view_otp_display.xml
index ff749a401..a588149ac 100644
--- a/app/src/main/res/layout/view_otp_display.xml
+++ b/app/src/main/res/layout/view_otp_display.xml
@@ -11,6 +11,7 @@
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_vertical"
+ android:layout_marginHorizontal="4dp"
tools:text="513 651" />