From 783cf03121afa33e6aeb343cc238cca4313d3cff Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Sun, 9 Aug 2026 20:08:35 +0000 Subject: [PATCH] Fix dynamic redirect for offline local links in Android app Co-authored-by: SayanthRock <202829406+SayanthRock@users.noreply.github.com> --- app/src/main/java/com/example/MainActivity.kt | 6 ++- .../java/com/example/viewmodel/QRViewModel.kt | 52 ++++++++++++++++++- 2 files changed, 56 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/com/example/MainActivity.kt b/app/src/main/java/com/example/MainActivity.kt index 7bdd9e5..f10f84d 100644 --- a/app/src/main/java/com/example/MainActivity.kt +++ b/app/src/main/java/com/example/MainActivity.kt @@ -93,7 +93,11 @@ class MainActivity : ComponentActivity() { private fun handleIntent(intent: android.content.Intent?, viewModel: QRViewModel) { intent?.dataString?.let { dataStr -> if (dataStr.contains("sayanthrock.github.io/Rock-QR-Code")) { - viewModel.importFromDeepLink(dataStr) + if (dataStr.contains("/redirect?code=local_")) { + viewModel.resolveDynamicRedirect(dataStr) + } else { + viewModel.importFromDeepLink(dataStr) + } intent.data = null } } diff --git a/app/src/main/java/com/example/viewmodel/QRViewModel.kt b/app/src/main/java/com/example/viewmodel/QRViewModel.kt index 50a02dd..cc6e48e 100644 --- a/app/src/main/java/com/example/viewmodel/QRViewModel.kt +++ b/app/src/main/java/com/example/viewmodel/QRViewModel.kt @@ -321,8 +321,27 @@ class QRViewModel(application: Application) : AndroidViewModel(application) { }.sortedByDescending { it.timestamp } } - fun saveScannedResult(content: String) { + fun saveScannedResult(rawContent: String) { viewModelScope.launch(Dispatchers.IO) { + var content = rawContent + + // Resolve local dynamic redirect codes instantly! + if (content.contains("sayanthrock.github.io/Rock-QR-Code/redirect?code=local_")) { + try { + val uri = android.net.Uri.parse(content) + val code = uri.getQueryParameter("code") + if (code != null) { + val currentRecords = repository.allHistory.first() + val record = currentRecords.find { it.shortCode == code } + if (record != null) { + content = record.content // swap redirect link for real target! + } + } + } catch(e: Exception) { + // Ignore + } + } + val type = detectQrType(content) val title = when (type) { "URL" -> { @@ -349,6 +368,37 @@ class QRViewModel(application: Application) : AndroidViewModel(application) { } } + fun resolveDynamicRedirect(url: String) { + viewModelScope.launch(Dispatchers.IO) { + try { + val uri = android.net.Uri.parse(url) + val code = uri.getQueryParameter("code") + if (code != null && code.startsWith("local_")) { + val currentRecords = repository.allHistory.first() + val record = currentRecords.find { it.shortCode == code } + if (record != null) { + val targetUrl = record.content + + kotlinx.coroutines.withContext(Dispatchers.Main) { + showToast("Resolved local dynamic link", CustomToastType.SUCCESS) + val intent = android.content.Intent(android.content.Intent.ACTION_VIEW, android.net.Uri.parse(targetUrl)) + intent.addFlags(android.content.Intent.FLAG_ACTIVITY_NEW_TASK) + getApplication().startActivity(intent) + } + + saveScannedResult(targetUrl) + } else { + kotlinx.coroutines.withContext(Dispatchers.Main) { + showToast("Offline link not found on this device", CustomToastType.ERROR) + } + } + } + } catch (e: Exception) { + // Ignore + } + } + } + private fun detectQrType(content: String): String { return when { content.startsWith("http://", ignoreCase = true) || content.startsWith("https://", ignoreCase = true) -> "URL"