1
- import { getNativeApplication , android as androidApp } from "../application" ;
2
-
1
+ import { android as androidApp , getNativeApplication } from "../application" ;
3
2
export enum connectionType {
4
3
none = 0 ,
5
4
wifi = 1 ,
6
5
mobile = 2 ,
7
6
ethernet = 3 ,
8
- bluetooth = 4
7
+ bluetooth = 4 ,
8
+ vpn = 5
9
9
}
10
10
11
11
const wifi = "wifi" ;
12
12
const mobile = "mobile" ;
13
13
const ethernet = "ethernet" ;
14
14
const bluetooth = "bluetooth" ;
15
+ const vpn = "vpn" ;
15
16
16
17
// Get Connection Type
17
18
function getConnectivityManager ( ) : android . net . ConnectivityManager {
@@ -27,33 +28,74 @@ function getActiveNetworkInfo(): android.net.NetworkInfo {
27
28
return connectivityManager . getActiveNetworkInfo ( ) ;
28
29
}
29
30
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 ) {
33
36
return connectionType . none ;
34
37
}
35
38
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 ) ) {
38
42
return connectionType . wifi ;
39
43
}
40
44
41
- if ( type . indexOf ( mobile ) !== - 1 ) {
45
+ if ( capabilities . hasTransport ( NetworkCapabilities . TRANSPORT_CELLULAR ) ) {
42
46
return connectionType . mobile ;
43
47
}
44
48
45
- if ( type . indexOf ( ethernet ) !== - 1 ) {
49
+ if ( capabilities . hasTransport ( NetworkCapabilities . TRANSPORT_ETHERNET ) ) {
46
50
return connectionType . ethernet ;
47
51
}
48
52
49
- if ( type . indexOf ( bluetooth ) !== - 1 ) {
53
+ if ( capabilities . hasTransport ( NetworkCapabilities . TRANSPORT_BLUETOOTH ) ) {
50
54
return connectionType . bluetooth ;
51
55
}
52
56
57
+ if ( capabilities . hasTransport ( NetworkCapabilities . TRANSPORT_VPN ) ) {
58
+ return connectionType . vpn ;
59
+ }
60
+
53
61
return connectionType . none ;
54
62
}
55
63
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 ) {
57
99
let onReceiveCallback = function onReceiveCallback ( context : android . content . Context , intent : android . content . Intent ) {
58
100
let newConnectionType = getConnectionType ( ) ;
59
101
connectionTypeChangedCallback ( newConnectionType ) ;
@@ -62,6 +104,53 @@ export function startMonitoring(connectionTypeChangedCallback: (newConnectionTyp
62
104
androidApp . registerBroadcastReceiver ( android . net . ConnectivityManager . CONNECTIVITY_ACTION , zoneCallback ) ;
63
105
}
64
106
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
+
65
145
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
+ }
67
156
}
0 commit comments