66//! For linux based platforms, it uses the XDG base directory spec but provides
77//! similar abstractions for non-linux platforms.
88
9+ #[ cfg( target_os = "android" ) ]
10+ use android_injected_glue;
911#[ cfg( any( target_os = "macos" , target_os = "windows" ) ) ]
1012use std:: env;
13+ #[ cfg( target_os = "android" ) ]
14+ use std:: ffi:: CStr ;
1115use std:: path:: PathBuf ;
1216#[ cfg( all( unix, not( target_os = "macos" ) , not( target_os = "ios" ) , not( target_os = "android" ) ) ) ]
1317use xdg;
@@ -20,8 +24,12 @@ pub fn default_config_dir() -> Option<PathBuf> {
2024}
2125
2226#[ cfg( target_os = "android" ) ]
27+ #[ allow( unsafe_code) ]
2328pub fn default_config_dir ( ) -> Option < PathBuf > {
24- Some ( PathBuf :: from ( "/sdcard/servo" ) )
29+ let dir = unsafe {
30+ CStr :: from_ptr ( ( * android_injected_glue:: get_app ( ) . activity ) . externalDataPath )
31+ } ;
32+ Some ( PathBuf :: from ( dir. to_str ( ) . unwrap ( ) ) )
2533}
2634
2735#[ cfg( all( unix, not( target_os = "macos" ) , not( target_os = "ios" ) , not( target_os = "android" ) ) ) ]
@@ -32,8 +40,12 @@ pub fn default_data_dir() -> Option<PathBuf> {
3240}
3341
3442#[ cfg( target_os = "android" ) ]
43+ #[ allow( unsafe_code) ]
3544pub fn default_data_dir ( ) -> Option < PathBuf > {
36- Some ( PathBuf :: from ( "/sdcard/servo" ) )
45+ let dir = unsafe {
46+ CStr :: from_ptr ( ( * android_injected_glue:: get_app ( ) . activity ) . internalDataPath )
47+ } ;
48+ Some ( PathBuf :: from ( dir. to_str ( ) . unwrap ( ) ) )
3749}
3850
3951#[ cfg( all( unix, not( target_os = "macos" ) , not( target_os = "ios" ) , not( target_os = "android" ) ) ) ]
@@ -44,8 +56,14 @@ pub fn default_cache_dir() -> Option<PathBuf> {
4456}
4557
4658#[ cfg( target_os = "android" ) ]
59+ #[ allow( unsafe_code) ]
4760pub fn default_cache_dir ( ) -> Option < PathBuf > {
48- Some ( PathBuf :: from ( "/sdcard/servo" ) )
61+ // TODO: Use JNI to call context.getCacheDir().
62+ // There is no equivalent function in NDK/NativeActivity.
63+ let dir = unsafe {
64+ CStr :: from_ptr ( ( * android_injected_glue:: get_app ( ) . activity ) . externalDataPath )
65+ } ;
66+ Some ( PathBuf :: from ( dir. to_str ( ) . unwrap ( ) ) )
4967}
5068
5169#[ cfg( target_os = "macos" ) ]
0 commit comments