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