1 /*
2  * Copyright (C) 2015 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 
17 package android.view;
18 
19 import android.annotation.NonNull;
20 import android.annotation.Nullable;
21 import android.annotation.SuppressLint;
22 import android.graphics.Matrix;
23 import android.graphics.Rect;
24 import android.os.Bundle;
25 import android.os.LocaleList;
26 import android.util.Pair;
27 import android.view.View.AutofillImportance;
28 import android.view.autofill.AutofillId;
29 import android.view.autofill.AutofillValue;
30 
31 import com.android.internal.util.Preconditions;
32 
33 import java.util.List;
34 
35 /**
36  * <p><code>ViewStructure</code> is a container for storing additional
37  * per-view data generated by {@link View#onProvideStructure
38  * View.onProvideStructure} and {@link View#onProvideAutofillStructure
39  * View.onProvideAutofillStructure}.
40  *
41  * <p>To learn more about using Autofill in your app, read the
42  * <a href="/guide/topics/text/autofill">Autofill Framework</a> guides.
43  *
44  */
45 public abstract class ViewStructure {
46 
47     /**
48      * Key used for writing active child view information to the content capture bundle.
49      *
50      * The value stored under this key will be an ordered list of Autofill IDs of child views.
51      *
52      * TODO(b/241498401): Add @TestApi in Android U
53      * @hide
54      */
55     public static final String EXTRA_ACTIVE_CHILDREN_IDS =
56             "android.view.ViewStructure.extra.ACTIVE_CHILDREN_IDS";
57 
58     /**
59      * Key used for writing the first active child's position to the content capture bundle.
60      *
61      * When active child view information is provided under the
62      * {@link #EXTRA_ACTIVE_CHILDREN_IDS}, the value stored under this key will be the
63      * 0-based position of the first child view in the list relative to the positions of child views
64      * in the containing View's dataset.
65      *
66      * TODO(b/241498401): Add @TestApi in Android U
67      * @hide */
68     public static final String EXTRA_FIRST_ACTIVE_POSITION =
69             "android.view.ViewStructure.extra.FIRST_ACTIVE_POSITION";
70 
71     /**
72      * Set the identifier for this view.
73      *
74      * @param id The view's identifier, as per {@link View#getId View.getId()}.
75      * @param packageName The package name of the view's identifier, or null if there is none.
76      * @param typeName The type name of the view's identifier, or null if there is none.
77      * @param entryName The entry name of the view's identifier, or null if there is none.
78      */
setId(int id, String packageName, String typeName, String entryName)79     public abstract void setId(int id, String packageName, String typeName, String entryName);
80 
81     /**
82      * Set the basic dimensions of this view.
83      *
84      * @param left The view's left position, in pixels relative to its parent's left edge.
85      * @param top The view's top position, in pixels relative to its parent's top edge.
86      * @param scrollX How much the view's x coordinate space has been scrolled, in pixels.
87      * @param scrollY How much the view's y coordinate space has been scrolled, in pixels.
88      * @param width The view's visible width, in pixels.  This is the width visible on screen,
89      * not the total data width of a scrollable view.
90      * @param height The view's visible height, in pixels.  This is the height visible on
91      * screen, not the total data height of a scrollable view.
92      */
setDimens(int left, int top, int scrollX, int scrollY, int width, int height)93     public abstract void setDimens(int left, int top, int scrollX, int scrollY, int width,
94             int height);
95 
96     /**
97      * Set the transformation matrix associated with this view, as per
98      * {@link View#getMatrix View.getMatrix()}, or null if there is none.
99      */
setTransformation(Matrix matrix)100     public abstract void setTransformation(Matrix matrix);
101 
102     /**
103      * Set the visual elevation (shadow) of the view, as per
104      * {@link View#getZ View.getZ()}.  Note this is <em>not</em> related
105      * to the physical Z-ordering of this view relative to its other siblings (that is how
106      * they overlap when drawing), it is only the visual representation for shadowing.
107      */
setElevation(float elevation)108     public abstract void setElevation(float elevation);
109 
110     /**
111      * Set an alpha transformation that is applied to this view, as per
112      * {@link View#getAlpha View.getAlpha()}.  Value ranges from 0
113      * (completely transparent) to 1 (completely opaque); the default is 1, which means
114      * no transformation.
115      */
setAlpha(float alpha)116     public abstract void setAlpha(float alpha);
117 
118     /**
119      * Set the visibility state of this view, as per
120      * {@link View#getVisibility View.getVisibility()}.
121      */
setVisibility(int visibility)122     public abstract void setVisibility(int visibility);
123 
124     /** @hide */
125     @SuppressWarnings("HiddenAbstractMethod")
setAssistBlocked(boolean state)126     public abstract void setAssistBlocked(boolean state);
127 
128     /**
129      * Set the enabled state of this view, as per {@link View#isEnabled View.isEnabled()}.
130      */
setEnabled(boolean state)131     public abstract void setEnabled(boolean state);
132 
133     /**
134      * Set the clickable state of this view, as per {@link View#isClickable View.isClickable()}.
135      */
setClickable(boolean state)136     public abstract void setClickable(boolean state);
137 
138     /**
139      * Set the long clickable state of this view, as per
140      * {@link View#isLongClickable View.isLongClickable()}.
141      */
setLongClickable(boolean state)142     public abstract void setLongClickable(boolean state);
143 
144     /**
145      * Set the context clickable state of this view, as per
146      * {@link View#isContextClickable View.isContextClickable()}.
147      */
setContextClickable(boolean state)148     public abstract void setContextClickable(boolean state);
149 
150     /**
151      * Set the focusable state of this view, as per {@link View#isFocusable View.isFocusable()}.
152      */
setFocusable(boolean state)153     public abstract void setFocusable(boolean state);
154 
155     /**
156      * Set the focused state of this view, as per {@link View#isFocused View.isFocused()}.
157      */
setFocused(boolean state)158     public abstract void setFocused(boolean state);
159 
160     /**
161      * Set the accessibility focused state of this view, as per
162      * {@link View#isAccessibilityFocused View.isAccessibilityFocused()}.
163      */
setAccessibilityFocused(boolean state)164     public abstract void setAccessibilityFocused(boolean state);
165 
166     /**
167      * Set the checkable state of this view, such as whether it implements the
168      * {@link android.widget.Checkable} interface.
169      */
setCheckable(boolean state)170     public abstract void setCheckable(boolean state);
171 
172     /**
173      * Set the checked state of this view, such as
174      * {@link android.widget.Checkable#isChecked Checkable.isChecked()}.
175      */
setChecked(boolean state)176     public abstract void setChecked(boolean state);
177 
178     /**
179      * Set the selected state of this view, as per {@link View#isSelected View.isSelected()}.
180      */
setSelected(boolean state)181     public abstract void setSelected(boolean state);
182 
183     /**
184      * Set the activated state of this view, as per {@link View#isActivated View.isActivated()}.
185      */
setActivated(boolean state)186     public abstract void setActivated(boolean state);
187 
188     /**
189      * Set the opaque state of this view, as per {@link View#isOpaque View.isOpaque()}.
190      */
setOpaque(boolean opaque)191     public abstract void setOpaque(boolean opaque);
192 
193     /**
194      * Set the class name of the view, as per
195      * {@link View#getAccessibilityClassName View.getAccessibilityClassName()}.
196      */
setClassName(String className)197     public abstract void setClassName(String className);
198 
199     /**
200      * Set the content description of the view, as per
201      * {@link View#getContentDescription View.getContentDescription()}.
202      */
setContentDescription(CharSequence contentDescription)203     public abstract void setContentDescription(CharSequence contentDescription);
204 
205     /**
206      * Set the text that is associated with this view.  There is no selection
207      * associated with the text.  The text may have style spans to supply additional
208      * display and semantic information.
209      */
setText(CharSequence text)210     public abstract void setText(CharSequence text);
211 
212     /**
213      * Like {@link #setText(CharSequence)} but with an active selection
214      * extending from <var>selectionStart</var> through <var>selectionEnd</var>.
215      */
setText(CharSequence text, int selectionStart, int selectionEnd)216     public abstract void setText(CharSequence text, int selectionStart, int selectionEnd);
217 
218     /**
219      * Explicitly set default global style information for text that was previously set with
220      * {@link #setText}.
221      *
222      * @param size The size, in pixels, of the text.
223      * @param fgColor The foreground color, packed as 0xAARRGGBB.
224      * @param bgColor The background color, packed as 0xAARRGGBB.
225      * @param style Style flags, as defined by {@link android.app.assist.AssistStructure.ViewNode}.
226      */
setTextStyle(float size, int fgColor, int bgColor, int style)227     public abstract void setTextStyle(float size, int fgColor, int bgColor, int style);
228 
229     /**
230      * Set line information for test that was previously supplied through
231      * {@link #setText(CharSequence)}.  This provides the line breaking of the text as it
232      * is shown on screen.  This function takes ownership of the provided arrays; you should
233      * not make further modification to them.
234      *
235      * @param charOffsets The offset in to {@link #setText} where a line starts.
236      * @param baselines The baseline where the line is drawn on screen.
237      */
setTextLines(int[] charOffsets, int[] baselines)238     public abstract void setTextLines(int[] charOffsets, int[] baselines);
239 
240     /**
241      * Sets the identifier used to set the text associated with this view.
242      *
243      * <p>Should only be set when the node is used for autofill purposes - it will be ignored
244      * when used for Assist.
245      */
setTextIdEntry(@onNull String entryName)246     public void setTextIdEntry(@NonNull String entryName) {
247         Preconditions.checkNotNull(entryName);
248     }
249 
250     /**
251      * Set optional hint text associated with this view; this is for example the text that is
252      * shown by an EditText when it is empty to indicate to the user the kind of text to input.
253      */
setHint(CharSequence hint)254     public abstract void setHint(CharSequence hint);
255 
256     /**
257      * Sets the identifier used to set the hint associated with this view.
258      *
259      * <p>Used as metadata for fingerprinting view nodes/structures.
260      *
261      * <p>Should only be set when the node is used for autofill purposes - it will be ignored
262      * when used for Assist.
263      */
setHintIdEntry(@onNull String entryName)264     public void setHintIdEntry(@NonNull String entryName) {
265         Preconditions.checkNotNull(entryName);
266     }
267 
268     /**
269      * Retrieve the last {@link #setText(CharSequence)}.
270      */
getText()271     public abstract CharSequence getText();
272 
273     /**
274      * Retrieve the last selection start set by {@link #setText(CharSequence, int, int)}.
275      */
getTextSelectionStart()276     public abstract int getTextSelectionStart();
277 
278     /**
279      * Retrieve the last selection end set by {@link #setText(CharSequence, int, int)}.
280      */
getTextSelectionEnd()281     public abstract int getTextSelectionEnd();
282 
283     /**
284      * Retrieve the last hint set by {@link #setHint}.
285      */
getHint()286     public abstract CharSequence getHint();
287 
288     /**
289      * Get extra data associated with this view structure; the returned Bundle is mutable,
290      * allowing you to view and modify its contents.  Keys placed in the Bundle should use
291      * an appropriate namespace prefix (such as com.google.MY_KEY) to avoid conflicts.
292      */
getExtras()293     public abstract Bundle getExtras();
294 
295     /**
296      * Returns true if {@link #getExtras} has been used to create extra content.
297      */
hasExtras()298     public abstract boolean hasExtras();
299 
300     /**
301      * Set the number of children of this view, which defines the range of indices you can
302      * use with {@link #newChild} and {@link #asyncNewChild}.  Calling this method again
303      * resets all of the child state of the view, removing any children that had previously
304      * been added.
305      */
setChildCount(int num)306     public abstract void setChildCount(int num);
307 
308     /**
309      * Add to this view's child count.  This increases the current child count by
310      * <var>num</var> children beyond what was last set by {@link #setChildCount}
311      * or {@link #addChildCount}.  The index at which the new child starts in the child
312      * array is returned.
313      *
314      * @param num The number of new children to add.
315      * @return Returns the index in the child array at which the new children start.
316      */
addChildCount(int num)317     public abstract int addChildCount(int num);
318 
319     /**
320      * Return the child count as set by {@link #setChildCount}.
321      */
getChildCount()322     public abstract int getChildCount();
323 
324     /**
325      * Create a new child {@link ViewStructure} in this view, putting into the list of
326      * children at <var>index</var>.
327      *
328      * <p><b>NOTE: </b>you must pre-allocate space for the child first, by calling either
329      * {@link #addChildCount(int)} or {@link #setChildCount(int)}.
330      *
331      * @return Returns an fresh {@link ViewStructure} ready to be filled in.
332      */
newChild(int index)333     public abstract ViewStructure newChild(int index);
334 
335     /**
336      * Like {@link #newChild}, but allows the caller to asynchronously populate the returned
337      * child.  It can transfer the returned {@link ViewStructure} to another thread for it
338      * to build its content (and children etc).  Once done, some thread must call
339      * {@link #asyncCommit} to tell the containing {@link ViewStructure} that the async
340      * population is done.
341      *
342      * <p><b>NOTE: </b>you must pre-allocate space for the child first, by calling either
343      * {@link #addChildCount(int)} or {@link #setChildCount(int)}.
344      *
345      * @return Returns an fresh {@link ViewStructure} ready to be filled in.
346      */
asyncNewChild(int index)347     public abstract ViewStructure asyncNewChild(int index);
348 
349     /**
350      * Gets the {@link AutofillId} associated with this node.
351      */
352     @Nullable
getAutofillId()353     public abstract AutofillId getAutofillId();
354 
355     /**
356      * Sets the {@link AutofillId} associated with this node.
357      */
setAutofillId(@onNull AutofillId id)358     public abstract void setAutofillId(@NonNull AutofillId id);
359 
360     /**
361      * Sets the {@link AutofillId} for this virtual node.
362      *
363      * @param parentId id of the parent node.
364      * @param virtualId an opaque ID to the Android System; it's the same id used on
365      *            {@link View#autofill(android.util.SparseArray)}.
366      */
setAutofillId(@onNull AutofillId parentId, int virtualId)367     public abstract void setAutofillId(@NonNull AutofillId parentId, int virtualId);
368 
369     /**
370      * Sets the {@link View#getAutofillType()} that can be used to autofill this node.
371      */
setAutofillType(@iew.AutofillType int type)372     public abstract void setAutofillType(@View.AutofillType int type);
373 
374     /**
375      * Sets the a hints that helps the autofill service to select the appropriate data to fill the
376      * view.
377      */
setAutofillHints(@ullable String[] hint)378     public abstract void setAutofillHints(@Nullable String[] hint);
379 
380     /**
381      * Sets the {@link AutofillValue} representing the current value of this node.
382      */
setAutofillValue(AutofillValue value)383     public abstract void setAutofillValue(AutofillValue value);
384 
385     /**
386      * Sets the options that can be used to autofill this node.
387      *
388      * <p>Typically used by nodes whose {@link View#getAutofillType()} is a list to indicate the
389      * meaning of each possible value in the list.
390      */
setAutofillOptions(CharSequence[] options)391     public abstract void setAutofillOptions(CharSequence[] options);
392 
393     /**
394      * Sets the {@link View#setImportantForAutofill(int) importantForAutofill mode} of the
395      * view associated with this node.
396      */
setImportantForAutofill(@utofillImportance int mode)397     public void setImportantForAutofill(@AutofillImportance int mode) {}
398 
399     /**
400      * Sets the MIME types accepted by this view. See {@link View#getReceiveContentMimeTypes()}.
401      *
402      * <p>Should only be set when the node is used for Autofill or Content Capture purposes - it
403      * will be ignored when used for Assist.
404      */
setReceiveContentMimeTypes( @uppressLintR) @ullable String[] mimeTypes)405     public void setReceiveContentMimeTypes(
406             @SuppressLint("NullableCollection") @Nullable String[] mimeTypes) {}
407 
408     /**
409      * Sets the {@link android.text.InputType} bits of this node.
410      *
411      * @param inputType inputType bits as defined by {@link android.text.InputType}.
412      */
setInputType(int inputType)413     public abstract void setInputType(int inputType);
414 
415     /**
416      * Sets whether the data on this node is sensitive; if it is, then its content (text, autofill
417      * value, etc..) is striped before calls to {@link
418      * android.service.autofill.AutofillService#onFillRequest(android.service.autofill.FillRequest,
419      * android.os.CancellationSignal, android.service.autofill.FillCallback)}.
420      *
421      * <p>By default, all nodes are assumed to be sensitive, and only nodes that does not have PII
422      * (Personally Identifiable Information - sensitive data such as email addresses, credit card
423      * numbers, passwords, etc...) should be marked as non-sensitive; a good rule of thumb is to
424      * mark as non-sensitive nodes whose value were statically set from resources.
425      *
426      * <p>Notice that the content of even sensitive nodes are sent to the service (through the
427      * {@link
428      * android.service.autofill.AutofillService#onSaveRequest(android.service.autofill.SaveRequest,
429      * android.service.autofill.SaveCallback)} call) when the user consented to save
430      * thedata, so it is important to set the content of sensitive nodes as well, but mark them as
431      * sensitive.
432      *
433      * <p>Should only be set when the node is used for autofill purposes - it will be ignored
434      * when used for Assist.
435      */
setDataIsSensitive(boolean sensitive)436     public abstract void setDataIsSensitive(boolean sensitive);
437 
438     /**
439      * Sets the minimum width in ems of the text associated with this view, when supported.
440      *
441      * <p>Should only be set when the node is used for autofill purposes - it will be ignored
442      * when used for Assist.
443      */
setMinTextEms(@uppressWarningsR) int minEms)444     public void setMinTextEms(@SuppressWarnings("unused") int minEms) {}
445 
446     /**
447      * Sets the maximum width in ems of the text associated with this view, when supported.
448      *
449      * <p>Should only be set when the node is used for autofill purposes - it will be ignored
450      * when used for Assist.
451      */
setMaxTextEms(@uppressWarningsR) int maxEms)452     public void setMaxTextEms(@SuppressWarnings("unused") int maxEms) {}
453 
454     /**
455      * Sets the maximum length of the text associated with this view, when supported.
456      *
457      * <p>Should only be set when the node is used for autofill purposes - it will be ignored
458      * when used for Assist.
459      */
setMaxTextLength(@uppressWarningsR) int maxLength)460     public void setMaxTextLength(@SuppressWarnings("unused") int maxLength) {}
461 
462     /**
463      * Call when done populating a {@link ViewStructure} returned by
464      * {@link #asyncNewChild}.
465      */
asyncCommit()466     public abstract void asyncCommit();
467 
468     /** @hide */
469     @SuppressWarnings("HiddenAbstractMethod")
getTempRect()470     public abstract Rect getTempRect();
471 
472     /**
473      * Sets the Web domain represented by this node.
474      *
475      * <p>Typically used when the view is a container for an HTML document.
476      *
477      * @param domain RFC 2396-compliant URI representing the domain.
478      */
setWebDomain(@ullable String domain)479     public abstract void setWebDomain(@Nullable String domain);
480 
481     /**
482      * Sets the the list of locales associated with this node.
483      */
setLocaleList(LocaleList localeList)484     public abstract void setLocaleList(LocaleList localeList);
485 
486     /**
487      * Creates a new {@link HtmlInfo.Builder} for the given HTML tag.
488      *
489      * @param tagName name of the HTML tag.
490      * @return a new builder.
491      */
newHtmlInfoBuilder(@onNull String tagName)492     public abstract HtmlInfo.Builder newHtmlInfoBuilder(@NonNull String tagName);
493 
494     /**
495      * Sets the HTML properties of this node when it represents an HTML element.
496      *
497      * <p>Should only be set when the node is used for autofill purposes - it will be ignored
498      * when used for assist.
499      *
500      * @param htmlInfo HTML properties.
501      */
setHtmlInfo(@onNull HtmlInfo htmlInfo)502     public abstract void setHtmlInfo(@NonNull HtmlInfo htmlInfo);
503 
504     /**
505      * Simplified representation of the HTML properties of a node that represents an HTML element.
506      */
507     public abstract static class HtmlInfo {
508 
509         /**
510          * Gets the HTML tag.
511          */
512         @NonNull
getTag()513         public abstract String getTag();
514 
515         /**
516          * Gets the list of HTML attributes.
517          *
518          * @return list of key/value pairs; could contain pairs with the same keys.
519          */
520         @Nullable
getAttributes()521         public abstract List<Pair<String, String>> getAttributes();
522 
523         /**
524          * Builder for {@link HtmlInfo} objects.
525          */
526         public abstract static class Builder {
527 
528             /**
529              * Adds an HTML attribute.
530              *
531              * @param name name of the attribute.
532              * @param value value of the attribute.
533              * @return same builder, for chaining.
534              */
addAttribute(@onNull String name, @NonNull String value)535             public abstract Builder addAttribute(@NonNull String name, @NonNull String value);
536 
537             /**
538              * Builds the {@link HtmlInfo} object.
539              *
540              * @return a new {@link HtmlInfo} instance.
541              */
build()542             public abstract HtmlInfo build();
543         }
544     }
545 }
546