xref: /aosp_15_r20/developers/build/prebuilts/gradle/AutofillFramework/README.md (revision d353a188ca6ec4b5eba25b5fbd7bcb8ce61322fb)
1*d353a188SXin Li
2*d353a188SXin LiAndroid AutofillFramework Sample
3*d353a188SXin Li===================================
4*d353a188SXin Li
5*d353a188SXin LiThis sample demonstrates the use of the Autofill Framework. It includes implementations of client
6*d353a188SXin LiActivities with views that should be autofilled, and a Service that can provide autofill data to
7*d353a188SXin Liclient Activities.
8*d353a188SXin Li
9*d353a188SXin LiIntroduction
10*d353a188SXin Li------------
11*d353a188SXin Li
12*d353a188SXin LiThis sample demonstrates the use of the Autofill framework from the service side and the client
13*d353a188SXin Liside. In practice, only a small handful of apps will develop Autofill services because a device
14*d353a188SXin Liwill only have one service as default at a time, and there is just a small number of 3rd-party apps
15*d353a188SXin Liproviding these services (typically password managers). However, all apps targeting O with any
16*d353a188SXin Liautofillable fields should follow the necessary steps to 1) ensure their views can be autofilled
17*d353a188SXin Liand 2) optimize their autofill performance. Most of the time, there is little to no extra code
18*d353a188SXin Liinvolved, but the use of custom views and views with virtual child views requires more work.
19*d353a188SXin Li
20*d353a188SXin LiThe sample's Autofill service is implemented to parse the client's view hierarchy in search of
21*d353a188SXin Liautofillable fields that it has data for. If such fields exist in the hierarchy, the service sends
22*d353a188SXin Lidata suggestions to the client to autofill those fields. The client uses the following attributes
23*d353a188SXin Lito specify autofill properties: `importantForAutofill`, `autofillHints`, and `autofillType`.
24*d353a188SXin Li`importantForAutofill` specifies whether the view is autofillable. `autofillHints` is a list of
25*d353a188SXin Listrings that hint to the service **what** data to fill the view with. This sample service only
26*d353a188SXin Lisupports the hints listed [here](https://developer.android.com/reference/android/view/View.html#AUTOFILL_HINT_CREDIT_CARD_EXPIRATION_DATE)
27*d353a188SXin Liwith the prefix AUTOFILL_HINT_*. `autofillType` tells the service the type of data it expects to
28*d353a188SXin Lireceive (i.e. a list index, a date, or a string). Specifying `autofillType` is only necessary
29*d353a188SXin Liwhen implementing a custom view since all of the provided widgets in the UI toolkit do this for you.
30*d353a188SXin Li
31*d353a188SXin LiTo set the device's default Autofill service to the one in the sample, edit **Settings** >
32*d353a188SXin Li**System** > **Languages & Input** > **Advanced** > **Auto-fill service** and select the sample
33*d353a188SXin Liapp. To edit the service's settings, tap the settings icon next to the **Auto-fill service** list
34*d353a188SXin Liitem or open the **Autofill Settings** launcher icon.. Here, you can set whether you want to enable
35*d353a188SXin Liauthentication on the entire autofill Response or just on individual autofill datasets. You should
36*d353a188SXin Lialso set the master password to “unlock” authenticated autofill data with.
37*d353a188SXin Li
38*d353a188SXin Li**Note:** This sample service stores all autofill data in SharedPreferences and thus is not secure.
39*d353a188SXin LiBe careful about what you store when experimenting with the sample because anyone with root access
40*d353a188SXin Lito your device will be able to view your autofill data.
41*d353a188SXin Li
42*d353a188SXin LiThe client side of the app has three Activities that each have autofillable fields. The first
43*d353a188SXin LiActivity uses standard views to comprise a login form. Very little needs to be done by the client
44*d353a188SXin Liapp to ensure the views get autofilled properly. The second Activity uses a custom view with
45*d353a188SXin Livirtual children, meaning some autofillable child views are not known to the View hierarchy to be
46*d353a188SXin Lichild views. Supporting autofill on these child views is a little more involved.
47*d353a188SXin Li
48*d353a188SXin LiThe following code snippet shows how to signal to the autofill service that a specific
49*d353a188SXin Liautofillable virtual view has come into focus:
50*d353a188SXin Li
51*d353a188SXin Li```java
52*d353a188SXin Liclass CustomVirtualView {
53*d353a188SXin Li...
54*d353a188SXin Li    // Cache AutofillManager system service
55*d353a188SXin Li    mAutofillManager = context.getSystemService(AutofillManager.class);
56*d353a188SXin Li...
57*d353a188SXin Li    // Notify service which virtual view has come into focus.
58*d353a188SXin Li    mAutofillManager.notifyViewEntered(CustomVirtualView.this, id, absBounds);
59*d353a188SXin Li...
60*d353a188SXin Li   // Notify service that a virtual view has left focus.
61*d353a188SXin Li   mAutofillManager.notifyViewExited(CustomVirtualView.this, id);
62*d353a188SXin Li}
63*d353a188SXin Li```
64*d353a188SXin Li
65*d353a188SXin LiNow that the autofillable view has signaled to the service that it has been autofilled, it needs
66*d353a188SXin Lito provide the virtual view hierarchy to the Autofill service. This is done out of the box for
67*d353a188SXin Liviews part of the UI toolkit, but you need to implement this yourself if you have the view has
68*d353a188SXin Livirtual child views. The following code example shows the `View` method you have to override in
69*d353a188SXin Liorder to provide this view hierarchy data to the Autofill service.
70*d353a188SXin Li
71*d353a188SXin Li```java
72*d353a188SXin Li@Override
73*d353a188SXin Lipublic void onProvideAutofillVirtualStructure(ViewStructure structure, int flags) {
74*d353a188SXin Li    // Build a ViewStructure that will get passed to the AutofillService by the framework
75*d353a188SXin Li    // when it is time to find autofill suggestions.
76*d353a188SXin Li    structure.setClassName(getClass().getName());
77*d353a188SXin Li    int childrenSize = mItems.size();
78*d353a188SXin Li    int index = structure.addChildCount(childrenSize);
79*d353a188SXin Li    // Traverse through the view hierarchy, including virtual child views. For each view, we
80*d353a188SXin Li    // need to set the relevant autofill metadata and add it to the ViewStructure.
81*d353a188SXin Li    for (int i = 0; i < childrenSize; i++) {
82*d353a188SXin Li        Item item = mItems.valueAt(i);
83*d353a188SXin Li        ViewStructure child = structure.newChild(index);
84*d353a188SXin Li        child.setAutofillId(structure, item.id);
85*d353a188SXin Li        child.setAutofillHints(item.hints);
86*d353a188SXin Li        child.setAutofillType(item.type);
87*d353a188SXin Li        child.setDataIsSensitive(!item.sanitized);
88*d353a188SXin Li        child.setText(item.text);
89*d353a188SXin Li        child.setAutofillValue(AutofillValue.forText(item.text));
90*d353a188SXin Li        child.setFocused(item.focused);
91*d353a188SXin Li        child.setId(item.id, getContext().getPackageName(), null, item.line.idEntry);
92*d353a188SXin Li        child.setClassName(item.getClassName());
93*d353a188SXin Li        index++;
94*d353a188SXin Li    }
95*d353a188SXin Li}
96*d353a188SXin Li```
97*d353a188SXin Li
98*d353a188SXin LiAfter the service processes the Autofill request and sends back a series of Autofill `Datasets`
99*d353a188SXin Li(wrapped in a `Response` object), the user can pick which `Dataset` they want to autofill their
100*d353a188SXin Liviews with. When a `Dataset` is selected, this method is invoked for all of the views that were
101*d353a188SXin Liassociated with that `Dataset` by the service. For example, the `Dataset` might contain Autofill
102*d353a188SXin Livalues for username, password, birthday, and address. This method would then be invoked on all
103*d353a188SXin Lifour of those fields. The following code example shows how the sample app implements the method
104*d353a188SXin Lito deliver a UI update to the appropriate child view after the user makes their selection.
105*d353a188SXin Li
106*d353a188SXin Li```java
107*d353a188SXin Li@Override
108*d353a188SXin Lipublic void autofill(SparseArray<AutofillValue> values) {
109*d353a188SXin Li    // User has just selected a Dataset from the list of autofill suggestions.
110*d353a188SXin Li    // The Dataset is comprised of a list of AutofillValues, with each AutofillValue meant
111*d353a188SXin Li    // to fill a specific autofillable view. Now we have to update the UI based on the
112*d353a188SXin Li    // AutofillValues in the list.
113*d353a188SXin Li    for (int i = 0; i < values.size(); i++) {
114*d353a188SXin Li        final int id = values.keyAt(i);
115*d353a188SXin Li        final AutofillValue value = values.valueAt(i);
116*d353a188SXin Li        final Item item = mItems.get(id);
117*d353a188SXin Li        if (item != null && item.editable) {
118*d353a188SXin Li            // Set the item's text to the text wrapped in the AutofillValue.
119*d353a188SXin Li            item.text = value.getTextValue();
120*d353a188SXin Li        } else if (item == null) {
121*d353a188SXin Li            // Component not found, so no-op.
122*d353a188SXin Li        } else {
123*d353a188SXin Li            // Component not editable, so no-op.
124*d353a188SXin Li        }
125*d353a188SXin Li    }
126*d353a188SXin Li    postInvalidate();
127*d353a188SXin Li}
128*d353a188SXin Li```
129*d353a188SXin Li
130*d353a188SXin LiPre-requisites
131*d353a188SXin Li--------------
132*d353a188SXin Li
133*d353a188SXin Li- Android SDK 26
134*d353a188SXin Li- Android Build Tools v27.0.3
135*d353a188SXin Li- Android Support Repository
136*d353a188SXin Li
137*d353a188SXin LiScreenshots
138*d353a188SXin Li-------------
139*d353a188SXin Li
140*d353a188SXin Li<img src="screenshots/1_MainPage.png" height="400" alt="Screenshot"/> <img src="screenshots/2_SampleLoginEditTexts.png" height="400" alt="Screenshot"/> <img src="screenshots/3_SampleLoginEditTextsAutofilled.png" height="400" alt="Screenshot"/> <img src="screenshots/4_WelcomeActivity.png" height="400" alt="Screenshot"/> <img src="screenshots/5_SampleLoginCustomVirtualView.png" height="400" alt="Screenshot"/> <img src="screenshots/6_SampleLoginCustomVirtualViewAutofilled.png" height="400" alt="Screenshot"/> <img src="screenshots/7_SampleCheckOutSpinnersAutofillable.png" height="400" alt="Screenshot"/> <img src="screenshots/8_SampleCheckOutSpinnersAutofilled.png" height="400" alt="Screenshot"/> <img src="screenshots/9_SettingsActivity.png" height="400" alt="Screenshot"/> <img src="screenshots/10_AuthNeeded.png" height="400" alt="Screenshot"/> <img src="screenshots/11_AuthActivity.png" height="400" alt="Screenshot"/>
141*d353a188SXin Li
142*d353a188SXin LiGetting Started
143*d353a188SXin Li---------------
144*d353a188SXin Li
145*d353a188SXin LiThis sample uses the Gradle build system. To build this project, use the
146*d353a188SXin Li"gradlew build" command or use "Import Project" in Android Studio.
147*d353a188SXin Li
148*d353a188SXin LiSupport
149*d353a188SXin Li-------
150*d353a188SXin Li
151*d353a188SXin Li- Google+ Community: https://plus.google.com/communities/105153134372062985968
152*d353a188SXin Li- Stack Overflow: http://stackoverflow.com/questions/tagged/android
153*d353a188SXin Li
154*d353a188SXin LiIf you've found an error in this sample, please file an issue:
155*d353a188SXin Lihttps://github.com/googlesamples/android-AutofillFramework
156*d353a188SXin Li
157*d353a188SXin LiPatches are encouraged, and may be submitted by forking this project and
158*d353a188SXin Lisubmitting a pull request through GitHub. Please see CONTRIBUTING.md for more details.
159*d353a188SXin Li
160*d353a188SXin LiLicense
161*d353a188SXin Li-------
162*d353a188SXin Li
163*d353a188SXin LiCopyright 2017 The Android Open Source Project, Inc.
164*d353a188SXin Li
165*d353a188SXin LiLicensed to the Apache Software Foundation (ASF) under one or more contributor
166*d353a188SXin Lilicense agreements.  See the NOTICE file distributed with this work for
167*d353a188SXin Liadditional information regarding copyright ownership.  The ASF licenses this
168*d353a188SXin Lifile to you under the Apache License, Version 2.0 (the "License"); you may not
169*d353a188SXin Liuse this file except in compliance with the License.  You may obtain a copy of
170*d353a188SXin Lithe License at
171*d353a188SXin Li
172*d353a188SXin Lihttp://www.apache.org/licenses/LICENSE-2.0
173*d353a188SXin Li
174*d353a188SXin LiUnless required by applicable law or agreed to in writing, software
175*d353a188SXin Lidistributed under the License is distributed on an "AS IS" BASIS, WITHOUT
176*d353a188SXin LiWARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
177*d353a188SXin LiLicense for the specific language governing permissions and limitations under
178*d353a188SXin Lithe License.
179