1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
|
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.service.autofill;
import static android.view.autofill.Helper.sDebug;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.app.assist.AssistStructure;
import android.app.assist.AssistStructure.ViewNode;
import android.os.Bundle;
import android.os.CancellationSignal;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.ArrayMap;
import android.util.SparseIntArray;
import android.view.autofill.AutofillId;
import com.android.internal.util.DataClass;
import java.util.LinkedList;
/**
* This class represents a context for each fill request made via {@link
* AutofillService#onFillRequest(FillRequest, CancellationSignal, FillCallback)}.
* It contains a snapshot of the UI state, the view ids that were returned by
* the {@link AutofillService autofill service} as both required to trigger a save
* and optional that can be saved, and the id of the corresponding {@link
* FillRequest}.
* <p>
* This context allows you to inspect the values for the interesting views
* in the context they appeared. Also a reference to the corresponding fill
* request is useful to store meta-data in the client state bundle passed
* to {@link FillResponse.Builder#setClientState(Bundle)} to avoid interpreting
* the UI state again while saving.
*/
@DataClass(
genHiddenConstructor = true,
genAidl = false)
public final class FillContext implements Parcelable {
/**
* The id of the {@link FillRequest fill request} this context
* corresponds to. This is useful to associate your custom client
* state with every request to avoid reinterpreting the UI when saving
* user data.
*/
private final int mRequestId;
/**
* The screen content.
*/
private final @NonNull AssistStructure mStructure;
/**
* The AutofillId of the view that triggered autofill.
*/
private final @NonNull AutofillId mFocusedId;
/**
* Lookup table AutofillId->ViewNode to speed up {@link #findViewNodesByAutofillIds}
* This is purely a cache and can be deleted at any time
*/
private transient @Nullable ArrayMap<AutofillId, AssistStructure.ViewNode> mViewNodeLookupTable;
@Override
public String toString() {
if (!sDebug) return super.toString();
return "FillContext [reqId=" + mRequestId + ", focusedId=" + mFocusedId + "]";
}
/**
* Finds {@link ViewNode ViewNodes} that have the requested ids.
*
* @param ids The ids of the node to find.
*
* @return The nodes indexed in the same way as the ids.
*
* @hide
*/
@NonNull public ViewNode[] findViewNodesByAutofillIds(@NonNull AutofillId[] ids) {
final LinkedList<ViewNode> nodesToProcess = new LinkedList<>();
final ViewNode[] foundNodes = new AssistStructure.ViewNode[ids.length];
// Indexes of foundNodes that are not found yet
final SparseIntArray missingNodeIndexes = new SparseIntArray(ids.length);
for (int i = 0; i < ids.length; i++) {
if (mViewNodeLookupTable != null) {
int lookupTableIndex = mViewNodeLookupTable.indexOfKey(ids[i]);
if (lookupTableIndex >= 0) {
foundNodes[i] = mViewNodeLookupTable.valueAt(lookupTableIndex);
} else {
missingNodeIndexes.put(i, /* ignored */ 0);
}
} else {
missingNodeIndexes.put(i, /* ignored */ 0);
}
}
final int numWindowNodes = mStructure.getWindowNodeCount();
for (int i = 0; i < numWindowNodes; i++) {
nodesToProcess.add(mStructure.getWindowNodeAt(i).getRootViewNode());
}
while (missingNodeIndexes.size() > 0 && !nodesToProcess.isEmpty()) {
final ViewNode node = nodesToProcess.removeFirst();
for (int i = 0; i < missingNodeIndexes.size(); i++) {
final int index = missingNodeIndexes.keyAt(i);
final AutofillId id = ids[index];
if (id.equals(node.getAutofillId())) {
foundNodes[index] = node;
if (mViewNodeLookupTable == null) {
mViewNodeLookupTable = new ArrayMap<>(ids.length);
}
mViewNodeLookupTable.put(id, node);
missingNodeIndexes.removeAt(i);
break;
}
}
for (int i = 0; i < node.getChildCount(); i++) {
nodesToProcess.addLast(node.getChildAt(i));
}
}
// Remember which ids could not be resolved to not search for them again the next time
for (int i = 0; i < missingNodeIndexes.size(); i++) {
if (mViewNodeLookupTable == null) {
mViewNodeLookupTable = new ArrayMap<>(missingNodeIndexes.size());
}
mViewNodeLookupTable.put(ids[missingNodeIndexes.keyAt(i)], null);
}
return foundNodes;
}
// Code below generated by codegen v1.0.0.
//
// DO NOT MODIFY!
//
// To regenerate run:
// $ codegen $ANDROID_BUILD_TOP/frameworks/base/core/java/android/service/autofill/FillContext.java
//
// CHECKSTYLE:OFF Generated code
/**
* Creates a new FillContext.
*
* @param requestId
* The id of the {@link FillRequest fill request} this context
* corresponds to. This is useful to associate your custom client
* state with every request to avoid reinterpreting the UI when saving
* user data.
* @param structure
* The screen content.
* @param focusedId
* The AutofillId of the view that triggered autofill.
* @hide
*/
@DataClass.Generated.Member
public FillContext(
int requestId,
@NonNull AssistStructure structure,
@NonNull AutofillId focusedId) {
this.mRequestId = requestId;
this.mStructure = structure;
com.android.internal.util.AnnotationValidations.validate(
NonNull.class, null, mStructure);
this.mFocusedId = focusedId;
com.android.internal.util.AnnotationValidations.validate(
NonNull.class, null, mFocusedId);
// onConstructed(); // You can define this method to get a callback
}
/**
* The id of the {@link FillRequest fill request} this context
* corresponds to. This is useful to associate your custom client
* state with every request to avoid reinterpreting the UI when saving
* user data.
*/
@DataClass.Generated.Member
public int getRequestId() {
return mRequestId;
}
/**
* The screen content.
*/
@DataClass.Generated.Member
public @NonNull AssistStructure getStructure() {
return mStructure;
}
/**
* The AutofillId of the view that triggered autofill.
*/
@DataClass.Generated.Member
public @NonNull AutofillId getFocusedId() {
return mFocusedId;
}
@Override
@DataClass.Generated.Member
public void writeToParcel(Parcel dest, int flags) {
// You can override field parcelling by defining methods like:
// void parcelFieldName(Parcel dest, int flags) { ... }
dest.writeInt(mRequestId);
dest.writeTypedObject(mStructure, flags);
dest.writeTypedObject(mFocusedId, flags);
}
@Override
@DataClass.Generated.Member
public int describeContents() { return 0; }
@DataClass.Generated.Member
public static final @NonNull Parcelable.Creator<FillContext> CREATOR
= new Parcelable.Creator<FillContext>() {
@Override
public FillContext[] newArray(int size) {
return new FillContext[size];
}
@Override
@SuppressWarnings({"unchecked", "RedundantCast"})
public FillContext createFromParcel(Parcel in) {
// You can override field unparcelling by defining methods like:
// static FieldType unparcelFieldName(Parcel in) { ... }
int requestId = in.readInt();
AssistStructure structure = (AssistStructure) in.readTypedObject(AssistStructure.CREATOR);
AutofillId focusedId = (AutofillId) in.readTypedObject(AutofillId.CREATOR);
return new FillContext(
requestId,
structure,
focusedId);
}
};
@DataClass.Generated(
time = 1565152135263L,
codegenVersion = "1.0.0",
sourceFile = "frameworks/base/core/java/android/service/autofill/FillContext.java",
inputSignatures = "private final int mRequestId\nprivate final @android.annotation.NonNull android.app.assist.AssistStructure mStructure\nprivate final @android.annotation.NonNull android.view.autofill.AutofillId mFocusedId\nprivate transient @android.annotation.Nullable android.util.ArrayMap<android.view.autofill.AutofillId,android.app.assist.AssistStructure.ViewNode> mViewNodeLookupTable\npublic @java.lang.Override java.lang.String toString()\npublic @android.annotation.NonNull android.app.assist.AssistStructure.ViewNode[] findViewNodesByAutofillIds(android.view.autofill.AutofillId[])\nclass FillContext extends java.lang.Object implements [android.os.Parcelable]\n@com.android.internal.util.DataClass(genHiddenConstructor=true, genAidl=false)")
@Deprecated
private void __metadata() {}
}
|