新增:卡槽信息获取补偿机制,修复远程发短信指定卡不明确问题 #293

格式化代码
This commit is contained in:
pppscn 2023-05-07 15:06:00 +08:00
parent cdf0cae0cf
commit d6b39b09c2

View File

@ -32,7 +32,7 @@ import com.xuexiang.xutil.resource.ResUtils
import java.text.SimpleDateFormat import java.text.SimpleDateFormat
import java.util.* import java.util.*
@Suppress("PropertyName", "DEPRECATION") @Suppress("DEPRECATION")
class PhoneUtils private constructor() { class PhoneUtils private constructor() {
companion object { companion object {
@ -47,10 +47,10 @@ class PhoneUtils private constructor() {
println("1.版本超过5.1,调用系统方法") println("1.版本超过5.1,调用系统方法")
val mSubscriptionManager = XUtil.getContext().getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE) as SubscriptionManager val mSubscriptionManager = XUtil.getContext().getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE) as SubscriptionManager
ActivityCompat.checkSelfPermission( ActivityCompat.checkSelfPermission(
XUtil.getContext(), permission.READ_PHONE_STATE XUtil.getContext(), permission.READ_PHONE_STATE
) )
val activeSubscriptionInfoList: List<SubscriptionInfo>? = mSubscriptionManager.activeSubscriptionInfoList val activeSubscriptionInfoList: List<SubscriptionInfo>? = mSubscriptionManager.activeSubscriptionInfoList
if (activeSubscriptionInfoList != null && activeSubscriptionInfoList.isNotEmpty()) { if (!activeSubscriptionInfoList.isNullOrEmpty()) {
//1.1.1 有使用的卡,就遍历所有卡 //1.1.1 有使用的卡,就遍历所有卡
for (subscriptionInfo in activeSubscriptionInfoList) { for (subscriptionInfo in activeSubscriptionInfoList) {
val simInfo = SimInfo() val simInfo = SimInfo()
@ -69,9 +69,9 @@ class PhoneUtils private constructor() {
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( 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" "_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()) { if (cursor != null && cursor.moveToFirst()) {
do { do {
@ -97,7 +97,7 @@ class PhoneUtils private constructor() {
//为空,两个卡都没有获取到信息 //为空,两个卡都没有获取到信息
if (infoList.isEmpty()) { if (infoList.isEmpty()) {
//卡1备注信息不为空 //卡1备注信息不为空
var etExtraSim1 = SettingUtils.extraSim1 val etExtraSim1 = SettingUtils.extraSim1
if (!TextUtils.isEmpty(etExtraSim1)) { if (!TextUtils.isEmpty(etExtraSim1)) {
val simInfo1 = SimInfo() val simInfo1 = SimInfo()
//卡1 //卡1
@ -108,7 +108,7 @@ class PhoneUtils private constructor() {
infoList[simInfo1.mSimSlotIndex] = simInfo1 infoList[simInfo1.mSimSlotIndex] = simInfo1
} }
//卡2备注信息不为空 //卡2备注信息不为空
var etExtraSim2 = SettingUtils.extraSim2 val etExtraSim2 = SettingUtils.extraSim2
if (!TextUtils.isEmpty(etExtraSim2)) { if (!TextUtils.isEmpty(etExtraSim2)) {
val simInfo2 = SimInfo() val simInfo2 = SimInfo()
simInfo2.mSimSlotIndex = 1 simInfo2.mSimSlotIndex = 1
@ -120,7 +120,7 @@ class PhoneUtils private constructor() {
//有一张卡,判断是卡几 //有一张卡,判断是卡几
} else { } else {
var infoListIndex = -1; var infoListIndex = -1
for (obj in infoList) { for (obj in infoList) {
infoListIndex = obj.key infoListIndex = obj.key
} }
@ -177,7 +177,7 @@ class PhoneUtils private constructor() {
val sendPI = PendingIntent.getBroadcast(XUtil.getContext(), 0, Intent(), sendFlags) 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( val smsManager = if (subId > -1 && Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) SmsManager.getSmsManagerForSubscriptionId(
subId subId
) else SmsManager.getDefault() ) 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) {
@ -192,7 +192,7 @@ 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 = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) PendingIntent.FLAG_IMMUTABLE else 0
val deliverPI = PendingIntent.getBroadcast( val deliverPI = PendingIntent.getBroadcast(
XUtil.getContext(), 0, Intent("DELIVERED_SMS_ACTION"), deliverFlags XUtil.getContext(), 0, Intent("DELIVERED_SMS_ACTION"), deliverFlags
) )
val sentPendingIntents = ArrayList<PendingIntent>() val sentPendingIntents = ArrayList<PendingIntent>()
@ -204,7 +204,7 @@ class PhoneUtils private constructor() {
deliveredPendingIntents.add(i, deliverPI) deliveredPendingIntents.add(i, deliverPI)
} }
smsManager.sendMultipartTextMessage( smsManager.sendMultipartTextMessage(
mobile, null, divideContents, sentPendingIntents, deliveredPendingIntents mobile, null, divideContents, sentPendingIntents, deliveredPendingIntents
) )
} else { } else {
smsManager.sendTextMessage(mobile, null, message, sendPI, null) smsManager.sendTextMessage(mobile, null, message, sendPI, null)
@ -220,7 +220,7 @@ class PhoneUtils private constructor() {
//获取通话记录列表 //获取通话记录列表
fun getCallInfoList( fun getCallInfoList(
type: Int, limit: Int, offset: Int, phoneNumber: String? type: Int, limit: Int, offset: Int, phoneNumber: String?
): MutableList<CallInfo> { ): MutableList<CallInfo> {
val callInfoList: MutableList<CallInfo> = mutableListOf() val callInfoList: MutableList<CallInfo> = mutableListOf()
try { try {
@ -239,7 +239,7 @@ class PhoneUtils private constructor() {
//为了兼容性这里全部取出后手动分页 //为了兼容性这里全部取出后手动分页
val cursor = Core.app.contentResolver.query( 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 ) ?: return callInfoList
Log.i(TAG, "cursor count:" + cursor.count) Log.i(TAG, "cursor count:" + cursor.count)
@ -280,14 +280,14 @@ class PhoneUtils private constructor() {
do { do {
if (curOffset >= offset) { if (curOffset >= offset) {
val callInfo = CallInfo( val callInfo = CallInfo(
cursor.getString(indexName) ?: "", //姓名 cursor.getString(indexName) ?: "", //姓名
cursor.getString(indexNumber) ?: "", //号码 cursor.getString(indexNumber) ?: "", //号码
cursor.getLong(indexDate), //获取通话日期 cursor.getLong(indexDate), //获取通话日期
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 0=Sim1, 1=Sim2, -1=获取失败 if (indexSimId != -1) getSimId(cursor.getInt(indexSimId), isSimId) else -1, //卡槽ID 0=Sim1, 1=Sim2, -1=获取失败
if (indexSubId != -1) cursor.getInt(indexSubId) else 0, //卡槽主键 if (indexSubId != -1) cursor.getInt(indexSubId) else 0, //卡槽主键
) )
Log.d(TAG, callInfo.toString()) Log.d(TAG, callInfo.toString())
callInfoList.add(callInfo) callInfoList.add(callInfo)
@ -318,7 +318,7 @@ class PhoneUtils private constructor() {
//获取联系人列表 //获取联系人列表
fun getContactInfoList( fun getContactInfoList(
limit: Int, offset: Int, phoneNumber: String?, name: String? limit: Int, offset: Int, phoneNumber: String?, name: String?
): MutableList<ContactInfo> { ): MutableList<ContactInfo> {
val contactInfoList: MutableList<ContactInfo> = mutableListOf() val contactInfoList: MutableList<ContactInfo> = mutableListOf()
@ -337,7 +337,7 @@ class PhoneUtils private constructor() {
Log.d(TAG, "selectionArgs = $selectionArgs") Log.d(TAG, "selectionArgs = $selectionArgs")
val cursor = Core.app.contentResolver.query( 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 ) ?: return contactInfoList
Log.i(TAG, "cursor count:" + cursor.count) Log.i(TAG, "cursor count:" + cursor.count)
@ -352,8 +352,8 @@ class PhoneUtils private constructor() {
val mobileNoIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER) val mobileNoIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)
do { do {
val contactInfo = ContactInfo( val contactInfo = ContactInfo(
cursor.getString(displayNameIndex), //姓名 cursor.getString(displayNameIndex), //姓名
cursor.getString(mobileNoIndex), //号码 cursor.getString(mobileNoIndex), //号码
) )
Log.d(TAG, contactInfo.toString()) Log.d(TAG, contactInfo.toString())
contactInfoList.add(contactInfo) contactInfoList.add(contactInfo)
@ -384,9 +384,9 @@ class PhoneUtils private constructor() {
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)).append(callInfo.viaNumber).append("\n")
if (callInfo.dateLong > 0L) sb.append(ResUtils.getString(R.string.call_date)).append( if (callInfo.dateLong > 0L) sb.append(ResUtils.getString(R.string.call_date)).append(
DateUtils.millis2String( DateUtils.millis2String(
callInfo.dateLong, SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault()) callInfo.dateLong, SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault())
) )
).append("\n") ).append("\n")
if (callInfo.duration > 0) { if (callInfo.duration > 0) {
if (callInfo.type == 3) { if (callInfo.type == 3) {
@ -412,7 +412,7 @@ class PhoneUtils private constructor() {
// 获取用户短信列表 // 获取用户短信列表
fun getSmsInfoList( fun getSmsInfoList(
type: Int, limit: Int, offset: Int, keyword: String type: Int, limit: Int, offset: Int, keyword: String
): MutableList<SmsInfo> { ): MutableList<SmsInfo> {
val smsInfoList: MutableList<SmsInfo> = mutableListOf() val smsInfoList: MutableList<SmsInfo> = mutableListOf()
try { try {
@ -431,7 +431,7 @@ class PhoneUtils private constructor() {
// 避免超过总数后循环取出 // 避免超过总数后循环取出
val cursorTotal = Core.app.contentResolver.query( 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 ) ?: return smsInfoList
if (offset >= cursorTotal.count) { if (offset >= cursorTotal.count) {
cursorTotal.close() cursorTotal.close()
@ -439,7 +439,7 @@ class PhoneUtils private constructor() {
} }
val cursor = Core.app.contentResolver.query( 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 ) ?: return smsInfoList
Log.i(TAG, "cursor count:" + cursor.count) Log.i(TAG, "cursor count:" + cursor.count)