@@ -28,17 +28,13 @@ Licensed to the Apache Software Foundation (ASF) under one
28
28
import org .json .JSONException ;
29
29
import org .json .JSONObject ;
30
30
31
- import android .annotation .TargetApi ;
32
31
import android .content .BroadcastReceiver ;
33
32
import android .content .Context ;
34
33
import android .content .Intent ;
35
34
import android .content .IntentFilter ;
36
35
import android .net .ConnectivityManager ;
37
- import android .net .Network ;
38
36
import android .net .NetworkInfo ;
39
- import android .net .NetworkRequest ;
40
37
import android .os .Build ;
41
- import android .text .TextUtils ;
42
38
43
39
import java .util .Locale ;
44
40
@@ -90,7 +86,6 @@ public class NetworkManager extends CordovaPlugin {
90
86
91
87
ConnectivityManager sockMan ;
92
88
BroadcastReceiver receiver ;
93
- private ConnectivityManager .NetworkCallback lollipopAndAboveNetworkCallback ;
94
89
private JSONObject lastInfo = null ;
95
90
96
91
/**
@@ -105,11 +100,7 @@ public void initialize(CordovaInterface cordova, CordovaWebView webView) {
105
100
this .sockMan = (ConnectivityManager ) cordova .getActivity ().getSystemService (Context .CONNECTIVITY_SERVICE );
106
101
this .connectionCallbackContext = null ;
107
102
108
- if (Build .VERSION .SDK_INT < Build .VERSION_CODES .LOLLIPOP ) {
109
- this .registerConnectivityActionReceiver ();
110
- } else {
111
- this .registerLollipopAndAboveNetworkCallback ();
112
- }
103
+ this .registerConnectivityActionReceiver ();
113
104
}
114
105
115
106
/**
@@ -156,63 +147,40 @@ public void onResume(boolean multitasking) {
156
147
super .onResume (multitasking );
157
148
158
149
this .unregisterReceiver ();
159
- if (Build .VERSION .SDK_INT < Build .VERSION_CODES .LOLLIPOP ) {
160
- this .registerConnectivityActionReceiver ();
161
- } else {
162
- this .registerLollipopAndAboveNetworkCallback ();
163
- }
150
+ this .registerConnectivityActionReceiver ();
164
151
}
165
152
166
153
//--------------------------------------------------------------------------
167
154
// LOCAL METHODS
168
155
//--------------------------------------------------------------------------
169
156
170
- @ TargetApi (Build .VERSION_CODES .LOLLIPOP )
171
- private void registerLollipopAndAboveNetworkCallback () {
172
- NetworkRequest .Builder builder = new NetworkRequest .Builder ();
173
-
174
- lollipopAndAboveNetworkCallback = new ConnectivityManager .NetworkCallback () {
175
-
176
- @ Override
177
- public void onAvailable (Network network ) {
178
- LOG .d (LOG_TAG , "In the on available: " );
179
- updateConnectionInfoIfWebViewNotNull (sockMan .getActiveNetworkInfo ());
180
-
181
- String connectionType = determineCurrentConnectionType ();
182
-
183
- if (TYPE_NONE .equals (connectionType )) {
184
- LOG .d (LOG_TAG , "ConnectionType none but in the onAvailable" );
185
- LOG .d (LOG_TAG , "!!! Switching to unknown, onAvailable states there is a connectivity." );
186
- sendUpdate (TYPE_UNKNOWN );
187
- }
188
- LOG .d (LOG_TAG , "End the on available: " );
189
- }
190
-
191
- @ Override
192
- public void onLost (Network network ) {
193
- LOG .d (LOG_TAG , "In the on lost: " );
194
- updateConnectionInfoIfWebViewNotNull (sockMan .getActiveNetworkInfo ());
195
- LOG .d (LOG_TAG , "End the on lost: " );
196
- }
197
- };
198
- sockMan .registerNetworkCallback (builder .build (), lollipopAndAboveNetworkCallback );
199
- }
200
-
201
157
private void registerConnectivityActionReceiver () {
202
158
// We need to listen to connectivity events to update navigator.connection
203
159
IntentFilter intentFilter = new IntentFilter ();
204
160
intentFilter .addAction (ConnectivityManager .CONNECTIVITY_ACTION );
205
-
206
161
if (this .receiver == null ) {
207
162
this .receiver = new BroadcastReceiver () {
208
163
@ Override
209
164
public void onReceive (Context context , Intent intent ) {
210
- updateConnectionInfoIfWebViewNotNull (sockMan .getActiveNetworkInfo ());
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
+ }
211
169
212
- String connectionType = determineCurrentConnectionType ();
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
+ }
213
181
214
- LOG . d ( LOG_TAG , "Intent " + intent . getAction ());
215
- if (TYPE_NONE .equals (connectionType )) {
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 )) {
216
184
boolean noConnectivity = intent .getBooleanExtra (ConnectivityManager .EXTRA_NO_CONNECTIVITY , false );
217
185
LOG .d (LOG_TAG , "Intent no connectivity: " + noConnectivity );
218
186
if (noConnectivity ) {
@@ -229,39 +197,14 @@ public void onReceive(Context context, Intent intent) {
229
197
webView .getContext ().registerReceiver (this .receiver , intentFilter );
230
198
}
231
199
232
- private String determineCurrentConnectionType () {
233
- String connectionType ;
234
- if (NetworkManager .this .lastInfo == null ) {
235
- connectionType = TYPE_NONE ;
236
- } else {
237
- try {
238
- connectionType = NetworkManager .this .lastInfo .get ("type" ).toString ();
239
- } catch (JSONException e ) {
240
- LOG .d (LOG_TAG , e .getLocalizedMessage ());
241
- connectionType = TYPE_NONE ;
242
- }
243
- }
244
- return connectionType ;
245
- }
246
-
247
200
private void unregisterReceiver () {
248
- if (Build .VERSION .SDK_INT < Build .VERSION_CODES .LOLLIPOP ) {
249
- if (this .receiver != null ) {
250
- try {
251
- webView .getContext ().unregisterReceiver (this .receiver );
252
- } catch (Exception e ) {
253
- LOG .e (LOG_TAG , "Error unregistering network receiver: " + e .getMessage (), e );
254
- } finally {
255
- receiver = null ;
256
- }
257
- }
258
- } else if (this .lollipopAndAboveNetworkCallback != null ) {
201
+ if (this .receiver != null ) {
259
202
try {
260
- sockMan . unregisterNetworkCallback ( this .lollipopAndAboveNetworkCallback );
203
+ webView . getContext (). unregisterReceiver ( this .receiver );
261
204
} catch (Exception e ) {
262
205
LOG .e (LOG_TAG , "Error unregistering network receiver: " + e .getMessage (), e );
263
206
} finally {
264
- lollipopAndAboveNetworkCallback = null ;
207
+ receiver = null ;
265
208
}
266
209
}
267
210
}
@@ -272,11 +215,7 @@ private void unregisterReceiver() {
272
215
* @param info the current active network info
273
216
* @return
274
217
*/
275
- private void updateConnectionInfoIfWebViewNotNull (NetworkInfo info ) {
276
- // (The null check is for the ARM Emulator, please use Intel Emulator for better results)
277
- if (NetworkManager .this .webView == null ) {
278
- return ;
279
- }
218
+ private void updateConnectionInfo (NetworkInfo info ) {
280
219
// send update to javascript "navigator.network.connection"
281
220
// Jellybean sends its own info
282
221
JSONObject thisInfo = this .getConnectionInfo (info );
0 commit comments