From 59b4805b8b5420adc64e23c49e381598226022cb Mon Sep 17 00:00:00 2001 From: alansley Date: Thu, 22 Aug 2024 09:51:01 +1000 Subject: [PATCH] SS-75 Prevent 'Are you sure you want to open this URL?' dialog from being excessively tall when given a very long URL --- .../securesms/SessionDialogBuilder.kt | 25 ++++++++++++++++++- .../preferences/BlockedContactsActivity.kt | 2 +- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/org/thoughtcrime/securesms/SessionDialogBuilder.kt b/app/src/main/java/org/thoughtcrime/securesms/SessionDialogBuilder.kt index 13de1a0750..d6d1e2d249 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/SessionDialogBuilder.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/SessionDialogBuilder.kt @@ -56,7 +56,27 @@ class SessionDialogBuilder(val context: Context) { .apply { orientation = VERTICAL } .also(dialogBuilder::setCustomTitle) - val contentView = LinearLayout(context).apply { orientation = VERTICAL } + // Maximum height of the dialog. This comes into play in the Open URL dialog where we + // might have a a very, very long URL (which we provide a ScrollView for) - but without + // limiting the height of the dialog, the WRAP_CONTENT part can allow the dialog to be + // super tall! + // Note: I eye-balled this DP value for the max dialog height to make it match the + // height of regular not-scrollable dialogs. + val maxHeightInDp = 137 + val scale = context.resources.displayMetrics.density + val maxHeightInPx = (maxHeightInDp * scale + 0.5f).toInt() + + val contentView = LinearLayout(context).apply { + orientation = VERTICAL + + // Limit the max height of the dialog to the above value + viewTreeObserver.addOnGlobalLayoutListener { + if (height > maxHeightInPx) { + layoutParams.height = maxHeightInPx + requestLayout() + } + } + } private val buttonLayout = LinearLayout(context) @@ -180,7 +200,10 @@ public fun Context.copyURLToClipboard(url: String) { // Method to show a dialog used to open or copy a URL fun Context.showOpenUrlDialog(url: String, showCloseButton: Boolean = true): AlertDialog { + return SessionDialogBuilder(this).apply { + + // If we're not showing a close button we can just use a simple title.. if (!showCloseButton) { title(R.string.urlOpen) diff --git a/app/src/main/java/org/thoughtcrime/securesms/preferences/BlockedContactsActivity.kt b/app/src/main/java/org/thoughtcrime/securesms/preferences/BlockedContactsActivity.kt index e49f3fb85e..f2b394fdae 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/preferences/BlockedContactsActivity.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/preferences/BlockedContactsActivity.kt @@ -32,7 +32,7 @@ class BlockedContactsActivity: PassphraseRequiredActionBarActivity() { val delayStepMilliseconds = when (toastLengthSetting) { Toast.LENGTH_SHORT -> 2000L - Toast.LENGTH_LONG -> 3500L + Toast.LENGTH_LONG -> 3500L else -> { Log.w("BlockContactsActivity", "Invalid toast length setting - using Toast.LENGTH_SHORT") 2000L