Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

On android 6.0, filter 0x00ad (unicode soft hyphen) from statuses #884

Merged
merged 1 commit into from
Oct 15, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@

package com.keylesspalace.tusky.viewdata;

import android.os.Build;
import android.support.annotation.Nullable;
import android.text.SpannableStringBuilder;
import android.text.Spanned;

import com.keylesspalace.tusky.entity.Attachment;
Expand Down Expand Up @@ -47,6 +49,9 @@ private StatusViewData() {
public abstract boolean deepEquals(StatusViewData other);

public static final class Concrete extends StatusViewData {
private static final char SOFT_HYPHEN = '\u00ad';
private static final char ASCII_HYPHEN = '-';

private final String id;
private final Spanned content;
private final boolean reblogged;
Expand Down Expand Up @@ -92,10 +97,18 @@ public Concrete(String id, Spanned content, boolean reblogged, boolean favourite
Status.Application application, List<Emoji> statusEmojis, List<Emoji> accountEmojis, @Nullable Card card,
boolean isCollapsible, boolean isCollapsed) {
this.id = id;
this.content = content;
if (Build.VERSION.SDK_INT == 23) {
// https://github.com/tuskyapp/Tusky/issues/563
this.content = replaceCrashingCharacters(content);
this.spoilerText = spoilerText == null ? null : replaceCrashingCharacters(spoilerText).toString();
this.nickname = replaceCrashingCharacters(nickname).toString();
} else {
this.content = content;
this.spoilerText = spoilerText;
this.nickname = nickname;
}
this.reblogged = reblogged;
this.favourited = favourited;
this.spoilerText = spoilerText;
this.visibility = visibility;
this.attachments = attachments;
this.rebloggedByUsername = rebloggedByUsername;
Expand All @@ -104,7 +117,6 @@ public Concrete(String id, Spanned content, boolean reblogged, boolean favourite
this.isExpanded = isExpanded;
this.isShowingContent = isShowingContent;
this.userFullName = userFullName;
this.nickname = nickname;
this.avatar = avatar;
this.createdAt = createdAt;
this.reblogsCount = reblogsCount;
Expand Down Expand Up @@ -288,6 +300,33 @@ public boolean deepEquals(StatusViewData o) {
Objects.equals(card, concrete.card)
&& isCollapsed == concrete.isCollapsed;
}

static Spanned replaceCrashingCharacters(Spanned content) {
return (Spanned) replaceCrashingCharacters((CharSequence) content);
}

static CharSequence replaceCrashingCharacters(CharSequence content) {
Boolean replacing = false;
SpannableStringBuilder builder = null;
int length = content.length();

for (int index = 0; index < length; ++index) {
char character = content.charAt(index);

// If there are more than one or two, switch to a map
if (character == SOFT_HYPHEN) {
if (!replacing) {
replacing = true;
builder = new SpannableStringBuilder(content, 0, index);
}
builder.append(ASCII_HYPHEN);
} else if (replacing) {
builder.append(character);
}
}

return replacing ? builder : content;
}
}

public static final class Placeholder extends StatusViewData {
Expand Down