From ac74a183cd2db615ad50677fceb6600ba8268f31 Mon Sep 17 00:00:00 2001
From: pppscn <35696959@qq.com>
Date: Wed, 22 Nov 2023 21:16:22 +0800
Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=EF=BC=9A=E5=BD=A9=E4=BF=A1?=
=?UTF-8?q?=E8=BD=AC=E5=8F=91=E5=8A=9F=E8=83=BD=EF=BC=88=E8=AF=95=E9=AA=8C?=
=?UTF-8?q?=EF=BC=89=20#351?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
app/src/main/AndroidManifest.xml | 2 +
.../forwarder/fragment/SettingsFragment.kt | 27 ++++--
.../sms/forwarder/receiver/SmsReceiver.kt | 82 ++++++++++++++++++-
3 files changed, 99 insertions(+), 12 deletions(-)
diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml
index 139ec21c..dcbc14ba 100644
--- a/app/src/main/AndroidManifest.xml
+++ b/app/src/main/AndroidManifest.xml
@@ -25,6 +25,8 @@
tools:ignore="ScopedStorage" />
+
+
diff --git a/app/src/main/java/com/idormy/sms/forwarder/fragment/SettingsFragment.kt b/app/src/main/java/com/idormy/sms/forwarder/fragment/SettingsFragment.kt
index e77629a8..5829e429 100644
--- a/app/src/main/java/com/idormy/sms/forwarder/fragment/SettingsFragment.kt
+++ b/app/src/main/java/com/idormy/sms/forwarder/fragment/SettingsFragment.kt
@@ -317,6 +317,10 @@ class SettingsFragment : BaseFragment(), View.OnClickL
if (isChecked) {
//检查权限是否获取
XXPermissions.with(this)
+ // 接收 WAP 推送消息
+ .permission(Permission.RECEIVE_WAP_PUSH)
+ // 接收彩信
+ .permission(Permission.RECEIVE_MMS)
// 接收短信
.permission(Permission.RECEIVE_SMS)
// 发送短信
@@ -1000,14 +1004,7 @@ class SettingsFragment : BaseFragment(), View.OnClickL
if (isChecked) {
XXPermissions.with(this).permission(Permission.ACCESS_COARSE_LOCATION).permission(Permission.ACCESS_FINE_LOCATION).permission(Permission.ACCESS_BACKGROUND_LOCATION).request(object : OnPermissionCallback {
override fun onGranted(permissions: List, all: Boolean) {
- //重启前台服务
- val serviceIntent = Intent(requireContext(), ForegroundService::class.java)
- serviceIntent.action = "START"
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
- requireContext().startForegroundService(serviceIntent)
- } else {
- requireContext().startService(serviceIntent)
- }
+ restartForegroundService()
}
override fun onDenied(permissions: List, never: Boolean) {
@@ -1020,12 +1017,26 @@ class SettingsFragment : BaseFragment(), View.OnClickL
}
SettingUtils.enableLocationTag = false
switchEnableLocationTag.isChecked = false
+ restartForegroundService()
}
})
+ } else {
+ restartForegroundService()
}
}
}
+ //重启前台服务
+ private fun restartForegroundService() {
+ val serviceIntent = Intent(requireContext(), ForegroundService::class.java)
+ serviceIntent.action = "START"
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
+ requireContext().startForegroundService(serviceIntent)
+ } else {
+ requireContext().startService(serviceIntent)
+ }
+ }
+
//获取当前手机品牌
private fun getAutoStartTips(): String {
return when (Build.BRAND.lowercase(Locale.ROOT)) {
diff --git a/app/src/main/java/com/idormy/sms/forwarder/receiver/SmsReceiver.kt b/app/src/main/java/com/idormy/sms/forwarder/receiver/SmsReceiver.kt
index df1becd3..f7181da0 100644
--- a/app/src/main/java/com/idormy/sms/forwarder/receiver/SmsReceiver.kt
+++ b/app/src/main/java/com/idormy/sms/forwarder/receiver/SmsReceiver.kt
@@ -31,13 +31,35 @@ class SmsReceiver : BroadcastReceiver() {
if (SettingUtils.enablePureClientMode) return
//过滤广播
- if (intent.action != Telephony.Sms.Intents.SMS_RECEIVED_ACTION && intent.action != Telephony.Sms.Intents.SMS_DELIVER_ACTION) return
+ if (intent.action != Telephony.Sms.Intents.SMS_RECEIVED_ACTION
+ && intent.action != Telephony.Sms.Intents.SMS_DELIVER_ACTION
+ && intent.action != Telephony.Sms.Intents.WAP_PUSH_RECEIVED_ACTION
+ && intent.action != Telephony.Sms.Intents.WAP_PUSH_DELIVER_ACTION
+ ) return
var from = ""
var message = ""
- for (smsMessage in Telephony.Sms.Intents.getMessagesFromIntent(intent)) {
- from = smsMessage.displayOriginatingAddress
- message += smsMessage.messageBody
+ if (intent.action == Telephony.Sms.Intents.WAP_PUSH_RECEIVED_ACTION || intent.action == Telephony.Sms.Intents.WAP_PUSH_DELIVER_ACTION) {
+ from = intent.getStringExtra("address") ?: ""
+ message = intent.getStringExtra("body") ?: ""
+ Log.d(TAG, "from = $from, message = $message")
+
+ val bundle = intent.extras
+ bundle?.let {
+ for (key in bundle.keySet()) {
+ val obj = bundle.get(key)
+ if (obj is ByteArray) {
+ val data = String(obj)
+ // 解析彩信内容
+ parseMMSContent(data, from)
+ }
+ }
+ }
+ } else {
+ for (smsMessage in Telephony.Sms.Intents.getMessagesFromIntent(intent)) {
+ from = smsMessage.displayOriginatingAddress
+ message += smsMessage.messageBody
+ }
}
Log.d(TAG, "from = $from, message = $message")
@@ -124,4 +146,56 @@ class SmsReceiver : BroadcastReceiver() {
SmsCommandUtils.execute(context, smsCommand)
}
+ private fun parseMMSContent(data: String, sender: String?) {
+ // 在这里实现解析彩信内容的逻辑
+ Log.d("MMSReceiver", "Received MMS from: $sender, Data: $data")
+
+ val parts = data.split("\n\n") // 假设彩信内容以两个换行符 "\n\n" 分隔不同部分
+ parts.forEach { part ->
+ val lines = part.split("\n")
+ var contentType: String? = null
+ var content: String? = null
+ var contentTransferEncoding: String? = null
+
+ lines.forEach { line ->
+ Log.d(TAG, "Line: $line")
+ val keyValue = line.split(":")
+ if (keyValue.size == 2) {
+ val key = keyValue[0].trim()
+ val value = keyValue[1].trim()
+
+ when (key.toLowerCase()) {
+ "content-type" -> contentType = value
+ "content-transfer-encoding" -> contentTransferEncoding = value
+ }
+ }
+ }
+
+ // 处理不同 MIME 类型的内容
+ contentType?.let {
+ when {
+ it.startsWith("text") -> {
+ // 文本内容
+ content = lines.last() // 假设文本内容在当前部分的最后一行
+ // 处理文本内容,例如展示在 TextView 中
+ Log.d(TAG, "Text data: $content")
+ }
+
+ it.startsWith("image") -> {
+ // 图片内容
+ // 如果 contentTransferEncoding 表示 base64 编码
+ if (contentTransferEncoding.equals("base64", true)) {
+ content = lines.last() // 假设图片数据在当前部分的最后一行
+ // 解码图片数据并展示或保存图片
+ val decodedImage = android.util.Base64.decode(content, android.util.Base64.DEFAULT)
+ // 处理解码后的图片数据
+ Log.d(TAG, "Image data: $decodedImage")
+ }
+ }
+ // 可以根据其他 MIME 类型继续添加处理逻辑,比如视频、音频等
+ }
+ }
+ }
+ }
+
}
\ No newline at end of file