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::get_volumes,
67            cmds::list_volumes,
68            cmds::create_new_public_file,
69            cmds::create_new_public_image_file,
70            cmds::create_new_public_video_file,
71            cmds::create_new_public_audio_file,
72            cmds::scan_public_file,
73            cmds::request_public_files_permission,
74            cmds::has_public_files_permission,
75            cmds::create_new_file,
76            cmds::create_dir_all,
77            cmds::copy_file,
78            cmds::truncate_file,
79            cmds::read_dir,
80            cmds::remove_file,
81            cmds::remove_empty_dir,
82            cmds::remove_dir_all,
83            cmds::persist_uri_permission,
84            cmds::check_persisted_uri_permission,
85            cmds::release_persisted_uri_permission,
86            cmds::release_all_persisted_uri_permissions,
87            cmds::show_open_file_picker,
88            cmds::show_open_dir_picker,
89            cmds::show_save_file_picker,
90            cmds::show_share_file_dialog,
91            cmds::show_view_file_dialog,
92            cmds::show_view_dir_dialog
93        ])
94        .build()
95}
96
97pub trait AndroidFsExt<R: tauri::Runtime> {
98
99    fn android_fs(&self) -> &api::api_sync::AndroidFs<R>;
100
101    fn android_fs_async(&self) -> &api::api_async::AndroidFs<R>;
102}
103
104impl<R: tauri::Runtime, T: tauri::Manager<R>> AndroidFsExt<R> for T {
105
106    fn android_fs(&self) -> &api::api_sync::AndroidFs<R> {
107        self.try_state::<api::api_sync::AndroidFs<R>>()
108            .map(|i| i.inner())
109            .expect("should register this plugin by tauri_plugin_android_fs::init(). see https://crates.io/crates/tauri-plugin-android-fs")
110    }
111
112    fn android_fs_async(&self) -> &api::api_async::AndroidFs<R> {
113        self.try_state::<api::api_async::AndroidFs<R>>()
114            .map(|i| i.inner())
115            .expect("should register this plugin by tauri_plugin_android_fs::init(). see https://crates.io/crates/tauri-plugin-android-fs")
116    }
117}