From ac7e902dca62fe00f442094a751048a802d13f90 Mon Sep 17 00:00:00 2001
From: wjc <1243177876@qq.com>
Date: 星期二, 16 九月 2025 09:15:28 +0800
Subject: [PATCH] 2025年09月16日09:15:27
---
app/src/main/java/com/hdl/photovoltaic/other/HdlThreadLogic.java | 208 ++++++++++++++++++++++++++++++++++++++++++++++------
1 files changed, 184 insertions(+), 24 deletions(-)
diff --git a/app/src/main/java/com/hdl/photovoltaic/other/HdlThreadLogic.java b/app/src/main/java/com/hdl/photovoltaic/other/HdlThreadLogic.java
index 73fe38b..1d87970 100644
--- a/app/src/main/java/com/hdl/photovoltaic/other/HdlThreadLogic.java
+++ b/app/src/main/java/com/hdl/photovoltaic/other/HdlThreadLogic.java
@@ -1,14 +1,21 @@
package com.hdl.photovoltaic.other;
-import android.app.Dialog;
import android.content.Context;
+import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Looper;
+import android.util.Log;
import android.widget.Toast;
-import androidx.appcompat.app.AlertDialog;
+import androidx.appcompat.content.res.AppCompatResources;
+import com.hdl.linkpm.sdk.core.exception.HDLException;
+import com.hdl.photovoltaic.R;
import com.hdl.photovoltaic.enums.ShowErrorMode;
+import com.hdl.photovoltaic.widget.ConfirmationCancelDialog;
+import com.hdl.photovoltaic.widget.ConfirmationExceptionDialog;
+import com.hdl.photovoltaic.widget.FlashingBoxDialog;
+import com.hdl.photovoltaic.widget.FlashingIconBoxDialog;
/**
* 绾跨▼閫昏緫
@@ -18,39 +25,57 @@
/**
- * 鍒囨崲鍥炰富绾跨▼
+ * 鍒囨崲鍥炰富绾跨▼鎵ц
*
- * @param run 鍥炶皟
+ * @param runnable 鍥炶皟
* @param context 涓婁笅鏂�(涓嶉渶瑕佸脊妗嗗~null)
* @param showErrorMode 鏄惁鏄剧ず閿欒(涓嶉渶瑕佹樉绀洪敊璇~null)
*/
- public static void runMainThread(Runnable run, Context context, ShowErrorMode showErrorMode) {
+ public static void runMainThread(Runnable runnable, Context context, ShowErrorMode showErrorMode) {
try {
if (Looper.myLooper() == Looper.getMainLooper()) {
- run.run();
+ runnable.run();
} else {
- handler.post(run);
+ handler.post(runnable);
}
} catch (Exception e) {
+
exception(e, showErrorMode, context);
+ }
+ }
+
+ /**
+ * 鍒囨崲鍥炰富绾跨▼鎵ц
+ *
+ * @param runnable 鍥炶皟
+ */
+ public static void runMainThread(Runnable runnable) {
+ try {
+ if (Looper.myLooper() == Looper.getMainLooper()) {
+ runnable.run();
+ } else {
+ handler.post(runnable);
+ }
+ } catch (Exception ignored) {
+
}
}
/**
- * 瀛愮嚎绋�
+ * 瀛愮嚎绋嬫墽琛�
*
- * @param run 鍥炶皟
+ * @param runnable 鍥炶皟
* @param context 涓婁笅鏂�(涓嶉渶瑕佸脊妗嗗~null)
* @param showErrorMode 鏄惁鏄剧ず閿欒(涓嶉渶瑕佸~null)
*/
- public static void runThread(Runnable run, Context context, ShowErrorMode showErrorMode) {
+ public static void runSubThread(Runnable runnable, Context context, ShowErrorMode showErrorMode) {
new Thread(new Runnable() {
@Override
public void run() {
try {
- run.run();
+ runnable.run();
} catch (Exception e) {
exception(e, showErrorMode, context);
}
@@ -59,14 +84,122 @@
}
+ /**
+ * 瀛愮嚎绋嬫墽琛�
+ *
+ * @param runnable 鍥炶皟
+ */
+ public static void runSubThread(Runnable runnable) {
+
+ new Thread(new Runnable() {
+ @Override
+ public void run() {
+ try {
+ runnable.run();
+ } catch (Exception ignored) {
+ }
+ }
+ }).start();
+
+ }
+
/**
- * @param e 寮傚父淇℃伅绫�
+ * 鍏ㄥ眬寮瑰嚭妗�,绯荤粺榛樿Toast
+ */
+ public static void toast(final Context context, final String text) {
+ handler.post(new Runnable() {
+ @Override
+ public void run() {
+ FlashingBoxDialog flashingBoxDialog = new FlashingBoxDialog(context, 1500);
+ flashingBoxDialog.setContent(text);
+ flashingBoxDialog.show();
+// Toast.makeText(context, text, Toast.LENGTH_SHORT).show();
+ }
+ });
+ }
+
+ /**
+ * 鍏ㄥ眬寮瑰嚭妗�,绯荤粺榛樿Toast
+ */
+ public static void toast(final Context context, final HDLException hdlException) {
+ handler.post(new Runnable() {
+ @Override
+ public void run() {
+// FlashingBoxDialog flashingIconBoxDialog = new FlashingBoxDialog(context, 1500);
+// flashingIconBoxDialog.setContent(hdlException.getMsg() + "(" + hdlException.getCode() + ")");
+// flashingIconBoxDialog.show();
+ Toast.makeText(context, hdlException.getMsg() + "(" + hdlException.getCode() + ")", Toast.LENGTH_SHORT).show();
+ }
+ });
+ }
+
+ /**
+ * 鍏ㄥ眬寮瑰嚭妗�,绯荤粺榛樿Toast
+ */
+ public static void toast(final Context context, final int resId) {
+ handler.post(new Runnable() {
+ @Override
+ public void run() {
+
+ Toast.makeText(context, resId, Toast.LENGTH_SHORT).show();
+ }
+ });
+ }
+
+
+ /**
+ * 鍏ㄥ眬寮瑰嚭妗�,閿欒鎻愮ず妗嗭紙钃濇箹涓婃牱寮忥級
+ *
+ * @param isBoolean 鍥炬爣(true浣跨敤鎴愬姛鍥炬爣,false浣跨敤澶辫触鍥炬爣)
+ * @param msg 淇℃伅
+ * @param code 鐘舵��
+ */
+ public static void tipFlashingBox(Context context, boolean isBoolean, String msg, int code) {
+ tipFlashingBox(context, isBoolean, msg, code, 1500);
+ }
+
+ /**
+ * 鍏ㄥ眬寮瑰嚭妗�,閿欒鎻愮ず妗嗭紙钃濇箹涓婃牱寮忥級
+ *
+ * @param isBoolean 鍥炬爣(true浣跨敤鎴愬姛鍥炬爣,false浣跨敤澶辫触鍥炬爣)
+ * @param msg 淇℃伅
+ * @param code 鐘舵��
+ * @param millisecond 璁剧疆寤舵椂鏃堕棿鍏抽棴寮圭獥(鍗曚綅ms)
+ */
+ public static void tipFlashingBox(Context context, boolean isBoolean, String msg, int code, int millisecond) {
+ if (context == null) {
+ return;
+ }
+ HdlThreadLogic.runMainThread(new Runnable() {
+ @Override
+ public void run() {
+ FlashingIconBoxDialog flashingIconBoxDialog = new FlashingIconBoxDialog(context, millisecond);
+ if (isBoolean) {
+ Drawable drawable = AppCompatResources.getDrawable(context, R.drawable.tip_succeed);
+ flashingIconBoxDialog.setImage(drawable);
+ } else {
+ Drawable drawable = AppCompatResources.getDrawable(context, R.drawable.tip_fail);
+ flashingIconBoxDialog.setImage(drawable);
+ }
+ if (code == 0) {
+ //鎴愬姛涓嶉渶瑕佸鍔犵姸鎬佺爜
+ flashingIconBoxDialog.setContent(msg);
+ } else {
+ flashingIconBoxDialog.setContent(msg + "(" + code + ")");
+ }
+ flashingIconBoxDialog.show();
+ }
+ }, null, null);
+ }
+
+ /**
+ * @param ex 寮傚父淇℃伅绫�
* @param context 涓婁笅鏂�(涓嶉渶瑕佸脊妗嗗~null)
* @param showErrorMode 鏄惁鏄剧ず閿欒(涓嶉渶瑕佸~null)
*/
- private static void exception(Exception e, ShowErrorMode showErrorMode, Context context) {
- if (showErrorMode == null || context == null || e == null) {
+ private static void exception(Exception ex, ShowErrorMode showErrorMode, Context context) {
+ if (showErrorMode == null || context == null || ex == null) {
return;
}
if (showErrorMode == ShowErrorMode.NO) {
@@ -76,18 +209,45 @@
handler.post(new Runnable() {
@Override
public void run() {
- Dialog alertDialog = new AlertDialog.Builder(context).
- setTitle("鎶辨瓑绋嬪簭鍑虹幇閿欒浜�,鐐瑰嚮\"纭\"鑾峰彇鏇村璇︾粏淇℃伅.").
- setMessage(e.getMessage()).
- create();
- alertDialog.show();
- //鎻愮ず
-// AlertDialog alertDialog = new AlertDialog(context, androidx.fragment.R.style.TextAppearance_Compat_Notification);
-// alertDialog.setTitle("鎶辨瓑绋嬪簭鍑虹幇閿欒浜�");
-// alertDialog.show();
-// Toast.makeText(context, "鎶辨瓑绋嬪簭鍑虹幇閿欒浜�", Toast.LENGTH_SHORT).show();
+
+ ConfirmationCancelDialog confirmationCancelDialog = new ConfirmationCancelDialog(context);
+ confirmationCancelDialog.setTitle("鎻愮ず");
+ confirmationCancelDialog.setContent("寰堟姳姝�,绋嬪簭鍑虹幇閿欒浜�,鐐瑰嚮\"纭\"鑾峰彇鏇村璇︾粏閿欒淇℃伅.");
+ confirmationCancelDialog.setConfirmation("纭");
+ confirmationCancelDialog.setCancel("鍙栨秷");
+ confirmationCancelDialog.show();
+ confirmationCancelDialog.setNoOnclickListener(new ConfirmationCancelDialog.onNoOnclickListener() {
+ @Override
+ public void Cancel() {
+ confirmationCancelDialog.dismiss();
+ }
+ });
+ confirmationCancelDialog.setYesOnclickListener(new ConfirmationCancelDialog.onYesOnclickListener() {
+ @Override
+ public void Confirm() {
+ confirmationCancelDialog.dismiss();
+ ConfirmationExceptionDialog confirmationExceptionDialog = new ConfirmationExceptionDialog(context);
+// String s = getStackTrace(ex);
+ String s = Log.getStackTraceString(ex);
+ confirmationExceptionDialog.setContent(s);
+ confirmationExceptionDialog.show();
+ }
+ });
}
});
}
+
+ /**
+ * @return 璋冪敤鏍�
+ */
+ private static String getStackTrace(Exception ex) {
+ StringBuilder sb = new StringBuilder();
+ StackTraceElement[] trace = ex.getStackTrace();
+ for (StackTraceElement stackTraceElement : trace) {
+ sb.append(stackTraceElement).append("\n");
+ }
+ return sb.toString();
+ }
+
}
--
Gitblit v1.8.0