How to use Android autofill API

7.3k views Asked by At

I've built a little browser using the android webview component and am looking to integrate password/credential manager support using the Android AutoFill API.

I've read the docs but am completely lost and can't find any examples of integration with complex things like webviews.

The javascript side of this is not a problem for me, I already have events that are triggered when a user selects an input on a login form (and the auto-fill dialog should be presented), and when a finishes entering a username and password and submits the page (and the credentials should be saved back to the password manager) but I'm struggling to understand the android side of this.

The webview seems to have some basic support for this already, for example if I long press on a login form input, and select "auto-fill" in the context menu, I can get it to insert some values saved in the credential manager. The problem is though that the credentials have been saved against my app ID, not the website domain, so my first question, how do I tell the API that when I'm requesting the auto fill menu that it's for a specific field type (e.g. username/password) and belonging to a particular website so it knows which credentials to fetch and can update them later? Here is my attempt to trigger the auto-fill dialog to appear when selecting a field in the login form.

UPDATE: When I create a static webview in my app the autofill correctly saves and prompts for credentials on forms and saves them correctly per site BUT I need this to work in webviews that are in a recyclerview and for some reason they don't despite sharing the same settings. I found this info about autofill in recyclerviews https://developer.android.com/guide/topics/text/autofill-optimize#recycle but using setAutofillId() doesn't seem to help and even the official example here seems a bit unreliable when I test it on my phone https://github.com/android/input-samples/blob/master/AutofillFramework/Application/src/main/java/com/example/android/autofill/app/commoncases/RecyclerViewActivity.java

3

There are 3 answers

0
MeatPopsicle On BEST ANSWER

The problem I was having is that my webviews were embedded in a recyclerview, and to enable autofill on a web view in a recyclerview, you must set it to be enabled on the recyclerview, NOT on the webview itself.

1
James Tomasino On

There's a guide available here describing how to get your app ready to leverage Autofill. Here's the most relevant part describing your choices, especially if using a Webview:

In many cases, Autofill may work in your app without any effort. But to ensure consistent behavior, we recommend providing explicit hints to tell the framework about the contents of your field. You can do this using either the android:autofillHints attribute or the setAutofillHints() method.

To associate your app with a website, though, you'll need to add some settings to your app's Manifest. You'll see this under Step 2 of the link above.

You'll need to update your app's manifest file with an asset_statements resource, which links to the URL where your assetlinks.json file is hosted. Once that's done, you'll need to submit your updated app to the Play Store, and fill out the Affiliation Submission Form for the association to go live.

When using Android Studio 3.0, the App Links Assistant can generate all of this for you. When you open the DAL generator tool (Tools -> App Links Assistant -> Open Digital Asset Links File Generator), simply make sure you enable the new checkbox labeled "Support sharing credentials between the app and website". Similarly, with WebViews in your apps, you can use HTML Autocomplete Attributes to provide hints about fields. Autofill will work in WebViews as long as you have Chrome 61 or later installed on your device. Even if your app is using custom views, you can also define the metadata that allows autofill to work.

6
Mayank Kumar Chaudhari On

A simple solution would be to let webview handle this.

webView.getSettings().setSaveFormData(true);

    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setPluginState(WebSettings.PluginState.ON);
    webView.getSettings().setBuiltInZoomControls(true);
    webView.getSettings().setDisplayZoomControls(true);
    webView.setWebChromeClient(new WebChromeClient() /*Or yourOwnWebChromeClient*/);
    webView.setWebViewClient(new WebViewClient() /*Or yourOwnWebViewClient*/);
    webView.getSettings().setDomStorageEnabled(true);
    webView.getSettings().setSaveFormData(true);

    CookieSyncManager.createInstance(this);
    CookieManager cookieManager = CookieManager.getInstance();
    cookieManager.setAcceptCookie(true);
    CookieSyncManager.getInstance().startSync();

You may also add JavaScript Interface if needed

    webView.addJavascriptInterface(this, JSInterface);

I used this approach and it saved the login tokens for facebook, instagram, as well as dailymotion.com .

Please try this https://stackoverflow.com/a/56562679/9640177

you may try Creating multiple cache

You might want to refer this too - https://developer.android.com/guide/webapps/managing-webview#java