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" applicationId "me.rxresu.app"
minSdk 21 minSdk 21
targetSdk 32 targetSdk 32
versionCode 2 versionCode 3
versionName "1.0" versionName "1.0"
resConfigs "en" resConfigs "en"

View File

@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?> <?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"> package="me.rxresu.app">
<uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.INTERNET"/>
@ -10,11 +11,11 @@
android:label="@string/app_name" android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round" android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true" android:supportsRtl="true"
android:theme="@style/Theme.ReactiveResume.NoActionBar"> android:theme="@style/AppTheme">
<activity <activity
android:configChanges="orientation|screenSize"
android:name=".MainActivity" android:name=".MainActivity"
android:exported="true" android:exported="true">
android:theme="@style/Theme.ReactiveResume.NoActionBar">
<intent-filter> <intent-filter>
<action android:name="android.intent.action.MAIN" /> <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 package me.rxresu.app
import android.annotation.SuppressLint import android.annotation.SuppressLint
import android.graphics.Bitmap
import android.os.Bundle import android.os.Bundle
import android.view.KeyEvent
import android.webkit.WebResourceError
import android.webkit.WebResourceRequest
import android.webkit.WebView import android.webkit.WebView
import android.webkit.WebViewClient
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() { class MainActivity : AppCompatActivity() {
private lateinit var webView: WebView private lateinit var webView: WebView
private var isLoaded: Boolean = false private var url = "https://rxresu.me"
private var webURL = "https://rxresu.me"
@SuppressLint("SetJavaScriptEnabled") @SuppressLint("SetJavaScriptEnabled")
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
@ -24,53 +18,18 @@ class MainActivity : AppCompatActivity() {
webView = findViewById(R.id.webview) webView = findViewById(R.id.webview)
webView.webViewClient = CustomWebViewClient()
webView.settings.javaScriptEnabled = true 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.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() { override fun onBackPressed() {
if (!isLoaded) loadWebView() if (webView.canGoBack()) {
webView.goBack()
super.onResume() } else {
} super.onBackPressed()
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 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 <androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android" xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent"> android:layout_height="match_parent">

View File

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