@@ -36,6 +36,13 @@ pub mod android {
36
36
use self :: jni:: sys:: { jdouble, jint, jstring} ;
37
37
use self :: jni:: JNIEnv ;
38
38
use super :: * ;
39
+
40
+ use std:: path:: PathBuf ;
41
+ use std:: sync:: Mutex ;
42
+
43
+ use crate :: config:: AWConfig ;
44
+ use crate :: endpoints;
45
+ use crate :: endpoints:: ServerState ;
39
46
use aw_datastore:: Datastore ;
40
47
use aw_models:: { Bucket , Event } ;
41
48
@@ -93,33 +100,49 @@ pub mod android {
93
100
string_to_jstring ( & env, obj. to_string ( ) )
94
101
}
95
102
103
+ lazy_static ! {
104
+ static ref ASSET_PATH : Mutex <String > = Mutex :: new( String :: new( ) ) ;
105
+ }
106
+
96
107
#[ no_mangle]
97
108
pub unsafe extern "C" fn Java_net_activitywatch_android_RustInterface_startServer (
98
109
env : JNIEnv ,
99
110
_: JClass ,
100
111
java_asset_path : JString ,
101
112
) {
102
- use crate :: config:: AWConfig ;
103
- use std:: path:: PathBuf ;
113
+ info ! ( "Starting server..." ) ;
114
+
115
+ * ASSET_PATH . lock ( ) . unwrap ( ) = jstring_to_string ( & env, java_asset_path) ;
116
+
117
+ start_server ( ) ;
104
118
105
- use crate :: endpoints;
119
+ info ! ( "Server exited" ) ;
120
+ }
106
121
122
+ #[ rocket:: main]
123
+ async fn start_server ( ) {
107
124
info ! ( "Building server state..." ) ;
108
125
109
- let asset_path = jstring_to_string ( & env, java_asset_path) ;
110
- info ! ( "Using asset dir: {}" , asset_path) ;
126
+ // FIXME: Why is unsafe needed here? Can we get rid of it?
127
+ unsafe {
128
+ let server_state: ServerState = endpoints:: ServerState {
129
+ datastore : Mutex :: new ( openDatastore ( ) ) ,
130
+ asset_path : PathBuf :: from ( ASSET_PATH . lock ( ) . unwrap ( ) . to_owned ( ) ) ,
131
+ device_id : device_id:: get_device_id ( ) ,
132
+ } ;
133
+ info ! (
134
+ "Using server_state:: asset dir: {}; device_id: {}" ,
135
+ server_state. asset_path. display( ) ,
136
+ server_state. device_id
137
+ ) ;
111
138
112
- let server_state = endpoints:: ServerState {
113
- datastore : Mutex :: new ( openDatastore ( ) ) ,
114
- asset_path : PathBuf :: from ( asset_path) ,
115
- device_id : device_id:: get_device_id ( ) ,
116
- } ;
139
+ let mut server_config: AWConfig = AWConfig :: default ( ) ;
140
+ server_config. port = 5600 ;
117
141
118
- let mut config = AWConfig :: default ( ) ;
119
- config. port = 5600 ;
120
- info ! ( "Starting server..." ) ;
121
- endpoints:: build_rocket ( server_state, config) . launch ( ) ;
122
- info ! ( "Server exited" ) ;
142
+ endpoints:: build_rocket ( server_state, server_config)
143
+ . launch ( )
144
+ . await ;
145
+ }
123
146
}
124
147
125
148
static mut INITIALIZED : bool = false ;
@@ -157,8 +180,9 @@ pub mod android {
157
180
_: JClass ,
158
181
java_dir : JString ,
159
182
) {
160
- debug ! ( "Setting android data dir" ) ;
161
- dirs:: set_android_data_dir ( & jstring_to_string ( & env, java_dir) ) ;
183
+ let path = & jstring_to_string ( & env, java_dir) ;
184
+ debug ! ( "Setting android data dir as {}" , path) ;
185
+ dirs:: set_android_data_dir ( path) ;
162
186
}
163
187
164
188
#[ no_mangle]
0 commit comments