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