From 7aa328b3dced477946ef00dee9e4eef38da18905 Mon Sep 17 00:00:00 2001 From: Konrad Pozniak Date: Fri, 15 Apr 2022 10:50:28 +0200 Subject: [PATCH] fix login on Android API level <24 (#2432) --- .../components/login/LoginWebViewActivity.kt | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/com/keylesspalace/tusky/components/login/LoginWebViewActivity.kt b/app/src/main/java/com/keylesspalace/tusky/components/login/LoginWebViewActivity.kt index 01f6c3b0e..827b56208 100644 --- a/app/src/main/java/com/keylesspalace/tusky/components/login/LoginWebViewActivity.kt +++ b/app/src/main/java/com/keylesspalace/tusky/components/login/LoginWebViewActivity.kt @@ -16,6 +16,7 @@ import android.webkit.WebStorage import android.webkit.WebView import android.webkit.WebViewClient import androidx.activity.result.contract.ActivityResultContract +import androidx.core.net.toUri import com.keylesspalace.tusky.BaseActivity import com.keylesspalace.tusky.BuildConfig import com.keylesspalace.tusky.databinding.LoginWebviewBinding @@ -103,8 +104,8 @@ class LoginWebViewActivity : BaseActivity(), Injectable { webView.webViewClient = object : WebViewClient() { override fun onReceivedError( - view: WebView?, - request: WebResourceRequest?, + view: WebView, + request: WebResourceRequest, error: WebResourceError ) { Log.d("LoginWeb", "Failed to load ${data.url}: $error") @@ -115,7 +116,17 @@ class LoginWebViewActivity : BaseActivity(), Injectable { view: WebView, request: WebResourceRequest ): Boolean { - val url = request.url + return shouldOverrideUrlLoading(request.url) + } + + /* overriding this deprecated method is necessary for it to work on api levels < 24 */ + @Suppress("OVERRIDE_DEPRECATION") + override fun shouldOverrideUrlLoading(view: WebView?, urlString: String?): Boolean { + val url = urlString?.toUri() ?: return false + return shouldOverrideUrlLoading(url) + } + + fun shouldOverrideUrlLoading(url: Uri): Boolean { return if (url.scheme == oauthUrl.scheme && url.host == oauthUrl.host) { val error = url.getQueryParameter("error") if (error != null) { @@ -130,6 +141,7 @@ class LoginWebViewActivity : BaseActivity(), Injectable { } } } + webView.setBackgroundColor(Color.TRANSPARENT) if (savedInstanceState == null) {