| | |
| | | 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; |
| | | |
| | | /** |
| | | * 线程逻辑 |
| | |
| | | |
| | | |
| | | /** |
| | | * 切换回主线程 |
| | | * 切换回主线程执行 |
| | | * |
| | | * @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); |
| | | } |
| | | } |
| | | }).start(); |
| | | |
| | | } |
| | | |
| | | /** |
| | | * 子线程执行 |
| | | * |
| | | * @param runnable 回调 |
| | | */ |
| | | public static void runSubThread(Runnable runnable) { |
| | | |
| | | new Thread(new Runnable() { |
| | | @Override |
| | | public void run() { |
| | | try { |
| | | runnable.run(); |
| | | } catch (Exception ignored) { |
| | | } |
| | | } |
| | | }).start(); |
| | |
| | | handler.post(new Runnable() { |
| | | @Override |
| | | public void run() { |
| | | Toast.makeText(context, text, Toast.LENGTH_SHORT).show(); |
| | | 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(); |
| | | } |
| | | }); |
| | | } |
| | |
| | | handler.post(new Runnable() { |
| | | @Override |
| | | public void run() { |
| | | |
| | | Toast.makeText(context, resId, Toast.LENGTH_SHORT).show(); |
| | | } |
| | | }); |
| | |
| | | |
| | | |
| | | /** |
| | | * @param e 异常信息类 |
| | | * 全局弹出框,错误提示框(蓝湖上样式) |
| | | * |
| | | * @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) { |
| | |
| | | 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(); |
| | | } |
| | | |
| | | } |