tauri_plugin_android_fs/api/
file_opener.rs

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