tauri_plugin_android_fs/models/dir.rs
1use serde::{Deserialize, Serialize};
2
3
4/// The application specific directory.
5#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, Deserialize, Serialize)]
6#[non_exhaustive]
7pub enum PrivateDir {
8
9 /// The application specific persistent-data directory.
10 ///
11 /// Files stored in this directory are included in [Android Auto Backup](https://developer.android.com/identity/data/autobackup).
12 ///
13 /// The system prevents other apps and user from accessing these locations.
14 /// In cases where the device is rooted or the user has special permissions, the user may be able to access this.
15 ///
16 /// These files will be deleted when the app is uninstalled and may also be deleted at the user’s request.
17 ///
18 /// e.g. `/data/user/0/{app-package-name}/files`
19 ///
20 /// <https://developer.android.com/reference/android/content/Context#getFilesDir()>
21 Data,
22
23 /// The application specific cache directory.
24 ///
25 /// Files stored in this directory are **not** included in [Android Auto Backup](https://developer.android.com/identity/data/autobackup).
26 ///
27 /// The system prevents other apps and user from accessing these locations.
28 /// In cases where the device is rooted or the user has special permissions, the user may be able to access this.
29 ///
30 /// These files will be deleted when the app is uninstalled and may also be deleted at the user’s request.
31 ///
32 /// In addition, the system will automatically delete files in this directory as disk space is needed elsewhere on the device.
33 /// But you should not rely on this. The cache should be explicitly cleared by yourself.
34 ///
35 /// e.g. `/data/user/0/{app-package-name}/cache`
36 ///
37 /// <https://developer.android.com/reference/android/content/Context#getCacheDir()>
38 Cache,
39
40 /// The application specific persistent-data directory.
41 ///
42 /// This is similar to [`PrivateDir::Data`].
43 /// But files stored in this directory are **not** included in [Android Auto Backup](https://developer.android.com/identity/data/autobackup).
44 ///
45 /// The system prevents other apps and user from accessing these locations.
46 /// In cases where the device is rooted or the user has special permissions, the user may be able to access this.
47 ///
48 /// These files will be deleted when the app is uninstalled and may also be deleted at the user’s request.
49 ///
50 /// e.g. `/data/user/0/{app-package-name}/no_backup`
51 ///
52 /// <https://developer.android.com/reference/android/content/Context#getNoBackupFilesDir()>
53 NoBackupData,
54}
55
56/// The directory for the app.
57#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, Deserialize, Serialize)]
58#[non_exhaustive]
59pub enum AppDir {
60
61 /// The directory for persistent-data files.
62 ///
63 /// These files will be deleted when the app is uninstalled and may also be deleted at the user’s request.
64 ///
65 /// Please note that, unlike [`PrivateDir::Data`], it may be accessible by other apps or user.
66 ///
67 /// e.g.
68 /// - `/storage/emulated/0/Android/data/{app-package-name}/files`
69 /// - `/storage/{sd-card-id}/Android/data/{app-package-name}/files`
70 ///
71 /// <https://developer.android.com/reference/android/content/Context#getExternalFilesDirs(java.lang.String)>
72 Data,
73
74 /// The directory for cache files.
75 ///
76 /// These files will be deleted when the app is uninstalled and may also be deleted at the user’s request.
77 ///
78 /// Please note that, unlike [`PrivateDir::Cache`], it may be accessible by other apps or user.
79 ///
80 /// e.g.
81 /// - `/storage/emulated/0/Android/data/{app-package-name}/cache`
82 /// - `/storage/{sd-card-id}/Android/data/{app-package-name}/cache`
83 ///
84 /// <https://developer.android.com/reference/android/content/Context#getExternalCacheDirs()>
85 Cache,
86
87 /// The directory for shared media files to other apps or user.
88 ///
89 /// These files will be deleted when the app is uninstalled and may also be deleted at the user’s request.
90 ///
91 /// For Android 11 (API level 30) or higher,
92 /// this has been marked as deprecated.
93 /// It still works, but you should consider migrating to [`PublicDir`] of [`PublicStorage`](crate::api::api_async::PublicStorage).
94 ///
95 /// e.g.
96 /// - `/storage/emulated/0/Android/media/{app-package-name}`
97 /// - `/storage/{sd-card-id}/Android/media/{app-package-name}`
98 ///
99 /// <https://developer.android.com/reference/android/content/Context#getExternalMediaDirs()>
100 #[deprecated(note = "For Android 11 (API level 30) or higher, this is deprecated. Use `PublicDir` of `PublicStorage` instead.")]
101 PublicMedia
102}
103
104#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, Deserialize, Serialize)]
105#[non_exhaustive]
106pub enum PublicDir {
107
108 #[serde(untagged)]
109 Image(PublicImageDir),
110
111 #[serde(untagged)]
112 Video(PublicVideoDir),
113
114 #[serde(untagged)]
115 Audio(PublicAudioDir),
116
117 #[serde(untagged)]
118 GeneralPurpose(PublicGeneralPurposeDir),
119}
120
121/// Directory in which to place images that are available to other applications and users.
122#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, Deserialize, Serialize)]
123#[non_exhaustive]
124pub enum PublicImageDir {
125
126 Pictures,
127
128 DCIM,
129}
130
131/// Directory in which to place videos that are available to other applications and users.
132#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, Deserialize, Serialize)]
133#[non_exhaustive]
134pub enum PublicVideoDir {
135
136 Movies,
137
138 DCIM,
139}
140
141/// Directory in which to place audios that are available to other applications and users.
142#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, Deserialize, Serialize)]
143#[non_exhaustive]
144pub enum PublicAudioDir {
145
146 Music,
147
148 Alarms,
149
150 /// This is not available on Android 9 (API level 28) and lower.
151 Audiobooks,
152
153 Notifications,
154
155 Podcasts,
156
157 Ringtones,
158
159 /// This is not available on Android 11 (API level 30) and lower.
160 Recordings,
161}
162
163/// Directory in which to place files that are available to other applications and users.
164#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, Deserialize, Serialize)]
165#[non_exhaustive]
166pub enum PublicGeneralPurposeDir {
167
168 Documents,
169
170 /// This is not the plural "Downloads", but the singular "Download".
171 /// <https://developer.android.com/reference/android/os/Environment#DIRECTORY_DOWNLOADS>
172 Download,
173}
174
175impl std::fmt::Display for PublicImageDir {
176 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
177 match self {
178 PublicImageDir::Pictures => write!(f, "Pictures"),
179 PublicImageDir::DCIM => write!(f, "DCIM"),
180 }
181 }
182}
183
184impl std::fmt::Display for PublicVideoDir {
185 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
186 match self {
187 PublicVideoDir::Movies => write!(f, "Movies"),
188 PublicVideoDir::DCIM => write!(f, "DCIM"),
189 }
190 }
191}
192
193impl std::fmt::Display for PublicAudioDir {
194 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
195 match self {
196 PublicAudioDir::Music => write!(f, "Music"),
197 PublicAudioDir::Alarms => write!(f, "Alarms"),
198 PublicAudioDir::Audiobooks => write!(f, "Audiobooks"),
199 PublicAudioDir::Notifications => write!(f, "Notifications"),
200 PublicAudioDir::Podcasts => write!(f, "Podcasts"),
201 PublicAudioDir::Ringtones => write!(f, "Ringtones"),
202 PublicAudioDir::Recordings => write!(f, "Recordings"),
203 }
204 }
205}
206
207impl std::fmt::Display for PublicGeneralPurposeDir {
208 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
209 match self {
210 PublicGeneralPurposeDir::Documents => write!(f, "Documents"),
211 PublicGeneralPurposeDir::Download => write!(f, "Download"),
212 }
213 }
214}
215
216impl std::fmt::Display for PublicDir {
217 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
218 match self {
219 PublicDir::Image(p) => p.fmt(f),
220 PublicDir::Video(p) => p.fmt(f),
221 PublicDir::Audio(p) => p.fmt(f),
222 PublicDir::GeneralPurpose(p) => p.fmt(f),
223 }
224 }
225}
226
227macro_rules! impl_into_pubdir {
228 ($target: ident, $wrapper: ident) => {
229 impl From<$target> for PublicDir {
230 fn from(value: $target) -> Self {
231 Self::$wrapper(value)
232 }
233 }
234 };
235}
236impl_into_pubdir!(PublicImageDir, Image);
237impl_into_pubdir!(PublicVideoDir, Video);
238impl_into_pubdir!(PublicAudioDir, Audio);
239impl_into_pubdir!(PublicGeneralPurposeDir, GeneralPurpose);