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#[deprecated = "Use AppDir instead"]
57pub type OutsidePrivateDir = AppDir;
58
59/// The directory for the app.
60#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, Deserialize, Serialize)]
61#[non_exhaustive]
62pub enum AppDir {
63
64 /// The directory for persistent-data files.
65 ///
66 /// These files will be deleted when the app is uninstalled and may also be deleted at the user’s request.
67 ///
68 /// Please note that, unlike [`PrivateDir::Data`], it may be accessible by other apps or user.
69 ///
70 /// e.g.
71 /// - `/storage/emulated/0/Android/data/{app-package-name}/files`
72 /// - `/storage/{sd-card-id}/Android/data/{app-package-name}/files`
73 ///
74 /// <https://developer.android.com/reference/android/content/Context#getExternalFilesDirs(java.lang.String)>
75 Data,
76
77 /// The directory for cache files.
78 ///
79 /// These files will be deleted when the app is uninstalled and may also be deleted at the user’s request.
80 ///
81 /// Please note that, unlike [`PrivateDir::Cache`], it may be accessible by other apps or user.
82 ///
83 /// e.g.
84 /// - `/storage/emulated/0/Android/data/{app-package-name}/cache`
85 /// - `/storage/{sd-card-id}/Android/data/{app-package-name}/cache`
86 ///
87 /// <https://developer.android.com/reference/android/content/Context#getExternalCacheDirs()>
88 Cache,
89
90 /// The directory for shared media files to other apps or user.
91 ///
92 /// These files will be deleted when the app is uninstalled and may also be deleted at the user’s request.
93 ///
94 /// For Android 11 (API level 30) or higher,
95 /// this has been marked as deprecated.
96 /// It still works, but you should consider migrating to [`PublicDir`] of [`PublicStorage`](crate::api::api_async::PublicStorage).
97 ///
98 /// e.g.
99 /// - `/storage/emulated/0/Android/media/{app-package-name}`
100 /// - `/storage/{sd-card-id}/Android/media/{app-package-name}`
101 ///
102 /// <https://developer.android.com/reference/android/content/Context#getExternalMediaDirs()>
103 #[deprecated(note = "For Android 11 (API level 30) or higher, this is deprecated. Use `PublicDir` of `PublicStorage` instead.")]
104 PublicMedia
105}
106
107#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, Deserialize, Serialize)]
108#[non_exhaustive]
109pub enum PublicDir {
110
111 #[serde(untagged)]
112 Image(PublicImageDir),
113
114 #[serde(untagged)]
115 Video(PublicVideoDir),
116
117 #[serde(untagged)]
118 Audio(PublicAudioDir),
119
120 #[serde(untagged)]
121 GeneralPurpose(PublicGeneralPurposeDir),
122}
123
124/// Directory in which to place images that are available to other applications and users.
125#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, Deserialize, Serialize)]
126#[non_exhaustive]
127pub enum PublicImageDir {
128
129 Pictures,
130
131 DCIM,
132}
133
134/// Directory in which to place videos that are available to other applications and users.
135#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, Deserialize, Serialize)]
136#[non_exhaustive]
137pub enum PublicVideoDir {
138
139 Movies,
140
141 DCIM,
142}
143
144/// Directory in which to place audios that are available to other applications and users.
145#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, Deserialize, Serialize)]
146#[non_exhaustive]
147pub enum PublicAudioDir {
148
149 Music,
150
151 Alarms,
152
153 /// This is not available on Android 9 (API level 28) and lower.
154 Audiobooks,
155
156 Notifications,
157
158 Podcasts,
159
160 Ringtones,
161
162 /// This is not available on Android 11 (API level 30) and lower.
163 Recordings,
164}
165
166/// Directory in which to place files that are available to other applications and users.
167#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, Deserialize, Serialize)]
168#[non_exhaustive]
169pub enum PublicGeneralPurposeDir {
170
171 Documents,
172
173 /// This is not the plural "Downloads", but the singular "Download".
174 /// <https://developer.android.com/reference/android/os/Environment#DIRECTORY_DOWNLOADS>
175 Download,
176}
177
178impl std::fmt::Display for PublicImageDir {
179 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
180 match self {
181 PublicImageDir::Pictures => write!(f, "Pictures"),
182 PublicImageDir::DCIM => write!(f, "DCIM"),
183 }
184 }
185}
186
187impl std::fmt::Display for PublicVideoDir {
188 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
189 match self {
190 PublicVideoDir::Movies => write!(f, "Movies"),
191 PublicVideoDir::DCIM => write!(f, "DCIM"),
192 }
193 }
194}
195
196impl std::fmt::Display for PublicAudioDir {
197 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
198 match self {
199 PublicAudioDir::Music => write!(f, "Music"),
200 PublicAudioDir::Alarms => write!(f, "Alarms"),
201 PublicAudioDir::Audiobooks => write!(f, "Audiobooks"),
202 PublicAudioDir::Notifications => write!(f, "Notifications"),
203 PublicAudioDir::Podcasts => write!(f, "Podcasts"),
204 PublicAudioDir::Ringtones => write!(f, "Ringtones"),
205 PublicAudioDir::Recordings => write!(f, "Recordings"),
206 }
207 }
208}
209
210impl std::fmt::Display for PublicGeneralPurposeDir {
211 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
212 match self {
213 PublicGeneralPurposeDir::Documents => write!(f, "Documents"),
214 PublicGeneralPurposeDir::Download => write!(f, "Download"),
215 }
216 }
217}
218
219impl std::fmt::Display for PublicDir {
220 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
221 match self {
222 PublicDir::Image(p) => p.fmt(f),
223 PublicDir::Video(p) => p.fmt(f),
224 PublicDir::Audio(p) => p.fmt(f),
225 PublicDir::GeneralPurpose(p) => p.fmt(f),
226 }
227 }
228}
229
230macro_rules! impl_into_pubdir {
231 ($target: ident, $wrapper: ident) => {
232 impl From<$target> for PublicDir {
233 fn from(value: $target) -> Self {
234 Self::$wrapper(value)
235 }
236 }
237 };
238}
239impl_into_pubdir!(PublicImageDir, Image);
240impl_into_pubdir!(PublicVideoDir, Video);
241impl_into_pubdir!(PublicAudioDir, Audio);
242impl_into_pubdir!(PublicGeneralPurposeDir, GeneralPurpose);