mirror of
https://github.com/kiwix/kiwix-android.git
synced 2025-09-08 06:42:21 -04:00
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:
parent
bb5e4d34c1
commit
c48eabc261
@ -51,7 +51,7 @@ internal class ExternalLinkOpenerTest {
|
|||||||
every { intent.data } returns Uri.parse(url.toString())
|
every { intent.data } returns Uri.parse(url.toString())
|
||||||
val lambdaSlot = slot<() -> Unit>()
|
val lambdaSlot = slot<() -> Unit>()
|
||||||
val externalLinkOpener = ExternalLinkOpener(activity, sharedPreferenceUtil, alertDialogShower)
|
val externalLinkOpener = ExternalLinkOpener(activity, sharedPreferenceUtil, alertDialogShower)
|
||||||
externalLinkOpener.openExternalUrl(intent)
|
externalLinkOpener.openExternalUrl(intent, R.string.no_browser_application_installed)
|
||||||
verify {
|
verify {
|
||||||
alertDialogShower.show(
|
alertDialogShower.show(
|
||||||
KiwixDialog.ExternalLinkPopup,
|
KiwixDialog.ExternalLinkPopup,
|
||||||
@ -72,7 +72,7 @@ internal class ExternalLinkOpenerTest {
|
|||||||
every { intent.data } returns uri
|
every { intent.data } returns uri
|
||||||
val lambdaSlot = slot<() -> Unit>()
|
val lambdaSlot = slot<() -> Unit>()
|
||||||
val externalLinkOpener = ExternalLinkOpener(activity, sharedPreferenceUtil, alertDialogShower)
|
val externalLinkOpener = ExternalLinkOpener(activity, sharedPreferenceUtil, alertDialogShower)
|
||||||
externalLinkOpener.openExternalUrl(intent)
|
externalLinkOpener.openExternalUrl(intent, R.string.no_browser_application_installed)
|
||||||
verify {
|
verify {
|
||||||
alertDialogShower.show(
|
alertDialogShower.show(
|
||||||
KiwixDialog.ExternalLinkPopup,
|
KiwixDialog.ExternalLinkPopup,
|
||||||
@ -92,7 +92,7 @@ internal class ExternalLinkOpenerTest {
|
|||||||
every { intent.data } returns Uri.parse("https://github.com/")
|
every { intent.data } returns Uri.parse("https://github.com/")
|
||||||
val lambdaSlot = slot<() -> Unit>()
|
val lambdaSlot = slot<() -> Unit>()
|
||||||
val externalLinkOpener = ExternalLinkOpener(activity, sharedPreferenceUtil, alertDialogShower)
|
val externalLinkOpener = ExternalLinkOpener(activity, sharedPreferenceUtil, alertDialogShower)
|
||||||
externalLinkOpener.openExternalUrl(intent)
|
externalLinkOpener.openExternalUrl(intent, R.string.no_browser_application_installed)
|
||||||
verify {
|
verify {
|
||||||
alertDialogShower.show(
|
alertDialogShower.show(
|
||||||
KiwixDialog.ExternalLinkPopup,
|
KiwixDialog.ExternalLinkPopup,
|
||||||
@ -112,7 +112,7 @@ internal class ExternalLinkOpenerTest {
|
|||||||
every { intent.data } returns Uri.parse("https://github.com/")
|
every { intent.data } returns Uri.parse("https://github.com/")
|
||||||
val lambdaSlot = slot<() -> Unit>()
|
val lambdaSlot = slot<() -> Unit>()
|
||||||
val externalLinkOpener = ExternalLinkOpener(activity, sharedPreferenceUtil, alertDialogShower)
|
val externalLinkOpener = ExternalLinkOpener(activity, sharedPreferenceUtil, alertDialogShower)
|
||||||
externalLinkOpener.openExternalUrl(intent)
|
externalLinkOpener.openExternalUrl(intent, R.string.no_browser_application_installed)
|
||||||
verify {
|
verify {
|
||||||
alertDialogShower.show(
|
alertDialogShower.show(
|
||||||
KiwixDialog.ExternalLinkPopup,
|
KiwixDialog.ExternalLinkPopup,
|
||||||
@ -133,7 +133,7 @@ internal class ExternalLinkOpenerTest {
|
|||||||
every { intent.resolveActivity(activity.packageManager) } returns mockk()
|
every { intent.resolveActivity(activity.packageManager) } returns mockk()
|
||||||
every { sharedPreferenceUtil.prefExternalLinkPopup } returns false
|
every { sharedPreferenceUtil.prefExternalLinkPopup } returns false
|
||||||
val externalLinkOpener = ExternalLinkOpener(activity, sharedPreferenceUtil, alertDialogShower)
|
val externalLinkOpener = ExternalLinkOpener(activity, sharedPreferenceUtil, alertDialogShower)
|
||||||
externalLinkOpener.openExternalUrl(intent)
|
externalLinkOpener.openExternalUrl(intent, R.string.no_browser_application_installed)
|
||||||
verify { activity.startActivity(intent) }
|
verify { activity.startActivity(intent) }
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -143,9 +143,19 @@ internal class ExternalLinkOpenerTest {
|
|||||||
val externalLinkOpener = ExternalLinkOpener(activity, sharedPreferenceUtil, alertDialogShower)
|
val externalLinkOpener = ExternalLinkOpener(activity, sharedPreferenceUtil, alertDialogShower)
|
||||||
mockkStatic(Toast::class)
|
mockkStatic(Toast::class)
|
||||||
justRun {
|
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)
|
externalLinkOpener.openExternalUrl(intent, R.string.no_browser_application_installed)
|
||||||
verify { activity.toast(R.string.no_reader_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) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -221,7 +221,10 @@ abstract class CoreMainActivity : BaseActivity(), WebViewProvider {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun openSupportKiwixExternalLink() {
|
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() {
|
override fun onBackPressed() {
|
||||||
|
@ -1382,8 +1382,8 @@ abstract class CoreReaderFragment :
|
|||||||
sharedPreferenceUtil?.putPrefFullScreen(false)
|
sharedPreferenceUtil?.putPrefFullScreen(false)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun openExternalUrl(intent: Intent) {
|
override fun openExternalUrl(intent: Intent, errorMessageId: Int) {
|
||||||
externalLinkOpener?.openExternalUrl(intent)
|
externalLinkOpener?.openExternalUrl(intent, errorMessageId)
|
||||||
}
|
}
|
||||||
|
|
||||||
protected fun openZimFile(file: File, isCustomApp: Boolean = false) {
|
protected fun openZimFile(file: File, isCustomApp: Boolean = false) {
|
||||||
|
@ -29,6 +29,7 @@ import android.webkit.WebView
|
|||||||
import android.webkit.WebViewClient
|
import android.webkit.WebViewClient
|
||||||
import androidx.core.content.FileProvider
|
import androidx.core.content.FileProvider
|
||||||
import org.kiwix.kiwixmobile.core.CoreApp.Companion.instance
|
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.ZimFileReader
|
||||||
import org.kiwix.kiwixmobile.core.reader.ZimReaderContainer
|
import org.kiwix.kiwixmobile.core.reader.ZimReaderContainer
|
||||||
import org.kiwix.kiwixmobile.core.utils.SharedPreferenceUtil
|
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
|
// 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))
|
val intent = Intent(Intent.ACTION_VIEW, Uri.parse(url))
|
||||||
callback.openExternalUrl(intent)
|
callback.openExternalUrl(intent, R.string.no_browser_application_installed)
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -102,7 +103,11 @@ open class CoreWebViewClient(
|
|||||||
flags = Intent.FLAG_ACTIVITY_NO_HISTORY
|
flags = Intent.FLAG_ACTIVITY_NO_HISTORY
|
||||||
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
|
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
|
return true
|
||||||
|
@ -24,7 +24,7 @@ interface WebViewCallback {
|
|||||||
fun webViewUrlLoading()
|
fun webViewUrlLoading()
|
||||||
fun webViewUrlFinishedLoading()
|
fun webViewUrlFinishedLoading()
|
||||||
fun webViewFailedLoading(failingUrl: String)
|
fun webViewFailedLoading(failingUrl: String)
|
||||||
fun openExternalUrl(intent: Intent)
|
fun openExternalUrl(intent: Intent, errorMessageId: Int)
|
||||||
fun webViewProgressChanged(progress: Int, webView: WebView)
|
fun webViewProgressChanged(progress: Int, webView: WebView)
|
||||||
fun webViewTitleUpdated(title: String)
|
fun webViewTitleUpdated(title: String)
|
||||||
fun webViewPageChanged(page: Int, maxPages: Int)
|
fun webViewPageChanged(page: Int, maxPages: Int)
|
||||||
|
@ -21,7 +21,6 @@ package org.kiwix.kiwixmobile.core.utils
|
|||||||
import android.app.Activity
|
import android.app.Activity
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import android.speech.tts.TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA
|
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.extensions.toast
|
||||||
import org.kiwix.kiwixmobile.core.utils.dialog.AlertDialogShower
|
import org.kiwix.kiwixmobile.core.utils.dialog.AlertDialogShower
|
||||||
import org.kiwix.kiwixmobile.core.utils.dialog.KiwixDialog
|
import org.kiwix.kiwixmobile.core.utils.dialog.KiwixDialog
|
||||||
@ -33,7 +32,7 @@ class ExternalLinkOpener @Inject constructor(
|
|||||||
private val alertDialogShower: AlertDialogShower
|
private val alertDialogShower: AlertDialogShower
|
||||||
) {
|
) {
|
||||||
|
|
||||||
fun openExternalUrl(intent: Intent) {
|
fun openExternalUrl(intent: Intent, errorMessageId: Int) {
|
||||||
if (intent.resolveActivity(activity.packageManager) != null) {
|
if (intent.resolveActivity(activity.packageManager) != null) {
|
||||||
// Show popup with warning that this url is external and could lead to additional costs
|
// 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.
|
// or may event not work when the user is offline.
|
||||||
@ -43,7 +42,7 @@ class ExternalLinkOpener @Inject constructor(
|
|||||||
openLink(intent)
|
openLink(intent)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
activity.toast(R.string.no_reader_application_installed)
|
activity.toast(errorMessageId)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -73,11 +73,11 @@ class AlertDialogShower @Inject constructor(private val activity: Activity) :
|
|||||||
}
|
}
|
||||||
uri?.let {
|
uri?.let {
|
||||||
/*
|
/*
|
||||||
Check if it is valid url then show it to the user
|
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 app.
|
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
|
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)
|
showUrlInDialog(this, it)
|
||||||
}
|
}
|
||||||
dialog.getView?.let { setView(it()) }
|
dialog.getView?.let { setView(it()) }
|
||||||
|
@ -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="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_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_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="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_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>
|
<string name="request_write_storage">To download zim files we need write access to your storage</string>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user