diff options
author | cyBerta <cyberta@riseup.net> | 2024-11-15 12:57:59 +0100 |
---|---|---|
committer | cyberta <cyberta@riseup.net> | 2024-12-11 00:09:34 +0000 |
commit | c62e338821f3772948cf1550a39157197b368cf6 (patch) | |
tree | bc83a4c440b58e53056ee939a91745e1a6c51105 /app/src/main | |
parent | 82943aabda399bd983968b2bde51c6ab3cb063b5 (diff) |
impplement BitmaskCoreProvider which allows us to swap and mock BitmaskCorey in unit tests
Diffstat (limited to 'app/src/main')
-rw-r--r-- | app/src/main/java/se/leap/bitmaskclient/base/utils/BitmaskCoreProvider.java | 22 | ||||
-rw-r--r-- | app/src/main/java/se/leap/bitmaskclient/base/utils/PreferenceHelper.java | 8 |
2 files changed, 25 insertions, 5 deletions
diff --git a/app/src/main/java/se/leap/bitmaskclient/base/utils/BitmaskCoreProvider.java b/app/src/main/java/se/leap/bitmaskclient/base/utils/BitmaskCoreProvider.java index a9797142..9cd2a747 100644 --- a/app/src/main/java/se/leap/bitmaskclient/base/utils/BitmaskCoreProvider.java +++ b/app/src/main/java/se/leap/bitmaskclient/base/utils/BitmaskCoreProvider.java @@ -1,2 +1,22 @@ -package se.leap.bitmaskclient.base.utils;public class BitmaskCoreProvider { +package se.leap.bitmaskclient.base.utils; + +import de.blinkt.openvpn.core.NativeUtils; +import mobile.BitmaskMobile; +import mobilemodels.BitmaskMobileCore; + +public class BitmaskCoreProvider { + private static BitmaskMobileCore customMobileCore; + public static BitmaskMobileCore getBitmaskMobile() { + if (customMobileCore == null) { + return new BitmaskMobile(new PreferenceHelper.SharedPreferenceStore()); + } + return customMobileCore; + } + + public static void initBitmaskMobile(BitmaskMobileCore bitmaskMobileCore) { + if (!NativeUtils.isUnitTest()) { + throw new IllegalStateException("Initializing custom BitmaskMobileCore implementation outside of an unit test is not allowed"); + } + BitmaskCoreProvider.customMobileCore = bitmaskMobileCore; + } } diff --git a/app/src/main/java/se/leap/bitmaskclient/base/utils/PreferenceHelper.java b/app/src/main/java/se/leap/bitmaskclient/base/utils/PreferenceHelper.java index 5cc3ff70..25a9a3fb 100644 --- a/app/src/main/java/se/leap/bitmaskclient/base/utils/PreferenceHelper.java +++ b/app/src/main/java/se/leap/bitmaskclient/base/utils/PreferenceHelper.java @@ -52,6 +52,7 @@ import static se.leap.bitmaskclient.base.models.Constants.USE_PORT_HOPPING; import static se.leap.bitmaskclient.base.models.Constants.USE_SNOWFLAKE; import static se.leap.bitmaskclient.base.models.Constants.USE_SYSTEM_PROXY; import static se.leap.bitmaskclient.base.models.Constants.USE_TUNNEL; +import static se.leap.bitmaskclient.base.utils.BitmaskCoreProvider.getBitmaskMobile; import android.content.Context; import android.content.SharedPreferences; @@ -82,6 +83,7 @@ import de.blinkt.openvpn.VpnProfile; import de.blinkt.openvpn.core.NativeUtils; import io.swagger.client.JSON; import mobile.BitmaskMobile; +import mobilemodels.BitmaskMobileCore; import se.leap.bitmaskclient.BuildConfig; import se.leap.bitmaskclient.base.models.Introducer; import se.leap.bitmaskclient.base.models.Provider; @@ -169,8 +171,7 @@ public class PreferenceHelper { provider.setService(preferences.getString(PROVIDER_MODELS_EIPSERVICE, null)); provider.setBridges(preferences.getString(PROVIDER_MODELS_BRIDGES, null)); provider.setGateways(preferences.getString(PROVIDER_MODELS_GATEWAYS, null)); - BitmaskMobile bm = new BitmaskMobile(new SharedPreferenceStore()); - provider.setIntroducer(bm.getIntroducerURLByDomain(provider.getDomain())); + provider.setIntroducer(getBitmaskMobile().getIntroducerURLByDomain(provider.getDomain())); } catch (Exception e) { e.printStackTrace(); @@ -218,13 +219,12 @@ public class PreferenceHelper { Set<String> providerDomains = getCustomProviderDomains(); HashMap<String, Provider> customProviders = new HashMap<>(); if (providerDomains.size() > 0) { - BitmaskMobile bm = new BitmaskMobile(new PreferenceHelper.SharedPreferenceStore()); for (String domain : providerDomains) { String mainURL = preferences.getString(Provider.MAIN_URL + "." + domain, null); if (mainURL != null) { Introducer introducer = null; try { - introducer = Introducer.fromUrl(bm.getIntroducerURLByDomain(domain)); + introducer = Introducer.fromUrl(BitmaskCoreProvider.getBitmaskMobile().getIntroducerURLByDomain(domain)); } catch (Exception e) { e.printStackTrace(); } |