> For the complete documentation index, see [llms.txt](https://devcenter.unico.io/unico-idcloud/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://devcenter.unico.io/unico-idcloud/by-unico-integration/sdk-redirect/app/android/android-sdk/usage-and-integration-guide.md).

# Usage and Integration Guide

## Initializing the SDK

Create an instance of the builder (generated through the **`IAcessoBioBuilder`** interface), providing the context and environment as parameters, along with the implementation of the **`AcessoBioListener`** class.

The implementation of this class is quite simple and can be done in just a few lines of code. All you need to do is instantiate the **builder** by providing the relevant context and override the **callback** methods with the business logic of your application:

{% tabs %}
{% tab title="Kotlin" %}

```kotlin
internal class MainActivity : AppCompatActivity() {

    private val acessoBioListener = object : AcessoBioListener {
        override fun onErrorAcessoBio(errorBio: ErrorBio?) { }
    
        override fun onUserClosedCameraManually() { }
    
        override fun onSystemClosedCameraTimeoutSession() { }
    
        override fun onSystemChangedTypeCameraTimeoutFaceInference() { }
    }

    private val acessoBioBuilder: IAcessoBioBuilder = AcessoBio(this, acessoBioListener)
}
```

{% endtab %}

{% tab title="Java" %}

```java
public class MainActivity extends AppCompatActivity {

    private AcessoBioListener acessoBioListener = new AcessoBioListener() {
        @Override
        public void onErrorAcessoBio(ErrorBio errorBio) { }

        @Override
        public void onUserClosedCameraManually() { }

        @Override
        public void onSystemClosedCameraTimeoutSession() { }

        @Override
        public void onSystemChangedTypeCameraTimeoutFaceInference() { }
    };

    private IAcessoBioBuilder acessoBioBuilder = new AcessoBio(this, acessoBioListener);
}
```

{% endtab %}
{% endtabs %}

## Environment configuration

Configure the environment that will be used during the SDK execution. Use the **`Environment`** enum, which contains the following options:

* `Environment.PROD`: for production environment;
* `Environment.UAT`: for approval environment.

See how to implement it in the example below:

{% tabs %}
{% tab title="Kotlin" %}

```kotlin
acessoBioBuilder.setEnvironment(Environment.UAT)
```

{% endtab %}

{% tab title="Java" %}

```java
acessoBioBuilder.setEnvironment(Environment.UAT);
```

{% endtab %}
{% endtabs %}

## Implementing Callback Functions

Note that the work of implementing the **`AcessoBioListener`** class is, for the most part, configuring the callback methods. Each method is called in a specific situation based on the SDK's return.

Simply override the methods shown in the previous step with your application's business logic.

This method is invoked whenever any implementation error occurs while using one of our methods:

{% stepper %}
{% step %}
**`onErrorAcessoBio(ErrorBio errorBio)`**

When invoked, the method receives a parameter of type **ErrorBio** that contains details about the error. Learn more about the **`ErrorBio`** type in the error handling section.

{% endstep %}

{% step %}
**`onUserClosedCameraManually()`**

This method is invoked whenever the user manually closes the camera, such as when clicking the "Back" button.

{% endstep %}

{% step %}
**`onSystemClosedCameraTimeoutSession()`**

This method is invoked as soon as the maximum session time is reached (without capturing any image).

{% hint style="warning" %}
It can be configured in the **builder** through the **setTimeoutSession** method. This method should receive the maximum session time in **seconds**. You can change the maximum session time for your user when using the face detection feature **(Selfie camera with smart capture)**. If the user exceeds the time set for capturing the photo, you can display a customizable message or instruction to the user. The default value is **40 seconds**, and the minimum **value is also 40 seconds.**
{% endhint %}

{% endstep %}

{% step %}
**`onSystemChangedTypeCameraTimeoutFaceInference()`**

This method is invoked as soon as the maximum time for face detection is reached (without detecting anything). In this case, the camera mode is automatically switched to manual capture mode (without the smart capture outline).

{% hint style="warning" %}
The maximum capture time when using face detection (Selfie camera with smart capture) is **13 seconds**. If the user encounters difficulty capturing the photo through face detection and exceeds the time set in the process, the capture is automatically switched to manual mode, aiming to make the action easier for the user (**TimeoutToFaceInference**).
{% endhint %}

{% endstep %}
{% endstepper %}

{% hint style="danger" %}
All of the above methods must be created as indicated in your project (even if without any logic). Otherwise, the project will not compile successfully.
{% endhint %}

{% tabs %}
{% tab title="Kotlin" %}

```kotlin
val unicoCheckCamera: UnicoCheckCamera = acessoBioBuilder
    .setAutoCapture(true)
    .setSmartFrame(true)
    .build()
```

{% endtab %}

{% tab title="Java" %}

```java
UnicoCheckCamera unicoCheckCamera = acessoBioBuilder
    .setAutoCapture(true)
    .setSmartFrame(true)
    .build();
```

{% endtab %}
{% endtabs %}

## Implementing Listeners for Camera Events

{% hint style="info" %}
The implementation of these listener methods must be done through an instance of the **`iAcessoBioSelfie`** class.
{% endhint %}

The camera opening method, which is called in the next step, needs to know what to do when it **successfully** captures an image or when an error occurs in the process. It is necessary to inform **"what to do"** to the camera opening method by implementing **listeners** that are called in cases of success or error.

Through the configuration of the **listeners**, you can specify what happens in your app in error situations (**`onErrorSelfie` method**) or success situations (**`onSuccessSelfie` method**) during image capture.

To configure the listeners, it is necessary to implement:

{% tabs %}
{% tab title="Kotlin" %}

```kotlin
val cameraListener: iAcessoBioSelfie = object : iAcessoBioSelfie {
    override fun onSuccessSelfie(result: ResultCamera?) {}
    
    override fun onSuccess(result: SuccessResult) {}

    override fun onErrorSelfie(errorBio: ErrorBio?) {}
}
```

{% endtab %}

{% tab title="Java" %}

```java
iAcessoBioSelfie cameraListener = new iAcessoBioSelfie() {
    @Override
    public void onSuccessSelfie(ResultCamera result) { }
    
    @Override
    public void onSuccess(SuccessResult result) {}

    @Override
    public void onErrorSelfie(ErrorBio errorBio) { }
};
```

{% endtab %}
{% endtabs %}

## Prepare and open the camera

To proceed with opening the camera, it is first necessary to prepare it using the **prepareCamera** method. This method receives as parameters the implementation of the **`CameraListener`** class.

When the camera is prepared, the **`onCameraReady`** event is triggered, which receives an object of type **`UnicoCheckCameraOpener.Camera`** as a parameter.

It is necessary to override this method, opening the camera with the object received through the **`open()`** method. The **`open()`** method should receive as parameters the **listeners** configured in the previous steps.

Since we have two integration flows (one native and one via webApp), there is a difference in the method used as a trigger to start the capture flow.

For integration with the webApp flow, the implementation is as follows:

{% tabs %}
{% tab title="Kotlin" %}

```kotlin
unicoCheckCamera.prepareCamera(unicoConfig, object : CameraListener {
    override fun onCameraReady(cameraOpener: UnicoCheckCameraOpener.Camera?) {
        cameraOpener?.open(cameraListener, "your_web_app_token")
    }

    override fun onCameraFailed(message: String?) {
        Log.e(TAG, message)
    }
})
```

{% endtab %}

{% tab title="Java" %}

```java
unicoCheckCamera.prepareCamera(unicoConfig, new CameraListener() {
    @Override
    public void onCameraReady(UnicoCheckCameraOpener.Camera cameraOpener) {
        cameraOpener.open(cameraListener, "your_web_app_token");
    }

    @Override
    public void onCameraFailed(String message) {
        Log.e(TAG, message);
    }
});
```

{% endtab %}
{% endtabs %}

{% hint style="warning" %}
The web\_app\_token is a parameter of the same name that is returned in the response when creating a process in by Unico.
{% endhint %}

### `onSucessSelfie` Method

{% hint style="info" %}
This method is mandatory, since the interface is used in integration by Client. In the context of Unico, it does not return the result of the operation directly; therefore, it is necessary to use the onSuccess method, detailed below.
{% endhint %}

When an image is successfully captured, this method is invoked and returns an object of type ResultCamera, which is later used in the REST API call:

{% tabs %}
{% tab title="Kotlin" %}

```kotlin
override fun onSuccessSelfie(result: ResultCamera?) {}
```

{% endtab %}

{% tab title="Java" %}

```java
public void onSuccessSelfie(ResultCamera result) { }
```

{% endtab %}
{% endtabs %}

The SuccessResult object returns one attribute: **processId**:

* The processId attribute can be used to obtain information about the previously created process.

### `onSucess` Method

{% hint style="warning" %}
It is a method with a default value already defined, and implementation is mandatory only if integration is via IDCloud One.
{% endhint %}

Upon successfully completing the flow in the WebApp, this method is invoked and returns an object of type SuccessResult, which is later used in the REST API call:

{% tabs %}
{% tab title="Kotlin" %}

```kotlin
override fun onSuccess(result: SuccessResult) {}
```

{% endtab %}

{% tab title="Java" %}

```java
public void onSuccess(ResultCamera result) { }
```

{% endtab %}
{% endtabs %}

The SuccessResult object returns one attribute: **processId**.

* The processId attribute can be used to query the result of the customer validation process.

### `onErrorSelfie` Method

When an error occurs during image capture, this method is invoked and returns an object of type ErrorBio:

{% tabs %}
{% tab title="Kotlin" %}

```kotlin
override fun onErrorSelfie(errorBio: ErrorBio?) {}
```

{% endtab %}

{% tab title="Java" %}

```java
public void onErrorSelfie(ErrorBio errorBio) { }
```

{% endtab %}
{% endtabs %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://devcenter.unico.io/unico-idcloud/by-unico-integration/sdk-redirect/app/android/android-sdk/usage-and-integration-guide.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
