public void autofill()

in AutofillFramework/Application/src/main/java/com/example/android/autofill/app/view/autofillable/CustomVirtualView.java [67:137]


    public void autofill(SparseArray<AutofillValue> values) {
        Context context = getContext();

        // User has just selected a Dataset from the list of autofill suggestions.
        // The Dataset is comprised of a list of AutofillValues, with each AutofillValue meant
        // to fill a specific autofillable view. Now we have to update the UI based on the
        // AutofillValues in the list, but first we make sure all autofilled values belong to the
        // same partition
        if (DEBUG) {
            Log.d(TAG, "autofill(): " + values);
        }

        // First get the name of all partitions in the values
        ArraySet<String> partitions = new ArraySet<>();
        for (int i = 0; i < values.size(); i++) {
            int id = values.keyAt(i);
            Partition partition = mPartitionsByAutofillId.get(id);
            if (partition == null) {
                showError(context.getString(R.string.message_autofill_no_partitions, id,
                        mPartitionsByAutofillId));
                return;
            }
            partitions.add(partition.mName);
        }

        // Then make sure they follow the Highlander rule (There can be only one)
        if (partitions.size() != 1) {
            showError(context.getString(R.string.message_autofill_blocked, partitions));
            return;
        }

        // Finally, autofill it.
        DateFormat df = android.text.format.DateFormat.getDateFormat(context);
        for (int i = 0; i < values.size(); i++) {
            int id = values.keyAt(i);
            AutofillValue value = values.valueAt(i);
            Item item = mVirtualViews.get(id);

            if (item == null) {
                Log.w(TAG, "No item for id " + id);
                continue;
            }

            if (!item.editable) {
                showError(context.getString(R.string.message_autofill_readonly, item.text));
                continue;
            }

            // Check if the type was properly set by the autofill service
            if (DEBUG) {
                Log.d(TAG, "Validating " + i
                        + ": expectedType=" + Util.getAutofillTypeAsString(item.type)
                        + "(" + item.type + "), value=" + value);
            }
            boolean valid = false;
            if (value.isText() && item.type == AUTOFILL_TYPE_TEXT) {
                item.text = value.getTextValue();
                valid = true;
            } else if (value.isDate() && item.type == AUTOFILL_TYPE_DATE) {
                item.text = df.format(new Date(value.getDateValue()));
                valid = true;
            } else {
                Log.w(TAG, "Unsupported type: " + value);
            }
            if (!valid) {
                item.text = context.getString(R.string.message_autofill_invalid);
            }
        }
        postInvalidate();
        showMessage(context.getString(R.string.message_autofill_ok, partitions.valueAt(0)));
    }