README.md
1 Android AutofillFramework Sample (Kotlin)
2 ===================================
3
4 This sample demonstrates the use of the Autofill Framework. It includes implementations of client
5 Activities that want to be autofilled, and a Service that can provide autofill data to client
6 Activities. For simplicity, this sample's service uses mock data to autofill what it thinks are
7 username and password text fields.
8
9 Introduction
10 ------------
11
12 This sample demonstrates the use of the Autofill framework from the service side and the client
13 side. In practice, only a small handful of apps will develop Autofill services because a device will
14 only have one service as default at a time. However, all apps targeting O with any autofillable
15 fields should follow the necessary steps to ensure their Views can be autofilled. Most of the time,
16 there is little to no extra code involved, but the use of custom views and views with virtual child
17 views requires more work.
18
19 The sample's Autofill service is implemented to parse the client's view hierarchy in search of text
20 fields that it has data for. If such text fields exist in the hierarchy, the service sends data
21 suggestions to the client to fill in those text fields. In this basic sample, it will only look for
22 views whose resource IDs are "usernameField" and "passwordField" and will send mock credential data
23 accordingly. A real Autofill service would attempt to autofill more than just login credentials and
24 would be able to fill in other view types in addition to text fields (e.g. spinners, checkboxes,
25 etc.). It would also use more advanced heuristics to determine what data to send to which views.
26
27 To set the device's default Autofill service to the one in the sample, edit
28 **Settings** > **Apps & Notifications** > **Default Apps** > **Auto-fill app** and select the
29 sample app. To edit the service's settings, open the **Autofill Settings** launcher icon. Here, you
30 can set whether you want to enable authentication on the entire Autofill Response or just on
31 individual datasets. You can also set the number of mock datasets that are sent to the client app.
32
33 The client side of the app has two Activities that each have a username field and a password field.
34 One of the Activities uses standard views and the other Activity uses a custom view with virtual
35 children. The standard views do not require any extra code to allow autofill. The following code
36 example shows the `View` method you have to override in order to provide view hierarchy data to the
37 Autofill service. This is triggered when the `View` goes into focus and Android invokes an Autofill
38 request.
39
40 ```java
41 /*
42 This method is responsible for building the ViewStructure that gets passed to the AutoFillService
43 by the framework when it is time to find Autofill suggestions. To do this, it should traverse
44 through its view hierarchy and add views to the ViewStructure on the way.
45 */
46 @Override
47 public void onProvideAutoFillVirtualStructure(ViewStructure structure, int flags) {
48 structure.setClassName(getClass().getName());
49 int childrenSize = mItems.size();
50 int index = structure.addChildCount(childrenSize);
51 for (int i = 0; i < childrenSize; i++) {
52 Item item = mItems.valueAt(i);
53 ViewStructure child = structure.newChild(index, item.id, flags);
54 child.setSanitized(item.sanitized);
55 child.setText(item.text);
56 child.setAutoFillValue(AutoFillValue.forText(item.text));
57 child.setFocused(item.line.focused);
58 child.setId(item.id, getContext().getPackageName(), null, item.line.idEntry);
59 index++;
60 }
61 }
62 ```
63
64 After the service processes the Autofill request and sends back a series of Autofill `Datasets`
65 (wrapped in a `Response` object), the user can pick which `Dataset` they want to autofill their
66 views with. When a `Dataset` is selected, this method is invoked for all of the views that were
67 associated with that `Dataset` by the service. For example, the `Dataset` might contain Autofill
68 values for username, password, birthday, and address. This method would then be invoked on all four
69 of those fields. The following code example shows how the sample app implements the method to
70 deliver a UI update to the appropriate child view after the user makes their selection.
71
72 ```java
73 /*
74 User has just selected a Dataset from the list of Autofill suggestions and the Dataset's
75 AutoFillValue gets passed into this method. This method updates the UI based on the data
76 in the AutoFillValue.
77 */
78 @Override
79 public void autoFillVirtual(int id, AutoFillValue value) {
80 Item item = mItems.get(id);
81 if (item == null) {
82 // ID not recognized so no-op.
83 return;
84 }
85 if (!item.editable) {
86 // Component is not editable so no-op.
87 return;
88 }
89 // Set the virtual child view's text to the text wrapped in the AutoFillValue.
90 item.text = value.getTextValue();
91 postInvalidate();
92 }
93 ```
94
95 Pre-requisites
96 --------------
97
98 - Android SDK Preview O
99 - Android Build Tools v25.0.3
100 - Android Support Repository
101
102 Screenshots
103 -------------
104
105 <img src="screenshots/1_HomePage.png" height="400" alt="Screenshot"/> <img src="screenshots/2_StandardViewAutofillable.png" height="400" alt="Screenshot"/> <img src="screenshots/3_StandardViewAutofilled.png" height="400" alt="Screenshot"/> <img src="screenshots/4_WelcomeActivity.png" height="400" alt="Screenshot"/> <img src="screenshots/5_CustomViewAutofillable.png" height="400" alt="Screenshot"/> <img src="screenshots/6_CustomViewAutofilled.png" height="400" alt="Screenshot"/> <img src="screenshots/7_SettingsActivity.png" height="400" alt="Screenshot"/> <img src="screenshots/8_AuthNeeded.png" height="400" alt="Screenshot"/> <img src="screenshots/9_AuthActivity.png" height="400" alt="Screenshot"/>
106
107 Getting Started
108 ---------------
109
110 This sample uses the Gradle build system. To build this project, use the
111 "gradlew build" command or use "Import Project" in Android Studio.
112
113 Support
114 -------
115
116 - Google+ Community: https://plus.google.com/communities/105153134372062985968
117 - Stack Overflow: http://stackoverflow.com/questions/tagged/android
118
119 If you've found an error in this sample, please file an issue:
120 https://github.com/googlesamples/android-AutofillFramework
121
122 Patches are encouraged, and may be submitted by forking this project and
123 submitting a pull request through GitHub. Please see CONTRIBUTING.md for more details.
124
125 License
126 -------
127
128 Copyright 2017 The Android Open Source Project, Inc.
129
130 Licensed to the Apache Software Foundation (ASF) under one or more contributor
131 license agreements. See the NOTICE file distributed with this work for
132 additional information regarding copyright ownership. The ASF licenses this
133 file to you under the Apache License, Version 2.0 (the "License"); you may not
134 use this file except in compliance with the License. You may obtain a copy of
135 the License at
136
137 http://www.apache.org/licenses/LICENSE-2.0
138
139 Unless required by applicable law or agreed to in writing, software
140 distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
141 WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
142 License for the specific language governing permissions and limitations under
143 the License.
144