From d6b39b09c2a6e0a44779a202260357cfb270fdda Mon Sep 17 00:00:00 2001 From: pppscn <35696959@qq.com> Date: Sun, 7 May 2023 15:06:00 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=EF=BC=9A=E5=8D=A1=E6=A7=BD?= =?UTF-8?q?=E4=BF=A1=E6=81=AF=E8=8E=B7=E5=8F=96=E8=A1=A5=E5=81=BF=E6=9C=BA?= =?UTF-8?q?=E5=88=B6=EF=BC=8C=E4=BF=AE=E5=A4=8D=E8=BF=9C=E7=A8=8B=E5=8F=91?= =?UTF-8?q?=E7=9F=AD=E4=BF=A1=E6=8C=87=E5=AE=9A=E5=8D=A1=E4=B8=8D=E6=98=8E?= =?UTF-8?q?=E7=A1=AE=E9=97=AE=E9=A2=98=20#293?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 格式化代码 --- .../idormy/sms/forwarder/utils/PhoneUtils.kt | 62 +++++++++---------- 1 file changed, 31 insertions(+), 31 deletions(-) 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 7c8ef92b..f836d053 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 @@ -32,7 +32,7 @@ import com.xuexiang.xutil.resource.ResUtils import java.text.SimpleDateFormat import java.util.* -@Suppress("PropertyName", "DEPRECATION") +@Suppress("DEPRECATION") class PhoneUtils private constructor() { companion object { @@ -47,10 +47,10 @@ 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? = mSubscriptionManager.activeSubscriptionInfoList - if (activeSubscriptionInfoList != null && activeSubscriptionInfoList.isNotEmpty()) { + if (!activeSubscriptionInfoList.isNullOrEmpty()) { //1.1.1 有使用的卡,就遍历所有卡 for (subscriptionInfo in activeSubscriptionInfoList) { val simInfo = SimInfo() @@ -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 { @@ -97,7 +97,7 @@ class PhoneUtils private constructor() { //为空,两个卡都没有获取到信息 if (infoList.isEmpty()) { //卡1备注信息不为空 - var etExtraSim1 = SettingUtils.extraSim1 + val etExtraSim1 = SettingUtils.extraSim1 if (!TextUtils.isEmpty(etExtraSim1)) { val simInfo1 = SimInfo() //卡1 @@ -108,7 +108,7 @@ class PhoneUtils private constructor() { infoList[simInfo1.mSimSlotIndex] = simInfo1 } //卡2备注信息不为空 - var etExtraSim2 = SettingUtils.extraSim2 + val etExtraSim2 = SettingUtils.extraSim2 if (!TextUtils.isEmpty(etExtraSim2)) { val simInfo2 = SimInfo() simInfo2.mSimSlotIndex = 1 @@ -120,7 +120,7 @@ class PhoneUtils private constructor() { //有一张卡,判断是卡几 } else { - var infoListIndex = -1; + var infoListIndex = -1 for (obj in infoList) { infoListIndex = obj.key } @@ -177,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) { @@ -192,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() @@ -204,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) @@ -220,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 { val callInfoList: MutableList = mutableListOf() try { @@ -239,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) @@ -280,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) @@ -318,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 { val contactInfoList: MutableList = mutableListOf() @@ -337,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) @@ -352,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) @@ -384,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) { @@ -412,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 { val smsInfoList: MutableList = mutableListOf() try { @@ -431,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() @@ -439,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)