添加卡槽信息获取补偿机制,修复远程发短信指定卡不明确问题 (#293)

This commit is contained in:
tomcat927 2023-05-07 15:02:19 +08:00 committed by GitHub
parent 42794a5b2c
commit cdf0cae0cf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -47,7 +47,7 @@ class PhoneUtils private constructor() {
println("1.版本超过5.1,调用系统方法")
val mSubscriptionManager = XUtil.getContext().getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE) as SubscriptionManager
ActivityCompat.checkSelfPermission(
XUtil.getContext(), permission.READ_PHONE_STATE
XUtil.getContext(), permission.READ_PHONE_STATE
)
val activeSubscriptionInfoList: List<SubscriptionInfo>? = mSubscriptionManager.activeSubscriptionInfoList
if (activeSubscriptionInfoList != null && activeSubscriptionInfoList.isNotEmpty()) {
@ -69,9 +69,9 @@ class PhoneUtils private constructor() {
val uri = Uri.parse("content://telephony/siminfo") //访问raw_contacts表
val resolver: ContentResolver = XUtil.getContext().contentResolver
val cursor = resolver.query(
uri, arrayOf(
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
), null, null, null
)
if (cursor != null && cursor.moveToFirst()) {
do {
@ -91,6 +91,57 @@ class PhoneUtils private constructor() {
} catch (e: java.lang.Exception) {
e.printStackTrace()
}
//仍然获取不到/只获取到一个->取出备注
if (infoList.isEmpty() || infoList.size == 1) {
println("3.直接取出备注框的数据作为信息")
//为空,两个卡都没有获取到信息
if (infoList.isEmpty()) {
//卡1备注信息不为空
var etExtraSim1 = SettingUtils.extraSim1
if (!TextUtils.isEmpty(etExtraSim1)) {
val simInfo1 = SimInfo()
//卡1
simInfo1.mSimSlotIndex = 0
simInfo1.mNumber = etExtraSim1
simInfo1.mSubscriptionId = SettingUtils.subidSim1
//把卡放入
infoList[simInfo1.mSimSlotIndex] = simInfo1
}
//卡2备注信息不为空
var etExtraSim2 = SettingUtils.extraSim2
if (!TextUtils.isEmpty(etExtraSim2)) {
val simInfo2 = SimInfo()
simInfo2.mSimSlotIndex = 1
simInfo2.mNumber = etExtraSim2
simInfo2.mSubscriptionId = SettingUtils.subidSim2
//把卡放入
infoList[simInfo2.mSimSlotIndex] = simInfo2
}
//有一张卡,判断是卡几
} else {
var infoListIndex = -1;
for (obj in infoList) {
infoListIndex = obj.key
}
//获取到卡1且卡2备注信息不为空
if (infoListIndex == 0 && !TextUtils.isEmpty(SettingUtils.extraSim2)) {
//获取到卡1信息卡2备注不为空创建卡2实体
val simInfo2 = SimInfo()
simInfo2.mSimSlotIndex = 1
simInfo2.mNumber = SettingUtils.extraSim2
simInfo2.mSubscriptionId = SettingUtils.subidSim1
infoList[simInfo2.mSimSlotIndex] = simInfo2
} else if (infoListIndex == 1 && !TextUtils.isEmpty(SettingUtils.extraSim1)) {
//获取到卡2信息卡1备注不为空创建卡1实体
val simInfo1 = SimInfo()
simInfo1.mSimSlotIndex = 0
simInfo1.mNumber = SettingUtils.extraSim1
simInfo1.mSubscriptionId = SettingUtils.subidSim1
infoList[simInfo1.mSimSlotIndex] = simInfo1
}
}
}
Log.e(TAG, infoList.toString())
return infoList
}
@ -126,7 +177,7 @@ class PhoneUtils private constructor() {
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
subId
) else SmsManager.getDefault()
// Android 5.1.1 以下使用反射指定卡槽
if (subId > -1 && Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP_MR1) {
@ -141,7 +192,7 @@ class PhoneUtils private constructor() {
if (message.length >= 70) {
val 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
XUtil.getContext(), 0, Intent("DELIVERED_SMS_ACTION"), deliverFlags
)
val sentPendingIntents = ArrayList<PendingIntent>()
@ -153,7 +204,7 @@ class PhoneUtils private constructor() {
deliveredPendingIntents.add(i, deliverPI)
}
smsManager.sendMultipartTextMessage(
mobile, null, divideContents, sentPendingIntents, deliveredPendingIntents
mobile, null, divideContents, sentPendingIntents, deliveredPendingIntents
)
} else {
smsManager.sendTextMessage(mobile, null, message, sendPI, null)
@ -169,7 +220,7 @@ class PhoneUtils private constructor() {
//获取通话记录列表
fun getCallInfoList(
type: Int, limit: Int, offset: Int, phoneNumber: String?
type: Int, limit: Int, offset: Int, phoneNumber: String?
): MutableList<CallInfo> {
val callInfoList: MutableList<CallInfo> = mutableListOf()
try {
@ -188,7 +239,7 @@ class PhoneUtils private constructor() {
//为了兼容性这里全部取出后手动分页
val cursor = Core.app.contentResolver.query(
CallLog.Calls.CONTENT_URI, null, selection, selectionArgs.toTypedArray(), CallLog.Calls.DEFAULT_SORT_ORDER // + " limit $limit offset $offset"
CallLog.Calls.CONTENT_URI, null, selection, selectionArgs.toTypedArray(), CallLog.Calls.DEFAULT_SORT_ORDER // + " limit $limit offset $offset"
) ?: return callInfoList
Log.i(TAG, "cursor count:" + cursor.count)
@ -229,14 +280,14 @@ class PhoneUtils private constructor() {
do {
if (curOffset >= offset) {
val callInfo = CallInfo(
cursor.getString(indexName) ?: "", //姓名
cursor.getString(indexNumber) ?: "", //号码
cursor.getLong(indexDate), //获取通话日期
cursor.getInt(indexDuration), //获取通话时长,值为多少秒
cursor.getInt(indexType), //获取通话类型1.呼入 2.呼出 3.未接
if (indexViaNumber != -1) cursor.getString(indexViaNumber) else "", //来源号码
if (indexSimId != -1) getSimId(cursor.getInt(indexSimId), isSimId) else -1, //卡槽ID 0=Sim1, 1=Sim2, -1=获取失败
if (indexSubId != -1) cursor.getInt(indexSubId) else 0, //卡槽主键
cursor.getString(indexName) ?: "", //姓名
cursor.getString(indexNumber) ?: "", //号码
cursor.getLong(indexDate), //获取通话日期
cursor.getInt(indexDuration), //获取通话时长,值为多少秒
cursor.getInt(indexType), //获取通话类型1.呼入 2.呼出 3.未接
if (indexViaNumber != -1) cursor.getString(indexViaNumber) else "", //来源号码
if (indexSimId != -1) getSimId(cursor.getInt(indexSimId), isSimId) else -1, //卡槽ID 0=Sim1, 1=Sim2, -1=获取失败
if (indexSubId != -1) cursor.getInt(indexSubId) else 0, //卡槽主键
)
Log.d(TAG, callInfo.toString())
callInfoList.add(callInfo)
@ -267,7 +318,7 @@ class PhoneUtils private constructor() {
//获取联系人列表
fun getContactInfoList(
limit: Int, offset: Int, phoneNumber: String?, name: String?
limit: Int, offset: Int, phoneNumber: String?, name: String?
): MutableList<ContactInfo> {
val contactInfoList: MutableList<ContactInfo> = mutableListOf()
@ -286,7 +337,7 @@ class PhoneUtils private constructor() {
Log.d(TAG, "selectionArgs = $selectionArgs")
val cursor = Core.app.contentResolver.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, selection, selectionArgs.toTypedArray(), ContactsContract.CommonDataKinds.Phone.SORT_KEY_PRIMARY
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, selection, selectionArgs.toTypedArray(), ContactsContract.CommonDataKinds.Phone.SORT_KEY_PRIMARY
) ?: return contactInfoList
Log.i(TAG, "cursor count:" + cursor.count)
@ -301,8 +352,8 @@ class PhoneUtils private constructor() {
val mobileNoIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)
do {
val contactInfo = ContactInfo(
cursor.getString(displayNameIndex), //姓名
cursor.getString(mobileNoIndex), //号码
cursor.getString(displayNameIndex), //姓名
cursor.getString(mobileNoIndex), //号码
)
Log.d(TAG, contactInfo.toString())
contactInfoList.add(contactInfo)
@ -333,9 +384,9 @@ class PhoneUtils private constructor() {
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 (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())
)
DateUtils.millis2String(
callInfo.dateLong, SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault())
)
).append("\n")
if (callInfo.duration > 0) {
if (callInfo.type == 3) {
@ -361,7 +412,7 @@ class PhoneUtils private constructor() {
// 获取用户短信列表
fun getSmsInfoList(
type: Int, limit: Int, offset: Int, keyword: String
type: Int, limit: Int, offset: Int, keyword: String
): MutableList<SmsInfo> {
val smsInfoList: MutableList<SmsInfo> = mutableListOf()
try {
@ -380,7 +431,7 @@ class PhoneUtils private constructor() {
// 避免超过总数后循环取出
val cursorTotal = Core.app.contentResolver.query(
Uri.parse("content://sms/"), null, selection, selectionArgs.toTypedArray(), "date desc"
Uri.parse("content://sms/"), null, selection, selectionArgs.toTypedArray(), "date desc"
) ?: return smsInfoList
if (offset >= cursorTotal.count) {
cursorTotal.close()
@ -388,7 +439,7 @@ class PhoneUtils private constructor() {
}
val cursor = Core.app.contentResolver.query(
Uri.parse("content://sms/"), null, selection, selectionArgs.toTypedArray(), "date desc limit $limit offset $offset"
Uri.parse("content://sms/"), null, selection, selectionArgs.toTypedArray(), "date desc limit $limit offset $offset"
) ?: return smsInfoList
Log.i(TAG, "cursor count:" + cursor.count)