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