AuthTabIntent
public class AuthTabIntent
Class holding an Intent and other data necessary to start an Auth Tab Activity.
Create an instance of this class using the Builder and then call launch to present the auth page to the user.
Upon completion of the auth flow, the webpage should redirect to a URL with the custom scheme or an HTTPS URL with the host and path provided to the launch method.
Before starting the auth flow, you should create an ActivityResultLauncher using registerActivityResultLauncher. This launcher should be created unconditionally before every fragment or activity creation. The ActivityResultCallback provided to the launcher will be called with the result of the authentication flow, indicating success or failure.
If using an HTTPS redirect URL, you need to establish that your app and the redirect URL are owned by the same organization using Digital Asset Links. If the verification fails, the Auth Tab will return an AuthResult with the result code RESULT_VERIFICATION_FAILED.
Code sample:
// In your activity private final ActivityResultLauncher<Intent> mLauncher = AuthTabIntent.registerActivityResultLauncher(this, this::handleAuthResult); private void handleAuthResult(AuthTabIntent.AuthResult result) { // Check the result code boolean success = result.resultCode == AuthTabIntent.RESULT_OK; String message = getResources() .getString(success ? R.string.auth_tab_success : R.string.auth_tab_failure); // Retrieve the result Uri message += " uri: " + result.resultUri; Toast.makeText(this, message, Toast.LENGTH_LONG).show(); } ... private void launchAuthTab() { AuthTabIntent authTabIntent = new AuthTabIntent.Builder().build(); authTabIntent.launch(mLauncher, Uri.parse("https://www.example.com/auth"), "myscheme"); } ...
Note: The constants below are public for the browser implementation's benefit. You are strongly encouraged to use AuthTabIntent.Builder.
Summary
Nested types |
|---|
public final class AuthTabIntent.AuthResultClass containing Auth Tab result data. |
public class AuthTabIntent.AuthenticateUserResultContract extends ActivityResultContractAn |
public final class AuthTabIntent.BuilderBuilder class for |
Constants |
|
|---|---|
static final String |
EXTRA_HTTPS_REDIRECT_HOST = "androidx.browser.auth.extra.HTTPS_REDIRECT_HOST"String extra that determines the host of the https redirect. |
static final String |
EXTRA_HTTPS_REDIRECT_PATH = "androidx.browser.auth.extra.HTTPS_REDIRECT_PATH"String extra that determines the path of the https redirect. |
static final String |
EXTRA_LAUNCH_AUTH_TAB = "androidx.browser.auth.extra.LAUNCH_AUTH_TAB"Boolean extra that triggers an Auth Tab launch. |
static final String |
EXTRA_REDIRECT_SCHEME = "androidx.browser.auth.extra.REDIRECT_SCHEME"String extra that determines the redirect scheme. |
static final int |
RESULT_CANCELED = 0Result code for when the Auth Tab is closed without the user completing the auth flow, e.g. the user clicked the close button. |
static final int |
RESULT_OK = -1Result code for when the Auth Tab is closed as a result of the expected redirect, implying the auth flow was completed. |
static final int |
RESULT_UNKNOWN_CODE = -2Result code for when the Auth Tab implementation returns an invalid or unknown result. |
static final int |
Result code for when the Auth Tab is closed because the verification of the ownership of the HTTPS redirect URL failed. |
static final int |
Result code for when the Auth Tab is closed because the verification of the ownership of the HTTPS redirect URL couldn't be completed in a reasonable amount of time. |
Public methods |
|
|---|---|
@Nullable Bitmap |
Returns the close button icon |
static @NonNull AuthTabColorSchemeParams |
getColorSchemeParams(Retrieves the instance of |
@Nullable AuthTabSession.PendingSession |
|
@Nullable AuthTabSession |
|
boolean |
Returns whether ephemeral browsing is enabled. |
void |
launch(Launches an Auth Tab Activity. |
void |
launch(Launches an Auth Tab Activity. |
static @NonNull ActivityResultLauncher<Intent> |
registerActivityResultLauncher(Registers a request to launch an Auth Tab and returns an |
Constants
EXTRA_HTTPS_REDIRECT_HOST
public static final String EXTRA_HTTPS_REDIRECT_HOST = "androidx.browser.auth.extra.HTTPS_REDIRECT_HOST"
String extra that determines the host of the https redirect.
EXTRA_HTTPS_REDIRECT_PATH
public static final String EXTRA_HTTPS_REDIRECT_PATH = "androidx.browser.auth.extra.HTTPS_REDIRECT_PATH"
String extra that determines the path of the https redirect.
EXTRA_LAUNCH_AUTH_TAB
public static final String EXTRA_LAUNCH_AUTH_TAB = "androidx.browser.auth.extra.LAUNCH_AUTH_TAB"
Boolean extra that triggers an Auth Tab launch.
EXTRA_REDIRECT_SCHEME
public static final String EXTRA_REDIRECT_SCHEME = "androidx.browser.auth.extra.REDIRECT_SCHEME"
String extra that determines the redirect scheme.
RESULT_CANCELED
public static final int RESULT_CANCELED = 0
Result code for when the Auth Tab is closed without the user completing the auth flow, e.g. the user clicked the close button.
RESULT_OK
public static final int RESULT_OK = -1
Result code for when the Auth Tab is closed as a result of the expected redirect, implying the auth flow was completed.
RESULT_UNKNOWN_CODE
public static final int RESULT_UNKNOWN_CODE = -2
Result code for when the Auth Tab implementation returns an invalid or unknown result.
RESULT_VERIFICATION_FAILED
public static final int RESULT_VERIFICATION_FAILED = 2
Result code for when the Auth Tab is closed because the verification of the ownership of the HTTPS redirect URL failed.
RESULT_VERIFICATION_TIMED_OUT
public static final int RESULT_VERIFICATION_TIMED_OUT = 3
Result code for when the Auth Tab is closed because the verification of the ownership of the HTTPS redirect URL couldn't be completed in a reasonable amount of time.
Public fields
Public methods
getCloseButtonIcon
public @Nullable Bitmap getCloseButtonIcon()
Returns the close button icon Bitmap.
getColorSchemeParams
public static @NonNull AuthTabColorSchemeParams getColorSchemeParams(
@NonNull Intent intent,
@IntRange(from = 1, to = 2) int colorScheme
)
Retrieves the instance of AuthTabColorSchemeParams from an Intent for a given color scheme.
| Parameters | |
|---|---|
@NonNull Intent intent |
|
@IntRange(from = 1, to = 2) int colorScheme |
A constant representing a color scheme. Must not be COLOR_SCHEME_SYSTEM. |
| Returns | |
|---|---|
@NonNull AuthTabColorSchemeParams |
An instance of |
getPendingSession
@ExperimentalPendingSession
public @Nullable AuthTabSession.PendingSession getPendingSession()
isEphemeralBrowsingEnabled
public boolean isEphemeralBrowsingEnabled()
Returns whether ephemeral browsing is enabled.
launch
public void launch(
@NonNull ActivityResultLauncher<Intent> launcher,
@NonNull Uri url,
@NonNull String redirectScheme
)
Launches an Auth Tab Activity. Must be used for flows that result in a redirect with a custom scheme.
| Parameters | |
|---|---|
@NonNull ActivityResultLauncher<Intent> launcher |
The |
@NonNull Uri url |
The url to load in the Auth Tab. |
@NonNull String redirectScheme |
The scheme of the resulting redirect. |
launch
public void launch(
@NonNull ActivityResultLauncher<Intent> launcher,
@NonNull Uri url,
@NonNull String redirectHost,
@NonNull String redirectPath
)
Launches an Auth Tab Activity. Must be used for flows that result in a redirect with the HTTPS scheme.
| Parameters | |
|---|---|
@NonNull ActivityResultLauncher<Intent> launcher |
The |
@NonNull Uri url |
The url to load in the Auth Tab. |
@NonNull String redirectHost |
The host portion of the resulting https redirect. |
@NonNull String redirectPath |
The path portion of the resulting https redirect. |
registerActivityResultLauncher
public static @NonNull ActivityResultLauncher<Intent> registerActivityResultLauncher(
@NonNull ActivityResultCaller caller,
@NonNull ActivityResultCallback<AuthTabIntent.AuthResult> callback
)
Registers a request to launch an Auth Tab and returns an ActivityResultLauncher that can be used to launch it. Should be called unconditionally before the fragment or activity is created.
| Parameters | |
|---|---|
@NonNull ActivityResultCaller caller |
An |
@NonNull ActivityResultCallback<AuthTabIntent.AuthResult> callback |
An |
| Returns | |
|---|---|
@NonNull ActivityResultLauncher<Intent> |
An |