tauri_plugin_android_fs/impls/
mod.rs

1#[cfg(target_os = "android")]
2mod android;
3#[cfg(target_os = "android")]
4use android::AndroidFsImpl;
5
6#[cfg(not(target_os = "android"))]
7mod other;
8#[cfg(not(target_os = "android"))]
9use other::AndroidFsImpl;
10
11use crate::AndroidFs;
12use tauri::{plugin::{Builder, TauriPlugin}, Manager, Runtime};
13
14
15/// Initializes the plugin.
16pub fn init<R: Runtime>() -> TauriPlugin<R> {
17    let builder = Builder::new("android-fs")
18        .setup(|app, api| {
19            app.manage(AndroidFsImpl::new(app, api)?);
20            Ok(())
21        });
22
23    // https://github.com/aiueo13/tauri-plugin-android-fs/issues/1
24    #[cfg(all(feature = "avoid-issue1", target_os = "android"))]
25    let builder = {
26        const SCRIPT: &str = "
27            ;(async function () {
28                const noop = async () => await window.__TAURI_INTERNALS__.invoke('plugin:android-fs|noop');
29
30                // check noop is allowed
31                await noop()
32
33                setInterval(noop, 800)
34            })();
35        ";
36
37        #[tauri::command]
38        fn noop() {}
39
40        builder
41            .invoke_handler(tauri::generate_handler![noop])
42            .js_init_script(SCRIPT.into())  
43    };
44
45    builder.build()
46}
47
48pub trait AndroidFsExt<R: Runtime> {
49
50    fn android_fs(&self) -> &impl AndroidFs<R>;
51}
52
53impl<R: Runtime, T: Manager<R>> AndroidFsExt<R> for T {
54
55    fn android_fs(&self) -> &impl AndroidFs<R> {
56        self.state::<AndroidFsImpl<R>>().inner()
57    }
58}