Skip to content

Commit db0d4b5

Browse files
Merge pull request #74 from PieterVanPoyer/master
CB-12035 (android) Fix bug [cordova-plugin-network-information] connection info is not reliable on Android 6
2 parents 5158556 + 9a45d63 commit db0d4b5

File tree

1 file changed

+79
-32
lines changed

1 file changed

+79
-32
lines changed

src/android/NetworkManager.java

Lines changed: 79 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ Licensed to the Apache Software Foundation (ASF) under one
3434
import android.content.IntentFilter;
3535
import android.net.ConnectivityManager;
3636
import android.net.NetworkInfo;
37+
import android.os.Build;
3738

3839
import java.util.Locale;
3940

@@ -99,21 +100,7 @@ public void initialize(CordovaInterface cordova, CordovaWebView webView) {
99100
this.sockMan = (ConnectivityManager) cordova.getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
100101
this.connectionCallbackContext = null;
101102

102-
// We need to listen to connectivity events to update navigator.connection
103-
IntentFilter intentFilter = new IntentFilter();
104-
intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
105-
if (this.receiver == null) {
106-
this.receiver = new BroadcastReceiver() {
107-
@Override
108-
public void onReceive(Context context, Intent intent) {
109-
// (The null check is for the ARM Emulator, please use Intel Emulator for better results)
110-
if(NetworkManager.this.webView != null)
111-
updateConnectionInfo(sockMan.getActiveNetworkInfo());
112-
}
113-
};
114-
webView.getContext().registerReceiver(this.receiver, intentFilter);
115-
}
116-
103+
this.registerConnectivityActionReceiver();
117104
}
118105

119106
/**
@@ -147,6 +134,70 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo
147134
* Stop network receiver.
148135
*/
149136
public void onDestroy() {
137+
this.unregisterReceiver();
138+
}
139+
140+
@Override
141+
public void onPause(boolean multitasking) {
142+
this.unregisterReceiver();
143+
}
144+
145+
@Override
146+
public void onResume(boolean multitasking) {
147+
super.onResume(multitasking);
148+
149+
this.unregisterReceiver();
150+
this.registerConnectivityActionReceiver();
151+
}
152+
153+
//--------------------------------------------------------------------------
154+
// LOCAL METHODS
155+
//--------------------------------------------------------------------------
156+
157+
private void registerConnectivityActionReceiver() {
158+
// We need to listen to connectivity events to update navigator.connection
159+
IntentFilter intentFilter = new IntentFilter();
160+
intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
161+
if (this.receiver == null) {
162+
this.receiver = new BroadcastReceiver() {
163+
@Override
164+
public void onReceive(Context context, Intent intent) {
165+
// (The null check is for the ARM Emulator, please use Intel Emulator for better results)
166+
if (NetworkManager.this.webView != null) {
167+
updateConnectionInfo(sockMan.getActiveNetworkInfo());
168+
}
169+
170+
String connectionType = null;
171+
if(NetworkManager.this.lastInfo == null) {
172+
connectionType = TYPE_NONE;
173+
} else {
174+
try {
175+
connectionType = NetworkManager.this.lastInfo.get("type").toString();
176+
} catch (JSONException e) {
177+
LOG.d(LOG_TAG, e.getLocalizedMessage());
178+
connectionType = TYPE_NONE;
179+
}
180+
}
181+
182+
// Lollipop always returns false for the EXTRA_NO_CONNECTIVITY flag => fix for Android M and above.
183+
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && TYPE_NONE.equals(connectionType)) {
184+
boolean noConnectivity = intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
185+
LOG.d(LOG_TAG, "Intent no connectivity: " + noConnectivity);
186+
if(noConnectivity) {
187+
LOG.d(LOG_TAG, "Really no connectivity");
188+
} else {
189+
LOG.d(LOG_TAG, "!!! Switching to unknown, Intent states there is a connectivity.");
190+
sendUpdate(TYPE_UNKNOWN);
191+
}
192+
}
193+
}
194+
};
195+
}
196+
197+
webView.getContext().registerReceiver(this.receiver, intentFilter);
198+
}
199+
200+
private void unregisterReceiver() {
150201
if (this.receiver != null) {
151202
try {
152203
webView.getContext().unregisterReceiver(this.receiver);
@@ -158,10 +209,6 @@ public void onDestroy() {
158209
}
159210
}
160211

161-
//--------------------------------------------------------------------------
162-
// LOCAL METHODS
163-
//--------------------------------------------------------------------------
164-
165212
/**
166213
* Updates the JavaScript side whenever the connection changes
167214
*
@@ -256,25 +303,25 @@ else if (type.toLowerCase().equals(TYPE_ETHERNET) || type.toLowerCase().startsWi
256303
else if (type.equals(MOBILE) || type.equals(CELLULAR)) {
257304
type = info.getSubtypeName().toLowerCase(Locale.US);
258305
if (type.equals(GSM) ||
259-
type.equals(GPRS) ||
260-
type.equals(EDGE) ||
261-
type.equals(TWO_G)) {
306+
type.equals(GPRS) ||
307+
type.equals(EDGE) ||
308+
type.equals(TWO_G)) {
262309
return TYPE_2G;
263310
}
264311
else if (type.startsWith(CDMA) ||
265-
type.equals(UMTS) ||
266-
type.equals(ONEXRTT) ||
267-
type.equals(EHRPD) ||
268-
type.equals(HSUPA) ||
269-
type.equals(HSDPA) ||
270-
type.equals(HSPA) ||
271-
type.equals(THREE_G)) {
312+
type.equals(UMTS) ||
313+
type.equals(ONEXRTT) ||
314+
type.equals(EHRPD) ||
315+
type.equals(HSUPA) ||
316+
type.equals(HSDPA) ||
317+
type.equals(HSPA) ||
318+
type.equals(THREE_G)) {
272319
return TYPE_3G;
273320
}
274321
else if (type.equals(LTE) ||
275-
type.equals(UMB) ||
276-
type.equals(HSPA_PLUS) ||
277-
type.equals(FOUR_G)) {
322+
type.equals(UMB) ||
323+
type.equals(HSPA_PLUS) ||
324+
type.equals(FOUR_G)) {
278325
return TYPE_4G;
279326
}
280327
}

0 commit comments

Comments
 (0)