Felipe Leme | bc055b0 | 2018-01-05 17:04:10 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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 | package android.service.autofill; |
| 17 | |
Felipe Leme | bc055b0 | 2018-01-05 17:04:10 -0800 | [diff] [blame] | 18 | import android.annotation.NonNull; |
| 19 | import android.annotation.Nullable; |
| 20 | import android.annotation.SystemApi; |
| 21 | import android.app.Service; |
| 22 | import android.content.Intent; |
| 23 | import android.os.Bundle; |
| 24 | import android.os.IBinder; |
| 25 | import android.os.Looper; |
| 26 | import android.os.Parcel; |
| 27 | import android.os.Parcelable; |
| 28 | import android.os.RemoteCallback; |
| 29 | import android.os.RemoteException; |
| 30 | import android.util.Log; |
| 31 | import android.view.autofill.AutofillValue; |
| 32 | |
| 33 | import com.android.internal.os.HandlerCaller; |
| 34 | import com.android.internal.os.SomeArgs; |
| 35 | |
| 36 | import java.util.Arrays; |
| 37 | import java.util.List; |
| 38 | |
| 39 | /** |
| 40 | * A service that calculates field classification scores. |
| 41 | * |
| 42 | * <p>A field classification score is a {@code float} representing how well an |
| 43 | * {@link AutofillValue} filled matches a expected value predicted by an autofill service |
| 44 | * —a full-match is {@code 1.0} (representing 100%), while a full mismatch is {@code 0.0}. |
| 45 | * |
| 46 | * <p>The exact score depends on the algorithm used to calculate it— the service must provide |
| 47 | * at least one default algorithm (which is used when the algorithm is not specified or is invalid), |
| 48 | * but it could provide more (in which case the algorithm name should be specifiied by the caller |
| 49 | * when calculating the scores). |
| 50 | * |
| 51 | * {@hide} |
| 52 | */ |
| 53 | @SystemApi |
| 54 | public abstract class AutofillFieldClassificationService extends Service { |
| 55 | |
| 56 | private static final String TAG = "AutofillFieldClassificationService"; |
| 57 | |
Felipe Leme | e4ac740 | 2018-01-16 19:37:00 -0800 | [diff] [blame] | 58 | private static final int MSG_GET_SCORES = 1; |
Felipe Leme | bc055b0 | 2018-01-05 17:04:10 -0800 | [diff] [blame] | 59 | |
| 60 | /** |
| 61 | * The {@link Intent} action that must be declared as handled by a service |
| 62 | * in its manifest for the system to recognize it as a quota providing service. |
| 63 | */ |
| 64 | public static final String SERVICE_INTERFACE = |
| 65 | "android.service.autofill.AutofillFieldClassificationService"; |
| 66 | |
Felipe Leme | d11a662 | 2018-01-18 13:38:24 -0800 | [diff] [blame] | 67 | /** |
| 68 | * Manifest metadata key for the resource string containing the name of the default field |
| 69 | * classification algorithm. |
| 70 | */ |
| 71 | public static final String SERVICE_META_DATA_KEY_DEFAULT_ALGORITHM = |
| 72 | "android.autofill.field_classification.default_algorithm"; |
| 73 | /** |
| 74 | * Manifest metadata key for the resource string array containing the names of all field |
| 75 | * classification algorithms provided by the service. |
| 76 | */ |
| 77 | public static final String SERVICE_META_DATA_KEY_AVAILABLE_ALGORITHMS = |
| 78 | "android.autofill.field_classification.available_algorithms"; |
| 79 | |
| 80 | |
Felipe Leme | bc055b0 | 2018-01-05 17:04:10 -0800 | [diff] [blame] | 81 | /** {@hide} **/ |
| 82 | public static final String EXTRA_SCORES = "scores"; |
| 83 | |
| 84 | private AutofillFieldClassificationServiceWrapper mWrapper; |
| 85 | |
| 86 | private final HandlerCaller.Callback mHandlerCallback = (msg) -> { |
| 87 | final int action = msg.what; |
| 88 | final Bundle data = new Bundle(); |
| 89 | final RemoteCallback callback; |
| 90 | switch (action) { |
Felipe Leme | bc055b0 | 2018-01-05 17:04:10 -0800 | [diff] [blame] | 91 | case MSG_GET_SCORES: |
| 92 | final SomeArgs args = (SomeArgs) msg.obj; |
| 93 | callback = (RemoteCallback) args.arg1; |
| 94 | final String algorithmName = (String) args.arg2; |
| 95 | final Bundle algorithmArgs = (Bundle) args.arg3; |
| 96 | @SuppressWarnings("unchecked") |
| 97 | final List<AutofillValue> actualValues = ((List<AutofillValue>) args.arg4); |
| 98 | @SuppressWarnings("unchecked") |
| 99 | final String[] userDataValues = (String[]) args.arg5; |
Felipe Leme | d11a662 | 2018-01-18 13:38:24 -0800 | [diff] [blame] | 100 | final float[][] scores = onGetScores(algorithmName, algorithmArgs, actualValues, |
Felipe Leme | bc055b0 | 2018-01-05 17:04:10 -0800 | [diff] [blame] | 101 | Arrays.asList(userDataValues)); |
Felipe Leme | 029f101 | 2018-01-22 11:53:44 -0800 | [diff] [blame] | 102 | if (scores != null) { |
| 103 | data.putParcelable(EXTRA_SCORES, new Scores(scores)); |
| 104 | } |
Felipe Leme | bc055b0 | 2018-01-05 17:04:10 -0800 | [diff] [blame] | 105 | break; |
| 106 | default: |
| 107 | Log.w(TAG, "Handling unknown message: " + action); |
| 108 | return; |
| 109 | } |
| 110 | callback.sendResult(data); |
| 111 | }; |
| 112 | |
| 113 | private final HandlerCaller mHandlerCaller = new HandlerCaller(null, Looper.getMainLooper(), |
| 114 | mHandlerCallback, true); |
| 115 | |
| 116 | /** @hide */ |
| 117 | public AutofillFieldClassificationService() { |
| 118 | |
| 119 | } |
| 120 | |
| 121 | @Override |
| 122 | public void onCreate() { |
| 123 | super.onCreate(); |
| 124 | mWrapper = new AutofillFieldClassificationServiceWrapper(); |
| 125 | } |
| 126 | |
| 127 | @Override |
| 128 | public IBinder onBind(Intent intent) { |
| 129 | return mWrapper; |
| 130 | } |
| 131 | |
| 132 | /** |
Felipe Leme | bc055b0 | 2018-01-05 17:04:10 -0800 | [diff] [blame] | 133 | * Calculates field classification scores in a batch. |
| 134 | * |
| 135 | * <p>See {@link AutofillFieldClassificationService} for more info about field classification |
| 136 | * scores. |
| 137 | * |
| 138 | * @param algorithm name of the algorithm to be used to calculate the scores. If invalid, the |
| 139 | * default algorithm will be used instead. |
| 140 | * @param args optional arguments to be passed to the algorithm. |
| 141 | * @param actualValues values entered by the user. |
| 142 | * @param userDataValues values predicted from the user data. |
Felipe Leme | d11a662 | 2018-01-18 13:38:24 -0800 | [diff] [blame] | 143 | * @return the calculated scores, with the first dimension representing actual values and the |
| 144 | * second dimension values from {@link UserData}. |
Felipe Leme | bc055b0 | 2018-01-05 17:04:10 -0800 | [diff] [blame] | 145 | * |
| 146 | * {@hide} |
| 147 | */ |
| 148 | @Nullable |
| 149 | @SystemApi |
Felipe Leme | d11a662 | 2018-01-18 13:38:24 -0800 | [diff] [blame] | 150 | public float[][] onGetScores(@Nullable String algorithm, |
Felipe Leme | bc055b0 | 2018-01-05 17:04:10 -0800 | [diff] [blame] | 151 | @Nullable Bundle args, @NonNull List<AutofillValue> actualValues, |
| 152 | @NonNull List<String> userDataValues) { |
Felipe Leme | 029f101 | 2018-01-22 11:53:44 -0800 | [diff] [blame] | 153 | Log.e(TAG, "service implementation (" + getClass() + " does not implement onGetScore()"); |
| 154 | return null; |
Felipe Leme | bc055b0 | 2018-01-05 17:04:10 -0800 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | private final class AutofillFieldClassificationServiceWrapper |
| 158 | extends IAutofillFieldClassificationService.Stub { |
Felipe Leme | bc055b0 | 2018-01-05 17:04:10 -0800 | [diff] [blame] | 159 | @Override |
| 160 | public void getScores(RemoteCallback callback, String algorithmName, Bundle algorithmArgs, |
| 161 | List<AutofillValue> actualValues, String[] userDataValues) |
| 162 | throws RemoteException { |
| 163 | // TODO(b/70939974): refactor to use PooledLambda |
| 164 | mHandlerCaller.obtainMessageOOOOO(MSG_GET_SCORES, callback, algorithmName, |
| 165 | algorithmArgs, actualValues, userDataValues).sendToTarget(); |
| 166 | } |
| 167 | } |
| 168 | |
Felipe Leme | bc055b0 | 2018-01-05 17:04:10 -0800 | [diff] [blame] | 169 | /** |
Felipe Leme | d11a662 | 2018-01-18 13:38:24 -0800 | [diff] [blame] | 170 | * Helper class used to encapsulate a float[][] in a Parcelable. |
Felipe Leme | bc055b0 | 2018-01-05 17:04:10 -0800 | [diff] [blame] | 171 | * |
| 172 | * {@hide} |
| 173 | */ |
Felipe Leme | bc055b0 | 2018-01-05 17:04:10 -0800 | [diff] [blame] | 174 | public static final class Scores implements Parcelable { |
Felipe Leme | fe05a52 | 2018-01-23 15:57:49 -0800 | [diff] [blame] | 175 | @NonNull |
Felipe Leme | d11a662 | 2018-01-18 13:38:24 -0800 | [diff] [blame] | 176 | public final float[][] scores; |
Felipe Leme | bc055b0 | 2018-01-05 17:04:10 -0800 | [diff] [blame] | 177 | |
Felipe Leme | d11a662 | 2018-01-18 13:38:24 -0800 | [diff] [blame] | 178 | private Scores(Parcel parcel) { |
Felipe Leme | bc055b0 | 2018-01-05 17:04:10 -0800 | [diff] [blame] | 179 | final int size1 = parcel.readInt(); |
| 180 | final int size2 = parcel.readInt(); |
Felipe Leme | d11a662 | 2018-01-18 13:38:24 -0800 | [diff] [blame] | 181 | scores = new float[size1][size2]; |
Felipe Leme | bc055b0 | 2018-01-05 17:04:10 -0800 | [diff] [blame] | 182 | for (int i = 0; i < size1; i++) { |
| 183 | for (int j = 0; j < size2; j++) { |
Felipe Leme | d11a662 | 2018-01-18 13:38:24 -0800 | [diff] [blame] | 184 | scores[i][j] = parcel.readFloat(); |
Felipe Leme | bc055b0 | 2018-01-05 17:04:10 -0800 | [diff] [blame] | 185 | } |
| 186 | } |
| 187 | } |
| 188 | |
Felipe Leme | fe05a52 | 2018-01-23 15:57:49 -0800 | [diff] [blame] | 189 | private Scores(@NonNull float[][] scores) { |
Felipe Leme | d11a662 | 2018-01-18 13:38:24 -0800 | [diff] [blame] | 190 | this.scores = scores; |
Felipe Leme | bc055b0 | 2018-01-05 17:04:10 -0800 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | @Override |
Felipe Leme | fe05a52 | 2018-01-23 15:57:49 -0800 | [diff] [blame] | 194 | public String toString() { |
| 195 | final int size1 = scores.length; |
| 196 | final int size2 = size1 > 0 ? scores[0].length : 0; |
| 197 | final StringBuilder builder = new StringBuilder("Scores [") |
| 198 | .append(size1).append("x").append(size2).append("] "); |
| 199 | for (int i = 0; i < size1; i++) { |
| 200 | builder.append(i).append(": ").append(Arrays.toString(scores[i])).append(' '); |
| 201 | } |
| 202 | return builder.toString(); |
| 203 | } |
| 204 | |
| 205 | @Override |
Felipe Leme | bc055b0 | 2018-01-05 17:04:10 -0800 | [diff] [blame] | 206 | public int describeContents() { |
| 207 | return 0; |
| 208 | } |
| 209 | |
| 210 | @Override |
| 211 | public void writeToParcel(Parcel parcel, int flags) { |
Felipe Leme | d11a662 | 2018-01-18 13:38:24 -0800 | [diff] [blame] | 212 | int size1 = scores.length; |
| 213 | int size2 = scores[0].length; |
Felipe Leme | bc055b0 | 2018-01-05 17:04:10 -0800 | [diff] [blame] | 214 | parcel.writeInt(size1); |
| 215 | parcel.writeInt(size2); |
| 216 | for (int i = 0; i < size1; i++) { |
| 217 | for (int j = 0; j < size2; j++) { |
Felipe Leme | d11a662 | 2018-01-18 13:38:24 -0800 | [diff] [blame] | 218 | parcel.writeFloat(scores[i][j]); |
Felipe Leme | bc055b0 | 2018-01-05 17:04:10 -0800 | [diff] [blame] | 219 | } |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | public static final Creator<Scores> CREATOR = new Creator<Scores>() { |
Felipe Leme | bc055b0 | 2018-01-05 17:04:10 -0800 | [diff] [blame] | 224 | @Override |
| 225 | public Scores createFromParcel(Parcel parcel) { |
| 226 | return new Scores(parcel); |
| 227 | } |
| 228 | |
| 229 | @Override |
| 230 | public Scores[] newArray(int size) { |
| 231 | return new Scores[size]; |
| 232 | } |
Felipe Leme | bc055b0 | 2018-01-05 17:04:10 -0800 | [diff] [blame] | 233 | }; |
| 234 | } |
| 235 | } |