# Non-SDK Integration Guide for Android
source: https://developer.mastercard.com/open-finance-us/documentation/connect/integrating/non-sdk/non-sdk-android/index.md

This page outlines the non-SDK solution and steps needed to load the Data Connect URL in your Android Mobile App (either inside a WebView or Secure Container).

This documentation also includes Universal Link and deep link configuration, as well as details on customizing the `WebViewClient` and secure container (Custom Chrome Tabs) for enhanced control and handling of web interactions.

These are the steps which occur when launching:

1. Launch your [Data Connect URL](https://developer.mastercard.com/open-finance-us/documentation/connect/generate-2-connect-url-apis/index.md) (generated with `isHostedInMobileApp` flag set as true and with the `redirectUri` provided) --- either in a web view or secure web container inside your mobile app.

2. After successful FI OAuth account addition, control returns to your app where you dismiss the already open secure container or web view.

Secure containers are mandatory in some situations (for example, Chase does not allow their consent URL to be launched from within insecure webviews).

Below we provide details of the following:

* **Implementation A** --- Data Connect URL launched in Web View, FI OAuth session opened in Secure Container (Chrome Custom Tabs) as we don't support WebViews for OAuth process.

  * Scenario 1: FI's app is installed --- the FI 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 new a Chrome Custom Tab as a pop up.

* **Implementation B** --- Data Connect URL launched in new separate Secure Container (Chrome Custom Tabs) and where no WebViews are involved.

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

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

Tip: In order to support [App To App Authentication](https://developer.mastercard.com/open-finance-us/documentation/connect/integrating/index.md#app-to-app-authentication) make sure you pass a suitable `redirectUri` parameter in your Generating Data Connect URL call.

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

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

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

Find the latest version here - <https://developer.chrome.com/docs/android/custom-tabs/guide-get-startedopens> in a new tab

The Android App needs the `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>
```

For further details on app to app setup see here.

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=".DefaultRedirectActivityAndroid"
            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>
```

## Implementation A (Data Connect URL in WebView) {#implementation-a-data-connect-url-in-webview}

In this implementation, the Data Connect URL (which must be generated with `isHostedInMobileApp` flag as true and `redirectUri` set) is launched in a WebView, then a secure container (Chrome Custom Tab) used for the OAuth popup.

### WebView Configurations {#webview-configurations}

Include the WebView component in your layout XML file.

```xml
<WebView
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  xmlns:tools="http://schemas.android.com/tools"
  android:id="@+id/webView"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
/>
```

### WebView Initialization {#webview-initialization}

Create an Activity and create the following variables globally in your Activity.

```java
private WebView webView;
private Context context;
```

Initialize `webView` and `context` in your Activity's `onCreate` using the following code. Create the `ConnectWebViewClient` and `OauthWebChromeClient` class within the same Activity.

```java
context = this;
webView = findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setAllowFileAccess(true);
webView.getSettings().setSupportMultipleWindows(true);
webView.setWebViewClient(new ConnectWebViewClient());
webView.setWebChromeClient(new OauthWebChromeClient());
webView.loadUrl("Pass Your Data Connect WebSDK integrated URL");
```

### Loading your Data Connect URL inside the WebView {#loading-your-data-connect-url-inside-the-webview}

The `ConnectWebViewClient` class extends `WebViewClient` and handles URL loading in the main WebView.

Customize the `shouldOverrideUrlLoading` method within `ConnectWebViewClient` to control how URLs are loaded in the main WebView.

```java
// this class handles the loading of the urls inside the WebView.
private class ConnectWebViewClient extends WebViewClient {
  @Override public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
    String url = request.getUrl().toString();
    handleConnect(view,request.getUrl());
    return true;
  }
}
```

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

In order for App to App authentication to work, the `OauthWebChromeClient` class extends `WebChromeClient` and manages the creation of a new WebView for handling Financial Institution OAuth events.

Customize the shouldOverrideUrlLoading method within `OauthWebChromeClient` to handle specific OAuth URLs and load them into the Chrome Custom Tabs.

When the FI's OAuth process is completed by the user, the FI page in the Chrome Custom Tab will automatically close itself as per the Activity configured via AppLink or DeepLink which is mentioned in the 'AndroidManifest.xml' file (see here). Control then returns to your app's Activity which runs Data Connect in the background with an add accounts screen.

```java
// handle the opening of the OAuth Login page OauthWebChromeClient

private class OauthWebChromeClient extends WebChromeClient {
  @Override public boolean onCreateWindow(WebView view, boolean isDialog, boolean isUserGesture, Message resultMsg) {
    Webview mWebviewPop = new WebView(context);
    WebView.WebViewTransport transport = (WebView.WebViewTransport) resultMsg.obj;
    transport.setWebView(mWebviewPop);
    resultMsg.sendToTarget();
  }
}
```

### Dismiss web view {#dismiss-web-view}

```java
// handle and dismiss the webview

private void handleConnect(WebView view, Uri uri){
    try {
        String reason = uri.getQueryParameter("reason");
        if( uri.toString().equals("https://example.com") )
        {
          // https://example.com is your app link
          view.loadUrl(connectURLGlobal);
        }
        else if(reason != null){
            switch (reason){
                case "timeout":
                    Log.d("Connect","Connect Flow Timeout");
                    view.destroy();
                    finish();
                    break;
                case "complete":
                    Log.d("Connect","Connect Flow Completed Successfully");
                    view.destroy();
                    finish();
                    break;
                case "exit":
                    Log.d("Connect","User Cancelled");
                    view.destroy();
                    finish();
                    break;
        case "error":
                    Log.d("Connect","Some Error Occurred");
                    finish();
                    break;
                default:
            }
        }
        else {
            view.loadUrl(uri.toString());
        }
        catch (Exception ignored){
          Log.d("Connect", "Some Error Occurred");
        }
    }
}
```

## Implementation B (Data Connect in Secure Container) {#implementation-b-data-connect-in-secure-container}

In this implementation, the Data Connect URL (which must be generated with `isHostedInMobileApp` flag as true and `redirectUri` set) is launched a secure container (Chrome Custom Tab) and no WebView is used.

### 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
// 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");
  }
}
```

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

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 FI 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). Control then returns to your app's Activity which runs Data Connect in the background with select or review account screen.
Tip: 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
public class DefaultRedirectActivityAndroid extends Activity {
  @Override
  protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
     Intent intent = new Intent(DefaultRedirectActivityAndroid.this, ConnectRedirectActivity.class);
     intent.setData(getIntent().getData());
     startActivity(intent);
  }
}
```

Reload Custom chrome tabs according to response from Applink query parameters:

```java
public class ConnectRedirectActivity extends AppCompatActivity {
  protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    webView.loadUrl(connectURL);

    if (intent.getData().getQueryParameter("reason") == null) {
      isCCTOpen = true;
      CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
      CustomTabsIntent intent1 = builder.build();
      intent1.launchUrl(WebviewActivityAllScenarios.this, Uri.parse(connectURL));
    }
  }
}
```

