@@ -4,13 +4,13 @@ use jnix::{
4
4
self ,
5
5
objects:: { GlobalRef , JObject , JValue } ,
6
6
signature:: { JavaType , Primitive } ,
7
- sys:: { jboolean, jlong, JNI_FALSE } ,
7
+ sys:: { jboolean, jlong, JNI_TRUE } ,
8
8
JNIEnv , JavaVM ,
9
9
} ,
10
10
JnixEnv ,
11
11
} ;
12
12
use std:: sync:: { Arc , Weak } ;
13
- use talpid_types:: { android:: AndroidContext , ErrorExt } ;
13
+ use talpid_types:: { android:: AndroidContext , net :: Connectivity , ErrorExt } ;
14
14
15
15
#[ derive( err_derive:: Error , Debug ) ]
16
16
#[ error( no_from) ]
@@ -43,13 +43,13 @@ pub struct MonitorHandle {
43
43
jvm : Arc < JavaVM > ,
44
44
class : GlobalRef ,
45
45
object : GlobalRef ,
46
- _sender : Arc < UnboundedSender < bool > > ,
46
+ _sender : Arc < UnboundedSender < Connectivity > > ,
47
47
}
48
48
49
49
impl MonitorHandle {
50
50
pub fn new (
51
51
android_context : AndroidContext ,
52
- sender : Arc < UnboundedSender < bool > > ,
52
+ sender : Arc < UnboundedSender < Connectivity > > ,
53
53
) -> Result < Self , Error > {
54
54
let env = JnixEnv :: from (
55
55
android_context
@@ -101,30 +101,29 @@ impl MonitorHandle {
101
101
}
102
102
103
103
#[ allow( clippy:: unused_async) ]
104
- pub async fn host_is_offline ( & self ) -> bool {
105
- match self . get_is_connected ( ) {
106
- Ok ( is_connected ) => !is_connected ,
107
- Err ( error) => {
104
+ pub async fn connectivity ( & self ) -> Connectivity {
105
+ self . get_is_connected ( )
106
+ . map ( |connected| Connectivity :: Status { connected } )
107
+ . unwrap_or_else ( | error| {
108
108
log:: error!(
109
109
"{}" ,
110
110
error. display_chain_with_msg( "Failed to check connectivity status" )
111
111
) ;
112
- false
113
- }
114
- }
112
+ Connectivity :: PresumeOnline
113
+ } )
115
114
}
116
115
117
116
fn get_is_connected ( & self ) -> Result < bool , Error > {
118
- let result = self . call_method (
117
+ let is_connected = self . call_method (
119
118
"isConnected" ,
120
119
"()Z" ,
121
120
& [ ] ,
122
121
JavaType :: Primitive ( Primitive :: Boolean ) ,
123
122
) ?;
124
123
125
- match result {
126
- JValue :: Bool ( JNI_FALSE ) => Ok ( false ) ,
127
- JValue :: Bool ( _) => Ok ( true ) ,
124
+ match is_connected {
125
+ JValue :: Bool ( JNI_TRUE ) => Ok ( true ) ,
126
+ JValue :: Bool ( _) => Ok ( false ) ,
128
127
value => Err ( Error :: InvalidMethodResult (
129
128
"ConnectivityListener" ,
130
129
"isConnected" ,
@@ -133,7 +132,7 @@ impl MonitorHandle {
133
132
}
134
133
}
135
134
136
- fn set_sender ( & self , sender : Weak < UnboundedSender < bool > > ) -> Result < ( ) , Error > {
135
+ fn set_sender ( & self , sender : Weak < UnboundedSender < Connectivity > > ) -> Result < ( ) , Error > {
137
136
let sender_ptr = Box :: new ( sender) ;
138
137
let sender_address = Box :: into_raw ( sender_ptr) as jlong ;
139
138
@@ -182,14 +181,16 @@ impl MonitorHandle {
182
181
pub extern "system" fn Java_net_mullvad_talpid_ConnectivityListener_notifyConnectivityChange (
183
182
_: JNIEnv < ' _ > ,
184
183
_: JObject < ' _ > ,
185
- is_connected : jboolean ,
184
+ connected : jboolean ,
186
185
sender_address : jlong ,
187
186
) {
187
+ let connected = JNI_TRUE == connected;
188
188
let sender_ref = Box :: leak ( unsafe { get_sender_from_address ( sender_address) } ) ;
189
- let is_offline = is_connected == JNI_FALSE ;
190
-
191
189
if let Some ( sender) = sender_ref. upgrade ( ) {
192
- if sender. unbounded_send ( is_offline) . is_err ( ) {
190
+ if sender
191
+ . unbounded_send ( Connectivity :: Status { connected } )
192
+ . is_err ( )
193
+ {
193
194
log:: warn!( "Failed to send offline change event" ) ;
194
195
}
195
196
}
@@ -206,13 +207,13 @@ pub extern "system" fn Java_net_mullvad_talpid_ConnectivityListener_destroySende
206
207
let _ = unsafe { get_sender_from_address ( sender_address) } ;
207
208
}
208
209
209
- unsafe fn get_sender_from_address ( address : jlong ) -> Box < Weak < UnboundedSender < bool > > > {
210
- Box :: from_raw ( address as * mut Weak < UnboundedSender < bool > > )
210
+ unsafe fn get_sender_from_address ( address : jlong ) -> Box < Weak < UnboundedSender < Connectivity > > > {
211
+ Box :: from_raw ( address as * mut Weak < UnboundedSender < Connectivity > > )
211
212
}
212
213
213
214
#[ allow( clippy:: unused_async) ]
214
215
pub async fn spawn_monitor (
215
- sender : UnboundedSender < bool > ,
216
+ sender : UnboundedSender < Connectivity > ,
216
217
android_context : AndroidContext ,
217
218
) -> Result < MonitorHandle , Error > {
218
219
let sender = Arc :: new ( sender) ;
0 commit comments