From 4beb3d80b42cd195bded4bd47a59cc598d45f680 Mon Sep 17 00:00:00 2001 From: Peter Csajtai Date: Fri, 8 Aug 2025 17:45:49 +0200 Subject: [PATCH 01/17] Replace Okhttp client with HttpUrlConnection --- .github/workflows/android-ci.yml | 4 +- build.gradle | 2 - gradle.properties | 2 +- gradle/libs.versions.toml | 4 - .../java/com/configcat/ConfigCatClient.java | 65 ++++++- .../com/configcat/ConfigCatLogMessages.java | 5 +- .../java/com/configcat/ConfigFetcher.java | 178 ++++++++++-------- .../com/configcat/FormattableLogMessage.java | 3 - src/main/java/com/configcat/Utils.java | 2 +- .../java/com/configcat/AutoPollingTest.java | 26 +-- .../ConfigCatClientIntegrationTest.java | 3 +- .../com/configcat/ConfigCatClientTest.java | 8 +- .../java/com/configcat/ConfigFetcherTest.java | 25 +-- .../com/configcat/DataGovernanceTest.java | 2 +- .../java/com/configcat/LazyLoadingTest.java | 12 +- .../java/com/configcat/ManualPollingTest.java | 10 +- .../java/com/configcat/VariationIdTests.java | 2 - 17 files changed, 203 insertions(+), 150 deletions(-) diff --git a/.github/workflows/android-ci.yml b/.github/workflows/android-ci.yml index 7b2207d..7fc8801 100644 --- a/.github/workflows/android-ci.yml +++ b/.github/workflows/android-ci.yml @@ -4,7 +4,7 @@ on: schedule: - cron: '0 0 * * *' push: - branches: [ master ] + branches: [ '*' ] tags: [ 'v[0-9]+.[0-9]+.[0-9]+' ] pull_request: branches: [ master ] @@ -80,7 +80,7 @@ jobs: deploy-snapshot: needs: [ coverage-scan, test ] - if: ${{ github.ref == 'refs/heads/master' }} + if: ${{ github.ref == 'refs/heads/replace-okhttp' }} runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 diff --git a/build.gradle b/build.gradle index 585ecb7..a72c99c 100644 --- a/build.gradle +++ b/build.gradle @@ -63,8 +63,6 @@ repositories { dependencies { api(libs.android.retrofuture) - api(libs.okhttp) - api(libs.okio) api(libs.slf4j.api) api(libs.gson) api(libs.commons.codec) diff --git a/gradle.properties b/gradle.properties index ad40c0a..d0ec49d 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,3 +1,3 @@ -version=10.4.3 +version=11.0.0 org.gradle.jvmargs=-Xmx2g \ No newline at end of file diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 7914fc8..ad7a3e5 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -1,8 +1,6 @@ [versions] sonarqube = "5.0.0.4638" maven-publish = "0.32.0" -okhttp = "4.12.0" -okio = "3.4.0" # indirect dependency to solve security vulnerability in 3.2.0 slf4j-api = "1.7.36" gson = "2.9.0" commons-codec = "1.15" @@ -15,8 +13,6 @@ android-retrofuture = "1.7.4" android-gradle = "8.0.0" [libraries] -okhttp = { module = "com.squareup.okhttp3:okhttp", version.ref = "okhttp" } -okio = { module = "com.squareup.okio:okio", version.ref = "okio" } slf4j-api = { module = "org.slf4j:slf4j-api", version.ref = "slf4j-api" } gson = { module = "com.google.code.gson:gson", version.ref = "gson" } commons-codec = { module = "commons-codec:commons-codec", version.ref = "commons-codec" } diff --git a/src/main/java/com/configcat/ConfigCatClient.java b/src/main/java/com/configcat/ConfigCatClient.java index 960ccde..cac97bb 100644 --- a/src/main/java/com/configcat/ConfigCatClient.java +++ b/src/main/java/com/configcat/ConfigCatClient.java @@ -2,7 +2,6 @@ import java9.util.concurrent.CompletableFuture; import java9.util.function.Consumer; -import okhttp3.OkHttpClient; import org.slf4j.LoggerFactory; import java.io.IOException; @@ -41,9 +40,7 @@ private ConfigCatClient(String sdkKey, Options options) throws IllegalArgumentEx this.rolloutEvaluator = new RolloutEvaluator(this.logger); if (this.overrideBehaviour != OverrideBehaviour.LOCAL_ONLY) { - ConfigFetcher fetcher = new ConfigFetcher(options.httpClient == null - ? new OkHttpClient() - : options.httpClient, + ConfigFetcher fetcher = new ConfigFetcher(options.httpOptions, this.logger, sdkKey, !options.isBaseURLCustom() @@ -669,7 +666,6 @@ public CompletableFuture waitForReadyAsync() { * Configuration options for a {@link ConfigCatClient} instance. */ public static class Options { - private OkHttpClient httpClient; private ConfigCache cache = new NullConfigCache(); private String baseUrl; private PollingMode pollingMode = PollingModes.autoPoll(60); @@ -681,15 +677,14 @@ public static class Options { private boolean offline; private LogFilterFunction logFilter; + private final HttpOptions httpOptions = new HttpOptions(); private final ConfigCatHooks hooks = new ConfigCatHooks(); /** - * Sets the underlying http client which will be used to fetch the latest configuration. - * - * @param httpClient the http client. + * HTTP related options for {@link ConfigCatClient}. */ - public void httpClient(OkHttpClient httpClient) { - this.httpClient = httpClient; + public final HttpOptions httpOptions() { + return this.httpOptions; } /** @@ -799,4 +794,54 @@ private boolean isBaseURLCustom() { } } + /** + * HTTP configuration options for a {@link ConfigCatClient} instance. + */ + public static class HttpOptions { + private int connectTimeoutMillis = 20000; + private int readTimeoutMillis = 20000; + private String proxyUrl; + + /** + * Sets HTTP connect timeout in milliseconds. + * + * @param connectTimeoutMillis the connect timeout in milliseconds. + */ + public HttpOptions connectTimeoutMillis(int connectTimeoutMillis) { + this.connectTimeoutMillis = connectTimeoutMillis; + return this; + } + + /** + * Sets the HTTP read timeout in milliseconds. + * + * @param readTimeoutMillis the read timeout in milliseconds. + */ + public HttpOptions readTimeoutMillis(int readTimeoutMillis) { + this.readTimeoutMillis = readTimeoutMillis; + return this; + } + + /** + * Sets the HTTP proxy url. + * + * @param proxyUrl the HTTP proxy url. + */ + public HttpOptions proxyUrl(String proxyUrl) { + this.proxyUrl = proxyUrl; + return this; + } + + int getConnectTimeoutMillis() { + return connectTimeoutMillis; + } + + int getReadTimeoutMillis() { + return readTimeoutMillis; + } + + String getProxyUrl() { + return proxyUrl; + } + } } \ No newline at end of file diff --git a/src/main/java/com/configcat/ConfigCatLogMessages.java b/src/main/java/com/configcat/ConfigCatLogMessages.java index d8204c4..2198981 100644 --- a/src/main/java/com/configcat/ConfigCatLogMessages.java +++ b/src/main/java/com/configcat/ConfigCatLogMessages.java @@ -167,11 +167,10 @@ public static FormattableLogMessage getFetchFailedDueToUnexpectedHttpResponse(fi * * @param connectTimeoutMillis Connect timeout in milliseconds. * @param readTimeoutMillis Read timeout in milliseconds. - * @param writeTimeoutMillis Write timeout in milliseconds. * @return The formattable log message. */ - public static FormattableLogMessage getFetchFailedDueToRequestTimeout(final Integer connectTimeoutMillis, final Integer readTimeoutMillis, final Integer writeTimeoutMillis) { - return new FormattableLogMessage("Request timed out while trying to fetch config JSON. Timeout values: [connect: %dms, read: %dms, write: %dms]", connectTimeoutMillis, readTimeoutMillis, writeTimeoutMillis); + public static FormattableLogMessage getFetchFailedDueToRequestTimeout(final Integer connectTimeoutMillis, final Integer readTimeoutMillis) { + return new FormattableLogMessage("Request timed out while trying to fetch config JSON. Timeout values: [connect: %dms, read: %dms]", connectTimeoutMillis, readTimeoutMillis); } /** diff --git a/src/main/java/com/configcat/ConfigFetcher.java b/src/main/java/com/configcat/ConfigFetcher.java index d04b0ca..fb36479 100644 --- a/src/main/java/com/configcat/ConfigFetcher.java +++ b/src/main/java/com/configcat/ConfigFetcher.java @@ -1,12 +1,15 @@ package com.configcat; import java9.util.concurrent.CompletableFuture; -import okhttp3.*; -import org.jetbrains.annotations.NotNull; -import java.io.Closeable; -import java.io.IOException; +import java.io.*; +import java.net.HttpURLConnection; import java.net.SocketTimeoutException; +import java.net.URL; +import java.util.List; +import java.util.Map; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; import java.util.concurrent.atomic.AtomicBoolean; class FetchResponse { @@ -70,9 +73,10 @@ public static FetchResponse failed(Object error, boolean fetchTimeUpdatable, Str } class ConfigFetcher implements Closeable { + private final ConfigCatClient.HttpOptions httpOptions; private final AtomicBoolean closed = new AtomicBoolean(false); + private final ExecutorService executorService; private final ConfigCatLogger logger; - private final OkHttpClient httpClient; private final String mode; private final String sdkKey; private final boolean urlIsCustom; @@ -85,7 +89,7 @@ enum RedirectMode { FORCE_REDIRECT } - ConfigFetcher(OkHttpClient httpClient, + ConfigFetcher(ConfigCatClient.HttpOptions httpOptions, ConfigCatLogger logger, String sdkKey, String url, @@ -95,8 +99,9 @@ enum RedirectMode { this.sdkKey = sdkKey; this.urlIsCustom = urlIsCustom; this.url = url; - this.httpClient = httpClient; this.mode = pollingIdentifier; + this.httpOptions = httpOptions; + this.executorService = Executors.newCachedThreadPool(); } public CompletableFuture fetchAsync(String eTag) { @@ -152,66 +157,73 @@ private CompletableFuture executeFetchAsync(int executionCount, S } private CompletableFuture getResponseAsync(String eTag) { - Request request = this.getRequest(eTag); CompletableFuture future = new CompletableFuture<>(); - this.httpClient.newCall(request).enqueue(new Callback() { - @Override - public void onFailure(@NotNull Call call, @NotNull IOException e) { - String generalMessage = ConfigCatLogMessages.FETCH_FAILED_DUE_TO_UNEXPECTED_ERROR; - if (!closed.get()) { - if (e instanceof SocketTimeoutException) { - FormattableLogMessage message = ConfigCatLogMessages.getFetchFailedDueToRequestTimeout(httpClient.connectTimeoutMillis(), httpClient.readTimeoutMillis(), httpClient.writeTimeoutMillis()); - logger.error(1102, message, e); - future.complete(FetchResponse.failed(message, false, null)); - return; - } - logger.error(1103, generalMessage, e); + this.executorService.execute(() -> this.callHTTP(eTag, future)); + return future; + } + + private void callHTTP(String previousETag, CompletableFuture result) { + String requestUrl = this.url + "/configuration-files/" + this.sdkKey + "/" + Constants.CONFIG_JSON_NAME; + HttpURLConnection urlConnection = null; + + try { + URL fetchUrl = new URL(requestUrl); + urlConnection = (HttpURLConnection) fetchUrl.openConnection(); + urlConnection.setConnectTimeout(httpOptions.getConnectTimeoutMillis()); + urlConnection.setReadTimeout(httpOptions.getReadTimeoutMillis()); + urlConnection.setUseCaches(false); + urlConnection.setInstanceFollowRedirects(false); + urlConnection.setRequestMethod("GET"); + urlConnection.setRequestProperty("X-ConfigCat-UserAgent", "ConfigCat-Droid/" + this.mode + "-" + Constants.VERSION); + urlConnection.setDoOutput(true); + + if (previousETag != null && !previousETag.isEmpty()) + urlConnection.setRequestProperty("If-None-Match", previousETag); + + int responseCode = urlConnection.getResponseCode(); + Map> responseHeaders = urlConnection.getHeaderFields(); + + String cfRayId = readHeaderValue(responseHeaders, "CF-RAY"); + if (responseCode == 200) { + String content = readBody(urlConnection.getInputStream()); + String eTag = readHeaderValue(responseHeaders,"ETag"); + Result configResult = deserializeConfig(content, cfRayId); + if (configResult.error() != null) { + result.complete(FetchResponse.failed(configResult.error(), false, cfRayId)); + return; + } + logger.debug("Fetch was successful: new config fetched."); + result.complete(FetchResponse.fetched(new Entry(configResult.value(), eTag, content, System.currentTimeMillis()), cfRayId)); + } else if (responseCode == 304) { + if(cfRayId != null) { + logger.debug(String.format("Fetch was successful: config not modified. %s", ConfigCatLogMessages.getCFRayIdPostFix(cfRayId))); + } else { + logger.debug("Fetch was successful: config not modified."); } - future.complete(FetchResponse.failed(generalMessage, false, null)); + result.complete(FetchResponse.notModified(cfRayId)); + } else if (responseCode == 403 || responseCode == 404) { + FormattableLogMessage message = ConfigCatLogMessages.getFetchFailedDueToInvalidSDKKey(cfRayId); + logger.error(1100, message); + result.complete(FetchResponse.failed(message, true, cfRayId)); + } else { + FormattableLogMessage message = ConfigCatLogMessages.getFetchFailedDueToUnexpectedHttpResponse(responseCode, urlConnection.getResponseMessage(), cfRayId); + logger.error(1101, message); + result.complete(FetchResponse.failed(message, false, cfRayId)); } - @Override - public void onResponse(@NotNull Call call, @NotNull Response response) { - try (ResponseBody body = response.body()) { - String cfRayId = response.header("CF-RAY"); - if (response.code() == 200) { - String content = body != null ? body.string() : null; - String eTag = response.header("ETag"); - Result result = deserializeConfig(content, cfRayId); - if (result.error() != null) { - future.complete(FetchResponse.failed(result.error(), false, cfRayId)); - return; - } - logger.debug("Fetch was successful: new config fetched."); - future.complete(FetchResponse.fetched(new Entry(result.value(), eTag, content, System.currentTimeMillis()), cfRayId)); - } else if (response.code() == 304) { - if(cfRayId != null) { - logger.debug(String.format("Fetch was successful: config not modified. %s", ConfigCatLogMessages.getCFRayIdPostFix(cfRayId))); - } else { - logger.debug("Fetch was successful: config not modified."); - } - future.complete(FetchResponse.notModified(cfRayId)); - } else if (response.code() == 403 || response.code() == 404) { - FormattableLogMessage message = ConfigCatLogMessages.getFetchFailedDueToInvalidSDKKey(cfRayId); - logger.error(1100, message); - future.complete(FetchResponse.failed(message, true, cfRayId)); - } else { - FormattableLogMessage message = ConfigCatLogMessages.getFetchFailedDueToUnexpectedHttpResponse(response.code(), response.message(), cfRayId); - logger.error(1101, message); - future.complete(FetchResponse.failed(message, false, cfRayId)); - } - } catch (SocketTimeoutException e) { - FormattableLogMessage message = ConfigCatLogMessages.getFetchFailedDueToRequestTimeout(httpClient.connectTimeoutMillis(), httpClient.readTimeoutMillis(), httpClient.writeTimeoutMillis()); - logger.error(1102, message, e); - future.complete(FetchResponse.failed(message, false, null)); - } catch (Exception e) { - String message = ConfigCatLogMessages.FETCH_FAILED_DUE_TO_UNEXPECTED_ERROR; - logger.error(1103, message, e); - future.complete(FetchResponse.failed(message + " " + e.getMessage(), false, null)); - } + } catch (SocketTimeoutException e) { + FormattableLogMessage message = ConfigCatLogMessages.getFetchFailedDueToRequestTimeout(httpOptions.getConnectTimeoutMillis(), httpOptions.getReadTimeoutMillis()); + logger.error(1102, message, e); + result.complete(FetchResponse.failed(message, false, null)); + } catch (Exception e) { + String message = ConfigCatLogMessages.FETCH_FAILED_DUE_TO_UNEXPECTED_ERROR; + logger.error(1103, message, e); + result.complete(FetchResponse.failed(message + " " + e.getMessage(), false, null)); + } finally { + if (urlConnection != null) { + urlConnection.disconnect(); } - }); - return future; + } } @Override @@ -219,25 +231,37 @@ public void close() throws IOException { if (!this.closed.compareAndSet(false, true)) { return; } - - if (this.httpClient != null) { - this.httpClient.dispatcher().executorService().shutdownNow(); - this.httpClient.connectionPool().evictAll(); - Cache cache = this.httpClient.cache(); - if (cache != null) - cache.close(); + if (this.executorService != null) { + this.executorService.shutdownNow(); } } - private Request getRequest(String eTag) { - String requestUrl = this.url + "/configuration-files/" + this.sdkKey + "/" + Constants.CONFIG_JSON_NAME; - Request.Builder builder = new Request.Builder() - .addHeader("X-ConfigCat-UserAgent", "ConfigCat-Droid/" + this.mode + "-" + Constants.VERSION); - - if (eTag != null && !eTag.isEmpty()) - builder.addHeader("If-None-Match", eTag); + private String readHeaderValue(Map> responseHeaders, String headerName) { + if (responseHeaders == null || responseHeaders.isEmpty()) { + return null; + } + for (Map.Entry> entry : responseHeaders.entrySet()) { + if (entry.getKey() != null && entry.getKey().equalsIgnoreCase(headerName)) { + if (entry.getValue() != null && !entry.getValue().isEmpty()) { + return entry.getValue().get(0); + } + } + } + return null; + } - return builder.url(requestUrl).build(); + private String readBody(InputStream inputStream) throws IOException { + if (inputStream == null) { + return null; + } + StringBuilder body = new StringBuilder(); + BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); + String line; + while ((line = reader.readLine()) != null) { + body.append(line); + } + reader.close(); + return body.toString(); } private Result deserializeConfig(String json, String cfRayId) { diff --git a/src/main/java/com/configcat/FormattableLogMessage.java b/src/main/java/com/configcat/FormattableLogMessage.java index c31d3a6..7ac6dbe 100644 --- a/src/main/java/com/configcat/FormattableLogMessage.java +++ b/src/main/java/com/configcat/FormattableLogMessage.java @@ -1,7 +1,5 @@ package com.configcat; -import org.jetbrains.annotations.NotNull; - import java.util.Arrays; import java.util.Objects; @@ -20,7 +18,6 @@ protected String formatLogMessage(){ return String.format(message, args); } - @NotNull @Override public String toString() { if(cachedMessage == null) { diff --git a/src/main/java/com/configcat/Utils.java b/src/main/java/com/configcat/Utils.java index 68950b2..a118b0a 100644 --- a/src/main/java/com/configcat/Utils.java +++ b/src/main/java/com/configcat/Utils.java @@ -56,7 +56,7 @@ private Constants() { /* prevent from instantiation*/ } static final long DISTANT_PAST = 0; static final String CONFIG_JSON_NAME = "config_v6.json"; static final String SERIALIZATION_FORMAT_VERSION = "v2"; - static final String VERSION = "10.4.3"; + static final String VERSION = "11.0.0"; static final String SDK_KEY_PROXY_PREFIX = "configcat-proxy/"; static final String SDK_KEY_PREFIX = "configcat-sdk-1"; diff --git a/src/test/java/com/configcat/AutoPollingTest.java b/src/test/java/com/configcat/AutoPollingTest.java index 8b6ff46..374259c 100644 --- a/src/test/java/com/configcat/AutoPollingTest.java +++ b/src/test/java/com/configcat/AutoPollingTest.java @@ -45,7 +45,7 @@ void get() throws InterruptedException, ExecutionException, IOException { ConfigCache cache = new NullConfigCache(); PollingMode pollingMode = PollingModes.autoPoll(2); - ConfigFetcher fetcher = new ConfigFetcher(new OkHttpClient.Builder().build(), + ConfigFetcher fetcher = new ConfigFetcher(new ConfigCatClient.HttpOptions(), logger, "", this.server.url("/").toString(), @@ -71,7 +71,7 @@ void getFail() throws InterruptedException, ExecutionException, IOException { ConfigCache cache = new NullConfigCache(); PollingMode pollingMode = PollingModes.autoPoll(2); - ConfigFetcher fetcher = new ConfigFetcher(new OkHttpClient.Builder().build(), + ConfigFetcher fetcher = new ConfigFetcher(new ConfigCatClient.HttpOptions(), logger, "", this.server.url("/").toString(), @@ -94,7 +94,7 @@ void getMany() throws InterruptedException, ExecutionException, IOException { ConfigCache cache = new NullConfigCache(); PollingMode pollingMode = PollingModes.autoPoll(2); - ConfigFetcher fetcher = new ConfigFetcher(new OkHttpClient.Builder().build(), + ConfigFetcher fetcher = new ConfigFetcher(new ConfigCatClient.HttpOptions(), logger, "", this.server.url("/").toString(), @@ -135,7 +135,7 @@ void getWithFailedRefresh() throws InterruptedException, ExecutionException, IOE ConfigCache cache = new NullConfigCache(); PollingMode pollingMode = PollingModes.autoPoll(2); - ConfigFetcher fetcher = new ConfigFetcher(new OkHttpClient.Builder().build(), + ConfigFetcher fetcher = new ConfigFetcher(new ConfigCatClient.HttpOptions(), logger, "", this.server.url("/").toString(), @@ -165,7 +165,7 @@ void getCacheFails() throws Exception { doThrow(new Exception()).when(cache).write(anyString(), anyString()); PollingMode pollingMode = PollingModes.autoPoll(2); - ConfigFetcher fetcher = new ConfigFetcher(new OkHttpClient.Builder().build(), + ConfigFetcher fetcher = new ConfigFetcher(new ConfigCatClient.HttpOptions(), logger, "", this.server.url("/").toString(), @@ -183,7 +183,7 @@ void testInitWaitTimeTimeout() throws Exception { this.server.enqueue(new MockResponse().setResponseCode(200).setBody(String.format(TEST_JSON, "test")).setBodyDelay(2, TimeUnit.SECONDS)); PollingMode pollingMode = PollingModes.autoPoll(60, 1); - ConfigFetcher fetcher = new ConfigFetcher(new OkHttpClient(), + ConfigFetcher fetcher = new ConfigFetcher(new ConfigCatClient.HttpOptions(), logger, "", this.server.url("/").toString(), @@ -206,7 +206,7 @@ void testPollIntervalRespectsCacheExpiration() throws Exception { ConfigCache cache = new SingleValueCache(Helpers.cacheValueFromConfigJson(String.format(TEST_JSON, "test"))); PollingMode pollingMode = PollingModes.autoPoll(2); - ConfigFetcher fetcher = new ConfigFetcher(new OkHttpClient(), + ConfigFetcher fetcher = new ConfigFetcher(new ConfigCatClient.HttpOptions(), logger, "", this.server.url("/").toString(), @@ -230,7 +230,7 @@ void testPollsWhenCacheExpired() throws Exception { ConfigCache cache = new SingleValueCache(Helpers.cacheValueFromConfigJsonAndTime(String.format(TEST_JSON, "test"), System.currentTimeMillis() - 5000)); PollingMode pollingMode = PollingModes.autoPoll(1); - ConfigFetcher fetcher = new ConfigFetcher(new OkHttpClient(), + ConfigFetcher fetcher = new ConfigFetcher(new ConfigCatClient.HttpOptions(), logger, "", this.server.url("/").toString(), @@ -254,7 +254,7 @@ void testNonExpiredCacheCallsReady() throws Exception { ConfigCatHooks hooks = new ConfigCatHooks(); hooks.addOnClientReady(clientReadyState -> ready.set(clientReadyState)); PollingMode pollingMode = PollingModes.autoPoll(2); - ConfigFetcher fetcher = new ConfigFetcher(new OkHttpClient(), + ConfigFetcher fetcher = new ConfigFetcher(new ConfigCatClient.HttpOptions(), logger, "", this.server.url("/").toString(), @@ -277,7 +277,7 @@ void testOnlineOffline() throws Exception { this.server.enqueue(new MockResponse().setResponseCode(200).setBody(String.format(TEST_JSON, "test"))); PollingMode pollingMode = PollingModes.autoPoll(1); - ConfigFetcher fetcher = new ConfigFetcher(new OkHttpClient(), + ConfigFetcher fetcher = new ConfigFetcher(new ConfigCatClient.HttpOptions(), logger, "", this.server.url("/").toString(), @@ -308,7 +308,7 @@ void testInitOffline() throws Exception { this.server.enqueue(new MockResponse().setResponseCode(200).setBody(String.format(TEST_JSON, "test"))); PollingMode pollingMode = PollingModes.autoPoll(1); - ConfigFetcher fetcher = new ConfigFetcher(new OkHttpClient(), + ConfigFetcher fetcher = new ConfigFetcher(new ConfigCatClient.HttpOptions(), logger, "", this.server.url("/").toString(), @@ -337,7 +337,7 @@ void testInitWaitTimeIgnoredWhenCacheIsNotExpired() throws Exception { ConfigCache cache = new SingleValueCache(Helpers.cacheValueFromConfigJson(String.format(TEST_JSON, "test"))); PollingMode pollingMode = PollingModes.autoPoll(60, 1); - ConfigFetcher fetcher = new ConfigFetcher(new OkHttpClient(), + ConfigFetcher fetcher = new ConfigFetcher(new ConfigCatClient.HttpOptions(), logger, "", this.server.url("/").toString(), @@ -360,7 +360,7 @@ void testInitWaitTimeReturnCached() throws Exception { ConfigCache cache = new SingleValueCache(Helpers.cacheValueFromConfigJsonAndTime(String.format(TEST_JSON, "test"), Constants.DISTANT_PAST)); PollingMode pollingMode = PollingModes.autoPoll(60, 1); - ConfigFetcher fetcher = new ConfigFetcher(new OkHttpClient(), + ConfigFetcher fetcher = new ConfigFetcher(new ConfigCatClient.HttpOptions(), logger, "", this.server.url("/").toString(), diff --git a/src/test/java/com/configcat/ConfigCatClientIntegrationTest.java b/src/test/java/com/configcat/ConfigCatClientIntegrationTest.java index 3cef102..cbfe6c5 100644 --- a/src/test/java/com/configcat/ConfigCatClientIntegrationTest.java +++ b/src/test/java/com/configcat/ConfigCatClientIntegrationTest.java @@ -192,7 +192,8 @@ void invalidateCacheFail() { @Test void getConfigurationJsonStringWithDefaultConfigTimeout() { - ConfigCatClient cl = ConfigCatClient.get("configcat-sdk-1/TEST_KEY1-123456789012/1234567890123456789012", options -> options.httpClient(new OkHttpClient.Builder().readTimeout(2, TimeUnit.SECONDS).build())); + ConfigCatClient cl = ConfigCatClient.get("configcat-sdk-1/TEST_KEY1-123456789012/1234567890123456789012", + options -> options.httpOptions().readTimeoutMillis(2000)); // makes a call to a real url which would fail, null expected String config = cl.getValue(String.class, "test", null); diff --git a/src/test/java/com/configcat/ConfigCatClientTest.java b/src/test/java/com/configcat/ConfigCatClientTest.java index 3574fc9..875fa56 100644 --- a/src/test/java/com/configcat/ConfigCatClientTest.java +++ b/src/test/java/com/configcat/ConfigCatClientTest.java @@ -92,7 +92,7 @@ void testSDKKeyValidation() throws IOException { @Test void getValueWithDefaultConfigTimeout() throws IOException { - ConfigCatClient cl = ConfigCatClient.get(Helpers.SDK_KEY, options -> options.httpClient(new OkHttpClient.Builder().readTimeout(2, TimeUnit.SECONDS).build())); + ConfigCatClient cl = ConfigCatClient.get(Helpers.SDK_KEY, options -> options.httpOptions().readTimeoutMillis(2000)); // makes a call to a real url which would fail, default expected boolean config = cl.getValue(Boolean.class, "key", true); @@ -183,7 +183,7 @@ void getConfigurationReturnsPreviousCachedOnTimeout() throws IOException { server.start(); ConfigCatClient cl = ConfigCatClient.get(Helpers.SDK_KEY, options -> { - options.httpClient(new OkHttpClient.Builder().readTimeout(1, TimeUnit.SECONDS).build()); + options.httpOptions().readTimeoutMillis(1000); options.pollingMode(PollingModes.manualPoll()); options.baseUrl(server.url("/").toString()); }); @@ -249,7 +249,7 @@ void getValueReturnsDefaultOnExceptionRepeatedly() throws IOException { server.start(); ConfigCatClient cl = ConfigCatClient.get(Helpers.SDK_KEY, options -> { - options.httpClient(new OkHttpClient.Builder().readTimeout(1, TimeUnit.SECONDS).build()); + options.httpOptions().readTimeoutMillis(1000); options.pollingMode(PollingModes.manualPoll()); options.baseUrl(server.url("/").toString()); }); @@ -275,7 +275,7 @@ void forceRefreshWithTimeout() throws IOException { server.start(); ConfigCatClient cl = ConfigCatClient.get(Helpers.SDK_KEY, options -> { - options.httpClient(new OkHttpClient.Builder().readTimeout(1, TimeUnit.SECONDS).build()); + options.httpOptions().readTimeoutMillis(1000); options.pollingMode(PollingModes.manualPoll()); options.baseUrl(server.url("/").toString()); }); diff --git a/src/test/java/com/configcat/ConfigFetcherTest.java b/src/test/java/com/configcat/ConfigFetcherTest.java index 5ce6c3b..df24e0f 100644 --- a/src/test/java/com/configcat/ConfigFetcherTest.java +++ b/src/test/java/com/configcat/ConfigFetcherTest.java @@ -45,7 +45,7 @@ void fetchNotModified() throws InterruptedException, ExecutionException, IOExcep this.server.enqueue(new MockResponse().setResponseCode(200).setBody(TEST_JSON).setHeader("ETag", "fakeETag")); this.server.enqueue(new MockResponse().setResponseCode(304)); - ConfigFetcher fetcher = new ConfigFetcher(new OkHttpClient.Builder().build(), logger, + ConfigFetcher fetcher = new ConfigFetcher(new ConfigCatClient.HttpOptions(), logger, "", this.server.url("/").toString(), false, PollingModes.manualPoll().getPollingIdentifier()); FetchResponse fResult = fetcher.fetchAsync(null).get(); @@ -68,10 +68,7 @@ void fetchNotModified() throws InterruptedException, ExecutionException, IOExcep @Test void fetchException() throws IOException, ExecutionException, InterruptedException { - - ConfigFetcher fetch = new ConfigFetcher(new OkHttpClient.Builder() - .readTimeout(1, TimeUnit.SECONDS) - .build(), + ConfigFetcher fetch = new ConfigFetcher(new ConfigCatClient.HttpOptions().readTimeoutMillis(1000), logger, "", this.server.url("/").toString(), @@ -98,7 +95,7 @@ void fetchedETagNotUpdatesCache() throws Exception { ConfigCache cache = mock(ConfigCache.class); when(cache.read(anyString())).thenReturn(gson.toJson(entry)); - ConfigFetcher fetcher = new ConfigFetcher(new OkHttpClient.Builder().build(), logger, + ConfigFetcher fetcher = new ConfigFetcher(new ConfigCatClient.HttpOptions(), logger, "", this.server.url("/").toString(), false, PollingModes.manualPoll().getPollingIdentifier()); ConfigService policy = new ConfigService("", PollingModes.autoPoll(2), cache, logger, fetcher, new ConfigCatHooks(), false); @@ -115,7 +112,7 @@ void fetchedSameResponseNotUpdatesCache() throws Exception { ConfigCache cache = mock(ConfigCache.class); when(cache.read(anyString())).thenReturn(TEST_JSON); - ConfigFetcher fetcher = new ConfigFetcher(new OkHttpClient.Builder().build(), logger, + ConfigFetcher fetcher = new ConfigFetcher(new ConfigCatClient.HttpOptions(), logger, "", this.server.url("/").toString(), false, PollingModes.manualPoll().getPollingIdentifier()); ConfigService policy = new ConfigService("", PollingModes.autoPoll(2), cache, logger, fetcher, new ConfigCatHooks(), false); @@ -134,7 +131,7 @@ void fetchSuccess() throws Exception { doThrow(new Exception()).when(cache).read(anyString()); doThrow(new Exception()).when(cache).write(anyString(), anyString()); - ConfigFetcher fetcher = new ConfigFetcher(new OkHttpClient.Builder().build(), logger, + ConfigFetcher fetcher = new ConfigFetcher(new ConfigCatClient.HttpOptions(), logger, "", this.server.url("/").toString(), false, PollingModes.manualPoll().getPollingIdentifier()); FetchResponse response = fetcher.fetchAsync(null).get(); @@ -156,7 +153,7 @@ private static Stream emptyFetchTestData() { void fetchEmpty(String body) throws Exception { this.server.enqueue(new MockResponse().setResponseCode(200).setBody(body)); - ConfigFetcher fetcher = new ConfigFetcher(new OkHttpClient.Builder().build(), + ConfigFetcher fetcher = new ConfigFetcher(new ConfigCatClient.HttpOptions(), logger, "", this.server.url("/").toString(), @@ -172,9 +169,7 @@ void fetchEmpty(String body) throws Exception { @Test void testIntegration() throws IOException, ExecutionException, InterruptedException { - ConfigFetcher fetch = new ConfigFetcher(new OkHttpClient.Builder() - .readTimeout(1, TimeUnit.SECONDS) - .build(), + ConfigFetcher fetch = new ConfigFetcher(new ConfigCatClient.HttpOptions().readTimeoutMillis(1000), logger, "PKDVCLf-Hq-h-kCzMp-L7Q/PaDVCFk9EpmD6sLpGLltTA", "https://cdn-global.configcat.com", @@ -196,7 +191,7 @@ public void fetchedFail403ContainsCFRAY() throws Exception { ConfigCatLogger localLogger = new ConfigCatLogger(mockLogger, LogLevel.DEBUG, null, null); - ConfigFetcher fetcher = new ConfigFetcher(new OkHttpClient.Builder().build(), + ConfigFetcher fetcher = new ConfigFetcher(new ConfigCatClient.HttpOptions(), localLogger, "", this.server.url("/").toString(), @@ -221,7 +216,7 @@ public void fetchedNotModified304ContainsCFRAY() throws Exception { ConfigCatLogger localLogger = new ConfigCatLogger(mockLogger, LogLevel.DEBUG, null, null); - ConfigFetcher fetcher = new ConfigFetcher(new OkHttpClient.Builder().build(), + ConfigFetcher fetcher = new ConfigFetcher(new ConfigCatClient.HttpOptions(), localLogger, "", this.server.url("/").toString(), @@ -246,7 +241,7 @@ public void fetchedReceivedInvalidBodyContainsCFRAY() throws Exception { ConfigCatLogger localLogger = new ConfigCatLogger(mockLogger, LogLevel.DEBUG, null, null); - ConfigFetcher fetcher = new ConfigFetcher(new OkHttpClient.Builder().build(), + ConfigFetcher fetcher = new ConfigFetcher(new ConfigCatClient.HttpOptions(), localLogger, "", this.server.url("/").toString(), diff --git a/src/test/java/com/configcat/DataGovernanceTest.java b/src/test/java/com/configcat/DataGovernanceTest.java index 43a98ac..7e1f1f1 100644 --- a/src/test/java/com/configcat/DataGovernanceTest.java +++ b/src/test/java/com/configcat/DataGovernanceTest.java @@ -245,6 +245,6 @@ private MockWebServer createServer() throws IOException { } private ConfigFetcher createFetcher(String url, boolean isCustomUrl) { - return new ConfigFetcher(new OkHttpClient.Builder().build(), logger, "", url, isCustomUrl, "m"); + return new ConfigFetcher(new ConfigCatClient.HttpOptions(), logger, "", url, isCustomUrl, "m"); } } diff --git a/src/test/java/com/configcat/LazyLoadingTest.java b/src/test/java/com/configcat/LazyLoadingTest.java index 5318a7e..107a1eb 100644 --- a/src/test/java/com/configcat/LazyLoadingTest.java +++ b/src/test/java/com/configcat/LazyLoadingTest.java @@ -28,7 +28,7 @@ public void setUp() throws IOException { PollingMode mode = PollingModes .lazyLoad(5); - ConfigFetcher fetcher = new ConfigFetcher(new OkHttpClient.Builder().build(), logger, "", this.server.url("/").toString(), false, mode.getPollingIdentifier()); + ConfigFetcher fetcher = new ConfigFetcher(new ConfigCatClient.HttpOptions(), logger, "", this.server.url("/").toString(), false, mode.getPollingIdentifier()); this.policy = new ConfigService("", mode, new NullConfigCache(), logger, fetcher, new ConfigCatHooks(), false); } @@ -57,7 +57,7 @@ void get() throws InterruptedException, ExecutionException { void getCacheFails() throws InterruptedException, ExecutionException { PollingMode mode = PollingModes .lazyLoad(5); - ConfigFetcher fetcher = new ConfigFetcher(new OkHttpClient.Builder().build(), logger, "", this.server.url("/").toString(), false, mode.getPollingIdentifier()); + ConfigFetcher fetcher = new ConfigFetcher(new ConfigCatClient.HttpOptions(), logger, "", this.server.url("/").toString(), false, mode.getPollingIdentifier()); ConfigService lPolicy = new ConfigService("", mode, new NullConfigCache(), logger, fetcher, new ConfigCatHooks(), false); this.server.enqueue(new MockResponse().setResponseCode(200).setBody(String.format(TEST_JSON, "test"))); @@ -96,7 +96,7 @@ void testCacheExpirationRespectedInTTLCalc() throws InterruptedException, Execut PollingMode mode = PollingModes .lazyLoad(1); - ConfigFetcher fetcher = new ConfigFetcher(new OkHttpClient.Builder().build(), logger, "", this.server.url("/").toString(), false, mode.getPollingIdentifier()); + ConfigFetcher fetcher = new ConfigFetcher(new ConfigCatClient.HttpOptions(), logger, "", this.server.url("/").toString(), false, mode.getPollingIdentifier()); ConfigService service = new ConfigService("", mode, cache, logger, fetcher, new ConfigCatHooks(), false); assertFalse(service.getSettings().get().settings().isEmpty()); @@ -120,7 +120,7 @@ void testCacheExpirationRespectedInTTLCalc304() throws InterruptedException, Exe PollingMode mode = PollingModes .lazyLoad(1); - ConfigFetcher fetcher = new ConfigFetcher(new OkHttpClient.Builder().build(), logger, "", this.server.url("/").toString(), false, mode.getPollingIdentifier()); + ConfigFetcher fetcher = new ConfigFetcher(new ConfigCatClient.HttpOptions(), logger, "", this.server.url("/").toString(), false, mode.getPollingIdentifier()); ConfigService service = new ConfigService("", mode, cache, logger, fetcher, new ConfigCatHooks(), false); assertFalse(service.getSettings().get().settings().isEmpty()); @@ -142,7 +142,7 @@ void testOnlineOffline() throws Exception { this.server.enqueue(new MockResponse().setResponseCode(200).setBody(String.format(TEST_JSON, "test"))); PollingMode pollingMode = PollingModes.lazyLoad(1); - ConfigFetcher fetcher = new ConfigFetcher(new OkHttpClient(), + ConfigFetcher fetcher = new ConfigFetcher(new ConfigCatClient.HttpOptions(), logger, "", this.server.url("/").toString(), @@ -176,7 +176,7 @@ void testInitOffline() throws Exception { this.server.enqueue(new MockResponse().setResponseCode(200).setBody(String.format(TEST_JSON, "test"))); PollingMode pollingMode = PollingModes.lazyLoad(1); - ConfigFetcher fetcher = new ConfigFetcher(new OkHttpClient(), + ConfigFetcher fetcher = new ConfigFetcher(new ConfigCatClient.HttpOptions(), logger, "", this.server.url("/").toString(), diff --git a/src/test/java/com/configcat/ManualPollingTest.java b/src/test/java/com/configcat/ManualPollingTest.java index 79b47d4..281acbe 100644 --- a/src/test/java/com/configcat/ManualPollingTest.java +++ b/src/test/java/com/configcat/ManualPollingTest.java @@ -26,7 +26,7 @@ public void setUp() throws IOException { this.server.start(); PollingMode mode = PollingModes.manualPoll(); - ConfigFetcher fetcher = new ConfigFetcher(new OkHttpClient.Builder().build(), logger, "", this.server.url("/").toString(), false, mode.getPollingIdentifier()); + ConfigFetcher fetcher = new ConfigFetcher(new ConfigCatClient.HttpOptions(), logger, "", this.server.url("/").toString(), false, mode.getPollingIdentifier()); this.policy = new ConfigService("", mode, new NullConfigCache(), logger, fetcher, new ConfigCatHooks(), false); } @@ -53,7 +53,7 @@ void get() throws InterruptedException, ExecutionException { @Test void getCacheFails() throws InterruptedException, ExecutionException, IOException { PollingMode mode = PollingModes.manualPoll(); - ConfigFetcher fetcher = new ConfigFetcher(new OkHttpClient.Builder().build(), logger, "", this.server.url("/").toString(), false, mode.getPollingIdentifier()); + ConfigFetcher fetcher = new ConfigFetcher(new ConfigCatClient.HttpOptions(), logger, "", this.server.url("/").toString(), false, mode.getPollingIdentifier()); ConfigService lPolicy = new ConfigService("", mode, new NullConfigCache(), logger, fetcher, new ConfigCatHooks(), false); this.server.enqueue(new MockResponse().setResponseCode(200).setBody(String.format(TEST_JSON, "test"))); @@ -91,7 +91,7 @@ void testCache() throws InterruptedException, ExecutionException, IOException { InMemoryCache cache = new InMemoryCache(); PollingMode mode = PollingModes.manualPoll(); - ConfigFetcher fetcher = new ConfigFetcher(new OkHttpClient.Builder().build(), logger, "", this.server.url("/").toString(), false, mode.getPollingIdentifier()); + ConfigFetcher fetcher = new ConfigFetcher(new ConfigCatClient.HttpOptions(), logger, "", this.server.url("/").toString(), false, mode.getPollingIdentifier()); ConfigService service = new ConfigService("", mode, cache, logger, fetcher, new ConfigCatHooks(), false); service.refresh().get(); @@ -119,7 +119,7 @@ void testOnlineOffline() throws Exception { this.server.enqueue(new MockResponse().setResponseCode(200).setBody(String.format(TEST_JSON, "test"))); PollingMode pollingMode = PollingModes.manualPoll(); - ConfigFetcher fetcher = new ConfigFetcher(new OkHttpClient(), + ConfigFetcher fetcher = new ConfigFetcher(new ConfigCatClient.HttpOptions(), logger, "", this.server.url("/").toString(), @@ -152,7 +152,7 @@ void testInitOffline() throws Exception { this.server.enqueue(new MockResponse().setResponseCode(200).setBody(String.format(TEST_JSON, "test"))); PollingMode pollingMode = PollingModes.manualPoll(); - ConfigFetcher fetcher = new ConfigFetcher(new OkHttpClient(), + ConfigFetcher fetcher = new ConfigFetcher(new ConfigCatClient.HttpOptions(), logger, "", this.server.url("/").toString(), diff --git a/src/test/java/com/configcat/VariationIdTests.java b/src/test/java/com/configcat/VariationIdTests.java index 044eccc..0b2df91 100644 --- a/src/test/java/com/configcat/VariationIdTests.java +++ b/src/test/java/com/configcat/VariationIdTests.java @@ -1,6 +1,5 @@ package com.configcat; -import okhttp3.OkHttpClient; import okhttp3.mockwebserver.MockResponse; import okhttp3.mockwebserver.MockWebServer; import org.junit.jupiter.api.AfterEach; @@ -27,7 +26,6 @@ public void setUp() throws IOException { this.server.start(); this.client = ConfigCatClient.get(Helpers.SDK_KEY, options -> { - options.httpClient(new OkHttpClient.Builder().build()); options.pollingMode(PollingModes.lazyLoad(2)); options.baseUrl(this.server.url("/").toString()); }); From ff61a259f0cafa63b80bb5ca2e817ca20f4d6d7a Mon Sep 17 00:00:00 2001 From: Peter Csajtai Date: Fri, 8 Aug 2025 18:21:54 +0200 Subject: [PATCH 02/17] Handle HTTP proxy --- .../java/com/configcat/ConfigCatClient.java | 19 ++++++++++--------- .../java/com/configcat/ConfigFetcher.java | 10 ++++++---- 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/configcat/ConfigCatClient.java b/src/main/java/com/configcat/ConfigCatClient.java index cac97bb..75b7314 100644 --- a/src/main/java/com/configcat/ConfigCatClient.java +++ b/src/main/java/com/configcat/ConfigCatClient.java @@ -5,6 +5,7 @@ import org.slf4j.LoggerFactory; import java.io.IOException; +import java.net.Proxy; import java.util.*; import java.util.concurrent.atomic.AtomicBoolean; @@ -798,9 +799,9 @@ private boolean isBaseURLCustom() { * HTTP configuration options for a {@link ConfigCatClient} instance. */ public static class HttpOptions { - private int connectTimeoutMillis = 20000; - private int readTimeoutMillis = 20000; - private String proxyUrl; + private int connectTimeoutMillis = 10000; + private int readTimeoutMillis = 10000; + private Proxy proxy; /** * Sets HTTP connect timeout in milliseconds. @@ -823,12 +824,12 @@ public HttpOptions readTimeoutMillis(int readTimeoutMillis) { } /** - * Sets the HTTP proxy url. + * Sets the HTTP proxy. * - * @param proxyUrl the HTTP proxy url. + * @param proxy the HTTP proxy. */ - public HttpOptions proxyUrl(String proxyUrl) { - this.proxyUrl = proxyUrl; + public HttpOptions proxy(Proxy proxy) { + this.proxy = proxy; return this; } @@ -840,8 +841,8 @@ int getReadTimeoutMillis() { return readTimeoutMillis; } - String getProxyUrl() { - return proxyUrl; + Proxy getProxy() { + return proxy; } } } \ No newline at end of file diff --git a/src/main/java/com/configcat/ConfigFetcher.java b/src/main/java/com/configcat/ConfigFetcher.java index fb36479..8f0463a 100644 --- a/src/main/java/com/configcat/ConfigFetcher.java +++ b/src/main/java/com/configcat/ConfigFetcher.java @@ -121,7 +121,7 @@ private CompletableFuture executeFetchAsync(int executionCount, S } String newUrl = config.getPreferences().getBaseUrl(); - if (newUrl.equals(this.url)) { + if (newUrl == null || newUrl.equals(this.url)) { return CompletableFuture.completedFuture(fetchResponse); } @@ -168,14 +168,16 @@ private void callHTTP(String previousETag, CompletableFuture resu try { URL fetchUrl = new URL(requestUrl); - urlConnection = (HttpURLConnection) fetchUrl.openConnection(); + if (httpOptions.getProxy() != null) { + urlConnection = (HttpURLConnection) fetchUrl.openConnection(httpOptions.getProxy()); + } else { + urlConnection = (HttpURLConnection) fetchUrl.openConnection(); + } urlConnection.setConnectTimeout(httpOptions.getConnectTimeoutMillis()); urlConnection.setReadTimeout(httpOptions.getReadTimeoutMillis()); urlConnection.setUseCaches(false); urlConnection.setInstanceFollowRedirects(false); - urlConnection.setRequestMethod("GET"); urlConnection.setRequestProperty("X-ConfigCat-UserAgent", "ConfigCat-Droid/" + this.mode + "-" + Constants.VERSION); - urlConnection.setDoOutput(true); if (previousETag != null && !previousETag.isEmpty()) urlConnection.setRequestProperty("If-None-Match", previousETag); From a2eecbaf27377d3901cd29c35b11c75574081f6b Mon Sep 17 00:00:00 2001 From: Peter Csajtai Date: Fri, 8 Aug 2025 18:49:52 +0200 Subject: [PATCH 03/17] Bump version --- gradle.properties | 2 +- src/main/java/com/configcat/Utils.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/gradle.properties b/gradle.properties index d0ec49d..93929ce 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,3 +1,3 @@ -version=11.0.0 +version=11.0.1 org.gradle.jvmargs=-Xmx2g \ No newline at end of file diff --git a/src/main/java/com/configcat/Utils.java b/src/main/java/com/configcat/Utils.java index a118b0a..fa44ff1 100644 --- a/src/main/java/com/configcat/Utils.java +++ b/src/main/java/com/configcat/Utils.java @@ -56,7 +56,7 @@ private Constants() { /* prevent from instantiation*/ } static final long DISTANT_PAST = 0; static final String CONFIG_JSON_NAME = "config_v6.json"; static final String SERIALIZATION_FORMAT_VERSION = "v2"; - static final String VERSION = "11.0.0"; + static final String VERSION = "11.0.1"; static final String SDK_KEY_PROXY_PREFIX = "configcat-proxy/"; static final String SDK_KEY_PREFIX = "configcat-sdk-1"; From b084f8a431244aaa99d2368aa5a1fdf763c0c3a8 Mon Sep 17 00:00:00 2001 From: Peter Csajtai Date: Wed, 13 Aug 2025 16:49:14 +0200 Subject: [PATCH 04/17] Update android-ci.yml --- .github/workflows/android-ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/android-ci.yml b/.github/workflows/android-ci.yml index 7fc8801..8942b8b 100644 --- a/.github/workflows/android-ci.yml +++ b/.github/workflows/android-ci.yml @@ -2,7 +2,7 @@ name: Android CI on: schedule: - - cron: '0 0 * * *' + - cron: '0 8 * * 1' push: branches: [ '*' ] tags: [ 'v[0-9]+.[0-9]+.[0-9]+' ] From 5824bf4ce5f91d05ebe10437a98390cc9cd496e5 Mon Sep 17 00:00:00 2001 From: Peter Csajtai Date: Fri, 5 Sep 2025 18:34:53 +0200 Subject: [PATCH 05/17] Add background / network state monitoring --- gradle.properties | 2 +- src/main/AndroidManifest.xml | 1 + .../java/com/configcat/AppStateMonitor.java | 145 ++++++++++++++++++ .../java/com/configcat/ConfigCatClient.java | 13 +- .../java/com/configcat/ConfigService.java | 58 ++++--- .../java/com/configcat/AutoPollingTest.java | 26 ++-- .../java/com/configcat/ConfigFetcherTest.java | 4 +- .../java/com/configcat/LazyLoadingTest.java | 12 +- .../java/com/configcat/ManualPollingTest.java | 10 +- 9 files changed, 220 insertions(+), 51 deletions(-) create mode 100644 src/main/java/com/configcat/AppStateMonitor.java diff --git a/gradle.properties b/gradle.properties index 93929ce..1d7bef0 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,3 +1,3 @@ -version=11.0.1 +version=11.0.3 org.gradle.jvmargs=-Xmx2g \ No newline at end of file diff --git a/src/main/AndroidManifest.xml b/src/main/AndroidManifest.xml index bd5df81..74a98bc 100644 --- a/src/main/AndroidManifest.xml +++ b/src/main/AndroidManifest.xml @@ -2,5 +2,6 @@ + \ No newline at end of file diff --git a/src/main/java/com/configcat/AppStateMonitor.java b/src/main/java/com/configcat/AppStateMonitor.java new file mode 100644 index 0000000..b137bba --- /dev/null +++ b/src/main/java/com/configcat/AppStateMonitor.java @@ -0,0 +1,145 @@ +package com.configcat; + +import android.app.Activity; +import android.app.Application; +import android.content.*; +import android.content.res.Configuration; +import android.net.ConnectivityManager; +import android.net.NetworkInfo; +import android.os.Bundle; +import java9.util.function.Consumer; + +import java.io.Closeable; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.locks.ReentrantReadWriteLock; + +interface StateMonitor extends Closeable { + void addStateChangeListener(Consumer listener); + boolean isNetworkAvailable(); +} + +class AppStateMonitor extends BroadcastReceiver implements Application.ActivityLifecycleCallbacks, ComponentCallbacks2, StateMonitor { + static final String CONNECTIVITY_CHANGE = "android.net.conn.CONNECTIVITY_CHANGE"; + + private final Context context; + private final Application application; + private final ConfigCatLogger logger; + private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); + private final List> stateChangeListeners = new ArrayList<>(); + private final AtomicBoolean inForeground = new AtomicBoolean(true); + private final AtomicBoolean hasNetwork = new AtomicBoolean(true); + + public AppStateMonitor(Context context, ConfigCatLogger logger) { + this.context = context; + this.logger = logger; + + application = (Application) context.getApplicationContext(); + application.registerActivityLifecycleCallbacks(this); + application.registerComponentCallbacks(this); + + IntentFilter filter = new IntentFilter(CONNECTIVITY_CHANGE); + application.registerReceiver(this, filter); + } + + public void addStateChangeListener(Consumer listener) { + lock.writeLock().lock(); + try { + this.stateChangeListeners.add(listener); + } finally { + lock.writeLock().unlock(); + } + } + + private void notifyListeners() { + logger.debug(String.format("App state has been changed, in foreground: %s, has network: %s", inForeground.get(), hasNetwork.get())); + lock.readLock().lock(); + try { + for (Consumer listener : this.stateChangeListeners) { + listener.accept(inForeground.get() && hasNetwork.get()); + } + } finally { + lock.readLock().unlock(); + } + } + + @Override + public void onActivityCreated(Activity activity, Bundle bundle) { + if (inForeground.compareAndSet(false, true)) { + notifyListeners(); + } + } + + @Override + public void onActivityStarted(Activity activity) { + if (inForeground.compareAndSet(false, true)) { + notifyListeners(); + } + } + + @Override + public void onActivityResumed(Activity activity) { + if (inForeground.compareAndSet(false, true)) { + notifyListeners(); + } + } + + @Override + public void onActivityPaused(Activity activity) {} + + @Override + public void onActivityStopped(Activity activity) {} + + @Override + public void onActivitySaveInstanceState(Activity activity, Bundle bundle) {} + + @Override + public void onActivityDestroyed(Activity activity) {} + + @Override + public void onReceive(Context context, Intent intent) { + boolean isConnected = isNetworkAvailable(); + if (isConnected && hasNetwork.compareAndSet(false, true)) { + notifyListeners(); + } else if (!isConnected && hasNetwork.compareAndSet(true, false)) { + notifyListeners(); + } + } + + @Override + public void onTrimMemory(int i) { + if (i == ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) { // We're in the background + if (inForeground.compareAndSet(true, false)) { + notifyListeners(); + } + } + } + + @Override + public void onConfigurationChanged(Configuration configuration) {} + + @Override + public void onLowMemory() {} + + public boolean isNetworkAvailable() { + ConnectivityManager conn = + (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); + NetworkInfo networkInfo = conn.getActiveNetworkInfo(); + return networkInfo != null && networkInfo.isConnected(); + } + + @Override + public void close() throws IOException { + lock.writeLock().lock(); + try { + stateChangeListeners.clear(); + } finally { + lock.writeLock().unlock(); + } + application.unregisterReceiver(this); + application.unregisterActivityLifecycleCallbacks(this); + application.unregisterComponentCallbacks(this); + } +} diff --git a/src/main/java/com/configcat/ConfigCatClient.java b/src/main/java/com/configcat/ConfigCatClient.java index 75b7314..adf6c95 100644 --- a/src/main/java/com/configcat/ConfigCatClient.java +++ b/src/main/java/com/configcat/ConfigCatClient.java @@ -52,7 +52,8 @@ private ConfigCatClient(String sdkKey, Options options) throws IllegalArgumentEx options.isBaseURLCustom(), options.pollingMode.getPollingIdentifier()); - this.configService = new ConfigService(sdkKey, options.pollingMode, options.cache, logger, fetcher, options.hooks, options.offline); + StateMonitor monitor = options.context != null ? new AppStateMonitor(options.context, logger) : null; + this.configService = new ConfigService(sdkKey, monitor, options.pollingMode, options.cache, logger, fetcher, options.hooks, options.offline); } else { this.hooks.invokeOnClientReady(ClientCacheState.HAS_LOCAL_OVERRIDE_FLAG_DATA_ONLY); } @@ -667,6 +668,7 @@ public CompletableFuture waitForReadyAsync() { * Configuration options for a {@link ConfigCatClient} instance. */ public static class Options { + private android.content.Context context; private ConfigCache cache = new NullConfigCache(); private String baseUrl; private PollingMode pollingMode = PollingModes.autoPoll(60); @@ -790,6 +792,15 @@ public void logFilter(LogFilterFunction logFilter) { this.logFilter = logFilter; } + /** + * Sets the Android context used by the SDK. + * + * @param context the Android {@link android.content.Context} instance to be used. + */ + public void context(android.content.Context context) { + this.context = context; + } + private boolean isBaseURLCustom() { return this.baseUrl != null && !this.baseUrl.isEmpty(); } diff --git a/src/main/java/com/configcat/ConfigService.java b/src/main/java/com/configcat/ConfigService.java index 9f063cb..a032b2c 100644 --- a/src/main/java/com/configcat/ConfigService.java +++ b/src/main/java/com/configcat/ConfigService.java @@ -45,8 +45,10 @@ class ConfigService implements Closeable { private CompletableFuture> runningTask; private final AtomicBoolean initialized = new AtomicBoolean(false); private final AtomicBoolean offline; + private final AtomicBoolean inForegroundAndHasNetwork; private final AtomicBoolean closed = new AtomicBoolean(false); private final String cacheKey; + private final StateMonitor stateMonitor; private final PollingMode mode; private final ConfigCache cache; private final ConfigCatLogger logger; @@ -55,6 +57,7 @@ class ConfigService implements Closeable { private final ReentrantLock lock = new ReentrantLock(true); public ConfigService(String sdkKey, + StateMonitor stateMonitor, PollingMode mode, ConfigCache cache, ConfigCatLogger logger, @@ -62,6 +65,7 @@ public ConfigService(String sdkKey, ConfigCatHooks hooks, boolean offline) { this.cacheKey = Utils.sha1(String.format(CACHE_BASE, sdkKey)); + this.stateMonitor = stateMonitor; this.mode = mode; this.cache = cache; this.logger = logger; @@ -69,6 +73,19 @@ public ConfigService(String sdkKey, this.hooks = hooks; this.offline = new AtomicBoolean(offline); + this.inForegroundAndHasNetwork = new AtomicBoolean(stateMonitor == null || stateMonitor.isNetworkAvailable()); + + if (stateMonitor != null) { + stateMonitor.addStateChangeListener(state -> { + this.inForegroundAndHasNetwork.set(state); + if (!state) { + setOffline(); + } else { + setOnline(); + } + }); + } + if (mode instanceof AutoPollingMode && !offline) { AutoPollingMode autoPollingMode = (AutoPollingMode) mode; @@ -127,28 +144,16 @@ public CompletableFuture refresh() { } public void setOnline() { - lock.lock(); - try { - if (!offline.compareAndSet(true, false)) return; - if (mode instanceof AutoPollingMode) { - startPoll((AutoPollingMode) mode); - } - logger.info(5200, ConfigCatLogMessages.getConfigServiceStatusChanged("ONLINE")); - } finally { - lock.unlock(); + if (!offline.compareAndSet(true, false)) return; + if (mode instanceof AutoPollingMode) { + startPoll((AutoPollingMode) mode); } + logger.info(5200, ConfigCatLogMessages.getConfigServiceStatusChanged("ONLINE")); } public void setOffline() { - lock.lock(); - try { - if (!offline.compareAndSet(false, true)) return; - if (pollScheduler != null) pollScheduler.shutdown(); - if (initScheduler != null) initScheduler.shutdown(); - logger.info(5200, ConfigCatLogMessages.getConfigServiceStatusChanged("OFFLINE")); - } finally { - lock.unlock(); - } + if (!offline.compareAndSet(false, true)) return; + logger.info(5200, ConfigCatLogMessages.getConfigServiceStatusChanged("OFFLINE")); } public boolean isOffline() { @@ -170,7 +175,7 @@ private CompletableFuture> fetchIfOlder(long threshold, boolean pr return CompletableFuture.completedFuture(Result.success(cachedEntry)); } // If we are in offline mode or the caller prefers cached values, do not initiate fetch. - if (offline.get() || preferCached) { + if (offline.get() || !inForegroundAndHasNetwork.get() || preferCached) { return CompletableFuture.completedFuture(Result.success(cachedEntry)); } @@ -224,10 +229,16 @@ private void setInitialized() { } private void startPoll(AutoPollingMode mode) { - long ageThreshold = (mode.getAutoPollRateInSeconds() * 1000L) - 500; - pollScheduler = Executors.newSingleThreadScheduledExecutor(); - pollScheduler.scheduleWithFixedDelay(() -> this.fetchIfOlder(System.currentTimeMillis() - ageThreshold, false), - 0, mode.getAutoPollRateInSeconds(), TimeUnit.SECONDS); + lock.lock(); + try { + long ageThreshold = (mode.getAutoPollRateInSeconds() * 1000L) - 500; + if (pollScheduler != null) pollScheduler.shutdown(); + pollScheduler = Executors.newSingleThreadScheduledExecutor(); + pollScheduler.scheduleWithFixedDelay(() -> this.fetchIfOlder(System.currentTimeMillis() - ageThreshold, false), + 0, mode.getAutoPollRateInSeconds(), TimeUnit.SECONDS); + } finally { + lock.unlock(); + } } private void writeCache(Entry entry) { @@ -280,6 +291,7 @@ public void close() throws IOException { } if (pollScheduler != null) pollScheduler.shutdown(); if (initScheduler != null) initScheduler.shutdown(); + if (stateMonitor != null) stateMonitor.close(); fetcher.close(); } } diff --git a/src/test/java/com/configcat/AutoPollingTest.java b/src/test/java/com/configcat/AutoPollingTest.java index 374259c..7e2e1ab 100644 --- a/src/test/java/com/configcat/AutoPollingTest.java +++ b/src/test/java/com/configcat/AutoPollingTest.java @@ -51,7 +51,7 @@ void get() throws InterruptedException, ExecutionException, IOException { this.server.url("/").toString(), false, pollingMode.getPollingIdentifier()); - ConfigService policy = new ConfigService("", pollingMode, cache, logger, fetcher, new ConfigCatHooks(), false); + ConfigService policy = new ConfigService("", null, pollingMode, cache, logger, fetcher, new ConfigCatHooks(), false); //first call assertEquals("test", policy.getSettings().get().settings().get("fakeKey").getSettingsValue().getStringValue()); @@ -77,7 +77,7 @@ void getFail() throws InterruptedException, ExecutionException, IOException { this.server.url("/").toString(), false, pollingMode.getPollingIdentifier()); - ConfigService policy = new ConfigService("", pollingMode, cache, logger, fetcher, new ConfigCatHooks(), false); + ConfigService policy = new ConfigService("", null, pollingMode, cache, logger, fetcher, new ConfigCatHooks(), false); //first call assertTrue(policy.getSettings().get().settings().isEmpty()); @@ -100,7 +100,7 @@ void getMany() throws InterruptedException, ExecutionException, IOException { this.server.url("/").toString(), false, pollingMode.getPollingIdentifier()); - ConfigService policy = new ConfigService("", pollingMode, cache, logger, fetcher, new ConfigCatHooks(), false); + ConfigService policy = new ConfigService("", null, pollingMode, cache, logger, fetcher, new ConfigCatHooks(), false); //first calls assertEquals("test", policy.getSettings().get().settings().get("fakeKey").getSettingsValue().getStringValue()); @@ -141,7 +141,7 @@ void getWithFailedRefresh() throws InterruptedException, ExecutionException, IOE this.server.url("/").toString(), false, pollingMode.getPollingIdentifier()); - ConfigService policy = new ConfigService("", pollingMode, cache, logger, fetcher, new ConfigCatHooks(), false); + ConfigService policy = new ConfigService("", null, pollingMode, cache, logger, fetcher, new ConfigCatHooks(), false); //first call assertEquals("test", policy.getSettings().get().settings().get("fakeKey").getSettingsValue().getStringValue()); @@ -171,7 +171,7 @@ void getCacheFails() throws Exception { this.server.url("/").toString(), false, pollingMode.getPollingIdentifier()); - ConfigService policy = new ConfigService("", pollingMode, cache, logger, fetcher, new ConfigCatHooks(), false); + ConfigService policy = new ConfigService("", null, pollingMode, cache, logger, fetcher, new ConfigCatHooks(), false); assertEquals("test", policy.getSettings().get().settings().get("fakeKey").getSettingsValue().getStringValue()); @@ -189,7 +189,7 @@ void testInitWaitTimeTimeout() throws Exception { this.server.url("/").toString(), false, pollingMode.getPollingIdentifier()); - ConfigService policy = new ConfigService("", pollingMode, new NullConfigCache(), logger, fetcher, new ConfigCatHooks(), false); + ConfigService policy = new ConfigService("", null, pollingMode, new NullConfigCache(), logger, fetcher, new ConfigCatHooks(), false); long start = System.currentTimeMillis(); assertTrue(policy.getSettings().get().settings().isEmpty()); @@ -212,7 +212,7 @@ void testPollIntervalRespectsCacheExpiration() throws Exception { this.server.url("/").toString(), false, pollingMode.getPollingIdentifier()); - ConfigService policy = new ConfigService("", pollingMode, cache, logger, fetcher, new ConfigCatHooks(), false); + ConfigService policy = new ConfigService("", null, pollingMode, cache, logger, fetcher, new ConfigCatHooks(), false); policy.getSettings().get(); @@ -236,7 +236,7 @@ void testPollsWhenCacheExpired() throws Exception { this.server.url("/").toString(), false, pollingMode.getPollingIdentifier()); - ConfigService configService = new ConfigService("", pollingMode, cache, logger, fetcher, new ConfigCatHooks(), false); + ConfigService configService = new ConfigService("", null, pollingMode, cache, logger, fetcher, new ConfigCatHooks(), false); configService.getSettings().get(); @@ -260,7 +260,7 @@ void testNonExpiredCacheCallsReady() throws Exception { this.server.url("/").toString(), false, pollingMode.getPollingIdentifier()); - ConfigService policy = new ConfigService("", pollingMode, cache, logger, fetcher, hooks, false); + ConfigService policy = new ConfigService("", null, pollingMode, cache, logger, fetcher, hooks, false); assertEquals(0, this.server.getRequestCount()); @@ -283,7 +283,7 @@ void testOnlineOffline() throws Exception { this.server.url("/").toString(), false, pollingMode.getPollingIdentifier()); - ConfigService policy = new ConfigService("", pollingMode, new NullConfigCache(), logger, fetcher, new ConfigCatHooks(), false); + ConfigService policy = new ConfigService("", null, pollingMode, new NullConfigCache(), logger, fetcher, new ConfigCatHooks(), false); Thread.sleep(1500); @@ -314,7 +314,7 @@ void testInitOffline() throws Exception { this.server.url("/").toString(), false, pollingMode.getPollingIdentifier()); - ConfigService policy = new ConfigService("", pollingMode, new NullConfigCache(), logger, fetcher, new ConfigCatHooks(), true); + ConfigService policy = new ConfigService("", null, pollingMode, new NullConfigCache(), logger, fetcher, new ConfigCatHooks(), true); assertTrue(policy.isOffline()); assertEquals(0, this.server.getRequestCount()); @@ -343,7 +343,7 @@ void testInitWaitTimeIgnoredWhenCacheIsNotExpired() throws Exception { this.server.url("/").toString(), false, pollingMode.getPollingIdentifier()); - ConfigService policy = new ConfigService("", pollingMode, cache, logger, fetcher, new ConfigCatHooks(), false); + ConfigService policy = new ConfigService("", null, pollingMode, cache, logger, fetcher, new ConfigCatHooks(), false); long start = System.currentTimeMillis(); assertFalse(policy.getSettings().get().settings().isEmpty()); @@ -366,7 +366,7 @@ void testInitWaitTimeReturnCached() throws Exception { this.server.url("/").toString(), false, pollingMode.getPollingIdentifier()); - ConfigService policy = new ConfigService("", pollingMode, cache, logger, fetcher, new ConfigCatHooks(), false); + ConfigService policy = new ConfigService("", null, pollingMode, cache, logger, fetcher, new ConfigCatHooks(), false); long start = System.currentTimeMillis(); assertEquals("test", policy.getSettings().get().settings().get("fakeKey").getSettingsValue().getStringValue()); diff --git a/src/test/java/com/configcat/ConfigFetcherTest.java b/src/test/java/com/configcat/ConfigFetcherTest.java index df24e0f..6ece674 100644 --- a/src/test/java/com/configcat/ConfigFetcherTest.java +++ b/src/test/java/com/configcat/ConfigFetcherTest.java @@ -98,7 +98,7 @@ void fetchedETagNotUpdatesCache() throws Exception { ConfigFetcher fetcher = new ConfigFetcher(new ConfigCatClient.HttpOptions(), logger, "", this.server.url("/").toString(), false, PollingModes.manualPoll().getPollingIdentifier()); - ConfigService policy = new ConfigService("", PollingModes.autoPoll(2), cache, logger, fetcher, new ConfigCatHooks(), false); + ConfigService policy = new ConfigService("", null, PollingModes.autoPoll(2), cache, logger, fetcher, new ConfigCatHooks(), false); assertEquals("fakeValue", policy.getSettings().get().settings().get("fakeKey").getSettingsValue().getStringValue()); verify(cache, never()).write(anyString(), eq(TEST_JSON)); @@ -115,7 +115,7 @@ void fetchedSameResponseNotUpdatesCache() throws Exception { ConfigFetcher fetcher = new ConfigFetcher(new ConfigCatClient.HttpOptions(), logger, "", this.server.url("/").toString(), false, PollingModes.manualPoll().getPollingIdentifier()); - ConfigService policy = new ConfigService("", PollingModes.autoPoll(2), cache, logger, fetcher, new ConfigCatHooks(), false); + ConfigService policy = new ConfigService("", null, PollingModes.autoPoll(2), cache, logger, fetcher, new ConfigCatHooks(), false); assertEquals("fakeValue", policy.getSettings().get().settings().get("fakeKey").getSettingsValue().getStringValue()); verify(cache, never()).write(anyString(), eq(TEST_JSON)); diff --git a/src/test/java/com/configcat/LazyLoadingTest.java b/src/test/java/com/configcat/LazyLoadingTest.java index 107a1eb..0d51769 100644 --- a/src/test/java/com/configcat/LazyLoadingTest.java +++ b/src/test/java/com/configcat/LazyLoadingTest.java @@ -29,7 +29,7 @@ public void setUp() throws IOException { PollingMode mode = PollingModes .lazyLoad(5); ConfigFetcher fetcher = new ConfigFetcher(new ConfigCatClient.HttpOptions(), logger, "", this.server.url("/").toString(), false, mode.getPollingIdentifier()); - this.policy = new ConfigService("", mode, new NullConfigCache(), logger, fetcher, new ConfigCatHooks(), false); + this.policy = new ConfigService("", null, mode, new NullConfigCache(), logger, fetcher, new ConfigCatHooks(), false); } @AfterEach @@ -58,7 +58,7 @@ void getCacheFails() throws InterruptedException, ExecutionException { PollingMode mode = PollingModes .lazyLoad(5); ConfigFetcher fetcher = new ConfigFetcher(new ConfigCatClient.HttpOptions(), logger, "", this.server.url("/").toString(), false, mode.getPollingIdentifier()); - ConfigService lPolicy = new ConfigService("", mode, new NullConfigCache(), logger, fetcher, new ConfigCatHooks(), false); + ConfigService lPolicy = new ConfigService("", null, mode, new NullConfigCache(), logger, fetcher, new ConfigCatHooks(), false); this.server.enqueue(new MockResponse().setResponseCode(200).setBody(String.format(TEST_JSON, "test"))); this.server.enqueue(new MockResponse().setResponseCode(200).setBody(String.format(TEST_JSON, "test2")).setBodyDelay(3, TimeUnit.SECONDS)); @@ -97,7 +97,7 @@ void testCacheExpirationRespectedInTTLCalc() throws InterruptedException, Execut PollingMode mode = PollingModes .lazyLoad(1); ConfigFetcher fetcher = new ConfigFetcher(new ConfigCatClient.HttpOptions(), logger, "", this.server.url("/").toString(), false, mode.getPollingIdentifier()); - ConfigService service = new ConfigService("", mode, cache, logger, fetcher, new ConfigCatHooks(), false); + ConfigService service = new ConfigService("", null, mode, cache, logger, fetcher, new ConfigCatHooks(), false); assertFalse(service.getSettings().get().settings().isEmpty()); assertFalse(service.getSettings().get().settings().isEmpty()); @@ -121,7 +121,7 @@ void testCacheExpirationRespectedInTTLCalc304() throws InterruptedException, Exe PollingMode mode = PollingModes .lazyLoad(1); ConfigFetcher fetcher = new ConfigFetcher(new ConfigCatClient.HttpOptions(), logger, "", this.server.url("/").toString(), false, mode.getPollingIdentifier()); - ConfigService service = new ConfigService("", mode, cache, logger, fetcher, new ConfigCatHooks(), false); + ConfigService service = new ConfigService("", null, mode, cache, logger, fetcher, new ConfigCatHooks(), false); assertFalse(service.getSettings().get().settings().isEmpty()); assertFalse(service.getSettings().get().settings().isEmpty()); @@ -148,7 +148,7 @@ void testOnlineOffline() throws Exception { this.server.url("/").toString(), false, pollingMode.getPollingIdentifier()); - ConfigService service = new ConfigService("", pollingMode, new NullConfigCache(), logger, fetcher, new ConfigCatHooks(), false); + ConfigService service = new ConfigService("", null, pollingMode, new NullConfigCache(), logger, fetcher, new ConfigCatHooks(), false); assertFalse(service.getSettings().get().settings().isEmpty()); assertEquals(1, this.server.getRequestCount()); @@ -182,7 +182,7 @@ void testInitOffline() throws Exception { this.server.url("/").toString(), false, pollingMode.getPollingIdentifier()); - ConfigService service = new ConfigService("", pollingMode, new NullConfigCache(), logger, fetcher, new ConfigCatHooks(), true); + ConfigService service = new ConfigService("", null, pollingMode, new NullConfigCache(), logger, fetcher, new ConfigCatHooks(), true); assertTrue(service.getSettings().get().settings().isEmpty()); assertEquals(0, this.server.getRequestCount()); diff --git a/src/test/java/com/configcat/ManualPollingTest.java b/src/test/java/com/configcat/ManualPollingTest.java index 281acbe..a65ba67 100644 --- a/src/test/java/com/configcat/ManualPollingTest.java +++ b/src/test/java/com/configcat/ManualPollingTest.java @@ -27,7 +27,7 @@ public void setUp() throws IOException { PollingMode mode = PollingModes.manualPoll(); ConfigFetcher fetcher = new ConfigFetcher(new ConfigCatClient.HttpOptions(), logger, "", this.server.url("/").toString(), false, mode.getPollingIdentifier()); - this.policy = new ConfigService("", mode, new NullConfigCache(), logger, fetcher, new ConfigCatHooks(), false); + this.policy = new ConfigService("", null, mode, new NullConfigCache(), logger, fetcher, new ConfigCatHooks(), false); } @AfterEach @@ -54,7 +54,7 @@ void get() throws InterruptedException, ExecutionException { void getCacheFails() throws InterruptedException, ExecutionException, IOException { PollingMode mode = PollingModes.manualPoll(); ConfigFetcher fetcher = new ConfigFetcher(new ConfigCatClient.HttpOptions(), logger, "", this.server.url("/").toString(), false, mode.getPollingIdentifier()); - ConfigService lPolicy = new ConfigService("", mode, new NullConfigCache(), logger, fetcher, new ConfigCatHooks(), false); + ConfigService lPolicy = new ConfigService("", null, mode, new NullConfigCache(), logger, fetcher, new ConfigCatHooks(), false); this.server.enqueue(new MockResponse().setResponseCode(200).setBody(String.format(TEST_JSON, "test"))); this.server.enqueue(new MockResponse().setResponseCode(200).setBody(String.format(TEST_JSON, "test2")).setBodyDelay(2, TimeUnit.SECONDS)); @@ -92,7 +92,7 @@ void testCache() throws InterruptedException, ExecutionException, IOException { InMemoryCache cache = new InMemoryCache(); PollingMode mode = PollingModes.manualPoll(); ConfigFetcher fetcher = new ConfigFetcher(new ConfigCatClient.HttpOptions(), logger, "", this.server.url("/").toString(), false, mode.getPollingIdentifier()); - ConfigService service = new ConfigService("", mode, cache, logger, fetcher, new ConfigCatHooks(), false); + ConfigService service = new ConfigService("", null, mode, cache, logger, fetcher, new ConfigCatHooks(), false); service.refresh().get(); assertEquals("test", service.getSettings().get().settings().get("fakeKey").getSettingsValue().getStringValue()); @@ -125,7 +125,7 @@ void testOnlineOffline() throws Exception { this.server.url("/").toString(), false, pollingMode.getPollingIdentifier()); - ConfigService service = new ConfigService("", pollingMode, new NullConfigCache(), logger, fetcher, new ConfigCatHooks(), false); + ConfigService service = new ConfigService("", null, pollingMode, new NullConfigCache(), logger, fetcher, new ConfigCatHooks(), false); assertFalse(service.isOffline()); assertTrue(service.refresh().get().isSuccess()); @@ -158,7 +158,7 @@ void testInitOffline() throws Exception { this.server.url("/").toString(), false, pollingMode.getPollingIdentifier()); - ConfigService service = new ConfigService("", pollingMode, new NullConfigCache(), logger, fetcher, new ConfigCatHooks(), true); + ConfigService service = new ConfigService("", null, pollingMode, new NullConfigCache(), logger, fetcher, new ConfigCatHooks(), true); assertTrue(service.isOffline()); assertFalse(service.refresh().get().isSuccess()); From 2088e8b972909a2d9176a6a0627890b29dec7e49 Mon Sep 17 00:00:00 2001 From: Peter Csajtai Date: Tue, 16 Dec 2025 16:10:20 +0100 Subject: [PATCH 06/17] Finalize app state monitor --- gradle.properties | 2 +- samples/android-java/app/build.gradle | 2 +- .../configcatsample/MainActivity.java | 13 +- .../gradle/wrapper/gradle-wrapper.properties | 7 +- samples/android-java/gradlew | 282 +++++++++------- samples/android-java/gradlew.bat | 15 +- samples/android-kotlin/app/build.gradle | 2 +- .../configcat/configcatsample/MainActivity.kt | 4 + .../gradle/wrapper/gradle-wrapper.properties | 6 +- samples/android-kotlin/gradlew | 300 +++++++++++------- samples/android-kotlin/gradlew.bat | 66 ++-- .../java/com/configcat/ConfigCatClient.java | 6 +- .../java/com/configcat/ConfigService.java | 49 +-- .../java/com/configcat/ConfigFetcherTest.java | 41 +++ .../java/com/configcat/TestStateMonitor.java | 55 ++++ 15 files changed, 557 insertions(+), 293 deletions(-) create mode 100644 src/test/java/com/configcat/TestStateMonitor.java diff --git a/gradle.properties b/gradle.properties index 1d7bef0..f795f2f 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,3 +1,3 @@ -version=11.0.3 +version=11.1.0 org.gradle.jvmargs=-Xmx2g \ No newline at end of file diff --git a/samples/android-java/app/build.gradle b/samples/android-java/app/build.gradle index 783fed9..1f72c39 100644 --- a/samples/android-java/app/build.gradle +++ b/samples/android-java/app/build.gradle @@ -36,7 +36,7 @@ dependencies { implementation 'androidx.lifecycle:lifecycle-viewmodel:2.4.0' implementation 'com.google.android.material:material:1.5.0' implementation 'androidx.constraintlayout:constraintlayout:2.1.4' - implementation 'com.configcat:configcat-android-client:10.0.0' + implementation 'com.configcat:configcat-android-client:11.1.0' implementation 'org.slf4j:slf4j-api:2.0.7' implementation 'com.github.tony19:logback-android:3.0.0' } \ No newline at end of file diff --git a/samples/android-java/app/src/main/java/com/configcat/configcatsample/MainActivity.java b/samples/android-java/app/src/main/java/com/configcat/configcatsample/MainActivity.java index ff90099..9716ece 100644 --- a/samples/android-java/app/src/main/java/com/configcat/configcatsample/MainActivity.java +++ b/samples/android-java/app/src/main/java/com/configcat/configcatsample/MainActivity.java @@ -6,10 +6,7 @@ import androidx.appcompat.app.AppCompatActivity; -import com.configcat.ConfigCatClient; -import com.configcat.LogLevel; -import com.configcat.SharedPreferencesCache; -import com.configcat.User; +import com.configcat.*; public class MainActivity extends AppCompatActivity { @@ -22,9 +19,15 @@ protected void onCreate(Bundle savedInstanceState) { setContentView(R.layout.activity_main); client = ConfigCatClient.get("PKDVCLf-Hq-h-kCzMp-L7Q/HhOWfwVtZ0mb30i9wi17GQ", options -> { + options.pollingMode(PollingModes.autoPoll(5)); + // Use ConfigCat's shared preferences cache. options.cache(new SharedPreferencesCache(getApplicationContext())); + // With this option, the SDK automatically switches between offline and online modes based on + // whether the application is in the foreground or background and on network availability. + options.watchAppStateChanges(getApplicationContext()); + // Info level logging helps to inspect the feature flag evaluation process. // Use the default Warning level to avoid too detailed logging in your application. options.logLevel(LogLevel.DEBUG); @@ -42,7 +45,7 @@ private void fetchNewConfig() { .thenAccept(value -> { this.runOnUiThread(() -> { TextView viewById = this.findViewById(R.id.editText); - viewById.setText("isPOCFeatureEnabled: " + value); + viewById.setText(String.format("isPOCFeatureEnabled: %s", value)); }); }); } diff --git a/samples/android-java/gradle/wrapper/gradle-wrapper.properties b/samples/android-java/gradle/wrapper/gradle-wrapper.properties index 1d4841f..1af9e09 100644 --- a/samples/android-java/gradle/wrapper/gradle-wrapper.properties +++ b/samples/android-java/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,7 @@ -#Thu Jan 05 11:38:37 CET 2023 distributionBase=GRADLE_USER_HOME -distributionUrl=https\://services.gradle.org/distributions/gradle-7.4-bin.zip distributionPath=wrapper/dists -zipStorePath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-8.5-bin.zip +networkTimeout=10000 +validateDistributionUrl=true zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists diff --git a/samples/android-java/gradlew b/samples/android-java/gradlew index 4f906e0..1aa94a4 100644 --- a/samples/android-java/gradlew +++ b/samples/android-java/gradlew @@ -1,7 +1,7 @@ -#!/usr/bin/env sh +#!/bin/sh # -# Copyright 2015 the original author or authors. +# Copyright © 2015-2021 the original authors. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -17,67 +17,99 @@ # ############################################################################## -## -## Gradle start up script for UN*X -## +# +# Gradle start up script for POSIX generated by Gradle. +# +# Important for running: +# +# (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is +# noncompliant, but you have some other compliant shell such as ksh or +# bash, then to run this script, type that shell name before the whole +# command line, like: +# +# ksh Gradle +# +# Busybox and similar reduced shells will NOT work, because this script +# requires all of these POSIX shell features: +# * functions; +# * expansions «$var», «${var}», «${var:-default}», «${var+SET}», +# «${var#prefix}», «${var%suffix}», and «$( cmd )»; +# * compound commands having a testable exit status, especially «case»; +# * various built-in commands including «command», «set», and «ulimit». +# +# Important for patching: +# +# (2) This script targets any POSIX shell, so it avoids extensions provided +# by Bash, Ksh, etc; in particular arrays are avoided. +# +# The "traditional" practice of packing multiple parameters into a +# space-separated string is a well documented source of bugs and security +# problems, so this is (mostly) avoided, by progressively accumulating +# options in "$@", and eventually passing that to Java. +# +# Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS, +# and GRADLE_OPTS) rely on word-splitting, this is performed explicitly; +# see the in-line comments for details. +# +# There are tweaks for specific operating systems such as AIX, CygWin, +# Darwin, MinGW, and NonStop. +# +# (3) This script is generated from the Groovy template +# https://github.com/gradle/gradle/blob/HEAD/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt +# within the Gradle project. +# +# You can find Gradle at https://github.com/gradle/gradle/. +# ############################################################################## # Attempt to set APP_HOME + # Resolve links: $0 may be a link -PRG="$0" -# Need this for relative symlinks. -while [ -h "$PRG" ] ; do - ls=`ls -ld "$PRG"` - link=`expr "$ls" : '.*-> \(.*\)$'` - if expr "$link" : '/.*' > /dev/null; then - PRG="$link" - else - PRG=`dirname "$PRG"`"/$link" - fi +app_path=$0 + +# Need this for daisy-chained symlinks. +while + APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path + [ -h "$app_path" ] +do + ls=$( ls -ld "$app_path" ) + link=${ls#*' -> '} + case $link in #( + /*) app_path=$link ;; #( + *) app_path=$APP_HOME$link ;; + esac done -SAVED="`pwd`" -cd "`dirname \"$PRG\"`/" >/dev/null -APP_HOME="`pwd -P`" -cd "$SAVED" >/dev/null -APP_NAME="Gradle" -APP_BASE_NAME=`basename "$0"` - -# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. -DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' +# This is normally unused +# shellcheck disable=SC2034 +APP_BASE_NAME=${0##*/} +# Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036) +APP_HOME=$( cd "${APP_HOME:-./}" > /dev/null && pwd -P ) || exit # Use the maximum available, or set MAX_FD != -1 to use that value. -MAX_FD="maximum" +MAX_FD=maximum warn () { echo "$*" -} +} >&2 die () { echo echo "$*" echo exit 1 -} +} >&2 # OS specific support (must be 'true' or 'false'). cygwin=false msys=false darwin=false nonstop=false -case "`uname`" in - CYGWIN* ) - cygwin=true - ;; - Darwin* ) - darwin=true - ;; - MINGW* ) - msys=true - ;; - NONSTOP* ) - nonstop=true - ;; +case "$( uname )" in #( + CYGWIN* ) cygwin=true ;; #( + Darwin* ) darwin=true ;; #( + MSYS* | MINGW* ) msys=true ;; #( + NONSTOP* ) nonstop=true ;; esac CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar @@ -87,9 +119,9 @@ CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar if [ -n "$JAVA_HOME" ] ; then if [ -x "$JAVA_HOME/jre/sh/java" ] ; then # IBM's JDK on AIX uses strange locations for the executables - JAVACMD="$JAVA_HOME/jre/sh/java" + JAVACMD=$JAVA_HOME/jre/sh/java else - JAVACMD="$JAVA_HOME/bin/java" + JAVACMD=$JAVA_HOME/bin/java fi if [ ! -x "$JAVACMD" ] ; then die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME @@ -98,88 +130,120 @@ Please set the JAVA_HOME variable in your environment to match the location of your Java installation." fi else - JAVACMD="java" - which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + JAVACMD=java + if ! command -v java >/dev/null 2>&1 + then + die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. Please set the JAVA_HOME variable in your environment to match the location of your Java installation." + fi fi # Increase the maximum file descriptors if we can. -if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then - MAX_FD_LIMIT=`ulimit -H -n` - if [ $? -eq 0 ] ; then - if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then - MAX_FD="$MAX_FD_LIMIT" - fi - ulimit -n $MAX_FD - if [ $? -ne 0 ] ; then - warn "Could not set maximum file descriptor limit: $MAX_FD" - fi - else - warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" - fi +if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then + case $MAX_FD in #( + max*) + # In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC2039,SC3045 + MAX_FD=$( ulimit -H -n ) || + warn "Could not query maximum file descriptor limit" + esac + case $MAX_FD in #( + '' | soft) :;; #( + *) + # In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC2039,SC3045 + ulimit -n "$MAX_FD" || + warn "Could not set maximum file descriptor limit to $MAX_FD" + esac fi -# For Darwin, add options to specify how the application appears in the dock -if $darwin; then - GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" -fi +# Collect all arguments for the java command, stacking in reverse order: +# * args from the command line +# * the main class name +# * -classpath +# * -D...appname settings +# * --module-path (only if needed) +# * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables. # For Cygwin or MSYS, switch paths to Windows format before running java -if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then - APP_HOME=`cygpath --path --mixed "$APP_HOME"` - CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` - - JAVACMD=`cygpath --unix "$JAVACMD"` - - # We build the pattern for arguments to be converted via cygpath - ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` - SEP="" - for dir in $ROOTDIRSRAW ; do - ROOTDIRS="$ROOTDIRS$SEP$dir" - SEP="|" - done - OURCYGPATTERN="(^($ROOTDIRS))" - # Add a user-defined pattern to the cygpath arguments - if [ "$GRADLE_CYGPATTERN" != "" ] ; then - OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" - fi +if "$cygwin" || "$msys" ; then + APP_HOME=$( cygpath --path --mixed "$APP_HOME" ) + CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" ) + + JAVACMD=$( cygpath --unix "$JAVACMD" ) + # Now convert the arguments - kludge to limit ourselves to /bin/sh - i=0 - for arg in "$@" ; do - CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` - CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option - - if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition - eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` - else - eval `echo args$i`="\"$arg\"" + for arg do + if + case $arg in #( + -*) false ;; # don't mess with options #( + /?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath + [ -e "$t" ] ;; #( + *) false ;; + esac + then + arg=$( cygpath --path --ignore --mixed "$arg" ) fi - i=`expr $i + 1` + # Roll the args list around exactly as many times as the number of + # args, so each arg winds up back in the position where it started, but + # possibly modified. + # + # NB: a `for` loop captures its iteration list before it begins, so + # changing the positional parameters here affects neither the number of + # iterations, nor the values presented in `arg`. + shift # remove old arg + set -- "$@" "$arg" # push replacement arg done - case $i in - 0) set -- ;; - 1) set -- "$args0" ;; - 2) set -- "$args0" "$args1" ;; - 3) set -- "$args0" "$args1" "$args2" ;; - 4) set -- "$args0" "$args1" "$args2" "$args3" ;; - 5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; - 6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; - 7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; - 8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; - 9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; - esac fi -# Escape application args -save () { - for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done - echo " " -} -APP_ARGS=`save "$@"` -# Collect all arguments for the java command, following the shell quoting and substitution rules -eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' + +# Collect all arguments for the java command: +# * DEFAULT_JVM_OPTS, JAVA_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments, +# and any embedded shellness will be escaped. +# * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be +# treated as '${Hostname}' itself on the command line. + +set -- \ + "-Dorg.gradle.appname=$APP_BASE_NAME" \ + -classpath "$CLASSPATH" \ + org.gradle.wrapper.GradleWrapperMain \ + "$@" + +# Stop when "xargs" is not available. +if ! command -v xargs >/dev/null 2>&1 +then + die "xargs is not available" +fi + +# Use "xargs" to parse quoted args. +# +# With -n1 it outputs one arg per line, with the quotes and backslashes removed. +# +# In Bash we could simply go: +# +# readarray ARGS < <( xargs -n1 <<<"$var" ) && +# set -- "${ARGS[@]}" "$@" +# +# but POSIX shell has neither arrays nor command substitution, so instead we +# post-process each arg (as a line of input to sed) to backslash-escape any +# character that might be a shell metacharacter, then use eval to reverse +# that process (while maintaining the separation between arguments), and wrap +# the whole thing up as a single "set" statement. +# +# This will of course break if any of these variables contains a newline or +# an unmatched quote. +# + +eval "set -- $( + printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" | + xargs -n1 | + sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' | + tr '\n' ' ' + )" '"$@"' exec "$JAVACMD" "$@" diff --git a/samples/android-java/gradlew.bat b/samples/android-java/gradlew.bat index 107acd3..93e3f59 100644 --- a/samples/android-java/gradlew.bat +++ b/samples/android-java/gradlew.bat @@ -14,7 +14,7 @@ @rem limitations under the License. @rem -@if "%DEBUG%" == "" @echo off +@if "%DEBUG%"=="" @echo off @rem ########################################################################## @rem @rem Gradle startup script for Windows @@ -25,7 +25,8 @@ if "%OS%"=="Windows_NT" setlocal set DIRNAME=%~dp0 -if "%DIRNAME%" == "" set DIRNAME=. +if "%DIRNAME%"=="" set DIRNAME=. +@rem This is normally unused set APP_BASE_NAME=%~n0 set APP_HOME=%DIRNAME% @@ -40,7 +41,7 @@ if defined JAVA_HOME goto findJavaFromJavaHome set JAVA_EXE=java.exe %JAVA_EXE% -version >NUL 2>&1 -if "%ERRORLEVEL%" == "0" goto execute +if %ERRORLEVEL% equ 0 goto execute echo. echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. @@ -75,13 +76,15 @@ set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar :end @rem End local scope for the variables with windows NT shell -if "%ERRORLEVEL%"=="0" goto mainEnd +if %ERRORLEVEL% equ 0 goto mainEnd :fail rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of rem the _cmd.exe /c_ return code! -if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 -exit /b 1 +set EXIT_CODE=%ERRORLEVEL% +if %EXIT_CODE% equ 0 set EXIT_CODE=1 +if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE% +exit /b %EXIT_CODE% :mainEnd if "%OS%"=="Windows_NT" endlocal diff --git a/samples/android-kotlin/app/build.gradle b/samples/android-kotlin/app/build.gradle index 1bc8ca2..5871bd0 100644 --- a/samples/android-kotlin/app/build.gradle +++ b/samples/android-kotlin/app/build.gradle @@ -36,7 +36,7 @@ dependencies { implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.7.21' implementation 'androidx.appcompat:appcompat:1.5.1' implementation 'androidx.constraintlayout:constraintlayout:2.1.4' - implementation 'com.configcat:configcat-android-client:10.0.0' + implementation 'com.configcat:configcat-android-client:11.1.0' implementation 'com.noveogroup.android:android-logger:1.3.1' testImplementation 'junit:junit:4.13.2' androidTestImplementation 'androidx.test.ext:junit:1.1.4' diff --git a/samples/android-kotlin/app/src/main/java/com/configcat/configcatsample/MainActivity.kt b/samples/android-kotlin/app/src/main/java/com/configcat/configcatsample/MainActivity.kt index 8123ec9..dc62a07 100644 --- a/samples/android-kotlin/app/src/main/java/com/configcat/configcatsample/MainActivity.kt +++ b/samples/android-kotlin/app/src/main/java/com/configcat/configcatsample/MainActivity.kt @@ -20,6 +20,10 @@ class MainActivity : AppCompatActivity() { // Use ConfigCat's shared preferences cache. options.cache(SharedPreferencesCache(this@MainActivity)) + // With this option, the SDK automatically switches between offline and online modes based on + // whether the application is in the foreground or background and on network availability. + options.watchAppStateChanges(this@MainActivity) + // Info level logging helps to inspect the feature flag evaluation process. // Use the default Warning level to avoid too detailed logging in your application. options.logLevel(LogLevel.DEBUG) diff --git a/samples/android-kotlin/gradle/wrapper/gradle-wrapper.properties b/samples/android-kotlin/gradle/wrapper/gradle-wrapper.properties index 09e2b38..f398c33 100644 --- a/samples/android-kotlin/gradle/wrapper/gradle-wrapper.properties +++ b/samples/android-kotlin/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ -#Thu Sep 22 18:23:09 CEST 2022 distributionBase=GRADLE_USER_HOME -distributionUrl=https\://services.gradle.org/distributions/gradle-7.4-bin.zip distributionPath=wrapper/dists -zipStorePath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-7.6-bin.zip +networkTimeout=10000 zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists diff --git a/samples/android-kotlin/gradlew b/samples/android-kotlin/gradlew index 9d82f78..65dcd68 100644 --- a/samples/android-kotlin/gradlew +++ b/samples/android-kotlin/gradlew @@ -1,74 +1,129 @@ -#!/usr/bin/env bash +#!/bin/sh + +# +# Copyright © 2015-2021 the original authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ############################################################################## -## -## Gradle start up script for UN*X -## +# +# Gradle start up script for POSIX generated by Gradle. +# +# Important for running: +# +# (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is +# noncompliant, but you have some other compliant shell such as ksh or +# bash, then to run this script, type that shell name before the whole +# command line, like: +# +# ksh Gradle +# +# Busybox and similar reduced shells will NOT work, because this script +# requires all of these POSIX shell features: +# * functions; +# * expansions «$var», «${var}», «${var:-default}», «${var+SET}», +# «${var#prefix}», «${var%suffix}», and «$( cmd )»; +# * compound commands having a testable exit status, especially «case»; +# * various built-in commands including «command», «set», and «ulimit». +# +# Important for patching: +# +# (2) This script targets any POSIX shell, so it avoids extensions provided +# by Bash, Ksh, etc; in particular arrays are avoided. +# +# The "traditional" practice of packing multiple parameters into a +# space-separated string is a well documented source of bugs and security +# problems, so this is (mostly) avoided, by progressively accumulating +# options in "$@", and eventually passing that to Java. +# +# Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS, +# and GRADLE_OPTS) rely on word-splitting, this is performed explicitly; +# see the in-line comments for details. +# +# There are tweaks for specific operating systems such as AIX, CygWin, +# Darwin, MinGW, and NonStop. +# +# (3) This script is generated from the Groovy template +# https://github.com/gradle/gradle/blob/HEAD/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt +# within the Gradle project. +# +# You can find Gradle at https://github.com/gradle/gradle/. +# ############################################################################## -# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. -DEFAULT_JVM_OPTS="" +# Attempt to set APP_HOME + +# Resolve links: $0 may be a link +app_path=$0 + +# Need this for daisy-chained symlinks. +while + APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path + [ -h "$app_path" ] +do + ls=$( ls -ld "$app_path" ) + link=${ls#*' -> '} + case $link in #( + /*) app_path=$link ;; #( + *) app_path=$APP_HOME$link ;; + esac +done + +# This is normally unused +# shellcheck disable=SC2034 +APP_BASE_NAME=${0##*/} +APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit -APP_NAME="Gradle" -APP_BASE_NAME=`basename "$0"` +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' # Use the maximum available, or set MAX_FD != -1 to use that value. -MAX_FD="maximum" +MAX_FD=maximum -warn ( ) { +warn () { echo "$*" -} +} >&2 -die ( ) { +die () { echo echo "$*" echo exit 1 -} +} >&2 # OS specific support (must be 'true' or 'false'). cygwin=false msys=false darwin=false -case "`uname`" in - CYGWIN* ) - cygwin=true - ;; - Darwin* ) - darwin=true - ;; - MINGW* ) - msys=true - ;; +nonstop=false +case "$( uname )" in #( + CYGWIN* ) cygwin=true ;; #( + Darwin* ) darwin=true ;; #( + MSYS* | MINGW* ) msys=true ;; #( + NONSTOP* ) nonstop=true ;; esac -# Attempt to set APP_HOME -# Resolve links: $0 may be a link -PRG="$0" -# Need this for relative symlinks. -while [ -h "$PRG" ] ; do - ls=`ls -ld "$PRG"` - link=`expr "$ls" : '.*-> \(.*\)$'` - if expr "$link" : '/.*' > /dev/null; then - PRG="$link" - else - PRG=`dirname "$PRG"`"/$link" - fi -done -SAVED="`pwd`" -cd "`dirname \"$PRG\"`/" >/dev/null -APP_HOME="`pwd -P`" -cd "$SAVED" >/dev/null - CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + # Determine the Java command to use to start the JVM. if [ -n "$JAVA_HOME" ] ; then if [ -x "$JAVA_HOME/jre/sh/java" ] ; then # IBM's JDK on AIX uses strange locations for the executables - JAVACMD="$JAVA_HOME/jre/sh/java" + JAVACMD=$JAVA_HOME/jre/sh/java else - JAVACMD="$JAVA_HOME/bin/java" + JAVACMD=$JAVA_HOME/bin/java fi if [ ! -x "$JAVACMD" ] ; then die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME @@ -77,7 +132,7 @@ Please set the JAVA_HOME variable in your environment to match the location of your Java installation." fi else - JAVACMD="java" + JAVACMD=java which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. Please set the JAVA_HOME variable in your environment to match the @@ -85,76 +140,105 @@ location of your Java installation." fi # Increase the maximum file descriptors if we can. -if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then - MAX_FD_LIMIT=`ulimit -H -n` - if [ $? -eq 0 ] ; then - if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then - MAX_FD="$MAX_FD_LIMIT" - fi - ulimit -n $MAX_FD - if [ $? -ne 0 ] ; then - warn "Could not set maximum file descriptor limit: $MAX_FD" - fi - else - warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" - fi +if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then + case $MAX_FD in #( + max*) + # In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC3045 + MAX_FD=$( ulimit -H -n ) || + warn "Could not query maximum file descriptor limit" + esac + case $MAX_FD in #( + '' | soft) :;; #( + *) + # In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC3045 + ulimit -n "$MAX_FD" || + warn "Could not set maximum file descriptor limit to $MAX_FD" + esac fi -# For Darwin, add options to specify how the application appears in the dock -if $darwin; then - GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" -fi +# Collect all arguments for the java command, stacking in reverse order: +# * args from the command line +# * the main class name +# * -classpath +# * -D...appname settings +# * --module-path (only if needed) +# * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables. + +# For Cygwin or MSYS, switch paths to Windows format before running java +if "$cygwin" || "$msys" ; then + APP_HOME=$( cygpath --path --mixed "$APP_HOME" ) + CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" ) + + JAVACMD=$( cygpath --unix "$JAVACMD" ) -# For Cygwin, switch paths to Windows format before running java -if $cygwin ; then - APP_HOME=`cygpath --path --mixed "$APP_HOME"` - CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` - JAVACMD=`cygpath --unix "$JAVACMD"` - - # We build the pattern for arguments to be converted via cygpath - ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` - SEP="" - for dir in $ROOTDIRSRAW ; do - ROOTDIRS="$ROOTDIRS$SEP$dir" - SEP="|" - done - OURCYGPATTERN="(^($ROOTDIRS))" - # Add a user-defined pattern to the cygpath arguments - if [ "$GRADLE_CYGPATTERN" != "" ] ; then - OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" - fi # Now convert the arguments - kludge to limit ourselves to /bin/sh - i=0 - for arg in "$@" ; do - CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` - CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option - - if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition - eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` - else - eval `echo args$i`="\"$arg\"" + for arg do + if + case $arg in #( + -*) false ;; # don't mess with options #( + /?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath + [ -e "$t" ] ;; #( + *) false ;; + esac + then + arg=$( cygpath --path --ignore --mixed "$arg" ) fi - i=$((i+1)) + # Roll the args list around exactly as many times as the number of + # args, so each arg winds up back in the position where it started, but + # possibly modified. + # + # NB: a `for` loop captures its iteration list before it begins, so + # changing the positional parameters here affects neither the number of + # iterations, nor the values presented in `arg`. + shift # remove old arg + set -- "$@" "$arg" # push replacement arg done - case $i in - (0) set -- ;; - (1) set -- "$args0" ;; - (2) set -- "$args0" "$args1" ;; - (3) set -- "$args0" "$args1" "$args2" ;; - (4) set -- "$args0" "$args1" "$args2" "$args3" ;; - (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; - (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; - (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; - (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; - (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; - esac fi -# Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules -function splitJvmOpts() { - JVM_OPTS=("$@") -} -eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS -JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" +# Collect all arguments for the java command; +# * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of +# shell script including quotes and variable substitutions, so put them in +# double quotes to make sure that they get re-expanded; and +# * put everything else in single quotes, so that it's not re-expanded. + +set -- \ + "-Dorg.gradle.appname=$APP_BASE_NAME" \ + -classpath "$CLASSPATH" \ + org.gradle.wrapper.GradleWrapperMain \ + "$@" + +# Stop when "xargs" is not available. +if ! command -v xargs >/dev/null 2>&1 +then + die "xargs is not available" +fi -exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" +# Use "xargs" to parse quoted args. +# +# With -n1 it outputs one arg per line, with the quotes and backslashes removed. +# +# In Bash we could simply go: +# +# readarray ARGS < <( xargs -n1 <<<"$var" ) && +# set -- "${ARGS[@]}" "$@" +# +# but POSIX shell has neither arrays nor command substitution, so instead we +# post-process each arg (as a line of input to sed) to backslash-escape any +# character that might be a shell metacharacter, then use eval to reverse +# that process (while maintaining the separation between arguments), and wrap +# the whole thing up as a single "set" statement. +# +# This will of course break if any of these variables contains a newline or +# an unmatched quote. +# + +eval "set -- $( + printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" | + xargs -n1 | + sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' | + tr '\n' ' ' + )" '"$@"' + +exec "$JAVACMD" "$@" diff --git a/samples/android-kotlin/gradlew.bat b/samples/android-kotlin/gradlew.bat index 8a0b282..93e3f59 100644 --- a/samples/android-kotlin/gradlew.bat +++ b/samples/android-kotlin/gradlew.bat @@ -1,4 +1,20 @@ -@if "%DEBUG%" == "" @echo off +@rem +@rem Copyright 2015 the original author or authors. +@rem +@rem Licensed under the Apache License, Version 2.0 (the "License"); +@rem you may not use this file except in compliance with the License. +@rem You may obtain a copy of the License at +@rem +@rem https://www.apache.org/licenses/LICENSE-2.0 +@rem +@rem Unless required by applicable law or agreed to in writing, software +@rem distributed under the License is distributed on an "AS IS" BASIS, +@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +@rem See the License for the specific language governing permissions and +@rem limitations under the License. +@rem + +@if "%DEBUG%"=="" @echo off @rem ########################################################################## @rem @rem Gradle startup script for Windows @@ -8,20 +24,24 @@ @rem Set local scope for the variables with windows NT shell if "%OS%"=="Windows_NT" setlocal -@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. -set DEFAULT_JVM_OPTS= - set DIRNAME=%~dp0 -if "%DIRNAME%" == "" set DIRNAME=. +if "%DIRNAME%"=="" set DIRNAME=. +@rem This is normally unused set APP_BASE_NAME=%~n0 set APP_HOME=%DIRNAME% +@rem Resolve any "." and ".." in APP_HOME to make it shorter. +for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" + @rem Find java.exe if defined JAVA_HOME goto findJavaFromJavaHome set JAVA_EXE=java.exe %JAVA_EXE% -version >NUL 2>&1 -if "%ERRORLEVEL%" == "0" goto init +if %ERRORLEVEL% equ 0 goto execute echo. echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. @@ -35,7 +55,7 @@ goto fail set JAVA_HOME=%JAVA_HOME:"=% set JAVA_EXE=%JAVA_HOME%/bin/java.exe -if exist "%JAVA_EXE%" goto init +if exist "%JAVA_EXE%" goto execute echo. echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% @@ -45,44 +65,26 @@ echo location of your Java installation. goto fail -:init -@rem Get command-line arguments, handling Windowz variants - -if not "%OS%" == "Windows_NT" goto win9xME_args -if "%@eval[2+2]" == "4" goto 4NT_args - -:win9xME_args -@rem Slurp the command line arguments. -set CMD_LINE_ARGS= -set _SKIP=2 - -:win9xME_args_slurp -if "x%~1" == "x" goto execute - -set CMD_LINE_ARGS=%* -goto execute - -:4NT_args -@rem Get arguments from the 4NT Shell from JP Software -set CMD_LINE_ARGS=%$ - :execute @rem Setup the command line set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + @rem Execute Gradle -"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* :end @rem End local scope for the variables with windows NT shell -if "%ERRORLEVEL%"=="0" goto mainEnd +if %ERRORLEVEL% equ 0 goto mainEnd :fail rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of rem the _cmd.exe /c_ return code! -if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 -exit /b 1 +set EXIT_CODE=%ERRORLEVEL% +if %EXIT_CODE% equ 0 set EXIT_CODE=1 +if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE% +exit /b %EXIT_CODE% :mainEnd if "%OS%"=="Windows_NT" endlocal diff --git a/src/main/java/com/configcat/ConfigCatClient.java b/src/main/java/com/configcat/ConfigCatClient.java index adf6c95..0c45087 100644 --- a/src/main/java/com/configcat/ConfigCatClient.java +++ b/src/main/java/com/configcat/ConfigCatClient.java @@ -793,11 +793,11 @@ public void logFilter(LogFilterFunction logFilter) { } /** - * Sets the Android context used by the SDK. + * Indicates that the SDK should react to application state changes. * - * @param context the Android {@link android.content.Context} instance to be used. + * @param context the Android {@link android.content.Context} instance. */ - public void context(android.content.Context context) { + public void watchAppStateChanges(android.content.Context context) { this.context = context; } diff --git a/src/main/java/com/configcat/ConfigService.java b/src/main/java/com/configcat/ConfigService.java index a032b2c..01da0c5 100644 --- a/src/main/java/com/configcat/ConfigService.java +++ b/src/main/java/com/configcat/ConfigService.java @@ -44,8 +44,9 @@ class ConfigService implements Closeable { private Entry cachedEntry = Entry.EMPTY; private CompletableFuture> runningTask; private final AtomicBoolean initialized = new AtomicBoolean(false); - private final AtomicBoolean offline; + private final AtomicBoolean userIndicatedOffline; private final AtomicBoolean inForegroundAndHasNetwork; + private final AtomicBoolean offline; private final AtomicBoolean closed = new AtomicBoolean(false); private final String cacheKey; private final StateMonitor stateMonitor; @@ -63,7 +64,7 @@ public ConfigService(String sdkKey, ConfigCatLogger logger, ConfigFetcher fetcher, ConfigCatHooks hooks, - boolean offline) { + boolean userIndicatedOffline) { this.cacheKey = Utils.sha1(String.format(CACHE_BASE, sdkKey)); this.stateMonitor = stateMonitor; this.mode = mode; @@ -71,22 +72,18 @@ public ConfigService(String sdkKey, this.logger = logger; this.fetcher = fetcher; this.hooks = hooks; - this.offline = new AtomicBoolean(offline); - + this.userIndicatedOffline = new AtomicBoolean(userIndicatedOffline); this.inForegroundAndHasNetwork = new AtomicBoolean(stateMonitor == null || stateMonitor.isNetworkAvailable()); + this.offline = new AtomicBoolean(isOffline()); if (stateMonitor != null) { stateMonitor.addStateChangeListener(state -> { - this.inForegroundAndHasNetwork.set(state); - if (!state) { - setOffline(); - } else { - setOnline(); - } + inForegroundAndHasNetwork.set(state); + switchStateIfNeeded(); }); } - if (mode instanceof AutoPollingMode && !offline) { + if (mode instanceof AutoPollingMode && !userIndicatedOffline) { AutoPollingMode autoPollingMode = (AutoPollingMode) mode; startPoll(autoPollingMode); @@ -133,7 +130,7 @@ public CompletableFuture getSettings() { } public CompletableFuture refresh() { - if (offline.get()) { + if (isOffline()) { String offlineWarning = ConfigCatLogMessages.CONFIG_SERVICE_CANNOT_INITIATE_HTTP_CALLS_WARN; logger.warn(3200, offlineWarning); return CompletableFuture.completedFuture(new RefreshResult(false, offlineWarning)); @@ -144,20 +141,30 @@ public CompletableFuture refresh() { } public void setOnline() { - if (!offline.compareAndSet(true, false)) return; - if (mode instanceof AutoPollingMode) { - startPoll((AutoPollingMode) mode); - } - logger.info(5200, ConfigCatLogMessages.getConfigServiceStatusChanged("ONLINE")); + if (!userIndicatedOffline.compareAndSet(true, false)) return; + switchStateIfNeeded(); } public void setOffline() { - if (!offline.compareAndSet(false, true)) return; - logger.info(5200, ConfigCatLogMessages.getConfigServiceStatusChanged("OFFLINE")); + if (!userIndicatedOffline.compareAndSet(false, true)) return; + switchStateIfNeeded(); } public boolean isOffline() { - return offline.get(); + return userIndicatedOffline.get() || !inForegroundAndHasNetwork.get(); + } + + private void switchStateIfNeeded() { + boolean isOffline = isOffline(); + if (isOffline && offline.compareAndSet(false, true)) { + logger.info(5200, ConfigCatLogMessages.getConfigServiceStatusChanged("OFFLINE")); + } + if (!isOffline && offline.compareAndSet(true, false)) { + if (mode instanceof AutoPollingMode) { + startPoll((AutoPollingMode) mode); + } + logger.info(5200, ConfigCatLogMessages.getConfigServiceStatusChanged("ONLINE")); + } } private CompletableFuture> fetchIfOlder(long threshold, boolean preferCached) { @@ -175,7 +182,7 @@ private CompletableFuture> fetchIfOlder(long threshold, boolean pr return CompletableFuture.completedFuture(Result.success(cachedEntry)); } // If we are in offline mode or the caller prefers cached values, do not initiate fetch. - if (offline.get() || !inForegroundAndHasNetwork.get() || preferCached) { + if (isOffline() || preferCached) { return CompletableFuture.completedFuture(Result.success(cachedEntry)); } diff --git a/src/test/java/com/configcat/ConfigFetcherTest.java b/src/test/java/com/configcat/ConfigFetcherTest.java index 6ece674..46f41d2 100644 --- a/src/test/java/com/configcat/ConfigFetcherTest.java +++ b/src/test/java/com/configcat/ConfigFetcherTest.java @@ -258,4 +258,45 @@ public void fetchedReceivedInvalidBodyContainsCFRAY() throws Exception { fetcher.close(); } + + @Test + public void ensureStateMonitorWorks() throws IOException { + this.server.enqueue(new MockResponse().setResponseCode(200).setBody(TEST_JSON)); + ConfigFetcher fetcher = new ConfigFetcher(new ConfigCatClient.HttpOptions(), logger, + "", this.server.url("/").toString(), false, PollingModes.manualPoll().getPollingIdentifier()); + + TestStateMonitor monitor = new TestStateMonitor(); + ConfigService service = new ConfigService("", monitor, PollingModes.autoPoll(), new NullConfigCache(), logger, fetcher, new ConfigCatHooks(), false); + + assertFalse(service.isOffline()); + + monitor.setState(false); + monitor.notifyListeners(); + + assertTrue(service.isOffline()); + + monitor.setState(true); + monitor.notifyListeners(); + + assertFalse(service.isOffline()); + + service.setOffline(); + assertTrue(service.isOffline()); + + monitor.setState(false); + monitor.notifyListeners(); + + assertTrue(service.isOffline()); + + monitor.setState(true); + monitor.notifyListeners(); + + assertTrue(service.isOffline()); + + service.setOnline(); + + assertFalse(service.isOffline()); + + service.close(); + } } diff --git a/src/test/java/com/configcat/TestStateMonitor.java b/src/test/java/com/configcat/TestStateMonitor.java new file mode 100644 index 0000000..a34cba7 --- /dev/null +++ b/src/test/java/com/configcat/TestStateMonitor.java @@ -0,0 +1,55 @@ +package com.configcat; + +import java9.util.function.Consumer; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.locks.ReentrantReadWriteLock; + +public class TestStateMonitor implements StateMonitor { + private final List> stateChangeListeners = new ArrayList<>(); + private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); + private final AtomicBoolean state = new AtomicBoolean(true); + + public void setState(boolean state) { + this.state.set(state); + } + + public void notifyListeners() { + lock.readLock().lock(); + try { + for (Consumer listener : this.stateChangeListeners) { + listener.accept(this.state.get()); + } + } finally { + lock.readLock().unlock(); + } + } + + @Override + public void addStateChangeListener(Consumer listener) { + lock.writeLock().lock(); + try { + this.stateChangeListeners.add(listener); + } finally { + lock.writeLock().unlock(); + } + } + + @Override + public boolean isNetworkAvailable() { + return this.state.get(); + } + + @Override + public void close() throws IOException { + lock.writeLock().lock(); + try { + this.stateChangeListeners.clear(); + } finally { + lock.writeLock().unlock(); + } + } +} From c4f379e63530de7e7b954822792a4ae185cc4ea1 Mon Sep 17 00:00:00 2001 From: Peter Csajtai Date: Tue, 16 Dec 2025 16:17:13 +0100 Subject: [PATCH 07/17] Update Utils.java --- src/main/java/com/configcat/Utils.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/configcat/Utils.java b/src/main/java/com/configcat/Utils.java index fa44ff1..b09a018 100644 --- a/src/main/java/com/configcat/Utils.java +++ b/src/main/java/com/configcat/Utils.java @@ -56,7 +56,7 @@ private Constants() { /* prevent from instantiation*/ } static final long DISTANT_PAST = 0; static final String CONFIG_JSON_NAME = "config_v6.json"; static final String SERIALIZATION_FORMAT_VERSION = "v2"; - static final String VERSION = "11.0.1"; + static final String VERSION = "11.1.0"; static final String SDK_KEY_PROXY_PREFIX = "configcat-proxy/"; static final String SDK_KEY_PREFIX = "configcat-sdk-1"; From 83082fad87d2d39004c554013450edf8634695b1 Mon Sep 17 00:00:00 2001 From: Peter Csajtai Date: Tue, 16 Dec 2025 16:19:02 +0100 Subject: [PATCH 08/17] Update android-ci.yml --- .github/workflows/android-ci.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/android-ci.yml b/.github/workflows/android-ci.yml index 0c817f2..b9bff39 100644 --- a/.github/workflows/android-ci.yml +++ b/.github/workflows/android-ci.yml @@ -2,7 +2,7 @@ name: Android CI on: push: - branches: [ '*' ] + branches: [ master ] tags: [ 'v[0-9]+.[0-9]+.[0-9]+' ] workflow_dispatch: @@ -76,7 +76,7 @@ jobs: deploy-snapshot: needs: [ coverage-scan, test ] - if: ${{ github.ref == 'refs/heads/replace-okhttp' }} + if: ${{ github.ref == 'refs/heads/master' }} runs-on: ubuntu-latest steps: - uses: actions/checkout@v6 From 7d120116af3c0517cfe036e986b542a49bb7988a Mon Sep 17 00:00:00 2001 From: Peter Csajtai Date: Tue, 16 Dec 2025 16:56:12 +0100 Subject: [PATCH 09/17] Update ConfigService.java --- src/main/java/com/configcat/ConfigService.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/configcat/ConfigService.java b/src/main/java/com/configcat/ConfigService.java index 01da0c5..a683a50 100644 --- a/src/main/java/com/configcat/ConfigService.java +++ b/src/main/java/com/configcat/ConfigService.java @@ -83,7 +83,7 @@ public ConfigService(String sdkKey, }); } - if (mode instanceof AutoPollingMode && !userIndicatedOffline) { + if (mode instanceof AutoPollingMode) { AutoPollingMode autoPollingMode = (AutoPollingMode) mode; startPoll(autoPollingMode); From 0b5590613a89267be0b8c6b8260026d548b509c0 Mon Sep 17 00:00:00 2001 From: Peter Csajtai Date: Tue, 16 Dec 2025 17:05:32 +0100 Subject: [PATCH 10/17] Remove unused imports --- .../java/com/configcat/AppStateMonitor.java | 27 ++++++++++++------- .../java/com/configcat/AutoPollingTest.java | 2 -- .../ConfigCatClientIntegrationTest.java | 1 - .../java/com/configcat/ConfigFetcherTest.java | 8 +++--- .../com/configcat/DataGovernanceTest.java | 1 - .../java/com/configcat/LazyLoadingTest.java | 1 - 6 files changed, 22 insertions(+), 18 deletions(-) diff --git a/src/main/java/com/configcat/AppStateMonitor.java b/src/main/java/com/configcat/AppStateMonitor.java index b137bba..adf2255 100644 --- a/src/main/java/com/configcat/AppStateMonitor.java +++ b/src/main/java/com/configcat/AppStateMonitor.java @@ -87,16 +87,24 @@ public void onActivityResumed(Activity activity) { } @Override - public void onActivityPaused(Activity activity) {} + public void onActivityPaused(Activity activity) { + // ignore + } @Override - public void onActivityStopped(Activity activity) {} + public void onActivityStopped(Activity activity) { + // ignore + } @Override - public void onActivitySaveInstanceState(Activity activity, Bundle bundle) {} + public void onActivitySaveInstanceState(Activity activity, Bundle bundle) { + // ignore + } @Override - public void onActivityDestroyed(Activity activity) {} + public void onActivityDestroyed(Activity activity) { + // ignore + } @Override public void onReceive(Context context, Intent intent) { @@ -110,15 +118,16 @@ public void onReceive(Context context, Intent intent) { @Override public void onTrimMemory(int i) { - if (i == ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) { // We're in the background - if (inForeground.compareAndSet(true, false)) { - notifyListeners(); - } + // We're in the background + if (i == ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN && inForeground.compareAndSet(true, false)) { + notifyListeners(); } } @Override - public void onConfigurationChanged(Configuration configuration) {} + public void onConfigurationChanged(Configuration configuration) { + // ignore + } @Override public void onLowMemory() {} diff --git a/src/test/java/com/configcat/AutoPollingTest.java b/src/test/java/com/configcat/AutoPollingTest.java index 7e2e1ab..5673a59 100644 --- a/src/test/java/com/configcat/AutoPollingTest.java +++ b/src/test/java/com/configcat/AutoPollingTest.java @@ -1,6 +1,5 @@ package com.configcat; -import okhttp3.OkHttpClient; import okhttp3.mockwebserver.MockResponse; import okhttp3.mockwebserver.MockWebServer; import org.junit.jupiter.api.AfterEach; @@ -11,7 +10,6 @@ import java.io.IOException; import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; -import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicReference; import static org.junit.jupiter.api.Assertions.*; diff --git a/src/test/java/com/configcat/ConfigCatClientIntegrationTest.java b/src/test/java/com/configcat/ConfigCatClientIntegrationTest.java index cbfe6c5..db815da 100644 --- a/src/test/java/com/configcat/ConfigCatClientIntegrationTest.java +++ b/src/test/java/com/configcat/ConfigCatClientIntegrationTest.java @@ -1,6 +1,5 @@ package com.configcat; -import okhttp3.OkHttpClient; import okhttp3.mockwebserver.MockResponse; import okhttp3.mockwebserver.MockWebServer; import org.junit.jupiter.api.AfterEach; diff --git a/src/test/java/com/configcat/ConfigFetcherTest.java b/src/test/java/com/configcat/ConfigFetcherTest.java index 46f41d2..31aaf0c 100644 --- a/src/test/java/com/configcat/ConfigFetcherTest.java +++ b/src/test/java/com/configcat/ConfigFetcherTest.java @@ -184,7 +184,7 @@ void testIntegration() throws IOException, ExecutionException, InterruptedExcept } @Test - public void fetchedFail403ContainsCFRAY() throws Exception { + void fetchedFail403ContainsCFRAY() throws Exception { this.server.enqueue(new MockResponse().setResponseCode(403).setBody(TEST_JSON).setHeader("ETag", "fakeETag").setHeader("CF-RAY", "12345")); Logger mockLogger = mock(Logger.class); @@ -209,7 +209,7 @@ public void fetchedFail403ContainsCFRAY() throws Exception { } @Test - public void fetchedNotModified304ContainsCFRAY() throws Exception { + void fetchedNotModified304ContainsCFRAY() throws Exception { this.server.enqueue(new MockResponse().setResponseCode(304).setHeader("CF-RAY", "12345")); Logger mockLogger = mock(Logger.class); @@ -234,7 +234,7 @@ public void fetchedNotModified304ContainsCFRAY() throws Exception { } @Test - public void fetchedReceivedInvalidBodyContainsCFRAY() throws Exception { + void fetchedReceivedInvalidBodyContainsCFRAY() throws Exception { this.server.enqueue(new MockResponse().setResponseCode(200).setHeader("CF-RAY", "12345").setBody("test")); Logger mockLogger = mock(Logger.class); @@ -260,7 +260,7 @@ public void fetchedReceivedInvalidBodyContainsCFRAY() throws Exception { } @Test - public void ensureStateMonitorWorks() throws IOException { + void ensureStateMonitorWorks() throws IOException { this.server.enqueue(new MockResponse().setResponseCode(200).setBody(TEST_JSON)); ConfigFetcher fetcher = new ConfigFetcher(new ConfigCatClient.HttpOptions(), logger, "", this.server.url("/").toString(), false, PollingModes.manualPoll().getPollingIdentifier()); diff --git a/src/test/java/com/configcat/DataGovernanceTest.java b/src/test/java/com/configcat/DataGovernanceTest.java index 7e1f1f1..87662fc 100644 --- a/src/test/java/com/configcat/DataGovernanceTest.java +++ b/src/test/java/com/configcat/DataGovernanceTest.java @@ -1,6 +1,5 @@ package com.configcat; -import okhttp3.OkHttpClient; import okhttp3.mockwebserver.MockResponse; import okhttp3.mockwebserver.MockWebServer; import org.junit.jupiter.api.Test; diff --git a/src/test/java/com/configcat/LazyLoadingTest.java b/src/test/java/com/configcat/LazyLoadingTest.java index 0d51769..7654a90 100644 --- a/src/test/java/com/configcat/LazyLoadingTest.java +++ b/src/test/java/com/configcat/LazyLoadingTest.java @@ -1,6 +1,5 @@ package com.configcat; -import okhttp3.OkHttpClient; import okhttp3.mockwebserver.MockResponse; import okhttp3.mockwebserver.MockWebServer; import org.junit.jupiter.api.AfterEach; From 748c0cb1ef37e53ca21f09184f689ff82e525e9c Mon Sep 17 00:00:00 2001 From: Peter Csajtai Date: Tue, 16 Dec 2025 17:46:11 +0100 Subject: [PATCH 11/17] Fix unit tests --- gradle/libs.versions.toml | 2 +- samples/android-kotlin/app/build.gradle | 2 +- src/main/java/com/configcat/AppStateMonitor.java | 5 +++++ src/main/java/com/configcat/ConfigService.java | 1 + 4 files changed, 8 insertions(+), 2 deletions(-) diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index ad7a3e5..ff948bc 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -6,7 +6,7 @@ gson = "2.9.0" commons-codec = "1.15" semantic-version = "2.1.1" junit-jupiter = "5.10.0" -logback = "1.5.13" +logback = "1.5.19" mockwebserver = "4.12.0" mockito = "4.8.0" android-retrofuture = "1.7.4" diff --git a/samples/android-kotlin/app/build.gradle b/samples/android-kotlin/app/build.gradle index 5871bd0..642ff40 100644 --- a/samples/android-kotlin/app/build.gradle +++ b/samples/android-kotlin/app/build.gradle @@ -36,7 +36,7 @@ dependencies { implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.7.21' implementation 'androidx.appcompat:appcompat:1.5.1' implementation 'androidx.constraintlayout:constraintlayout:2.1.4' - implementation 'com.configcat:configcat-android-client:11.1.0' + implementation 'com.configcat:configcat-android-client:0.0.6' implementation 'com.noveogroup.android:android-logger:1.3.1' testImplementation 'junit:junit:4.13.2' androidTestImplementation 'androidx.test.ext:junit:1.1.4' diff --git a/src/main/java/com/configcat/AppStateMonitor.java b/src/main/java/com/configcat/AppStateMonitor.java index adf2255..64a3028 100644 --- a/src/main/java/com/configcat/AppStateMonitor.java +++ b/src/main/java/com/configcat/AppStateMonitor.java @@ -13,6 +13,7 @@ import java.io.IOException; import java.util.ArrayList; import java.util.List; +import java.util.Objects; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.locks.ReentrantReadWriteLock; @@ -108,6 +109,9 @@ public void onActivityDestroyed(Activity activity) { @Override public void onReceive(Context context, Intent intent) { + if (!Objects.equals(intent.getAction(), CONNECTIVITY_CHANGE)) { + return; + } boolean isConnected = isNetworkAvailable(); if (isConnected && hasNetwork.compareAndSet(false, true)) { notifyListeners(); @@ -132,6 +136,7 @@ public void onConfigurationChanged(Configuration configuration) { @Override public void onLowMemory() {} + @SuppressWarnings("deprecation") public boolean isNetworkAvailable() { ConnectivityManager conn = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); diff --git a/src/main/java/com/configcat/ConfigService.java b/src/main/java/com/configcat/ConfigService.java index a683a50..6b0808e 100644 --- a/src/main/java/com/configcat/ConfigService.java +++ b/src/main/java/com/configcat/ConfigService.java @@ -183,6 +183,7 @@ private CompletableFuture> fetchIfOlder(long threshold, boolean pr } // If we are in offline mode or the caller prefers cached values, do not initiate fetch. if (isOffline() || preferCached) { + setInitialized(); return CompletableFuture.completedFuture(Result.success(cachedEntry)); } From 4a3f047aa61384df2b2bd9a74a541dd79e0e6650 Mon Sep 17 00:00:00 2001 From: Peter Csajtai Date: Tue, 16 Dec 2025 17:46:38 +0100 Subject: [PATCH 12/17] Update build.gradle --- samples/android-kotlin/app/build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/android-kotlin/app/build.gradle b/samples/android-kotlin/app/build.gradle index 642ff40..5871bd0 100644 --- a/samples/android-kotlin/app/build.gradle +++ b/samples/android-kotlin/app/build.gradle @@ -36,7 +36,7 @@ dependencies { implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.7.21' implementation 'androidx.appcompat:appcompat:1.5.1' implementation 'androidx.constraintlayout:constraintlayout:2.1.4' - implementation 'com.configcat:configcat-android-client:0.0.6' + implementation 'com.configcat:configcat-android-client:11.1.0' implementation 'com.noveogroup.android:android-logger:1.3.1' testImplementation 'junit:junit:4.13.2' androidTestImplementation 'androidx.test.ext:junit:1.1.4' From 2a866a7a0bd83fc29d726404abad40a7ab01c68b Mon Sep 17 00:00:00 2001 From: Peter Csajtai Date: Tue, 16 Dec 2025 18:06:30 +0100 Subject: [PATCH 13/17] Remove unused imports --- src/main/java/com/configcat/AppStateMonitor.java | 4 +++- src/test/java/com/configcat/ConfigCatClientTest.java | 1 - src/test/java/com/configcat/ConfigFetcherTest.java | 1 - src/test/java/com/configcat/ManualPollingTest.java | 1 - 4 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/configcat/AppStateMonitor.java b/src/main/java/com/configcat/AppStateMonitor.java index 64a3028..730ea7c 100644 --- a/src/main/java/com/configcat/AppStateMonitor.java +++ b/src/main/java/com/configcat/AppStateMonitor.java @@ -134,7 +134,9 @@ public void onConfigurationChanged(Configuration configuration) { } @Override - public void onLowMemory() {} + public void onLowMemory() { + // ignore + } @SuppressWarnings("deprecation") public boolean isNetworkAvailable() { diff --git a/src/test/java/com/configcat/ConfigCatClientTest.java b/src/test/java/com/configcat/ConfigCatClientTest.java index 875fa56..abd2426 100644 --- a/src/test/java/com/configcat/ConfigCatClientTest.java +++ b/src/test/java/com/configcat/ConfigCatClientTest.java @@ -1,7 +1,6 @@ package com.configcat; import java9.util.concurrent.CompletableFuture; -import okhttp3.OkHttpClient; import okhttp3.mockwebserver.MockResponse; import okhttp3.mockwebserver.MockWebServer; import org.junit.jupiter.api.Test; diff --git a/src/test/java/com/configcat/ConfigFetcherTest.java b/src/test/java/com/configcat/ConfigFetcherTest.java index 31aaf0c..ce405e9 100644 --- a/src/test/java/com/configcat/ConfigFetcherTest.java +++ b/src/test/java/com/configcat/ConfigFetcherTest.java @@ -2,7 +2,6 @@ import com.google.gson.Gson; import com.google.gson.GsonBuilder; -import okhttp3.OkHttpClient; import okhttp3.mockwebserver.MockResponse; import okhttp3.mockwebserver.MockWebServer; import org.junit.jupiter.api.AfterEach; diff --git a/src/test/java/com/configcat/ManualPollingTest.java b/src/test/java/com/configcat/ManualPollingTest.java index a65ba67..b8913ba 100644 --- a/src/test/java/com/configcat/ManualPollingTest.java +++ b/src/test/java/com/configcat/ManualPollingTest.java @@ -1,6 +1,5 @@ package com.configcat; -import okhttp3.OkHttpClient; import okhttp3.mockwebserver.MockResponse; import okhttp3.mockwebserver.MockWebServer; import org.junit.jupiter.api.AfterEach; From 9625670a43c16aa7cba1539c73e05f525907e649 Mon Sep 17 00:00:00 2001 From: Peter Csajtai Date: Tue, 16 Dec 2025 19:43:55 +0100 Subject: [PATCH 14/17] Update AppStateMonitor.java --- src/main/java/com/configcat/AppStateMonitor.java | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/configcat/AppStateMonitor.java b/src/main/java/com/configcat/AppStateMonitor.java index 730ea7c..0d8725d 100644 --- a/src/main/java/com/configcat/AppStateMonitor.java +++ b/src/main/java/com/configcat/AppStateMonitor.java @@ -13,7 +13,6 @@ import java.io.IOException; import java.util.ArrayList; import java.util.List; -import java.util.Objects; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.locks.ReentrantReadWriteLock; @@ -31,7 +30,6 @@ class AppStateMonitor extends BroadcastReceiver implements Application.ActivityL private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); private final List> stateChangeListeners = new ArrayList<>(); private final AtomicBoolean inForeground = new AtomicBoolean(true); - private final AtomicBoolean hasNetwork = new AtomicBoolean(true); public AppStateMonitor(Context context, ConfigCatLogger logger) { this.context = context; @@ -55,11 +53,12 @@ public void addStateChangeListener(Consumer listener) { } private void notifyListeners() { - logger.debug(String.format("App state has been changed, in foreground: %s, has network: %s", inForeground.get(), hasNetwork.get())); + boolean hasNetwork = this.isNetworkAvailable(); + logger.debug(String.format("App state has been changed, in foreground: %s, has network: %s", inForeground.get(), hasNetwork)); lock.readLock().lock(); try { for (Consumer listener : this.stateChangeListeners) { - listener.accept(inForeground.get() && hasNetwork.get()); + listener.accept(inForeground.get() && hasNetwork); } } finally { lock.readLock().unlock(); @@ -109,15 +108,10 @@ public void onActivityDestroyed(Activity activity) { @Override public void onReceive(Context context, Intent intent) { - if (!Objects.equals(intent.getAction(), CONNECTIVITY_CHANGE)) { + if (!CONNECTIVITY_CHANGE.equals(intent.getAction())) { return; } - boolean isConnected = isNetworkAvailable(); - if (isConnected && hasNetwork.compareAndSet(false, true)) { - notifyListeners(); - } else if (!isConnected && hasNetwork.compareAndSet(true, false)) { - notifyListeners(); - } + notifyListeners(); } @Override From d18b847cd899c4bbd5f6fb09d068d93146174229 Mon Sep 17 00:00:00 2001 From: Peter Csajtai Date: Wed, 17 Dec 2025 10:47:16 +0100 Subject: [PATCH 15/17] Fix version --- gradle.properties | 2 +- samples/android-java/app/build.gradle | 2 +- samples/android-kotlin/app/build.gradle | 2 +- src/main/java/com/configcat/Utils.java | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/gradle.properties b/gradle.properties index f795f2f..63bf32e 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,3 +1,3 @@ -version=11.1.0 +version=10.5.0 org.gradle.jvmargs=-Xmx2g \ No newline at end of file diff --git a/samples/android-java/app/build.gradle b/samples/android-java/app/build.gradle index 1f72c39..94555d5 100644 --- a/samples/android-java/app/build.gradle +++ b/samples/android-java/app/build.gradle @@ -36,7 +36,7 @@ dependencies { implementation 'androidx.lifecycle:lifecycle-viewmodel:2.4.0' implementation 'com.google.android.material:material:1.5.0' implementation 'androidx.constraintlayout:constraintlayout:2.1.4' - implementation 'com.configcat:configcat-android-client:11.1.0' + implementation 'com.configcat:configcat-android-client:10.5.0' implementation 'org.slf4j:slf4j-api:2.0.7' implementation 'com.github.tony19:logback-android:3.0.0' } \ No newline at end of file diff --git a/samples/android-kotlin/app/build.gradle b/samples/android-kotlin/app/build.gradle index 5871bd0..048ff17 100644 --- a/samples/android-kotlin/app/build.gradle +++ b/samples/android-kotlin/app/build.gradle @@ -36,7 +36,7 @@ dependencies { implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.7.21' implementation 'androidx.appcompat:appcompat:1.5.1' implementation 'androidx.constraintlayout:constraintlayout:2.1.4' - implementation 'com.configcat:configcat-android-client:11.1.0' + implementation 'com.configcat:configcat-android-client:10.5.0' implementation 'com.noveogroup.android:android-logger:1.3.1' testImplementation 'junit:junit:4.13.2' androidTestImplementation 'androidx.test.ext:junit:1.1.4' diff --git a/src/main/java/com/configcat/Utils.java b/src/main/java/com/configcat/Utils.java index b09a018..4823428 100644 --- a/src/main/java/com/configcat/Utils.java +++ b/src/main/java/com/configcat/Utils.java @@ -56,7 +56,7 @@ private Constants() { /* prevent from instantiation*/ } static final long DISTANT_PAST = 0; static final String CONFIG_JSON_NAME = "config_v6.json"; static final String SERIALIZATION_FORMAT_VERSION = "v2"; - static final String VERSION = "11.1.0"; + static final String VERSION = "10.5.0"; static final String SDK_KEY_PROXY_PREFIX = "configcat-proxy/"; static final String SDK_KEY_PREFIX = "configcat-sdk-1"; From 01af8d50d08fc9c78f76720987fb3a11a2aa9cd1 Mon Sep 17 00:00:00 2001 From: Peter Csajtai Date: Wed, 17 Dec 2025 10:50:11 +0100 Subject: [PATCH 16/17] Bump major version --- gradle.properties | 2 +- samples/android-java/app/build.gradle | 2 +- samples/android-kotlin/app/build.gradle | 2 +- src/main/java/com/configcat/Utils.java | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/gradle.properties b/gradle.properties index 63bf32e..d0ec49d 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,3 +1,3 @@ -version=10.5.0 +version=11.0.0 org.gradle.jvmargs=-Xmx2g \ No newline at end of file diff --git a/samples/android-java/app/build.gradle b/samples/android-java/app/build.gradle index 94555d5..1bef44e 100644 --- a/samples/android-java/app/build.gradle +++ b/samples/android-java/app/build.gradle @@ -36,7 +36,7 @@ dependencies { implementation 'androidx.lifecycle:lifecycle-viewmodel:2.4.0' implementation 'com.google.android.material:material:1.5.0' implementation 'androidx.constraintlayout:constraintlayout:2.1.4' - implementation 'com.configcat:configcat-android-client:10.5.0' + implementation 'com.configcat:configcat-android-client:11.0.0' implementation 'org.slf4j:slf4j-api:2.0.7' implementation 'com.github.tony19:logback-android:3.0.0' } \ No newline at end of file diff --git a/samples/android-kotlin/app/build.gradle b/samples/android-kotlin/app/build.gradle index 048ff17..eba8336 100644 --- a/samples/android-kotlin/app/build.gradle +++ b/samples/android-kotlin/app/build.gradle @@ -36,7 +36,7 @@ dependencies { implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.7.21' implementation 'androidx.appcompat:appcompat:1.5.1' implementation 'androidx.constraintlayout:constraintlayout:2.1.4' - implementation 'com.configcat:configcat-android-client:10.5.0' + implementation 'com.configcat:configcat-android-client:11.0.0' implementation 'com.noveogroup.android:android-logger:1.3.1' testImplementation 'junit:junit:4.13.2' androidTestImplementation 'androidx.test.ext:junit:1.1.4' diff --git a/src/main/java/com/configcat/Utils.java b/src/main/java/com/configcat/Utils.java index 4823428..a118b0a 100644 --- a/src/main/java/com/configcat/Utils.java +++ b/src/main/java/com/configcat/Utils.java @@ -56,7 +56,7 @@ private Constants() { /* prevent from instantiation*/ } static final long DISTANT_PAST = 0; static final String CONFIG_JSON_NAME = "config_v6.json"; static final String SERIALIZATION_FORMAT_VERSION = "v2"; - static final String VERSION = "10.5.0"; + static final String VERSION = "11.0.0"; static final String SDK_KEY_PROXY_PREFIX = "configcat-proxy/"; static final String SDK_KEY_PREFIX = "configcat-sdk-1"; From 1f182ce7651fa4bea3ee3d08171f2eb1cf6021f4 Mon Sep 17 00:00:00 2001 From: Peter Csajtai Date: Fri, 19 Dec 2025 13:02:37 +0100 Subject: [PATCH 17/17] Update AppStateMonitor.java --- .../java/com/configcat/AppStateMonitor.java | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/configcat/AppStateMonitor.java b/src/main/java/com/configcat/AppStateMonitor.java index 0d8725d..0016423 100644 --- a/src/main/java/com/configcat/AppStateMonitor.java +++ b/src/main/java/com/configcat/AppStateMonitor.java @@ -134,10 +134,15 @@ public void onLowMemory() { @SuppressWarnings("deprecation") public boolean isNetworkAvailable() { - ConnectivityManager conn = - (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); - NetworkInfo networkInfo = conn.getActiveNetworkInfo(); - return networkInfo != null && networkInfo.isConnected(); + try { + ConnectivityManager conn = + (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); + NetworkInfo networkInfo = conn.getActiveNetworkInfo(); + return networkInfo != null && networkInfo.isConnected(); + } catch (SecurityException e) { + // Fall back to assuming a network is available + return true; + } } @Override @@ -148,7 +153,11 @@ public void close() throws IOException { } finally { lock.writeLock().unlock(); } - application.unregisterReceiver(this); + try { + application.unregisterReceiver(this); + } catch (IllegalArgumentException e) { + // ignore, the receiver was not registered + } application.unregisterActivityLifecycleCallbacks(this); application.unregisterComponentCallbacks(this); }