Show counter and fix HOTP

This commit is contained in:
J-Jamet
2021-08-26 20:09:49 +02:00
parent fe0235da43
commit 8196e05679
2 changed files with 69 additions and 21 deletions

View File

@@ -41,6 +41,8 @@ import com.kunzisoft.keepass.database.element.SortNodeEnum
import com.kunzisoft.keepass.database.element.node.Node import com.kunzisoft.keepass.database.element.node.Node
import com.kunzisoft.keepass.database.element.node.NodeVersionedInterface import com.kunzisoft.keepass.database.element.node.NodeVersionedInterface
import com.kunzisoft.keepass.database.element.node.Type import com.kunzisoft.keepass.database.element.node.Type
import com.kunzisoft.keepass.otp.OtpElement
import com.kunzisoft.keepass.otp.OtpType
import com.kunzisoft.keepass.settings.PreferencesUtil import com.kunzisoft.keepass.settings.PreferencesUtil
import com.kunzisoft.keepass.view.setTextSize import com.kunzisoft.keepass.view.setTextSize
import com.kunzisoft.keepass.view.strikeOut import com.kunzisoft.keepass.view.strikeOut
@@ -363,21 +365,19 @@ class NodeAdapter (private val context: Context,
val otpElement = entry.getOtpElement() val otpElement = entry.getOtpElement()
holder.otpContainer?.removeCallbacks(holder.otpRunnable) holder.otpContainer?.removeCallbacks(holder.otpRunnable)
holder.otpRunnable = null
if (otpElement != null if (otpElement != null
&& mShowOTP && mShowOTP
&& otpElement.token.isNotEmpty()) { && otpElement.token.isNotEmpty()) {
holder.otpProgress?.apply {
max = otpElement.period // Execute runnable to show progress
progress = otpElement.secondsRemaining holder.otpRunnable.action = {
populateOtpView(holder, otpElement)
} }
holder.otpToken?.text = otpElement.token if (otpElement.type == OtpType.TOTP) {
holder.otpRunnable = Runnable { holder.otpRunnable.postDelayed()
holder.otpProgress?.progress = otpElement.secondsRemaining
holder.otpToken?.text = otpElement.token
holder.otpContainer?.postDelayed(holder.otpRunnable, 200)
} }
holder.otpContainer?.postDelayed(holder.otpRunnable, 500) populateOtpView(holder, otpElement)
holder.otpContainer?.visibility = View.VISIBLE holder.otpContainer?.visibility = View.VISIBLE
} else { } else {
holder.otpContainer?.visibility = View.GONE holder.otpContainer?.visibility = View.GONE
@@ -411,7 +411,41 @@ class NodeAdapter (private val context: Context,
mNodeClickCallback?.onNodeLongClick(database, subNode) ?: false mNodeClickCallback?.onNodeLongClick(database, subNode) ?: false
} }
} }
private fun populateOtpView(holder: NodeViewHolder?, otpElement: OtpElement?) {
when (otpElement?.type) {
OtpType.HOTP -> {
holder?.otpCounter?.text = otpElement.counter.toString()
holder?.otpProgress?.apply {
max = 100
progress = 100
}
}
OtpType.TOTP -> {
holder?.otpCounter?.text = otpElement.secondsRemaining.toString()
holder?.otpProgress?.apply {
max = otpElement.period
progress = otpElement.secondsRemaining
}
}
}
holder?.otpToken?.text = otpElement?.token
}
class OtpRunnable(val view: View?): Runnable {
var action: (() -> Unit)? = null
override fun run() {
action?.invoke()
postDelayed()
}
fun postDelayed() {
view?.postDelayed(this, 500)
}
}
override fun getItemCount(): Int { override fun getItemCount(): Int {
return mNodeSortedList.size() return mNodeSortedList.size()
} }
@@ -440,8 +474,9 @@ class NodeAdapter (private val context: Context,
var meta: TextView = itemView.findViewById(R.id.node_meta) var meta: TextView = itemView.findViewById(R.id.node_meta)
var otpContainer: ViewGroup? = itemView.findViewById(R.id.node_otp_container) var otpContainer: ViewGroup? = itemView.findViewById(R.id.node_otp_container)
var otpProgress: ProgressBar? = itemView.findViewById(R.id.node_otp_progress) var otpProgress: ProgressBar? = itemView.findViewById(R.id.node_otp_progress)
var otpCounter: TextView? = itemView.findViewById(R.id.node_otp_counter)
var otpToken: TextView? = itemView.findViewById(R.id.node_otp_token) var otpToken: TextView? = itemView.findViewById(R.id.node_otp_token)
var otpRunnable: Runnable? = null var otpRunnable: OtpRunnable = OtpRunnable(otpContainer)
var numberChildren: TextView? = itemView.findViewById(R.id.node_child_numbers) var numberChildren: TextView? = itemView.findViewById(R.id.node_child_numbers)
var attachmentIcon: ImageView? = itemView.findViewById(R.id.node_attachment_icon) var attachmentIcon: ImageView? = itemView.findViewById(R.id.node_attachment_icon)
} }

View File

@@ -132,17 +132,30 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="center" android:layout_gravity="center"
tools:text="5136" /> tools:text="5136" />
<FrameLayout
<ProgressBar android:layout_width="wrap_content"
android:id="@+id/node_otp_progress" android:layout_height="wrap_content"
style="@style/KeepassDXStyle.ProgressBar.Circle.Secondary"
android:layout_width="18dp"
android:layout_height="18dp"
android:layout_gravity="center" android:layout_gravity="center"
android:layout_marginStart="4dp" android:layout_marginStart="4dp"
android:layout_marginLeft="4dp" android:layout_marginLeft="4dp">
android:max="100" <ProgressBar
android:progress="60" /> android:id="@+id/node_otp_progress"
style="@style/KeepassDXStyle.ProgressBar.Circle.Secondary"
android:layout_width="18dp"
android:layout_height="18dp"
android:layout_gravity="center"
android:max="100"
android:progress="60" />
<TextView
android:id="@+id/node_otp_counter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
style="@style/KeepassDXStyle.TextAppearance.Entry.Info"
android:textSize="11sp"
tools:text="70" />
</FrameLayout>
</LinearLayout> </LinearLayout>
<androidx.appcompat.widget.AppCompatImageView <androidx.appcompat.widget.AppCompatImageView