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