Skip to content

Commit

Permalink
fix(app): fix issue with external link redirection in android app
Browse files Browse the repository at this point in the history
  • Loading branch information
AmruthPillai committed Mar 13, 2022
1 parent ebd9253 commit b18120b
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 62 deletions.
2 changes: 1 addition & 1 deletion app/app/build.gradle
Expand Up @@ -10,7 +10,7 @@ android {
applicationId "me.rxresu.app"
minSdk 21
targetSdk 32
versionCode 2
versionCode 3
versionName "1.0"

resConfigs "en"
Expand Down
9 changes: 5 additions & 4 deletions app/app/src/main/AndroidManifest.xml
@@ -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"/>
Expand All @@ -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" />

Expand Down
21 changes: 21 additions & 0 deletions 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
}
}
57 changes: 8 additions & 49 deletions 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?) {
Expand All @@ -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)
}
}
1 change: 0 additions & 1 deletion app/app/src/main/res/layout/content_main.xml
Expand Up @@ -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">

Expand Down
8 changes: 1 addition & 7 deletions app/app/src/main/res/values/themes.xml
@@ -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>

0 comments on commit b18120b

Please sign in to comment.