erisu commented on code in PR #130:
URL: 
https://github.com/apache/cordova-plugin-network-information/pull/130#discussion_r995294370


##########
src/android/NetworkManager.java:
##########
@@ -46,17 +54,17 @@ public class NetworkManager extends CordovaPlugin {
 
     public static final String WIFI = "wifi";
     public static final String WIMAX = "wimax";
-    // mobile
+  // mobile

Review Comment:
   ```suggestion
       // mobile
   ```
   Just a nitpick, but can we change the indention level back to 4 spaces?
   
   I prefer a 2-space indention, but Cordova uses a 4-space indention 
everywhere.



##########
src/android/NetworkManager.java:
##########
@@ -65,11 +73,16 @@ public class NetworkManager extends CordovaPlugin {
     public static final String HSDPA = "hsdpa";
     public static final String ONEXRTT = "1xrtt";
     public static final String EHRPD = "ehrpd";
-    // 4G network types
+  // 4G network types
     public static final String FOUR_G = "4g";
     public static final String LTE = "lte";
     public static final String UMB = "umb";
     public static final String HSPA_PLUS = "hspa+";
+
+  // 5G network types

Review Comment:
   ```suggestion
       // 5G network types
   ```
   
   Same here.



##########
src/android/NetworkManager.java:
##########
@@ -46,17 +54,17 @@ public class NetworkManager extends CordovaPlugin {
 
     public static final String WIFI = "wifi";
     public static final String WIMAX = "wimax";
-    // mobile
+  // mobile
     public static final String MOBILE = "mobile";
 
-    // Android L calls this Cellular, because I have no idea!
+  // Android L calls this Cellular, because I have no idea!

Review Comment:
   ```suggestion
       // Android L calls this Cellular, because I have no idea!
   ```
   
   Same here.



##########
src/android/NetworkManager.java:
##########
@@ -96,15 +115,17 @@ public class NetworkManager extends CordovaPlugin {
      * @param webView The CordovaWebView Cordova is running in.
      */
     public void initialize(CordovaInterface cordova, CordovaWebView webView) {
-        super.initialize(cordova, webView);
-        this.sockMan = (ConnectivityManager) 
cordova.getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
-        this.connectionCallbackContext = null;
+    super.initialize(cordova, webView);
+    this.sockMan = (ConnectivityManager) 
cordova.getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
+    this.telMan = (TelephonyManager) 
cordova.getActivity().getSystemService(Context.TELEPHONY_SERVICE);
+    this.telMan.listen( phoneStateListener, LISTEN_SERVICE_STATE);
+    this.connectionCallbackContext = null;
 
-        this.registerConnectivityActionReceiver();
+    this.registerConnectivityActionReceiver();

Review Comment:
   ```suggestion
           this.registerConnectivityActionReceiver();
   ```
   
   Same here



##########
types/index.d.ts:
##########
@@ -57,6 +58,7 @@ declare var Connection: {
     CELL_2G: string;
     CELL_3G: string;
     CELL_4G: string;
+    CELL_5G: string;
     CELL: string;
     NONE: string;
 }

Review Comment:
   ```suggestion
   }
   
   ```
   
   Add new line at end of file.



##########
src/android/NetworkManager.java:
##########
@@ -96,15 +115,17 @@ public class NetworkManager extends CordovaPlugin {
      * @param webView The CordovaWebView Cordova is running in.
      */
     public void initialize(CordovaInterface cordova, CordovaWebView webView) {
-        super.initialize(cordova, webView);
-        this.sockMan = (ConnectivityManager) 
cordova.getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
-        this.connectionCallbackContext = null;
+    super.initialize(cordova, webView);
+    this.sockMan = (ConnectivityManager) 
cordova.getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
+    this.telMan = (TelephonyManager) 
cordova.getActivity().getSystemService(Context.TELEPHONY_SERVICE);
+    this.telMan.listen( phoneStateListener, LISTEN_SERVICE_STATE);
+    this.connectionCallbackContext = null;
 
-        this.registerConnectivityActionReceiver();
+    this.registerConnectivityActionReceiver();
     }
 
     /**
-     * Executes the request and returns PluginResult.
+ Executes the request and returns PluginResult.

Review Comment:
   ```suggestion
        * Executes the request and returns PluginResult.
   ```
   
   Revert this change.



##########
src/android/NetworkManager.java:
##########
@@ -270,28 +298,104 @@ private String getType(NetworkInfo info) {
         } 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) ||
-                    type.equals(UMTS) ||
-                    type.equals(ONEXRTT) ||
-                    type.equals(EHRPD) ||
-                    type.equals(HSUPA) ||
-                    type.equals(HSDPA) ||
-                    type.equals(HSPA) ||
-                    type.equals(THREE_G)) {
-                return TYPE_3G;
-            } else if (type.equals(LTE) ||
-                    type.equals(UMB) ||
-                    type.equals(HSPA_PLUS) ||
-                    type.equals(FOUR_G)) {
-                return TYPE_4G;
+            return getMobileType(info);
+        }
+        return TYPE_UNKNOWN;
+    }
+
+    /**
+     * Determine the subtype of mobile connection
+     *
+     * @param info the network info so we can determine connection type.
+     * @return the type of mobile network we are on
+     */
+    private String getMobileType(NetworkInfo info){
+        int subTypeId = info.getSubtype();
+        String subTypeName = info.getSubtypeName().toLowerCase(Locale.US);
+        if(is2G(subTypeId, subTypeName)){
+            return TYPE_2G;
+        } else if(is3G(subTypeId, subTypeName)) {
+            return TYPE_3G;
+        } else if(is4G(subTypeId, subTypeName)) {
+            if(isNrAvailable){ // if is LTE network could be 5g if NR is 
available
+                return TYPE_5G;
             }
+            return TYPE_4G;
+        } else if(is5G(subTypeId, subTypeName)) {
+            return TYPE_5G;
         }
         return TYPE_UNKNOWN;
     }
+
+    private boolean is2G(int type, String name){
+        return  type == TelephonyManager.NETWORK_TYPE_GPRS ||
+                type == TelephonyManager.NETWORK_TYPE_EDGE ||
+                type == TelephonyManager.NETWORK_TYPE_CDMA ||
+                type == TelephonyManager.NETWORK_TYPE_1xRTT ||
+                type == TelephonyManager.NETWORK_TYPE_IDEN ||    // api< 8: 
replace by 11
+                type == TelephonyManager.NETWORK_TYPE_GSM ||     // api<25: 
replace by 16
+                name.equals(GSM) ||
+                name.equals(GPRS) ||
+                name.equals(EDGE) ||
+                name.equals(TWO_G);
+    }
+
+    private boolean is3G(int type, String name){
+        return  type ==  TelephonyManager.NETWORK_TYPE_UMTS ||
+                type ==  TelephonyManager.NETWORK_TYPE_EVDO_0 ||
+                type ==  TelephonyManager.NETWORK_TYPE_EVDO_A ||
+                type ==  TelephonyManager.NETWORK_TYPE_HSDPA ||
+                type ==  TelephonyManager.NETWORK_TYPE_HSUPA ||
+                type ==  TelephonyManager.NETWORK_TYPE_HSPA ||
+                type ==  TelephonyManager.NETWORK_TYPE_EVDO_B ||   // api< 9: 
replace by 12
+                type ==  TelephonyManager.NETWORK_TYPE_EHRPD ||    // api<11: 
replace by 14
+                type ==  TelephonyManager.NETWORK_TYPE_HSPAP ||    // api<13: 
replace by 15
+                type ==  TelephonyManager.NETWORK_TYPE_TD_SCDMA || // api<25: 
replace by 17
+                name.startsWith(CDMA) ||
+                name.equals(UMTS) ||
+                name.equals(ONEXRTT) ||
+                name.equals(EHRPD) ||
+                name.equals(HSUPA) ||
+                name.equals(HSDPA) ||
+                name.equals(HSPA) ||
+                name.equals(THREE_G);
+    }
+
+    private boolean is4G(int type, String name){
+

Review Comment:
   ```suggestion
   ```
   
   Nitpick: No need of new line here.



##########
src/android/NetworkManager.java:
##########
@@ -96,15 +115,17 @@ public class NetworkManager extends CordovaPlugin {
      * @param webView The CordovaWebView Cordova is running in.
      */
     public void initialize(CordovaInterface cordova, CordovaWebView webView) {
-        super.initialize(cordova, webView);
-        this.sockMan = (ConnectivityManager) 
cordova.getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
-        this.connectionCallbackContext = null;
+    super.initialize(cordova, webView);
+    this.sockMan = (ConnectivityManager) 
cordova.getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
+    this.telMan = (TelephonyManager) 
cordova.getActivity().getSystemService(Context.TELEPHONY_SERVICE);
+    this.telMan.listen( phoneStateListener, LISTEN_SERVICE_STATE);
+    this.connectionCallbackContext = null;

Review Comment:
   ```suggestion
           super.initialize(cordova, webView);
           this.sockMan = (ConnectivityManager) 
cordova.getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
           this.telMan = (TelephonyManager) 
cordova.getActivity().getSystemService(Context.TELEPHONY_SERVICE);
           this.telMan.listen( phoneStateListener, LISTEN_SERVICE_STATE);
           this.connectionCallbackContext = null;
   ```
   
   Same here. It seems to not be indented at all within the method block.



##########
src/android/NetworkManager.java:
##########
@@ -270,28 +298,104 @@ private String getType(NetworkInfo info) {
         } 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) ||
-                    type.equals(UMTS) ||
-                    type.equals(ONEXRTT) ||
-                    type.equals(EHRPD) ||
-                    type.equals(HSUPA) ||
-                    type.equals(HSDPA) ||
-                    type.equals(HSPA) ||
-                    type.equals(THREE_G)) {
-                return TYPE_3G;
-            } else if (type.equals(LTE) ||
-                    type.equals(UMB) ||
-                    type.equals(HSPA_PLUS) ||
-                    type.equals(FOUR_G)) {
-                return TYPE_4G;
+            return getMobileType(info);
+        }
+        return TYPE_UNKNOWN;
+    }
+
+    /**
+     * Determine the subtype of mobile connection
+     *
+     * @param info the network info so we can determine connection type.
+     * @return the type of mobile network we are on
+     */
+    private String getMobileType(NetworkInfo info){
+        int subTypeId = info.getSubtype();
+        String subTypeName = info.getSubtypeName().toLowerCase(Locale.US);
+        if(is2G(subTypeId, subTypeName)){
+            return TYPE_2G;
+        } else if(is3G(subTypeId, subTypeName)) {
+            return TYPE_3G;
+        } else if(is4G(subTypeId, subTypeName)) {
+            if(isNrAvailable){ // if is LTE network could be 5g if NR is 
available
+                return TYPE_5G;
             }
+            return TYPE_4G;
+        } else if(is5G(subTypeId, subTypeName)) {
+            return TYPE_5G;
         }
         return TYPE_UNKNOWN;
     }
+
+    private boolean is2G(int type, String name){
+        return  type == TelephonyManager.NETWORK_TYPE_GPRS ||
+                type == TelephonyManager.NETWORK_TYPE_EDGE ||
+                type == TelephonyManager.NETWORK_TYPE_CDMA ||
+                type == TelephonyManager.NETWORK_TYPE_1xRTT ||
+                type == TelephonyManager.NETWORK_TYPE_IDEN ||    // api< 8: 
replace by 11
+                type == TelephonyManager.NETWORK_TYPE_GSM ||     // api<25: 
replace by 16
+                name.equals(GSM) ||
+                name.equals(GPRS) ||
+                name.equals(EDGE) ||
+                name.equals(TWO_G);
+    }
+
+    private boolean is3G(int type, String name){
+        return  type ==  TelephonyManager.NETWORK_TYPE_UMTS ||
+                type ==  TelephonyManager.NETWORK_TYPE_EVDO_0 ||
+                type ==  TelephonyManager.NETWORK_TYPE_EVDO_A ||
+                type ==  TelephonyManager.NETWORK_TYPE_HSDPA ||
+                type ==  TelephonyManager.NETWORK_TYPE_HSUPA ||
+                type ==  TelephonyManager.NETWORK_TYPE_HSPA ||
+                type ==  TelephonyManager.NETWORK_TYPE_EVDO_B ||   // api< 9: 
replace by 12
+                type ==  TelephonyManager.NETWORK_TYPE_EHRPD ||    // api<11: 
replace by 14
+                type ==  TelephonyManager.NETWORK_TYPE_HSPAP ||    // api<13: 
replace by 15
+                type ==  TelephonyManager.NETWORK_TYPE_TD_SCDMA || // api<25: 
replace by 17
+                name.startsWith(CDMA) ||
+                name.equals(UMTS) ||
+                name.equals(ONEXRTT) ||
+                name.equals(EHRPD) ||
+                name.equals(HSUPA) ||
+                name.equals(HSDPA) ||
+                name.equals(HSPA) ||
+                name.equals(THREE_G);
+    }
+
+    private boolean is4G(int type, String name){
+
+        return  type == TelephonyManager.NETWORK_TYPE_LTE && 
name.equals(FOUR_G) ||     // api<11: replace by 13
+                type == TelephonyManager.NETWORK_TYPE_IWLAN ||  // api<25: 
replace by 18
+                type == NETWORK_TYPE_LTE_CA || // LTE_CA
+                name.equals(LTE) ||
+                name.equals(UMB) ||
+                name.equals(HSPA_PLUS) ||
+                name.equals(FOUR_G);
+    }
+
+    private boolean is5G(int type, String name){
+

Review Comment:
   ```suggestion
   ```
   
   Nitpick: No need of new line here.



##########
src/android/NetworkManager.java:
##########
@@ -65,11 +73,16 @@ public class NetworkManager extends CordovaPlugin {
     public static final String HSDPA = "hsdpa";
     public static final String ONEXRTT = "1xrtt";
     public static final String EHRPD = "ehrpd";
-    // 4G network types
+  // 4G network types

Review Comment:
   ```suggestion
       // 4G network types
   ```
   
   Same here.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to