Interface AccountSelector

Functional Interface:
This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.

@FunctionalInterface public interface AccountSelector
The object in charge of selecting an account. This interface must be implemented by the user of the SDK only when username-less out-of-band authentication is required with multiple accounts.
See Also:
  • Method Details

    • selectAccount

      void selectAccount(AccountSelectionContext context, AccountSelectionHandler handler)
      The account selection interaction. The implementing class must ask the user to choose one of the accounts exposed by the AccountSelectionContext and provide the choice to the AccountSelectionHandler.

      This method will be invoked in the main/UI thread.

      Basic implementation layout:

       import java.util.ArrayList;
       import java.util.List;
      
       [...]
      
           @Override
           public void selectAccount(AuthenticatorSelectionContext context, AuthenticatorSelectionHandler handler) {
               List<Authenticator> validAuthenticators = getValidAuthenticators(context);
               if (validAuthenticators.isEmpty()) {
                   // Display a message informing the user that there are no valid authenticators available and that we cannot
                   // proceed with  the operation. When the user discards the message, cancel the operation invoking:
                   //   handler.cancel();
               } else if (validAuthenticators.size() == 1) {
                   // There is just one valid authenticator available. We can decide simply to not interact with the user and
                   // return the available authenticator.
                   handler.aaid(validAuthenticators.get(0).aaid());
               } else {
                   // More than one authenticator available, display them to the user to pick one. If one is picked, provide
                   // it invoking:
                   //   Authenticator chosenAuthenticator = ...
                   //   handler.aaid(chosenAuthenticator.aaid());
      
                   // If the user cancels the operation, cancel it invoking:
                   // handler.cancel();
               }
           }
      
           private static List<Authenticator> getValidAuthenticators(AuthenticatorSelectionContext context) {
               List<Authenticator> validAuthenticators = new ArrayList<>();
               for (Authenticator authenticator : context.authenticators()) {
                   if (isValid(authenticator, context)) {
                       validAuthenticators.add(authenticator);
                   }
               }
               return validAuthenticators;
           }
      
           private static boolean isValid(Authenticator authenticator,
               AuthenticatorSelectionContext context) {
               String username = context.account().username();
               Registration registration = authenticator.registration();
               return registration.isRegistered(username) && authenticator.isSupportedByHardware() &&
                   context.isPolicyCompliant(authenticator.aaid());
           }
       
      Parameters:
      context - the object containing the list of existing authenticators
      handler - the object that must be notified with the selection result