From 23acf01c61503eee33e2cfde02620997200f665f Mon Sep 17 00:00:00 2001 From: pppscn <35696959@qq.com> Date: Thu, 13 Feb 2025 19:18:05 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=EF=BC=9A=20`{{PHONE=5FAREA}}?= =?UTF-8?q?`=20=E6=A0=87=E7=AD=BE=E7=94=A8=E4=BA=8E=E5=8F=8D=E6=9F=A5`{{FR?= =?UTF-8?q?OM}}`=E5=AF=B9=E5=BA=94=E7=9A=84=E5=BD=92=E5=B1=9E=E5=9C=B0=20#?= =?UTF-8?q?564?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/com/idormy/sms/forwarder/App.kt | 2 + .../idormy/sms/forwarder/entity/MsgInfo.kt | 14 +++++++ .../idormy/sms/forwarder/utils/PhoneUtils.kt | 42 +++++++++++++++++++ app/src/main/res/values-en/strings.xml | 2 + app/src/main/res/values-zh-rCN/strings.xml | 2 + app/src/main/res/values-zh-rTW/strings.xml | 2 + app/src/main/res/values/strings.xml | 3 ++ 7 files changed, 67 insertions(+) diff --git a/app/src/main/java/com/idormy/sms/forwarder/App.kt b/app/src/main/java/com/idormy/sms/forwarder/App.kt index c40e370d..f234a1ea 100644 --- a/app/src/main/java/com/idormy/sms/forwarder/App.kt +++ b/app/src/main/java/com/idormy/sms/forwarder/App.kt @@ -417,6 +417,7 @@ class App : Application(), CactusCallback, Configuration.Provider by Core { getString(R.string.tag_card_slot) to getString(R.string.insert_tag_card_slot), getString(R.string.tag_card_subid) to getString(R.string.insert_tag_card_subid), getString(R.string.tag_contact_name) to getString(R.string.insert_tag_contact_name), + getString(R.string.tag_phone_area) to getString(R.string.insert_tag_phone_area), ) ) CALL_TAG_MAP.clear() @@ -428,6 +429,7 @@ class App : Application(), CactusCallback, Configuration.Provider by Core { getString(R.string.tag_card_subid) to getString(R.string.insert_tag_card_subid), getString(R.string.tag_call_type) to getString(R.string.insert_tag_call_type), getString(R.string.tag_contact_name) to getString(R.string.insert_tag_contact_name), + getString(R.string.tag_phone_area) to getString(R.string.insert_tag_phone_area), ) ) APP_TAG_MAP.clear() diff --git a/app/src/main/java/com/idormy/sms/forwarder/entity/MsgInfo.kt b/app/src/main/java/com/idormy/sms/forwarder/entity/MsgInfo.kt index 0160ed51..c8911f8f 100644 --- a/app/src/main/java/com/idormy/sms/forwarder/entity/MsgInfo.kt +++ b/app/src/main/java/com/idormy/sms/forwarder/entity/MsgInfo.kt @@ -131,6 +131,7 @@ data class MsgInfo( .replaceAppNameTag(from, encoderName) .replaceLocationTag(encoderName) .replaceContactNameTag(encoderName) + .replacePhoneAreaTag(encoderName) .regexReplace(regexReplace) .trim() } @@ -201,6 +202,19 @@ data class MsgInfo( return this.replaceTag(getString(R.string.tag_contact_name), contactName) } + //替换{{PHONE_AREA}}标签 + private fun String.replacePhoneAreaTag(encoderName: String = ""): String { + if (TextUtils.isEmpty(this)) return this + if (this.indexOf(getString(R.string.tag_phone_area)) == -1) return this + + var phoneArea = PhoneUtils.getPhoneArea(from) + when (encoderName) { + "Gson" -> phoneArea = toJsonStr(phoneArea) + "URLEncoder" -> phoneArea = URLEncoder.encode(phoneArea, "UTF-8") + } + return this.replaceTag(getString(R.string.tag_phone_area), phoneArea) + } + //替换{{APP名称}}标签 private fun String.replaceAppNameTag(packageName: String, encoderName: String = ""): String { if (TextUtils.isEmpty(this)) return this diff --git a/app/src/main/java/com/idormy/sms/forwarder/utils/PhoneUtils.kt b/app/src/main/java/com/idormy/sms/forwarder/utils/PhoneUtils.kt index e54d63e0..80cdd701 100644 --- a/app/src/main/java/com/idormy/sms/forwarder/utils/PhoneUtils.kt +++ b/app/src/main/java/com/idormy/sms/forwarder/utils/PhoneUtils.kt @@ -29,6 +29,13 @@ import com.xuexiang.xutil.XUtil import com.xuexiang.xutil.app.IntentUtils import com.xuexiang.xutil.data.DateUtils import com.xuexiang.xutil.resource.ResUtils.getString +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch +import kotlinx.coroutines.runBlocking +import okhttp3.OkHttpClient +import okhttp3.Request +import org.json.JSONObject import java.text.SimpleDateFormat import java.util.Locale import kotlin.math.min @@ -397,6 +404,41 @@ class PhoneUtils private constructor() { return contactInfoList } + // 获取号码归属地 + fun getPhoneArea(phoneNumber: String): String { + val client = OkHttpClient() + val url = "https://cx.shouji.360.cn/phonearea.php?number=$phoneNumber" + val request = Request.Builder().url(url).build() + + var result = getString(R.string.unknown_area) // 默认值 + + // 使用协程来执行网络请求 + runBlocking { + val job = CoroutineScope(Dispatchers.IO).launch { + try { + val response = client.newCall(request).execute() + if (response.isSuccessful) { + val responseData = response.body()?.string() + Log.i(TAG, "getPhoneArea: $responseData") + if (responseData != null) { + val jsonObject = JSONObject(responseData) + val data = jsonObject.getJSONObject("data") + val province = data.getString("province") + val city = data.getString("city") + val sp = data.getString("sp") + result = "$province $city $sp" + } + } + } catch (e: Exception) { + e.printStackTrace() + } + } + job.join() // 等待协程执行完毕 + } + + return result + } + //获取联系人姓名 fun getContactByNumber(phoneNumber: String?): MutableList { val contactInfoList = mutableListOf() diff --git a/app/src/main/res/values-en/strings.xml b/app/src/main/res/values-en/strings.xml index 89c5ee91..ecd299c6 100644 --- a/app/src/main/res/values-en/strings.xml +++ b/app/src/main/res/values-en/strings.xml @@ -28,6 +28,7 @@ IP List Network Status Contact Name + Phone Area Sms Call @@ -350,6 +351,7 @@ Battery Optimization Set it to manual management, including automatic startup, associated startup, and background running Unknown Number + Unknown Area Your phone does not support this setting Set successfully! Can not directly operate the system power saving optimization Settings diff --git a/app/src/main/res/values-zh-rCN/strings.xml b/app/src/main/res/values-zh-rCN/strings.xml index f7ee6ccd..6a78c795 100644 --- a/app/src/main/res/values-zh-rCN/strings.xml +++ b/app/src/main/res/values-zh-rCN/strings.xml @@ -28,6 +28,7 @@ IP地址列表 网络状态 来源姓名 + 来源归属 短信 通话 @@ -351,6 +352,7 @@ 忽略电池优化设置 请设置为手动管理:允许自启动、允许关联启动、允许后台运行 未知号码 + 未知归属地 您的手机不支持此设置 已将省电优化设置为无限制(不优化)! 本界面无法直接操作系统的省电优化设置 diff --git a/app/src/main/res/values-zh-rTW/strings.xml b/app/src/main/res/values-zh-rTW/strings.xml index deabf87d..17c88224 100644 --- a/app/src/main/res/values-zh-rTW/strings.xml +++ b/app/src/main/res/values-zh-rTW/strings.xml @@ -28,6 +28,7 @@ IP地址列表 網路狀態 來源姓名 + 来源歸屬 簡訊 通話 @@ -345,6 +346,7 @@ 忽略電池優化設置 請設置為手動管理:允許自啟動、允許關聯啟動、允許後台運行 未知號碼 + 未知歸屬地 您的手機不支持此設置 已將省電優化設置為無限制(不優化)! 本界面無法直接操作系統的省電優化設置 diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index e9b0fbfe..d3a27441 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -27,6 +27,7 @@ {{IP_LIST}} {{NET_TYPE}} {{CONTACT_NAME}} + {{PHONE_AREA}} 来源号码 短信内容 @@ -56,6 +57,7 @@ IP地址列表 网络状态 来源姓名 + 来源归属 短信 通话 @@ -379,6 +381,7 @@ 忽略电池优化设置 请设置为手动管理:允许自启动、允许关联启动、允许后台运行 未知号码 + 未知归属地 您的手机不支持此设置 已将省电优化设置为无限制(不优化)! 本界面无法直接操作系统的省电优化设置