Skip to content

Commit 0a86a7b

Browse files
authored
feat(android): add 5g check and test (#172)
Generated-By: GPT-5.3-Codex, GitHub Copilot Chat
1 parent d88517d commit 0a86a7b

File tree

2 files changed

+31
-18
lines changed

2 files changed

+31
-18
lines changed

src/android/NetworkManager.java

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ Licensed to the Apache Software Foundation (ASF) under one
3636
import android.net.ConnectivityManager;
3737
import android.net.NetworkInfo;
3838
import android.os.Build;
39+
import android.telephony.TelephonyManager;
3940

4041
import java.util.Locale;
4142

@@ -71,6 +72,9 @@ public class NetworkManager extends CordovaPlugin {
7172
public static final String LTE = "lte";
7273
public static final String UMB = "umb";
7374
public static final String HSPA_PLUS = "hspa+";
75+
// 5G network types
76+
public static final String FIVE_G = "5g";
77+
public static final String NR = "nr";
7478
// return type
7579
public static final String TYPE_UNKNOWN = "unknown";
7680
public static final String TYPE_ETHERNET = "ethernet";
@@ -79,6 +83,7 @@ public class NetworkManager extends CordovaPlugin {
7983
public static final String TYPE_2G = "2g";
8084
public static final String TYPE_3G = "3g";
8185
public static final String TYPE_4G = "4g";
86+
public static final String TYPE_5G = "5g";
8287
public static final String TYPE_NONE = "none";
8388

8489
private static final String LOG_TAG = "NetworkManager";
@@ -271,26 +276,32 @@ private String getType(NetworkInfo info) {
271276
} else if (type.toLowerCase().equals(TYPE_ETHERNET) || type.toLowerCase().startsWith(TYPE_ETHERNET_SHORT)) {
272277
return TYPE_ETHERNET;
273278
} else if (type.equals(MOBILE) || type.equals(CELLULAR)) {
274-
type = info.getSubtypeName().toLowerCase(Locale.US);
275-
if (type.equals(GSM) ||
276-
type.equals(GPRS) ||
277-
type.equals(EDGE) ||
278-
type.equals(TWO_G)) {
279+
int subtype = info.getSubtype();
280+
String subtypeName = info.getSubtypeName().toLowerCase(Locale.US);
281+
if (subtypeName.equals(GSM) ||
282+
subtypeName.equals(GPRS) ||
283+
subtypeName.equals(EDGE) ||
284+
subtypeName.equals(TWO_G)) {
279285
return TYPE_2G;
280-
} else if (type.startsWith(CDMA) ||
281-
type.equals(UMTS) ||
282-
type.equals(ONEXRTT) ||
283-
type.equals(EHRPD) ||
284-
type.equals(HSUPA) ||
285-
type.equals(HSDPA) ||
286-
type.equals(HSPA) ||
287-
type.equals(THREE_G)) {
286+
} else if (subtypeName.startsWith(CDMA) ||
287+
subtypeName.equals(UMTS) ||
288+
subtypeName.equals(ONEXRTT) ||
289+
subtypeName.equals(EHRPD) ||
290+
subtypeName.equals(HSUPA) ||
291+
subtypeName.equals(HSDPA) ||
292+
subtypeName.equals(HSPA) ||
293+
subtypeName.equals(THREE_G)) {
288294
return TYPE_3G;
289-
} else if (type.equals(LTE) ||
290-
type.equals(UMB) ||
291-
type.equals(HSPA_PLUS) ||
292-
type.equals(FOUR_G)) {
295+
} else if (subtypeName.equals(LTE) ||
296+
subtypeName.equals(UMB) ||
297+
subtypeName.equals(HSPA_PLUS) ||
298+
subtypeName.equals(FOUR_G)) {
293299
return TYPE_4G;
300+
// Android 10+ exposes 5G as NR (New Radio).
301+
} else if ((Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q && subtype == TelephonyManager.NETWORK_TYPE_NR) ||
302+
subtypeName.equals(NR) ||
303+
subtypeName.equals(FIVE_G)) {
304+
return TYPE_5G;
294305
}
295306
}
296307
return TYPE_UNKNOWN;

tests/tests.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ exports.defineAutoTests = function () {
3232
cellular: 1,
3333
'3g': 1,
3434
'4g': 1,
35+
'5g': 1,
3536
none: 1
3637
};
3738
expect(validValues[navigator.connection.type]).toBe(1);
@@ -44,6 +45,7 @@ exports.defineAutoTests = function () {
4445
expect(Connection.CELL_2G).toBe('2g');
4546
expect(Connection.CELL_3G).toBe('3g');
4647
expect(Connection.CELL_4G).toBe('4g');
48+
expect(Connection.CELL_5G).toBe('5g');
4749
expect(Connection.NONE).toBe('none');
4850
expect(Connection.CELL).toBe('cellular');
4951
});
@@ -76,7 +78,7 @@ exports.defineManualTests = function (contentEl, createActionButton) {
7678
'<span id="results"></span>' +
7779
'</div><div id="connection"></div>' +
7880
'Expected result: Status box will update with type of connection using two different methods. Both values must match.' +
79-
' The result will be unknown, ethernet, wifi, 2g, 3g, 4g, none, or cellular. Make sure it matches what the device is connected to.' +
81+
' The result will be unknown, ethernet, wifi, 2g, 3g, 4g, 5g, none, or cellular. Make sure it matches what the device is connected to.' +
8082
'</p> <div id="actions"></div>';
8183

8284
document.addEventListener('online', onEvent, false);

0 commit comments

Comments
 (0)