Skip to content
This repository was archived by the owner on Mar 10, 2022. It is now read-only.

Commit 2f91f48

Browse files
committed
Merge pull request #621 from couchbase/feature/issue_636_networkreachability
Fixed Java Core 636: Exel: Push replication "losing" documents create…
2 parents 2b6b65d + 42d5957 commit 2f91f48

File tree

2 files changed

+16
-42
lines changed

2 files changed

+16
-42
lines changed

src/main/AndroidManifest.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
22
package="com.couchbase.lite">
33

4-
<uses-permission android:name="android.permission.INTERNET"/>
4+
<uses-permission android:name="android.permission.INTERNET" />
5+
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
56

67
<application />
78

src/main/java/com/couchbase/lite/android/AndroidNetworkReachabilityManager.java

Lines changed: 14 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -4,44 +4,23 @@
44
import android.content.Intent;
55
import android.content.IntentFilter;
66
import android.net.ConnectivityManager;
7+
import android.net.NetworkInfo;
78

8-
import com.couchbase.lite.Context;
99
import com.couchbase.lite.NetworkReachabilityManager;
1010
import com.couchbase.lite.util.Log;
1111

1212
public class AndroidNetworkReachabilityManager extends NetworkReachabilityManager {
1313

14-
private Context context;
1514
private boolean listening;
1615
private android.content.Context wrappedContext;
1716
private ConnectivityBroadcastReceiver receiver;
18-
private State state;
1917

20-
public enum State {
21-
UNKNOWN,
22-
23-
/** This state is returned if there is connectivity to any network **/
24-
CONNECTED,
25-
/**
26-
* This state is returned if there is no connectivity to any network. This is set to true
27-
* under two circumstances:
28-
* <ul>
29-
* <li>When connectivity is lost to one network, and there is no other available network to
30-
* attempt to switch to.</li>
31-
* <li>When connectivity is lost to one network, and the attempt to switch to another
32-
* network fails.</li>
33-
*/
34-
NOT_CONNECTED
35-
}
36-
37-
public AndroidNetworkReachabilityManager(AndroidContext context) {
38-
this.context = context;
39-
this.wrappedContext = context.getWrappedContext();
18+
public AndroidNetworkReachabilityManager(AndroidContext androidContextcontext) {
19+
this.listening = false;
20+
this.wrappedContext = androidContextcontext.getWrappedContext();
4021
this.receiver = new ConnectivityBroadcastReceiver();
41-
this.state = State.UNKNOWN;
4222
}
4323

44-
4524
public synchronized void startListening() {
4625
if (!listening) {
4726
IntentFilter filter = new IntentFilter();
@@ -60,38 +39,32 @@ public synchronized void stopListening() {
6039
} catch (Exception e) {
6140
Log.e(Log.TAG_SYNC, "%s: stopListening() exception unregistering %s with context %s", e, this, receiver, wrappedContext);
6241
}
63-
context = null;
6442
listening = false;
6543
}
6644
}
6745

6846
private class ConnectivityBroadcastReceiver extends BroadcastReceiver {
69-
7047
@Override
7148
public void onReceive(android.content.Context context, Intent intent) {
7249
String action = intent.getAction();
73-
7450
if (!action.equals(ConnectivityManager.CONNECTIVITY_ACTION) || listening == false) {
7551
return;
7652
}
7753

78-
boolean noConnectivity = intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
54+
boolean bOnline = isOnline(context);
55+
Log.v(Log.TAG_SYNC, "BroadcastReceiver.onReceive() bOnline=" + bOnline);
7956

80-
if (noConnectivity) {
81-
state = State.NOT_CONNECTED;
57+
if (bOnline) {
58+
notifyListenersNetworkReachable();
8259
} else {
83-
state = State.CONNECTED;
84-
}
85-
86-
if (state == State.NOT_CONNECTED) {
8760
notifyListenersNetworkUneachable();
8861
}
89-
90-
if (state == State.CONNECTED) {
91-
notifyListenersNetworkReachable();
92-
}
93-
9462
}
95-
};
63+
}
9664

65+
private boolean isOnline(android.content.Context context) {
66+
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(android.content.Context.CONNECTIVITY_SERVICE);
67+
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
68+
return activeNetwork != null && activeNetwork.isConnected();
69+
}
9770
}

0 commit comments

Comments
 (0)