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