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#![allow(unused_variables)]
4
5mod models;
6mod cmds;
7mod consts;
8mod utils;
9
10pub mod api;
11
12use utils::*;
13
14pub use models::*;
15pub use consts::*;
16
17/// Initializes the plugin.
18/// 
19/// # Usage
20/// `src-tauri/src/lib.rs`
21/// ```
22/// #[cfg_attr(mobile, tauri::mobile_entry_point)]
23/// pub fn run() {
24///     tauri::Builder::default()
25///         .plugin(tauri_plugin_android_fs::init())
26///         .run(tauri::generate_context!())
27///         .expect("error while running tauri application");
28/// }
29/// ```
30pub fn init<R: tauri::Runtime>() -> tauri::plugin::TauriPlugin<R> {
31    tauri::plugin::Builder::new("android-fs")
32        .setup(|app, api| {
33            use tauri::Manager as _;
34
35            #[cfg(target_os = "android")] {
36                let handle = api.register_android_plugin("com.plugin.android_fs", "AndroidFsPlugin")?;
37                let afs_sync = crate::api::api_sync::AndroidFs { handle: handle.clone() };
38                let afs_async = crate::api::api_async::AndroidFs { handle: handle.clone() };
39                app.manage(afs_sync);
40                app.manage(afs_async);
41
42                app.manage(cmds::new_file_stream_resources_state(app.app_handle().clone()));
43                app.manage(cmds::new_file_writer_resources_state(app.app_handle().clone()));
44
45                // 前回作成した一時ファイルを全て削除
46                let app_handle = app.app_handle().clone();
47                std::thread::spawn(move || {
48                    let afs = app_handle.android_fs();
49                    afs.impls().remove_all_temp_files().ok();
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        .js_init_script(format!(
62            "window.__TAURI_ANDROID_FS_PLUGIN_INTERNALS__ = {{ isAndroid: {} }};",
63            cfg!(target_os = "android")
64        ))
65        .invoke_handler(tauri::generate_handler![
66            cmds::get_android_api_level,
67            cmds::get_name,
68            cmds::get_byte_length,
69            cmds::get_mime_type,
70            cmds::get_type,
71            cmds::get_metadata,
72            cmds::get_fs_path,
73            cmds::get_thumbnail,
74            cmds::get_thumbnail_as_bytes,
75            cmds::get_thumbnail_as_base64,
76            cmds::get_thumbnail_as_data_url,
77            cmds::list_volumes,
78            cmds::create_new_public_file,
79            cmds::create_new_public_image_file,
80            cmds::create_new_public_video_file,
81            cmds::create_new_public_audio_file,
82            cmds::scan_public_file,
83            cmds::set_public_file_pending,
84            cmds::request_public_files_permission,
85            cmds::has_public_files_permission,
86            cmds::create_new_file,
87            cmds::create_dir,
88            cmds::count_all_file_streams,
89            cmds::close_all_file_streams,
90            cmds::open_read_file_stream,
91            cmds::open_read_text_file_lines_stream,
92            cmds::open_write_file_stream,
93            cmds::read_file,
94            cmds::read_file_as_base64,
95            cmds::read_file_as_data_url,
96            cmds::read_text_file,
97            cmds::write_file,
98            cmds::write_text_file,
99            cmds::copy_file,
100            cmds::truncate_file,
101            cmds::read_dir,
102            cmds::rename_file,
103            cmds::rename_dir,
104            cmds::remove_file,
105            cmds::remove_empty_dir,
106            cmds::remove_dir_all,
107            cmds::check_picker_uri_permission,
108            cmds::persist_picker_uri_permission,
109            cmds::check_persisted_picker_uri_permission,
110            cmds::release_persisted_picker_uri_permission,
111            cmds::release_all_persisted_picker_uri_permissions,
112            cmds::show_open_file_picker,
113            cmds::show_open_dir_picker,
114            cmds::show_save_file_picker,
115            cmds::show_share_file_dialog,
116            cmds::show_view_file_dialog,
117            cmds::show_view_dir_dialog,
118        ])
119        .build()
120}
121
122pub trait AndroidFsExt<R: tauri::Runtime> {
123
124    fn android_fs(&self) -> &api::api_sync::AndroidFs<R>;
125
126    fn android_fs_async(&self) -> &api::api_async::AndroidFs<R>;
127}
128
129impl<R: tauri::Runtime, T: tauri::Manager<R>> AndroidFsExt<R> for T {
130
131    fn android_fs(&self) -> &api::api_sync::AndroidFs<R> {
132        self.try_state::<api::api_sync::AndroidFs<R>>()
133            .map(|i| i.inner())
134            .expect("should register this plugin by tauri_plugin_android_fs::init(). see https://crates.io/crates/tauri-plugin-android-fs")
135    }
136
137    fn android_fs_async(&self) -> &api::api_async::AndroidFs<R> {
138        self.try_state::<api::api_async::AndroidFs<R>>()
139            .map(|i| i.inner())
140            .expect("should register this plugin by tauri_plugin_android_fs::init(). see https://crates.io/crates/tauri-plugin-android-fs")
141    }
142}