From ae286fb57a7d89acf69ab9398ebdecd2994b6aa6 Mon Sep 17 00:00:00 2001 From: artdeell Date: Mon, 9 Oct 2023 13:04:50 +0300 Subject: [PATCH] Feat[remoteerror]: custom dialog handling --- .../kdt/pojavlaunch/ShowErrorActivity.java | 25 +++++++++---- .../main/java/net/kdt/pojavlaunch/Tools.java | 12 +++++++ .../fragments/ModVersionListFragment.java | 1 - .../pojavlaunch/mirrors/DownloadMirror.java | 31 ---------------- .../mirrors/MirrorTamperedException.java | 35 ++++++++++++++++++- .../tasks/AsyncMinecraftDownloader.java | 4 +-- .../tasks/ContextAwareDoneListener.java | 3 +- 7 files changed, 68 insertions(+), 43 deletions(-) diff --git a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/ShowErrorActivity.java b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/ShowErrorActivity.java index 1c78021e1..88de61926 100644 --- a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/ShowErrorActivity.java +++ b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/ShowErrorActivity.java @@ -1,19 +1,16 @@ package net.kdt.pojavlaunch; import android.app.Activity; -import android.app.NotificationManager; -import android.app.PendingIntent; +import android.app.AlertDialog; import android.content.Context; import android.content.Intent; -import android.os.Build; import android.os.Bundle; +import androidx.annotation.NonNull; import androidx.annotation.Nullable; -import androidx.core.app.NotificationCompat; import net.kdt.pojavlaunch.contextexecutor.ContextExecutorTask; import net.kdt.pojavlaunch.utils.NotificationUtils; -import net.kdt.pojavlaunch.utils.NotificationUtils; import java.io.Serializable; @@ -49,7 +46,11 @@ public class ShowErrorActivity extends Activity { @Override public void executeWithActivity(Activity activity) { - Tools.showError(activity, mRolledMsg, mThrowable, true); + if(mThrowable instanceof ContextExecutorTask) { + ((ContextExecutorTask)mThrowable).executeWithActivity(activity); + }else { + Tools.showError(activity, mRolledMsg, mThrowable, activity instanceof ShowErrorActivity); + } } @Override @@ -65,4 +66,16 @@ public class ShowErrorActivity extends Activity { ); } } + + /** + * Install remote dialog handling onto a dialog. This should be used when the dialog is planned to be presented + * through Tools.showError or Tools.showErrorRemote as a Throwable implementing a ContextExecutorTask. + * @param callerActivity the activity provided by the ContextExecutorTask.executeWithActivity + * @param builder the alert dialog builder. + */ + public static void installRemoteDialogHandling(Activity callerActivity, @NonNull AlertDialog.Builder builder) { + if (callerActivity instanceof ShowErrorActivity) { + builder.setOnDismissListener(d -> callerActivity.finish()); + } + } } diff --git a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/Tools.java b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/Tools.java index a96c4d9b8..94170fbb9 100644 --- a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/Tools.java +++ b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/Tools.java @@ -48,6 +48,7 @@ import com.google.gson.Gson; import com.google.gson.GsonBuilder; import net.kdt.pojavlaunch.contextexecutor.ContextExecutor; +import net.kdt.pojavlaunch.contextexecutor.ContextExecutorTask; import net.kdt.pojavlaunch.multirt.MultiRTUtils; import net.kdt.pojavlaunch.multirt.Runtime; import net.kdt.pojavlaunch.plugins.FFmpegPlugin; @@ -556,6 +557,9 @@ public final class Tools { } private static void showError(final Context ctx, final int titleId, final String rolledMessage, final Throwable e, final boolean exitIfOk, final boolean showMore) { + if(e instanceof ContextExecutorTask) { + ContextExecutor.execute((ContextExecutorTask) e); + } e.printStackTrace(); Runnable runnable = () -> { @@ -599,6 +603,14 @@ public final class Tools { } } + /** + * Show the error remotely in a context-aware fashion. Has generally the same behaviour as + * Tools.showError when in an activity, but when not in one, sends a notification that opens an + * activity and calls Tools.showError(). + * NOTE: If the Throwable is a ContextExecutorTask and when not in an activity, + * its executeWithApplication() method will never be called. + * @param e the error (throwable) + */ public static void showErrorRemote(Throwable e) { showErrorRemote(null, e); } diff --git a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/fragments/ModVersionListFragment.java b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/fragments/ModVersionListFragment.java index b8150987e..68530d2cf 100644 --- a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/fragments/ModVersionListFragment.java +++ b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/fragments/ModVersionListFragment.java @@ -144,7 +144,6 @@ public abstract class ModVersionListFragment extends Fragment implements Runn getTaskProxy().detachListener(); setTaskProxy(null); mExpandableListView.setEnabled(true); - if(DownloadMirror.checkForTamperedException(context, e)) return; Tools.showError(context, e); }); } diff --git a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/mirrors/DownloadMirror.java b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/mirrors/DownloadMirror.java index 369cd1108..9ccdae1e4 100644 --- a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/mirrors/DownloadMirror.java +++ b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/mirrors/DownloadMirror.java @@ -1,13 +1,9 @@ package net.kdt.pojavlaunch.mirrors; -import android.app.AlertDialog; -import android.content.Context; -import android.text.Html; import android.util.Log; import androidx.annotation.Nullable; -import net.kdt.pojavlaunch.R; import net.kdt.pojavlaunch.Tools; import net.kdt.pojavlaunch.prefs.LauncherPreferences; import net.kdt.pojavlaunch.utils.DownloadUtils; @@ -61,33 +57,6 @@ public class DownloadMirror { return !LauncherPreferences.PREF_DOWNLOAD_SOURCE.equals("default"); } - public static boolean checkForTamperedException(Context context, Throwable e) { - if(e instanceof MirrorTamperedException){ - showMirrorTamperedDialog(context); - return true; - } - return false; - } - - private static void showMirrorTamperedDialog(Context ctx) { - Tools.runOnUiThread(()->{ - AlertDialog.Builder builder = new AlertDialog.Builder(ctx); - builder.setTitle(R.string.dl_tampered_manifest_title); - builder.setMessage(Html.fromHtml(ctx.getString(R.string.dl_tampered_manifest))); - builder.setPositiveButton(R.string.dl_switch_to_official_site,(d,w)->{ - LauncherPreferences.DEFAULT_PREF.edit().putString("downloadSource", "default").apply(); - LauncherPreferences.PREF_DOWNLOAD_SOURCE = "default"; - - }); - builder.setNegativeButton(R.string.dl_turn_off_manifest_checks,(d,w)->{ - LauncherPreferences.DEFAULT_PREF.edit().putBoolean("verifyManifest", false).apply(); - LauncherPreferences.PREF_VERIFY_MANIFEST = false; - }); - builder.setNeutralButton(android.R.string.cancel, (d,w)->{}); - builder.show(); - }); - } - private static String[] getMirrorSettings() { switch (LauncherPreferences.PREF_DOWNLOAD_SOURCE) { case "mcbbs": return MIRROR_MCBBS; diff --git a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/mirrors/MirrorTamperedException.java b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/mirrors/MirrorTamperedException.java index 548d414e1..31c245535 100644 --- a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/mirrors/MirrorTamperedException.java +++ b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/mirrors/MirrorTamperedException.java @@ -1,4 +1,37 @@ package net.kdt.pojavlaunch.mirrors; -public class MirrorTamperedException extends Exception{ +import android.app.Activity; +import android.app.AlertDialog; +import android.content.Context; +import android.text.Html; + +import net.kdt.pojavlaunch.R; +import net.kdt.pojavlaunch.ShowErrorActivity; +import net.kdt.pojavlaunch.contextexecutor.ContextExecutorTask; +import net.kdt.pojavlaunch.prefs.LauncherPreferences; + +public class MirrorTamperedException extends Exception implements ContextExecutorTask { + // Do not change. Android really hates when this value changes for some reason. + private static final long serialVersionUID = -7482301619612640658L; + @Override + public void executeWithActivity(Activity activity) { + AlertDialog.Builder builder = new AlertDialog.Builder(activity); + builder.setTitle(R.string.dl_tampered_manifest_title); + builder.setMessage(Html.fromHtml(activity.getString(R.string.dl_tampered_manifest))); + builder.setPositiveButton(R.string.dl_switch_to_official_site,(d,w)->{ + LauncherPreferences.DEFAULT_PREF.edit().putString("downloadSource", "default").apply(); + LauncherPreferences.PREF_DOWNLOAD_SOURCE = "default"; + + }); + builder.setNegativeButton(R.string.dl_turn_off_manifest_checks,(d,w)->{ + LauncherPreferences.DEFAULT_PREF.edit().putBoolean("verifyManifest", false).apply(); + LauncherPreferences.PREF_VERIFY_MANIFEST = false; + }); + builder.setNeutralButton(android.R.string.cancel, (d,w)->{}); + ShowErrorActivity.installRemoteDialogHandling(activity, builder); + builder.show(); + } + + @Override + public void executeWithApplication(Context context) {} } diff --git a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/tasks/AsyncMinecraftDownloader.java b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/tasks/AsyncMinecraftDownloader.java index def624f51..54d0b32a1 100644 --- a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/tasks/AsyncMinecraftDownloader.java +++ b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/tasks/AsyncMinecraftDownloader.java @@ -399,10 +399,10 @@ public class AsyncMinecraftDownloader { } private boolean verifyManifest(File verJsonDir, JMinecraftVersionList.Version verInfo) { - return verJsonDir.exists() + return /*verJsonDir.exists() && (!LauncherPreferences.PREF_CHECK_LIBRARY_SHA || verInfo.sha1 == null - || Tools.compareSHA1(verJsonDir, verInfo.sha1)); + || Tools.compareSHA1(verJsonDir, verInfo.sha1));*/false; } public static String normalizeVersionId(String versionString) { diff --git a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/tasks/ContextAwareDoneListener.java b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/tasks/ContextAwareDoneListener.java index 4d8c280d8..588ee1d1f 100644 --- a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/tasks/ContextAwareDoneListener.java +++ b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/tasks/ContextAwareDoneListener.java @@ -37,8 +37,7 @@ public class ContextAwareDoneListener implements AsyncMinecraftDownloader.DoneLi @Override public void onDownloadFailed(Throwable throwable) { - if(throwable != null) - Tools.showErrorRemote(mErrorString, throwable); + Tools.showErrorRemote(mErrorString, throwable); } @Override