Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit a829e98d authored by Simranjit Kohli's avatar Simranjit Kohli Committed by Android (Google) Code Review
Browse files

Merge "[Autofill PCC][Save]: Fix Save for PCC If the provider doesn't set...

Merge "[Autofill PCC][Save]: Fix Save for PCC If the provider doesn't set proper SaveInfo, update it so that data can be saved. This achieves 2 purposes. 1. It unblocks testing of the Save flow, and allows to dogfood the feature. 2. It's needed in cases PCC types are selected." into udc-dev
parents 583829fc 030307c8
Loading
Loading
Loading
Loading
+3 −2
Original line number Diff line number Diff line
@@ -122,10 +122,11 @@ public final class FillResponse implements Parcelable {
    *
    * @hide
    */
    public static FillResponse shallowCopy(FillResponse r, List<Dataset> datasets) {
    public static FillResponse shallowCopy(
            FillResponse r, List<Dataset> datasets, SaveInfo saveInfo) {
        return new FillResponse(
                (datasets != null) ? new ParceledListSlice<>(datasets) : null,
                r.mSaveInfo,
                saveInfo,
                r.mClientState,
                r.mPresentation,
                r.mInlinePresentation,
+33 −0
Original line number Diff line number Diff line
@@ -334,6 +334,39 @@ public final class SaveInfo implements Parcelable {
    private final AutofillId[][] mSanitizerValues;
    private final AutofillId mTriggerId;

    /**
     * Creates a copy of the provided SaveInfo.
     *
     * @hide
     */
    public static SaveInfo copy(SaveInfo s, AutofillId[] optionalIds) {
        return new SaveInfo(s.mType, s.mNegativeButtonStyle, s.mPositiveButtonStyle,
                s.mNegativeActionListener, s.mRequiredIds, assertValid(optionalIds), s.mDescription,
                s.mFlags, s.mCustomDescription, s.mValidator, s.mSanitizerKeys, s.mSanitizerValues,
                s.mTriggerId);
    }

    private SaveInfo(@SaveDataType int type, @NegativeButtonStyle int negativeButtonStyle,
            @PositiveButtonStyle int positiveButtonStyle, IntentSender negativeActionListener,
            AutofillId[] requiredIds, AutofillId[] optionalIds, CharSequence description, int flags,
            CustomDescription customDescription, InternalValidator validator,
            InternalSanitizer[] sanitizerKeys, AutofillId[][] sanitizerValues,
            AutofillId triggerId) {
        mType = type;
        mNegativeButtonStyle = negativeButtonStyle;
        mNegativeActionListener = negativeActionListener;
        mPositiveButtonStyle = positiveButtonStyle;
        mRequiredIds = requiredIds;
        mOptionalIds = optionalIds;
        mDescription = description;
        mFlags = flags;
        mCustomDescription = customDescription;
        mValidator = validator;
        mSanitizerKeys = sanitizerKeys;
        mSanitizerValues = sanitizerValues;
        mTriggerId = triggerId;
    }

    private SaveInfo(Builder builder) {
        mType = builder.mType;
        mNegativeButtonStyle = builder.mNegativeButtonStyle;
+130 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2023 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.server.autofill;

import static android.service.autofill.SaveInfo.SAVE_DATA_TYPE_ADDRESS;
import static android.service.autofill.SaveInfo.SAVE_DATA_TYPE_CREDIT_CARD;
import static android.service.autofill.SaveInfo.SAVE_DATA_TYPE_DEBIT_CARD;
import static android.service.autofill.SaveInfo.SAVE_DATA_TYPE_EMAIL_ADDRESS;
import static android.service.autofill.SaveInfo.SAVE_DATA_TYPE_GENERIC_CARD;
import static android.service.autofill.SaveInfo.SAVE_DATA_TYPE_PASSWORD;
import static android.service.autofill.SaveInfo.SAVE_DATA_TYPE_PAYMENT_CARD;
import static android.service.autofill.SaveInfo.SAVE_DATA_TYPE_USERNAME;

import android.util.ArraySet;

import java.util.Set;

/**
 * Helper class to manage autofill hints.
 * Provides utility methods like converting SaveTypes to applicable HintsConstants.
 */
public class HintsHelper {
    // Username fields
    public static final String AUTOFILL_HINT_NEW_USERNAME = "newUsername";
    public static final String AUTOFILL_HINT_USERNAME = "username";

    // Password fields
    public static final String AUTOFILL_HINT_NEW_PASSWORD = "newPassword";
    public static final String AUTOFILL_HINT_PASSWORD = "password";

    // Email hints
    public static final String AUTOFILL_HINT_EMAIL_ADDRESS = "emailAddress";

    // Phone number hints
    public static final String AUTOFILL_HINT_PHONE_COUNTRY_CODE = "phoneCountryCode";
    public static final String AUTOFILL_HINT_PHONE = "phone";
    public static final String AUTOFILL_HINT_PHONE_NATIONAL = "phoneNational";
    public static final String AUTOFILL_HINT_PHONE_NUMBER = "phoneNumber";
    public static final String AUTOFILL_HINT_PHONE_NUMBER_DEVICE = "phoneNumberDevice";

    // Credit card hints
    public static final String AUTOFILL_HINT_CREDIT_CARD_EXPIRATION_DATE =
            "creditCardExpirationDate";
    public static final String AUTOFILL_HINT_CREDIT_CARD_EXPIRATION_DAY = "creditCardExpirationDay";
    public static final String AUTOFILL_HINT_CREDIT_CARD_EXPIRATION_MONTH =
            "creditCardExpirationMonth";
    public static final String AUTOFILL_HINT_CREDIT_CARD_EXPIRATION_YEAR =
            "creditCardExpirationYear";
    public static final String AUTOFILL_HINT_CREDIT_CARD_NUMBER = "creditCardNumber";
    public static final String AUTOFILL_HINT_CREDIT_CARD_SECURITY_CODE = "creditCardSecurityCode";

    // Address hints
    public static final String AUTOFILL_HINT_POSTAL_ADDRESS = "postalAddress";
    public static final String AUTOFILL_HINT_POSTAL_ADDRESS_APT_NUMBER = "aptNumber";
    public static final String AUTOFILL_HINT_POSTAL_ADDRESS_COUNTRY = "addressCountry";
    public static final String AUTOFILL_HINT_POSTAL_ADDRESS_DEPENDENT_LOCALITY =
            "dependentLocality";
    public static final String AUTOFILL_HINT_POSTAL_ADDRESS_EXTENDED_ADDRESS = "extendedAddress";
    public static final String AUTOFILL_HINT_POSTAL_ADDRESS_EXTENDED_POSTAL_CODE =
            "extendedPostalCode";
    public static final String AUTOFILL_HINT_POSTAL_ADDRESS_LOCALITY = "addressLocality";
    public static final String AUTOFILL_HINT_POSTAL_ADDRESS_REGION = "addressRegion";
    public static final String AUTOFILL_HINT_POSTAL_ADDRESS_STREET_ADDRESS = "streetAddress";
    public static final String AUTOFILL_HINT_POSTAL_CODE = "postalCode";

    private HintsHelper() {}

    /**
     * Converts saveType to Autofill HintsConstants.
     * @param saveType
     * @return
     */
    public static Set<String> getHintsForSaveType(int saveType) {
        ArraySet<String> hintSet = new ArraySet<>();
        switch (saveType) {
            case SAVE_DATA_TYPE_PASSWORD:
                hintSet.add(AUTOFILL_HINT_NEW_USERNAME);
                hintSet.add(AUTOFILL_HINT_USERNAME);
                hintSet.add(AUTOFILL_HINT_NEW_PASSWORD);
                hintSet.add(AUTOFILL_HINT_PASSWORD);
                return hintSet;
            case SAVE_DATA_TYPE_USERNAME:
                hintSet.add(AUTOFILL_HINT_NEW_USERNAME);
                hintSet.add(AUTOFILL_HINT_USERNAME);
                return hintSet;
            case SAVE_DATA_TYPE_EMAIL_ADDRESS:
                hintSet.add(AUTOFILL_HINT_EMAIL_ADDRESS);
                return hintSet;
            case SAVE_DATA_TYPE_CREDIT_CARD:
            case SAVE_DATA_TYPE_DEBIT_CARD:
            case SAVE_DATA_TYPE_PAYMENT_CARD:
            case SAVE_DATA_TYPE_GENERIC_CARD:
                hintSet.add(AUTOFILL_HINT_CREDIT_CARD_EXPIRATION_DATE);
                hintSet.add(AUTOFILL_HINT_CREDIT_CARD_EXPIRATION_DAY);
                hintSet.add(AUTOFILL_HINT_CREDIT_CARD_EXPIRATION_MONTH);
                hintSet.add(AUTOFILL_HINT_CREDIT_CARD_EXPIRATION_YEAR);
                hintSet.add(AUTOFILL_HINT_CREDIT_CARD_NUMBER);
                hintSet.add(AUTOFILL_HINT_CREDIT_CARD_SECURITY_CODE);
                return hintSet;
            case SAVE_DATA_TYPE_ADDRESS:
                hintSet.add(AUTOFILL_HINT_POSTAL_ADDRESS);
                hintSet.add(AUTOFILL_HINT_POSTAL_ADDRESS_APT_NUMBER);
                hintSet.add(AUTOFILL_HINT_POSTAL_ADDRESS_COUNTRY);
                hintSet.add(AUTOFILL_HINT_POSTAL_ADDRESS_DEPENDENT_LOCALITY);
                hintSet.add(AUTOFILL_HINT_POSTAL_ADDRESS_EXTENDED_ADDRESS);
                hintSet.add(AUTOFILL_HINT_POSTAL_ADDRESS_EXTENDED_POSTAL_CODE);
                hintSet.add(AUTOFILL_HINT_POSTAL_ADDRESS_LOCALITY);
                hintSet.add(AUTOFILL_HINT_POSTAL_ADDRESS_REGION);
                hintSet.add(AUTOFILL_HINT_POSTAL_ADDRESS_STREET_ADDRESS);
                hintSet.add(AUTOFILL_HINT_POSTAL_CODE);
                return hintSet;
            default:
                return hintSet;
        }
    }
}
+42 −3
Original line number Diff line number Diff line
@@ -158,6 +158,7 @@ import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
@@ -1640,12 +1641,50 @@ final class Session implements RemoteFillService.FillServiceCallbacks, ViewState
        }
        // Create FillResponse with effectiveDatasets, and all the rest value from the original
        // response.
        return FillResponse.shallowCopy(response, new ArrayList<>(resultContainer.mDatasets));
        return createShallowCopy(response, resultContainer);
    }

    private static FillResponse createShallowCopy(
    private FillResponse createShallowCopy(
            FillResponse response, DatasetComputationContainer container) {
        return FillResponse.shallowCopy(response, new ArrayList<>(container.mDatasets));
        return FillResponse.shallowCopy(
                response,
                new ArrayList<>(container.mDatasets),
                getEligibleSaveInfo(response));
    }

    private SaveInfo getEligibleSaveInfo(FillResponse response) {
        SaveInfo saveInfo = response.getSaveInfo();
        if (saveInfo == null || (!ArrayUtils.isEmpty(saveInfo.getOptionalIds())
                || !ArrayUtils.isEmpty(saveInfo.getRequiredIds())
                || (saveInfo.getFlags() & SaveInfo.FLAG_DELAY_SAVE) != 0)) {
            return saveInfo;
        }
        synchronized (mLock) {
            ArrayMap<String, Set<AutofillId>> hintsToAutofillIdMap =
                    mClassificationState.mHintsToAutofillIdMap;
            if (hintsToAutofillIdMap == null || hintsToAutofillIdMap.isEmpty()) {
                return saveInfo;
            }

            ArraySet<AutofillId> ids = new ArraySet<>();
            int saveType = saveInfo.getType();
            if (saveType == SaveInfo.SAVE_DATA_TYPE_GENERIC) {
                for (Set<AutofillId> autofillIds: hintsToAutofillIdMap.values()) {
                    ids.addAll(autofillIds);
                }
            } else {
                Set<String> hints = HintsHelper.getHintsForSaveType(saveType);
                for (Map.Entry<String, Set<AutofillId>> entry: hintsToAutofillIdMap.entrySet()) {
                    String hint = entry.getKey();
                    if (hints.contains(hint)) {
                        ids.addAll(entry.getValue());
                    }
                }
            }
            AutofillId[] autofillIds = new AutofillId[ids.size()];
            ids.toArray(autofillIds);
            return SaveInfo.copy(saveInfo, autofillIds);
        }
    }

    /**