tauri_plugin_android_fs/api/private_storage.rs
1use sync_async::sync_async;
2use crate::*;
3use super::*;
4
5
6/// API of file storage intended for the app’s use only.
7///
8/// # Examples
9/// ```no_run
10/// use tauri_plugin_android_fs::{AndroidFsExt, PrivateDir};
11///
12/// async fn example(app: &tauri::AppHandle) {
13/// let api = app.android_fs_async();
14/// let ps = api.private_storage();
15///
16/// // Resolve the absolute paths.
17/// // Files and directories in these locations can be fully managed using `std::fs`.
18/// let cache_dir_path: std::path::PathBuf = ps.resolve_path(PrivateDir::Cache).await?;
19/// let data_dir_path: std::path::PathBuf = ps.resolve_path(PrivateDir::Data).await?;
20/// let nobackup_data_dir_path: std::path::PathBuf = ps.resolve_path(PrivateDir::NoBackupData).await?;
21///
22/// // These directories may also contain files created by other Tauri plugins
23/// // or the WebView runtime. To avoid conflicts, it is recommended to use
24/// // a uniquely named subdirectory for your application.
25/// let cache_dir_path = cache_dir_path.join("01K6049FVCD4SAGMAB6X20SA5S");
26/// let data_dir_path = data_dir_path.join("01K6049FVCD4SAGMAB6X20SA5S");
27/// let nobackup_data_dir_path = nobackup_data_dir_path.join("01K6049FVCD4SAGMAB6X20SA5S");
28/// }
29/// ```
30#[sync_async]
31pub struct PrivateStorage<'a, R: tauri::Runtime> {
32 #[cfg(target_os = "android")]
33 pub(crate) handle: &'a tauri::plugin::PluginHandle<R>,
34
35 #[cfg(not(target_os = "android"))]
36 #[allow(unused)]
37 pub(crate) handle: &'a std::marker::PhantomData<fn() -> R>,
38}
39
40#[cfg(target_os = "android")]
41#[sync_async(
42 use(if_sync) impls::SyncImpls as Impls;
43 use(if_async) impls::AsyncImpls as Impls;
44)]
45impl<'a, R: tauri::Runtime> PrivateStorage<'a, R> {
46
47 #[always_sync]
48 fn impls(&self) -> Impls<'_, R> {
49 Impls { handle: &self.handle }
50 }
51}
52
53#[sync_async(
54 use(if_async) api_async::{AndroidFs, Opener, Picker, PublicStorage};
55 use(if_sync) api_sync::{AndroidFs, Opener, Picker, PublicStorage};
56)]
57impl<'a, R: tauri::Runtime> PrivateStorage<'a, R> {
58
59 /// Returns the absolute path of an app-specific directory on the internal storage.
60 ///
61 /// Files and directories in this location can be managed directly using [`std::fs`].
62 ///
63 /// This function does not guarantee directory creation.
64 ///
65 /// Since these locations may also contain files created
66 /// by other Tauri plugins or by the WebView runtime,
67 /// it is recommended to create a uniquely named subdirectory for your application.
68 ///
69 /// # Notes
70 /// Files in these locations are removed when the app is uninstalled.
71 ///
72 /// When using [`PrivateDir::Cache`], the system may automatically delete files
73 /// when additional storage space is needed.
74 /// Applications should not rely on this behavior and should clear cache files explicitly.
75 ///
76 /// These directories are inaccessible to other apps under normal circumstances.
77 /// On rooted devices or when the user has elevated privileges,
78 /// their contents may still be accessible.
79 ///
80 /// The returned path may change if the app is moved to adopted storage.
81 /// Persist only relative paths if the path needs to be stored.
82 ///
83 /// Each Android user has a separate app-specific directory.
84 ///
85 /// # Support
86 /// All Android versions supported by Tauri.
87 #[maybe_async]
88 pub fn resolve_path(
89 &self,
90 dir: PrivateDir
91 ) -> Result<std::path::PathBuf> {
92
93 #[cfg(not(target_os = "android"))] {
94 Err(Error::NOT_ANDROID)
95 }
96 #[cfg(target_os = "android")] {
97 self.impls().private_dir_path(dir).map(Clone::clone)
98 }
99 }
100
101 /// See [`PrivateStorage::resolve_path`] and [`FileUri::from_path`]
102 #[maybe_async]
103 pub fn resolve_uri(
104 &self,
105 dir: PrivateDir,
106 relative_path: impl AsRef<std::path::Path>
107 ) -> Result<FsUri> {
108
109 #[cfg(not(target_os = "android"))] {
110 Err(Error::NOT_ANDROID)
111 }
112 #[cfg(target_os = "android")] {
113 let mut path = self.resolve_path(dir).await?;
114 path.push(relative_path.as_ref());
115 Ok(path.into())
116 }
117 }
118}