@@ -34,6 +34,7 @@ Licensed to the Apache Software Foundation (ASF) under one
34
34
import android .content .IntentFilter ;
35
35
import android .net .ConnectivityManager ;
36
36
import android .net .NetworkInfo ;
37
+ import android .os .Build ;
37
38
38
39
import java .util .Locale ;
39
40
@@ -99,21 +100,7 @@ public void initialize(CordovaInterface cordova, CordovaWebView webView) {
99
100
this .sockMan = (ConnectivityManager ) cordova .getActivity ().getSystemService (Context .CONNECTIVITY_SERVICE );
100
101
this .connectionCallbackContext = null ;
101
102
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 ();
117
104
}
118
105
119
106
/**
@@ -147,6 +134,70 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo
147
134
* Stop network receiver.
148
135
*/
149
136
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 () {
150
201
if (this .receiver != null ) {
151
202
try {
152
203
webView .getContext ().unregisterReceiver (this .receiver );
@@ -158,10 +209,6 @@ public void onDestroy() {
158
209
}
159
210
}
160
211
161
- //--------------------------------------------------------------------------
162
- // LOCAL METHODS
163
- //--------------------------------------------------------------------------
164
-
165
212
/**
166
213
* Updates the JavaScript side whenever the connection changes
167
214
*
@@ -256,25 +303,25 @@ else if (type.toLowerCase().equals(TYPE_ETHERNET) || type.toLowerCase().startsWi
256
303
else if (type .equals (MOBILE ) || type .equals (CELLULAR )) {
257
304
type = info .getSubtypeName ().toLowerCase (Locale .US );
258
305
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 )) {
262
309
return TYPE_2G ;
263
310
}
264
311
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 )) {
272
319
return TYPE_3G ;
273
320
}
274
321
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 )) {
278
325
return TYPE_4G ;
279
326
}
280
327
}
0 commit comments