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