I have created an app with a basic login/signup process. When a user creates a new account, the Android Autofill Framework asks to save the username and password. Now once the user logs-in, there's an option to change the password, a simple screen that asks for current password and a new password. Is there any way for me to force Android Autofill Framework to update the password?
Autofill manager needs information about username. Unfortunately invisible or disabled EditText is ignored in autofill process. The trick is to add custom invisible field with hint for username. Your new password should also be available with hint for password.
Example:
<com.example.myapplication.InvisibleField
android:id="@+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:autofillHints="username" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:autofillHints="password" />
class InvisibleField @JvmOverloads constructor(context: Context, attrs: AttributeSet?, defStyleRes: Int = 0) : View(context, attrs, defStyleRes) {
private var value: AutofillValue? = null
override fun autofill(value: AutofillValue?) {
super.autofill(value)
this.value = value
}
override fun getAutofillType(): Int {
return AUTOFILL_TYPE_TEXT
}
override fun getAutofillValue(): AutofillValue? {
return value
}
@RequiresApi(Build.VERSION_CODES.O)
fun setValue(username: String?) {
value = AutofillValue.forText(username)
}
override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec)
// IMPORTANT! Non-zero size is required
setMeasuredDimension(1, 1)
}
}
This depends on where the password is saved and what autofill service is assigned. To manage the passwords you need to implement your own autofill service.
If you are the Autofill provider app, you can update the password on 'change password' type of form by changing the implementation for onSaveRequest() method. Override the onSaveRequest method such that if you see >=2 password fields on the page and your intelligence identifies it as a 'change password' form type, do an update of the saved password instead of saving a new one.
How to find the particular credentials/username for which to update? You can look for the domain's username in saved credentials or if there are multiple usernames already saved for that domain/app, you can show a bottomsheet/dialog to the user to select the appropriate username for which password is changing. You can update that password according to the input.
Note: You can't force the Autofill framework to show update prompt. If a user edits an autofilled fields, then update prompt appears else save prompt comes up, I would say do an update on the save prompt also.
© 2022 - 2024 — McMap. All rights reserved.