| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
package android.service.autofill; |
| 17 |
|
| 18 |
import static android.provider.Settings.Secure.AUTOFILL_USER_DATA_MAX_CATEGORY_COUNT; |
| 19 |
import static android.provider.Settings.Secure.AUTOFILL_USER_DATA_MAX_FIELD_CLASSIFICATION_IDS_SIZE; |
| 20 |
import static android.provider.Settings.Secure.AUTOFILL_USER_DATA_MAX_USER_DATA_SIZE; |
| 21 |
import static android.provider.Settings.Secure.AUTOFILL_USER_DATA_MAX_VALUE_LENGTH; |
| 22 |
import static android.provider.Settings.Secure.AUTOFILL_USER_DATA_MIN_VALUE_LENGTH; |
| 23 |
import static android.view.autofill.Helper.sDebug; |
| 24 |
|
| 25 |
import android.annotation.NonNull; |
| 26 |
import android.annotation.Nullable; |
| 27 |
import android.annotation.TestApi; |
| 28 |
import android.app.ActivityThread; |
| 29 |
import android.content.ContentResolver; |
| 30 |
import android.os.Bundle; |
| 31 |
import android.os.Parcel; |
| 32 |
import android.os.Parcelable; |
| 33 |
import android.provider.Settings; |
| 34 |
import android.service.autofill.FieldClassification.Match; |
| 35 |
import android.text.TextUtils; |
| 36 |
import android.util.ArrayMap; |
| 37 |
import android.util.ArraySet; |
| 38 |
import android.util.Log; |
| 39 |
import android.view.autofill.AutofillManager; |
| 40 |
import android.view.autofill.Helper; |
| 41 |
|
| 42 |
import com.android.internal.util.Preconditions; |
| 43 |
|
| 44 |
import java.io.PrintWriter; |
| 45 |
import java.util.ArrayList; |
| 46 |
import java.util.Objects; |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
public final class UserData implements FieldClassificationUserData, Parcelable { |
| 53 |
|
| 54 |
private static final String TAG = "UserData"; |
| 55 |
|
| 56 |
private static final int DEFAULT_MAX_USER_DATA_SIZE = 50; |
| 57 |
private static final int DEFAULT_MAX_CATEGORY_COUNT = 10; |
| 58 |
private static final int DEFAULT_MAX_FIELD_CLASSIFICATION_IDS_SIZE = 10; |
| 59 |
private static final int DEFAULT_MIN_VALUE_LENGTH = 3; |
| 60 |
private static final int DEFAULT_MAX_VALUE_LENGTH = 100; |
| 61 |
|
| 62 |
private final String mId; |
| 63 |
private final String[] mCategoryIds; |
| 64 |
private final String[] mValues; |
| 65 |
|
| 66 |
private final String mDefaultAlgorithm; |
| 67 |
private final Bundle mDefaultArgs; |
| 68 |
private final ArrayMap<String, String> mCategoryAlgorithms; |
| 69 |
private final ArrayMap<String, Bundle> mCategoryArgs; |
| 70 |
|
| 71 |
private UserData(Builder builder) { |
| 72 |
mId = builder.mId; |
| 73 |
mCategoryIds = new String[builder.mCategoryIds.size()]; |
| 74 |
builder.mCategoryIds.toArray(mCategoryIds); |
| 75 |
mValues = new String[builder.mValues.size()]; |
| 76 |
builder.mValues.toArray(mValues); |
| 77 |
builder.mValues.toArray(mValues); |
| 78 |
|
| 79 |
mDefaultAlgorithm = builder.mDefaultAlgorithm; |
| 80 |
mDefaultArgs = builder.mDefaultArgs; |
| 81 |
mCategoryAlgorithms = builder.mCategoryAlgorithms; |
| 82 |
mCategoryArgs = builder.mCategoryArgs; |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
@Nullable |
| 90 |
@Override |
| 91 |
public String getFieldClassificationAlgorithm() { |
| 92 |
return mDefaultAlgorithm; |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
@Override |
| 97 |
public Bundle getDefaultFieldClassificationArgs() { |
| 98 |
return mDefaultArgs; |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
@Nullable |
| 110 |
@Override |
| 111 |
public String getFieldClassificationAlgorithmForCategory(@NonNull String categoryId) { |
| 112 |
Objects.requireNonNull(categoryId); |
| 113 |
if (mCategoryAlgorithms == null || !mCategoryAlgorithms.containsKey(categoryId)) { |
| 114 |
return null; |
| 115 |
} |
| 116 |
return mCategoryAlgorithms.get(categoryId); |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
public String getId() { |
| 123 |
return mId; |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
@Override |
| 128 |
public String[] getCategoryIds() { |
| 129 |
return mCategoryIds; |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
@Override |
| 134 |
public String[] getValues() { |
| 135 |
return mValues; |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
@TestApi |
| 140 |
@Override |
| 141 |
public ArrayMap<String, String> getFieldClassificationAlgorithms() { |
| 142 |
return mCategoryAlgorithms; |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
@Override |
| 147 |
public ArrayMap<String, Bundle> getFieldClassificationArgs() { |
| 148 |
return mCategoryArgs; |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
public void dump(String prefix, PrintWriter pw) { |
| 153 |
pw.print(prefix); pw.print("id: "); pw.print(mId); |
| 154 |
pw.print(prefix); pw.print("Default Algorithm: "); pw.print(mDefaultAlgorithm); |
| 155 |
pw.print(prefix); pw.print("Default Args"); pw.print(mDefaultArgs); |
| 156 |
if (mCategoryAlgorithms != null && mCategoryAlgorithms.size() > 0) { |
| 157 |
pw.print(prefix); pw.print("Algorithms per category: "); |
| 158 |
for (int i = 0; i < mCategoryAlgorithms.size(); i++) { |
| 159 |
pw.print(prefix); pw.print(prefix); pw.print(mCategoryAlgorithms.keyAt(i)); |
| 160 |
pw.print(": "); pw.println(Helper.getRedacted(mCategoryAlgorithms.valueAt(i))); |
| 161 |
pw.print("args="); pw.print(mCategoryArgs.get(mCategoryAlgorithms.keyAt(i))); |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
pw.print(prefix); pw.print("Field ids size: "); pw.println(mCategoryIds.length); |
| 166 |
for (int i = 0; i < mCategoryIds.length; i++) { |
| 167 |
pw.print(prefix); pw.print(prefix); pw.print(i); pw.print(": "); |
| 168 |
pw.println(Helper.getRedacted(mCategoryIds[i])); |
| 169 |
} |
| 170 |
pw.print(prefix); pw.print("Values size: "); pw.println(mValues.length); |
| 171 |
for (int i = 0; i < mValues.length; i++) { |
| 172 |
pw.print(prefix); pw.print(prefix); pw.print(i); pw.print(": "); |
| 173 |
pw.println(Helper.getRedacted(mValues[i])); |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
public static void dumpConstraints(String prefix, PrintWriter pw) { |
| 179 |
pw.print(prefix); pw.print("maxUserDataSize: "); pw.println(getMaxUserDataSize()); |
| 180 |
pw.print(prefix); pw.print("maxFieldClassificationIdsSize: "); |
| 181 |
pw.println(getMaxFieldClassificationIdsSize()); |
| 182 |
pw.print(prefix); pw.print("maxCategoryCount: "); pw.println(getMaxCategoryCount()); |
| 183 |
pw.print(prefix); pw.print("minValueLength: "); pw.println(getMinValueLength()); |
| 184 |
pw.print(prefix); pw.print("maxValueLength: "); pw.println(getMaxValueLength()); |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
public static final class Builder { |
| 191 |
private final String mId; |
| 192 |
private final ArrayList<String> mCategoryIds; |
| 193 |
private final ArrayList<String> mValues; |
| 194 |
private String mDefaultAlgorithm; |
| 195 |
private Bundle mDefaultArgs; |
| 196 |
|
| 197 |
|
| 198 |
private ArrayMap<String, String> mCategoryAlgorithms; |
| 199 |
private ArrayMap<String, Bundle> mCategoryArgs; |
| 200 |
|
| 201 |
private boolean mDestroyed; |
| 202 |
|
| 203 |
|
| 204 |
private final ArraySet<String> mUniqueCategoryIds; |
| 205 |
|
| 206 |
private final ArraySet<String> mUniqueValueCategoryPairs; |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
public Builder(@NonNull String id, @NonNull String value, @NonNull String categoryId) { |
| 241 |
mId = checkNotEmpty("id", id); |
| 242 |
checkNotEmpty("categoryId", categoryId); |
| 243 |
checkValidValue(value); |
| 244 |
final int maxUserDataSize = getMaxUserDataSize(); |
| 245 |
mCategoryIds = new ArrayList<>(maxUserDataSize); |
| 246 |
mValues = new ArrayList<>(maxUserDataSize); |
| 247 |
mUniqueValueCategoryPairs = new ArraySet<>(maxUserDataSize); |
| 248 |
|
| 249 |
mUniqueCategoryIds = new ArraySet<>(getMaxCategoryCount()); |
| 250 |
|
| 251 |
addMapping(value, categoryId); |
| 252 |
} |
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
@NonNull |
| 271 |
public Builder setFieldClassificationAlgorithm(@Nullable String name, |
| 272 |
@Nullable Bundle args) { |
| 273 |
throwIfDestroyed(); |
| 274 |
mDefaultAlgorithm = name; |
| 275 |
mDefaultArgs = args; |
| 276 |
return this; |
| 277 |
} |
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
@NonNull |
| 297 |
public Builder setFieldClassificationAlgorithmForCategory(@NonNull String categoryId, |
| 298 |
@Nullable String name, @Nullable Bundle args) { |
| 299 |
throwIfDestroyed(); |
| 300 |
Objects.requireNonNull(categoryId); |
| 301 |
if (mCategoryAlgorithms == null) { |
| 302 |
mCategoryAlgorithms = new ArrayMap<>(getMaxCategoryCount()); |
| 303 |
} |
| 304 |
if (mCategoryArgs == null) { |
| 305 |
mCategoryArgs = new ArrayMap<>(getMaxCategoryCount()); |
| 306 |
} |
| 307 |
mCategoryAlgorithms.put(categoryId, name); |
| 308 |
mCategoryArgs.put(categoryId, args); |
| 309 |
return this; |
| 310 |
} |
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
@NonNull |
| 340 |
public Builder add(@NonNull String value, @NonNull String categoryId) { |
| 341 |
throwIfDestroyed(); |
| 342 |
checkNotEmpty("categoryId", categoryId); |
| 343 |
checkValidValue(value); |
| 344 |
|
| 345 |
if (!mUniqueCategoryIds.contains(categoryId)) { |
| 346 |
|
| 347 |
Preconditions.checkState(mUniqueCategoryIds.size() < getMaxCategoryCount(), |
| 348 |
"already added %d unique category ids", mUniqueCategoryIds.size()); |
| 349 |
} |
| 350 |
|
| 351 |
Preconditions.checkState(mValues.size() < getMaxUserDataSize(), |
| 352 |
"already added %d elements", mValues.size()); |
| 353 |
addMapping(value, categoryId); |
| 354 |
|
| 355 |
return this; |
| 356 |
} |
| 357 |
|
| 358 |
private void addMapping(@NonNull String value, @NonNull String categoryId) { |
| 359 |
final String pair = value + ":" + categoryId; |
| 360 |
if (mUniqueValueCategoryPairs.contains(pair)) { |
| 361 |
|
| 362 |
Log.w(TAG, "Ignoring entry with same value / category"); |
| 363 |
return; |
| 364 |
} |
| 365 |
mCategoryIds.add(categoryId); |
| 366 |
mValues.add(value); |
| 367 |
mUniqueCategoryIds.add(categoryId); |
| 368 |
mUniqueValueCategoryPairs.add(pair); |
| 369 |
} |
| 370 |
|
| 371 |
private String checkNotEmpty(@NonNull String name, @Nullable String value) { |
| 372 |
Objects.requireNonNull(value); |
| 373 |
Preconditions.checkArgument(!TextUtils.isEmpty(value), "%s cannot be empty", name); |
| 374 |
return value; |
| 375 |
} |
| 376 |
|
| 377 |
private void checkValidValue(@Nullable String value) { |
| 378 |
Objects.requireNonNull(value); |
| 379 |
final int length = value.length(); |
| 380 |
Preconditions.checkArgumentInRange(length, getMinValueLength(), |
| 381 |
getMaxValueLength(), "value length (" + length + ")"); |
| 382 |
} |
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
@NonNull |
| 394 |
public UserData build() { |
| 395 |
throwIfDestroyed(); |
| 396 |
mDestroyed = true; |
| 397 |
return new UserData(this); |
| 398 |
} |
| 399 |
|
| 400 |
private void throwIfDestroyed() { |
| 401 |
if (mDestroyed) { |
| 402 |
throw new IllegalStateException("Already called #build()"); |
| 403 |
} |
| 404 |
} |
| 405 |
} |
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
@Override |
| 411 |
public String toString() { |
| 412 |
if (!sDebug) return super.toString(); |
| 413 |
|
| 414 |
final StringBuilder builder = new StringBuilder("UserData: [id=").append(mId); |
| 415 |
|
| 416 |
builder.append(", categoryIds="); |
| 417 |
Helper.appendRedacted(builder, mCategoryIds); |
| 418 |
builder.append(", values="); |
| 419 |
Helper.appendRedacted(builder, mValues); |
| 420 |
return builder.append("]").toString(); |
| 421 |
} |
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
@Override |
| 428 |
public int describeContents() { |
| 429 |
return 0; |
| 430 |
} |
| 431 |
|
| 432 |
@Override |
| 433 |
public void writeToParcel(Parcel parcel, int flags) { |
| 434 |
parcel.writeString(mId); |
| 435 |
parcel.writeStringArray(mCategoryIds); |
| 436 |
parcel.writeStringArray(mValues); |
| 437 |
parcel.writeString(mDefaultAlgorithm); |
| 438 |
parcel.writeBundle(mDefaultArgs); |
| 439 |
parcel.writeMap(mCategoryAlgorithms); |
| 440 |
parcel.writeMap(mCategoryArgs); |
| 441 |
} |
| 442 |
|
| 443 |
public static final @android.annotation.NonNull Parcelable.Creator<UserData> CREATOR = |
| 444 |
new Parcelable.Creator<UserData>() { |
| 445 |
@Override |
| 446 |
public UserData createFromParcel(Parcel parcel) { |
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
final String id = parcel.readString(); |
| 451 |
final String[] categoryIds = parcel.readStringArray(); |
| 452 |
final String[] values = parcel.readStringArray(); |
| 453 |
final String defaultAlgorithm = parcel.readString(); |
| 454 |
final Bundle defaultArgs = parcel.readBundle(); |
| 455 |
final ArrayMap<String, String> categoryAlgorithms = new ArrayMap<>(); |
| 456 |
parcel.readMap(categoryAlgorithms, String.class.getClassLoader()); |
| 457 |
final ArrayMap<String, Bundle> categoryArgs = new ArrayMap<>(); |
| 458 |
parcel.readMap(categoryArgs, Bundle.class.getClassLoader()); |
| 459 |
|
| 460 |
final Builder builder = new Builder(id, values[0], categoryIds[0]) |
| 461 |
.setFieldClassificationAlgorithm(defaultAlgorithm, defaultArgs); |
| 462 |
|
| 463 |
for (int i = 1; i < categoryIds.length; i++) { |
| 464 |
String categoryId = categoryIds[i]; |
| 465 |
builder.add(values[i], categoryId); |
| 466 |
} |
| 467 |
|
| 468 |
final int size = categoryAlgorithms.size(); |
| 469 |
if (size > 0) { |
| 470 |
for (int i = 0; i < size; i++) { |
| 471 |
final String categoryId = categoryAlgorithms.keyAt(i); |
| 472 |
builder.setFieldClassificationAlgorithmForCategory(categoryId, |
| 473 |
categoryAlgorithms.valueAt(i), categoryArgs.get(categoryId)); |
| 474 |
} |
| 475 |
} |
| 476 |
return builder.build(); |
| 477 |
} |
| 478 |
|
| 479 |
@Override |
| 480 |
public UserData[] newArray(int size) { |
| 481 |
return new UserData[size]; |
| 482 |
} |
| 483 |
}; |
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
public static int getMaxUserDataSize() { |
| 489 |
return getInt(AUTOFILL_USER_DATA_MAX_USER_DATA_SIZE, DEFAULT_MAX_USER_DATA_SIZE); |
| 490 |
} |
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
public static int getMaxFieldClassificationIdsSize() { |
| 497 |
return getInt(AUTOFILL_USER_DATA_MAX_FIELD_CLASSIFICATION_IDS_SIZE, |
| 498 |
DEFAULT_MAX_FIELD_CLASSIFICATION_IDS_SIZE); |
| 499 |
} |
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
public static int getMaxCategoryCount() { |
| 506 |
return getInt(AUTOFILL_USER_DATA_MAX_CATEGORY_COUNT, DEFAULT_MAX_CATEGORY_COUNT); |
| 507 |
} |
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
public static int getMinValueLength() { |
| 514 |
return getInt(AUTOFILL_USER_DATA_MIN_VALUE_LENGTH, DEFAULT_MIN_VALUE_LENGTH); |
| 515 |
} |
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
public static int getMaxValueLength() { |
| 522 |
return getInt(AUTOFILL_USER_DATA_MAX_VALUE_LENGTH, DEFAULT_MAX_VALUE_LENGTH); |
| 523 |
} |
| 524 |
|
| 525 |
private static int getInt(String settings, int defaultValue) { |
| 526 |
ContentResolver cr = null; |
| 527 |
final ActivityThread at = ActivityThread.currentActivityThread(); |
| 528 |
if (at != null) { |
| 529 |
cr = at.getApplication().getContentResolver(); |
| 530 |
} |
| 531 |
|
| 532 |
if (cr == null) { |
| 533 |
Log.w(TAG, "Could not read from " + settings + "; hardcoding " + defaultValue); |
| 534 |
return defaultValue; |
| 535 |
} |
| 536 |
return Settings.Secure.getInt(cr, settings, defaultValue); |
| 537 |
} |
| 538 |
} |