diff options
10 files changed, 49 insertions, 106 deletions
diff --git a/api/current.txt b/api/current.txt index 65bcec7bf233..a98b05899a48 100644 --- a/api/current.txt +++ b/api/current.txt @@ -37911,7 +37911,6 @@ package android.service.autofill { } public static final class FieldClassification.Match { - method public java.lang.String getAlgorithm(); method public java.lang.String getRemoteId(); method public float getScore(); } diff --git a/api/system-current.txt b/api/system-current.txt index 49bd885f2641..66b6d990ec0c 100644 --- a/api/system-current.txt +++ b/api/system-current.txt @@ -3875,18 +3875,10 @@ package android.service.autofill { public abstract class AutofillFieldClassificationService extends android.app.Service { method public android.os.IBinder onBind(android.content.Intent); - method public android.service.autofill.AutofillFieldClassificationService.Scores onGetScores(java.lang.String, android.os.Bundle, java.util.List<android.view.autofill.AutofillValue>, java.util.List<java.lang.String>); + method public float[][] onGetScores(java.lang.String, android.os.Bundle, java.util.List<android.view.autofill.AutofillValue>, java.util.List<java.lang.String>); field public static final java.lang.String SERVICE_INTERFACE = "android.service.autofill.AutofillFieldClassificationService"; - } - - public static final class AutofillFieldClassificationService.Scores implements android.os.Parcelable { - ctor public AutofillFieldClassificationService.Scores(java.lang.String, int, int); - ctor public AutofillFieldClassificationService.Scores(android.os.Parcel); - method public int describeContents(); - method public java.lang.String getAlgorithm(); - method public float[][] getScores(); - method public void writeToParcel(android.os.Parcel, int); - field public static final android.os.Parcelable.Creator<android.service.autofill.AutofillFieldClassificationService.Scores> CREATOR; + field public static final java.lang.String SERVICE_META_DATA_KEY_AVAILABLE_ALGORITHMS = "android.autofill.field_classification.available_algorithms"; + field public static final java.lang.String SERVICE_META_DATA_KEY_DEFAULT_ALGORITHM = "android.autofill.field_classification.default_algorithm"; } } diff --git a/core/java/android/service/autofill/AutofillFieldClassificationService.java b/core/java/android/service/autofill/AutofillFieldClassificationService.java index 78e2bd1e667a..4e4caf04579f 100644 --- a/core/java/android/service/autofill/AutofillFieldClassificationService.java +++ b/core/java/android/service/autofill/AutofillFieldClassificationService.java @@ -64,6 +64,20 @@ public abstract class AutofillFieldClassificationService extends Service { public static final String SERVICE_INTERFACE = "android.service.autofill.AutofillFieldClassificationService"; + /** + * Manifest metadata key for the resource string containing the name of the default field + * classification algorithm. + */ + public static final String SERVICE_META_DATA_KEY_DEFAULT_ALGORITHM = + "android.autofill.field_classification.default_algorithm"; + /** + * Manifest metadata key for the resource string array containing the names of all field + * classification algorithms provided by the service. + */ + public static final String SERVICE_META_DATA_KEY_AVAILABLE_ALGORITHMS = + "android.autofill.field_classification.available_algorithms"; + + /** {@hide} **/ public static final String EXTRA_SCORES = "scores"; @@ -83,9 +97,9 @@ public abstract class AutofillFieldClassificationService extends Service { final List<AutofillValue> actualValues = ((List<AutofillValue>) args.arg4); @SuppressWarnings("unchecked") final String[] userDataValues = (String[]) args.arg5; - final Scores scores = onGetScores(algorithmName, algorithmArgs, actualValues, + final float[][] scores = onGetScores(algorithmName, algorithmArgs, actualValues, Arrays.asList(userDataValues)); - data.putParcelable(EXTRA_SCORES, scores); + data.putParcelable(EXTRA_SCORES, new Scores(scores)); break; default: Log.w(TAG, "Handling unknown message: " + action); @@ -124,13 +138,14 @@ public abstract class AutofillFieldClassificationService extends Service { * @param args optional arguments to be passed to the algorithm. * @param actualValues values entered by the user. * @param userDataValues values predicted from the user data. - * @return the calculated scores and the algorithm used. + * @return the calculated scores, with the first dimension representing actual values and the + * second dimension values from {@link UserData}. * * {@hide} */ @Nullable @SystemApi - public Scores onGetScores(@Nullable String algorithm, + public float[][] onGetScores(@Nullable String algorithm, @Nullable Bundle args, @NonNull List<AutofillValue> actualValues, @NonNull List<String> userDataValues) { throw new UnsupportedOperationException("Must be implemented by external service"); @@ -148,52 +163,27 @@ public abstract class AutofillFieldClassificationService extends Service { } } - - // TODO(b/70939974): it might be simpler to remove this class and return the float[][] directly, - // ignoring the request if the algorithm name is invalid. /** - * Represents field classification scores used in a batch calculation. + * Helper class used to encapsulate a float[][] in a Parcelable. * * {@hide} */ - @SystemApi public static final class Scores implements Parcelable { - private final String mAlgorithmName; - private final float[][] mScores; - - /* @hide */ - public Scores(String algorithmName, int size1, int size2) { - mAlgorithmName = algorithmName; - mScores = new float[size1][size2]; - } + public final float[][] scores; - public Scores(Parcel parcel) { - mAlgorithmName = parcel.readString(); + private Scores(Parcel parcel) { final int size1 = parcel.readInt(); final int size2 = parcel.readInt(); - mScores = new float[size1][size2]; + scores = new float[size1][size2]; for (int i = 0; i < size1; i++) { for (int j = 0; j < size2; j++) { - mScores[i][j] = parcel.readFloat(); + scores[i][j] = parcel.readFloat(); } } } - /** - * Gets the name of algorithm used to calculate the score. - */ - @NonNull - public String getAlgorithm() { - return mAlgorithmName; - } - - /** - * Gets the resulting scores, with the 1st dimension representing actual values and the 2nd - * dimension values from {@link UserData}. - */ - @NonNull - public float[][] getScores() { - return mScores; + private Scores(float[][] scores) { + this.scores = scores; } @Override @@ -203,20 +193,18 @@ public abstract class AutofillFieldClassificationService extends Service { @Override public void writeToParcel(Parcel parcel, int flags) { - parcel.writeString(mAlgorithmName); - int size1 = mScores.length; - int size2 = mScores[0].length; + int size1 = scores.length; + int size2 = scores[0].length; parcel.writeInt(size1); parcel.writeInt(size2); for (int i = 0; i < size1; i++) { for (int j = 0; j < size2; j++) { - parcel.writeFloat(mScores[i][j]); + parcel.writeFloat(scores[i][j]); } } } public static final Creator<Scores> CREATOR = new Creator<Scores>() { - @Override public Scores createFromParcel(Parcel parcel) { return new Scores(parcel); @@ -226,7 +214,6 @@ public abstract class AutofillFieldClassificationService extends Service { public Scores[] newArray(int size) { return new Scores[size]; } - }; } } diff --git a/core/java/android/service/autofill/FieldClassification.java b/core/java/android/service/autofill/FieldClassification.java index 536180335a83..cd1efd68df6f 100644 --- a/core/java/android/service/autofill/FieldClassification.java +++ b/core/java/android/service/autofill/FieldClassification.java @@ -105,21 +105,16 @@ public final class FieldClassification { /** * Represents the score of a {@link UserData} entry for the field. - * - * <p>The score is calculated by the given {@link #getAlgorithm() algorithm} and - * the entry is identified by {@link #getRemoteId()}. */ public static final class Match { private final String mRemoteId; private final float mScore; - private final String mAlgorithm; /** @hide */ - public Match(String remoteId, float score, String algorithm) { + public Match(String remoteId, float score) { mRemoteId = Preconditions.checkNotNull(remoteId); mScore = score; - mAlgorithm = algorithm; } /** @@ -150,38 +145,22 @@ public final class FieldClassification { return mScore; } - /** - * Gets the algorithm used to calculate this score. - * - * <p>Typically, this is either the algorithm set by - * {@link UserData.Builder#setFieldClassificationAlgorithm(String, android.os.Bundle)}, - * or the - * {@link android.view.autofill.AutofillManager#getDefaultFieldClassificationAlgorithm()}. - */ - @NonNull - public String getAlgorithm() { - return mAlgorithm; - } - @Override public String toString() { if (!sDebug) return super.toString(); final StringBuilder string = new StringBuilder("Match: remoteId="); Helper.appendRedacted(string, mRemoteId); - return string.append(", score=").append(mScore) - .append(", algorithm=").append(mAlgorithm) - .toString(); + return string.append(", score=").append(mScore).toString(); } private void writeToParcel(@NonNull Parcel parcel) { parcel.writeString(mRemoteId); parcel.writeFloat(mScore); - parcel.writeString(mAlgorithm); } private static Match readFromParcel(@NonNull Parcel parcel) { - return new Match(parcel.readString(), parcel.readFloat(), parcel.readString()); + return new Match(parcel.readString(), parcel.readFloat()); } } } diff --git a/core/java/android/service/autofill/UserData.java b/core/java/android/service/autofill/UserData.java index 2f9225acc520..9017848055e2 100644 --- a/core/java/android/service/autofill/UserData.java +++ b/core/java/android/service/autofill/UserData.java @@ -155,12 +155,9 @@ public final class UserData implements Parcelable { * <p>The currently available algorithms can be retrieve through * {@link AutofillManager#getAvailableFieldClassificationAlgorithms()}. * - * <p><b>Note: </b>The available algorithms in the Android System can change dinamically, - * so it's not guaranteed that the algorithm set here is the one that will be effectually - * used. If the algorithm set here is not available at runtime, the - * {@link AutofillManager#getDefaultFieldClassificationAlgorithm()} is used instead. - * You can verify which algorithm was used by calling - * {@link FieldClassification.Match#getAlgorithm()}. + * <p>If not set, the + * {@link AutofillManager#getDefaultFieldClassificationAlgorithm() default algorithm} is + * used instead. * * @param name name of the algorithm or {@code null} to used default. * @param args optional arguments to the algorithm. diff --git a/packages/ExtServices/src/android/ext/services/autofill/AutofillFieldClassificationServiceImpl.java b/packages/ExtServices/src/android/ext/services/autofill/AutofillFieldClassificationServiceImpl.java index 068ff8b4c5ae..4709d35018c2 100644 --- a/packages/ExtServices/src/android/ext/services/autofill/AutofillFieldClassificationServiceImpl.java +++ b/packages/ExtServices/src/android/ext/services/autofill/AutofillFieldClassificationServiceImpl.java @@ -34,7 +34,7 @@ public class AutofillFieldClassificationServiceImpl extends AutofillFieldClassif @Nullable @Override - public Scores onGetScores(@Nullable String algorithmName, + public float[][] onGetScores(@Nullable String algorithmName, @Nullable Bundle algorithmArgs, @NonNull List<AutofillValue> actualValues, @NonNull List<String> userDataValues) { if (ArrayUtils.isEmpty(actualValues) || ArrayUtils.isEmpty(userDataValues)) { @@ -55,14 +55,13 @@ public class AutofillFieldClassificationServiceImpl extends AutofillFieldClassif Log.d(TAG, "getScores() will return a " + actualValuesSize + "x" + userDataValuesSize + " matrix for " + actualAlgorithmName); } - final Scores scores = new Scores(actualAlgorithmName, actualValuesSize, userDataValuesSize); - final float[][] scoresMatrix = scores.getScores(); + final float[][] scores = new float[actualValuesSize][userDataValuesSize]; final EditDistanceScorer algorithm = EditDistanceScorer.getInstance(); for (int i = 0; i < actualValuesSize; i++) { for (int j = 0; j < userDataValuesSize; j++) { final float score = algorithm.getScore(actualValues.get(i), userDataValues.get(j)); - scoresMatrix[i][j] = score; + scores[i][j] = score; } } return scores; diff --git a/services/autofill/java/com/android/server/autofill/AutofillManagerService.java b/services/autofill/java/com/android/server/autofill/AutofillManagerService.java index c0628005d410..978ed25378e1 100644 --- a/services/autofill/java/com/android/server/autofill/AutofillManagerService.java +++ b/services/autofill/java/com/android/server/autofill/AutofillManagerService.java @@ -52,7 +52,6 @@ import android.os.UserHandle; import android.os.UserManager; import android.os.UserManagerInternal; import android.provider.Settings; -import android.service.autofill.AutofillFieldClassificationService.Scores; import android.service.autofill.FillEventHistory; import android.service.autofill.UserData; import android.util.LocalLog; diff --git a/services/autofill/java/com/android/server/autofill/AutofillManagerServiceShellCommand.java b/services/autofill/java/com/android/server/autofill/AutofillManagerServiceShellCommand.java index 44560879f028..4d69ef952f72 100644 --- a/services/autofill/java/com/android/server/autofill/AutofillManagerServiceShellCommand.java +++ b/services/autofill/java/com/android/server/autofill/AutofillManagerServiceShellCommand.java @@ -191,10 +191,7 @@ public final class AutofillManagerServiceShellCommand extends ShellCommand { if (scores == null) { pw.println("no score"); } else { - pw.print("algorithm: "); - pw.print(scores.getAlgorithm()); - pw.print(" score: "); - pw.println(scores.getScores()[0][0]); + pw.println(scores.scores[0][0]); } latch.countDown(); })); diff --git a/services/autofill/java/com/android/server/autofill/FieldClassificationStrategy.java b/services/autofill/java/com/android/server/autofill/FieldClassificationStrategy.java index 594032adb4b5..da5220104e3c 100644 --- a/services/autofill/java/com/android/server/autofill/FieldClassificationStrategy.java +++ b/services/autofill/java/com/android/server/autofill/FieldClassificationStrategy.java @@ -19,6 +19,8 @@ import static android.view.autofill.AutofillManager.FC_SERVICE_TIMEOUT; import static com.android.server.autofill.Helper.sDebug; import static com.android.server.autofill.Helper.sVerbose; +import static android.service.autofill.AutofillFieldClassificationService.SERVICE_META_DATA_KEY_AVAILABLE_ALGORITHMS; +import static android.service.autofill.AutofillFieldClassificationService.SERVICE_META_DATA_KEY_DEFAULT_ALGORITHM; import android.Manifest; import android.annotation.MainThread; @@ -62,11 +64,6 @@ final class FieldClassificationStrategy { private static final String TAG = "FieldClassificationStrategy"; - private static final String METADATA_KEY_DEFAULT_ALGORITHM = - "android.autofill.field_classification.default_algorithm"; - private static final String METADATA_KEY_AVAILABLE_ALGORITHMS = - "android.autofill.field_classification.available_algorithms"; - private final Context mContext; private final Object mLock = new Object(); private final int mUserId; @@ -221,7 +218,7 @@ final class FieldClassificationStrategy { */ @Nullable String[] getAvailableAlgorithms() { - return getMetadataValue(METADATA_KEY_AVAILABLE_ALGORITHMS, + return getMetadataValue(SERVICE_META_DATA_KEY_AVAILABLE_ALGORITHMS, (res, id) -> res.getStringArray(id)); } @@ -230,7 +227,7 @@ final class FieldClassificationStrategy { */ @Nullable String getDefaultAlgorithm() { - return getMetadataValue(METADATA_KEY_DEFAULT_ALGORITHM, (res, id) -> res.getString(id)); + return getMetadataValue(SERVICE_META_DATA_KEY_DEFAULT_ALGORITHM, (res, id) -> res.getString(id)); } @Nullable diff --git a/services/autofill/java/com/android/server/autofill/Session.java b/services/autofill/java/com/android/server/autofill/Session.java index a0e23a152224..63f83849db34 100644 --- a/services/autofill/java/com/android/server/autofill/Session.java +++ b/services/autofill/java/com/android/server/autofill/Session.java @@ -1172,8 +1172,6 @@ final class Session implements RemoteFillService.FillServiceCallbacks, ViewState Slog.w(TAG, "No field classification score on " + result); return; } - final float[][] scoresMatrix = scores.getScores(); - int i = 0, j = 0; try { for (i = 0; i < viewsSize; i++) { @@ -1182,8 +1180,7 @@ final class Session implements RemoteFillService.FillServiceCallbacks, ViewState ArrayList<Match> matches = null; for (j = 0; j < userValues.length; j++) { String remoteId = remoteIds[j]; - final String actualAlgorithm = scores.getAlgorithm(); - final float score = scoresMatrix[i][j]; + final float score = scores.scores[i][j]; if (score > 0) { if (sVerbose) { Slog.v(TAG, "adding score " + score + " at index " + j + " and id " @@ -1192,7 +1189,7 @@ final class Session implements RemoteFillService.FillServiceCallbacks, ViewState if (matches == null) { matches = new ArrayList<>(userValues.length); } - matches.add(new Match(remoteId, score, actualAlgorithm)); + matches.add(new Match(remoteId, score)); } else if (sVerbose) { Slog.v(TAG, "skipping score 0 at index " + j + " and id " + fieldId); @@ -1205,7 +1202,7 @@ final class Session implements RemoteFillService.FillServiceCallbacks, ViewState } } catch (ArrayIndexOutOfBoundsException e) { Slog.wtf(TAG, "Error accessing FC score at " + i + " x " + j + ": " - + Arrays.toString(scoresMatrix), e); + + Arrays.toString(scores.scores), e); return; } |