Skip to content

Commit cafdd67

Browse files
Try to fix: #64: in case of TYPE_NONE (android bug?) return TYPE_UNKNOWN if ConnectivityManager.EXTRA_NO_CONNECTIVITY from the intent return false.
1 parent e816db4 commit cafdd67

File tree

1 file changed

+64
-19
lines changed

1 file changed

+64
-19
lines changed

src/android/NetworkManager.java

+64-19
Original file line numberDiff line numberDiff line change
@@ -99,21 +99,7 @@ public void initialize(CordovaInterface cordova, CordovaWebView webView) {
9999
this.sockMan = (ConnectivityManager) cordova.getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
100100
this.connectionCallbackContext = null;
101101

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-
102+
this.registerConnectivityActionReceiver();
117103
}
118104

119105
/**
@@ -147,6 +133,69 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo
147133
* Stop network receiver.
148134
*/
149135
public void onDestroy() {
136+
this.unregisterReceiver();
137+
}
138+
139+
@Override
140+
public void onPause(boolean multitasking) {
141+
this.unregisterReceiver();
142+
}
143+
144+
@Override
145+
public void onResume(boolean multitasking) {
146+
super.onResume(multitasking);
147+
148+
this.unregisterReceiver();
149+
this.registerConnectivityActionReceiver();
150+
}
151+
152+
//--------------------------------------------------------------------------
153+
// LOCAL METHODS
154+
//--------------------------------------------------------------------------
155+
156+
private void registerConnectivityActionReceiver() {
157+
// We need to listen to connectivity events to update navigator.connection
158+
IntentFilter intentFilter = new IntentFilter();
159+
intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
160+
if (this.receiver == null) {
161+
this.receiver = new BroadcastReceiver() {
162+
@Override
163+
public void onReceive(Context context, Intent intent) {
164+
// (The null check is for the ARM Emulator, please use Intel Emulator for better results)
165+
if (NetworkManager.this.webView != null) {
166+
updateConnectionInfo(sockMan.getActiveNetworkInfo());
167+
}
168+
169+
String connectionType = null;
170+
if(NetworkManager.this.lastInfo == null) {
171+
connectionType = TYPE_NONE;
172+
} else {
173+
try {
174+
connectionType = NetworkManager.this.lastInfo.get("type").toString();
175+
} catch (JSONException e) {
176+
LOG.d(LOG_TAG, e.getLocalizedMessage());
177+
connectionType = TYPE_NONE;
178+
}
179+
}
180+
181+
if(TYPE_NONE.equals(connectionType)) {
182+
boolean noConnectivity = intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
183+
LOG.d(LOG_TAG, "Intent no connectivity: " + noConnectivity);
184+
if(noConnectivity) {
185+
LOG.d(LOG_TAG, "Really no connectivity");
186+
} else {
187+
LOG.d(LOG_TAG, "!!! Switching to unknown");
188+
sendUpdate(TYPE_UNKNOWN);
189+
}
190+
}
191+
}
192+
};
193+
}
194+
195+
webView.getContext().registerReceiver(this.receiver, intentFilter);
196+
}
197+
198+
private void unregisterReceiver() {
150199
if (this.receiver != null) {
151200
try {
152201
webView.getContext().unregisterReceiver(this.receiver);
@@ -158,10 +207,6 @@ public void onDestroy() {
158207
}
159208
}
160209

161-
//--------------------------------------------------------------------------
162-
// LOCAL METHODS
163-
//--------------------------------------------------------------------------
164-
165210
/**
166211
* Updates the JavaScript side whenever the connection changes
167212
*

0 commit comments

Comments
 (0)