blob: 5bf56cb9c1b5fccf6e5174335c0cb8a1ccef22b6 [file] [log] [blame]
Felipe Lemebb6bfea2017-12-04 11:22:25 -08001/*
2 * Copyright (C) 2017 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
17package android.service.autofill;
18
19import static android.view.autofill.Helper.sDebug;
20
21import android.annotation.NonNull;
Felipe Lemebb6bfea2017-12-04 11:22:25 -080022import android.os.Parcel;
Felipe Lemebb6bfea2017-12-04 11:22:25 -080023import android.view.autofill.Helper;
24
25import com.android.internal.util.Preconditions;
26
Felipe Leme51f6cd72017-12-18 09:30:53 -080027import java.util.ArrayList;
28import java.util.Collections;
29import java.util.Comparator;
Felipe Leme329d0402017-12-06 09:22:43 -080030import java.util.List;
31
Felipe Lemebb6bfea2017-12-04 11:22:25 -080032/**
Felipe Leme78172e72017-12-08 17:01:15 -080033 * Represents the <a href="AutofillService.html#FieldClassification">field classification</a>
34 * results for a given field.
Felipe Lemebb6bfea2017-12-04 11:22:25 -080035 */
Felipe Lemef1141c02017-12-18 09:38:36 -080036public final class FieldClassification {
Felipe Lemebb6bfea2017-12-04 11:22:25 -080037
Felipe Leme51f6cd72017-12-18 09:30:53 -080038 private final ArrayList<Match> mMatches;
Felipe Lemebb6bfea2017-12-04 11:22:25 -080039
40 /** @hide */
Felipe Leme51f6cd72017-12-18 09:30:53 -080041 public FieldClassification(@NonNull ArrayList<Match> matches) {
42 mMatches = Preconditions.checkNotNull(matches);
43 Collections.sort(mMatches, new Comparator<Match>() {
44 @Override
45 public int compare(Match o1, Match o2) {
46 if (o1.mScore > o2.mScore) return -1;
47 if (o1.mScore < o2.mScore) return 1;
48 return 0;
49 }}
50 );
Felipe Lemebb6bfea2017-12-04 11:22:25 -080051 }
52
53 /**
Felipe Leme51f6cd72017-12-18 09:30:53 -080054 * Gets the {@link Match matches} with the highest {@link Match#getScore() scores} (sorted in
55 * descending order).
Felipe Leme329d0402017-12-06 09:22:43 -080056 *
57 * <p><b>Note:</b> There's no guarantee of how many matches will be returned. In fact,
58 * the Android System might return just the top match to minimize the impact of field
59 * classification in the device's health.
Felipe Lemebb6bfea2017-12-04 11:22:25 -080060 */
61 @NonNull
Felipe Leme329d0402017-12-06 09:22:43 -080062 public List<Match> getMatches() {
Felipe Leme51f6cd72017-12-18 09:30:53 -080063 return mMatches;
Felipe Lemebb6bfea2017-12-04 11:22:25 -080064 }
65
66 @Override
67 public String toString() {
68 if (!sDebug) return super.toString();
69
Felipe Leme51f6cd72017-12-18 09:30:53 -080070 return "FieldClassification: " + mMatches;
Felipe Lemebb6bfea2017-12-04 11:22:25 -080071 }
72
Felipe Lemef1141c02017-12-18 09:38:36 -080073 private void writeToParcel(Parcel parcel) {
Felipe Leme51f6cd72017-12-18 09:30:53 -080074 parcel.writeInt(mMatches.size());
75 for (int i = 0; i < mMatches.size(); i++) {
76 mMatches.get(i).writeToParcel(parcel);
77 }
Felipe Lemebb6bfea2017-12-04 11:22:25 -080078 }
79
Felipe Lemef1141c02017-12-18 09:38:36 -080080 private static FieldClassification readFromParcel(Parcel parcel) {
81 final int size = parcel.readInt();
82 final ArrayList<Match> matches = new ArrayList<>();
83 for (int i = 0; i < size; i++) {
84 matches.add(i, Match.readFromParcel(parcel));
Felipe Lemebb6bfea2017-12-04 11:22:25 -080085 }
86
Felipe Lemef1141c02017-12-18 09:38:36 -080087 return new FieldClassification(matches);
88 }
89
90 static FieldClassification[] readArrayFromParcel(Parcel parcel) {
91 final int length = parcel.readInt();
92 final FieldClassification[] fcs = new FieldClassification[length];
93 for (int i = 0; i < length; i++) {
94 fcs[i] = readFromParcel(parcel);
Felipe Lemebb6bfea2017-12-04 11:22:25 -080095 }
Felipe Lemef1141c02017-12-18 09:38:36 -080096 return fcs;
97 }
98
99 static void writeArrayToParcel(@NonNull Parcel parcel, @NonNull FieldClassification[] fcs) {
100 parcel.writeInt(fcs.length);
101 for (int i = 0; i < fcs.length; i++) {
102 fcs[i].writeToParcel(parcel);
103 }
104 }
Felipe Lemebb6bfea2017-12-04 11:22:25 -0800105
106 /**
Felipe Leme78172e72017-12-08 17:01:15 -0800107 * Represents the score of a {@link UserData} entry for the field.
Felipe Lemebb6bfea2017-12-04 11:22:25 -0800108 */
Felipe Leme78172e72017-12-08 17:01:15 -0800109 public static final class Match {
Felipe Lemebb6bfea2017-12-04 11:22:25 -0800110
Felipe Lemefebb7332018-02-12 18:12:55 -0800111 private final String mCategoryId;
Felipe Leme329d0402017-12-06 09:22:43 -0800112 private final float mScore;
Felipe Lemebb6bfea2017-12-04 11:22:25 -0800113
114 /** @hide */
Felipe Lemefebb7332018-02-12 18:12:55 -0800115 public Match(String categoryId, float score) {
116 mCategoryId = Preconditions.checkNotNull(categoryId);
Felipe Lemebb6bfea2017-12-04 11:22:25 -0800117 mScore = score;
118 }
119
120 /**
Felipe Lemefebb7332018-02-12 18:12:55 -0800121 * Gets the category id of the {@link UserData} entry.
Felipe Lemebb6bfea2017-12-04 11:22:25 -0800122 */
123 @NonNull
Felipe Lemefebb7332018-02-12 18:12:55 -0800124 public String getCategoryId() {
125 return mCategoryId;
Felipe Lemebb6bfea2017-12-04 11:22:25 -0800126 }
127
128 /**
Felipe Leme78172e72017-12-08 17:01:15 -0800129 * Gets a classification score for the value of this field compared to the value of the
130 * {@link UserData} entry.
Felipe Lemebb6bfea2017-12-04 11:22:25 -0800131 *
Felipe Leme78172e72017-12-08 17:01:15 -0800132 * <p>The score is based in a comparison of the field value and the user data entry, and it
133 * ranges from {@code 0.0F} to {@code 1.0F}:
Felipe Lemebb6bfea2017-12-04 11:22:25 -0800134 * <ul>
Felipe Leme78172e72017-12-08 17:01:15 -0800135 * <li>{@code 1.0F} represents a full match ({@code 100%}).
136 * <li>{@code 0.0F} represents a full mismatch ({@code 0%}).
Felipe Lemebb6bfea2017-12-04 11:22:25 -0800137 * <li>Any other value is a partial match.
138 * </ul>
139 *
Felipe Leme27f45732017-12-22 09:05:22 -0800140 * <p>How the score is calculated depends on the
141 * {@link UserData.Builder#setFieldClassificationAlgorithm(String, android.os.Bundle)
142 * algorithm} used.
Felipe Lemebb6bfea2017-12-04 11:22:25 -0800143 */
Felipe Leme329d0402017-12-06 09:22:43 -0800144 public float getScore() {
Felipe Lemebb6bfea2017-12-04 11:22:25 -0800145 return mScore;
146 }
147
148 @Override
149 public String toString() {
150 if (!sDebug) return super.toString();
151
Felipe Lemefebb7332018-02-12 18:12:55 -0800152 final StringBuilder string = new StringBuilder("Match: categoryId=");
153 Helper.appendRedacted(string, mCategoryId);
Felipe Lemed11a6622018-01-18 13:38:24 -0800154 return string.append(", score=").append(mScore).toString();
Felipe Lemebb6bfea2017-12-04 11:22:25 -0800155 }
156
Felipe Leme78172e72017-12-08 17:01:15 -0800157 private void writeToParcel(@NonNull Parcel parcel) {
Felipe Lemefebb7332018-02-12 18:12:55 -0800158 parcel.writeString(mCategoryId);
Felipe Leme329d0402017-12-06 09:22:43 -0800159 parcel.writeFloat(mScore);
Felipe Lemebb6bfea2017-12-04 11:22:25 -0800160 }
161
Felipe Leme78172e72017-12-08 17:01:15 -0800162 private static Match readFromParcel(@NonNull Parcel parcel) {
Felipe Lemed11a6622018-01-18 13:38:24 -0800163 return new Match(parcel.readString(), parcel.readFloat());
Felipe Leme78172e72017-12-08 17:01:15 -0800164 }
Felipe Lemebb6bfea2017-12-04 11:22:25 -0800165 }
166}