From c2df047dc1b36f7ed24559adca823b8c7a84c53e Mon Sep 17 00:00:00 2001 From: pppscn <35696959@qq.com> Date: Thu, 22 Sep 2022 14:11:20 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=EF=BC=9A=E8=87=AA=E5=8A=A8?= =?UTF-8?q?=E6=B6=88=E9=99=A4=E9=80=9A=E7=9F=A5=E4=BB=85=E6=B6=88=E9=99=A4?= =?UTF-8?q?=E5=B7=B2=E5=8C=B9=E9=85=8D=E7=9A=84=E9=80=9A=E7=9F=A5=20#204?= =?UTF-8?q?=EF=BC=88=E4=B8=B4=E6=97=B6=E6=96=B9=E6=A1=88=EF=BC=8C=E9=87=8D?= =?UTF-8?q?=E5=A4=8D=E6=9F=A5=E8=AF=A2=E6=8D=A2=E5=8F=96=E5=87=86=E7=A1=AE?= =?UTF-8?q?=E6=80=A7=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../sms/forwarder/activity/MainActivity.kt | 10 ++-- .../sms/forwarder/database/dao/RuleDao.kt | 4 ++ .../database/repository/RuleRepository.kt | 2 + .../sms/forwarder/service/NotifyService.kt | 48 ++++++++----------- .../sms/forwarder/workers/SendWorker.kt | 9 ---- 5 files changed, 30 insertions(+), 43 deletions(-) diff --git a/app/src/main/java/com/idormy/sms/forwarder/activity/MainActivity.kt b/app/src/main/java/com/idormy/sms/forwarder/activity/MainActivity.kt index d8a3ab32..1a864c9a 100644 --- a/app/src/main/java/com/idormy/sms/forwarder/activity/MainActivity.kt +++ b/app/src/main/java/com/idormy/sms/forwarder/activity/MainActivity.kt @@ -46,7 +46,6 @@ import com.xuexiang.xui.utils.WidgetUtils import com.xuexiang.xui.widget.dialog.materialdialog.DialogAction import com.xuexiang.xui.widget.dialog.materialdialog.GravityEnum import com.xuexiang.xui.widget.dialog.materialdialog.MaterialDialog -import com.xuexiang.xutil.common.CollectionUtils import com.xuexiang.xutil.file.FileUtils import frpclib.Frpclib import io.reactivex.CompletableObserver @@ -200,14 +199,14 @@ class MainActivity : BaseActivity(), val item = binding!!.includeMain.bottomNavigation.menu.getItem(position) binding!!.includeMain.toolbar.title = item.title binding!!.includeMain.toolbar.menu.clear() - when { - getString(R.string.menu_rules) == item.title -> binding!!.includeMain.toolbar.inflateMenu( + when (item.title) { + getString(R.string.menu_rules) -> binding!!.includeMain.toolbar.inflateMenu( R.menu.menu_rules ) - getString(R.string.menu_senders) == item.title -> binding!!.includeMain.toolbar.inflateMenu( + getString(R.string.menu_senders) -> binding!!.includeMain.toolbar.inflateMenu( R.menu.menu_senders ) - getString(R.string.menu_settings) == item.title -> binding!!.includeMain.toolbar.inflateMenu( + getString(R.string.menu_settings) -> binding!!.includeMain.toolbar.inflateMenu( R.menu.menu_settings ) else -> binding!!.includeMain.toolbar.inflateMenu(R.menu.menu_logs) @@ -349,6 +348,7 @@ class MainActivity : BaseActivity(), } //按返回键不退出回到桌面 + @Deprecated("Deprecated in Java") override fun onBackPressed() { val intent = Intent(Intent.ACTION_MAIN) intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK diff --git a/app/src/main/java/com/idormy/sms/forwarder/database/dao/RuleDao.kt b/app/src/main/java/com/idormy/sms/forwarder/database/dao/RuleDao.kt index 899d9e33..14614364 100644 --- a/app/src/main/java/com/idormy/sms/forwarder/database/dao/RuleDao.kt +++ b/app/src/main/java/com/idormy/sms/forwarder/database/dao/RuleDao.kt @@ -46,6 +46,10 @@ interface RuleDao { @Query("SELECT * FROM Rule where type=:type and status=:status and (sim_slot='ALL' or sim_slot=:simSlot)") suspend fun getRuleAndSender(type: String, status: Int, simSlot: String): List + @Transaction + @Query("SELECT * FROM Rule where type=:type and status=:status and (sim_slot='ALL' or sim_slot=:simSlot)") + fun getRuleList(type: String, status: Int, simSlot: String): List + //TODO:允许主线程访问,后面再优化 @Query("SELECT * FROM Rule ORDER BY id ASC") fun getAll(): List diff --git a/app/src/main/java/com/idormy/sms/forwarder/database/repository/RuleRepository.kt b/app/src/main/java/com/idormy/sms/forwarder/database/repository/RuleRepository.kt index d53114f2..e8924446 100644 --- a/app/src/main/java/com/idormy/sms/forwarder/database/repository/RuleRepository.kt +++ b/app/src/main/java/com/idormy/sms/forwarder/database/repository/RuleRepository.kt @@ -26,6 +26,8 @@ class RuleRepository( suspend fun getRuleAndSender(type: String, status: Int, simSlot: String) = ruleDao.getRuleAndSender(type, status, simSlot) + fun getRuleList(type: String, status: Int, simSlot: String) = ruleDao.getRuleList(type, status, simSlot) + @WorkerThread fun update(rule: Rule) = ruleDao.update(rule) diff --git a/app/src/main/java/com/idormy/sms/forwarder/service/NotifyService.kt b/app/src/main/java/com/idormy/sms/forwarder/service/NotifyService.kt index 93a9336b..278bbfc8 100644 --- a/app/src/main/java/com/idormy/sms/forwarder/service/NotifyService.kt +++ b/app/src/main/java/com/idormy/sms/forwarder/service/NotifyService.kt @@ -9,6 +9,8 @@ import androidx.work.OneTimeWorkRequestBuilder import androidx.work.WorkManager import androidx.work.workDataOf import com.google.gson.Gson +import com.idormy.sms.forwarder.core.Core +import com.idormy.sms.forwarder.database.entity.Rule import com.idormy.sms.forwarder.entity.MsgInfo import com.idormy.sms.forwarder.utils.PACKAGE_NAME import com.idormy.sms.forwarder.utils.SettingUtils @@ -20,14 +22,11 @@ import java.util.* @Suppress("PrivatePropertyName", "DEPRECATION") -class NotifyService : NotificationListenerService()/*, LifecycleOwner*/ { +class NotifyService : NotificationListenerService() { - /*private val mRegistry = LifecycleRegistry(this)*/ private val TAG: String = "NotifyService" override fun onListenerConnected() { - //super.onListenerConnected() - // Check if Notification Listener Permission is allowed Log.d(TAG, "onListenerConnected") } @@ -81,37 +80,32 @@ class NotifyService : NotificationListenerService()/*, LifecycleOwner*/ { if (TextUtils.isEmpty(title) && TextUtils.isEmpty(text)) return val msgInfo = MsgInfo("app", from, text, Date(), title, -1) - val request = OneTimeWorkRequestBuilder() - .setInputData( - workDataOf( - Worker.sendMsgInfo to Gson().toJson(msgInfo), - //Worker.sendSbnId to sbn.id - ) - ) - .build() - WorkManager.getInstance(applicationContext).enqueue(request) - //TODO:收不到返回信息,自动消除待解决 - /*WorkManager.getInstance(context).getWorkInfoByIdLiveData(request.id) - .observe(this) { info: WorkInfo? -> - Log.e(TAG, info.toString()) - if (info != null && info.state == WorkInfo.State.SUCCEEDED) { + //TODO:自动消除通知(临时方案,重复查询换取准确性) + if (SettingUtils.enableCancelAppNotify) { + val ruleList: List = Core.rule.getRuleList(msgInfo.type, 1, "SIM0") + for (rule in ruleList) { + if (rule.checkMsg(msgInfo)) { + Log.d(TAG, "自动消除通知") if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { cancelNotification(sbn.key) } else { cancelNotification(sbn.packageName, sbn.tag, sbn.id) } + break } - }*/ - //自动消除全部通知(临时方案) - if (SettingUtils.enableCancelAppNotify) { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { - cancelNotification(sbn.key) - } else { - cancelNotification(sbn.packageName, sbn.tag, sbn.id) } } + val request = OneTimeWorkRequestBuilder() + .setInputData( + workDataOf( + Worker.sendMsgInfo to Gson().toJson(msgInfo), + ) + ) + .build() + WorkManager.getInstance(applicationContext).enqueue(request) + } catch (e: Exception) { Log.e(TAG, "Parsing Notification failed: " + e.message.toString()) } @@ -119,11 +113,7 @@ class NotifyService : NotificationListenerService()/*, LifecycleOwner*/ { } override fun onNotificationRemoved(sbn: StatusBarNotification?) { - //super.onNotificationRemoved(sbn) Log.d(TAG, "Removed Package Name : ${sbn?.packageName}") } - /*override fun getLifecycle(): Lifecycle { - return mRegistry - }*/ } \ No newline at end of file diff --git a/app/src/main/java/com/idormy/sms/forwarder/workers/SendWorker.kt b/app/src/main/java/com/idormy/sms/forwarder/workers/SendWorker.kt index d2bab5ff..a2012fe4 100644 --- a/app/src/main/java/com/idormy/sms/forwarder/workers/SendWorker.kt +++ b/app/src/main/java/com/idormy/sms/forwarder/workers/SendWorker.kt @@ -73,7 +73,6 @@ class SendWorker( HistoryUtils.put(key, timestamp) } - //val sendSbnId = inputData.getInt(Worker.sendSbnId, 0) //【注意】卡槽id:-1=获取失败、0=卡槽1、1=卡槽2,但是 Rule 表里存的是 SIM1/SIM2 val simSlot = "SIM" + (msgInfo.simSlot + 1) val ruleList: List = Core.rule.getRuleAndSender(msgInfo.type, 1, simSlot) @@ -81,21 +80,13 @@ class SendWorker( return@withContext Result.failure(workDataOf("send" to "failed")) } - //var matchNum = 0 for (rule in ruleList) { if (!rule.rule.checkMsg(msgInfo)) continue - //matchNum++ val log = Logs(0, msgInfo.type, msgInfo.from, msgInfo.content, rule.rule.id, msgInfo.simInfo) val logId = Core.logs.insert(log) SendUtils.sendMsgSender(msgInfo, rule.rule, rule.sender, logId) } - //TODO:自动消除通知 - /*if (matchNum > 0 && sendSbnId != 0 && SettingUtils.enableCancelAppNotify) { - Log.e("SendWorker", "自动消除通知") - return@withContext Result.success(workDataOf("matchNum" to matchNum)) - }*/ - } catch (e: Exception) { e.printStackTrace() return@withContext Result.failure(workDataOf("send" to e.message.toString()))