Merge "Minor API changes on Autofill Field Classification:"
diff --git a/api/current.txt b/api/current.txt
index 65bcec7..a98b058 100644
--- a/api/current.txt
+++ b/api/current.txt
@@ -37911,7 +37911,6 @@
   }
 
   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 49bd885..66b6d99 100644
--- a/api/system-current.txt
+++ b/api/system-current.txt
@@ -3875,18 +3875,10 @@
 
   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 78e2bd1..4e4caf0 100644
--- a/core/java/android/service/autofill/AutofillFieldClassificationService.java
+++ b/core/java/android/service/autofill/AutofillFieldClassificationService.java
@@ -64,6 +64,20 @@
     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 @@
                 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 @@
      * @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 @@
         }
     }
 
-
-    // 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;
+        public final float[][] scores;
 
-        /* @hide */
-        public Scores(String algorithmName, int size1, int size2) {
-            mAlgorithmName = algorithmName;
-            mScores = new float[size1][size2];
-        }
-
-        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 @@
 
         @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 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 5361803..cd1efd6 100644
--- a/core/java/android/service/autofill/FieldClassification.java
+++ b/core/java/android/service/autofill/FieldClassification.java
@@ -105,21 +105,16 @@
 
     /**
      * 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 @@
             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 2f9225a..9017848 100644
--- a/core/java/android/service/autofill/UserData.java
+++ b/core/java/android/service/autofill/UserData.java
@@ -155,12 +155,9 @@
          * <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 068ff8b..4709d35 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 @@
 
     @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 @@
             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 c062800..978ed25 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.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 4456087..4d69ef9 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 @@
             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 594032a..da52201 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 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 @@
 
     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 @@
      */
     @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 @@
      */
     @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 a0e23a1..63f8384 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 @@
                 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 @@
                     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 @@
                             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 @@
                 }
             } catch (ArrayIndexOutOfBoundsException e) {
                 Slog.wtf(TAG, "Error accessing FC score at " + i + " x " + j + ": "
-                        + Arrays.toString(scoresMatrix), e);
+                        + Arrays.toString(scores.scores), e);
                 return;
             }