From 8036c78e08715b1bc6b9fcb0c59a570eec98014f Mon Sep 17 00:00:00 2001 From: Amr Bashir Date: Thu, 24 Oct 2024 06:43:02 +0300 Subject: [PATCH] feat(core/path): add `PathResolver::home_dir` on Android (#11455) ref: https://github.com/tauri-apps/tauri/issues/10478#issuecomment-2383754176 --- .changes/android-home-dir.md | 5 +++++ .../android/src/main/java/app/tauri/PathPlugin.kt | 5 +++++ crates/tauri/src/path/android.rs | 11 +++++++++++ crates/tauri/src/path/mod.rs | 14 +++++--------- 4 files changed, 26 insertions(+), 9 deletions(-) create mode 100644 .changes/android-home-dir.md diff --git a/.changes/android-home-dir.md b/.changes/android-home-dir.md new file mode 100644 index 000000000000..b2b5a5885cd0 --- /dev/null +++ b/.changes/android-home-dir.md @@ -0,0 +1,5 @@ +--- +"tauri": "patch:feat" +--- + +Add `PathResolver::home_dir()` method on Android. diff --git a/crates/tauri/mobile/android/src/main/java/app/tauri/PathPlugin.kt b/crates/tauri/mobile/android/src/main/java/app/tauri/PathPlugin.kt index ec72d008ad0e..04ea644f71aa 100644 --- a/crates/tauri/mobile/android/src/main/java/app/tauri/PathPlugin.kt +++ b/crates/tauri/mobile/android/src/main/java/app/tauri/PathPlugin.kt @@ -76,4 +76,9 @@ class PathPlugin(private val activity: Activity): Plugin(activity) { fun getCacheDir(invoke: Invoke) { resolvePath(invoke, activity.cacheDir.absolutePath) } + + @Command + fun getHomeDir(invoke: Invoke) { + resolvePath(invoke, Environment.getExternalStorageDirectory().absolutePath) + } } diff --git a/crates/tauri/src/path/android.rs b/crates/tauri/src/path/android.rs index c2f6be2f88fb..3015e44808e7 100644 --- a/crates/tauri/src/path/android.rs +++ b/crates/tauri/src/path/android.rs @@ -117,4 +117,15 @@ impl PathResolver { pub fn temp_dir(&self) -> Result { Ok(std::env::temp_dir()) } + + /// Returns the path to the user's home directory. + /// + /// ## Platform-specific + /// + /// - **Linux:** Resolves to `$HOME`. + /// - **macOS:** Resolves to `$HOME`. + /// - **Windows:** Resolves to `{FOLDERID_Profile}`. + pub fn home_dir(&self) -> Result { + self.call_resolve("getHomeDir") + } } diff --git a/crates/tauri/src/path/mod.rs b/crates/tauri/src/path/mod.rs index 7d786f844e0e..28854fb547ff 100644 --- a/crates/tauri/src/path/mod.rs +++ b/crates/tauri/src/path/mod.rs @@ -122,6 +122,8 @@ pub enum BaseDirectory { /// Resolves to [`BaseDirectory::Home`]`/Library/Logs/{bundle_identifier}` on macOS /// and [`BaseDirectory::Config`]`/{bundle_identifier}/logs` on linux and Windows. AppLog, + /// The Home directory. + Home, /// The Desktop directory. #[cfg(not(target_os = "android"))] @@ -132,9 +134,6 @@ pub enum BaseDirectory { /// The Font directory. #[cfg(not(target_os = "android"))] Font, - /// The Home directory. - #[cfg(not(target_os = "android"))] - Home, /// The Runtime directory. #[cfg(not(target_os = "android"))] Runtime, @@ -164,6 +163,7 @@ impl BaseDirectory { Self::AppLocalData => "$APPLOCALDATA", Self::AppCache => "$APPCACHE", Self::AppLog => "$APPLOG", + Self::Home => "$HOME", #[cfg(not(target_os = "android"))] Self::Desktop => "$DESKTOP", @@ -172,8 +172,6 @@ impl BaseDirectory { #[cfg(not(target_os = "android"))] Self::Font => "$FONT", #[cfg(not(target_os = "android"))] - Self::Home => "$HOME", - #[cfg(not(target_os = "android"))] Self::Runtime => "$RUNTIME", #[cfg(not(target_os = "android"))] Self::Template => "$TEMPLATE", @@ -201,6 +199,7 @@ impl BaseDirectory { "$APPLOCALDATA" => Self::AppLocalData, "$APPCACHE" => Self::AppCache, "$APPLOG" => Self::AppLog, + "$HOME" => Self::Home, #[cfg(not(target_os = "android"))] "$DESKTOP" => Self::Desktop, @@ -209,8 +208,6 @@ impl BaseDirectory { #[cfg(not(target_os = "android"))] "$FONT" => Self::Font, #[cfg(not(target_os = "android"))] - "$HOME" => Self::Home, - #[cfg(not(target_os = "android"))] "$RUNTIME" => Self::Runtime, #[cfg(not(target_os = "android"))] "$TEMPLATE" => Self::Template, @@ -302,6 +299,7 @@ fn resolve_path( BaseDirectory::AppLocalData => resolver.app_local_data_dir(), BaseDirectory::AppCache => resolver.app_cache_dir(), BaseDirectory::AppLog => resolver.app_log_dir(), + BaseDirectory::Home => resolver.home_dir(), #[cfg(not(target_os = "android"))] BaseDirectory::Desktop => resolver.desktop_dir(), #[cfg(not(target_os = "android"))] @@ -309,8 +307,6 @@ fn resolve_path( #[cfg(not(target_os = "android"))] BaseDirectory::Font => resolver.font_dir(), #[cfg(not(target_os = "android"))] - BaseDirectory::Home => resolver.home_dir(), - #[cfg(not(target_os = "android"))] BaseDirectory::Runtime => resolver.runtime_dir(), #[cfg(not(target_os = "android"))] BaseDirectory::Template => resolver.template_dir(),