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 ///
43 /// By default, [`ProgressNotificationGuard`] finishes the notification as a "failure" when dropped.
44 /// You can change the drop behavior by using [`ProgressNotificationGuard::set_drop_behavior_to_complete`] or [`ProgressNotificationGuard::set_drop_behavior_to_fail`].
45 ///
46 /// # Note
47 /// This needs two steps:
48 ///
49 /// 1. Declare :
50 /// By enabling the `notification_permission` feature,
51 /// you can declare the permissions automatically at build time.
52 ///
53 /// 2. Runtime request :
54 /// By calling [`Utils::request_notification_permission`],
55 /// you can request the permissions from the user at runtime.
56 ///
57 /// # Support
58 /// All Android version.
59 #[maybe_async]
60 pub fn create_progress_notification(
61 &self,
62 icon: ProgressNotificationIcon,
63 title: Option<&str>,
64 text: Option<&str>,
65 sub_text: Option<&str>,
66 progress: Option<u64>,
67 progress_max: Option<u64>,
68 ) -> Result<ProgressNotificationGuard<R>> {
69
70 #[cfg(not(target_os = "android"))] {
71 Err(Error::NOT_ANDROID)
72 }
73 #[cfg(target_os = "android")] {
74 ProgressNotificationGuard::start_new_notification(
75 icon,
76 title.map(|s| s.to_string()),
77 text.map(|s| s.to_string()),
78 sub_text.map(|s| s.to_string()),
79 progress,
80 progress_max,
81 self.handle.clone()
82 ).await
83 }
84 }
85
86 /// # Note
87 /// To request the permissions, you must declare [`POST_NOTIFICATIONS`](https://developer.android.com/reference/android/Manifest.permission#POST_NOTIFICATIONS) in `AndroidManifest.xml`.
88 /// By enabling the `notification_permission` feature,
89 /// the permissions will be declared automatically.
90 #[maybe_async]
91 pub fn request_notification_permission(&self) -> Result<bool> {
92 #[cfg(not(target_os = "android"))] {
93 Err(Error::NOT_ANDROID)
94 }
95 #[cfg(target_os = "android")] {
96 self.impls().request_notification_permission().await
97 }
98 }
99
100 #[maybe_async]
101 pub fn check_notification_permission(&self) -> Result<bool> {
102 #[cfg(not(target_os = "android"))] {
103 Err(Error::NOT_ANDROID)
104 }
105 #[cfg(target_os = "android")] {
106 self.impls().check_notification_permission().await
107 }
108 }
109}