# Secure Web Containers on Android
source: https://developer.mastercard.com/open-finance-us/documentation/connect/integrating/sdk/android/websdk-android/index.md

The following section explains the steps to load your WebApp via the Data Connect WebSDK in your mobile App utilizing Chrome Custom Tabs. The FI's OAuth login page will use the same Chrome Custom Tabs when the FI's app is not installed on the user's device, or it will redirect to the FI's mobile app if the app is present on the user's mobile device. This documentation also includes the App Link/Deep Link configuration.

These are the steps which occur when launching:

* Launch your web app (integrated with the Data Connect WebSDK) URL address in a secure web container inside your mobile app.

* Select an option or link which will open the Data Connect experience in same Secure Container (Note this option should have redirectUrl code integrated already)

* After successful FI OAuth account addition control returns to the your app's Activity via AppLink/DeepLink of the `redirectUrl` declared in the `AndroidManifest.xml` file.

The following scenarios need to be handled:

* Scenario 1: FI's app is installed - the FI's app will launch and the OAuth session will occur in the FI's app.

* Scenario 2: FI's app is not installed - the FI OAuth login page will open in Chrome Custom Tabs.

Note: In order to support App to App authentication make sure you pass a suitable `redirectUrl` parameter via the Data Connect Web SDK. See [Data Connect Web SDK App to App support](https://developer.mastercard.com/open-finance-us/documentation/connect/integrating/sdk/web-sdk/sdk/index.md#app-to-app-support)

## Android project setup {#android-project-setup}

Modify your app-level Gradle file (build.gradle) as follows:

```groovy
dependencies {
    // ...
    implementation 'androidx.browser:browser:<insert latest version>'
}
```

You can find the latest version of the library here: <https://developer.chrome.com/docs/android/custom-tabs/guide-get-started>

The Android App needs Internet permission to access the WebView. Include this permission in the `AndroidManifest.xml` file.

```xml
<?xml version="1.0" encoding="utf-8"?>
<manifest
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools">

  <uses-permission android:name="android.permission.INTERNET"/>

  <application>
    .....
  </application>

</manifest>
```

## Loading your Web URL inside the secure web container {#loading-your-web-url-inside-the-secure-web-container}

To open Chrome Custom Tabs, use the `openLinkInChromeCustomTabs` function. This function takes the String URL as a parameter and relies on a context instance typically representing your `Activity`.
* Java
* Kotlin

```java
// pass your web url here for example "https://example.com"
private void openLinkInChromeCustomTabs(String url) {
  try {
    // To open Chrome Custom Tabs
    CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
    CustomTabsIntent customTabsIntent = builder.build();
    customTabsIntent.intent.setPackage("com.android.chrome");
    customTabsIntent.launchUrl(context, Uri.parse(url));
  } catch (Exception exception) {
    Log.d("Connect", "Something went wrong, please check if Chrome Browser is installed");
  }
}
```

```kotlin
// pass your web url here for example "https://example.com"
fun openLinkInChromeCustomTabs(url: String) {
  try {
    // To open Chrome Custom Tabs
    val builder = CustomTabsIntent.Builder()
    val customTabsIntent = builder.build()
    customTabsIntent.intent.setPackage("com.android.chrome")
    customTabsIntent.launchUrl(this, Uri.parse(url))
  } catch (ignore:Exception) {
    Log.d("Connect","Something went wrong, please check if Chrome Browser is installed")
  }
}
```

## Handling OAuth Login URL and App to App {#handling-oauth-login-url-and-app-to-app}

No extra code is required to handle OAuth events. Chrome Custom Tabs will handle the events automatically. It does this by opening another page in the same Chrome Custom Tab when the financial institution's app is not present, or it will open the app if present on the user's device.

When the FI's OAuth process is completed by the user, the FI page in the Chrome Custom Tabs will automatically close itself as per the Activity configured via AppLink or DeepLink which is mentioned in the `AndroidManifest.xml` file (see [here](https://developer.mastercard.com/open-finance-us/documentation/connect/integrating/sdk/android/android-sdk/index.md#app-link-support)). Control then returns to your app's Activity which runs Data Connect in the background with an add accounts screen.
Note: To maintain the Chrome Custom Tabs session when redirecting back to your app, it is necessary to create an additional Activity and listen for your AppLinks / DeepLinks in that Activity. This ensures a seamless user experience, preventing any disruption in the flow of the application and the closure of the Chrome Custom Tabs.

Create an Activity named `ConnectRedirectActivity` and finish the Activity in its `onCreate` method to handle redirection back to the original Activity from which you initially launched the Chrome Custom Tabs.
* Java
* Kotlin

```java
public class ConnectRedirectActivity extends Activity {
  @Override
  protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    finish();
  }
}
```

```kotlin
class ConnectRedirectActivity : Activity() {
  override fun onCreate(savedInstanceState: Bundle?, persistentState: PersistableBundle?) {
    super.onCreate(savedInstanceState, persistentState)
    finish()
  }
}
```

Listen to your AppLinks / DeepLinks to the `ConnectRedirectActivity` in the `AndroidManifest.xml` file.

```xml
<?xml version="1.0" encoding="utf-8"?>
<manifest
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">

    <uses-permission android:name="android.permission.INTERNET"/>

    <application>
           <!-- ... Other activities and configurations ... -->

           <activity
            android:name=".ConnectRedirectActivity"
            android:exported="true"
            android:launchMode="singleTask">

            <intent-filter android:autoVerify="true">
                <action android:name="android.intent.action.VIEW" />

                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />

                  <data
                    android:scheme="https"
                    android:host="{{example.com}}"/>

            </intent-filter>

        </activity>
    </application>

</manifest>
```

