优化:测试发送通道/转发规则时创建子线程运行 & 异常捕获

This commit is contained in:
pppscn 2022-06-27 17:12:43 +08:00
parent ea94e30347
commit b3b51a74ce
15 changed files with 228 additions and 98 deletions

View File

@ -1,5 +1,6 @@
package com.idormy.sms.forwarder.fragment
import android.os.Looper
import android.util.Log
import android.view.LayoutInflater
import android.view.View
@ -627,7 +628,16 @@ class RulesEditFragment : BaseFragment<FragmentRulesEditBinding?>(), View.OnClic
}
override fun onSuccess(sender: Sender) {
SendUtils.sendMsgSender(msgInfo, rule, sender, 0L)
Thread {
try {
SendUtils.sendMsgSender(msgInfo, rule, sender, 0L)
} catch (e: Exception) {
e.printStackTrace()
if (Looper.myLooper() == null) Looper.prepare()
XToastUtils.error(e.message.toString())
Looper.loop()
}
}.start()
}
})

View File

@ -1,5 +1,6 @@
package com.idormy.sms.forwarder.fragment.senders
import android.os.Looper
import android.text.TextUtils
import android.util.Log
import android.view.LayoutInflater
@ -183,10 +184,19 @@ class BarkFragment : BaseFragment<FragmentSendersBarkBinding?>(), View.OnClickLi
}
R.id.btn_test -> {
mCountDownHelper?.start()
val settingVo = checkSetting()
Log.d(TAG, settingVo.toString())
val msgInfo = MsgInfo("sms", getString(R.string.test_phone_num), getString(R.string.test_sender_sms), Date(), getString(R.string.test_sim_info))
BarkUtils.sendMsg(settingVo, msgInfo)
Thread {
try {
val settingVo = checkSetting()
Log.d(TAG, settingVo.toString())
val msgInfo = MsgInfo("sms", getString(R.string.test_phone_num), getString(R.string.test_sender_sms), Date(), getString(R.string.test_sim_info))
BarkUtils.sendMsg(settingVo, msgInfo)
} catch (e: Exception) {
e.printStackTrace()
if (Looper.myLooper() == null) Looper.prepare()
XToastUtils.error(e.message.toString())
Looper.loop()
}
}.start()
return
}
R.id.btn_del -> {

View File

@ -1,5 +1,6 @@
package com.idormy.sms.forwarder.fragment.senders
import android.os.Looper
import android.text.TextUtils
import android.util.Log
import android.view.LayoutInflater
@ -158,10 +159,19 @@ class DingtalkFragment : BaseFragment<FragmentSendersDingtalkBinding?>(), View.O
when (v.id) {
R.id.btn_test -> {
mCountDownHelper?.start()
val settingVo = checkSetting()
Log.d(TAG, settingVo.toString())
val msgInfo = MsgInfo("sms", getString(R.string.test_phone_num), getString(R.string.test_sender_sms), Date(), getString(R.string.test_sim_info))
DingtalkUtils.sendMsg(settingVo, msgInfo)
Thread {
try {
val settingVo = checkSetting()
Log.d(TAG, settingVo.toString())
val msgInfo = MsgInfo("sms", getString(R.string.test_phone_num), getString(R.string.test_sender_sms), Date(), getString(R.string.test_sim_info))
DingtalkUtils.sendMsg(settingVo, msgInfo)
} catch (e: Exception) {
e.printStackTrace()
if (Looper.myLooper() == null) Looper.prepare()
XToastUtils.error(e.message.toString())
Looper.loop()
}
}.start()
return
}
R.id.btn_del -> {

View File

@ -1,5 +1,6 @@
package com.idormy.sms.forwarder.fragment.senders
import android.os.Looper
import android.text.TextUtils
import android.util.Log
import android.view.LayoutInflater
@ -212,10 +213,19 @@ class EmailFragment : BaseFragment<FragmentSendersEmailBinding?>(), View.OnClick
}
R.id.btn_test -> {
mCountDownHelper?.start()
val settingVo = checkSetting()
Log.d(TAG, settingVo.toString())
val msgInfo = MsgInfo("sms", getString(R.string.test_phone_num), getString(R.string.test_sender_sms), Date(), getString(R.string.test_sim_info))
EmailUtils.sendMsg(settingVo, msgInfo)
Thread {
try {
val settingVo = checkSetting()
Log.d(TAG, settingVo.toString())
val msgInfo = MsgInfo("sms", getString(R.string.test_phone_num), getString(R.string.test_sender_sms), Date(), getString(R.string.test_sim_info))
EmailUtils.sendMsg(settingVo, msgInfo)
} catch (e: Exception) {
e.printStackTrace()
if (Looper.myLooper() == null) Looper.prepare()
XToastUtils.error(e.message.toString())
Looper.loop()
}
}.start()
return
}
R.id.btn_del -> {

View File

@ -1,5 +1,6 @@
package com.idormy.sms.forwarder.fragment.senders
import android.os.Looper
import android.text.TextUtils
import android.util.Log
import android.view.LayoutInflater
@ -157,10 +158,19 @@ class FeishuFragment : BaseFragment<FragmentSendersFeishuBinding?>(), View.OnCli
}
R.id.btn_test -> {
mCountDownHelper?.start()
val settingVo = checkSetting()
Log.d(TAG, settingVo.toString())
val msgInfo = MsgInfo("sms", getString(R.string.test_phone_num), getString(R.string.test_sender_sms), Date(), getString(R.string.test_sim_info))
FeishuUtils.sendMsg(settingVo, msgInfo)
Thread {
try {
val settingVo = checkSetting()
Log.d(TAG, settingVo.toString())
val msgInfo = MsgInfo("sms", getString(R.string.test_phone_num), getString(R.string.test_sender_sms), Date(), getString(R.string.test_sim_info))
FeishuUtils.sendMsg(settingVo, msgInfo)
} catch (e: Exception) {
e.printStackTrace()
if (Looper.myLooper() == null) Looper.prepare()
XToastUtils.error(e.message.toString())
Looper.loop()
}
}.start()
return
}
R.id.btn_del -> {

View File

@ -1,5 +1,6 @@
package com.idormy.sms.forwarder.fragment.senders
import android.os.Looper
import android.text.TextUtils
import android.util.Log
import android.view.LayoutInflater
@ -156,10 +157,19 @@ class GotifyFragment : BaseFragment<FragmentSendersGotifyBinding?>(), View.OnCli
}
R.id.btn_test -> {
mCountDownHelper?.start()
val settingVo = checkSetting()
Log.d(TAG, settingVo.toString())
val msgInfo = MsgInfo("sms", getString(R.string.test_phone_num), getString(R.string.test_sender_sms), Date(), getString(R.string.test_sim_info))
GotifyUtils.sendMsg(settingVo, msgInfo)
Thread {
try {
val settingVo = checkSetting()
Log.d(TAG, settingVo.toString())
val msgInfo = MsgInfo("sms", getString(R.string.test_phone_num), getString(R.string.test_sender_sms), Date(), getString(R.string.test_sim_info))
GotifyUtils.sendMsg(settingVo, msgInfo)
} catch (e: Exception) {
e.printStackTrace()
if (Looper.myLooper() == null) Looper.prepare()
XToastUtils.error(e.message.toString())
Looper.loop()
}
}.start()
return
}
R.id.btn_del -> {

View File

@ -1,5 +1,6 @@
package com.idormy.sms.forwarder.fragment.senders
import android.os.Looper
import android.text.TextUtils
import android.util.Log
import android.view.LayoutInflater
@ -176,10 +177,19 @@ class PushplusFragment : BaseFragment<FragmentSendersPushplusBinding?>(), View.O
}
R.id.btn_test -> {
mCountDownHelper?.start()
val settingVo = checkSetting()
Log.d(TAG, settingVo.toString())
val msgInfo = MsgInfo("sms", getString(R.string.test_phone_num), getString(R.string.test_sender_sms), Date(), getString(R.string.test_sim_info))
PushplusUtils.sendMsg(settingVo, msgInfo)
Thread {
try {
val settingVo = checkSetting()
Log.d(TAG, settingVo.toString())
val msgInfo = MsgInfo("sms", getString(R.string.test_phone_num), getString(R.string.test_sender_sms), Date(), getString(R.string.test_sim_info))
PushplusUtils.sendMsg(settingVo, msgInfo)
} catch (e: Exception) {
e.printStackTrace()
if (Looper.myLooper() == null) Looper.prepare()
XToastUtils.error(e.message.toString())
Looper.loop()
}
}.start()
return
}
R.id.btn_del -> {

View File

@ -1,5 +1,6 @@
package com.idormy.sms.forwarder.fragment.senders
import android.os.Looper
import android.text.TextUtils
import android.util.Log
import android.view.LayoutInflater
@ -138,10 +139,19 @@ class ServerchanFragment : BaseFragment<FragmentSendersServerchanBinding?>(), Vi
when (v.id) {
R.id.btn_test -> {
mCountDownHelper?.start()
val settingVo = checkSetting()
Log.d(TAG, settingVo.toString())
val msgInfo = MsgInfo("sms", getString(R.string.test_phone_num), getString(R.string.test_sender_sms), Date(), getString(R.string.test_sim_info))
ServerchanUtils.sendMsg(settingVo, msgInfo)
Thread {
try {
val settingVo = checkSetting()
Log.d(TAG, settingVo.toString())
val msgInfo = MsgInfo("sms", getString(R.string.test_phone_num), getString(R.string.test_sender_sms), Date(), getString(R.string.test_sim_info))
ServerchanUtils.sendMsg(settingVo, msgInfo)
} catch (e: Exception) {
e.printStackTrace()
if (Looper.myLooper() == null) Looper.prepare()
XToastUtils.error(e.message.toString())
Looper.loop()
}
}.start()
return
}
R.id.btn_del -> {

View File

@ -1,5 +1,6 @@
package com.idormy.sms.forwarder.fragment.senders
import android.os.Looper
import android.text.TextUtils
import android.util.Log
import android.view.LayoutInflater
@ -164,10 +165,19 @@ class SmsFragment : BaseFragment<FragmentSendersSmsBinding?>(), View.OnClickList
when (v.id) {
R.id.btn_test -> {
mCountDownHelper?.start()
val settingVo = checkSetting()
Log.d(TAG, settingVo.toString())
val msgInfo = MsgInfo("sms", getString(R.string.test_phone_num), getString(R.string.test_sender_sms), Date(), getString(R.string.test_sim_info))
SmsUtils.sendMsg(settingVo, msgInfo)
Thread {
try {
val settingVo = checkSetting()
Log.d(TAG, settingVo.toString())
val msgInfo = MsgInfo("sms", getString(R.string.test_phone_num), getString(R.string.test_sender_sms), Date(), getString(R.string.test_sim_info))
SmsUtils.sendMsg(settingVo, msgInfo)
} catch (e: Exception) {
e.printStackTrace()
if (Looper.myLooper() == null) Looper.prepare()
XToastUtils.error(e.message.toString())
Looper.loop()
}
}.start()
return
}
R.id.btn_del -> {

View File

@ -1,5 +1,6 @@
package com.idormy.sms.forwarder.fragment.senders
import android.os.Looper
import android.text.TextUtils
import android.util.Log
import android.view.LayoutInflater
@ -150,10 +151,19 @@ class WebhookFragment : BaseFragment<FragmentSendersWebhookBinding?>(), View.OnC
when (v.id) {
R.id.btn_test -> {
mCountDownHelper?.start()
val settingVo = checkSetting()
Log.d(TAG, settingVo.toString())
val msgInfo = MsgInfo("sms", getString(R.string.test_phone_num), getString(R.string.test_sender_sms), Date(), getString(R.string.test_sim_info))
WebhookUtils.sendMsg(settingVo, msgInfo)
Thread {
try {
val settingVo = checkSetting()
Log.d(TAG, settingVo.toString())
val msgInfo = MsgInfo("sms", getString(R.string.test_phone_num), getString(R.string.test_sender_sms), Date(), getString(R.string.test_sim_info))
WebhookUtils.sendMsg(settingVo, msgInfo)
} catch (e: Exception) {
e.printStackTrace()
if (Looper.myLooper() == null) Looper.prepare()
XToastUtils.error(e.message.toString())
Looper.loop()
}
}.start()
return
}
R.id.btn_del -> {

View File

@ -1,6 +1,7 @@
package com.idormy.sms.forwarder.fragment.senders
import android.annotation.SuppressLint
import android.os.Looper
import android.text.TextUtils
import android.util.Log
import android.view.LayoutInflater
@ -155,10 +156,19 @@ class WeworkAgentFragment : BaseFragment<FragmentSendersWeworkAgentBinding?>(),
when (v.id) {
R.id.btn_test -> {
mCountDownHelper?.start()
val settingVo = checkSetting()
Log.d(TAG, settingVo.toString())
val msgInfo = MsgInfo("sms", getString(R.string.test_phone_num), getString(R.string.test_sender_sms), Date(), getString(R.string.test_sim_info))
WeworkAgentUtils.sendMsg(settingVo, msgInfo)
Thread {
try {
val settingVo = checkSetting()
Log.d(TAG, settingVo.toString())
val msgInfo = MsgInfo("sms", getString(R.string.test_phone_num), getString(R.string.test_sender_sms), Date(), getString(R.string.test_sim_info))
WeworkAgentUtils.sendMsg(settingVo, msgInfo)
} catch (e: Exception) {
e.printStackTrace()
if (Looper.myLooper() == null) Looper.prepare()
XToastUtils.error(e.message.toString())
Looper.loop()
}
}.start()
return
}
R.id.btn_del -> {

View File

@ -1,5 +1,6 @@
package com.idormy.sms.forwarder.fragment.senders
import android.os.Looper
import android.text.TextUtils
import android.util.Log
import android.view.LayoutInflater
@ -136,10 +137,19 @@ class WeworkRobotFragment : BaseFragment<FragmentSendersWeworkRobotBinding?>(),
when (v.id) {
R.id.btn_test -> {
mCountDownHelper?.start()
val settingVo = checkSetting()
Log.d(TAG, settingVo.toString())
val msgInfo = MsgInfo("sms", getString(R.string.test_phone_num), getString(R.string.test_sender_sms), Date(), getString(R.string.test_sim_info))
WeworkRobotUtils.sendMsg(settingVo, msgInfo)
Thread {
try {
val settingVo = checkSetting()
Log.d(TAG, settingVo.toString())
val msgInfo = MsgInfo("sms", getString(R.string.test_phone_num), getString(R.string.test_sender_sms), Date(), getString(R.string.test_sim_info))
WeworkRobotUtils.sendMsg(settingVo, msgInfo)
} catch (e: Exception) {
e.printStackTrace()
if (Looper.myLooper() == null) Looper.prepare()
XToastUtils.error(e.message.toString())
Looper.loop()
}
}.start()
return
}
R.id.btn_del -> {

View File

@ -1,5 +1,6 @@
package com.idormy.sms.forwarder.utils
import android.os.Looper
import android.util.Log
import androidx.work.OneTimeWorkRequestBuilder
import androidx.work.WorkManager
@ -68,68 +69,76 @@ object SendUtils {
//匹配发送通道发送消息
fun sendMsgSender(msgInfo: MsgInfo, rule: Rule, sender: Sender, logId: Long) {
when (sender.type) {
TYPE_DINGDING -> {
val settingVo = Gson().fromJson(sender.jsonSetting, DingtalkSetting::class.java)
DingtalkUtils.sendMsg(settingVo, msgInfo, rule, logId)
try {
when (sender.type) {
TYPE_DINGDING -> {
val settingVo = Gson().fromJson(sender.jsonSetting, DingtalkSetting::class.java)
DingtalkUtils.sendMsg(settingVo, msgInfo, rule, logId)
}
TYPE_EMAIL -> {
val settingVo = Gson().fromJson(sender.jsonSetting, EmailSetting::class.java)
EmailUtils.sendMsg(settingVo, msgInfo, rule, logId)
}
TYPE_BARK -> {
val settingVo = Gson().fromJson(sender.jsonSetting, BarkSetting::class.java)
BarkUtils.sendMsg(settingVo, msgInfo, rule, logId)
}
TYPE_WEBHOOK -> {
val settingVo = Gson().fromJson(sender.jsonSetting, WebhookSetting::class.java)
WebhookUtils.sendMsg(settingVo, msgInfo, rule, logId)
}
TYPE_WEWORK_ROBOT -> {
val settingVo = Gson().fromJson(sender.jsonSetting, WeworkRobotSetting::class.java)
WeworkRobotUtils.sendMsg(settingVo, msgInfo, rule, logId)
}
TYPE_WEWORK_AGENT -> {
val settingVo = Gson().fromJson(sender.jsonSetting, WeworkAgentSetting::class.java)
WeworkAgentUtils.sendMsg(settingVo, msgInfo, rule, logId)
}
TYPE_SERVERCHAN -> {
val settingVo = Gson().fromJson(sender.jsonSetting, ServerchanSetting::class.java)
ServerchanUtils.sendMsg(settingVo, msgInfo, rule, logId)
}
TYPE_TELEGRAM -> {
val settingVo = Gson().fromJson(sender.jsonSetting, TelegramSetting::class.java)
TelegramUtils.sendMsg(settingVo, msgInfo, rule, logId)
}
TYPE_SMS -> {
val settingVo = Gson().fromJson(sender.jsonSetting, SmsSetting::class.java)
SmsUtils.sendMsg(settingVo, msgInfo, rule, logId)
}
TYPE_FEISHU -> {
val settingVo = Gson().fromJson(sender.jsonSetting, FeishuSetting::class.java)
FeishuUtils.sendMsg(settingVo, msgInfo, rule, logId)
}
TYPE_PUSHPLUS -> {
val settingVo = Gson().fromJson(sender.jsonSetting, PushplusSetting::class.java)
PushplusUtils.sendMsg(settingVo, msgInfo, rule, logId)
}
TYPE_GOTIFY -> {
val settingVo = Gson().fromJson(sender.jsonSetting, GotifySetting::class.java)
GotifyUtils.sendMsg(settingVo, msgInfo, rule, logId)
}
else -> {}
}
TYPE_EMAIL -> {
val settingVo = Gson().fromJson(sender.jsonSetting, EmailSetting::class.java)
EmailUtils.sendMsg(settingVo, msgInfo, rule, logId)
}
TYPE_BARK -> {
val settingVo = Gson().fromJson(sender.jsonSetting, BarkSetting::class.java)
BarkUtils.sendMsg(settingVo, msgInfo, rule, logId)
}
TYPE_WEBHOOK -> {
val settingVo = Gson().fromJson(sender.jsonSetting, WebhookSetting::class.java)
WebhookUtils.sendMsg(settingVo, msgInfo, rule, logId)
}
TYPE_WEWORK_ROBOT -> {
val settingVo = Gson().fromJson(sender.jsonSetting, WeworkRobotSetting::class.java)
WeworkRobotUtils.sendMsg(settingVo, msgInfo, rule, logId)
}
TYPE_WEWORK_AGENT -> {
val settingVo = Gson().fromJson(sender.jsonSetting, WeworkAgentSetting::class.java)
WeworkAgentUtils.sendMsg(settingVo, msgInfo, rule, logId)
}
TYPE_SERVERCHAN -> {
val settingVo = Gson().fromJson(sender.jsonSetting, ServerchanSetting::class.java)
ServerchanUtils.sendMsg(settingVo, msgInfo, rule, logId)
}
TYPE_TELEGRAM -> {
val settingVo = Gson().fromJson(sender.jsonSetting, TelegramSetting::class.java)
TelegramUtils.sendMsg(settingVo, msgInfo, rule, logId)
}
TYPE_SMS -> {
val settingVo = Gson().fromJson(sender.jsonSetting, SmsSetting::class.java)
SmsUtils.sendMsg(settingVo, msgInfo, rule, logId)
}
TYPE_FEISHU -> {
val settingVo = Gson().fromJson(sender.jsonSetting, FeishuSetting::class.java)
FeishuUtils.sendMsg(settingVo, msgInfo, rule, logId)
}
TYPE_PUSHPLUS -> {
val settingVo = Gson().fromJson(sender.jsonSetting, PushplusSetting::class.java)
PushplusUtils.sendMsg(settingVo, msgInfo, rule, logId)
}
TYPE_GOTIFY -> {
val settingVo = Gson().fromJson(sender.jsonSetting, GotifySetting::class.java)
GotifyUtils.sendMsg(settingVo, msgInfo, rule, logId)
}
else -> {}
} catch (e: Exception) {
e.printStackTrace()
updateLogs(logId, 0, e.message.toString())
}
}
//更新转发日志状态
fun updateLogs(logId: Long?, status: Int, response: String) {
//测试的没有记录ID这里取巧了
if (logId == null || logId == 0L) {
//测试的没有记录ID这里取巧了
if (Looper.myLooper() == null) Looper.prepare()
if (status == 2) {
XToastUtils.success(ResUtils.getString(R.string.request_succeeded))
} else {
XToastUtils.error(ResUtils.getString(R.string.request_failed) + response)
}
Looper.loop()
return
}

View File

@ -165,7 +165,7 @@ class WebhookUtils {
client.newCall(requestBuilder.build()).enqueue(object : Callback {
override fun onFailure(call: Call, e: IOException) {
//解决在子线程中调用Toast的异常情况处理
Looper.prepare()
if (Looper.myLooper() == null) Looper.prepare()
e.printStackTrace()
SendUtils.updateLogs(logId, 0, e.message.toString())
//XToastUtils.error(ResUtils.getString(R.string.request_failed) + e.message)
@ -179,12 +179,12 @@ class WebhookUtils {
//返回http状态200即为成功
if (200 == response.code()) {
Looper.prepare()
if (Looper.myLooper() == null) Looper.prepare()
SendUtils.updateLogs(logId, 2, responseStr)
//XToastUtils.success(ResUtils.getString(R.string.request_succeeded))
Looper.loop()
} else {
Looper.prepare()
if (Looper.myLooper() == null) Looper.prepare()
SendUtils.updateLogs(logId, 0, responseStr)
//XToastUtils.error(ResUtils.getString(R.string.request_failed) + response)
Looper.loop()

View File

@ -98,6 +98,7 @@ class SendWorker(
} catch (e: Exception) {
e.printStackTrace()
return@withContext Result.failure(workDataOf("send" to e.message.toString()))
}
return@withContext Result.success()