tauri_plugin_android_fs/
lib.rs

1//! Overview and usage is [here](https://crates.io/crates/tauri-plugin-android-fs)
2
3#![allow(unused)]
4
5mod models;
6mod error;
7mod api;
8mod utils;
9
10pub use models::*;
11pub use error::{Error, Result};
12pub use api::{AndroidFs, PrivateStorage, PublicStorage};
13pub(crate) use utils::*;
14
15/// Initializes the plugin.
16pub fn init<R: tauri::Runtime>() -> tauri::plugin::TauriPlugin<R> {
17    let builder = tauri::plugin::Builder::new("android-fs")
18        .setup(|app, api| {
19            use tauri::Manager as _;
20
21            let afs = AndroidFs::new(app.clone(), api)?;
22
23            // Cleanup temporary directory
24            let _ = afs
25                .private_storage()
26                .remove_dir_all(PrivateDir::Cache, Some(TMP_DIR_RELATIVE_PATH));
27
28            app.manage(afs);
29            Ok(())
30        });
31
32    // https://github.com/aiueo13/tauri-plugin-android-fs/issues/1
33    #[cfg(all(feature = "avoid-issue1", target_os = "android"))]
34    let builder = {
35        const SCRIPT: &str = "
36            ;(async function () {
37                const noop = async () => await window.__TAURI_INTERNALS__.invoke('plugin:android-fs|noop');
38
39                setInterval(noop, 800)
40            })();
41        ";
42
43        #[tauri::command]
44        fn noop() {}
45
46        builder
47            .invoke_handler(tauri::generate_handler![noop])
48            .js_init_script(SCRIPT.into())  
49    };
50
51    builder.build()
52}
53
54pub trait AndroidFsExt<R: tauri::Runtime> {
55
56    fn android_fs(&self) -> &AndroidFs<R>;
57}
58
59impl<R: tauri::Runtime, T: tauri::Manager<R>> AndroidFsExt<R> for T {
60
61    fn android_fs(&self) -> &AndroidFs<R> {
62        self.state::<AndroidFs<R>>().inner()
63    }
64}