# TokenUSDK

## Introduction

TokenUSDK is an Android SDK designed to provide users with access to multiple libraries through a unified interface. This documentation will guide you through the steps to integrate TokenUSDK into your Android project using a dedicated plugin to manage repository access credentials.

## Integration Guide

### Step 1: Applying Tokeninc Gradle Build Tool Plugin

This plugin allows you to configure your Android projects to consume artifacts from private repositories.

These credentials are asked by the plugin in a Java GUI and this procedure only has to be done once unless credentials are not changed.

If the repository dependencies are managed in project level build.gradle file, enable the build plugin as follows:

Using the [plugins DSL](https://docs.gradle.org/current/userguide/plugins.html#sec:plugins_block), in newer versions of gradle

**Project level build.gradle**

```groovy
plugins {
  id "com.tokeninc.tools.build" version "0.2"
}
```

Using [legacy plugin application](https://docs.gradle.org/current/userguide/plugins.html#sec:old_plugin_application), in older versions of gradle

**Project level build.gradle**

```groovy
buildscript {
  repositories {
    gradlePluginPortal()
  }
  dependencies {
    classpath "com.tokeninc.tools:tokeninc-gradle-plugin:0.2"
  }
}

apply plugin: "com.tokeninc.tools.build"
```

#### Applying Settings Plugin

If the repository dependencies are managed in project level settings.gradle file, enable the build plugin as follows:

Using the [plugins DSL](https://docs.gradle.org/current/userguide/plugins.html#sec:plugins_block), in newer versions of gradle

**Project level settings.gradle**

```groovy
plugins {
  id "com.tokeninc.tools.settings" version "0.2"
}

dependencyResolutionManagement {
  repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
  repositories {
    // ..
  }
}

```

#### Java GUI for Settings Plugin, Preferred on Android Studio IDE

On first sync, you will be prompted a window to enter repository consumer credentials:

<figure><img src="https://3604734571-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FPaz8tFKqEd6volXC5G4B%2Fuploads%2FqdR0KfQSti0zTYVlzGhI%2Fconsumer.png?alt=media&#x26;token=1d5d0233-5790-453c-b38e-d609b995957f" alt=""><figcaption></figcaption></figure>

You need to provide the following 3 properties to consume from a Maven Repository

* consume-repo-url-1
* consume-repo-usr-1
* consume-repo-pwd-1

### Step 2: Adding the Dependency

After applying plugin, you should be able to add TokenUSDK dependency as follow:

```groovy
dependencies {
    implementation 'com.tokeninc:TokenUSDK:1.0.4'
}
```

## Usage

TokenUSDK is initialized and accessed through the singleton class `TokenUSDKManager`. Once `TokenUSDKManager` is initialized, it can be used anywhere within your project. However, there are important considerations to ensure proper usage and avoid common pitfalls.

### Initialization

The initialization and destroy process should be performed in the <mark style="color:red;">**onCreate**</mark> and <mark style="color:red;">**onDestroy**</mark> method of the <mark style="color:red;">**Launcher activity**</mark>.  <mark style="color:red;">Same activity context should ne used for bot methods!</mark>

```java
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TokenUSDKManager.getInstance().initUSDK(getBaseContext(), new ITokenUSDKCallback() {
            @Override
            public void onServicesConnected() {
                Log.i(TAG,"TokenUSDK -> onServiceConnected");
                binding.progressCircular.setVisibility(View.GONE);
            }

            @Override
            public void onServiceDisconnected() {
                Log.i(TAG,"TokenUSDK -> onServiceDisconnected");
                binding.progressCircular.setVisibility(View.VISIBLE);
            }
        });
    }
    
    @Override
    protected void onDestroy() {
        TokenUSDKManager.onDestroyUSDK(getBaseContext());
        super.onDestroy();
    }
```

Once initialized, the `TokenUSDKManager` class allows access to any service connection from anywhere in the application.

```java
// example usage of KMSService usage
TokenUSDKManager.getInstance().getKMSService()
```

### Functions

* public PrinterServiceWrapper getPrinterService() : Returns 'PrinterServiceWrapper' for printer functionalities. For further information about Printer Service APIs, please visit: [printer-service](https://developer.tokeninc.com/token-developer-portal-1/payment-service/system-and-security-services/printer-service "mention")
* public RKLServiceWrapper getRKLService() : Returns 'RKLServiceWrapper 'for RKL service functionalities. For further information about RKL service APIs, please visit:  [rkl-ca-service](https://developer.tokeninc.com/token-developer-portal-1/payment-service/system-and-security-services/rkl-ca-service "mention")
* public DeviceInfoWrapper getDeviceInfo() : Returns 'DeviceInfoWrapper 'for DeviceInfo service functionalities. For further information about DeviceInfo service APIs, please visit: [device-library](https://developer.tokeninc.com/token-developer-portal-1/payment-service/system-and-security-services/device-library "mention")
* public KMSWrapper getKMSService(): Returns 'KMSWrapper' for KMS service functionalities. For further information about KMS service APIs, please visit: [kms-library](https://developer.tokeninc.com/token-developer-portal-1/payment-service/system-and-security-services/kms-library "mention")
* public CardServiceWrapper getCardService() : Returns 'CardServiceWrapper' for Card Service functionalities. For further information about Card Service APIs, please visit:  [card-service](https://developer.tokeninc.com/token-developer-portal-1/payment-service/payment-services/card-service "mention")

### Known Issues

#### Java GUI does not appear on first sync

When you apply and sync the project for the first time, you may not see a Java GUI popping up.&#x20;

You might instead come across a warning message from the plugin that says: "Cannot instantiate credentials panel, current graphics environment does not support it!"&#x20;

This is because the previous gradle daemon instance was started in a headless environment, and it must be stopped.&#x20;

Start a terminal on the root directory of the project (e.g. by clicking the tab called Terminal located in the bottom left of Android Studio IDE) and stop the running gradle daemon(s):

&#x20;./gradlew --stop
