Android
Android SDK API reference and configuration guide.
GeelabCaptchaClient
The primary public client class for Geelab Captcha 4.0.
Methods
getClient(Context)
Returns a GeelabCaptchaClient instance.
Declaration
public static GeelabCaptchaClient getClient(Context context)Parameters
| Parameter | Type | Description |
|---|---|---|
| context | Context | The current context. |
Discussion
This is not a singleton implementation. Retain the GeelabCaptchaClient instance returned by getClient() so that it is available from onDestroy and other lifecycle callbacks.
init(String)
Initializes the client with the default configuration.
Declaration
public GeelabCaptchaClient init(String captchaId)Parameters
| Parameter | Type | Description |
|---|---|---|
| captchaId | String | The captcha ID created in the Geelab Console. |
Discussion
Calling init() starts loading the captcha. Calling it before onResume() preloads the captcha so that it can be displayed quickly when verifyWithCaptcha() is called. The same applies below.
init(String, GeelabCaptchaConfig)
Initializes the client with a custom configuration.
Declaration
public GeelabCaptchaClient init(String captchaId, GeelabCaptchaConfig config)Parameters
| Parameter | Type | Description |
|---|---|---|
| captchaId | String | The captcha ID created in the Geelab Console. |
| config | GeelabCaptchaConfig | The configuration object. |
addOnSuccessListener(GeelabCaptchaClient.OnSuccessListener)
Registers a listener for captcha results. The listener is called after the captcha is loaded and the user completes an answer.
Declaration
public GeelabCaptchaClient addOnSuccessListener(GeelabCaptchaClient.OnSuccessListener response)Parameters
| Parameter | Type | Description |
|---|---|---|
| response | GeelabCaptchaClient.OnSuccessListener | The verification-result listener. |
addOnFailureListener(GeelabCaptchaClient.OnFailureListener)
Registers a listener for captcha loading failures.
Declaration
public GeelabCaptchaClient addOnFailureListener(GeelabCaptchaClient.OnFailureListener listener)Parameters
| Parameter | Type | Description |
|---|---|---|
| listener | GeelabCaptchaClient.OnFailureListener | The captcha-loading failure listener. |
addOnWebViewShowListener(GeelabCaptchaClient.OnWebViewShowListener)
Registers a listener that is called when the verification frontend has loaded. When preloading is complete, the listener is called immediately after verifyWithCaptcha().
Declaration
public GeelabCaptchaClient addOnWebViewShowListener(GeelabCaptchaClient.OnWebViewShowListener listener)Parameters
| Parameter | Type | Description |
|---|---|---|
| listener | GeelabCaptchaClient.OnWebViewShowListener | The verification-frontend loaded listener. |
verifyWithCaptcha()
Starts the verification flow and displays the captcha.
Declaration
public GeelabCaptchaClient verifyWithCaptcha()Discussion
Call init(), addOnSuccessListener(), and addOnFailureListener() before calling verifyWithCaptcha(), otherwise an error may occur.
cancel()
Cancels verification and closes the verification window.
Declaration
public void cancel()Discussion
Call init(), addOnSuccessListener(), and addOnFailureListener() before calling cancel(), otherwise an error may occur.
setLogEnable(boolean)
Enables or disables diagnostic logs.
Declaration
public void setLogEnable(boolean enable)Discussion
setLogEnable controls SDK log output.
destroy()
Releases resources with a long lifecycle.
Declaration
public void destroy()Discussion
Releases verification resources. You must call this method in the onDestroy lifecycle callback of the screen that uses the captcha.
configurationChanged(Configuration)
Handles orientation changes.
Declaration
public void configurationChanged(Configuration newConfig)Discussion
Call this method from the onConfigurationChanged lifecycle callback when the device orientation changes.
getVersion()
Returns the SDK version.
Declaration
public static String getVersion()Return Value
The version number as a String.
isSupportWebView(Context)
Checks whether the system WebView is supported. If it is not supported, use a fallback flow.
Declaration
public static Pair<Boolean, String> isSupportWebView(Context context)Return Value
| Parameter | Type | Description |
|---|---|---|
| first | Boolean | Whether WebView is supported. Use a fallback flow when false. |
| second | String | The error description returned while creating the WebView. |
GeelabCaptchaConfig#Builder
The configuration builder.
setTimeOut(int)
Sets the request timeout.
Declaration
public Builder setTimeOut(int timeOut)Parameters
| Parameter | Type | Description |
|---|---|---|
| timeOut | int | Timeout in milliseconds. The default is 10000. |
setResourcePath(String)
Sets the resource URL loaded by WebView.
Declaration
public Builder setResourcePath(String html)Parameters
| Parameter | Type | Description |
|---|---|---|
| html | String | The URL loaded by WebView. |
Discussion
By default, the SDK loads gl4-index*.html from the assets directory. Do not set this option unless custom resources are required.
setLanguage(String)
Sets the language sent to the JavaScript frontend. This setting controls only the language of the loaded WebView; the SDK UI follows the current application language.
Declaration
public Builder setLanguage(String language)Parameters
| Parameter | Type | Description |
|---|---|---|
| language | String | A frontend-supported language code. See the Web documentation for supported codes. |
Discussion
When this value is null or not set, the application default language code is sent to the frontend.
setCanceledOnTouchOutside(boolean)
Sets whether the dialog is dismissed when the dimmed area is tapped. The default is true.
Declaration
public Builder setCanceledOnTouchOutside(boolean canceledOnTouchOutside)Parameters
| Parameter | Type | Description |
|---|---|---|
| canceledOnTouchOutside | boolean | true dismisses the dialog when tapped outside. |
setDebug(boolean)
Enables or disables debug mode. Set this to false, or leave it unset, in production.
Declaration
public Builder setDebug(boolean debug)Parameters
| Parameter | Type | Description |
|---|---|---|
| debug | boolean | true enables debug mode. The default is false. |
setBackgroundColor(int)
Sets the Android WebView control background color. This is distinct from the JavaScript frontend background color.
Declaration
public Builder setBackgroundColor(int backgroundColor)Parameters
| Parameter | Type | Description |
|---|---|---|
| backgroundColor | int | A color resource ID or the value returned by Color.parseColor(). |
setDialogStyle(String)
Sets the captcha dialog theme.
Declaration
public Builder setDialogStyle(String dialogStyle)Parameters
| Parameter | Type | Description |
|---|---|---|
| dialogStyle | String | The name of the dialog theme style. |
The following is the default theme style:
<style name="geelab_captcha_dialog_style" parent="android:Theme.Dialog">
<!-- Remove the Dialog window frame -->
<item name="android:windowFrame">@null</item>
<!-- Display above the activity -->
<item name="android:windowIsFloating">true</item>
<!-- Make the window translucent -->
<item name="android:windowIsTranslucent">true</item>
<!-- Remove the title -->
<item name="android:windowNoTitle">true</item>
<item name="android:windowContentOverlay">@null</item>
<!-- Use a dimmed background outside the displayed area -->
<item name="android:backgroundDimEnabled">true</item>
<!-- Set the dialog background -->
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:padding">0dp</item>
</style>setDialogShowListener(GeelabCaptchaClient.OnDialogShowListener)
Sets the listener for verification-window display events.
Declaration
public void setDialogShowListener(GeelabCaptchaClient.OnDialogShowListener dialogShowListener)Parameters
| Parameter | Type | Description |
|---|---|---|
| dialogShowListener | GeelabCaptchaClient.OnDialogShowListener | The verification-window display listener. |
setParams(Map<String, Object>)
Sets additional parameters. These parameters are passed directly to JavaScript.
Declaration
public Builder setParams(Map<String, Object> params)Parameters
| Parameter | Type | Description |
|---|---|---|
| params | JSONObject | Additional configuration parameters. |
Discussion
Supported key-value pairs are listed below.
| Key | Value type | Description | Default value |
|---|---|---|---|
| displayMode | int | UI style: 0 system, 1 light, 2 dark. | 0 |
| protocol | String | Protocol prefix. Supports "https://" and "http://". | https:// |
| bgColor | String | Verification mask color. Follows CSS color rules; the final two digits specify opacity. | #00000000 |
| loading | String | Custom loading asset path relative to the HTML loaded by WebView. An empty string disables loading. | "/geelab-loading.gif" |
| rem | float | Overall captcha scale. | 1 |
| hideBar | JSONArray | Hides the close and refresh buttons in subsequent verification screens. | N/A |
| nextWidth | String | Captcha dialog width, including units, such as "300px". | N/A |
| riskType | String | Specifies the verification type when used with risk-control integration. | N/A |
| hideSuccess | boolean | Hides the verification-success dialog. | false |
Sample Code
Map<String, Object> params = new HashMap<>();
params.put("displayMode", 0);
params.put("protocol", "https://");
params.put("bgColor", "#CC0000FF");
params.put("loading", "./loading_test.gif");
params.put("rem", 0.6);
JSONArray hideBar = new JSONArray()
.put("close")
.put("refresh");
params.put("hideBar", hideBar);
params.put("nextWidth", "300px");
params.put("hideSuccess", true);
GeelabCaptchaConfig.Builder builder = new GeelabCaptchaConfig.Builder()
.setParams(params);build()
Builds a GeelabCaptchaConfig object for use with init().
Declaration
public GeelabCaptchaConfig build()GeelabCaptchaClient#OnSuccessListener
The verification-result callback listener.
onSuccess(Boolean, String)
Called with the verification result.
Declaration
void onSuccess(Boolean status, String response)Parameters
| Parameter | Type | Description |
|---|---|---|
| status | Boolean | The user verification result. true indicates success; false indicates failure and refreshes the captcha. |
| response | String | Returned when verification succeeds. Send this data to your server for secondary verification. |
Discussion
A successful verification response resembles the following:
{"lot_number":"b150e61198924eff9eaae121e9b67aa8","pass_token":"6fcfba4c35cb57b873c99c5b3b02089c58a26244c18f7706aaa1e87aed3aa8f4","gen_time":"1622020720","captcha_output":"Tu6rW0dRNO0NLxMKftwdH00G5whF5HHddthoXSEA1G4="}A failed verification response resembles the following:
{"captchaId":"647f5ed2ed8acb4be36784e01556bb71","captchaType":"slide","challenge":"95ed53b4-95c6-4c6b-9aac-769b29ef74a0"}GeelabCaptchaClient#OnFailureListener
The captcha-loading failure callback listener.
onFailure(String)
Called when captcha loading fails.
Declaration
void onFailure(String error)Parameters
| Parameter | Type | Description |
|---|---|---|
| error | String | The captcha-loading failure description. This also includes user cancellation. |
Discussion
A loading failure response resembles the following:
{"code":"-14460","msg":"Verification session cancelled","desc":{"description":"User cancelled 'Captcha'"}}GeelabCaptchaClient#OnWebViewShowListener
The verification-frontend loaded callback listener.
onWebViewShow()
Called after the verification frontend is loaded. When preloading is complete, it is called immediately after verifyWithCaptcha().
Declaration
void onWebViewShow()GeelabCaptchaClient#OnDialogShowListener
The listener for verification-window display events.
actionBeforeDialogShow(Dialog)
Called before the verification window is displayed.
Declaration
void actionBeforeDialogShow(Dialog dialog)Parameters
| Parameter | Type | Description |
|---|---|---|
| dialog | Dialog | The verification-window instance. |
actionAfterDialogShow(Dialog)
Called after the verification window is displayed.
Declaration
void actionAfterDialogShow(Dialog dialog)Parameters
| Parameter | Type | Description |
|---|---|---|
| dialog | Dialog | The verification-window instance. |
Discussion
Use this callback to adjust system UI, such as hiding the navigation bar or status bar.
onDialogFocusChanged()
Called when the verification window focus changes.
Declaration
void onDialogFocusChanged(Dialog dialog, boolean hasFocus)Parameters
| Parameter | Type | Description |
|---|---|---|
| dialog | Dialog | The verification-window instance. |
| hasFocus | boolean | Whether the window has focus. |
Error Codes
| Code | Description | Details |
|---|---|---|
-10170 | Invalid parameter | The successful Response callback is null. |
-10171 | Invalid parameter | The Context object is null. |
-10172 | Invalid parameter | The Context instance must be an Activity. |
-10174 | Invalid parameter | appId is not configured. |
-13099 | Unsupported WebView | Creating a WebView failed on the current device. |
-13175 | Invalid parameter | The H5 static resource URL is invalid. |
-13201 | Network error | General ERROR_UNKNOWN error. |
-13202 | Network error | ERR_ADDRESS_UNREACHABLE: the address cannot be reached. |
-13203 | Network error | ERROR_UNSUPPORTED_AUTH_SCHEME: unsupported authentication scheme. |
-13204 | Network error | ERROR_AUTHENTICATION: user authentication failed. |
-13205 | Network error | ERROR_PROXY_AUTHENTICATION: proxy authentication failed. |
-13206 | Network error | ERROR_CONNECT: unable to connect to the server. |
-13207 | Network error | ERROR_IO: unable to read from or write to the server. |
-13208 | Network error | ERR_CONNECTION_TIMED_OUT: connection timed out. |
-13209 | Network error | ERROR_REDIRECT_LOOP: too many redirects. |
-13210 | Network error | ERROR_UNSUPPORTED_SCHEME: unsupported URI scheme. |
-13211 | Network error | ERROR_FAILED_SSL_HANDSHAKE: SSL handshake failed. |
-13212 | Network error | ERROR_BAD_URL: invalid URL format. |
-13213 | Network error | ERROR_FILE: general file error. |
-13214 | Network error | ERROR_FILE_NOT_FOUND: file not found. |
-13215 | Network error | ERROR_TOO_MANY_REQUESTS: too many loading requests. |
-13216 | Network error | ERROR_UNSAFE_RESOURCE: resource loading was cancelled. |
-13220 | Network error | WebView loading failed; systems earlier than 4.4w provide no detailed error code. |
-13221 | Network error | WebView loading failed; Android 5.0 and 5.1 provide no detailed error code. |
-13300 | Certificate error | SSL_NOTYETVALID: certificate is not yet valid. |
-13301 | Certificate error | SSL_EXPIRED: certificate has expired. |
-13302 | Certificate error | SSL_IDMISMATCH: certificate does not match the host. |
-13303 | Certificate error | SSL_UNTRUSTED: untrusted certificate authority. |
-13304 | Certificate error | SSL_DATE_INVALID: invalid certificate date. |
-13305 | Certificate error | SSL_INVALID: general certificate error. |
-13580 | Loading issue | H5 callback data is empty. |
-13581 | Loading issue | Failed to parse H5 callback data. |
-13582 | Loading issue | H5 callback data is missing a required value. |
-14460 | Verification session cancelled | The user cancelled the verification flow. |