1 /*
<lambda>null2  * 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.multidatasetservice
17 
18 import android.app.assist.AssistStructure
19 import android.app.assist.AssistStructure.ViewNode
20 import android.util.Log
21 import com.example.android.autofillframework.CommonUtil.TAG
22 import com.example.android.autofillframework.multidatasetservice.model.FilledAutofillField
23 import com.example.android.autofillframework.multidatasetservice.model.FilledAutofillFieldCollection
24 
25 /**
26  * Parser for an AssistStructure object. This is invoked when the Autofill Service receives an
27  * AssistStructure from the client Activity, representing its View hierarchy. In this sample, it
28  * parses the hierarchy and collects autofill metadata from {@link ViewNode}s along the way.
29  */
30 internal class StructureParser(private val autofillStructure: AssistStructure) {
31     val autofillFields = AutofillFieldMetadataCollection()
32     var filledAutofillFieldCollection: FilledAutofillFieldCollection = FilledAutofillFieldCollection()
33         private set
34 
35     fun parseForFill() {
36         parse(true)
37     }
38 
39     fun parseForSave() {
40         parse(false)
41     }
42 
43     /**
44      * Traverse AssistStructure and add ViewNode metadata to a flat list.
45      */
46     private fun parse(forFill: Boolean) {
47         Log.d(TAG, "Parsing structure for " + autofillStructure.activityComponent)
48         val nodes = autofillStructure.windowNodeCount
49         filledAutofillFieldCollection = FilledAutofillFieldCollection()
50         for (i in 0 until nodes) {
51             parseLocked(forFill, autofillStructure.getWindowNodeAt(i).rootViewNode)
52         }
53     }
54 
55     private fun parseLocked(forFill: Boolean, viewNode: ViewNode) {
56         viewNode.autofillHints?.let { autofillHints ->
57             if (autofillHints.isNotEmpty()) {
58                 if (forFill) {
59                     autofillFields.add(AutofillFieldMetadata(viewNode))
60                 } else {
61                     filledAutofillFieldCollection.add(FilledAutofillField(viewNode))
62                 }
63             }
64         }
65         val childrenSize = viewNode.childCount
66         for (i in 0 until childrenSize) {
67             parseLocked(forFill, viewNode.getChildAt(i))
68 
69         }
70     }
71 }
72