|
2 | 2 | // SPDX-License-Identifier: Apache-2.0 |
3 | 3 | // SPDX-License-Identifier: MIT |
4 | 4 |
|
5 | | -use std::path::PathBuf; |
| 5 | +use std::{collections::HashSet, path::PathBuf}; |
6 | 6 |
|
7 | 7 | use anyhow::{Context, Result}; |
8 | | -use tauri_utils::write_if_changed; |
| 8 | +use tauri_utils::{config::AndroidIntentAction, write_if_changed}; |
| 9 | + |
| 10 | +/// Updates the Android manifest to add file association intent filters |
| 11 | +pub fn update_android_manifest_file_associations( |
| 12 | + associations: &[tauri_utils::config::FileAssociation], |
| 13 | +) -> Result<()> { |
| 14 | + if associations.is_empty() { |
| 15 | + return Ok(()); |
| 16 | + } |
| 17 | + |
| 18 | + let intent_filters = generate_file_association_intent_filters(associations); |
| 19 | + tauri_utils::build::update_android_manifest("tauri-file-associations", "activity", intent_filters) |
| 20 | +} |
| 21 | + |
| 22 | +fn generate_file_association_intent_filters( |
| 23 | + associations: &[tauri_utils::config::FileAssociation], |
| 24 | +) -> String { |
| 25 | + let mut filters = String::new(); |
| 26 | + |
| 27 | + for association in associations { |
| 28 | + // Get mime types - use explicit mime_type, or infer from extensions |
| 29 | + let mut mime_types = HashSet::new(); |
| 30 | + |
| 31 | + if let Some(mime_type) = &association.mime_type { |
| 32 | + mime_types.insert(( |
| 33 | + mime_type.clone(), |
| 34 | + association.android_intent_action_filters.clone(), |
| 35 | + )); |
| 36 | + } else { |
| 37 | + // Infer mime types from extensions |
| 38 | + for ext in &association.ext { |
| 39 | + if let Some(mime) = extension_to_mime_type(&ext.0) { |
| 40 | + mime_types.insert((mime, association.android_intent_action_filters.clone())); |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + // If we have mime types, create intent filters |
| 46 | + if !mime_types.is_empty() { |
| 47 | + for (mime_type, actions) in &mime_types { |
| 48 | + filters.push_str("<intent-filter>\n"); |
| 49 | + if let Some(actions) = actions { |
| 50 | + for action in actions { |
| 51 | + let action = match action { |
| 52 | + AndroidIntentAction::Send => "SEND", |
| 53 | + AndroidIntentAction::SendMultiple => "SEND_MULTIPLE", |
| 54 | + AndroidIntentAction::View => "VIEW", |
| 55 | + _ => unimplemented!(), |
| 56 | + }; |
| 57 | + filters.push_str(&format!( |
| 58 | + " <action android:name=\"android.intent.action.{action}\" />\n" |
| 59 | + )); |
| 60 | + } |
| 61 | + } else { |
| 62 | + filters.push_str(" <action android:name=\"android.intent.action.SEND\" />\n"); |
| 63 | + filters.push_str(" <action android:name=\"android.intent.action.SEND_MULTIPLE\" />\n"); |
| 64 | + filters.push_str(" <action android:name=\"android.intent.action.VIEW\" />\n"); |
| 65 | + } |
| 66 | + filters.push_str(" <category android:name=\"android.intent.category.DEFAULT\" />\n"); |
| 67 | + filters.push_str(" <category android:name=\"android.intent.category.BROWSABLE\" />\n"); |
| 68 | + filters.push_str(&format!( |
| 69 | + " <data android:mimeType=\"{}\" />\n", |
| 70 | + mime_type |
| 71 | + )); |
| 72 | + |
| 73 | + // Add file scheme and path patterns for extensions |
| 74 | + if !association.ext.is_empty() { |
| 75 | + // Create path patterns for each extension |
| 76 | + // Android's pathPattern needs \\. (double backslash-dot) in XML to match a literal dot |
| 77 | + let path_patterns: Vec<String> = association |
| 78 | + .ext |
| 79 | + .iter() |
| 80 | + .map(|ext| format!(".*\\\\.{}", ext.0)) |
| 81 | + .collect(); |
| 82 | + |
| 83 | + for pattern in &path_patterns { |
| 84 | + filters.push_str(&format!( |
| 85 | + " <data android:pathPattern=\"{}\" />\n", |
| 86 | + pattern |
| 87 | + )); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + filters.push_str("</intent-filter>\n"); |
| 92 | + } |
| 93 | + } else if !association.ext.is_empty() { |
| 94 | + // If no mime type but we have extensions, use a generic approach |
| 95 | + filters.push_str("<intent-filter>\n"); |
| 96 | + filters.push_str(" <action android:name=\"android.intent.action.VIEW\" />\n"); |
| 97 | + filters.push_str(" <category android:name=\"android.intent.category.DEFAULT\" />\n"); |
| 98 | + filters.push_str(" <category android:name=\"android.intent.category.BROWSABLE\" />\n"); |
| 99 | + |
| 100 | + for ext in &association.ext { |
| 101 | + // Android's pathPattern needs \\. (double backslash-dot) in XML to match a literal dot |
| 102 | + filters.push_str(&format!( |
| 103 | + " <data android:pathPattern=\".*\\\\.{}\" />\n", |
| 104 | + ext.0 |
| 105 | + )); |
| 106 | + } |
| 107 | + |
| 108 | + filters.push_str("</intent-filter>\n"); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + filters |
| 113 | +} |
| 114 | + |
| 115 | +fn extension_to_mime_type(ext: &str) -> Option<String> { |
| 116 | + Some( |
| 117 | + match ext.to_lowercase().as_str() { |
| 118 | + "png" => "image/png", |
| 119 | + "jpg" | "jpeg" => "image/jpeg", |
| 120 | + "gif" => "image/gif", |
| 121 | + "bmp" => "image/bmp", |
| 122 | + "webp" => "image/webp", |
| 123 | + "svg" => "image/svg+xml", |
| 124 | + "ico" => "image/x-icon", |
| 125 | + "tiff" | "tif" => "image/tiff", |
| 126 | + "heic" | "heif" => "image/heic", |
| 127 | + "mp4" => "video/mp4", |
| 128 | + "mov" => "video/quicktime", |
| 129 | + "avi" => "video/x-msvideo", |
| 130 | + "mkv" => "video/x-matroska", |
| 131 | + "mp3" => "audio/mpeg", |
| 132 | + "wav" => "audio/wav", |
| 133 | + "aac" => "audio/aac", |
| 134 | + "m4a" => "audio/mp4", |
| 135 | + "pdf" => "application/pdf", |
| 136 | + "txt" => "text/plain", |
| 137 | + "html" | "htm" => "text/html", |
| 138 | + "json" => "application/json", |
| 139 | + "xml" => "application/xml", |
| 140 | + "rtf" => "application/rtf", |
| 141 | + _ => return None, |
| 142 | + } |
| 143 | + .to_string(), |
| 144 | + ) |
| 145 | +} |
9 | 146 |
|
10 | 147 | pub fn generate_gradle_files(project_dir: PathBuf) -> Result<()> { |
11 | 148 | let gradle_settings_path = project_dir.join("tauri.settings.gradle"); |
|
0 commit comments