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