6.6.2 - Alpha6 - 优化 VSCode 插件保存项目或运行项目出现异常时的消息提示方式

This commit is contained in:
SuperMonster003
2025-04-14 23:58:19 +08:00
parent 6c61b19fb5
commit 2ba88e5029
7 changed files with 112 additions and 16 deletions

View File

@@ -255,7 +255,12 @@
<activity
android:name="org.autojs.autojs.ui.project.BuildActivity"
android:configChanges="orientation|keyboardHidden|screenSize|locale" />
<activity
android:name="org.autojs.autojs.ui.error.ErrorDialogActivity"
android:theme="@style/AppTheme.Transparent"
android:exported="false" />
<activity android:name="org.autojs.autojs.ui.project.ProjectConfigActivity" />
<activity android:name="org.autojs.autojs.ui.keystore.ManageKeyStoreActivity" />

View File

@@ -3,7 +3,6 @@ package org.autojs.autojs.pluginclient;
import android.annotation.SuppressLint;
import android.content.Context;
import android.text.TextUtils;
import com.afollestad.materialdialogs.MaterialDialog;
import com.google.gson.JsonElement;
import com.google.gson.JsonNull;
import com.google.gson.JsonObject;
@@ -18,12 +17,17 @@ import org.autojs.autojs.model.script.Scripts;
import org.autojs.autojs.pio.PFiles;
import org.autojs.autojs.project.ProjectLauncher;
import org.autojs.autojs.script.StringScriptSource;
import org.autojs.autojs.ui.error.ErrorDialogActivity;
import org.autojs.autojs.util.MD5Utils;
import org.autojs.autojs.util.ViewUtils;
import org.autojs.autojs.util.WorkingDirectoryUtils;
import org.autojs.autojs6.R;
import java.io.*;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.HashMap;
/**
@@ -124,7 +128,19 @@ public class DevPluginResponseHandler implements Handler {
new ProjectLauncher(dir).launch(AutoJs.getInstance().getScriptEngineService());
} catch (Exception e) {
e.printStackTrace();
ViewUtils.showToast(mContext, R.string.text_invalid_project, true);
String message = e.getMessage();
if (message == null) {
ViewUtils.showToast(mContext, R.string.text_invalid_project, true);
} else {
// 使用 ErrorDialogActivity 代替直接显示 MaterialDialog
// 这样可以避免在非UI线程或Activity不可见状态下调用对话框的问题
ErrorDialogActivity.showErrorDialog(
mContext.getApplicationContext(),
R.string.text_invalid_project,
message,
R.string.dialog_button_dismiss
);
}
}
}
@@ -191,11 +207,12 @@ public class DevPluginResponseHandler implements Handler {
var e = err.getMessage();
if (e != null) msg += "\n" + e;
new MaterialDialog.Builder(mContext)
.title(R.string.text_failed)
.content(msg)
.positiveText(R.string.dialog_button_confirm)
.show();
ErrorDialogActivity.showErrorDialog(
mContext.getApplicationContext(),
R.string.text_failed,
msg,
R.string.dialog_button_confirm
);
}
);
}

View File

@@ -6,7 +6,10 @@ import org.mozilla.javascript.EvaluatorException
// @Hint by SuperMonster003 on Oct 31, 2024.
// ! This class include the exception's code line number when printing the error stack trace.
// ! zh-CN: 当前类可在打印错误堆栈信息时包含异常所在的 code line number (代码行号).
class WrappedRuntimeException(detailMessage: String, private val causeException: Throwable? = null) : EvaluatorException(detailMessage) {
class WrappedRuntimeException @JvmOverloads constructor(
detailMessage: String,
private val causeException: Throwable? = null,
) : EvaluatorException(detailMessage) {
constructor(causeException: Throwable) : this(causeException.message ?: "", causeException)

View File

@@ -0,0 +1,71 @@
package org.autojs.autojs.ui.error;
import android.content.Intent;
import android.os.Bundle;
import android.view.Window;
import com.afollestad.materialdialogs.MaterialDialog;
import org.autojs.autojs.ui.BaseActivity;
import org.autojs.autojs6.R;
/**
* A simple activity to show error dialogs when a context is not available or valid
* for showing dialogs directly.
*/
public class ErrorDialogActivity extends BaseActivity {
public static final String EXTRA_TITLE = "title";
public static final String EXTRA_MESSAGE = "message";
public static final String EXTRA_POSITIVE_BUTTON_TEXT = "positive_button_text";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
// Get extras from intent
Intent intent = getIntent();
String title = intent.getStringExtra(EXTRA_TITLE);
String message = intent.getStringExtra(EXTRA_MESSAGE);
String positiveButtonText = intent.getStringExtra(EXTRA_POSITIVE_BUTTON_TEXT);
// If no positive button text provided, use default dismiss text
if (positiveButtonText == null) {
positiveButtonText = getString(R.string.dialog_button_dismiss);
}
// Show dialog
new MaterialDialog.Builder(this)
.title(title)
.content(message)
.positiveText(positiveButtonText)
.positiveColorRes(R.color.dialog_button_default)
.cancelable(true)
.canceledOnTouchOutside(true)
.onPositive((dialog, which) -> finish())
.dismissListener(dialog -> finish())
.show();
}
/**
* Helper method to show an error dialog from any context
*/
public static void showErrorDialog(android.content.Context context, int titleRes, String message) {
Intent intent = new Intent(context, ErrorDialogActivity.class);
intent.putExtra(EXTRA_TITLE, context.getString(titleRes));
intent.putExtra(EXTRA_MESSAGE, message);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
/**
* Helper method to show an error dialog from any context with custom positive button text
*/
public static void showErrorDialog(android.content.Context context, int titleRes, String message, int positiveButtonTextRes) {
Intent intent = new Intent(context, ErrorDialogActivity.class);
intent.putExtra(EXTRA_TITLE, context.getString(titleRes));
intent.putExtra(EXTRA_MESSAGE, message);
intent.putExtra(EXTRA_POSITIVE_BUTTON_TEXT, context.getString(positiveButtonTextRes));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
}

View File

@@ -4,7 +4,7 @@
<!ENTITY url_github_autojs6 "http://project.autojs6.com">
<!ENTITY url_github_autojs6_vscode_extension "http://vsc_ext-project.autojs6.com">
<!ENTITY url_github_autojs6_vscode_extension "http://vscext-project.autojs6.com">
]>