Skip to content

Commit

Permalink
Fido: Improve PIN entry UX
Browse files Browse the repository at this point in the history
  • Loading branch information
mar-v-in committed Aug 19, 2024
1 parent ef7034d commit 25deea5
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 42 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
package org.microg.gms.fido.core.ui

import android.os.Bundle
import android.view.KeyEvent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.inputmethod.EditorInfo
import android.view.inputmethod.InputMethodManager
import android.widget.Button
import android.widget.EditText
import androidx.core.content.getSystemService
import androidx.databinding.adapters.TextViewBindingAdapter
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.ViewModel
import androidx.navigation.fragment.findNavController
Expand Down Expand Up @@ -33,10 +39,30 @@ class PinFragment: AuthenticatorActivityFragment() {
binding.onEnterPin = View.OnClickListener {
enterPin()
}
binding.onInputChange = TextViewBindingAdapter.AfterTextChanged {
view?.findViewById<Button>(R.id.pin_fragment_ok)?.isEnabled = it.toString().encodeToByteArray().size in 4..63
}
binding.root.findViewById<EditText>(R.id.pin_editor)?.setOnEditorActionListener { v, actionId, event ->
if (actionId == EditorInfo.IME_ACTION_DONE &&
(event == null || event.action == KeyEvent.ACTION_DOWN) &&
v.text.toString().encodeToByteArray().size in 4 ..63) {
enterPin()
true
} else {
false
}
}

return binding.root
}

override fun onResume() {
super.onResume()
view?.findViewById<EditText>(R.id.pin_editor)?.let { editText ->
requireContext().getSystemService<InputMethodManager>()?.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT)
}
}

fun enterPin () {
val textEditor = view?.findViewById<EditText>(R.id.pin_editor)
if (textEditor != null) {
Expand Down
105 changes: 63 additions & 42 deletions play-services-fido/core/src/main/res/layout/fido_pin_fragment.xml
Original file line number Diff line number Diff line change
@@ -1,65 +1,86 @@
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">

<data>

<variable
name="onEnterPin"
type="android.view.View.OnClickListener" />

<variable
name="onCancel"
type="android.view.View.OnClickListener" />

<variable
name="onInputChange"
type="androidx.databinding.adapters.TextViewBindingAdapter.AfterTextChanged" />
</data>

<androidx.constraintlayout.widget.ConstraintLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="24dp">

<ImageView
android:layout_width="32dp"
android:layout_height="32dp"
android:scaleType="fitXY"
android:src="@drawable/ic_fido_key" />

<TextView
android:id="@+id/pin_fragment_title"
android:layout_width="wrap_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:paddingTop="24dp"
android:paddingBottom="8dp"
android:text="@string/fido_pin_title"
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
android:textAppearance="@style/TextAppearance.AppCompat.Title" />

<EditText
android:id="@+id/pin_editor"
android:layout_width="wrap_content"
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:ems="10"
android:hint="@string/fido_pin_hint"
android:inputType="textPassword"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/pin_fragment_title"
android:autofillHints="password" />
android:hint="@string/fido_pin_hint">

<Button
android:id="@+id/pin_fragment_ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:text="@string/fido_pin_ok"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/pin_editor"
android:onClick="@{onEnterPin}"/>
<requestFocus />

<Button
android:id="@+id/pin_fragment_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/pin_editor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:ems="10"
android:inputType="textPassword"
android:afterTextChanged="@{onInputChange}"
android:imeOptions="actionDone"
android:autofillHints="password" />
</com.google.android.material.textfield.TextInputLayout>

<LinearLayout
android:layout_marginTop="24dp"
android:text="@string/fido_pin_cancel"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/pin_fragment_ok"
android:onClick="@{onCancel}"/>
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="bottom|end"
android:orientation="horizontal">

<Button
android:id="@+id/pin_fragment_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/fido_pin_cancel"
android:onClick="@{onCancel}"
style="@style/Widget.AppCompat.Button.Borderless.Colored" />

<Button
android:id="@+id/pin_fragment_ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/fido_pin_ok"
android:onClick="@{onEnterPin}"
style="@style/Widget.AppCompat.Button.Borderless.Colored" />

</androidx.constraintlayout.widget.ConstraintLayout>
</LinearLayout>
</LinearLayout>
</layout>

0 comments on commit 25deea5

Please sign in to comment.