tauri-plugin-intent 0.1.0

Tauri plugin for handling Android and iOS intents.
Documentation
use serde::de::DeserializeOwned;
use tauri::{plugin::PluginApi, AppHandle, Runtime};

use crate::models::*;

pub fn init<R: Runtime, C: DeserializeOwned>(
    app: &AppHandle<R>,
    _api: PluginApi<R, C>,
) -> crate::Result<Intent<R>> {
    Ok(Intent(app.clone()))
}

/// Access to the intent APIs.
pub struct Intent<R: Runtime>(AppHandle<R>);

impl<R: Runtime> Intent<R> {
    pub async fn open_intent(
        &self,
        payload: OpenIntentRequest,
    ) -> crate::Result<OpenIntentResponse> {
        // On desktop, we can't open Android intents directly
        // This is a fallback implementation that could use system default handlers
        match payload.action.as_str() {
            "android.intent.action.VIEW" => {
                if let Some(data) = payload.data {
                    // Try to open the URL with the system default handler
                    #[cfg(desktop)]
                    {
                        if let Err(e) = opener::open(&data) {
                            return Ok(OpenIntentResponse {
                                success: false,
                                error: Some(format!("Failed to open URL: {}", e)),
                            });
                        }
                    }
                    Ok(OpenIntentResponse {
                        success: true,
                        error: None,
                    })
                } else {
                    Ok(OpenIntentResponse {
                        success: false,
                        error: Some("No data provided for VIEW intent".to_string()),
                    })
                }
            }
            "android.intent.action.CALL" => {
                // Desktop doesn't support phone calls directly
                Ok(OpenIntentResponse {
                    success: false,
                    error: Some("Phone calls are not supported on desktop".to_string()),
                })
            }
            _ => Ok(OpenIntentResponse {
                success: false,
                error: Some(format!("Unsupported intent action: {}", payload.action)),
            }),
        }
    }
}