From b18120b3f7223981e28c0441a6b7725787186edb Mon Sep 17 00:00:00 2001 From: Amruth Pillai Date: Sun, 13 Mar 2022 18:53:48 +0100 Subject: [PATCH] fix(app): fix issue with external link redirection in android app --- app/app/build.gradle | 2 +- app/app/src/main/AndroidManifest.xml | 9 +-- .../java/me/rxresu/app/CustomWebViewClient.kt | 21 +++++++ .../main/java/me/rxresu/app/MainActivity.kt | 57 +++---------------- app/app/src/main/res/layout/content_main.xml | 1 - app/app/src/main/res/values/themes.xml | 8 +-- 6 files changed, 36 insertions(+), 62 deletions(-) create mode 100644 app/app/src/main/java/me/rxresu/app/CustomWebViewClient.kt diff --git a/app/app/build.gradle b/app/app/build.gradle index 801e4b954..2910ab650 100644 --- a/app/app/build.gradle +++ b/app/app/build.gradle @@ -10,7 +10,7 @@ android { applicationId "me.rxresu.app" minSdk 21 targetSdk 32 - versionCode 2 + versionCode 3 versionName "1.0" resConfigs "en" diff --git a/app/app/src/main/AndroidManifest.xml b/app/app/src/main/AndroidManifest.xml index 2325060a6..e6006731c 100644 --- a/app/app/src/main/AndroidManifest.xml +++ b/app/app/src/main/AndroidManifest.xml @@ -1,5 +1,6 @@ - @@ -10,11 +11,11 @@ android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" - android:theme="@style/Theme.ReactiveResume.NoActionBar"> + android:theme="@style/AppTheme"> + android:exported="true"> diff --git a/app/app/src/main/java/me/rxresu/app/CustomWebViewClient.kt b/app/app/src/main/java/me/rxresu/app/CustomWebViewClient.kt new file mode 100644 index 000000000..a97da13e8 --- /dev/null +++ b/app/app/src/main/java/me/rxresu/app/CustomWebViewClient.kt @@ -0,0 +1,21 @@ +package me.rxresu.app + +import android.content.Intent +import android.net.Uri +import android.webkit.WebView +import android.webkit.WebViewClient + +internal class CustomWebViewClient : WebViewClient() { + override fun shouldOverrideUrlLoading(view: WebView, url: String): Boolean { + val hostname = "rxresu.me" + val uri = Uri.parse(url) + + if (uri.host != null && uri.host!!.endsWith(hostname)) { + return false + } + + view.context.startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(url))) + + return true + } +} diff --git a/app/app/src/main/java/me/rxresu/app/MainActivity.kt b/app/app/src/main/java/me/rxresu/app/MainActivity.kt index ba63ae568..a57dbb402 100644 --- a/app/app/src/main/java/me/rxresu/app/MainActivity.kt +++ b/app/app/src/main/java/me/rxresu/app/MainActivity.kt @@ -1,21 +1,15 @@ package me.rxresu.app import android.annotation.SuppressLint -import android.graphics.Bitmap import android.os.Bundle -import android.view.KeyEvent -import android.webkit.WebResourceError -import android.webkit.WebResourceRequest import android.webkit.WebView -import android.webkit.WebViewClient import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() { private lateinit var webView: WebView - private var isLoaded: Boolean = false - private var webURL = "https://rxresu.me" + private var url = "https://rxresu.me" @SuppressLint("SetJavaScriptEnabled") override fun onCreate(savedInstanceState: Bundle?) { @@ -24,53 +18,18 @@ class MainActivity : AppCompatActivity() { webView = findViewById(R.id.webview) + webView.webViewClient = CustomWebViewClient() webView.settings.javaScriptEnabled = true webView.settings.userAgentString = "Mozilla/5.0 (Linux; Android 8.0; Pixel 2 Build/OPD3.170816.012) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.82 Mobile Safari/537.36" - } - - override fun onResume() { - if (!isLoaded) loadWebView() - super.onResume() + webView.loadUrl(url) } - private fun loadWebView() { - webView.loadUrl(webURL) - - webView.webViewClient = object : WebViewClient() { - override fun shouldOverrideUrlLoading(view: WebView?, request: WebResourceRequest?): Boolean { - val url = request?.url.toString() - view?.loadUrl(url) - return super.shouldOverrideUrlLoading(view, request) - } - - override fun onPageStarted(view: WebView?, url: String?, favicon: Bitmap?) { - super.onPageStarted(view, url, favicon) - } - - override fun onPageFinished(view: WebView?, url: String?) { - isLoaded = true - super.onPageFinished(view, url) - } - - override fun onReceivedError(view: WebView, request: WebResourceRequest, error: WebResourceError) { - isLoaded = false - super.onReceivedError(view, request, error) - } + override fun onBackPressed() { + if (webView.canGoBack()) { + webView.goBack() + } else { + super.onBackPressed() } } - - override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean { - if (event.action == KeyEvent.ACTION_DOWN) { - if (keyCode == KeyEvent.KEYCODE_BACK) { - if (webView.canGoBack()) { - webView.goBack() - } - - return true - } - } - - return super.onKeyDown(keyCode, event) - } } diff --git a/app/app/src/main/res/layout/content_main.xml b/app/app/src/main/res/layout/content_main.xml index f4dc8711c..238cd0220 100644 --- a/app/app/src/main/res/layout/content_main.xml +++ b/app/app/src/main/res/layout/content_main.xml @@ -2,7 +2,6 @@ diff --git a/app/app/src/main/res/values/themes.xml b/app/app/src/main/res/values/themes.xml index ea6cbf296..b81c93fa6 100644 --- a/app/app/src/main/res/values/themes.xml +++ b/app/app/src/main/res/values/themes.xml @@ -1,12 +1,6 @@ - - -