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
37                // 一時ファイルを全て削除
38                afs_sync.impls().remove_all_temp_files().ok();
39
40                app.manage(afs_sync);
41                app.manage(afs_async);
42            }
43            #[cfg(not(target_os = "android"))] {
44                let afs_sync = crate::api::api_sync::AndroidFs::<R> { handle: Default::default() };
45                let afs_async = crate::api::api_async::AndroidFs::<R> { handle: Default::default() };
46                app.manage(afs_sync);
47                app.manage(afs_async);
48            }
49
50            Ok(())
51        })
52        .js_init_script(format!(
53            "window.__TAURI_ANDROID_FS_PLUGIN_INTERNALS__ = {{ isAndroid: {} }};",
54            cfg!(target_os = "android")
55        ))
56        .invoke_handler(tauri::generate_handler![
57            cmds::get_name,
58            cmds::get_byte_length,
59            cmds::get_mime_type,
60            cmds::get_type,
61            cmds::get_metadata,
62            cmds::get_fs_path,
63            cmds::get_thumbnail,
64            cmds::get_thumbnail_base64,
65            cmds::get_thumbnail_data_url,
66            cmds::list_volumes,
67            cmds::create_new_public_file,
68            cmds::create_new_public_image_file,
69            cmds::create_new_public_video_file,
70            cmds::create_new_public_audio_file,
71            cmds::scan_public_file,
72            cmds::request_public_files_permission,
73            cmds::has_public_files_permission,
74            cmds::create_new_file,
75            cmds::create_dir_all,
76            cmds::copy_file,
77            cmds::truncate_file,
78            cmds::read_dir,
79            cmds::remove_file,
80            cmds::remove_empty_dir,
81            cmds::remove_dir_all,
82            cmds::persist_uri_permission,
83            cmds::check_persisted_uri_permission,
84            cmds::release_persisted_uri_permission,
85            cmds::release_all_persisted_uri_permissions,
86            cmds::show_open_file_picker,
87            cmds::show_open_dir_picker,
88            cmds::show_save_file_picker,
89            cmds::show_share_file_dialog,
90            cmds::show_view_file_dialog,
91            cmds::show_view_dir_dialog
92        ])
93        .build()
94}
95
96pub trait AndroidFsExt<R: tauri::Runtime> {
97
98    fn android_fs(&self) -> &api::api_sync::AndroidFs<R>;
99
100    fn android_fs_async(&self) -> &api::api_async::AndroidFs<R>;
101}
102
103impl<R: tauri::Runtime, T: tauri::Manager<R>> AndroidFsExt<R> for T {
104
105    fn android_fs(&self) -> &api::api_sync::AndroidFs<R> {
106        self.try_state::<api::api_sync::AndroidFs<R>>()
107            .map(|i| i.inner())
108            .expect("should register this plugin by tauri_plugin_android_fs::init(). see https://crates.io/crates/tauri-plugin-android-fs")
109    }
110
111    fn android_fs_async(&self) -> &api::api_async::AndroidFs<R> {
112        self.try_state::<api::api_async::AndroidFs<R>>()
113            .map(|i| i.inner())
114            .expect("should register this plugin by tauri_plugin_android_fs::init(). see https://crates.io/crates/tauri-plugin-android-fs")
115    }
116}