Skip to content

Commit f909960

Browse files
triniwiztarunama
authored andcommitted
fix(connectivity): getActiveNetworkInfo and NetworkInfo modern compliance NativeScript#8580 (NativeScript#8652)
1 parent d69a3c4 commit f909960

File tree

3 files changed

+175
-15
lines changed

3 files changed

+175
-15
lines changed
Lines changed: 102 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
1-
import { getNativeApplication, android as androidApp } from "../application";
2-
1+
import { android as androidApp, getNativeApplication } from "../application";
32
export enum connectionType {
43
none = 0,
54
wifi = 1,
65
mobile = 2,
76
ethernet = 3,
8-
bluetooth = 4
7+
bluetooth = 4,
8+
vpn = 5
99
}
1010

1111
const wifi = "wifi";
1212
const mobile = "mobile";
1313
const ethernet = "ethernet";
1414
const bluetooth = "bluetooth";
15+
const vpn = "vpn";
1516

1617
// Get Connection Type
1718
function getConnectivityManager(): android.net.ConnectivityManager {
@@ -27,33 +28,74 @@ function getActiveNetworkInfo(): android.net.NetworkInfo {
2728
return connectivityManager.getActiveNetworkInfo();
2829
}
2930

30-
export function getConnectionType(): number {
31-
let activeNetworkInfo = getActiveNetworkInfo();
32-
if (!activeNetworkInfo || !activeNetworkInfo.isConnected()) {
31+
function getNetworkCapabilities() {
32+
const connectivityManager = getConnectivityManager() as any;
33+
const network = connectivityManager.getActiveNetwork();
34+
const capabilities = connectivityManager.getNetworkCapabilities(network);
35+
if (capabilities == null) {
3336
return connectionType.none;
3437
}
3538

36-
let type = activeNetworkInfo.getTypeName().toLowerCase();
37-
if (type.indexOf(wifi) !== -1) {
39+
const NetworkCapabilities = (android as any).net.NetworkCapabilities;
40+
41+
if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) {
3842
return connectionType.wifi;
3943
}
4044

41-
if (type.indexOf(mobile) !== -1) {
45+
if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) {
4246
return connectionType.mobile;
4347
}
4448

45-
if (type.indexOf(ethernet) !== -1) {
49+
if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET)) {
4650
return connectionType.ethernet;
4751
}
4852

49-
if (type.indexOf(bluetooth) !== -1) {
53+
if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_BLUETOOTH)) {
5054
return connectionType.bluetooth;
5155
}
5256

57+
if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_VPN)) {
58+
return connectionType.vpn;
59+
}
60+
5361
return connectionType.none;
5462
}
5563

56-
export function startMonitoring(connectionTypeChangedCallback: (newConnectionType: number) => void): void {
64+
export function getConnectionType(): number {
65+
if (android.os.Build.VERSION.SDK_INT >= 28) {
66+
return getNetworkCapabilities();
67+
} else {
68+
let activeNetworkInfo = getActiveNetworkInfo();
69+
if (!activeNetworkInfo || !activeNetworkInfo.isConnected()) {
70+
return connectionType.none;
71+
}
72+
73+
let type = activeNetworkInfo.getTypeName().toLowerCase();
74+
if (type.indexOf(wifi) !== -1) {
75+
return connectionType.wifi;
76+
}
77+
78+
if (type.indexOf(mobile) !== -1) {
79+
return connectionType.mobile;
80+
}
81+
82+
if (type.indexOf(ethernet) !== -1) {
83+
return connectionType.ethernet;
84+
}
85+
86+
if (type.indexOf(bluetooth) !== -1) {
87+
return connectionType.bluetooth;
88+
}
89+
90+
if (type.indexOf(vpn) !== -1) {
91+
return connectionType.vpn;
92+
}
93+
}
94+
95+
return connectionType.none;
96+
}
97+
98+
function startMonitoringLegacy(connectionTypeChangedCallback) {
5799
let onReceiveCallback = function onReceiveCallback(context: android.content.Context, intent: android.content.Intent) {
58100
let newConnectionType = getConnectionType();
59101
connectionTypeChangedCallback(newConnectionType);
@@ -62,6 +104,53 @@ export function startMonitoring(connectionTypeChangedCallback: (newConnectionTyp
62104
androidApp.registerBroadcastReceiver(android.net.ConnectivityManager.CONNECTIVITY_ACTION, zoneCallback);
63105
}
64106

107+
let callback;
108+
let networkCallback;
109+
let notifyCallback;
110+
export function startMonitoring(connectionTypeChangedCallback: (newConnectionType: number) => void): void {
111+
if (android.os.Build.VERSION.SDK_INT >= 28) {
112+
const manager = getConnectivityManager() as any;
113+
if (manager) {
114+
notifyCallback = () => {
115+
let newConnectionType = getConnectionType();
116+
let zoneCallback = <any>zonedCallback(connectionTypeChangedCallback);
117+
zoneCallback(newConnectionType);
118+
};
119+
const ConnectivityManager = (android as any).net.ConnectivityManager;
120+
if (!networkCallback) {
121+
networkCallback = ConnectivityManager.NetworkCallback.extend({
122+
onAvailable(network) {
123+
notifyCallback();
124+
},
125+
onCapabilitiesChanged(network, networkCapabilities) {
126+
notifyCallback();
127+
},
128+
onLost(network) {
129+
notifyCallback();
130+
},
131+
onUnavailable() {
132+
notifyCallback();
133+
}
134+
});
135+
}
136+
callback = new networkCallback();
137+
manager.registerDefaultNetworkCallback(callback);
138+
}
139+
140+
} else {
141+
startMonitoringLegacy(connectionTypeChangedCallback);
142+
}
143+
}
144+
65145
export function stopMonitoring(): void {
66-
androidApp.unregisterBroadcastReceiver(android.net.ConnectivityManager.CONNECTIVITY_ACTION);
146+
if (android.os.Build.VERSION.SDK_INT >= 28) {
147+
const manager = getConnectivityManager() as any;
148+
if (manager && callback) {
149+
manager.unregisterNetworkCallback(callback);
150+
notifyCallback = null;
151+
callback = null;
152+
}
153+
} else {
154+
androidApp.unregisterBroadcastReceiver(android.net.ConnectivityManager.CONNECTIVITY_ACTION);
155+
}
67156
}

nativescript-core/connectivity/connectivity.d.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,14 @@ export enum connectionType {
3535
ethernet = 3,
3636

3737
/**
38-
* Denotes an bluetooth connection
38+
* Denotes a bluetooth connection
3939
*/
40-
bluetooth = 4
40+
bluetooth = 4,
41+
42+
/**
43+
* Denotes a vpn connection
44+
*/
45+
vpn = 5
4146
}
4247

4348
/**

nativescript-core/connectivity/connectivity.ios.ts

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ export enum connectionType {
22
none = 0,
33
wifi = 1,
44
mobile = 2,
5+
ethernet = 3,
6+
bluetooth = 4,
7+
vpn = 5
58
}
69

710
// Get Connection Type
@@ -52,9 +55,71 @@ function _getConnectionTypeFromFlags(flags: number): number {
5255
return connectionType.mobile;
5356
}
5457

58+
const cfDict = CFNetworkCopySystemProxySettings();
59+
const nsDict = cfDict.takeUnretainedValue();
60+
const keys = nsDict.objectForKey("__SCOPED__");
61+
62+
if (isVPNConnected(keys)) {
63+
return connectionType.vpn;
64+
}
65+
66+
/*
67+
TODO try improving with CBCentralManager since toggling bluetooth
68+
with multiple connections fails to detect switch, require key added
69+
to Info.plist.
70+
*/
71+
if (isBluetoothConnected(keys)) {
72+
return connectionType.bluetooth;
73+
}
74+
5575
return connectionType.wifi;
5676
}
5777

78+
function isBluetoothConnected (keys) {
79+
if (!keys) {
80+
return false;
81+
}
82+
const allKeys = keys.allKeys;
83+
const size = allKeys.count;
84+
let isBlueTooth = false;
85+
for (let i = 0; i < size; i++) {
86+
const key = allKeys.objectAtIndex(i);
87+
if (
88+
key === "en4"
89+
) {
90+
isBlueTooth = true;
91+
break;
92+
}
93+
}
94+
95+
return isBlueTooth;
96+
}
97+
98+
function isVPNConnected(keys) {
99+
if (!keys) {
100+
return false;
101+
}
102+
const allKeys = keys.allKeys;
103+
const size = allKeys.count;
104+
let isVPN = false;
105+
for (let i = 0; i < size; i++) {
106+
const key = allKeys.objectAtIndex(i);
107+
if (
108+
key === "tap" ||
109+
key === "tun" ||
110+
key === "ppp" ||
111+
key === "ipsec" ||
112+
key === "ipsec0" ||
113+
key === "utun1"
114+
) {
115+
isVPN = true;
116+
break;
117+
}
118+
}
119+
120+
return isVPN;
121+
}
122+
58123
export function getConnectionType(): number {
59124
return _getConnectionType();
60125
}
@@ -65,6 +130,7 @@ function _reachabilityCallback(target: any, flags: number, info: any) {
65130
const newConnectionType = _getConnectionTypeFromFlags(flags);
66131
_connectionTypeChangedCallback(newConnectionType);
67132
}
133+
68134
}
69135

70136
const _reachabilityCallbackFunctionRef = new interop.FunctionReference(_reachabilityCallback);

0 commit comments

Comments
 (0)