Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Android autofill API

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

like image 388
MeatPopsicle Avatar asked Jul 24 '20 14:07

MeatPopsicle


People also ask

How do you use autofill on Android?

Users can enable or disable autofill as well as change the autofill service by navigating to Settings > System > Languages & input > Advanced > Input assistance > Autofill service.

What is Android autofill service?

An autofill service is an app that makes it easier for users to fill out forms by injecting data into the views of other apps. Autofill services can also retrieve user data from the views in an app and store it to use it at a later time.


1 Answers

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

like image 177
mayank1513 Avatar answered Oct 11 '22 19:10

mayank1513