Browse Source

Spotbug: don't access Array with constant index

Signed-off-by: Andy Scherzinger <info@andy-scherzinger.de>
Andy Scherzinger 2 years ago
parent
commit
946ec09315

+ 5 - 5
app/src/main/java/com/nextcloud/talk/callbacks/MentionAutocompleteCallback.java

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

+ 1 - 1
app/src/main/java/com/nextcloud/talk/utils/DisplayUtils.java

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

+ 28 - 13
app/src/main/java/com/nextcloud/talk/utils/MagicCharPolicy.java

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