Feat[remoteerror]: custom dialog handling

This commit is contained in:
artdeell 2023-10-09 13:04:50 +03:00
parent 64daa0799f
commit ae286fb57a
7 changed files with 68 additions and 43 deletions

View File

@ -1,19 +1,16 @@
package net.kdt.pojavlaunch; package net.kdt.pojavlaunch;
import android.app.Activity; import android.app.Activity;
import android.app.NotificationManager; import android.app.AlertDialog;
import android.app.PendingIntent;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat;
import net.kdt.pojavlaunch.contextexecutor.ContextExecutorTask; import net.kdt.pojavlaunch.contextexecutor.ContextExecutorTask;
import net.kdt.pojavlaunch.utils.NotificationUtils; import net.kdt.pojavlaunch.utils.NotificationUtils;
import net.kdt.pojavlaunch.utils.NotificationUtils;
import java.io.Serializable; import java.io.Serializable;
@ -49,7 +46,11 @@ public class ShowErrorActivity extends Activity {
@Override @Override
public void executeWithActivity(Activity activity) { 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 @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());
}
}
} }

View File

@ -48,6 +48,7 @@ import com.google.gson.Gson;
import com.google.gson.GsonBuilder; import com.google.gson.GsonBuilder;
import net.kdt.pojavlaunch.contextexecutor.ContextExecutor; import net.kdt.pojavlaunch.contextexecutor.ContextExecutor;
import net.kdt.pojavlaunch.contextexecutor.ContextExecutorTask;
import net.kdt.pojavlaunch.multirt.MultiRTUtils; import net.kdt.pojavlaunch.multirt.MultiRTUtils;
import net.kdt.pojavlaunch.multirt.Runtime; import net.kdt.pojavlaunch.multirt.Runtime;
import net.kdt.pojavlaunch.plugins.FFmpegPlugin; 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) { 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(); e.printStackTrace();
Runnable runnable = () -> { 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) { public static void showErrorRemote(Throwable e) {
showErrorRemote(null, e); showErrorRemote(null, e);
} }

View File

@ -144,7 +144,6 @@ public abstract class ModVersionListFragment<T> extends Fragment implements Runn
getTaskProxy().detachListener(); getTaskProxy().detachListener();
setTaskProxy(null); setTaskProxy(null);
mExpandableListView.setEnabled(true); mExpandableListView.setEnabled(true);
if(DownloadMirror.checkForTamperedException(context, e)) return;
Tools.showError(context, e); Tools.showError(context, e);
}); });
} }

View File

@ -1,13 +1,9 @@
package net.kdt.pojavlaunch.mirrors; package net.kdt.pojavlaunch.mirrors;
import android.app.AlertDialog;
import android.content.Context;
import android.text.Html;
import android.util.Log; import android.util.Log;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import net.kdt.pojavlaunch.R;
import net.kdt.pojavlaunch.Tools; import net.kdt.pojavlaunch.Tools;
import net.kdt.pojavlaunch.prefs.LauncherPreferences; import net.kdt.pojavlaunch.prefs.LauncherPreferences;
import net.kdt.pojavlaunch.utils.DownloadUtils; import net.kdt.pojavlaunch.utils.DownloadUtils;
@ -61,33 +57,6 @@ public class DownloadMirror {
return !LauncherPreferences.PREF_DOWNLOAD_SOURCE.equals("default"); 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() { private static String[] getMirrorSettings() {
switch (LauncherPreferences.PREF_DOWNLOAD_SOURCE) { switch (LauncherPreferences.PREF_DOWNLOAD_SOURCE) {
case "mcbbs": return MIRROR_MCBBS; case "mcbbs": return MIRROR_MCBBS;

View File

@ -1,4 +1,37 @@
package net.kdt.pojavlaunch.mirrors; 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) {}
} }

View File

@ -399,10 +399,10 @@ public class AsyncMinecraftDownloader {
} }
private boolean verifyManifest(File verJsonDir, JMinecraftVersionList.Version verInfo) { private boolean verifyManifest(File verJsonDir, JMinecraftVersionList.Version verInfo) {
return verJsonDir.exists() return /*verJsonDir.exists()
&& (!LauncherPreferences.PREF_CHECK_LIBRARY_SHA && (!LauncherPreferences.PREF_CHECK_LIBRARY_SHA
|| verInfo.sha1 == null || verInfo.sha1 == null
|| Tools.compareSHA1(verJsonDir, verInfo.sha1)); || Tools.compareSHA1(verJsonDir, verInfo.sha1));*/false;
} }
public static String normalizeVersionId(String versionString) { public static String normalizeVersionId(String versionString) {

View File

@ -37,8 +37,7 @@ public class ContextAwareDoneListener implements AsyncMinecraftDownloader.DoneLi
@Override @Override
public void onDownloadFailed(Throwable throwable) { public void onDownloadFailed(Throwable throwable) {
if(throwable != null) Tools.showErrorRemote(mErrorString, throwable);
Tools.showErrorRemote(mErrorString, throwable);
} }
@Override @Override