From 1ace91868df8344e2354c7a4d66a5ce642f3650a Mon Sep 17 00:00:00 2001 From: Sergiu Danalachi Date: Wed, 5 Apr 2023 10:11:46 +0300 Subject: [PATCH] feat(config): orientation (#103) Co-authored-by: Alexande B --- CHANGES.md | 4 ++ README.md | 1 + sdk/build.gradle | 4 +- sdk/src/main/html/hcaptcha.html | 2 + .../java/com/hcaptcha/sdk/HCaptchaConfig.java | 6 +++ .../com/hcaptcha/sdk/HCaptchaOrientation.java | 39 +++++++++++++++++++ .../com/hcaptcha/sdk/HCaptchaConfigTest.java | 6 ++- .../hcaptcha/sdk/HCaptchaJSInterfaceTest.java | 6 +++ 8 files changed, 65 insertions(+), 3 deletions(-) create mode 100644 sdk/src/main/java/com/hcaptcha/sdk/HCaptchaOrientation.java diff --git a/CHANGES.md b/CHANGES.md index da95d26a..44757a3d 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,5 +1,9 @@ # Changelog +# 3.7.0 + +- Feat: new `HCaptchaConfig.orientation` to set either `portrait` or `landscape` challenge orientation. + # 3.6.0 - Feat: new `HCaptcha.removeAllListener` and `HCaptcha.removeOn[Success|Failure|Open]Listener(listener)` to remove all or specific listener. diff --git a/README.md b/README.md index bf28cb0d..54673d73 100644 --- a/README.md +++ b/README.md @@ -153,6 +153,7 @@ The following list contains configuration properties to allows customization of |-------------------|-------------------------|----------|----------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------| | `siteKey` | String | **Yes** | - | This is your sitekey, this allows you to load challenges. If you need a sitekey, please visit [hCaptcha](https://www.hcaptcha.com), and sign up to get your sitekey. | | `size` | Enum | No | INVISIBLE | This specifies the "size" of the checkbox component. By default, the checkbox is invisible and the challenge is shown automatically. | +| `orientation` | Enum | No | PORTRAIT | This specifies the "orientation" of the challenge. | | `theme` | Enum | No | LIGHT | hCaptcha supports light, dark, and contrast themes. | | `locale` | String (ISO 639-1 code) | No | AUTO | You can enforce a specific language or let hCaptcha auto-detect the local language based on user's device. | | `resetOnTimeout` | Boolean | No | False | (DEPRECATED, use `retryPredicate`) Automatically reload to fetch new challenge if user does not submit challenge. (Matches iOS SDK behavior.) | diff --git a/sdk/build.gradle b/sdk/build.gradle index 2d62ed91..3ca8c12b 100644 --- a/sdk/build.gradle +++ b/sdk/build.gradle @@ -22,11 +22,11 @@ android { // See https://developer.android.com/studio/publish/versioning // versionCode must be integer and be incremented by one for every new update // android system uses this to prevent downgrades - versionCode 30 + versionCode 31 // version number visible to the user // should follow semantic versioning (See https://semver.org) - versionName "3.6.0" + versionName "3.7.0" buildConfigField 'String', 'VERSION_NAME', "\"${defaultConfig.versionName}_${defaultConfig.versionCode}\"" diff --git a/sdk/src/main/html/hcaptcha.html b/sdk/src/main/html/hcaptcha.html index eb56d878..5a18b238 100644 --- a/sdk/src/main/html/hcaptcha.html +++ b/sdk/src/main/html/hcaptcha.html @@ -45,6 +45,7 @@ siteKey: '10000000-ffff-ffff-ffff-000000000001', locale: 'ro', size: 'compact', + orientation: 'portrait', theme: 'dark', sentry: true, rqdata: null, @@ -94,6 +95,7 @@ return { sitekey: bridgeConfig.siteKey, size: bridgeConfig.size, + orientation: bridgeConfig.orientation, theme: getTheme(bridgeConfig), callback: function callback(token) { return BridgeObject.onPass(token); diff --git a/sdk/src/main/java/com/hcaptcha/sdk/HCaptchaConfig.java b/sdk/src/main/java/com/hcaptcha/sdk/HCaptchaConfig.java index a8c79ac0..fcb97a07 100644 --- a/sdk/src/main/java/com/hcaptcha/sdk/HCaptchaConfig.java +++ b/sdk/src/main/java/com/hcaptcha/sdk/HCaptchaConfig.java @@ -101,6 +101,12 @@ public class HCaptchaConfig implements Serializable { @Builder.Default private HCaptchaSize size = HCaptchaSize.INVISIBLE; + /** + * The orientation of the challenge. Default is {@link HCaptchaOrientation#PORTRAIT}. + */ + @Builder.Default + private HCaptchaOrientation orientation = HCaptchaOrientation.PORTRAIT; + /** * The theme. Default is {@link HCaptchaTheme#LIGHT}. */ diff --git a/sdk/src/main/java/com/hcaptcha/sdk/HCaptchaOrientation.java b/sdk/src/main/java/com/hcaptcha/sdk/HCaptchaOrientation.java new file mode 100644 index 00000000..5d6d8f8e --- /dev/null +++ b/sdk/src/main/java/com/hcaptcha/sdk/HCaptchaOrientation.java @@ -0,0 +1,39 @@ +package com.hcaptcha.sdk; + +import androidx.annotation.NonNull; + +import com.fasterxml.jackson.annotation.JsonValue; + +import java.io.Serializable; + + +/** + * The hCaptcha challenge orientation + */ +public enum HCaptchaOrientation implements Serializable { + + PORTRAIT("portrait"), + + LANDSCAPE("landscape"); + + private final String orientation; + + HCaptchaOrientation(final String orientation) { + this.orientation = orientation; + } + + /** + * @return the hCaptcha api.js string encoding + */ + public String getOrientation() { + return this.orientation; + } + + @JsonValue + @NonNull + @Override + public String toString() { + return orientation; + } + +} diff --git a/sdk/src/test/java/com/hcaptcha/sdk/HCaptchaConfigTest.java b/sdk/src/test/java/com/hcaptcha/sdk/HCaptchaConfigTest.java index 83031869..e9e6832e 100644 --- a/sdk/src/test/java/com/hcaptcha/sdk/HCaptchaConfigTest.java +++ b/sdk/src/test/java/com/hcaptcha/sdk/HCaptchaConfigTest.java @@ -22,11 +22,12 @@ public void custom_locale() { } @Test - public void default_confis() { + public void default_config() { final HCaptchaConfig config = HCaptchaConfig.builder().siteKey(MOCK_SITE_KEY).build(); assertEquals(MOCK_SITE_KEY, config.getSiteKey()); assertEquals(true, config.getSentry()); assertEquals(HCaptchaSize.INVISIBLE, config.getSize()); + assertEquals(HCaptchaOrientation.PORTRAIT, config.getOrientation()); assertEquals(HCaptchaTheme.LIGHT, config.getTheme()); assertEquals(Locale.getDefault().getLanguage(), config.getLocale()); assertEquals("https://js.hcaptcha.com/1/api.js", config.getJsSrc()); @@ -36,6 +37,7 @@ public void default_confis() { @Test public void custom_config() { + final HCaptchaOrientation hCaptchaOrientation = HCaptchaOrientation.LANDSCAPE; final HCaptchaSize hCaptchaSize = HCaptchaSize.COMPACT; final HCaptchaTheme hCaptchaTheme = HCaptchaTheme.DARK; final String customRqdata = "custom rqdata value"; @@ -55,11 +57,13 @@ public void custom_config() { .sentry(sentry) .theme(hCaptchaTheme) .size(hCaptchaSize) + .orientation(hCaptchaOrientation) .customTheme(customTheme) .build(); assertEquals(MOCK_SITE_KEY, config.getSiteKey()); assertEquals(sentry, config.getSentry()); assertEquals(hCaptchaSize, config.getSize()); + assertEquals(hCaptchaOrientation, config.getOrientation()); assertEquals(hCaptchaTheme, config.getTheme()); assertEquals(customLocale, config.getLocale()); assertEquals(customRqdata, config.getRqdata()); diff --git a/sdk/src/test/java/com/hcaptcha/sdk/HCaptchaJSInterfaceTest.java b/sdk/src/test/java/com/hcaptcha/sdk/HCaptchaJSInterfaceTest.java index 32752975..a7daf63f 100644 --- a/sdk/src/test/java/com/hcaptcha/sdk/HCaptchaJSInterfaceTest.java +++ b/sdk/src/test/java/com/hcaptcha/sdk/HCaptchaJSInterfaceTest.java @@ -50,6 +50,7 @@ public void init() { public void full_config_serialization() throws JSONException { final String siteKey = "0000-1111-2222-3333"; final String locale = "ro"; + final HCaptchaOrientation orientation = HCaptchaOrientation.PORTRAIT; final HCaptchaSize size = HCaptchaSize.NORMAL; final String rqdata = "custom rqdata"; final String jsSrc = "127.0.0.1/api.js"; @@ -63,6 +64,7 @@ public void full_config_serialization() throws JSONException { .siteKey(siteKey) .locale(locale) .size(size) + .orientation(orientation) .theme(HCaptchaTheme.DARK) .rqdata(rqdata) .jsSrc(jsSrc) @@ -90,6 +92,7 @@ public void full_config_serialization() throws JSONException { expected.put("imghost", imghost); expected.put("locale", locale); expected.put("size", size.toString()); + expected.put("orientation", orientation.toString()); expected.put("theme", HCaptchaTheme.DARK.toString()); expected.put("customTheme", JSONObject.NULL); expected.put("host", host); @@ -106,12 +109,14 @@ public void subset_config_serialization() throws JSONException { final String siteKey = "0000-1111-2222-3333"; final String locale = "ro"; final HCaptchaSize size = HCaptchaSize.NORMAL; + final HCaptchaOrientation orientation = HCaptchaOrientation.LANDSCAPE; final String rqdata = "custom rqdata"; final long defaultTimeout = 120; final HCaptchaConfig config = HCaptchaConfig.builder() .siteKey(siteKey) .locale(locale) .size(size) + .orientation(orientation) .theme(HCaptchaTheme.DARK) .rqdata(rqdata) .build(); @@ -129,6 +134,7 @@ public void subset_config_serialization() throws JSONException { expected.put("imghost", JSONObject.NULL); expected.put("locale", locale); expected.put("size", size.toString()); + expected.put("orientation", orientation.toString()); expected.put("theme", HCaptchaTheme.DARK.toString()); expected.put("customTheme", JSONObject.NULL); expected.put("host", JSONObject.NULL);