Spotbug: don't access Array with constant index

Signed-off-by: Andy Scherzinger <info@andy-scherzinger.de>
This commit is contained in:
Andy Scherzinger 2022-12-28 11:50:51 +01:00
parent ff3dffd051
commit 946ec09315
No known key found for this signature in database
GPG Key ID: 6CADC7E3523C308B
3 changed files with 34 additions and 19 deletions

View File

@ -60,12 +60,10 @@ public class MentionAutocompleteCallback implements AutocompleteCallback<Mention
@OptIn(markerClass = kotlin.ExperimentalStdlibApi.class) @OptIn(markerClass = kotlin.ExperimentalStdlibApi.class)
@Override @Override
public boolean onPopupItemClicked(Editable editable, Mention item) { public boolean onPopupItemClicked(Editable editable, Mention item) {
int[] range = MagicCharPolicy.getQueryRange(editable); MagicCharPolicy.TextSpan range = MagicCharPolicy.getQueryRange(editable);
if (range == null) { if (range == null) {
return false; return false;
} }
int start = range[0];
int end = range[1];
String replacement = item.getLabel(); String replacement = item.getLabel();
StringBuilder replacementStringBuilder = new StringBuilder(item.getLabel()); StringBuilder replacementStringBuilder = new StringBuilder(item.getLabel());
@ -73,7 +71,7 @@ public class MentionAutocompleteCallback implements AutocompleteCallback<Mention
replacementStringBuilder.delete(emojiRange.range.getStart(), emojiRange.range.getEndInclusive()); replacementStringBuilder.delete(emojiRange.range.getStart(), emojiRange.range.getEndInclusive());
} }
editable.replace(start, end, replacementStringBuilder + " "); editable.replace(range.getStart(), range.getEnd(), replacementStringBuilder + " ");
Spans.MentionChipSpan mentionChipSpan = Spans.MentionChipSpan mentionChipSpan =
new Spans.MentionChipSpan(DisplayUtils.getDrawableForMentionChipSpan(context, new Spans.MentionChipSpan(DisplayUtils.getDrawableForMentionChipSpan(context,
item.getId(), item.getId(),
@ -85,7 +83,9 @@ public class MentionAutocompleteCallback implements AutocompleteCallback<Mention
viewThemeUtils), viewThemeUtils),
BetterImageSpan.ALIGN_CENTER, BetterImageSpan.ALIGN_CENTER,
item.getId(), item.getLabel()); item.getId(), item.getLabel());
editable.setSpan(mentionChipSpan, start, start + replacementStringBuilder.toString().length(), editable.setSpan(mentionChipSpan,
range.getStart(),
range.getStart() + replacementStringBuilder.toString().length(),
Spanned.SPAN_INCLUSIVE_INCLUSIVE); Spanned.SPAN_INCLUSIVE_INCLUSIVE);

View File

@ -380,7 +380,7 @@ public class DisplayUtils {
* @param color the color * @param color the color
* @return true if primaryColor is lighter than MAX_LIGHTNESS * @return true if primaryColor is lighter than MAX_LIGHTNESS
*/ */
@SuppressWarnings("correctness") @SuppressWarnings("CLI_CONSTANT_LIST_INDEX")
public static boolean lightTheme(int color) { public static boolean lightTheme(int color) {
float[] hsl = colorToHSL(color); float[] hsl = colorToHSL(color);

View File

@ -37,31 +37,28 @@ public class MagicCharPolicy implements AutocompletePolicy {
} }
@Nullable @Nullable
public static int[] getQueryRange(Spannable text) { public static TextSpan getQueryRange(Spannable text) {
QuerySpan[] span = text.getSpans(0, text.length(), QuerySpan.class); QuerySpan[] span = text.getSpans(0, text.length(), QuerySpan.class);
if (span == null || span.length == 0) return null; if (span == null || span.length == 0) {
if (span.length > 1) { return null;
// Do absolutely nothing } else {
}
QuerySpan sp = span[0]; QuerySpan sp = span[0];
return new int[]{text.getSpanStart(sp), text.getSpanEnd(sp)}; return new TextSpan(text.getSpanStart(sp), text.getSpanEnd(sp));
}
} }
private int[] checkText(Spannable text, int cursorPos) { private TextSpan checkText(Spannable text, int cursorPos) {
if (text.length() == 0) { if (text.length() == 0) {
return null; return null;
} }
int[] span = new int[2];
Pattern pattern = Pattern.compile("@+\\S*", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE); Pattern pattern = Pattern.compile("@+\\S*", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);
Matcher matcher = pattern.matcher(text); Matcher matcher = pattern.matcher(text);
while (matcher.find()) { while (matcher.find()) {
if (cursorPos >= matcher.start() && cursorPos <= matcher.end()) { if (cursorPos >= matcher.start() && cursorPos <= matcher.end()) {
span[0] = matcher.start();
span[1] = matcher.end();
if (text.subSequence(matcher.start(), matcher.end()).charAt(0) == character) { if (text.subSequence(matcher.start(), matcher.end()).charAt(0) == character) {
return span; return new TextSpan(matcher.start(), matcher.end());
} }
} }
} }
@ -69,11 +66,29 @@ public class MagicCharPolicy implements AutocompletePolicy {
return null; return null;
} }
public static class TextSpan {
int start;
int end;
public TextSpan(int start, int end) {
this.start = start;
this.end = end;
}
public int getStart() {
return start;
}
public int getEnd() {
return end;
}
}
@Override @Override
public boolean shouldShowPopup(Spannable text, int cursorPos) { public boolean shouldShowPopup(Spannable text, int cursorPos) {
int[] show = checkText(text, cursorPos); TextSpan show = checkText(text, cursorPos);
if (show != null) { if (show != null) {
text.setSpan(new QuerySpan(), show[0], show[1], Spanned.SPAN_INCLUSIVE_INCLUSIVE); text.setSpan(new QuerySpan(), show.getStart(), show.getEnd(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
return true; return true;
} }
return false; return false;