fix(app): fix issue with external link redirection in android app

This commit is contained in:
Amruth Pillai 2022-03-13 18:53:48 +01:00
parent ebd9253038
commit b18120b3f7
No known key found for this signature in database
GPG Key ID: E3C57DF9B80855AD
6 changed files with 37 additions and 63 deletions

View File

@ -10,7 +10,7 @@ android {
applicationId "me.rxresu.app"
minSdk 21
targetSdk 32
versionCode 2
versionCode 3
versionName "1.0"
resConfigs "en"

View File

@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="me.rxresu.app">
<uses-permission android:name="android.permission.INTERNET"/>
@ -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">
<activity
android:configChanges="orientation|screenSize"
android:name=".MainActivity"
android:exported="true"
android:theme="@style/Theme.ReactiveResume.NoActionBar">
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

View File

@ -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
}
}

View File

@ -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"
webView.loadUrl(url)
}
override fun onResume() {
if (!isLoaded) loadWebView()
super.onResume()
}
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)
}
}

View File

@ -2,7 +2,6 @@
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">

View File

@ -1,12 +1,6 @@
<resources>
<style name="Theme.ReactiveResume" parent="Theme.MaterialComponents.DayNight.DarkActionBar" />
<style name="Theme.ReactiveResume.NoActionBar">
<style name="AppTheme" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="Theme.ReactiveResume.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="Theme.ReactiveResume.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
</resources>