tauri_plugin_silent_mode/lib.rs
1//! `tauri-plugin-silent-mode`: tells an app whether the phone is set to be
2//! quiet, so its sounds can keep to the choice the player made for the whole
3//! phone.
4//!
5//! Only Android has a ringer mode an app can read. There the Kotlin side
6//! answers `state` and sends `change` whenever the mode moves. On iOS the
7//! silent switch already mutes an app's sound when it asks for ambient audio,
8//! and a desktop has no such mode, so everywhere else `state` answers
9//! [`Mode::Normal`] and `change` never fires.
10
11use serde::Serialize;
12use tauri::plugin::{Builder, TauriPlugin};
13use tauri::Runtime;
14
15/// How the phone's ringer is set.
16#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
17#[serde(rename_all = "camelCase")]
18pub enum Mode {
19 /// Sounds play.
20 Normal,
21 /// The phone buzzes instead of ringing.
22 Vibrate,
23 /// The phone neither rings nor buzzes.
24 Silent,
25}
26
27/// What `state` answers, in the shape the Kotlin side answers it.
28#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
29pub struct State {
30 pub mode: Mode,
31}
32
33/// Where no ringer mode can be read: always [`Mode::Normal`]. On Android the
34/// Kotlin plugin answers this command instead, as it has no Rust handler there.
35#[cfg(not(target_os = "android"))]
36#[tauri::command]
37fn state() -> State {
38 State { mode: Mode::Normal }
39}
40
41/// Registers the plugin: the Kotlin side on Android, the `state` stub
42/// everywhere else.
43#[must_use]
44pub fn init<R: Runtime>() -> TauriPlugin<R> {
45 let builder = Builder::new("silent-mode");
46 #[cfg(not(target_os = "android"))]
47 let builder = builder.invoke_handler(tauri::generate_handler![state]);
48 builder
49 .setup(|_app, _api| {
50 #[cfg(target_os = "android")]
51 _api.register_android_plugin("dev.hoangmirs.silentmode", "SilentModePlugin")?;
52 Ok(())
53 })
54 .build()
55}