Merge "Improved WebView -> ViewStructure mapping for Autofill."

This commit is contained in:
TreeHugger Robot
2017-03-21 19:49:34 +00:00
committed by Android (Google) Code Review
6 changed files with 103 additions and 0 deletions

View File

@@ -46356,6 +46356,7 @@ package android.view {
method public abstract void setFocused(boolean);
method public abstract void setHint(java.lang.CharSequence);
method public abstract void setId(int, java.lang.String, java.lang.String, java.lang.String);
method public abstract void setIdEntry(java.lang.String);
method public abstract void setInputType(int);
method public abstract void setLongClickable(boolean);
method public abstract void setOpaque(boolean);

View File

@@ -49819,6 +49819,7 @@ package android.view {
method public abstract void setFocused(boolean);
method public abstract void setHint(java.lang.CharSequence);
method public abstract void setId(int, java.lang.String, java.lang.String, java.lang.String);
method public abstract void setIdEntry(java.lang.String);
method public abstract void setInputType(int);
method public abstract void setLongClickable(boolean);
method public abstract void setOpaque(boolean);

View File

@@ -46725,6 +46725,7 @@ package android.view {
method public abstract void setFocused(boolean);
method public abstract void setHint(java.lang.CharSequence);
method public abstract void setId(int, java.lang.String, java.lang.String, java.lang.String);
method public abstract void setIdEntry(java.lang.String);
method public abstract void setInputType(int);
method public abstract void setLongClickable(boolean);
method public abstract void setOpaque(boolean);

View File

@@ -639,6 +639,7 @@ public class AssistStructure implements Parcelable {
static final int FLAGS_HAS_CHILDREN = 0x00100000;
static final int FLAGS_HAS_URL = 0x00080000;
static final int FLAGS_HAS_INPUT_TYPE = 0x00040000;
static final int FLAGS_HAS_ENTRY_ID = 0x00020000;
static final int FLAGS_ALL_CONTROL = 0xfff00000;
int mFlags;
@@ -672,7 +673,10 @@ public class AssistStructure implements Parcelable {
mIdPackage = preader.readString();
}
}
} else if ((flags&FLAGS_HAS_ENTRY_ID) != 0) {
mIdEntry = preader.readString();
}
if ((flags&FLAGS_HAS_AUTOFILL_DATA) != 0) {
mSanitized = in.readInt() == 1;
mAutofillId = in.readParcelable(null);
@@ -745,6 +749,8 @@ public class AssistStructure implements Parcelable {
int flags = mFlags & ~FLAGS_ALL_CONTROL;
if (mId != View.NO_ID) {
flags |= FLAGS_HAS_ID;
} else if (mIdEntry != null ){
flags |= FLAGS_HAS_ENTRY_ID;
}
if (mAutofillId != null) {
flags |= FLAGS_HAS_AUTOFILL_DATA;
@@ -805,7 +811,10 @@ public class AssistStructure implements Parcelable {
pwriter.writeString(mIdPackage);
}
}
} else if ((flags&FLAGS_HAS_ENTRY_ID) != 0) {
pwriter.writeString(mIdEntry);
}
if ((flags&FLAGS_HAS_AUTOFILL_DATA) != 0) {
writeSensitive = mSanitized || !sanitizeOnWrite;
out.writeInt(mSanitized ? 1 : 0);
@@ -887,6 +896,10 @@ public class AssistStructure implements Parcelable {
* If {@link #getId()} is a resource identifier, this is the entry name of that
* identifier. See {@link android.view.ViewStructure#setId ViewStructure.setId}
* for more information.
*
* <p>If the node represents a virtual view, it could also represent the entry id set by
* {@link android.view.ViewStructure#setIdEntry ViewStructure.setIdEntry}
*
*/
public String getIdEntry() {
return mIdEntry;
@@ -1360,6 +1373,11 @@ public class AssistStructure implements Parcelable {
mNode.mIdEntry = entryName;
}
@Override
public void setIdEntry(String entryName) {
mNode.mIdEntry = entryName;
}
@Override
public void setDimens(int left, int top, int scrollX, int scrollY, int width, int height) {
mNode.mX = left;

View File

@@ -40,6 +40,17 @@ public abstract class ViewStructure {
*/
public abstract void setId(int id, String packageName, String typeName, String entryName);
/**
* Sets the name of the identifier for this view.
*
* <p>Typically used when adding virtual children (through
* {@link #asyncNewChild(int, int, int)}) that does not map to Android {@link View}
* - otherwise, it's better to call {@link #setId(int, String, String, String)}.
*
* @param entryName The entry name of the view's identifier, or {@code null} if there is none.
*/
public abstract void setIdEntry(String entryName);
/**
* Set the basic dimensions of this view.
*

View File

@@ -41,6 +41,7 @@ import android.os.StrictMode;
import android.os.RemoteException;
import android.print.PrintDocumentAdapter;
import android.security.KeyChain;
import android.text.InputType;
import android.util.AttributeSet;
import android.util.Log;
import android.view.DragEvent;
@@ -2611,6 +2612,76 @@ public class WebView extends AbsoluteLayout
mProvider.getViewDelegate().onProvideVirtualStructure(structure);
}
/**
* {@inheritDoc}
*
* <p>The {@link ViewStructure} traditionally represents a {@link View}, while for web pages
* it represent HTML nodes. Hence, it's necessary to "map" the HTML properties in a way that is
* understood by the {@link android.service.autofill.AutofillService} implementations:
*
* <ol>
* <li>{@link ViewStructure#setClassName(String)} should be use to describe the type of node:
* <ol>
* <li>If the Android SDK provides a similar View, the full-qualified class name of that
* view should be used.
* <li>Otherwise, the class name should be {@code HTML.iframe}.
* </ol>
* <li>The W3C autofill field ({@code autocomplete} tag attribute) maps to
* {@link ViewStructure#setAutofillHint(String[])}.
* <li>The {@code type} attribute of {@code INPUT} tags maps to
* {@link ViewStructure#setInputType(int)}.
* <li>The {@code name} attribute maps to {@link ViewStructure#setIdEntry(String)}.
* <li>The {@code value} attribute maps to {@link ViewStructure#setText(CharSequence)}.
* <li>The {@code placeholder} attribute maps to {@link ViewStructure#setHint(CharSequence)}.
* <li>{@link ViewStructure#setDataIsSensitive(boolean)} whould only be called with
* {@code true} for form fields whose {@code value} attribute was not pre-loaded.
* </ol>
*
* <p>Example1: an HTML form with 2 fields for username and password.
*
* <pre class="prettyprint">
* <input type="text" name="username" value="mr.sparkle" autocomplete="username" placeholder="Email or username">
* <input type="password" name="password" autocomplete="current-password" placeholder="Password">
* </pre>
*
* <p>Would map to:
*
* <pre class="prettyprint">
* ViewStructure username = //structure.newChildForAutofill(...);
* username.setClassName("input");
* username.setInputType("android.widget.EditText");
* username.setAutofillHints("username");
* username.setIdEntry("username");
* username.setHint("Email or username");
* username.setAutofillType(View.AUTOFILL_TYPE_TEXT);
* username.setAutofillValue(AutofillValue.forText("mr.sparkle"));
* username.setText("mr.sparkle");
* username.setDataIsSensitive(true); // Contains real username, which is sensitive
*
* ViewStructure password = //structure.newChildForAutofill(...);
* password.setInputType("android.widget.EditText");
* password.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
* password.setAutofillHints("current-password");
* password.setIdEntry("password");
* password.setHint("Password");
* password.setAutofillType(View.AUTOFILL_TYPE_TEXT);
* password.setDataIsSensitive(false); // Value is not set
* </pre>
*
* <p>Example2: an IFRAME tag.
*
* <pre class="prettyprint">
* <iframe src="http://example.com/login"/>
* </pre>
*
* <p>Would map to:
*
* <pre class="prettyprint">
* ViewStructure iframe = //structure.newChildForAutofill(...);
* iframe.setClassName("HTML.iframe");
* iframe.setUrl("http://example.com/login");
* </pre>
*/
@Override
public void onProvideAutofillVirtualStructure(ViewStructure structure, int flags) {
mProvider.getViewDelegate().onProvideAutofillVirtualStructure(structure, flags);