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