From dd02af9911cf451da512955b5ab361e8c4593565 Mon Sep 17 00:00:00 2001 From: Konrad Pozniak Date: Sun, 7 Apr 2019 16:32:58 +0200 Subject: [PATCH] fix IndexOutOfBoundException caused by ListStatusAccessibilityDelegate (#1178) --- .../tusky/fragment/NotificationsFragment.java | 2 +- .../keylesspalace/tusky/fragment/TimelineFragment.java | 2 +- .../tusky/fragment/ViewThreadFragment.java | 2 +- .../java/com/keylesspalace/tusky/util/PairedList.java | 10 ++++++++++ 4 files changed, 13 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/com/keylesspalace/tusky/fragment/NotificationsFragment.java b/app/src/main/java/com/keylesspalace/tusky/fragment/NotificationsFragment.java index d893267df..8f046b804 100644 --- a/app/src/main/java/com/keylesspalace/tusky/fragment/NotificationsFragment.java +++ b/app/src/main/java/com/keylesspalace/tusky/fragment/NotificationsFragment.java @@ -191,7 +191,7 @@ public class NotificationsFragment extends SFragment implements recyclerView.setLayoutManager(layoutManager); recyclerView.setAccessibilityDelegateCompat( new ListStatusAccessibilityDelegate(recyclerView, this, (pos) -> { - NotificationViewData notification = notifications.getPairedItem(pos); + NotificationViewData notification = notifications.getPairedItemOrNull(pos); // We support replies only for now if (notification instanceof NotificationViewData.Concrete) { return ((NotificationViewData.Concrete) notification).getStatusViewData(); diff --git a/app/src/main/java/com/keylesspalace/tusky/fragment/TimelineFragment.java b/app/src/main/java/com/keylesspalace/tusky/fragment/TimelineFragment.java index 8e3ee76fe..ab0109cce 100644 --- a/app/src/main/java/com/keylesspalace/tusky/fragment/TimelineFragment.java +++ b/app/src/main/java/com/keylesspalace/tusky/fragment/TimelineFragment.java @@ -397,7 +397,7 @@ public class TimelineFragment extends SFragment implements private void setupRecyclerView() { recyclerView.setAccessibilityDelegateCompat( - new ListStatusAccessibilityDelegate(recyclerView, this, statuses::getPairedItem)); + new ListStatusAccessibilityDelegate(recyclerView, this, statuses::getPairedItemOrNull)); Context context = recyclerView.getContext(); recyclerView.setHasFixedSize(true); layoutManager = new LinearLayoutManager(context); diff --git a/app/src/main/java/com/keylesspalace/tusky/fragment/ViewThreadFragment.java b/app/src/main/java/com/keylesspalace/tusky/fragment/ViewThreadFragment.java index 313a11ae5..e3845e4dd 100644 --- a/app/src/main/java/com/keylesspalace/tusky/fragment/ViewThreadFragment.java +++ b/app/src/main/java/com/keylesspalace/tusky/fragment/ViewThreadFragment.java @@ -141,7 +141,7 @@ public final class ViewThreadFragment extends SFragment implements LinearLayoutManager layoutManager = new LinearLayoutManager(context); recyclerView.setLayoutManager(layoutManager); recyclerView.setAccessibilityDelegateCompat( - new ListStatusAccessibilityDelegate(recyclerView, this, statuses::getPairedItem)); + new ListStatusAccessibilityDelegate(recyclerView, this, statuses::getPairedItemOrNull)); DividerItemDecoration divider = new DividerItemDecoration( context, layoutManager.getOrientation()); recyclerView.addItemDecoration(divider); diff --git a/app/src/main/java/com/keylesspalace/tusky/util/PairedList.java b/app/src/main/java/com/keylesspalace/tusky/util/PairedList.java index 7bf6c63bf..a0880a519 100644 --- a/app/src/main/java/com/keylesspalace/tusky/util/PairedList.java +++ b/app/src/main/java/com/keylesspalace/tusky/util/PairedList.java @@ -1,5 +1,6 @@ package com.keylesspalace.tusky.util; +import androidx.annotation.Nullable; import androidx.arch.core.util.Function; import java.util.AbstractList; @@ -44,6 +45,15 @@ public final class PairedList extends AbstractList { return synced.get(index); } + @Nullable + public V getPairedItemOrNull(int index) { + if (index >= 0 && index < synced.size()) { + return synced.get(index); + } else { + return null; + } + } + public void setPairedItem(int index, V element) { synced.set(index, element); }