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

Commit c44d59d7 authored by Brad Lassey's avatar Brad Lassey
Browse files

Update javadoc to make it clear that polling loop filter needs at least

one byte and the implementation to throw if the filter isn't valid.

Bug: b/321265135
Test: tested with CTS
Change-Id: I3844cbfdcb7110e382b1cdeb7c525d7ae258c85e
parent 07054da8
Loading
Loading
Loading
Loading
+15 −12
Original line number Diff line number Diff line
@@ -42,6 +42,7 @@ import android.provider.Settings.SettingNotFoundException;
import android.util.Log;

import java.util.HashMap;
import java.util.HexFormat;
import java.util.List;
import java.util.regex.Pattern;

@@ -59,7 +60,6 @@ import java.util.regex.Pattern;
 */
public final class CardEmulation {
    private static final Pattern AID_PATTERN = Pattern.compile("[0-9A-Fa-f]{10,32}\\*?\\#?");
    private static final Pattern PLF_PATTERN = Pattern.compile("[0-9A-Fa-f]{1,32}");

    static final String TAG = "CardEmulation";

@@ -361,19 +361,23 @@ public final class CardEmulation {

    /**
     * Register a polling loop filter (PLF) for a HostApduService and indicate whether it should
     * auto-transact or not.  When non-standard polling loop frame matches this sequence exactly,
     *  it may be delivered to {@link HostApduService#processPollingFrames(List)}. If auto-transact
     * is set to true, then observe mode will also be disabled.  if this service is currently
     * preferred or there are no other services registered for this filter.
     * auto-transact or not.  The PLF can be sequence of an
     * even number of at least 2 hexadecimal numbers (0-9, A-F or a-f), representing a series of
     * bytes. When non-standard polling loop frame matches this sequence exactly, it may be
     * delivered to {@link HostApduService#processPollingFrames(List)}. If auto-transact is set to
     * true, then observe mode will also be disabled.  if this service is currently preferred or
     * there are no other services registered for this filter.
     * @param service The HostApduService to register the filter for
     * @param pollingLoopFilter The filter to register
     * @param autoTransact true to have the NFC stack automatically disable observe mode and allow
     *         transactions to proceed when this filter matches, false otherwise
     * @return true if the filter was registered, false otherwise
     * @throws IllegalArgumentException if the passed in string doesn't parse to at least one byte
     */
    @FlaggedApi(Flags.FLAG_NFC_READ_POLLING_LOOP)
    public boolean registerPollingLoopFilterForService(@NonNull ComponentName service,
            @NonNull String pollingLoopFilter, boolean autoTransact) {
        pollingLoopFilter = validatePollingLoopFilter(pollingLoopFilter);

        try {
            return sService.registerPollingLoopFilterForService(mContext.getUser().getIdentifier(),
@@ -983,15 +987,14 @@ public final class CardEmulation {
     * @hide
     */
    @FlaggedApi(Flags.FLAG_NFC_READ_POLLING_LOOP)
    public static boolean isValidPollingLoopFilter(@NonNull String pollingLoopFilter) {
    public static @NonNull String validatePollingLoopFilter(@NonNull String pollingLoopFilter) {
        // Verify hex characters
        if (!PLF_PATTERN.matcher(pollingLoopFilter).matches()) {
            Log.e(TAG, "Polling Loop Filter " + pollingLoopFilter
                    + " is not a valid Polling Loop Filter.");
            return false;
        byte[] plfBytes = HexFormat.of().parseHex(pollingLoopFilter);
        if (plfBytes.length == 0) {
            throw new IllegalArgumentException(
                "Polling loop filter must contain at least one byte.");
        }

        return true;
        return HexFormat.of().withUpperCase().formatHex(plfBytes);
    }

    /**