| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.service.autofill; |
| 18 |
|
| 19 |
import static android.view.autofill.Helper.sDebug; |
| 20 |
|
| 21 |
import android.annotation.NonNull; |
| 22 |
import android.os.Parcel; |
| 23 |
import android.view.autofill.Helper; |
| 24 |
|
| 25 |
import java.util.ArrayList; |
| 26 |
import java.util.Collections; |
| 27 |
import java.util.Comparator; |
| 28 |
import java.util.List; |
| 29 |
import java.util.Objects; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
public final class FieldClassification { |
| 36 |
|
| 37 |
private final ArrayList<Match> mMatches; |
| 38 |
|
| 39 |
|
| 40 |
public FieldClassification(@NonNull ArrayList<Match> matches) { |
| 41 |
mMatches = Objects.requireNonNull(matches); |
| 42 |
Collections.sort(mMatches, new Comparator<Match>() { |
| 43 |
@Override |
| 44 |
public int compare(Match o1, Match o2) { |
| 45 |
if (o1.mScore > o2.mScore) return -1; |
| 46 |
if (o1.mScore < o2.mScore) return 1; |
| 47 |
return 0; |
| 48 |
}} |
| 49 |
); |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
@NonNull |
| 61 |
public List<Match> getMatches() { |
| 62 |
return mMatches; |
| 63 |
} |
| 64 |
|
| 65 |
@Override |
| 66 |
public String toString() { |
| 67 |
if (!sDebug) return super.toString(); |
| 68 |
|
| 69 |
return "FieldClassification: " + mMatches; |
| 70 |
} |
| 71 |
|
| 72 |
private void writeToParcel(Parcel parcel) { |
| 73 |
parcel.writeInt(mMatches.size()); |
| 74 |
for (int i = 0; i < mMatches.size(); i++) { |
| 75 |
mMatches.get(i).writeToParcel(parcel); |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
private static FieldClassification readFromParcel(Parcel parcel) { |
| 80 |
final int size = parcel.readInt(); |
| 81 |
final ArrayList<Match> matches = new ArrayList<>(); |
| 82 |
for (int i = 0; i < size; i++) { |
| 83 |
matches.add(i, Match.readFromParcel(parcel)); |
| 84 |
} |
| 85 |
|
| 86 |
return new FieldClassification(matches); |
| 87 |
} |
| 88 |
|
| 89 |
static FieldClassification[] readArrayFromParcel(Parcel parcel) { |
| 90 |
final int length = parcel.readInt(); |
| 91 |
final FieldClassification[] fcs = new FieldClassification[length]; |
| 92 |
for (int i = 0; i < length; i++) { |
| 93 |
fcs[i] = readFromParcel(parcel); |
| 94 |
} |
| 95 |
return fcs; |
| 96 |
} |
| 97 |
|
| 98 |
static void writeArrayToParcel(@NonNull Parcel parcel, @NonNull FieldClassification[] fcs) { |
| 99 |
parcel.writeInt(fcs.length); |
| 100 |
for (int i = 0; i < fcs.length; i++) { |
| 101 |
fcs[i].writeToParcel(parcel); |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
public static final class Match { |
| 109 |
|
| 110 |
private final String mCategoryId; |
| 111 |
private final float mScore; |
| 112 |
|
| 113 |
|
| 114 |
public Match(String categoryId, float score) { |
| 115 |
mCategoryId = Objects.requireNonNull(categoryId); |
| 116 |
mScore = score; |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
@NonNull |
| 123 |
public String getCategoryId() { |
| 124 |
return mCategoryId; |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
public float getScore() { |
| 144 |
return mScore; |
| 145 |
} |
| 146 |
|
| 147 |
@Override |
| 148 |
public String toString() { |
| 149 |
if (!sDebug) return super.toString(); |
| 150 |
|
| 151 |
final StringBuilder string = new StringBuilder("Match: categoryId="); |
| 152 |
Helper.appendRedacted(string, mCategoryId); |
| 153 |
return string.append(", score=").append(mScore).toString(); |
| 154 |
} |
| 155 |
|
| 156 |
private void writeToParcel(@NonNull Parcel parcel) { |
| 157 |
parcel.writeString(mCategoryId); |
| 158 |
parcel.writeFloat(mScore); |
| 159 |
} |
| 160 |
|
| 161 |
private static Match readFromParcel(@NonNull Parcel parcel) { |
| 162 |
return new Match(parcel.readString(), parcel.readFloat()); |
| 163 |
} |
| 164 |
} |
| 165 |
} |