From f8b0ed7f18cee839885e2d4b9f9bfde17817072b Mon Sep 17 00:00:00 2001 From: Thomas Date: Tue, 31 Jan 2023 15:00:40 +0100 Subject: [PATCH] Fix some crashes --- .../mastodon/ui/drawer/StatusAdapter.java | 31 +++++++------------ .../mathjax/android/MathJaxView.java | 19 +++++++----- 2 files changed, 22 insertions(+), 28 deletions(-) diff --git a/app/src/main/java/app/fedilab/android/mastodon/ui/drawer/StatusAdapter.java b/app/src/main/java/app/fedilab/android/mastodon/ui/drawer/StatusAdapter.java index ac19ca87..9ab1f58f 100644 --- a/app/src/main/java/app/fedilab/android/mastodon/ui/drawer/StatusAdapter.java +++ b/app/src/main/java/app/fedilab/android/mastodon/ui/drawer/StatusAdapter.java @@ -2434,26 +2434,17 @@ public class StatusAdapter extends RecyclerView.Adapter @Override public List getPreloadItems(int position) { List attachments = new ArrayList<>(); - if (position == 0 && statusList.size() > 0) { - for (Status status : statusList.subList(0, 1)) { - Status statusToDeal = status.reblog != null ? status.reblog : status; - if (statusToDeal.media_attachments != null && statusToDeal.media_attachments.size() > 0) { - attachments.addAll(statusToDeal.media_attachments); - } - } - } else if (position > 0 && position < (statusList.size() - 1)) { - for (Status status : statusList.subList(position - 1, position + 1)) { - Status statusToDeal = status.reblog != null ? status.reblog : status; - if (statusToDeal.media_attachments != null && statusToDeal.media_attachments.size() > 0) { - attachments.addAll(statusToDeal.media_attachments); - } - } - } else { - for (Status status : statusList.subList(position, position)) { - Status statusToDeal = status.reblog != null ? status.reblog : status; - if (statusToDeal.media_attachments != null && statusToDeal.media_attachments.size() > 0) { - attachments.addAll(statusToDeal.media_attachments); - } + int max_size = statusList.size(); + int siblings = 3; + int from = Math.max((position - siblings), 0); + if (from > max_size - 1) { + from = max_size - 1; + } + int to = Math.min(position + siblings, max_size - 1); + for (Status status : statusList.subList(from, to)) { + Status statusToDeal = status.reblog != null ? status.reblog : status; + if (statusToDeal.media_attachments != null && statusToDeal.media_attachments.size() > 0) { + attachments.addAll(statusToDeal.media_attachments); } } return attachments; diff --git a/mathjaxandroid/src/main/java/de/timfreiheit/mathjax/android/MathJaxView.java b/mathjaxandroid/src/main/java/de/timfreiheit/mathjax/android/MathJaxView.java index 57e4c7df..8d76ea2f 100644 --- a/mathjaxandroid/src/main/java/de/timfreiheit/mathjax/android/MathJaxView.java +++ b/mathjaxandroid/src/main/java/de/timfreiheit/mathjax/android/MathJaxView.java @@ -1,8 +1,9 @@ package de.timfreiheit.mathjax.android; + import android.annotation.SuppressLint; -import android.app.ActivityManager; +import android.app.Application; import android.content.Context; import android.content.res.TypedArray; import android.os.Build; @@ -91,7 +92,7 @@ public class MathJaxView extends FrameLayout { } } - public static String getProcessName(Context context) { + /* public static String getProcessName(Context context) { ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); for (ActivityManager.RunningAppProcessInfo processInfo : manager.getRunningAppProcesses()) { if (processInfo.pid == android.os.Process.myPid()) { @@ -100,10 +101,16 @@ public class MathJaxView extends FrameLayout { } return null; - } + }*/ @SuppressLint({"SetJavaScriptEnabled", "AddJavascriptInterface"}) private void init(Context context, AttributeSet attrSet, MathJaxConfig config) { + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { + String process = Application.getProcessName(); + if (!context.getPackageName().equals(process)) + WebView.setDataDirectorySuffix(process); + } mWebView = new WebView(context); int gravity = Gravity.START; @@ -170,11 +177,7 @@ public class MathJaxView extends FrameLayout { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { WebView.setWebContentsDebuggingEnabled(true); } - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { - String process = getProcessName(context); - if (!context.getPackageName().equals(process)) - WebView.setDataDirectorySuffix(process); - } + } /**