You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by no...@apache.org on 2020/07/08 13:16:44 UTC

[cordova-plugin-network-information] branch master updated: Android - Fixes https://github.com/apache/cordova-plugin-network-information#110 (#114)

This is an automated email from the ASF dual-hosted git repository.

normanbreau pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/cordova-plugin-network-information.git


The following commit(s) were added to refs/heads/master by this push:
     new 3066e3c  Android - Fixes https://github.com/apache/cordova-plugin-network-information#110 (#114)
3066e3c is described below

commit 3066e3cf88dc40249009ee81e691eaea665ceed5
Author: Pieter Van Poyer <pi...@portofantwerp.com>
AuthorDate: Wed Jul 8 15:14:41 2020 +0200

    Android - Fixes https://github.com/apache/cordova-plugin-network-information#110 (#114)
    
    * Try to fix https://github.com/apache/cordova-plugin-network-information/issues/110 - The android JSONObject doesn't provide an equal implementation for JSON-Object.
    
    * undo autoreformatting of android studio
    
    * Android - remove JSONObject and extraInfo. KIS.
    
    * Update src/android/NetworkManager.java
    
    Co-authored-by: Tim Brust <ra...@gmx.de>
    
    * Update src/android/NetworkManager.java
    
    Co-authored-by: Tim Brust <ra...@gmx.de>
    
    * Update src/android/NetworkManager.java
    
    Co-authored-by: Tim Brust <ra...@gmx.de>
    
    * Android - code review PR
    
    * Android - code review PR -> remove obsolete logging of constant WIFI
    
    Co-authored-by: Tim Brust <ra...@gmx.de>
---
 src/android/NetworkManager.java | 105 +++++++++++++---------------------------
 1 file changed, 34 insertions(+), 71 deletions(-)

diff --git a/src/android/NetworkManager.java b/src/android/NetworkManager.java
index 8698d49..95c8f07 100755
--- a/src/android/NetworkManager.java
+++ b/src/android/NetworkManager.java
@@ -86,7 +86,7 @@ public class NetworkManager extends CordovaPlugin {
 
     ConnectivityManager sockMan;
     BroadcastReceiver receiver;
-    private JSONObject lastInfo = null;
+    private String lastTypeOfNetwork;
 
     /**
      * Sets the context of the Command. This can then be used to do things like
@@ -115,13 +115,7 @@ public class NetworkManager extends CordovaPlugin {
         if (action.equals("getConnectionInfo")) {
             this.connectionCallbackContext = callbackContext;
             NetworkInfo info = sockMan.getActiveNetworkInfo();
-            String connectionType = "";
-            try {
-                connectionType = this.getConnectionInfo(info).get("type").toString();
-            } catch (JSONException e) {
-                LOG.d(LOG_TAG, e.getLocalizedMessage());
-            }
-
+            String connectionType = this.getTypeOfNetworkFallbackToTypeNoneIfNotConnected(info);
             PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, connectionType);
             pluginResult.setKeepCallback(true);
             callbackContext.sendPluginResult(pluginResult);
@@ -167,16 +161,11 @@ public class NetworkManager extends CordovaPlugin {
                         updateConnectionInfo(sockMan.getActiveNetworkInfo());
                     }
 
-                    String connectionType = null;
-                    if(NetworkManager.this.lastInfo == null) {
+                    String connectionType;
+                    if (NetworkManager.this.lastTypeOfNetwork == null) {
                         connectionType = TYPE_NONE;
                     } else {
-                        try {
-                            connectionType = NetworkManager.this.lastInfo.get("type").toString();
-                        } catch (JSONException e) {
-                            LOG.d(LOG_TAG, e.getLocalizedMessage());
-                            connectionType = TYPE_NONE;
-                        }
+                        connectionType = NetworkManager.this.lastTypeOfNetwork;
                     }
 
                     // Lollipop always returns false for the EXTRA_NO_CONNECTIVITY flag => fix for Android M and above.
@@ -218,30 +207,24 @@ public class NetworkManager extends CordovaPlugin {
     private void updateConnectionInfo(NetworkInfo info) {
         // send update to javascript "navigator.connection"
         // Jellybean sends its own info
-        JSONObject thisInfo = this.getConnectionInfo(info);
-        if(!thisInfo.equals(lastInfo))
-        {
-            String connectionType = "";
-            try {
-                connectionType = thisInfo.get("type").toString();
-            } catch (JSONException e) {
-                LOG.d(LOG_TAG, e.getLocalizedMessage());
-            }
-
-            sendUpdate(connectionType);
-            lastInfo = thisInfo;
+        String currentNetworkType = this.getTypeOfNetworkFallbackToTypeNoneIfNotConnected(info);
+        if (currentNetworkType.equals(this.lastTypeOfNetwork)) {
+            LOG.d(LOG_TAG, "Networkinfo state didn't change, there is no event propagated to the JavaScript side.");
+        } else {
+            sendUpdate(currentNetworkType);
+            this.lastTypeOfNetwork = currentNetworkType;
         }
     }
 
     /**
-     * Get the latest network connection information
+     * Gets the type of network connection of the NetworkInfo input
      *
      * @param info the current active network info
-     * @return a JSONObject that represents the network info
+     * @return type the type of network
      */
-    private JSONObject getConnectionInfo(NetworkInfo info) {
-        String type = TYPE_NONE;
-        String extraInfo = "";
+    private String getTypeOfNetworkFallbackToTypeNoneIfNotConnected(NetworkInfo info) {
+        // the info might still be null in this part of the code
+        String type;
         if (info != null) {
             // If we are not connected to any network set type to none
             if (!info.isConnected()) {
@@ -250,22 +233,12 @@ public class NetworkManager extends CordovaPlugin {
             else {
                 type = getType(info);
             }
-            extraInfo = info.getExtraInfo();
+        } else {
+            type = TYPE_NONE;
         }
 
         LOG.d(LOG_TAG, "Connection Type: " + type);
-        LOG.d(LOG_TAG, "Connection Extra Info: " + extraInfo);
-
-        JSONObject connectionInfo = new JSONObject();
-
-        try {
-            connectionInfo.put("type", type);
-            connectionInfo.put("extraInfo", extraInfo);
-        } catch (JSONException e) {
-            LOG.d(LOG_TAG, e.getLocalizedMessage());
-        }
-
-        return connectionInfo;
+        return type;
     }
 
     /**
@@ -289,26 +262,21 @@ public class NetworkManager extends CordovaPlugin {
      * @return the type of mobile network we are on
      */
     private String getType(NetworkInfo info) {
-        if (info != null) {
-            String type = info.getTypeName().toLowerCase(Locale.US);
-
-            LOG.d(LOG_TAG, "toLower : " + type.toLowerCase());
-            LOG.d(LOG_TAG, "wifi : " + WIFI);
-            if (type.equals(WIFI)) {
-                return TYPE_WIFI;
-            }
-            else if (type.toLowerCase().equals(TYPE_ETHERNET) || type.toLowerCase().startsWith(TYPE_ETHERNET_SHORT)) {
-                return TYPE_ETHERNET;
-            }
-            else if (type.equals(MOBILE) || type.equals(CELLULAR)) {
-                type = info.getSubtypeName().toLowerCase(Locale.US);
-                if (type.equals(GSM) ||
+        String type = info.getTypeName().toLowerCase(Locale.US);
+
+        LOG.d(LOG_TAG, "toLower : " + type);
+        if (type.equals(WIFI)) {
+            return TYPE_WIFI;
+        } else if (type.toLowerCase().equals(TYPE_ETHERNET) || type.toLowerCase().startsWith(TYPE_ETHERNET_SHORT)) {
+            return TYPE_ETHERNET;
+        } else if (type.equals(MOBILE) || type.equals(CELLULAR)) {
+            type = info.getSubtypeName().toLowerCase(Locale.US);
+            if (type.equals(GSM) ||
                     type.equals(GPRS) ||
                     type.equals(EDGE) ||
                     type.equals(TWO_G)) {
-                    return TYPE_2G;
-                }
-                else if (type.startsWith(CDMA) ||
+                return TYPE_2G;
+            } else if (type.startsWith(CDMA) ||
                     type.equals(UMTS) ||
                     type.equals(ONEXRTT) ||
                     type.equals(EHRPD) ||
@@ -316,19 +284,14 @@ public class NetworkManager extends CordovaPlugin {
                     type.equals(HSDPA) ||
                     type.equals(HSPA) ||
                     type.equals(THREE_G)) {
-                    return TYPE_3G;
-                }
-                else if (type.equals(LTE) ||
+                return TYPE_3G;
+            } else if (type.equals(LTE) ||
                     type.equals(UMB) ||
                     type.equals(HSPA_PLUS) ||
                     type.equals(FOUR_G)) {
-                    return TYPE_4G;
-                }
+                return TYPE_4G;
             }
         }
-        else {
-            return TYPE_NONE;
-        }
         return TYPE_UNKNOWN;
     }
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cordova.apache.org
For additional commands, e-mail: commits-help@cordova.apache.org