# Android Implementation
source: https://developer.mastercard.com/mastercard-checkout-solutions/tutorial/scof_passkey_mobile_integration/android/index.md

The mobile app must launch the **Authentication UI URL** using an Android **Custom Tab** when the consumer initiates checkout within their Android app

The following representation shows the consumer's Passkey authentication during checkout on merchant's mobile app in their Android device.

![Chrome Custom Tab Sample Code](https://static.developer.mastercard.com/content/mastercard-checkout-solutions/documentation/images/android2.png "Chrome Custom Tab session")

Follow the sequence outlined in [Create Passkey](https://developer.mastercard.com/mastercard-checkout-solutions/documentation/token-authentication/secure-card-on-file/by-mastercard/authentication-with-passkey/create-passkey/index.md) on the Mastercard Checkout Solutions
documentation. After receiving the **Authentication URL** from Mastercard Checkout Solutions, ensure that your mobile app launches this URL within an Android Custom Tab.

Mastercard recommends that customers refer to the official Android documentation for guidance on implementing Custom Tab launches, as well as configuring the callback and close handling.

![Chrome Custom Tab Mermaid Diagram](https://static.developer.mastercard.com/content/mastercard-checkout-solutions/documentation/images/mermaid_android.png "Chrome Custom Tab session Mermaid")

**Pre-requisites**

* You must have a native Android application.
* Ensure that the `androidx.browser` dependency is included in your app's **build.gradle** file.For more information, refer to the official [AndroidX Browser release notes](https://developer.android.com/jetpack/androidx/releases/browser).

## Step 1: Create the Custom Tab Intent {#step-1--create-the-custom-tab-intent}

This function builds a **CustomTabsIntent** configured to launch as a bottom sheet of a specific height.
* Kotlin

```kotlin
fun getCustomTabsIntent() : CustomTabsIntent {

    // Provide customization if required by the application.
    return CustomTabsIntent.Builder()
        // Hide/Disable unnecessary browser features
        .setDownloadButtonEnabled(false)
        .setBookmarksButtonEnabled(false)
        .setShareState(CustomTabsIntent.SHARE_STATE_OFF)
        .setInstantAppsEnabled(false)
        .setBackgroundInteractionEnabled(false)
        // Set the initial height of the Custom Tab to appear as a bottom sheet
        .setInitialActivityHeightPx(heightInPx, CustomTabsIntent.ACTIVITY_HEIGHT_FIXED)
        .build()
}
```

More details about this component can be found in the Android [CustomTabsIntent](https://developer.android.com/reference/androidx/browser/customtabs/CustomTabsIntent) reference.

## Step 2: Launch Custom Tab {#step-2-launch-custom-tab}

With the **CustomTabsIntent** builder ready, you can launch the URL from either the traditional View system or Jetpack Compose.

### 2.1 : Launching from Views (Activity / Fragment) {#21--launching-from-views-activity--fragment}

Within an Activity or Fragment, call this method to open the specified URL.It leverages the `getCustomTabsIntent()` helper function to apply your defined customizations.
* Kotlin

```kotlin
/**
 * Launches a URL in a Chrome Custom Tab from an Activity or Fragment.
 */
fun launchChromeCustomTabs(url: String) {
  val qualifiedUrl = Uri.parse(url)

  // Invoke the Custom Tabs intent to open the URL in a Chrome Custom Tab
  getCustomTabsIntent().launchUrl(context, qualifiedUrl)
}
```

<br />

### 2.2 : Launching from Compose {#22--launching-from-compose}

This function is a **Composable** that can be invoked within your Jetpack Compose UI to launch the Custom Tab.
* Kotlin

```kotlin
/**
 * Launches a URL in a Chrome Custom Tab from an Activity or Fragment.
 */
fun launchChromeCustomTabs(url: String) {
  val qualifiedUrl = Uri.parse(url)

  // Invoke the Custom Tabs intent to open the URL in a Chrome Custom Tab
  getCustomTabsIntent().launchUrl(context, qualifiedUrl)
}
```

<br />

For more advanced implementations, consider using a **wrapper class** to encapsulate the Custom Tab logic.  

This approach offers a unified interface for launching the tab and managing callbacks, helping to prevent potential lifecycle conflicts between Activities and Composables.

## Step 3: Returning to your App (Callback) {#step-3-returning-to-your-app-callback}

To return to your app from a Custom Tab, the web page must trigger a navigation to an [Android Deeplink](https://developer.android.com/training/app-links/create-deeplinks#types-deep) that your app is configured to handle.

Common approaches include:

* [App Links](https://developer.android.com/training/app-links/about) : Standard `http://` or `https://` links that resolve directly to your app.
* **Custom Schemes** : Unique URIs such as `myapp://callback`.
* [Chrome Intents](https://developer.chrome.com/docs/android/intents) : A specific `intent://` format.

Note:   

* Chrome Intents may not be supported by non-Chromium-based browsers such as Firefox.  
* Regardless of the method used, it's critical to follow [Android's security guidelines for Deeplinks](https://developer.android.com/privacy-and-security/risks/unsafe-use-of-deeplinks) to prevent potential vulnerabilities.
