Skip to main content

tauri_plugin_android_fs/api/
utils.rs

1use sync_async::sync_async;
2use crate::*;
3use super::*;
4
5
6/// API of utils.
7#[sync_async]
8pub struct Utils<'a, R: tauri::Runtime> {
9    #[cfg(target_os = "android")]
10    pub(crate) handle: &'a tauri::plugin::PluginHandle<R>,
11
12    #[cfg(not(target_os = "android"))]
13    #[allow(unused)]
14    pub(crate) handle: &'a std::marker::PhantomData<fn() -> R>,
15}
16
17#[cfg(target_os = "android")]
18#[sync_async(
19    use(if_sync) impls::SyncImpls as Impls;
20    use(if_async) impls::AsyncImpls as Impls;
21)]
22impl<'a, R: tauri::Runtime> Utils<'a, R> {
23    
24    #[always_sync]
25    fn impls(&self) -> Impls<'_, R> {
26        Impls { handle: &self.handle }
27    }
28}
29
30#[sync_async(
31    use(if_async) api_async::{AndroidFs, ProgressNotificationGuard};
32    use(if_sync) api_sync::{AndroidFs, ProgressNotificationGuard};
33)]
34impl<'a, R: tauri::Runtime> Utils<'a, R> {
35
36    /// Displays a notification indicating progress on status bar.  
37    /// 
38    /// The returned [`ProgressNotificationGuard`] can be used to manage the notification.
39    /// - Calling [`ProgressNotificationGuard::update`] will update the notification.  
40    /// - Calling [`ProgressNotificationGuard::complete`] will finish the notification as a "success".  
41    /// - Calling [`ProgressNotificationGuard::fail`] will finish the notification as a "failure".
42    /// - Calling [`ProgressNotificationGuard::cancel`] will finish and close the notification.  
43    /// 
44    /// By default, it finishes the notification as a "failure" when dropped.   
45    /// You can change the drop behavior by using `ProgressNotificationGuard::set_drop_behavior_to_*`.  
46    /// 
47    /// # Note
48    /// This needs two steps: 
49    /// 
50    /// 1. Declare :  
51    ///     By enabling the `notification_permission` feature,  
52    ///     you can declare the permissions automatically at build time.  
53    ///
54    /// 2. Runtime request :  
55    ///     By calling [`Utils::request_notification_permission`],
56    ///     you can request the permissions from the user at runtime.  
57    /// 
58    /// # Support
59    /// All Android version.
60    #[maybe_async]
61    pub fn create_progress_notification(
62        &self,
63        icon: ProgressNotificationIcon,
64        title: Option<&str>,
65        text: Option<&str>,
66        sub_text: Option<&str>,
67        progress: Option<u64>,
68        progress_max: Option<u64>,
69    ) -> Result<ProgressNotificationGuard<R>> {
70
71        #[cfg(not(target_os = "android"))] {
72            Err(Error::NOT_ANDROID)
73        }
74        #[cfg(target_os = "android")] {
75            ProgressNotificationGuard::with_new_notification(
76                icon,
77                title.map(|s| s.to_string()), 
78                text.map(|s| s.to_string()),
79                sub_text.map(|s| s.to_string()),
80                progress,
81                progress_max,
82                self.handle.clone()
83            ).await
84        }
85    }
86
87    #[maybe_async]
88    pub fn cancel_all_notifications(&self) -> Result<()> {
89        #[cfg(not(target_os = "android"))] {
90            Err(Error::NOT_ANDROID)
91        }
92        #[cfg(target_os = "android")] {
93            self.impls().cancel_all_notifications().await
94        }
95    }
96
97    /// # Note
98    /// To request the permissions, you must declare [`POST_NOTIFICATIONS`](https://developer.android.com/reference/android/Manifest.permission#POST_NOTIFICATIONS) in `AndroidManifest.xml`.
99    /// By enabling the `notification_permission` feature,
100    /// the permissions will be declared automatically.
101    #[maybe_async]
102    pub fn request_notification_permission(&self) -> Result<bool> {
103        #[cfg(not(target_os = "android"))] {
104            Err(Error::NOT_ANDROID)
105        }
106        #[cfg(target_os = "android")] {
107            self.impls().request_notification_permission().await
108        }
109    }
110
111    #[maybe_async]
112    pub fn check_notification_permission(&self) -> Result<bool> {
113        #[cfg(not(target_os = "android"))] {
114            Err(Error::NOT_ANDROID)
115        }
116        #[cfg(target_os = "android")] {
117            self.impls().check_notification_permission().await
118        }
119    }
120}