From 16320abb7d96678cdeeac049eedab79aa2cbc33b Mon Sep 17 00:00:00 2001 From: J-Jamet Date: Tue, 5 Nov 2019 16:04:30 +0100 Subject: [PATCH] Global OTP variables, add progress bar, remove seconds --- .../keepass/activities/EntryActivity.kt | 8 +- .../kunzisoft/keepass/otp/OtpEntryFields.java | 8 +- .../keepass/view/EntryContentsView.kt | 79 +++++++++++-------- app/src/main/res/layout/activity_entry.xml | 11 +++ .../main/res/layout/view_entry_contents.xml | 20 ++--- app/src/main/res/values/strings.xml | 5 +- 6 files changed, 82 insertions(+), 49 deletions(-) diff --git a/app/src/main/java/com/kunzisoft/keepass/activities/EntryActivity.kt b/app/src/main/java/com/kunzisoft/keepass/activities/EntryActivity.kt index 092e42f99..c5e68bfbf 100644 --- a/app/src/main/java/com/kunzisoft/keepass/activities/EntryActivity.kt +++ b/app/src/main/java/com/kunzisoft/keepass/activities/EntryActivity.kt @@ -32,6 +32,7 @@ import android.view.Menu import android.view.MenuItem import android.view.View import android.widget.ImageView +import android.widget.ProgressBar import android.widget.Toast import com.kunzisoft.keepass.R import com.kunzisoft.keepass.activities.helpers.ReadOnlyHelper @@ -59,6 +60,7 @@ class EntryActivity : LockingHideActivity() { private var titleIconView: ImageView? = null private var historyView: View? = null private var entryContentsView: EntryContentsView? = null + private var entryProgress: ProgressBar? = null private var toolbar: Toolbar? = null private var mDatabase: Database? = null @@ -103,6 +105,7 @@ class EntryActivity : LockingHideActivity() { historyView = findViewById(R.id.history_container) entryContentsView = findViewById(R.id.entry_contents) entryContentsView?.applyFontVisibilityToFields(PreferencesUtil.fieldFontIsInVisibility(this)) + entryProgress = findViewById(R.id.entry_progress) // Init the clipboard helper clipboardHelper = ClipboardHelper(this) @@ -226,10 +229,11 @@ class EntryActivity : LockingHideActivity() { } mOtpEntryFields?.let { otpEntryFields -> - entryContentsView?.assignTotp(otpEntryFields, View.OnClickListener { + entryContentsView?.assignOtp(otpEntryFields, entryProgress, + View.OnClickListener { clipboardHelper?.timeoutCopyToClipboard( otpEntryFields.token, - getString(R.string.copy_field, getString(R.string.entry_totp)) + getString(R.string.copy_field, getString(R.string.entry_otp)) ) }) } diff --git a/app/src/main/java/com/kunzisoft/keepass/otp/OtpEntryFields.java b/app/src/main/java/com/kunzisoft/keepass/otp/OtpEntryFields.java index a038aef6b..0238d1e2c 100644 --- a/app/src/main/java/com/kunzisoft/keepass/otp/OtpEntryFields.java +++ b/app/src/main/java/com/kunzisoft/keepass/otp/OtpEntryFields.java @@ -120,8 +120,8 @@ public class OtpEntryFields { } } - public boolean isConfigured() { - return type != OtpType.UNDEFINED; + public OtpType getType() { + return type; } public String getToken() { @@ -174,6 +174,10 @@ public class OtpEntryFields { } } + public int getStep() { + return step; + } + private void setStep(int step) { if (step <= 0 || step > 60) { this.step = DEFAULT_STEP; diff --git a/app/src/main/java/com/kunzisoft/keepass/view/EntryContentsView.kt b/app/src/main/java/com/kunzisoft/keepass/view/EntryContentsView.kt index 2f463146a..6246ceccc 100644 --- a/app/src/main/java/com/kunzisoft/keepass/view/EntryContentsView.kt +++ b/app/src/main/java/com/kunzisoft/keepass/view/EntryContentsView.kt @@ -20,7 +20,6 @@ package com.kunzisoft.keepass.view import android.content.Context import android.graphics.Color -import android.os.Handler import android.text.method.PasswordTransformationMethod import android.util.AttributeSet import android.view.LayoutInflater @@ -28,6 +27,7 @@ import android.view.View import android.view.ViewGroup import android.widget.ImageView import android.widget.LinearLayout +import android.widget.ProgressBar import android.widget.TextView import androidx.core.content.ContextCompat import androidx.recyclerview.widget.LinearLayoutManager @@ -37,8 +37,8 @@ import com.kunzisoft.keepass.adapters.EntryHistoryAdapter import com.kunzisoft.keepass.database.element.EntryVersioned import com.kunzisoft.keepass.database.element.PwDate import com.kunzisoft.keepass.database.element.security.ProtectedString -import java.util.* import com.kunzisoft.keepass.otp.OtpEntryFields +import java.util.* class EntryContentsView @JvmOverloads constructor(context: Context, var attrs: AttributeSet? = null, @@ -56,10 +56,10 @@ class EntryContentsView @JvmOverloads constructor(context: Context, private val passwordView: TextView private val passwordActionView: ImageView - private val totpContainerView: View - private val totpView: TextView - private val totpActionView: ImageView - private var totpCurrentToken: String = "" + private val otpContainerView: View + private val otpLabelView: TextView + private val otpView: TextView + private val otpActionView: ImageView private val urlContainerView: View private val urlView: TextView @@ -100,9 +100,10 @@ class EntryContentsView @JvmOverloads constructor(context: Context, passwordView = findViewById(R.id.entry_password) passwordActionView = findViewById(R.id.entry_password_action_image) - totpContainerView = findViewById(R.id.entry_totp_container); - totpView = findViewById(R.id.entry_totp); - totpActionView = findViewById(R.id.entry_totp_action_image); + otpContainerView = findViewById(R.id.entry_otp_container) + otpLabelView = findViewById(R.id.entry_otp_label) + otpView = findViewById(R.id.entry_otp) + otpActionView = findViewById(R.id.entry_otp_action_image) urlContainerView = findViewById(R.id.entry_url_container) urlView = findViewById(R.id.entry_url) @@ -210,36 +211,50 @@ class EntryContentsView @JvmOverloads constructor(context: Context, } } - fun assignTotp(otpEntryFields: OtpEntryFields, onClickListener: OnClickListener) { - if (otpEntryFields.isConfigured) { - totpContainerView.visibility = View.VISIBLE + fun assignOtp(otpEntryFields: OtpEntryFields, + otpProgressView: ProgressBar?, + onClickListener: OnClickListener) { + if (otpEntryFields.type != OtpEntryFields.OtpType.UNDEFINED) { + otpContainerView.visibility = View.VISIBLE - val totpToken = otpEntryFields.token - if (totpToken.isEmpty()) { - totpView.text = context.getString(R.string.error_invalid_TOTP) - totpActionView - .setColorFilter(ContextCompat.getColor(context, R.color.grey_dark)) - assignTotpCopyListener(null) + if (otpEntryFields.token.isEmpty()) { + otpView.text = context.getString(R.string.error_invalid_OTP) + otpActionView.setColorFilter(ContextCompat.getColor(context, R.color.grey_dark)) + assignOtpCopyListener(null) } else { - assignTotpCopyListener(onClickListener) - totpCurrentToken = otpEntryFields.token - val totpHandler = Handler() - totpHandler.post(object : Runnable { - override fun run() { - if (otpEntryFields.shouldRefreshToken()) { - totpCurrentToken = otpEntryFields.token - } - totpView.text = context.getString(R.string.entry_totp_format, - totpCurrentToken, otpEntryFields.secondsRemaining) - totpHandler.postDelayed(this, 1000) + assignOtpCopyListener(onClickListener) + otpView.text = otpEntryFields.token + otpLabelView.text = otpEntryFields.type.name + + when (otpEntryFields.type) { + // Only add token if HOTP + OtpEntryFields.OtpType.HOTP -> { } - }) + // Refresh view if TOTP + OtpEntryFields.OtpType.TOTP -> { + otpProgressView?.apply { + max = otpEntryFields.step + progress = otpEntryFields.secondsRemaining + visibility = View.VISIBLE + } + otpContainerView.post(object : Runnable { + override fun run() { + if (otpEntryFields.shouldRefreshToken()) { + otpView.text = otpEntryFields.token + } + otpProgressView?.progress = otpEntryFields.secondsRemaining + otpContainerView.postDelayed(this, 1000) + } + }) + } + else -> {} + } } } } - fun assignTotpCopyListener(onClickListener: OnClickListener?) { - totpActionView.setOnClickListener(onClickListener) + fun assignOtpCopyListener(onClickListener: OnClickListener?) { + otpActionView.setOnClickListener(onClickListener) } fun assignURL(url: String?) { diff --git a/app/src/main/res/layout/activity_entry.xml b/app/src/main/res/layout/activity_entry.xml index 981e2d991..88df31be7 100644 --- a/app/src/main/res/layout/activity_entry.xml +++ b/app/src/main/res/layout/activity_entry.xml @@ -72,6 +72,17 @@ tools:targetApi="lollipop"> + + diff --git a/app/src/main/res/layout/view_entry_contents.xml b/app/src/main/res/layout/view_entry_contents.xml index a86018d4e..ae5343346 100644 --- a/app/src/main/res/layout/view_entry_contents.xml +++ b/app/src/main/res/layout/view_entry_contents.xml @@ -105,34 +105,34 @@ android:tint="?attr/colorAccent" /> - + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index da6314d6e..c44317b22 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -87,8 +87,7 @@ Password Save Title - TOTP - %1$s (%2$d) + OTP URL Username The ARCFOUR stream cipher is not supported. @@ -96,7 +95,7 @@ Could not create file: Could not read database. Make sure the path is correct. - Invalid TOTP secret. + Invalid OTP secret. Enter a name. Select a keyfile. No memory to load your entire database.