1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.example.android.autofillframework.service; 17 18 import android.app.assist.AssistStructure; 19 import android.app.assist.AssistStructure.ViewNode; 20 import android.app.assist.AssistStructure.WindowNode; 21 import android.util.Log; 22 23 import com.example.android.autofillframework.service.model.AutofillField; 24 import com.example.android.autofillframework.service.model.AutofillFieldsCollection; 25 import com.example.android.autofillframework.service.model.ClientFormData; 26 import com.example.android.autofillframework.service.model.SavedAutofillValue; 27 28 import static com.example.android.autofillframework.CommonUtil.TAG; 29 30 /** 31 * Parser for an AssistStructure object. This is invoked when the Autofill Service receives an 32 * AssistStructure from the client Activity, representing its View hierarchy. In this 33 * sample, it parses the hierarchy and records 34 */ 35 final class StructureParser { 36 private final AutofillFieldsCollection mAutofillFields = new AutofillFieldsCollection(); 37 private final AssistStructure mStructure; 38 private ClientFormData mClientFormData; 39 40 StructureParser(AssistStructure structure) { 41 mStructure = structure; 42 43 } 44 45 /** 46 * Traverse AssistStructure and add ViewNode metadata to a flat list. 47 */ 48 void parse() { 49 Log.d(TAG, "Parsing structure for " + mStructure.getActivityComponent()); 50 int nodes = mStructure.getWindowNodeCount(); 51 mClientFormData = new ClientFormData(); 52 for (int i = 0; i < nodes; i++) { 53 WindowNode node = mStructure.getWindowNodeAt(i); 54 ViewNode view = node.getRootViewNode(); 55 parseLocked(view); 56 } 57 } 58 59 private void parseLocked(ViewNode viewNode) { 60 if (viewNode.getAutofillHints() != null && viewNode.getAutofillHints().length > 0) { 61 //TODO check to make sure hints are supported by service. 62 mAutofillFields.add(new AutofillField(viewNode)); 63 mClientFormData 64 .set(viewNode.getAutofillHints(), SavedAutofillValue.fromViewNode(viewNode)); 65 } 66 int childrenSize = viewNode.getChildCount(); 67 if (childrenSize > 0) { 68 for (int i = 0; i < childrenSize; i++) { 69 parseLocked(viewNode.getChildAt(i)); 70 } 71 } 72 } 73 74 public AutofillFieldsCollection getAutofillFields() { 75 return mAutofillFields; 76 } 77 78 public int getSaveTypes() { 79 return mAutofillFields.getSaveType(); 80 } 81 82 public ClientFormData getClientFormData() { 83 return mClientFormData; 84 } 85 } 86