Fix some crashes

This commit is contained in:
Thomas 2023-01-31 15:00:40 +01:00
parent c37d8ab34f
commit f8b0ed7f18
2 changed files with 22 additions and 28 deletions

View file

@ -2434,28 +2434,19 @@ public class StatusAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
@Override @Override
public List<Attachment> getPreloadItems(int position) { public List<Attachment> getPreloadItems(int position) {
List<Attachment> attachments = new ArrayList<>(); List<Attachment> attachments = new ArrayList<>();
if (position == 0 && statusList.size() > 0) { int max_size = statusList.size();
for (Status status : statusList.subList(0, 1)) { 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; Status statusToDeal = status.reblog != null ? status.reblog : status;
if (statusToDeal.media_attachments != null && statusToDeal.media_attachments.size() > 0) { if (statusToDeal.media_attachments != null && statusToDeal.media_attachments.size() > 0) {
attachments.addAll(statusToDeal.media_attachments); 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);
}
}
}
return attachments; return attachments;
} }

View file

@ -1,8 +1,9 @@
package de.timfreiheit.mathjax.android; package de.timfreiheit.mathjax.android;
import android.annotation.SuppressLint; import android.annotation.SuppressLint;
import android.app.ActivityManager; import android.app.Application;
import android.content.Context; import android.content.Context;
import android.content.res.TypedArray; import android.content.res.TypedArray;
import android.os.Build; 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); ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
for (ActivityManager.RunningAppProcessInfo processInfo : manager.getRunningAppProcesses()) { for (ActivityManager.RunningAppProcessInfo processInfo : manager.getRunningAppProcesses()) {
if (processInfo.pid == android.os.Process.myPid()) { if (processInfo.pid == android.os.Process.myPid()) {
@ -100,10 +101,16 @@ public class MathJaxView extends FrameLayout {
} }
return null; return null;
} }*/
@SuppressLint({"SetJavaScriptEnabled", "AddJavascriptInterface"}) @SuppressLint({"SetJavaScriptEnabled", "AddJavascriptInterface"})
private void init(Context context, AttributeSet attrSet, MathJaxConfig config) { 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); mWebView = new WebView(context);
int gravity = Gravity.START; int gravity = Gravity.START;
@ -170,11 +177,7 @@ public class MathJaxView extends FrameLayout {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
WebView.setWebContentsDebuggingEnabled(true); WebView.setWebContentsDebuggingEnabled(true);
} }
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
String process = getProcessName(context);
if (!context.getPackageName().equals(process))
WebView.setDataDirectorySuffix(process);
}
} }
/** /**