mirror of
https://github.com/pppscn/SmsForwarder
synced 2025-08-03 09:27:41 +08:00
修复:通话转发卡槽信息识别错误
This commit is contained in:
parent
fc45b7fc5b
commit
cabad8b14e
@ -45,9 +45,14 @@ class PhoneUtils private constructor() {
|
|||||||
try {
|
try {
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
|
||||||
println("1.版本超过5.1,调用系统方法")
|
println("1.版本超过5.1,调用系统方法")
|
||||||
val mSubscriptionManager = XUtil.getContext().getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE) as SubscriptionManager
|
val mSubscriptionManager = XUtil.getContext()
|
||||||
ActivityCompat.checkSelfPermission(XUtil.getContext(), permission.READ_PHONE_STATE)
|
.getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE) as SubscriptionManager
|
||||||
val activeSubscriptionInfoList: List<SubscriptionInfo>? = mSubscriptionManager.activeSubscriptionInfoList
|
ActivityCompat.checkSelfPermission(
|
||||||
|
XUtil.getContext(),
|
||||||
|
permission.READ_PHONE_STATE
|
||||||
|
)
|
||||||
|
val activeSubscriptionInfoList: List<SubscriptionInfo>? =
|
||||||
|
mSubscriptionManager.activeSubscriptionInfoList
|
||||||
if (activeSubscriptionInfoList != null && activeSubscriptionInfoList.isNotEmpty()) {
|
if (activeSubscriptionInfoList != null && activeSubscriptionInfoList.isNotEmpty()) {
|
||||||
//1.1.1 有使用的卡,就遍历所有卡
|
//1.1.1 有使用的卡,就遍历所有卡
|
||||||
for (subscriptionInfo in activeSubscriptionInfoList) {
|
for (subscriptionInfo in activeSubscriptionInfoList) {
|
||||||
@ -66,11 +71,31 @@ class PhoneUtils private constructor() {
|
|||||||
println("2.版本低于5.1的系统,首先调用数据库,看能不能访问到")
|
println("2.版本低于5.1的系统,首先调用数据库,看能不能访问到")
|
||||||
val uri = Uri.parse("content://telephony/siminfo") //访问raw_contacts表
|
val uri = Uri.parse("content://telephony/siminfo") //访问raw_contacts表
|
||||||
val resolver: ContentResolver = XUtil.getContext().contentResolver
|
val resolver: ContentResolver = XUtil.getContext().contentResolver
|
||||||
val cursor = resolver.query(uri, arrayOf("_id", "icc_id", "sim_id", "display_name", "carrier_name", "name_source", "color", "number", "display_number_format", "data_roaming", "mcc", "mnc"), null, null, null)
|
val cursor = resolver.query(
|
||||||
|
uri,
|
||||||
|
arrayOf(
|
||||||
|
"_id",
|
||||||
|
"icc_id",
|
||||||
|
"sim_id",
|
||||||
|
"display_name",
|
||||||
|
"carrier_name",
|
||||||
|
"name_source",
|
||||||
|
"color",
|
||||||
|
"number",
|
||||||
|
"display_number_format",
|
||||||
|
"data_roaming",
|
||||||
|
"mcc",
|
||||||
|
"mnc"
|
||||||
|
),
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
null
|
||||||
|
)
|
||||||
if (cursor != null && cursor.moveToFirst()) {
|
if (cursor != null && cursor.moveToFirst()) {
|
||||||
do {
|
do {
|
||||||
val simInfo = SimInfo()
|
val simInfo = SimInfo()
|
||||||
simInfo.mCarrierName = cursor.getString(cursor.getColumnIndex("carrier_name"))
|
simInfo.mCarrierName =
|
||||||
|
cursor.getString(cursor.getColumnIndex("carrier_name"))
|
||||||
simInfo.mIccId = cursor.getString(cursor.getColumnIndex("icc_id"))
|
simInfo.mIccId = cursor.getString(cursor.getColumnIndex("icc_id"))
|
||||||
simInfo.mSimSlotIndex = cursor.getInt(cursor.getColumnIndex("sim_id"))
|
simInfo.mSimSlotIndex = cursor.getInt(cursor.getColumnIndex("sim_id"))
|
||||||
simInfo.mNumber = cursor.getString(cursor.getColumnIndex("number"))
|
simInfo.mNumber = cursor.getString(cursor.getColumnIndex("number"))
|
||||||
@ -116,10 +141,15 @@ class PhoneUtils private constructor() {
|
|||||||
val mobileArray = mobiles.split(";".toRegex()).toTypedArray()
|
val mobileArray = mobiles.split(";".toRegex()).toTypedArray()
|
||||||
for (mobile in mobileArray) {
|
for (mobile in mobileArray) {
|
||||||
try {
|
try {
|
||||||
val sendFlags = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) PendingIntent.FLAG_IMMUTABLE else PendingIntent.FLAG_ONE_SHOT
|
val sendFlags =
|
||||||
val sendPI = PendingIntent.getBroadcast(XUtil.getContext(), 0, Intent(), sendFlags)
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) PendingIntent.FLAG_IMMUTABLE else PendingIntent.FLAG_ONE_SHOT
|
||||||
|
val sendPI =
|
||||||
|
PendingIntent.getBroadcast(XUtil.getContext(), 0, Intent(), sendFlags)
|
||||||
|
|
||||||
val smsManager = if (subId > -1 && Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) SmsManager.getSmsManagerForSubscriptionId(subId) else SmsManager.getDefault()
|
val smsManager =
|
||||||
|
if (subId > -1 && Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) SmsManager.getSmsManagerForSubscriptionId(
|
||||||
|
subId
|
||||||
|
) else SmsManager.getDefault()
|
||||||
// Android 5.1.1 以下使用反射指定卡槽
|
// Android 5.1.1 以下使用反射指定卡槽
|
||||||
if (subId > -1 && Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP_MR1) {
|
if (subId > -1 && Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP_MR1) {
|
||||||
Log.d(TAG, "Android 5.1.1 以下使用反射指定卡槽")
|
Log.d(TAG, "Android 5.1.1 以下使用反射指定卡槽")
|
||||||
@ -131,8 +161,14 @@ class PhoneUtils private constructor() {
|
|||||||
|
|
||||||
// 切割长短信
|
// 切割长短信
|
||||||
if (message.length >= 70) {
|
if (message.length >= 70) {
|
||||||
val deliverFlags = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) PendingIntent.FLAG_IMMUTABLE else 0
|
val deliverFlags =
|
||||||
val deliverPI = PendingIntent.getBroadcast(XUtil.getContext(), 0, Intent("DELIVERED_SMS_ACTION"), deliverFlags)
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) PendingIntent.FLAG_IMMUTABLE else 0
|
||||||
|
val deliverPI = PendingIntent.getBroadcast(
|
||||||
|
XUtil.getContext(),
|
||||||
|
0,
|
||||||
|
Intent("DELIVERED_SMS_ACTION"),
|
||||||
|
deliverFlags
|
||||||
|
)
|
||||||
|
|
||||||
val sentPendingIntents = ArrayList<PendingIntent>()
|
val sentPendingIntents = ArrayList<PendingIntent>()
|
||||||
val deliveredPendingIntents = ArrayList<PendingIntent>()
|
val deliveredPendingIntents = ArrayList<PendingIntent>()
|
||||||
@ -142,7 +178,13 @@ class PhoneUtils private constructor() {
|
|||||||
sentPendingIntents.add(i, sendPI)
|
sentPendingIntents.add(i, sendPI)
|
||||||
deliveredPendingIntents.add(i, deliverPI)
|
deliveredPendingIntents.add(i, deliverPI)
|
||||||
}
|
}
|
||||||
smsManager.sendMultipartTextMessage(mobile, null, divideContents, sentPendingIntents, deliveredPendingIntents)
|
smsManager.sendMultipartTextMessage(
|
||||||
|
mobile,
|
||||||
|
null,
|
||||||
|
divideContents,
|
||||||
|
sentPendingIntents,
|
||||||
|
deliveredPendingIntents
|
||||||
|
)
|
||||||
} else {
|
} else {
|
||||||
smsManager.sendTextMessage(mobile, null, message, sendPI, null)
|
smsManager.sendTextMessage(mobile, null, message, sendPI, null)
|
||||||
}
|
}
|
||||||
@ -156,7 +198,12 @@ class PhoneUtils private constructor() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//获取通话记录列表
|
//获取通话记录列表
|
||||||
fun getCallInfoList(type: Int, limit: Int, offset: Int, phoneNumber: String?): MutableList<CallInfo> {
|
fun getCallInfoList(
|
||||||
|
type: Int,
|
||||||
|
limit: Int,
|
||||||
|
offset: Int,
|
||||||
|
phoneNumber: String?
|
||||||
|
): MutableList<CallInfo> {
|
||||||
val callInfoList: MutableList<CallInfo> = mutableListOf()
|
val callInfoList: MutableList<CallInfo> = mutableListOf()
|
||||||
try {
|
try {
|
||||||
var selection = "1=1"
|
var selection = "1=1"
|
||||||
@ -186,19 +233,24 @@ class PhoneUtils private constructor() {
|
|||||||
if (cursor.count == 0 || offset >= cursor.count) return callInfoList
|
if (cursor.count == 0 || offset >= cursor.count) return callInfoList
|
||||||
|
|
||||||
if (cursor.moveToFirst()) {
|
if (cursor.moveToFirst()) {
|
||||||
|
Log.d(TAG, "Call ColumnNames=${cursor.columnNames.contentToString()}")
|
||||||
val indexName = cursor.getColumnIndex(CallLog.Calls.CACHED_NAME)
|
val indexName = cursor.getColumnIndex(CallLog.Calls.CACHED_NAME)
|
||||||
val indexNumber = cursor.getColumnIndex(CallLog.Calls.NUMBER)
|
val indexNumber = cursor.getColumnIndex(CallLog.Calls.NUMBER)
|
||||||
val indexDate = cursor.getColumnIndex(CallLog.Calls.DATE)
|
val indexDate = cursor.getColumnIndex(CallLog.Calls.DATE)
|
||||||
val indexDuration = cursor.getColumnIndex(CallLog.Calls.DURATION)
|
val indexDuration = cursor.getColumnIndex(CallLog.Calls.DURATION)
|
||||||
val indexType = cursor.getColumnIndex(CallLog.Calls.TYPE)
|
val indexType = cursor.getColumnIndex(CallLog.Calls.TYPE)
|
||||||
val indexViaNumber = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && cursor.getColumnIndex("via_number") != -1) cursor.getColumnIndex("via_number") else -1
|
val indexViaNumber =
|
||||||
val isSimId = false
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && cursor.getColumnIndex(
|
||||||
|
"via_number"
|
||||||
|
) != -1
|
||||||
|
) cursor.getColumnIndex("via_number") else -1
|
||||||
|
//TODO:卡槽识别,这里需要适配机型
|
||||||
|
var isSimId = false
|
||||||
var indexSimId = -1
|
var indexSimId = -1
|
||||||
if (cursor.getColumnIndex("simid") != -1) {
|
if (cursor.getColumnIndex("simid") != -1) { //MIUI系统必须用这个字段
|
||||||
indexSimId = cursor.getColumnIndex("simid")
|
indexSimId = cursor.getColumnIndex("simid")
|
||||||
} else if (cursor.getColumnIndex(CallLog.Calls.PHONE_ACCOUNT_ID) != -1
|
isSimId = true
|
||||||
&& Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP
|
} else if (cursor.getColumnIndex(CallLog.Calls.PHONE_ACCOUNT_ID) != -1) {
|
||||||
) {
|
|
||||||
indexSimId = cursor.getColumnIndex(CallLog.Calls.PHONE_ACCOUNT_ID)
|
indexSimId = cursor.getColumnIndex(CallLog.Calls.PHONE_ACCOUNT_ID)
|
||||||
}
|
}
|
||||||
var curOffset = 0
|
var curOffset = 0
|
||||||
@ -211,7 +263,10 @@ class PhoneUtils private constructor() {
|
|||||||
cursor.getInt(indexDuration), //获取通话时长,值为多少秒
|
cursor.getInt(indexDuration), //获取通话时长,值为多少秒
|
||||||
cursor.getInt(indexType), //获取通话类型:1.呼入 2.呼出 3.未接
|
cursor.getInt(indexType), //获取通话类型:1.呼入 2.呼出 3.未接
|
||||||
if (indexViaNumber != -1) cursor.getString(indexViaNumber) else "", //来源号码
|
if (indexViaNumber != -1) cursor.getString(indexViaNumber) else "", //来源号码
|
||||||
if (indexSimId != -1) getSimId(cursor.getInt(indexSimId), isSimId) else -1 //卡槽id
|
if (indexSimId != -1) getSimId(
|
||||||
|
cursor.getInt(indexSimId),
|
||||||
|
isSimId
|
||||||
|
) else -1 //卡槽id
|
||||||
)
|
)
|
||||||
Log.d(TAG, callInfo.toString())
|
Log.d(TAG, callInfo.toString())
|
||||||
callInfoList.add(callInfo)
|
callInfoList.add(callInfo)
|
||||||
@ -241,7 +296,12 @@ class PhoneUtils private constructor() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//获取联系人列表
|
//获取联系人列表
|
||||||
fun getContactInfoList(limit: Int, offset: Int, phoneNumber: String?, name: String?): MutableList<ContactInfo> {
|
fun getContactInfoList(
|
||||||
|
limit: Int,
|
||||||
|
offset: Int,
|
||||||
|
phoneNumber: String?,
|
||||||
|
name: String?
|
||||||
|
): MutableList<ContactInfo> {
|
||||||
val contactInfoList: MutableList<ContactInfo> = mutableListOf()
|
val contactInfoList: MutableList<ContactInfo> = mutableListOf()
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@ -271,8 +331,10 @@ class PhoneUtils private constructor() {
|
|||||||
if (cursor.count == 0 || offset >= cursor.count) return contactInfoList
|
if (cursor.count == 0 || offset >= cursor.count) return contactInfoList
|
||||||
|
|
||||||
if (cursor.moveToFirst()) {
|
if (cursor.moveToFirst()) {
|
||||||
val displayNameIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)
|
val displayNameIndex =
|
||||||
val mobileNoIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)
|
cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)
|
||||||
|
val mobileNoIndex =
|
||||||
|
cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)
|
||||||
do {
|
do {
|
||||||
val contactInfo = ContactInfo(
|
val contactInfo = ContactInfo(
|
||||||
cursor.getString(displayNameIndex), //姓名
|
cursor.getString(displayNameIndex), //姓名
|
||||||
@ -305,8 +367,14 @@ class PhoneUtils private constructor() {
|
|||||||
fun getCallMsg(callInfo: CallInfo): String {
|
fun getCallMsg(callInfo: CallInfo): String {
|
||||||
val sb = StringBuilder()
|
val sb = StringBuilder()
|
||||||
sb.append(ResUtils.getString(R.string.linkman)).append(callInfo.name).append("\n")
|
sb.append(ResUtils.getString(R.string.linkman)).append(callInfo.name).append("\n")
|
||||||
if (!TextUtils.isEmpty(callInfo.viaNumber)) sb.append(ResUtils.getString(R.string.via_number)).append(callInfo.viaNumber).append("\n")
|
if (!TextUtils.isEmpty(callInfo.viaNumber)) sb.append(ResUtils.getString(R.string.via_number))
|
||||||
if (callInfo.dateLong > 0L) sb.append(ResUtils.getString(R.string.call_date)).append(DateUtils.millis2String(callInfo.dateLong, SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault()))).append("\n")
|
.append(callInfo.viaNumber).append("\n")
|
||||||
|
if (callInfo.dateLong > 0L) sb.append(ResUtils.getString(R.string.call_date)).append(
|
||||||
|
DateUtils.millis2String(
|
||||||
|
callInfo.dateLong,
|
||||||
|
SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault())
|
||||||
|
)
|
||||||
|
).append("\n")
|
||||||
if (callInfo.duration > 0) {
|
if (callInfo.duration > 0) {
|
||||||
if (callInfo.type == 3) {
|
if (callInfo.type == 3) {
|
||||||
sb.append(ResUtils.getString(R.string.ring_duration))
|
sb.append(ResUtils.getString(R.string.ring_duration))
|
||||||
@ -327,7 +395,12 @@ class PhoneUtils private constructor() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 获取用户短信列表
|
// 获取用户短信列表
|
||||||
fun getSmsInfoList(type: Int, limit: Int, offset: Int, keyword: String): MutableList<SmsInfo> {
|
fun getSmsInfoList(
|
||||||
|
type: Int,
|
||||||
|
limit: Int,
|
||||||
|
offset: Int,
|
||||||
|
keyword: String
|
||||||
|
): MutableList<SmsInfo> {
|
||||||
val smsInfoList: MutableList<SmsInfo> = mutableListOf()
|
val smsInfoList: MutableList<SmsInfo> = mutableListOf()
|
||||||
try {
|
try {
|
||||||
var selection = "1=1"
|
var selection = "1=1"
|
||||||
@ -365,21 +438,27 @@ class PhoneUtils private constructor() {
|
|||||||
if (cursor.count == 0) return smsInfoList
|
if (cursor.count == 0) return smsInfoList
|
||||||
|
|
||||||
if (cursor.moveToFirst()) {
|
if (cursor.moveToFirst()) {
|
||||||
|
Log.d(TAG, "SMS ColumnNames=${cursor.columnNames.contentToString()}")
|
||||||
val indexAddress = cursor.getColumnIndex("address")
|
val indexAddress = cursor.getColumnIndex("address")
|
||||||
val indexBody = cursor.getColumnIndex("body")
|
val indexBody = cursor.getColumnIndex("body")
|
||||||
val indexDate = cursor.getColumnIndex("date")
|
val indexDate = cursor.getColumnIndex("date")
|
||||||
val indexType = cursor.getColumnIndex("type")
|
val indexType = cursor.getColumnIndex("type")
|
||||||
val isSimId = false
|
//TODO:卡槽识别,这里需要适配机型
|
||||||
|
var isSimId = false
|
||||||
var indexSimId = -1
|
var indexSimId = -1
|
||||||
if (cursor.getColumnIndex("sim_id") != -1) {
|
if (cursor.getColumnIndex("sim_id") != -1) { //MIUI系统必须用这个字段
|
||||||
indexSimId = cursor.getColumnIndex("sim_id")
|
indexSimId = cursor.getColumnIndex("sim_id")
|
||||||
|
isSimId = true
|
||||||
|
} else if (cursor.getColumnIndex("sub_id") != -1) {
|
||||||
|
indexSimId = cursor.getColumnIndex("sub_id")
|
||||||
}
|
}
|
||||||
do {
|
do {
|
||||||
val smsInfo = SmsInfo()
|
val smsInfo = SmsInfo()
|
||||||
val phoneNumber = cursor.getString(indexAddress)
|
val phoneNumber = cursor.getString(indexAddress)
|
||||||
// 根据手机号码查询用户名
|
// 根据手机号码查询用户名
|
||||||
val contacts = getContactByNumber(phoneNumber)
|
val contacts = getContactByNumber(phoneNumber)
|
||||||
smsInfo.name = if (contacts.isNotEmpty()) contacts[0].name else ResUtils.getString(R.string.unknown_number)
|
smsInfo.name =
|
||||||
|
if (contacts.isNotEmpty()) contacts[0].name else ResUtils.getString(R.string.unknown_number)
|
||||||
// 联系人号码
|
// 联系人号码
|
||||||
smsInfo.number = phoneNumber
|
smsInfo.number = phoneNumber
|
||||||
// 短信内容
|
// 短信内容
|
||||||
@ -389,7 +468,10 @@ class PhoneUtils private constructor() {
|
|||||||
// 短信类型: 1=接收, 2=发送
|
// 短信类型: 1=接收, 2=发送
|
||||||
smsInfo.type = cursor.getInt(indexType)
|
smsInfo.type = cursor.getInt(indexType)
|
||||||
// 卡槽id
|
// 卡槽id
|
||||||
smsInfo.simId = if (indexSimId != -1) getSimId(cursor.getInt(indexSimId), isSimId) else -1
|
smsInfo.simId = if (indexSimId != -1) getSimId(
|
||||||
|
cursor.getInt(indexSimId),
|
||||||
|
isSimId
|
||||||
|
) else -1
|
||||||
smsInfoList.add(smsInfo)
|
smsInfoList.add(smsInfo)
|
||||||
} while (cursor.moveToNext())
|
} while (cursor.moveToNext())
|
||||||
if (!cursor.isClosed) cursor.close()
|
if (!cursor.isClosed) cursor.close()
|
||||||
@ -431,7 +513,21 @@ class PhoneUtils private constructor() {
|
|||||||
*/
|
*/
|
||||||
private fun getSimId(mId: Int, isSimId: Boolean): Int {
|
private fun getSimId(mId: Int, isSimId: Boolean): Int {
|
||||||
Log.i(TAG, "mId = $mId, isSimId = $isSimId")
|
Log.i(TAG, "mId = $mId, isSimId = $isSimId")
|
||||||
if (isSimId) return mId
|
//if (isSimId) return mId
|
||||||
|
|
||||||
|
/**
|
||||||
|
* TODO:特别处理
|
||||||
|
* MIUI系统:simId 字段实际为 subscription_id
|
||||||
|
* EMUI系统:subscription_id 实际为 simId
|
||||||
|
*/
|
||||||
|
val manufacturer = Build.MANUFACTURER.lowercase(Locale.getDefault())
|
||||||
|
Log.i(TAG, "manufacturer = $manufacturer")
|
||||||
|
if (isSimId && !manufacturer.contains(Regex(pattern = "xiaomi|redmi"))) {
|
||||||
|
return mId
|
||||||
|
}
|
||||||
|
if (!isSimId && manufacturer.contains(Regex(pattern = "huawei|honor"))) {
|
||||||
|
return mId
|
||||||
|
}
|
||||||
|
|
||||||
//获取卡槽信息
|
//获取卡槽信息
|
||||||
if (App.SimInfoList.isEmpty()) {
|
if (App.SimInfoList.isEmpty()) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user