1- import { getNativeApplication , android as androidApp } from "../application" ;
2-
1+ import { android as androidApp , getNativeApplication } from "../application" ;
32export 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
1111const wifi = "wifi" ;
1212const mobile = "mobile" ;
1313const ethernet = "ethernet" ;
1414const bluetooth = "bluetooth" ;
15+ const vpn = "vpn" ;
1516
1617// Get Connection Type
1718function 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+
65145export 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}
0 commit comments