Ver código fonte

feat(core/path): add `PathResolver::home_dir` on Android (#11455)

ref: https://github.com/tauri-apps/tauri/issues/10478#issuecomment-2383754176
Amr Bashir 9 meses atrás
pai
commit
8036c78e08

+ 5 - 0
.changes/android-home-dir.md

@@ -0,0 +1,5 @@
+---
+"tauri": "patch:feat"
+---
+
+Add `PathResolver::home_dir()` method on Android.

+ 5 - 0
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)
+    }
 }

+ 11 - 0
crates/tauri/src/path/android.rs

@@ -117,4 +117,15 @@ impl<R: Runtime> PathResolver<R> {
   pub fn temp_dir(&self) -> Result<PathBuf> {
     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<PathBuf> {
+    self.call_resolve("getHomeDir")
+  }
 }

+ 5 - 9
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<R: Runtime>(
     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<R: Runtime>(
     #[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(),