Skip to main content
Android capture

Open the capture in a Custom Tab and bring the person back to your app.

Markdown

Your back-end creates the process in Human Plugin with a Journey flow and a callbackUri that uses your app's URL scheme. The app opens the link from the response in a Custom Tab. When the capture ends, the browser opens your scheme and the app takes over.

Keep the Human Plugin id from the create response. Your back-end reads the outcome with it.

Prerequisites​

  • Android 8 (API 26) or later
  • androidx.browser:browser:1.5.0
  • A URL scheme unique to your app, such as myapp

1. Add the dependency​

In build.gradle:

implementation("androidx.browser:browser:1.5.0")

2. Create the process with your scheme​

Your back-end sends the scheme as callbackUri:

{
"flow": "humantoken",
"clientId": "client-a",
"correlationId": "order-1001",
"credential": { "type": "DUI_TYPE_NG_BVN", "value": "<document number>" },
"callbackUri": "myapp://callback"
}

and returns id and link to the app.

3. Create the activity​

import android.content.Intent
import android.net.Uri
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.browser.customtabs.CustomTabsIntent

class CaptureActivity : AppCompatActivity() {

private var processId: String? = null

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

processId = intent.getStringExtra(EXTRA_PROCESS_ID)
val link = intent.getStringExtra(EXTRA_LINK) ?: return finish()

CustomTabsIntent.Builder()
.build()
.launchUrl(this, Uri.parse(link))
}

override fun onNewIntent(intent: Intent) {
super.onNewIntent(intent)
if (intent.data?.scheme == "myapp") {
// The capture ended. Ask your back-end to read the outcome
// with GET /v1/processes/{id}, using the Human Plugin id.
onCaptureEnded(processId)
}
}

private fun onCaptureEnded(processId: String?) {
finish()
}

companion object {
const val EXTRA_PROCESS_ID = "process_id"
const val EXTRA_LINK = "link"
}
}

4. Register the scheme in AndroidManifest.xml​

<uses-feature android:name="android.hardware.camera" android:required="false" />
<uses-permission android:name="android.permission.CAMERA" />

<activity
android:name=".CaptureActivity"
android:launchMode="singleTop"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="myapp" android:host="callback" />
</intent-filter>
</activity>

singleTop makes Android deliver the return to onNewIntent() of the same activity.

5. Start the capture​

val intent = Intent(this, CaptureActivity::class.java)
.putExtra(CaptureActivity.EXTRA_PROCESS_ID, id)
.putExtra(CaptureActivity.EXTRA_LINK, link)
startActivity(intent)

Summary​

FilePurpose
build.gradleandroidx.browser dependency
CaptureActivity.ktOpens the Custom Tab and handles the return
AndroidManifest.xmlIntent filter for your scheme

Using a WebView​

warning

Prefer Custom Tabs. Use a WebView only if Custom Tabs is not an option.

The capture needs JavaScript, DOM storage, media autoplay and the camera and location permissions:

webView.settings.javaScriptEnabled = true
webView.settings.domStorageEnabled = true
webView.settings.mediaPlaybackRequiresUserGesture = false
webView.settings.setGeolocationEnabled(true)

webView.webChromeClient = object : WebChromeClient() {
override fun onPermissionRequest(request: PermissionRequest) {
if (PermissionRequest.RESOURCE_VIDEO_CAPTURE in request.resources) {
request.grant(arrayOf(PermissionRequest.RESOURCE_VIDEO_CAPTURE))
} else {
request.deny()
}
}

override fun onGeolocationPermissionsShowPrompt(
origin: String,
callback: GeolocationPermissions.Callback
) {
callback.invoke(origin, true, false)
}
}

Declare the permissions in AndroidManifest.xml and request camera and location at runtime before loading the link:

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

In a WebView, open the link directly. Do not load the Journeys SDK inside it.