Trim long text before displaying if necessary.

Fixes #8759
This commit is contained in:
Greyson Parrelli 2019-04-12 17:49:22 -04:00
parent 9c40de5bf1
commit 8cf3ba424a
5 changed files with 33 additions and 7 deletions

View File

@ -61,6 +61,8 @@ public class ConversationListItem extends RelativeLayout
private final static Typeface BOLD_TYPEFACE = Typeface.create("sans-serif-medium", Typeface.NORMAL); private final static Typeface BOLD_TYPEFACE = Typeface.create("sans-serif-medium", Typeface.NORMAL);
private final static Typeface LIGHT_TYPEFACE = Typeface.create("sans-serif", Typeface.NORMAL); private final static Typeface LIGHT_TYPEFACE = Typeface.create("sans-serif", Typeface.NORMAL);
private static final int MAX_SNIPPET_LENGTH = 500;
private Set<Long> selectedThreads; private Set<Long> selectedThreads;
private Recipient recipient; private Recipient recipient;
private long threadId; private long threadId;
@ -156,7 +158,7 @@ public class ConversationListItem extends RelativeLayout
this.typingView.stopAnimation(); this.typingView.stopAnimation();
this.subjectView.setVisibility(VISIBLE); this.subjectView.setVisibility(VISIBLE);
this.subjectView.setText(thread.getDisplayBody(getContext())); this.subjectView.setText(getTrimmedSnippet(thread.getDisplayBody(getContext())));
this.subjectView.setTypeface(unreadCount == 0 ? LIGHT_TYPEFACE : BOLD_TYPEFACE); this.subjectView.setTypeface(unreadCount == 0 ? LIGHT_TYPEFACE : BOLD_TYPEFACE);
this.subjectView.setTextColor(unreadCount == 0 ? ThemeUtil.getThemedColor(getContext(), R.attr.conversation_list_item_subject_color) this.subjectView.setTextColor(unreadCount == 0 ? ThemeUtil.getThemedColor(getContext(), R.attr.conversation_list_item_subject_color)
: ThemeUtil.getThemedColor(getContext(), R.attr.conversation_list_item_unread_color)); : ThemeUtil.getThemedColor(getContext(), R.attr.conversation_list_item_unread_color));
@ -265,6 +267,11 @@ public class ConversationListItem extends RelativeLayout
return lastSeen; return lastSeen;
} }
private @NonNull CharSequence getTrimmedSnippet(@NonNull CharSequence snippet) {
return snippet.length() <= MAX_SNIPPET_LENGTH ? snippet
: snippet.subSequence(0, MAX_SNIPPET_LENGTH);
}
private void setThumbnailSnippet(ThreadRecord thread) { private void setThumbnailSnippet(ThreadRecord thread) {
if (thread.getSnippetUri() != null) { if (thread.getSnippetUri() != null) {
this.thumbnailView.setVisibility(View.VISIBLE); this.thumbnailView.setVisibility(View.VISIBLE);

View File

@ -42,6 +42,8 @@ public class LongMessageActivity extends PassphraseRequiredActionBarActivity imp
private static final String KEY_MESSAGE_ID = "message_id"; private static final String KEY_MESSAGE_ID = "message_id";
private static final String KEY_IS_MMS = "is_mms"; private static final String KEY_IS_MMS = "is_mms";
private static final int MAX_DISPLAY_LENGTH = 64 * 1024;
private final DynamicLanguage dynamicLanguage = new DynamicLanguage(); private final DynamicLanguage dynamicLanguage = new DynamicLanguage();
private final DynamicTheme dynamicTheme = new DynamicTheme(); private final DynamicTheme dynamicTheme = new DynamicTheme();
@ -150,14 +152,22 @@ public class LongMessageActivity extends PassphraseRequiredActionBarActivity imp
TextView text = bubble.findViewById(R.id.longmessage_text); TextView text = bubble.findViewById(R.id.longmessage_text);
ConversationItemFooter footer = bubble.findViewById(R.id.longmessage_footer); ConversationItemFooter footer = bubble.findViewById(R.id.longmessage_footer);
String trimmedBody = getTrimmedBody(message.get().getFullBody());
SpannableString styledBody = linkifyMessageBody(new SpannableString(trimmedBody));
bubble.setVisibility(View.VISIBLE); bubble.setVisibility(View.VISIBLE);
text.setText(linkifyMessageBody(new SpannableString(message.get().getFullBody()))); text.setText(styledBody);
text.setMovementMethod(LinkMovementMethod.getInstance()); text.setMovementMethod(LinkMovementMethod.getInstance());
text.setTextSize(TypedValue.COMPLEX_UNIT_SP, TextSecurePreferences.getMessageBodyTextSize(this)); text.setTextSize(TypedValue.COMPLEX_UNIT_SP, TextSecurePreferences.getMessageBodyTextSize(this));
footer.setMessageRecord(message.get().getMessageRecord(), dynamicLanguage.getCurrentLocale()); footer.setMessageRecord(message.get().getMessageRecord(), dynamicLanguage.getCurrentLocale());
}); });
} }
private String getTrimmedBody(@NonNull String text) {
return text.length() <= MAX_DISPLAY_LENGTH ? text
: text.substring(0, MAX_DISPLAY_LENGTH);
}
private SpannableString linkifyMessageBody(SpannableString messageBody) { private SpannableString linkifyMessageBody(SpannableString messageBody) {
int linkPattern = Linkify.WEB_URLS | Linkify.EMAIL_ADDRESSES | Linkify.PHONE_NUMBERS; int linkPattern = Linkify.WEB_URLS | Linkify.EMAIL_ADDRESSES | Linkify.PHONE_NUMBERS;
boolean hasLinks = Linkify.addLinks(messageBody, linkPattern); boolean hasLinks = Linkify.addLinks(messageBody, linkPattern);

View File

@ -22,6 +22,8 @@ public abstract class AbstractNotificationBuilder extends NotificationCompat.Bui
@SuppressWarnings("unused") @SuppressWarnings("unused")
private static final String TAG = AbstractNotificationBuilder.class.getSimpleName(); private static final String TAG = AbstractNotificationBuilder.class.getSimpleName();
private static final int MAX_DISPLAY_LENGTH = 500;
protected Context context; protected Context context;
protected NotificationPrivacyPreference privacy; protected NotificationPrivacyPreference privacy;
@ -74,7 +76,7 @@ public abstract class AbstractNotificationBuilder extends NotificationCompat.Bui
public void setTicker(@NonNull Recipient recipient, @Nullable CharSequence message) { public void setTicker(@NonNull Recipient recipient, @Nullable CharSequence message) {
if (privacy.isDisplayMessage()) { if (privacy.isDisplayMessage()) {
setTicker(getStyledMessage(recipient, message)); setTicker(getStyledMessage(recipient, trimToDisplayLength(message)));
} else if (privacy.isDisplayContact()) { } else if (privacy.isDisplayContact()) {
setTicker(getStyledMessage(recipient, context.getString(R.string.AbstractNotificationBuilder_new_message))); setTicker(getStyledMessage(recipient, context.getString(R.string.AbstractNotificationBuilder_new_message)));
} else { } else {
@ -88,4 +90,11 @@ public abstract class AbstractNotificationBuilder extends NotificationCompat.Bui
return blinkPattern.split(","); return blinkPattern.split(",");
} }
protected @NonNull CharSequence trimToDisplayLength(@Nullable CharSequence text) {
text = text == null ? "" : text;
return text.length() <= MAX_DISPLAY_LENGTH ? text
: text.subSequence(0, MAX_DISPLAY_LENGTH);
}
} }

View File

@ -81,7 +81,7 @@ public class MultipleRecipientNotificationBuilder extends AbstractNotificationBu
NotificationCompat.InboxStyle style = new NotificationCompat.InboxStyle(); NotificationCompat.InboxStyle style = new NotificationCompat.InboxStyle();
for (CharSequence body : messageBodies) { for (CharSequence body : messageBodies) {
style.addLine(body); style.addLine(trimToDisplayLength(body));
} }
setStyle(style); setStyle(style);

View File

@ -257,15 +257,15 @@ public class SingleRecipientNotificationBuilder extends AbstractNotificationBuil
} }
public NotificationCompat.Builder setContentText(CharSequence contentText) { public NotificationCompat.Builder setContentText(CharSequence contentText) {
this.contentText = contentText; this.contentText = trimToDisplayLength(contentText);
return super.setContentText(contentText); return super.setContentText(this.contentText);
} }
private CharSequence getBigText(List<CharSequence> messageBodies) { private CharSequence getBigText(List<CharSequence> messageBodies) {
SpannableStringBuilder content = new SpannableStringBuilder(); SpannableStringBuilder content = new SpannableStringBuilder();
for (int i = 0; i < messageBodies.size(); i++) { for (int i = 0; i < messageBodies.size(); i++) {
content.append(messageBodies.get(i)); content.append(trimToDisplayLength(messageBodies.get(i)));
if (i < messageBodies.size() - 1) { if (i < messageBodies.size() - 1) {
content.append('\n'); content.append('\n');
} }