tauri_plugin_android_battery_optimization/
commands.rs

1use tauri::Runtime;
2
3use crate::{BatteryStatus, Error};
4
5/// Check the current battery optimization status.
6///
7/// Returns a `BatteryStatus` object with:
8/// - `isOptimized`: `true` if the app is subject to battery optimization
9/// - `isIgnoringOptimizations`: `true` if the app has unrestricted background access
10#[tauri::command]
11pub fn check_battery_optimization_status<R: Runtime>(
12    app: tauri::AppHandle<R>,
13) -> Result<BatteryStatus, Error> {
14    #[cfg(target_os = "android")]
15    {
16        app.state::<AndroidBatteryOptimization<R>>().check_status()
17    }
18    #[cfg(not(target_os = "android"))]
19    {
20        let _ = app;
21        Ok(BatteryStatus {
22            is_optimized: false,
23            is_ignoring_optimizations: true,
24        })
25    }
26}
27
28/// Request exemption from battery optimization.
29///
30/// This will show a system dialog asking the user to allow the app
31/// to run without battery optimization restrictions.
32#[tauri::command]
33pub fn request_battery_optimization_exemption<R: Runtime>(
34    app: tauri::AppHandle<R>,
35) -> Result<(), Error> {
36    #[cfg(target_os = "android")]
37    {
38        app.state::<AndroidBatteryOptimization<R>>()
39            .request_exemption()
40    }
41    #[cfg(not(target_os = "android"))]
42    {
43        let _ = app;
44        Ok(())
45    }
46}
47
48/// Open the system battery optimization settings page.
49///
50/// This allows users to manually configure battery optimization
51/// for all installed apps.
52#[tauri::command]
53pub fn open_battery_settings<R: Runtime>(app: tauri::AppHandle<R>) -> Result<(), Error> {
54    #[cfg(target_os = "android")]
55    {
56        app.state::<AndroidBatteryOptimization<R>>().open_settings()
57    }
58    #[cfg(not(target_os = "android"))]
59    {
60        let _ = app;
61        Ok(())
62    }
63}