1<?xml version="1.0" encoding="UTF-8"?> 2<!-- 3 Copyright 2017 The Android Open Source Project 4 5 Licensed under the Apache License, Version 2.0 (the "License"); 6 you may not use this file except in compliance with the License. 7 You may obtain a copy of the License at 8 9 http://www.apache.org/licenses/LICENSE-2.0 10 11 Unless required by applicable law or agreed to in writing, software 12 distributed under the License is distributed on an "AS IS" BASIS, 13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 See the License for the specific language governing permissions and 15 limitations under the License. 16--> 17 18<sample> 19 <name>AutofillFramework</name> 20 <group>Input</group> <!-- This field will be deprecated in the future 21 and replaced with the "categories" tags below. --> 22 <package>com.example.android.autofill.app</package> 23 24 <!-- change minSdk if needed--> 25 <minSdk>26</minSdk> 26 <compileSdkVersion>26</compileSdkVersion> 27 <targetSdkVersion>26</targetSdkVersion> 28 <!-- Include additional dependencies here.--> 29 <!-- dependency>com.google.android.gms:play-services:5.0.+</dependency --> 30 31 <strings> 32 <intro> 33 <![CDATA[ 34 This sample app demos the Autofill feature introduced in Android O. 35 ]]> 36 </intro> 37 </strings> 38 39 <!-- The basic templates have already been enabled. Uncomment more as desired. --> 40 <template src="base-build" /> 41 42 <metadata> 43 <!-- Values: {DRAFT | PUBLISHED | INTERNAL | DEPRECATED | SUPERCEDED} --> 44 <status>PUBLISHED</status> 45 <!-- See http://go/sample-categories for details on the next 4 fields. --> 46 <!-- Most samples just need to udpate the Categories field. This is a comma- 47 seperated list of topic tags. Unlike the old category system, samples 48 may have multiple categories, so feel free to add extras. Try to avoid 49 simply tagging everything with "UI". :)--> 50 <categories>Input, Android O Preview</categories> 51 <technologies>Android</technologies> 52 <languages>Java</languages> 53 <solutions>Mobile</solutions> 54 <!-- Values: {BEGINNER | INTERMEDIATE | ADVANCED | EXPERT} --> 55 <!-- Beginner is for "getting started" type content, or essential content. 56 (e.g. "Hello World", activities, intents) 57 58 Intermediate is for content that covers material a beginner doesn't need 59 to know, but that a skilled developer is expected to know. 60 (e.g. services, basic styles and theming, sync adapters) 61 62 Advanced is for highly technical content geared towards experienced developers. 63 (e.g. performance optimizations, custom views, bluetooth) 64 65 Expert is reserved for highly technical or specialized content, and should 66 be used sparingly. (e.g. VPN clients, SELinux, custom instrumentation runners) --> 67 <level>ADVANCED</level> 68 <!-- Dimensions: 512x512, PNG fomrat --> 69 <icon>screenshots/icon-web.png</icon> 70 <!-- Path to screenshots. Use <img> tags for each. --> 71 <screenshots> 72 <img>screenshots/1_MainPage.png</img> 73 <img>screenshots/2_SampleLoginEditTexts.png</img> 74 <img>screenshots/3_SampleLoginEditTextsAutofilled.png</img> 75 <img>screenshots/4_WelcomeActivity.png</img> 76 <img>screenshots/5_SampleLoginCustomVirtualView.png</img> 77 <img>screenshots/6_SampleLoginCustomVirtualViewAutofilled.png</img> 78 <img>screenshots/7_SampleCheckOutSpinnersAutofillable.png</img> 79 <img>screenshots/8_SampleCheckOutSpinnersAutofilled.png</img> 80 <img>screenshots/9_SettingsActivity.png</img> 81 <img>screenshots/10_AuthNeeded.png</img> 82 <img>screenshots/11_AuthActivity.png</img> 83 </screenshots> 84 <!-- List of APIs that this sample should be cross-referenced under. Use <android> 85 for fully-qualified Framework class names ("android:" namespace). 86 87 Use <ext> for custom namespaces, if needed. See "Samples Index API" documentation 88 for more details. --> 89 <api_refs> 90 <android>android.view.View</android> 91 <android>android.service.autofill.AutoFillService</android> 92 <android>android.view.autofill.AutoFillManager</android> 93 </api_refs> 94 95 <!-- 1-3 line description of the sample here. 96 97 Avoid simply rearranging the sample's title. What does this sample actually 98 accomplish, and how does it do it? --> 99 <description> 100<![CDATA[ 101This sample demonstrates the use of the Autofill Framework. It includes implementations of client 102Activities with views that should be autofilled, and a Service that can provide autofill data to 103client Activities. 104]]> 105 </description> 106 107 <!-- Multi-paragraph introduction to sample, from an educational point-of-view. 108 Makrdown formatting allowed. This will be used to generate a mini-article for the 109 sample on DAC. --> 110 <intro> 111<![CDATA[ 112This sample demonstrates the use of the Autofill framework from the service side and the client 113side. In practice, only a small handful of apps will develop Autofill services because a device 114will only have one service as default at a time, and there is just a small number of 3rd-party apps 115providing these services (typically password managers). However, all apps targeting O with any 116autofillable fields should follow the necessary steps to 1) ensure their views can be autofilled 117and 2) optimize their autofill performance. Most of the time, there is little to no extra code 118involved, but the use of custom views and views with virtual child views requires more work. 119 120The sample's Autofill service is implemented to parse the client's view hierarchy in search of 121autofillable fields that it has data for. If such fields exist in the hierarchy, the service sends 122data suggestions to the client to autofill those fields. The client uses the following attributes 123to specify autofill properties: `importantForAutofill`, `autofillHints`, and `autofillType`. 124`importantForAutofill` specifies whether the view is autofillable. `autofillHints` is a list of 125strings that hint to the service **what** data to fill the view with. This sample service only 126supports the hints listed [here](https://developer.android.com/reference/android/view/View.html#AUTOFILL_HINT_CREDIT_CARD_EXPIRATION_DATE) 127with the prefix AUTOFILL_HINT_*. `autofillType` tells the service the type of data it expects to 128receive (i.e. a list index, a date, or a string). Specifying `autofillType` is only necessary 129when implementing a custom view since all of the provided widgets in the UI toolkit do this for you. 130 131To set the device's default Autofill service to the one in the sample, edit **Settings** > 132**System** > **Languages & Input** > **Advanced** > **Auto-fill service** and select the sample 133app. To edit the service's settings, tap the settings icon next to the **Auto-fill service** list 134item or open the **Autofill Settings** launcher icon.. Here, you can set whether you want to enable 135authentication on the entire autofill Response or just on individual autofill datasets. You should 136also set the master password to “unlock” authenticated autofill data with. 137 138**Note:** This sample service stores all autofill data in SharedPreferences and thus is not secure. 139Be careful about what you store when experimenting with the sample because anyone with root access 140to your device will be able to view your autofill data. 141 142The client side of the app has three Activities that each have autofillable fields. The first 143Activity uses standard views to comprise a login form. Very little needs to be done by the client 144app to ensure the views get autofilled properly. The second Activity uses a custom view with 145virtual children, meaning some autofillable child views are not known to the View hierarchy to be 146child views. Supporting autofill on these child views is a little more involved. 147 148The following code snippet shows how to signal to the autofill service that a specific 149autofillable virtual view has come into focus: 150 151```java 152class CustomVirtualView { 153... 154 // Cache AutofillManager system service 155 mAutofillManager = context.getSystemService(AutofillManager.class); 156... 157 // Notify service which virtual view has come into focus. 158 mAutofillManager.notifyViewEntered(CustomVirtualView.this, id, absBounds); 159... 160 // Notify service that a virtual view has left focus. 161 mAutofillManager.notifyViewExited(CustomVirtualView.this, id); 162} 163``` 164 165Now that the autofillable view has signaled to the service that it has been autofilled, it needs 166to provide the virtual view hierarchy to the Autofill service. This is done out of the box for 167views part of the UI toolkit, but you need to implement this yourself if you have the view has 168virtual child views. The following code example shows the `View` method you have to override in 169order to provide this view hierarchy data to the Autofill service. 170 171```java 172@Override 173public void onProvideAutofillVirtualStructure(ViewStructure structure, int flags) { 174 // Build a ViewStructure that will get passed to the AutofillService by the framework 175 // when it is time to find autofill suggestions. 176 structure.setClassName(getClass().getName()); 177 int childrenSize = mItems.size(); 178 int index = structure.addChildCount(childrenSize); 179 // Traverse through the view hierarchy, including virtual child views. For each view, we 180 // need to set the relevant autofill metadata and add it to the ViewStructure. 181 for (int i = 0; i < childrenSize; i++) { 182 Item item = mItems.valueAt(i); 183 ViewStructure child = structure.newChild(index); 184 child.setAutofillId(structure, item.id); 185 child.setAutofillHints(item.hints); 186 child.setAutofillType(item.type); 187 child.setDataIsSensitive(!item.sanitized); 188 child.setText(item.text); 189 child.setAutofillValue(AutofillValue.forText(item.text)); 190 child.setFocused(item.focused); 191 child.setId(item.id, getContext().getPackageName(), null, item.line.idEntry); 192 child.setClassName(item.getClassName()); 193 index++; 194 } 195} 196``` 197 198After the service processes the Autofill request and sends back a series of Autofill `Datasets` 199(wrapped in a `Response` object), the user can pick which `Dataset` they want to autofill their 200views with. When a `Dataset` is selected, this method is invoked for all of the views that were 201associated with that `Dataset` by the service. For example, the `Dataset` might contain Autofill 202values for username, password, birthday, and address. This method would then be invoked on all 203four of those fields. The following code example shows how the sample app implements the method 204to deliver a UI update to the appropriate child view after the user makes their selection. 205 206```java 207@Override 208public void autofill(SparseArray<AutofillValue> values) { 209 // User has just selected a Dataset from the list of autofill suggestions. 210 // The Dataset is comprised of a list of AutofillValues, with each AutofillValue meant 211 // to fill a specific autofillable view. Now we have to update the UI based on the 212 // AutofillValues in the list. 213 for (int i = 0; i < values.size(); i++) { 214 final int id = values.keyAt(i); 215 final AutofillValue value = values.valueAt(i); 216 final Item item = mItems.get(id); 217 if (item != null && item.editable) { 218 // Set the item's text to the text wrapped in the AutofillValue. 219 item.text = value.getTextValue(); 220 } else if (item == null) { 221 // Component not found, so no-op. 222 } else { 223 // Component not editable, so no-op. 224 } 225 } 226 postInvalidate(); 227} 228``` 229]]> 230 </intro> 231 </metadata> 232</sample> 233