Skip to main content

tauri_plugin_android_fs/
lib.rs

1//! Overview and usage is [here](https://crates.io/crates/tauri-plugin-android-fs)
2
3#![cfg_attr(not(target_os = "android"), allow(unused_variables))]
4
5mod cmds;
6mod protocols;
7mod config;
8mod scope;
9mod utils;
10
11pub mod api;
12
13use utils::*;
14
15pub use api::models::*;
16pub use api::consts::*;
17
18/// Initializes the plugin.
19/// 
20/// # Usage
21/// `src-tauri/src/lib.rs`
22/// ```
23/// #[cfg_attr(mobile, tauri::mobile_entry_point)]
24/// pub fn run() {
25///     tauri::Builder::default()
26///         .plugin(tauri_plugin_android_fs::init())
27///         .run(tauri::generate_context!())
28///         .expect("error while running tauri application");
29/// }
30/// ```
31pub fn init<R: tauri::Runtime>() -> tauri::plugin::TauriPlugin<R, Option<config::Config>> {
32    let builder = tauri::plugin::Builder::<R, Option<config::Config>>::new("android-fs")
33        .setup(|app, api| {
34            use tauri::Manager as _;
35
36            #[cfg(target_os = "android")] {
37                let handle = api.register_android_plugin("com.plugin.android_fs", "AndroidFsPlugin")?;
38                let afs_sync = crate::api::api_sync::AndroidFs { handle: handle.clone() };
39                let afs_async = crate::api::api_async::AndroidFs { handle: handle.clone() };
40                app.manage(afs_sync);
41                app.manage(afs_async);
42
43                #[cfg(feature = "commands")] {
44                    app.manage(cmds::new_file_stream_resources_state(app.app_handle().clone()));
45                    app.manage(cmds::new_file_writer_resources_state(app.app_handle().clone()));
46                }
47
48                #[cfg(any(feature = "protocol-content", feature = "protocol-thumbnail"))] {
49                    app.manage(protocols::new_config_state(api.config().as_ref(), app));
50                }
51            }
52            #[cfg(not(target_os = "android"))] {
53                let afs_sync = crate::api::api_sync::AndroidFs::<R> { handle: Default::default() };
54                let afs_async = crate::api::api_async::AndroidFs::<R> { handle: Default::default() };
55                app.manage(afs_sync);
56                app.manage(afs_async);
57            }
58
59            Ok(())
60        });
61
62    #[cfg(feature = "commands")]
63    let builder = builder
64        .js_init_script(format!(
65            "window.__TAURI_ANDROID_FS_PLUGIN_INTERNALS__ = {{ isAndroid: {} }};",
66            cfg!(target_os = "android")
67        ))
68        .invoke_handler(tauri::generate_handler![
69            cmds::get_android_api_level,
70            cmds::get_name,
71            cmds::get_byte_length,
72            cmds::get_mime_type,
73            cmds::get_type,
74            cmds::get_metadata,
75            cmds::get_fs_path,
76            cmds::get_thumbnail,
77            cmds::get_thumbnail_as_bytes,
78            cmds::get_thumbnail_as_base64,
79            cmds::get_thumbnail_as_data_url,
80            cmds::list_volumes,
81            cmds::create_new_public_file,
82            cmds::create_new_public_image_file,
83            cmds::create_new_public_video_file,
84            cmds::create_new_public_audio_file,
85            cmds::scan_public_file,
86            cmds::set_public_file_pending,
87            cmds::request_public_files_permission,
88            cmds::check_public_files_permission,
89            cmds::create_new_file,
90            cmds::create_dir,
91            cmds::count_all_file_streams,
92            cmds::close_all_file_streams,
93            cmds::open_read_file_stream,
94            cmds::open_read_text_file_lines_stream,
95            cmds::open_write_file_stream,
96            cmds::read_file,
97            cmds::read_file_as_base64,
98            cmds::read_file_as_data_url,
99            cmds::read_text_file,
100            cmds::write_file,
101            cmds::write_text_file,
102            cmds::copy_file,
103            cmds::truncate_file,
104            cmds::read_dir,
105            cmds::rename_file,
106            cmds::rename_dir,
107            cmds::remove_file,
108            cmds::remove_empty_dir,
109            cmds::remove_dir_all,
110            cmds::check_picker_uri_permission,
111            cmds::persist_picker_uri_permission,
112            cmds::check_persisted_picker_uri_permission,
113            cmds::release_persisted_picker_uri_permission,
114            cmds::release_all_persisted_picker_uri_permissions,
115            cmds::show_open_file_picker,
116            cmds::show_open_dir_picker,
117            cmds::show_save_file_picker,
118            cmds::show_share_file_dialog,
119            cmds::show_view_file_dialog,
120            cmds::show_view_dir_dialog,
121        ]);
122
123    #[cfg(all(target_os = "android", feature = "protocol-thumbnail"))]
124    let builder = builder
125        .register_asynchronous_uri_scheme_protocol(
126            protocols::protocol_thumbnail::URI_SCHEME, 
127            protocols::protocol_thumbnail::protocol,
128        );
129
130    #[cfg(all(target_os = "android", feature = "protocol-content"))]
131    let builder = builder
132        .register_asynchronous_uri_scheme_protocol(
133            protocols::protocol_content::URI_SCHEME, 
134            protocols::protocol_content::protocol,
135        );
136    
137    builder.build()
138}
139
140pub trait AndroidFsExt<R: tauri::Runtime> {
141
142    fn android_fs(&self) -> &api::api_sync::AndroidFs<R>;
143
144    fn android_fs_async(&self) -> &api::api_async::AndroidFs<R>;
145}
146
147impl<R: tauri::Runtime, T: tauri::Manager<R>> AndroidFsExt<R> for T {
148
149    fn android_fs(&self) -> &api::api_sync::AndroidFs<R> {
150        self.try_state::<api::api_sync::AndroidFs<R>>()
151            .map(|i| i.inner())
152            .expect("tauri_plugin_android_fs should be initialized to use; see https://crates.io/crates/tauri-plugin-android-fs")
153    }
154
155    fn android_fs_async(&self) -> &api::api_async::AndroidFs<R> {
156        self.try_state::<api::api_async::AndroidFs<R>>()
157            .map(|i| i.inner())
158            .expect("tauri_plugin_android_fs should be initialized to use; see https://crates.io/crates/tauri-plugin-android-fs")
159    }
160}