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                let app_handle = app.app_handle().clone();
40                std::thread::spawn(move || {
41                    let afs = app_handle.android_fs();
42
43                    // 前回作成した一時ファイルを全て削除
44                    afs.impls().remove_all_temp_files().ok();
45                });
46            }
47            #[cfg(not(target_os = "android"))] {
48                let afs_sync = crate::api::api_sync::AndroidFs::<R> { handle: Default::default() };
49                let afs_async = crate::api::api_async::AndroidFs::<R> { handle: Default::default() };
50                app.manage(afs_sync);
51                app.manage(afs_async);
52            }
53
54            Ok(())
55        })
56        .js_init_script(format!(
57            "window.__TAURI_ANDROID_FS_PLUGIN_INTERNALS__ = {{ isAndroid: {} }};",
58            cfg!(target_os = "android")
59        ))
60        .invoke_handler(tauri::generate_handler![
61            cmds::get_android_api_level,
62            cmds::get_name,
63            cmds::get_byte_length,
64            cmds::get_mime_type,
65            cmds::get_type,
66            cmds::get_metadata,
67            cmds::get_fs_path,
68            cmds::get_thumbnail,
69            cmds::get_thumbnail_base64,
70            cmds::get_thumbnail_data_url,
71            cmds::list_volumes,
72            cmds::create_new_public_file,
73            cmds::create_new_public_image_file,
74            cmds::create_new_public_video_file,
75            cmds::create_new_public_audio_file,
76            cmds::scan_public_file,
77            cmds::set_public_file_pending,
78            cmds::request_public_files_permission,
79            cmds::has_public_files_permission,
80            cmds::create_new_file,
81            cmds::create_dir_all,
82            cmds::open_read_file_stream,
83            cmds::open_read_text_file_lines_stream,
84            cmds::open_write_file_stream,
85            cmds::read_file,
86            cmds::read_text_file,
87            cmds::write_file,
88            cmds::write_text_file,
89            cmds::copy_file,
90            cmds::truncate_file,
91            cmds::read_dir,
92            cmds::rename_file,
93            cmds::rename_dir,
94            cmds::remove_file,
95            cmds::remove_empty_dir,
96            cmds::remove_dir_all,
97            cmds::check_picker_uri_permission,
98            cmds::persist_picker_uri_permission,
99            cmds::check_persisted_picker_uri_permission,
100            cmds::release_persisted_picker_uri_permission,
101            cmds::release_all_persisted_picker_uri_permissions,
102            cmds::show_open_file_picker,
103            cmds::show_open_dir_picker,
104            cmds::show_save_file_picker,
105            cmds::show_share_file_dialog,
106            cmds::show_view_file_dialog,
107            cmds::show_view_dir_dialog,
108        ])
109        .build()
110}
111
112pub trait AndroidFsExt<R: tauri::Runtime> {
113
114    fn android_fs(&self) -> &api::api_sync::AndroidFs<R>;
115
116    fn android_fs_async(&self) -> &api::api_async::AndroidFs<R>;
117}
118
119impl<R: tauri::Runtime, T: tauri::Manager<R>> AndroidFsExt<R> for T {
120
121    fn android_fs(&self) -> &api::api_sync::AndroidFs<R> {
122        self.try_state::<api::api_sync::AndroidFs<R>>()
123            .map(|i| i.inner())
124            .expect("should register this plugin by tauri_plugin_android_fs::init(). see https://crates.io/crates/tauri-plugin-android-fs")
125    }
126
127    fn android_fs_async(&self) -> &api::api_async::AndroidFs<R> {
128        self.try_state::<api::api_async::AndroidFs<R>>()
129            .map(|i| i.inner())
130            .expect("should register this plugin by tauri_plugin_android_fs::init(). see https://crates.io/crates/tauri-plugin-android-fs")
131    }
132}