termux_api/api/
notification.rs

1use std::process::Command;
2
3use super::errors::TermuxError;
4
5pub struct TermuxNotification {
6    pub title: Option<String>,
7    pub content: Option<String>,
8    pub action: Option<String>,
9    pub button1_text: Option<String>,
10    pub button1_action: Option<String>,
11    pub button2_text: Option<String>,
12    pub button2_action: Option<String>,
13    pub button3_text: Option<String>,
14    pub button3_action: Option<String>,
15    pub group: Option<String>,
16    pub id: Option<u32>,
17    pub image_path: Option<String>,
18    pub led_color: Option<String>,
19    pub led_off: Option<u32>,
20    pub led_on: Option<u32>,
21    pub on_delete: Option<String>,
22    pub ongoing: bool,
23    pub priority: Option<String>,
24    pub sound: bool,
25    pub vibrate_pattern: Option<String>,
26    pub notification_type: Option<String>,
27    pub media_next: Option<String>,
28    pub media_pause: Option<String>,
29    pub media_play: Option<String>,
30    pub media_previous: Option<String>,
31    pub alert_once: bool,
32}
33
34impl TermuxNotification {
35    pub fn title(mut self, title: &str) -> Self {
36        self.title = Some(title.to_string());
37        self
38    }
39
40    pub fn content(mut self, content: &str) -> Self {
41        self.content = Some(content.to_string());
42        self
43    }
44
45    pub fn action(mut self, action: &str) -> Self {
46        self.action = Some(action.to_string());
47        self
48    }
49
50    pub fn button1(mut self, text: &str, action: &str) -> Self {
51        self.button1_text = Some(text.to_string());
52        self.button1_action = Some(action.to_string());
53        self
54    }
55
56    pub fn button2(mut self, text: &str, action: &str) -> Self {
57        self.button2_text = Some(text.to_string());
58        self.button2_action = Some(action.to_string());
59        self
60    }
61
62    pub fn button3(mut self, text: &str, action: &str) -> Self {
63        self.button3_text = Some(text.to_string());
64        self.button3_action = Some(action.to_string());
65        self
66    }
67
68    pub fn group(mut self, group: &str) -> Self {
69        self.group = Some(group.to_string());
70        self
71    }
72
73    pub fn id(mut self, id: &u32) -> Self {
74        self.id = Some(id.clone());
75        self
76    }
77
78    pub fn image_path(mut self, path: &str) -> Self {
79        self.image_path = Some(path.to_string());
80        self
81    }
82
83    pub fn led_color(mut self, color: &str) -> Self {
84        self.led_color = Some(color.to_string());
85        self
86    }
87    pub fn led_off(mut self, milliseconds: u32) -> Self {
88        self.led_off = Some(milliseconds);
89        self
90    }
91
92    pub fn led_on(mut self, milliseconds: u32) -> Self {
93        self.led_on = Some(milliseconds);
94        self
95    }
96
97    pub fn on_delete(mut self, action: &str) -> Self {
98        self.on_delete = Some(action.to_string());
99        self
100    }
101
102    pub fn ongoing(mut self, ongoing: bool) -> Self {
103        self.ongoing = ongoing;
104        self
105    }
106
107    pub fn priority(mut self, priority: &str) -> Self {
108        self.priority = Some(priority.to_string());
109        self
110    }
111
112    pub fn sound(mut self, sound: bool) -> Self {
113        self.sound = sound;
114        self
115    }
116
117    pub fn vibrate(mut self, pattern: &str) -> Self {
118        self.vibrate_pattern = Some(pattern.to_string());
119        self
120    }
121
122    pub fn notification_type(mut self, ntype: &str) -> Self {
123        self.notification_type = Some(ntype.to_string());
124        self
125    }
126
127    pub fn media_next(mut self, action: &str) -> Self {
128        self.media_next = Some(action.to_string());
129        self
130    }
131
132    pub fn media_pause(mut self, action: &str) -> Self {
133        self.media_pause = Some(action.to_string());
134        self
135    }
136
137    pub fn media_play(mut self, action: &str) -> Self {
138        self.media_play = Some(action.to_string());
139        self
140    }
141
142    pub fn media_previous(mut self, action: &str) -> Self {
143        self.media_previous = Some(action.to_string());
144        self
145    }
146
147    pub fn alert_once(mut self, alert_once: bool) -> Self {
148        self.alert_once = alert_once;
149        self
150    }
151
152    pub fn new() -> Self {
153        Self {
154            action: None,
155            alert_once: false,
156            button1_text: None,
157            button1_action: None,
158            button2_text: None,
159            button2_action: None,
160            button3_text: None,
161            button3_action: None,
162            content: None,
163            group: None,
164            id: None,
165            image_path: None,
166            led_color: None,
167            led_off: None,
168            led_on: None,
169            on_delete: None,
170            ongoing: false,
171            priority: None,
172            sound: false,
173            title: None,
174            vibrate_pattern: None,
175            notification_type: None,
176            media_next: None,
177            media_pause: None,
178            media_play: None,
179            media_previous: None,
180        }
181    }
182    pub fn run(&self) -> Result<(), TermuxError> {
183        let mut command = Command::new("termux-notification");
184
185        if let Some(ref action) = self.action {
186            command.arg("--action").arg(action);
187        }
188        if self.alert_once {
189            command.arg("--alert-once");
190        }
191        if let Some(ref text) = self.button1_text {
192            command.arg("--button1").arg(text);
193        }
194        if let Some(ref text) = self.button1_action {
195            command.arg("--button1-action").arg(text);
196        }
197        if let Some(ref text) = self.button2_text {
198            command.arg("--button2").arg(text);
199        }
200        if let Some(ref text) = self.button2_action {
201            command.arg("--button2-action").arg(text);
202        }
203        if let Some(ref text) = self.button3_text {
204            command.arg("--button3").arg(text);
205        }
206        if let Some(ref text) = self.button3_action {
207            command.arg("--button3-action").arg(text);
208        }
209        if let Some(ref content) = self.content {
210            command.arg("-c").arg(content);
211        }
212        if let Some(ref group) = self.group {
213            command.arg("--group").arg(group);
214        }
215        if let Some(ref id) = self.id {
216            command.arg("-i").arg(format!("{}", id));
217        }
218        if let Some(ref image_path) = self.image_path {
219            command.arg("--image-path").arg(image_path);
220        }
221        if let Some(ref led_color) = self.led_color {
222            command.arg("--led-color").arg(led_color);
223        }
224        if let Some(led_off) = self.led_off {
225            command.arg("--led-off").arg(led_off.to_string());
226        }
227        if let Some(led_on) = self.led_on {
228            command.arg("--led-on").arg(led_on.to_string());
229        }
230        if let Some(ref on_delete) = self.on_delete {
231            command.arg("--on-delete").arg(on_delete);
232        }
233        if self.ongoing {
234            command.arg("--ongoing");
235        }
236        if let Some(ref priority) = self.priority {
237            command.arg("--priority").arg(priority);
238        }
239        if self.sound {
240            command.arg("--sound");
241        }
242        if let Some(ref title) = self.title {
243            command.arg("-t").arg(title);
244        }
245        if let Some(ref vibrate) = self.vibrate_pattern {
246            command.arg("--vibrate").arg(vibrate);
247        }
248        if let Some(ref notification_type) = self.notification_type {
249            command.arg("--type").arg(notification_type);
250        }
251        if let Some(ref media_next) = self.media_next {
252            command.arg("--media-next").arg(media_next);
253        }
254        if let Some(ref media_pause) = self.media_pause {
255            command.arg("--media-pause").arg(media_pause);
256        }
257        if let Some(ref media_play) = self.media_play {
258            command.arg("--media-play").arg(media_play);
259        }
260        if let Some(ref media_previous) = self.media_previous {
261            command.arg("--media-previous").arg(media_previous);
262        }
263
264        let output = command.output();
265        match output {
266            Ok(output) => {
267                if output.status.success() {
268                    return Ok(());
269                }
270                Err(TermuxError::Output(output))
271            }
272            Err(e) => Err(TermuxError::IOError(e)),
273        }
274    }
275}