优化:自动消除通知仅消除已匹配的通知 #204(临时方案,重复查询换取准确性)

This commit is contained in:
pppscn 2022-09-22 14:11:20 +08:00
parent 73ce800e09
commit c2df047dc1
5 changed files with 30 additions and 43 deletions

View File

@ -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<ActivityMainBinding?>(),
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<ActivityMainBinding?>(),
}
//按返回键不退出回到桌面
@Deprecated("Deprecated in Java")
override fun onBackPressed() {
val intent = Intent(Intent.ACTION_MAIN)
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK

View File

@ -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<RuleAndSender>
@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<Rule>
//TODO:允许主线程访问,后面再优化
@Query("SELECT * FROM Rule ORDER BY id ASC")
fun getAll(): List<Rule>

View File

@ -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)

View File

@ -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<SendWorker>()
.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<Rule> = 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<SendWorker>()
.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
}*/
}

View File

@ -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<RuleAndSender> = 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()))