Skip to content

Commit

Permalink
Merge abd35ab into c9fa461
Browse files Browse the repository at this point in the history
  • Loading branch information
qdpham13 committed Mar 28, 2024
2 parents c9fa461 + abd35ab commit 47d73f0
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import java.util.Set;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import org.json.JSONException;
import org.json.JSONObject;

Expand All @@ -54,21 +55,24 @@ public class ConfigAutoFetch {
private final ConfigUpdateListener retryCallback;
private final ScheduledExecutorService scheduledExecutorService;
private final Random random;
private AtomicBoolean isInBackground;

public ConfigAutoFetch(
HttpURLConnection httpURLConnection,
ConfigFetchHandler configFetchHandler,
ConfigCacheClient activatedCache,
Set<ConfigUpdateListener> eventListeners,
ConfigUpdateListener retryCallback,
ScheduledExecutorService scheduledExecutorService) {
ScheduledExecutorService scheduledExecutorService,
AtomicBoolean isInBackground) {
this.httpURLConnection = httpURLConnection;
this.configFetchHandler = configFetchHandler;
this.activatedCache = activatedCache;
this.eventListeners = eventListeners;
this.retryCallback = retryCallback;
this.scheduledExecutorService = scheduledExecutorService;
this.random = new Random();
this.isInBackground = isInBackground;
}

private synchronized void propagateErrors(FirebaseRemoteConfigException exception) {
Expand Down Expand Up @@ -126,7 +130,7 @@ private void handleNotifications(InputStream inputStream) throws IOException {
// Multiple config update messages can be sent through this loop. Each message comes in line by
// line as partialConfigUpdateMessage and are accumulated together into
// currentConfigUpdateMessage.
while ((partialConfigUpdateMessage = reader.readLine()) != null) {
while ((partialConfigUpdateMessage = reader.readLine()) != null && !isInBackground.get()) {
// Accumulate all the partial parts of the message until we have a full message.
currentConfigUpdateMessage += partialConfigUpdateMessage;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
import java.util.Set;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.json.JSONObject;
Expand Down Expand Up @@ -98,7 +99,7 @@ public class ConfigRealtimeHttpClient {
private boolean isRealtimeDisabled;

/** Flag to indicate whether or not the app is in the background or not. */
private boolean isInBackground;
private AtomicBoolean isInBackground;

private final int ORIGINAL_RETRIES = 8;
private final ScheduledExecutorService scheduledExecutorService;
Expand Down Expand Up @@ -144,7 +145,7 @@ public ConfigRealtimeHttpClient(
this.namespace = namespace;
this.metadataClient = metadataClient;
this.isRealtimeDisabled = false;
this.isInBackground = false;
this.isInBackground = new AtomicBoolean(false);
}

private static String extractProjectNumberFromAppId(String gmpAppId) {
Expand Down Expand Up @@ -286,7 +287,7 @@ private synchronized boolean canMakeHttpStreamConnection() {
return !listeners.isEmpty()
&& !isHttpConnectionRunning
&& !isRealtimeDisabled
&& !isInBackground;
&& !isInBackground.get();
}

private String getRealtimeURL(String namespace) {
Expand Down Expand Up @@ -383,7 +384,7 @@ public void run() {
},
retryMilliseconds,
TimeUnit.MILLISECONDS);
} else if (!isInBackground) {
} else if (!isInBackground.get()) {
propagateErrors(
new FirebaseRemoteConfigClientException(
"Unable to connect to the server. Check your connection and try again.",
Expand All @@ -392,7 +393,7 @@ public void run() {
}

void setRealtimeBackgroundState(boolean backgroundState) {
isInBackground = backgroundState;
isInBackground.set(backgroundState);
}

private synchronized void resetRetryCount() {
Expand Down Expand Up @@ -429,7 +430,8 @@ public void onError(@NonNull FirebaseRemoteConfigException error) {
activatedCache,
listeners,
retryCallback,
scheduledExecutorService);
scheduledExecutorService,
isInBackground);
}

// HTTP status code that the Realtime client should retry on.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
Expand Down Expand Up @@ -348,7 +349,8 @@ public void onError(@NonNull FirebaseRemoteConfigException error) {
mockActivatedCache,
listeners,
mockRetryListener,
scheduledExecutorService);
scheduledExecutorService,
new AtomicBoolean(false));
realtimeMetadataClient =
new ConfigMetadataClient(context.getSharedPreferences("test_file", Context.MODE_PRIVATE));
configRealtimeHttpClient =
Expand Down

0 comments on commit 47d73f0

Please sign in to comment.