Improved code to show external browser url.

* We are now showing proper error messages to the user when there is no app installed on their device to handle the pdf/epub/URL.
This commit is contained in:
MohitMali 2023-10-11 18:12:49 +05:30
parent bb5e4d34c1
commit c48eabc261
8 changed files with 40 additions and 20 deletions

View File

@ -51,7 +51,7 @@ internal class ExternalLinkOpenerTest {
every { intent.data } returns Uri.parse(url.toString())
val lambdaSlot = slot<() -> Unit>()
val externalLinkOpener = ExternalLinkOpener(activity, sharedPreferenceUtil, alertDialogShower)
externalLinkOpener.openExternalUrl(intent)
externalLinkOpener.openExternalUrl(intent, R.string.no_browser_application_installed)
verify {
alertDialogShower.show(
KiwixDialog.ExternalLinkPopup,
@ -72,7 +72,7 @@ internal class ExternalLinkOpenerTest {
every { intent.data } returns uri
val lambdaSlot = slot<() -> Unit>()
val externalLinkOpener = ExternalLinkOpener(activity, sharedPreferenceUtil, alertDialogShower)
externalLinkOpener.openExternalUrl(intent)
externalLinkOpener.openExternalUrl(intent, R.string.no_browser_application_installed)
verify {
alertDialogShower.show(
KiwixDialog.ExternalLinkPopup,
@ -92,7 +92,7 @@ internal class ExternalLinkOpenerTest {
every { intent.data } returns Uri.parse("https://github.com/")
val lambdaSlot = slot<() -> Unit>()
val externalLinkOpener = ExternalLinkOpener(activity, sharedPreferenceUtil, alertDialogShower)
externalLinkOpener.openExternalUrl(intent)
externalLinkOpener.openExternalUrl(intent, R.string.no_browser_application_installed)
verify {
alertDialogShower.show(
KiwixDialog.ExternalLinkPopup,
@ -112,7 +112,7 @@ internal class ExternalLinkOpenerTest {
every { intent.data } returns Uri.parse("https://github.com/")
val lambdaSlot = slot<() -> Unit>()
val externalLinkOpener = ExternalLinkOpener(activity, sharedPreferenceUtil, alertDialogShower)
externalLinkOpener.openExternalUrl(intent)
externalLinkOpener.openExternalUrl(intent, R.string.no_browser_application_installed)
verify {
alertDialogShower.show(
KiwixDialog.ExternalLinkPopup,
@ -133,7 +133,7 @@ internal class ExternalLinkOpenerTest {
every { intent.resolveActivity(activity.packageManager) } returns mockk()
every { sharedPreferenceUtil.prefExternalLinkPopup } returns false
val externalLinkOpener = ExternalLinkOpener(activity, sharedPreferenceUtil, alertDialogShower)
externalLinkOpener.openExternalUrl(intent)
externalLinkOpener.openExternalUrl(intent, R.string.no_browser_application_installed)
verify { activity.startActivity(intent) }
}
@ -143,9 +143,19 @@ internal class ExternalLinkOpenerTest {
val externalLinkOpener = ExternalLinkOpener(activity, sharedPreferenceUtil, alertDialogShower)
mockkStatic(Toast::class)
justRun {
Toast.makeText(activity, R.string.no_reader_application_installed, Toast.LENGTH_LONG).show()
Toast.makeText(activity, R.string.no_browser_application_installed, Toast.LENGTH_LONG).show()
}
externalLinkOpener.openExternalUrl(intent)
verify { activity.toast(R.string.no_reader_application_installed) }
externalLinkOpener.openExternalUrl(intent, R.string.no_browser_application_installed)
verify { activity.toast(R.string.no_browser_application_installed) }
justRun {
Toast.makeText(activity, R.string.no_pdf_application_installed, Toast.LENGTH_LONG).show()
}
externalLinkOpener.openExternalUrl(intent, R.string.no_pdf_application_installed)
verify { activity.toast(R.string.no_pdf_application_installed) }
justRun {
Toast.makeText(activity, R.string.no_epub_application_installed, Toast.LENGTH_LONG).show()
}
externalLinkOpener.openExternalUrl(intent, R.string.no_epub_application_installed)
verify { activity.toast(R.string.no_epub_application_installed) }
}
}

View File

@ -221,7 +221,10 @@ abstract class CoreMainActivity : BaseActivity(), WebViewProvider {
}
private fun openSupportKiwixExternalLink() {
externalLinkOpener.openExternalUrl(KIWIX_SUPPORT_URL.toUri().browserIntent())
externalLinkOpener.openExternalUrl(
KIWIX_SUPPORT_URL.toUri().browserIntent(),
R.string.no_browser_application_installed
)
}
override fun onBackPressed() {

View File

@ -1382,8 +1382,8 @@ abstract class CoreReaderFragment :
sharedPreferenceUtil?.putPrefFullScreen(false)
}
override fun openExternalUrl(intent: Intent) {
externalLinkOpener?.openExternalUrl(intent)
override fun openExternalUrl(intent: Intent, errorMessageId: Int) {
externalLinkOpener?.openExternalUrl(intent, errorMessageId)
}
protected fun openZimFile(file: File, isCustomApp: Boolean = false) {

View File

@ -29,6 +29,7 @@ import android.webkit.WebView
import android.webkit.WebViewClient
import androidx.core.content.FileProvider
import org.kiwix.kiwixmobile.core.CoreApp.Companion.instance
import org.kiwix.kiwixmobile.core.R
import org.kiwix.kiwixmobile.core.reader.ZimFileReader
import org.kiwix.kiwixmobile.core.reader.ZimReaderContainer
import org.kiwix.kiwixmobile.core.utils.SharedPreferenceUtil
@ -70,7 +71,7 @@ open class CoreWebViewClient(
// Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
val intent = Intent(Intent.ACTION_VIEW, Uri.parse(url))
callback.openExternalUrl(intent)
callback.openExternalUrl(intent, R.string.no_browser_application_installed)
return true
}
@ -102,7 +103,11 @@ open class CoreWebViewClient(
flags = Intent.FLAG_ACTIVITY_NO_HISTORY
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
}
callback.openExternalUrl(intent)
val errorMessage = if (extension == "application/pdf")
R.string.no_pdf_application_installed
else
R.string.no_epub_application_installed
callback.openExternalUrl(intent, errorMessage)
}
}
return true

View File

@ -24,7 +24,7 @@ interface WebViewCallback {
fun webViewUrlLoading()
fun webViewUrlFinishedLoading()
fun webViewFailedLoading(failingUrl: String)
fun openExternalUrl(intent: Intent)
fun openExternalUrl(intent: Intent, errorMessageId: Int)
fun webViewProgressChanged(progress: Int, webView: WebView)
fun webViewTitleUpdated(title: String)
fun webViewPageChanged(page: Int, maxPages: Int)

View File

@ -21,7 +21,6 @@ package org.kiwix.kiwixmobile.core.utils
import android.app.Activity
import android.content.Intent
import android.speech.tts.TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA
import org.kiwix.kiwixmobile.core.R
import org.kiwix.kiwixmobile.core.extensions.toast
import org.kiwix.kiwixmobile.core.utils.dialog.AlertDialogShower
import org.kiwix.kiwixmobile.core.utils.dialog.KiwixDialog
@ -33,7 +32,7 @@ class ExternalLinkOpener @Inject constructor(
private val alertDialogShower: AlertDialogShower
) {
fun openExternalUrl(intent: Intent) {
fun openExternalUrl(intent: Intent, errorMessageId: Int) {
if (intent.resolveActivity(activity.packageManager) != null) {
// Show popup with warning that this url is external and could lead to additional costs
// or may event not work when the user is offline.
@ -43,7 +42,7 @@ class ExternalLinkOpener @Inject constructor(
openLink(intent)
}
} else {
activity.toast(R.string.no_reader_application_installed)
activity.toast(errorMessageId)
}
}

View File

@ -73,11 +73,11 @@ class AlertDialogShower @Inject constructor(private val activity: Activity) :
}
uri?.let {
/*
Check if it is valid url then show it to the user
otherwise don't show uri to the user as they are not directly openable in the external app.
Check if it is external browser url then show it to the user
otherwise don't show uri to the user as they are not directly openable in the external browser.
We place this condition to improve the user experience see https://github.com/kiwix/kiwix-android/pull/3455
*/
if (!"$it".startsWith("content://")) {
if ("$it".startsWith("http://") || "$it".startsWith("https://")) {
showUrlInDialog(this, it)
}
dialog.getView?.let { setView(it()) }

View File

@ -63,6 +63,9 @@
<string name="tts_lang_not_supported">The language of this page is not supported. The article may not be properly read.</string>
<string name="no_reader_application_installed">Could not find an installed application for this type of file</string>
<string name="no_email_application_installed">Please install an email service provider or email us at %1s</string>
<string name="no_browser_application_installed">Please install any web browser to open it e.g. chrome or firefox.</string>
<string name="no_pdf_application_installed">Please install any pdf reader application to open it.</string>
<string name="no_epub_application_installed">Please install any epub reader application to open it.</string>
<string name="no_section_info">No Content Headers Found</string>
<string name="request_storage">To access offline content we need access to your storage</string>
<string name="request_write_storage">To download zim files we need write access to your storage</string>