Android Question Android Autofill Services Framework

mfstuart

Active Member
Licensed User
Longtime User
I'm in the process of trying to understand the Android framework of auto-filling.
Of course, autofilling from my app into a browser, like Chrome.

I've found this page on Android Developers website, titled: Autofill Framework.

On this page, the Optimizing of the 2 EditText views are username and password.
It seems to suggest that the EditText view needs to have this property (?) set:
android:autofillHints=username
android:autofillHints=password


There's some Java code on this page that suggests setting the property like so:
Java:
EditText password = findViewById(R.id.password);
password.setAutofillHints(View.AUTOFILL_HINT_PASSWORD);
I would assume using the JavaObject library in B4A for the above.

But this whole thing eeks of knowing Eclipse and Java, which I do not.

Can someone enlighten me on this and or do you already have a java library that handles all this?

Thank you kindly,
Mark Stuart
 

mfstuart

Active Member
Licensed User
Longtime User
I run this code and the app crashes. It is called from Activity_Create, after loading the layout.
B4X:
Sub Init_Username(et As EditText)
    Dim jo As JavaObject = et
    jo.RunMethod("setAutofillHints", Array("View.AUTOFILL_HINT_USERNAME"))
End Sub
I tried the above code, aligning it with the JAVA code in post #1, but it throws this error in the Logs tab: java.lang.RuntimeException: Method: setAutofillHints not matched.

Any idea on how to fix this, or is the error stating that there is no such method or attribute and can therefore cannot complete?
@Erel - does this need to be added to B4A for it to work successfully?
Again, I'm trying to implement the Android Autofill Services Framework in my app.

App is attached.

Regards,
Mark Stuart
 

Attachments

  • Autofill_Service.zip
    10.5 KB · Views: 59
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
@Erel - does this need to be added to B4A for it to work successfully?
There is no such thing. You can call any API you like, but you need to do it correctly.

1. View.AUTOFILL_HINT_USERNAME is a string constant. You need to use its value.
2. The method expects an array of strings: https://developer.android.com/reference/android/view/View.html#setAutofillHints(java.lang.String[])
B4X:
et.As(JavaObject).RunMethod("setAutofillHints", Array(Array As String("username")))
 
Upvote 0

mfstuart

Active Member
Licensed User
Longtime User
I would never have resolved this. Thank you very much Erel!

So I go to the web page you referenced and click on the View.AUTOFILL_HINT_??? I want to use, and look for and use the "Constant value".
Is that what you are saying to use?
 
Upvote 0

mfstuart

Active Member
Licensed User
Longtime User
I applied your code changes to my app and ran it to my tablet.
The app loads successfully, but crashes when I touch into any of the 3 B4XFloatTextField views I have on the layout:
WebSite, Username, and Password.
Log:
java.lang.RuntimeException: Unable to instantiate service com.mfstuart.autofill_demo.MyAutofillService: java.lang.ClassNotFoundException: Didn't find class "com.mfstuart.autofill_demo.MyAutofillService" on path: DexPathList[[zip file "/data/app/com.mfstuart.autofill_demo-fmehVsDnOETFQrOSdcdFJw==/base.apk"],nativeLibraryDirectories=[/data/app/com.mfstuart.autofill_demo-fmehVsDnOETFQrOSdcdFJw==/lib/arm, /system/lib, /system/vendor/lib]]
    at android.app.ActivityThread.handleCreateService(ActivityThread.java:3791)
    at android.app.ActivityThread.access$1500(ActivityThread.java:248)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1846)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:216)
    at android.app.ActivityThread.main(ActivityThread.java:7266)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:975)
Caused by: java.lang.ClassNotFoundException: Didn't find class "com.mfstuart.autofill_demo.MyAutofillService" on path: DexPathList[[zip file "/data/app/com.mfstuart.autofill_demo-fmehVsDnOETFQrOSdcdFJw==/base.apk"],nativeLibraryDirectories=[/data/app/com.mfstuart.autofill_demo-fmehVsDnOETFQrOSdcdFJw==/lib/arm, /system/lib, /system/vendor/lib]]
    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:134)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
    at android.app.AppComponentFactory.instantiateService(AppComponentFactory.java:103)
    at android.app.ActivityThread.handleCreateService(ActivityThread.java:3786)
    ... 8 more
Should I be using the default EditText view instead? Or is there something else I'm not doing?
 
Upvote 0

mfstuart

Active Member
Licensed User
Longtime User
So now I'm applying the other method: "importantForAutofill", and it fails with the same error as I was getting before: java.lang.RuntimeException: Method: importantForAutofill not found in: android.widget.EditText

Here's the Sub scripts I call (in the order they are listed below) after loading the layout, using the same code structure as Erel suggested:
B4X:
Sub Init_Website(et As EditText)
    et.As(JavaObject).RunMethod("importantForAutofill", Array(Array As String("no")))    '<== crashes here
End Sub

Sub Init_Username(et As EditText)
    et.As(JavaObject).RunMethod("setAutofillHints", Array(Array As String("username")))
    et.As(JavaObject).RunMethod("importantForAutofill", Array(Array As String("yes")))
End Sub

Sub Init_Password(et As EditText)
    et.As(JavaObject).RunMethod("setAutofillHints", Array(Array As String("password")))
    et.As(JavaObject).RunMethod("importantForAutofill", Array(Array As String("yes")))
End Sub
On this Android Developer page, it references the values for the "importantForAutofill" method: https://developer.android.com/guide/topics/text/autofill-optimize#java
But it also references the values as numeric. I've tried using both. Still get the error.

Help would be appreciated.
 
Upvote 0

walterf25

Expert
Licensed User
Longtime User
So now I'm applying the other method: "importantForAutofill", and it fails with the same error as I was getting before: java.lang.RuntimeException: Method: importantForAutofill not found in: android.widget.EditText

Here's the Sub scripts I call (in the order they are listed below) after loading the layout, using the same code structure as Erel suggested:
B4X:
Sub Init_Website(et As EditText)
    et.As(JavaObject).RunMethod("importantForAutofill", Array(Array As String("no")))    '<== crashes here
End Sub

Sub Init_Username(et As EditText)
    et.As(JavaObject).RunMethod("setAutofillHints", Array(Array As String("username")))
    et.As(JavaObject).RunMethod("importantForAutofill", Array(Array As String("yes")))
End Sub

Sub Init_Password(et As EditText)
    et.As(JavaObject).RunMethod("setAutofillHints", Array(Array As String("password")))
    et.As(JavaObject).RunMethod("importantForAutofill", Array(Array As String("yes")))
End Sub
On this Android Developer page, it references the values for the "importantForAutofill" method: https://developer.android.com/guide/topics/text/autofill-optimize#java
But it also references the values as numeric. I've tried using both. Still get the error.

Help would be appreciated.
This method requires and Int type parameter

You can do it like this

B4X:
Sub Init_Username(et As EditText)
    Dim jo As JavaObject = et
    jo.RunMethod("setImportantForAutofill", Array(1))  ''1 means yes
End Sub

Sub Init_Password(et As EditText)
    Dim jo As JavaObject = et
    jo.RunMethod("setImportantForAutofill", Array(0)) '' 0 means Auto
End Sub

You can refer to this page here for the other parameters.

Walter
 
Upvote 0

mfstuart

Active Member
Licensed User
Longtime User
This method requires and Int type parameter

You can do it like this

B4X:
Sub Init_Username(et As EditText)
    Dim jo As JavaObject = et
    jo.RunMethod("setImportantForAutofill", Array(1))  ''1 means yes
End Sub

Sub Init_Password(et As EditText)
    Dim jo As JavaObject = et
    jo.RunMethod("setImportantForAutofill", Array(0)) '' 0 means Auto
End Sub

You can refer to this page here for the other parameters.

Walter
Yes, that did allow the app to load successfully and allow me to touch into the Website view without crashing.
But touching into the other 2 views, crashes the app immediately.

The crash is producing the same error as in post #5.

A "service_configuration" file sits in the /Objects/res/xml folder, and its file property is set to ReadOnly.
Its contents:
service_configuration file::
<autofill-service
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:settingsActivity="com.mfstuart.autofill_demo.Main" />
Searching on Google for "settingsActivity", it shows that this is a very specific Activity - something to do with setup? I don't know.
And what would the Activity contain?
I defined it as the Activity (Main in my case) that the 3 views are that have to deal with the Autofill Service.

Does anyone know about this?
 
Upvote 0
Top