@@ -28,17 +28,13 @@ Licensed to the Apache Software Foundation (ASF) under one
2828import org .json .JSONException ;
2929import org .json .JSONObject ;
3030
31- import android .annotation .TargetApi ;
3231import android .content .BroadcastReceiver ;
3332import android .content .Context ;
3433import android .content .Intent ;
3534import android .content .IntentFilter ;
3635import android .net .ConnectivityManager ;
37- import android .net .Network ;
3836import android .net .NetworkInfo ;
39- import android .net .NetworkRequest ;
4037import android .os .Build ;
41- import android .text .TextUtils ;
4238
4339import java .util .Locale ;
4440
@@ -90,7 +86,6 @@ public class NetworkManager extends CordovaPlugin {
9086
9187 ConnectivityManager sockMan ;
9288 BroadcastReceiver receiver ;
93- private ConnectivityManager .NetworkCallback lollipopAndAboveNetworkCallback ;
9489 private JSONObject lastInfo = null ;
9590
9691 /**
@@ -105,11 +100,7 @@ public void initialize(CordovaInterface cordova, CordovaWebView webView) {
105100 this .sockMan = (ConnectivityManager ) cordova .getActivity ().getSystemService (Context .CONNECTIVITY_SERVICE );
106101 this .connectionCallbackContext = null ;
107102
108- if (Build .VERSION .SDK_INT < Build .VERSION_CODES .LOLLIPOP ) {
109- this .registerConnectivityActionReceiver ();
110- } else {
111- this .registerLollipopAndAboveNetworkCallback ();
112- }
103+ this .registerConnectivityActionReceiver ();
113104 }
114105
115106 /**
@@ -156,63 +147,40 @@ public void onResume(boolean multitasking) {
156147 super .onResume (multitasking );
157148
158149 this .unregisterReceiver ();
159- if (Build .VERSION .SDK_INT < Build .VERSION_CODES .LOLLIPOP ) {
160- this .registerConnectivityActionReceiver ();
161- } else {
162- this .registerLollipopAndAboveNetworkCallback ();
163- }
150+ this .registerConnectivityActionReceiver ();
164151 }
165152
166153 //--------------------------------------------------------------------------
167154 // LOCAL METHODS
168155 //--------------------------------------------------------------------------
169156
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-
201157 private void registerConnectivityActionReceiver () {
202158 // We need to listen to connectivity events to update navigator.connection
203159 IntentFilter intentFilter = new IntentFilter ();
204160 intentFilter .addAction (ConnectivityManager .CONNECTIVITY_ACTION );
205-
206161 if (this .receiver == null ) {
207162 this .receiver = new BroadcastReceiver () {
208163 @ Override
209164 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+ }
211169
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+ }
213181
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 )) {
216184 boolean noConnectivity = intent .getBooleanExtra (ConnectivityManager .EXTRA_NO_CONNECTIVITY , false );
217185 LOG .d (LOG_TAG , "Intent no connectivity: " + noConnectivity );
218186 if (noConnectivity ) {
@@ -229,39 +197,14 @@ public void onReceive(Context context, Intent intent) {
229197 webView .getContext ().registerReceiver (this .receiver , intentFilter );
230198 }
231199
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-
247200 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 ) {
259202 try {
260- sockMan . unregisterNetworkCallback ( this .lollipopAndAboveNetworkCallback );
203+ webView . getContext (). unregisterReceiver ( this .receiver );
261204 } catch (Exception e ) {
262205 LOG .e (LOG_TAG , "Error unregistering network receiver: " + e .getMessage (), e );
263206 } finally {
264- lollipopAndAboveNetworkCallback = null ;
207+ receiver = null ;
265208 }
266209 }
267210 }
@@ -272,11 +215,7 @@ private void unregisterReceiver() {
272215 * @param info the current active network info
273216 * @return
274217 */
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 ) {
280219 // send update to javascript "navigator.network.connection"
281220 // Jellybean sends its own info
282221 JSONObject thisInfo = this .getConnectionInfo (info );
0 commit comments