Skip to content

Commit cbdd29f

Browse files
Use manual foreground detection to trigger network reconnect
1 parent 30ee760 commit cbdd29f

File tree

4 files changed

+53
-24
lines changed

4 files changed

+53
-24
lines changed

firebase-firestore/src/main/java/com/google/firebase/firestore/remote/AndroidConnectivityMonitor.java

Lines changed: 47 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,20 @@
1717
import static com.google.firebase.firestore.util.Assert.hardAssert;
1818

1919
import android.annotation.TargetApi;
20+
import android.app.Activity;
21+
import android.app.Application;
2022
import android.content.BroadcastReceiver;
2123
import android.content.Context;
2224
import android.content.Intent;
2325
import android.content.IntentFilter;
2426
import android.net.ConnectivityManager;
2527
import android.net.Network;
2628
import android.os.Build;
29+
import android.os.Bundle;
30+
import androidx.annotation.NonNull;
2731
import androidx.annotation.Nullable;
28-
import com.google.android.gms.common.api.internal.BackgroundDetector;
2932
import com.google.firebase.firestore.util.Consumer;
33+
import com.google.firebase.firestore.util.Logger;
3034
import java.util.ArrayList;
3135
import java.util.List;
3236

@@ -36,8 +40,9 @@
3640
* <p>Implementation note: Most of the code here was shamelessly stolen from
3741
* https://github.com/grpc/grpc-java/blob/master/android/src/main/java/io/grpc/android/AndroidChannelBuilder.java
3842
*/
39-
public final class AndroidConnectivityMonitor
40-
implements ConnectivityMonitor, BackgroundDetector.BackgroundStateChangeListener {
43+
public final class AndroidConnectivityMonitor implements ConnectivityMonitor {
44+
45+
private static final String LOG_TAG = "AndroidConnectivityMonitor";
4146

4247
private final Context context;
4348
@Nullable private final ConnectivityManager connectivityManager;
@@ -78,34 +83,56 @@ private void configureNetworkMonitoring() {
7883
final DefaultNetworkCallback defaultNetworkCallback = new DefaultNetworkCallback();
7984
connectivityManager.registerDefaultNetworkCallback(defaultNetworkCallback);
8085
unregisterRunnable =
81-
new Runnable() {
82-
@Override
83-
public void run() {
84-
connectivityManager.unregisterNetworkCallback(defaultNetworkCallback);
85-
}
86-
};
86+
() -> connectivityManager.unregisterNetworkCallback(defaultNetworkCallback);
8787
} else {
8888
NetworkReceiver networkReceiver = new NetworkReceiver();
8989
@SuppressWarnings("deprecation")
9090
IntentFilter networkIntentFilter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
9191
context.registerReceiver(networkReceiver, networkIntentFilter);
92-
unregisterRunnable =
93-
new Runnable() {
94-
@Override
95-
public void run() {
96-
context.unregisterReceiver(networkReceiver);
97-
}
98-
};
92+
unregisterRunnable = () -> context.unregisterReceiver(networkReceiver);
9993
}
10094
}
10195

10296
private void configureBackgroundStateListener() {
103-
BackgroundDetector.getInstance().addListener(this);
97+
// Manually register an ActivityLifecycleCallback. Android's BackgroundDetector only notifies
98+
// when it is certain that the app transitioned from background to foreground. Instead, we
99+
// want to be notified whenever there is a slight chance that this transition happened.
100+
((Application) context.getApplicationContext())
101+
.registerActivityLifecycleCallbacks(
102+
new Application.ActivityLifecycleCallbacks() {
103+
@Override
104+
public void onActivityCreated(@NonNull Activity activity, Bundle savedInstanceState) {
105+
raiseForegroundNotification();
106+
}
107+
108+
@Override
109+
public void onActivityStarted(@NonNull Activity activity) {
110+
raiseForegroundNotification();
111+
}
112+
113+
@Override
114+
public void onActivityResumed(@NonNull Activity activity) {
115+
raiseForegroundNotification();
116+
}
117+
118+
@Override
119+
public void onActivityPaused(@NonNull Activity activity) {}
120+
121+
@Override
122+
public void onActivityStopped(@NonNull Activity activity) {}
123+
124+
@Override
125+
public void onActivitySaveInstanceState(
126+
@NonNull Activity activity, @NonNull Bundle outState) {}
127+
128+
@Override
129+
public void onActivityDestroyed(@NonNull Activity activity) {}
130+
});
104131
}
105132

106-
@Override
107-
public void onBackgroundStateChanged(boolean background) {
108-
if (!background && isConnected()) {
133+
public void raiseForegroundNotification() {
134+
Logger.debug(LOG_TAG, "App has entered the foreground.");
135+
if (isConnected()) {
109136
raiseCallbacks(/* connected= */ true);
110137
}
111138
}

firebase-firestore/src/main/java/com/google/firebase/firestore/remote/OnlineStateTracker.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,11 @@ interface OnlineStateCallback {
8888
shouldWarnClientIsOffline = true;
8989
}
9090

91+
/** Returns the current online state. */
92+
OnlineState getState() {
93+
return state;
94+
}
95+
9196
/**
9297
* Called by RemoteStore when a watch stream is started (including on each backoff attempt).
9398
*

firebase-firestore/src/main/java/com/google/firebase/firestore/remote/RemoteStore.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ public void onClose(Status status) {
216216

217217
// If the network has been explicitly disabled, make sure we don't accidentally
218218
// re-enable it.
219-
if (canUseNetwork()) {
219+
if (!onlineStateTracker.getState().equals(OnlineState.ONLINE) && canUseNetwork()) {
220220
// Tear down and re-create our network streams. This will ensure the backoffs are
221221
// reset.
222222
Logger.debug(LOG_TAG, "Restarting streams for network reachability change.");

subprojects.cfg

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@ firebase-components:firebase-dynamic-module-support
1616
firebase-config
1717
firebase-config:ktx
1818
firebase-config:bandwagoner
19-
firebase-crashlytics
20-
firebase-crashlytics:ktx
21-
firebase-crashlytics-ndk
2219
firebase-database
2320
firebase-database:ktx
2421
firebase-database-collection

0 commit comments

Comments
 (0)