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 consts;
7mod utils;
8
9pub mod api;
10
11pub use models::*;
12pub use consts::*;
13
14#[allow(unused_imports)]
15pub(crate) use utils::*;
16
17/// Initializes the plugin.
18/// 
19/// # Usage
20/// `src-tauri/src/lib.rs`
21/// ```
22/// #[cfg_attr(mobile, tauri::mobile_entry_point)]
23/// pub fn run() {
24///     tauri::Builder::default()
25///         .plugin(tauri_plugin_android_fs::init())
26///         .run(tauri::generate_context!())
27///         .expect("error while running tauri application");
28/// }
29/// ```
30pub fn init<R: tauri::Runtime>() -> tauri::plugin::TauriPlugin<R> {
31    tauri::plugin::Builder::new("android-fs")
32        .setup(|app, api| {
33            use tauri::Manager as _;
34
35            #[cfg(target_os = "android")] {
36                let handle = api.register_android_plugin("com.plugin.android_fs", "AndroidFsPlugin")?;
37                let afs_sync = crate::api::api_sync::AndroidFs { handle: handle.clone() };
38                let afs_async = crate::api::api_async::AndroidFs { handle: handle.clone() };
39
40                // クリーンアップされなかった一時ファイルを全て削除
41                afs_sync.impls().remove_all_tmp_files().ok();
42
43                app.manage(afs_sync);
44                app.manage(afs_async);
45            }
46            #[cfg(not(target_os = "android"))] {
47                let afs_sync = crate::api::api_sync::AndroidFs::<R> { handle: Default::default() };
48                let afs_async = crate::api::api_async::AndroidFs::<R> { handle: Default::default() };
49                app.manage(afs_sync);
50                app.manage(afs_async);
51            }
52
53            Ok(())
54        })
55        .build()
56}
57
58pub trait AndroidFsExt<R: tauri::Runtime> {
59
60    fn android_fs(&self) -> &api::api_sync::AndroidFs<R>;
61
62    fn android_fs_async(&self) -> &api::api_async::AndroidFs<R>;
63}
64
65impl<R: tauri::Runtime, T: tauri::Manager<R>> AndroidFsExt<R> for T {
66
67    fn android_fs(&self) -> &api::api_sync::AndroidFs<R> {
68        self.try_state::<api::api_sync::AndroidFs<R>>()
69            .map(|i| i.inner())
70            .expect("should register this plugin by tauri_plugin_android_fs::init(). see https://crates.io/crates/tauri-plugin-android-fs")
71    }
72
73    fn android_fs_async(&self) -> &api::api_async::AndroidFs<R> {
74        self.try_state::<api::api_async::AndroidFs<R>>()
75            .map(|i| i.inner())
76            .expect("should register this plugin by tauri_plugin_android_fs::init(). see https://crates.io/crates/tauri-plugin-android-fs")
77    }
78}