From 4eea995c898974ba28ce98682756a09b4a18d5c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Mart=C3=AD?= Date: Fri, 18 Sep 2015 18:36:39 -0700 Subject: [PATCH] Trim newlines from the description in a safer way This avoids problems in a few corner cases: * The description is empty (it would probably throw an exception) * The description doens't end in newlines (as seen in #422) For the general case that was already working, the result is the same. --- F-Droid/src/org/fdroid/fdroid/AppDetails.java | 24 ++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/F-Droid/src/org/fdroid/fdroid/AppDetails.java b/F-Droid/src/org/fdroid/fdroid/AppDetails.java index 8b36bfcfb..98d7e611f 100644 --- a/F-Droid/src/org/fdroid/fdroid/AppDetails.java +++ b/F-Droid/src/org/fdroid/fdroid/AppDetails.java @@ -1089,12 +1089,34 @@ public class AppDetails extends AppCompatActivity implements ProgressListener, A updateViews(getView()); } + // The HTML formatter adds "\n\n" at the end of every paragraph. This + // is desired between paragraphs, but not at the end of the whole + // string as it adds unwanted spacing at the end of the TextView. + // Remove all trailing newlines. + // Use this function instead of a trim() as that would require + // converting to String and thus losing formatting (e.g. bold). + private static CharSequence trimNewlines(CharSequence s) { + if (s == null || s.length() < 1) { + return s; + } + int i; + for (i = s.length()-1; i >= 0; i--) { + if (s.charAt(i) != '\n') { + break; + } + } + if (i == s.length()-1) { + return s; + } + return s.subSequence(0, i); + } + private void setupView(final View view) { // Expandable description final TextView description = (TextView) view.findViewById(R.id.description); final Spanned desc = Html.fromHtml(getApp().description, null, new Utils.HtmlTagHandler()); description.setMovementMethod(SafeLinkMovementMethod.getInstance(getActivity())); - description.setText(desc.subSequence(0, desc.length() - 2)); + description.setText(trimNewlines(desc)); final ImageView view_more_description = (ImageView) view.findViewById(R.id.view_more_description); description.post(new Runnable() { @Override