1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
use sync_async::sync_async;
use crate::*;
use super::*;
/// API of opening file/dir with other apps.
///
/// # Examples
/// ```no_run
/// fn example(app: &tauri::AppHandle) {
/// use tauri_plugin_android_fs::AndroidFsExt as _;
///
/// let api = app.android_fs();
/// let file_sender = api.file_sender();
/// }
/// ```
#[sync_async]
pub struct FileOpener<'a, R: tauri::Runtime> {
#[cfg(target_os = "android")]
pub(crate) handle: &'a tauri::plugin::PluginHandle<R>,
#[cfg(not(target_os = "android"))]
#[allow(unused)]
pub(crate) handle: &'a std::marker::PhantomData<fn() -> R>,
}
#[cfg(target_os = "android")]
#[sync_async(
use(if_sync) impls::SyncImpls as Impls;
use(if_async) impls::AsyncImpls as Impls;
)]
impl<'a, R: tauri::Runtime> FileOpener<'a, R> {
#[always_sync]
fn impls(&self) -> Impls<'_, R> {
Impls { handle: &self.handle }
}
}
#[sync_async(
use(if_async) api_async::{AndroidFs, FilePicker, PrivateStorage, PublicStorage};
use(if_sync) api_sync::{AndroidFs, FilePicker, PrivateStorage, PublicStorage};
)]
impl<'a, R: tauri::Runtime> FileOpener<'a, R> {
/// Show app chooser for sharing files with other apps.
/// This function returns immediately after requesting to open the app chooser,
/// without waiting for the app’s response.
///
/// This sends the files as a single unit.
/// The available apps depend on the MIME types associated with the files.
/// This does not result in an error even if no available apps are found.
/// An empty app chooser is displayed.
///
/// # Args
/// - ***uris*** :
/// Target file URIs to share.
/// This all needs to be **readable**.
/// URIs converted directly from a path, such as via [`FileUri::from_path`], can **not** be used.
///
/// # Support
/// All Android versions supported by Tauri.
///
/// # References
/// - <https://developer.android.com/reference/android/content/Intent#ACTION_SEND_MULTIPLE>
/// - <https://developer.android.com/reference/android/content/Intent#ACTION_SEND>
#[maybe_async]
pub fn share_files<'b>(
&self,
uris: impl IntoIterator<Item = &'b FileUri>,
) -> Result<()> {
#[cfg(not(target_os = "android"))] {
Err(Error::NOT_ANDROID)
}
#[cfg(target_os = "android")] {
self.impls().show_share_file_app_chooser(uris).await
}
}
/// Show app chooser for sharing file with other apps.
/// This function returns immediately after requesting to open the app chooser,
/// without waiting for the app’s response.
///
/// The available apps depend on the MIME type associated with the file.
/// This does not result in an error even if no available apps are found.
/// An empty app chooser is displayed.
///
/// # Args
/// - ***uri*** :
/// Target file URI to share.
/// Must be **readable**.
/// URIs converted directly from a path, such as via [`FileUri::from_path`], can **not** be used.
///
/// # Support
/// All Android versions supported by Tauri.
///
/// # References
/// - <https://developer.android.com/reference/android/content/Intent#ACTION_SEND>
#[maybe_async]
pub fn share_file(
&self,
uri: &FileUri,
) -> Result<()> {
#[cfg(not(target_os = "android"))] {
Err(Error::NOT_ANDROID)
}
#[cfg(target_os = "android")] {
self.impls().show_share_file_app_chooser([uri]).await
}
}
/// Show app chooser for opening file with other apps.
/// This function returns immediately after requesting to open the app chooser,
/// without waiting for the app’s response.
///
/// The available apps depend on the MIME type associated with the file.
/// This does not result in an error even if no available apps are found.
/// An empty app chooser is displayed.
///
/// # Args
/// - ***uri*** :
/// Target file URI to view.
/// Must be **readable**.
/// URIs converted directly from a path, such as via [`FileUri::from_path`], can **not** be used.
///
/// # Support
/// All Android versions supported by Tauri.
///
/// # References
/// - <https://developer.android.com/reference/android/content/Intent#ACTION_VIEW>
#[maybe_async]
pub fn open_file(
&self,
uri: &FileUri,
) -> Result<()> {
#[cfg(not(target_os = "android"))] {
Err(Error::NOT_ANDROID)
}
#[cfg(target_os = "android")] {
self.impls().show_open_file_app_chooser(uri).await
}
}
/// Show app chooser for opening dir with other apps.
/// This function returns immediately after requesting to open the app chooser,
/// without waiting for the app’s response.
///
/// This does not result in an error even if no available apps are found.
/// An empty app chooser is displayed.
///
/// # Args
/// - ***uri*** :
/// Target dir URI to view.
/// Must be **readable**.
/// URIs converted directly from a path, such as via [`FileUri::from_path`], can **not** be used.
///
/// # Support
/// All Android versions supported by Tauri.
///
/// # References
/// - <https://developer.android.com/reference/android/content/Intent#ACTION_VIEW>
#[maybe_async]
pub fn open_dir(
&self,
uri: &FileUri,
) -> Result<()> {
#[cfg(not(target_os = "android"))] {
Err(Error::NOT_ANDROID)
}
#[cfg(target_os = "android")] {
self.impls().show_open_dir_app_chooser(uri).await
}
}
/// Show app chooser for editing file with other apps.
/// This function returns immediately after requesting to open the app chooser,
/// without waiting for the app’s response.
///
/// The available apps depend on the MIME type associated with the file.
/// This does not result in an error even if no available apps are found.
/// An empty app chooser is displayed.
///
/// # Note
/// I think that this may be the least commonly used request for sending file to app.
/// Even if you want to open an image or video editing app,
/// [`FileOpener::open_file`] allows you to choose from a wider range of apps in many cases.
///
/// # Args
/// - ***uri*** :
/// Target file URI to view.
/// Must be **read-writeable**.
/// URIs converted directly from a path, such as via [`FileUri::from_path`], can **not** be used.
///
/// # Support
/// All Android versions supported by Tauri.
///
/// # References
/// - <https://developer.android.com/reference/android/content/Intent#ACTION_EDIT>
#[maybe_async]
pub fn edit_file(
&self,
uri: &FileUri,
) -> Result<()> {
#[cfg(not(target_os = "android"))] {
Err(Error::NOT_ANDROID)
}
#[cfg(target_os = "android")] {
self.impls().show_edit_file_app_chooser(uri).await
}
}
}