From bb5e4d34c124c8b8e82c74b79d185e44d9de88da Mon Sep 17 00:00:00 2001 From: MohitMali Date: Thu, 20 Jul 2023 12:41:34 +0530 Subject: [PATCH] Hide content uri in external link popup. * It is unnecessary to show the epub and pdf URIs in the popup, as they are not URLs. If the user copies and pastes these links into an external browser, they will not open. Hence, displaying these links in the popup for epub and pdf files is not ideal. * Both URIs start with `content://` prefix so we have placed a check for these type of uris in our `AlertDialogShower.kt` file if the uri is content type then it will not display this uri as link to the user. --- .../core/utils/dialog/AlertDialogShower.kt | 59 +++++++++++-------- 1 file changed, 35 insertions(+), 24 deletions(-) diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/utils/dialog/AlertDialogShower.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/utils/dialog/AlertDialogShower.kt index 72e58c2b2..55fa7433e 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/utils/dialog/AlertDialogShower.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/utils/dialog/AlertDialogShower.kt @@ -72,36 +72,47 @@ class AlertDialogShower @Inject constructor(private val activity: Activity) : } } uri?.let { - val frameLayout = FrameLayout(activity.baseContext) - - val textView = TextView(activity.baseContext).apply { - layoutParams = getFrameLayoutParams() - gravity = Gravity.CENTER - setLinkTextColor(activity.getAttribute(R.attr.colorPrimary)) - setOnLongClickListener { - val clipboard = - ContextCompat.getSystemService(activity.baseContext, ClipboardManager::class.java) - val clip = ClipData.newPlainText("External Url", "$uri") - clipboard?.setPrimaryClip(clip) - Toast.makeText( - activity.baseContext, - R.string.external_link_copied_message, - Toast.LENGTH_SHORT - ).show() - true - } - @SuppressLint("SetTextI18n") - text = "
$uri".fromHtml() + /* + 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. + We place this condition to improve the user experience see https://github.com/kiwix/kiwix-android/pull/3455 + */ + if (!"$it".startsWith("content://")) { + showUrlInDialog(this, it) } - frameLayout.addView(textView) - setView(frameLayout) + dialog.getView?.let { setView(it()) } + setCancelable(dialog.cancelable) } - dialog.getView?.let { setView(it()) } - setCancelable(dialog.cancelable) } .create() } + private fun showUrlInDialog(builder: AlertDialog.Builder, uri: Uri) { + val frameLayout = FrameLayout(activity.baseContext) + + val textView = TextView(activity.baseContext).apply { + layoutParams = getFrameLayoutParams() + gravity = Gravity.CENTER + setLinkTextColor(activity.getAttribute(R.attr.colorPrimary)) + setOnLongClickListener { + val clipboard = + ContextCompat.getSystemService(activity.baseContext, ClipboardManager::class.java) + val clip = ClipData.newPlainText("External Url", "$uri") + clipboard?.setPrimaryClip(clip) + Toast.makeText( + activity.baseContext, + R.string.external_link_copied_message, + Toast.LENGTH_SHORT + ).show() + true + } + @SuppressLint("SetTextI18n") + text = "
$uri".fromHtml() + } + frameLayout.addView(textView) + builder.setView(frameLayout) + } + private fun getFrameLayoutParams() = FrameLayout.LayoutParams( LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT