Skip to content

Commit

Permalink
initial plugin config with androidmanifest rewrite
Browse files Browse the repository at this point in the history
  • Loading branch information
FabianLars committed Jul 19, 2023
1 parent a379e6a commit c4356b4
Show file tree
Hide file tree
Showing 11 changed files with 131 additions and 20 deletions.
5 changes: 5 additions & 0 deletions plugins/deep-link/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ links = "tauri-plugin-deep-link"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[build-dependencies]
serde = "1"
serde_json = "1"
tauri-build = { git = "https://github.com/lucasfernog/tauri", branch = "feat/ipc-custom-protocol" }

[dependencies]
Expand All @@ -19,3 +21,6 @@ log = "0.4"
thiserror = "1"
url = "2"

[patch."https://github.com/lucasfernog/tauri"]
tauri-build = { path = "C:/Users/Fabian-Lars/dev/FabianLars/tauri-lucas/core/tauri-build" }
tauri = { path = "C:/Users/Fabian-Lars/dev/FabianLars/tauri-lucas/core/tauri" }
4 changes: 1 addition & 3 deletions plugins/deep-link/android/src/main/WantedAndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<!-- TODO: (tauri-cli?) Add auto manifest rewriter to add domains. Format: -->

<application>
<activity android:name="app-id.MainActivity" android:exported="true">
<activity android:name="${applicationId}.MainActivity" android:exported="true">
<intent-filter android:autoVerify="true">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
Expand Down
2 changes: 1 addition & 1 deletion plugins/deep-link/android/src/main/java/DeepLinkPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class DeepLinkPlugin(private val activity: Activity): Plugin(activity) {
if (intent.action == Intent.ACTION_VIEW) {
// TODO: check if it makes sense to split up init url and last url
this.lastUrl = intent.data.toString()
// TODO: Emit event - may not work here timing wise
// TODO: Test if emitting it here makes sense timing wise
val event = JSObject()
event.put("url", this.lastUrl)
this.channel?.send(event)
Expand Down
62 changes: 62 additions & 0 deletions plugins/deep-link/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,38 @@

use std::process::exit;

#[derive(Clone, serde::Deserialize)]
struct Config {
android: Vec<AndroidConfig>,
}

#[derive(Clone, serde::Deserialize)]
struct AndroidConfig {
domain: String,
#[serde(rename = "pathPrefix")]
path_prefix: Option<String>,
}

// TODO: Consider using activity-alias in case users may have multiple activities in their app.
// TODO: Do we need multiple pathPrefixes? Do we want to support the other path* configs too?
fn intent_filter(domain: &str, path: Option<&str>) -> String {
format!(
r#"<intent-filter android:autoVerify="true">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="http" />
<data android:scheme="https" />
<data android:host="{domain}" />{}
</intent-filter>"#,
if let Some(path) = path {
format!("\n <data android:pathPrefix=\"{path}\" />")
} else {
String::new()
}
)
}

fn main() {
if let Err(error) = tauri_build::mobile::PluginBuilder::new()
.android_path("android")
Expand All @@ -12,4 +44,34 @@ fn main() {
println!("{error:#}");
exit(1);
}

if let Some(config) = tauri_build::config::plugin_config::<Config>("deep-link") {
tauri_build::mobile::update_android_manifest(
"DEEP LINK PLUGIN",
"activity",
config
.android
.iter()
.map(|e| intent_filter(&e.domain, e.path_prefix.as_deref()))
.collect::<Vec<_>>()
.join("\n"),
)
.expect("failed to rewrite AndroidManifest.xml");

/* #[cfg(target_os = "macos")]
{
tauri_build::mobile::update_entitlements(|entitlements| {
entitlements.insert(
"com.apple.developer.associated-domains".into(),
config
.domains
.into_iter()
.map(Into::into)
.collect::<Vec<_>>()
.into(),
);
})
.expect("failed to update entitlements");
} */
}
}
13 changes: 4 additions & 9 deletions plugins/deep-link/examples/old/src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions plugins/deep-link/examples/old/src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,15 @@ tauri-build = { git = "https://github.com/lucasfernog/tauri", branch = "feat/ipc
[dependencies]
tauri = { git = "https://github.com/lucasfernog/tauri", branch = "feat/ipc-custom-protocol", features = [] }
tauri-plugin-deep-link = { path = "../../../" }
serde = "1"
serde_json = "1"

[features]
# this feature is used for production builds or when `devPath` points to the filesystem and the built-in dev server is disabled.
# If you use cargo directly instead of tauri's cli you can use this feature flag to switch between tauri's `dev` and `build` modes.
# DO NOT REMOVE!!
custom-protocol = [ "tauri/custom-protocol" ]

[patch."https://github.com/lucasfernog/tauri"]
tauri-build = { path = "C:/Users/Fabian-Lars/dev/FabianLars/tauri-lucas/core/tauri-build" }
tauri = { path = "C:/Users/Fabian-Lars/dev/FabianLars/tauri-lucas/core/tauri" }
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,39 @@
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter android:autoVerify="true">
<!-- <intent-filter android:autoVerify="true">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="http" />
<data android:scheme="https" />
<!-- Each host needs a new intent-filter -->
<!- - Each host needs a new intent-filter - ->
<data android:host="fabianlars.de" />
<!-- This should also be configurable otherwise the app will trigger on all paths -->
<!- - This should also be configurable otherwise the app will trigger on all paths - ->
<data android:pathPrefix="/intent" />
</intent-filter> -->
<!-- DEEP LINK PLUGIN. AUTO-GENERATED. DO NOT REMOVE. -->
<intent-filter android:autoVerify="true">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="http" />
<data android:scheme="https" />
<data android:host="fabianlars.de" />
<data android:pathPrefix="/intent" />
</intent-filter>
<intent-filter android:autoVerify="true">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="http" />
<data android:scheme="https" />
<data android:host="tauri.app" />
</intent-filter>
<!-- DEEP LINK PLUGIN. AUTO-GENERATED. DO NOT REMOVE. -->
</activity>

<provider android:name="androidx.core.content.FileProvider" android:authorities="${applicationId}.fileprovider" android:exported="false" android:grantUriPermissions="true">
Expand Down
16 changes: 14 additions & 2 deletions plugins/deep-link/examples/old/src-tauri/tauri.conf.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"build": {
"beforeDevCommand": "yarn dev",
"beforeBuildCommand": "yarn build",
"beforeDevCommand": "pnpm dev",
"beforeBuildCommand": "pnpm build",
"devPath": "http://localhost:1420",
"distDir": "../dist",
"withGlobalTauri": false
Expand Down Expand Up @@ -56,5 +56,17 @@
"width": 800
}
]
},
"plugins": {
"whatever": "helloworld",
"sec": {
"hello": "world"
},
"deep-link": {
"android": [
{ "domain": "fabianlars.de", "pathPrefix": "/intent" },
{ "domain": "tauri.app" }
]
}
}
}
2 changes: 1 addition & 1 deletion plugins/deep-link/guest-js/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ export async function getLastLink(): Promise<string[] | null> {
return await window.__TAURI_INVOKE__("plugin:deep-link|get_last_link");
}

// TODO: REGISTER EVENT LISTENER
// TODO: onUrlEvent function (helper function for the event listener)
13 changes: 13 additions & 0 deletions plugins/deep-link/src/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#![allow(dead_code)]

#[derive(serde::Deserialize)]
pub struct Config {
android: Vec<AndroidConfig>,
}

#[derive(serde::Deserialize)]
pub struct AndroidConfig {
domain: String,
#[serde(rename = "pathPrefix")]
path_prefix: Option<String>,
}
3 changes: 2 additions & 1 deletion plugins/deep-link/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use tauri::{
};

mod commands;
mod config;
mod error;

pub use error::{Error, Result};
Expand Down Expand Up @@ -129,7 +130,7 @@ impl<R: Runtime, T: Manager<R>> crate::DeepLinkExt<R> for T {
}

/// Initializes the plugin.
pub fn init<R: Runtime>() -> TauriPlugin<R> {
pub fn init<R: Runtime>() -> TauriPlugin<R, Option<config::Config>> {
Builder::new("deep-link")
.js_init_script(include_str!("api-iife.js").to_string())
.invoke_handler(tauri::generate_handler![
Expand Down

0 comments on commit c4356b4

Please sign in to comment.