mirror of
https://github.com/pppscn/SmsForwarder
synced 2025-08-04 01:47:40 +08:00
优化:获取来电卡槽信息
This commit is contained in:
parent
967c096e7c
commit
2f5215caec
@ -0,0 +1,40 @@
|
|||||||
|
package com.idormy.sms.forwarder.model;
|
||||||
|
|
||||||
|
import androidx.annotation.NonNull;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public class CallInfo {
|
||||||
|
public String name; //姓名
|
||||||
|
public String number; //号码
|
||||||
|
public Long dateLong; //获取通话日期
|
||||||
|
public int duration;//获取通话时长,值为多少秒
|
||||||
|
public int type; //获取通话类型:1.呼入2.呼出3.未接
|
||||||
|
public int subscriptionId;
|
||||||
|
|
||||||
|
public CallInfo() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public CallInfo(String name, String number, Long dateLong, int duration, int type, int subscriptionId) {
|
||||||
|
this.name = name;
|
||||||
|
this.number = number;
|
||||||
|
this.dateLong = dateLong;
|
||||||
|
this.duration = duration;
|
||||||
|
this.type = type;
|
||||||
|
this.subscriptionId = subscriptionId;
|
||||||
|
}
|
||||||
|
|
||||||
|
@NonNull
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "CallInfo{" +
|
||||||
|
"name='" + name + '\'' +
|
||||||
|
", number='" + number + '\'' +
|
||||||
|
", dateLong=" + dateLong +
|
||||||
|
", duration=" + duration +
|
||||||
|
", type=" + type +
|
||||||
|
", subscriptionId=" + subscriptionId +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
|
}
|
@ -3,16 +3,17 @@ package com.idormy.sms.forwarder.receiver;
|
|||||||
import android.content.BroadcastReceiver;
|
import android.content.BroadcastReceiver;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.os.Bundle;
|
|
||||||
import android.telephony.TelephonyManager;
|
import android.telephony.TelephonyManager;
|
||||||
import android.text.TextUtils;
|
import android.text.TextUtils;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
import com.idormy.sms.forwarder.R;
|
import com.idormy.sms.forwarder.R;
|
||||||
|
import com.idormy.sms.forwarder.model.CallInfo;
|
||||||
import com.idormy.sms.forwarder.model.PhoneBookEntity;
|
import com.idormy.sms.forwarder.model.PhoneBookEntity;
|
||||||
import com.idormy.sms.forwarder.model.vo.SmsVo;
|
import com.idormy.sms.forwarder.model.vo.SmsVo;
|
||||||
import com.idormy.sms.forwarder.sender.SendUtil;
|
import com.idormy.sms.forwarder.sender.SendUtil;
|
||||||
import com.idormy.sms.forwarder.utils.ContactHelper;
|
import com.idormy.sms.forwarder.utils.ContactHelper;
|
||||||
|
import com.idormy.sms.forwarder.utils.PhoneUtils;
|
||||||
import com.idormy.sms.forwarder.utils.SettingUtil;
|
import com.idormy.sms.forwarder.utils.SettingUtil;
|
||||||
import com.idormy.sms.forwarder.utils.SimUtil;
|
import com.idormy.sms.forwarder.utils.SimUtil;
|
||||||
|
|
||||||
@ -31,30 +32,6 @@ public class PhoneStateReceiver extends BroadcastReceiver {
|
|||||||
|
|
||||||
String action = intent.getAction();
|
String action = intent.getAction();
|
||||||
if (TelephonyManager.ACTION_PHONE_STATE_CHANGED.equals(action)) {
|
if (TelephonyManager.ACTION_PHONE_STATE_CHANGED.equals(action)) {
|
||||||
//获取卡槽信息
|
|
||||||
Bundle extras = intent.getExtras();
|
|
||||||
String simInfo = "";
|
|
||||||
//卡槽ID,默认卡槽为1
|
|
||||||
int simId = 1;
|
|
||||||
try {
|
|
||||||
if (extras.containsKey("simId")) {
|
|
||||||
simId = extras.getInt("simId");
|
|
||||||
} else if (extras.containsKey("subscription")) {
|
|
||||||
simId = SimUtil.getSimIdBySubscriptionId(extras.getInt("subscription"));
|
|
||||||
}
|
|
||||||
|
|
||||||
//自定义备注优先
|
|
||||||
simInfo = simId == 2 ? SettingUtil.getAddExtraSim2() : SettingUtil.getAddExtraSim1();
|
|
||||||
if (!simInfo.isEmpty()) {
|
|
||||||
simInfo = "SIM" + simId + "_" + simInfo;
|
|
||||||
} else {
|
|
||||||
simInfo = SimUtil.getSimInfo(simId);
|
|
||||||
}
|
|
||||||
} catch (Exception e) {
|
|
||||||
Log.e(TAG, "获取接收手机号失败:" + e.getMessage());
|
|
||||||
}
|
|
||||||
Log.d(TAG, "卡槽信息:" + simInfo);
|
|
||||||
|
|
||||||
//获取来电号码
|
//获取来电号码
|
||||||
String phoneNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
|
String phoneNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
|
||||||
if (mTelephonyManager == null) {
|
if (mTelephonyManager == null) {
|
||||||
@ -66,7 +43,7 @@ public class PhoneStateReceiver extends BroadcastReceiver {
|
|||||||
switch (state) {
|
switch (state) {
|
||||||
case TelephonyManager.CALL_STATE_RINGING:
|
case TelephonyManager.CALL_STATE_RINGING:
|
||||||
if (!TextUtils.isEmpty(phoneNumber)) {
|
if (!TextUtils.isEmpty(phoneNumber)) {
|
||||||
sendReceiveCallMsg(context, phoneNumber, simId, simInfo);
|
sendReceiveCallMsg(context, phoneNumber);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case TelephonyManager.CALL_STATE_IDLE:
|
case TelephonyManager.CALL_STATE_IDLE:
|
||||||
@ -78,13 +55,34 @@ public class PhoneStateReceiver extends BroadcastReceiver {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void sendReceiveCallMsg(Context context, String phoneNumber, int simId, String simInfo) {
|
private void sendReceiveCallMsg(Context context, String phoneNumber) {
|
||||||
List<PhoneBookEntity> contacts = ContactHelper.getInstance().getContactByNumber(context, phoneNumber);
|
int simId = 0;
|
||||||
String name = "";
|
String name = "";
|
||||||
|
String simInfo = "";
|
||||||
|
|
||||||
|
//获取后一条通话记录
|
||||||
|
CallInfo callInfo = PhoneUtils.getLastCallInfo();
|
||||||
|
if (callInfo != null) {
|
||||||
|
Log.d(TAG, callInfo.toString());
|
||||||
|
|
||||||
|
simId = SimUtil.getSimIdBySubscriptionId(callInfo.getSubscriptionId());
|
||||||
|
//自定义备注优先
|
||||||
|
simInfo = simId == 2 ? SettingUtil.getAddExtraSim2() : SettingUtil.getAddExtraSim1();
|
||||||
|
if (!simInfo.isEmpty()) {
|
||||||
|
simInfo = "SIM" + simId + "_" + simInfo;
|
||||||
|
} else {
|
||||||
|
simInfo = SimUtil.getSimInfo(simId);
|
||||||
|
}
|
||||||
|
|
||||||
|
name = callInfo.getName();
|
||||||
|
} else {
|
||||||
|
List<PhoneBookEntity> contacts = ContactHelper.getInstance().getContactByNumber(context, phoneNumber);
|
||||||
if (contacts != null && contacts.size() > 0) {
|
if (contacts != null && contacts.size() > 0) {
|
||||||
PhoneBookEntity phoneBookEntity = contacts.get(0);
|
PhoneBookEntity phoneBookEntity = contacts.get(0);
|
||||||
name = phoneBookEntity.getName();
|
name = phoneBookEntity.getName();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (TextUtils.isEmpty(name)) {
|
if (TextUtils.isEmpty(name)) {
|
||||||
name = context.getString(R.string.unknown_number);
|
name = context.getString(R.string.unknown_number);
|
||||||
}
|
}
|
||||||
|
@ -76,7 +76,7 @@ public class SmsBroadcastReceiver extends BroadcastReceiver {
|
|||||||
String content = mobileToContent.get(mobile);
|
String content = mobileToContent.get(mobile);
|
||||||
if (content == null) content = "";
|
if (content == null) content = "";
|
||||||
|
|
||||||
content += sms.getMessageBody();//短信内容
|
content += sms.getMessageBody().trim();//短信内容
|
||||||
mobileToContent.put(mobile, content);
|
mobileToContent.put(mobile, content);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -8,6 +8,7 @@ import android.content.pm.PackageManager;
|
|||||||
import android.database.Cursor;
|
import android.database.Cursor;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.os.Build;
|
import android.os.Build;
|
||||||
|
import android.provider.CallLog;
|
||||||
import android.provider.Settings;
|
import android.provider.Settings;
|
||||||
import android.telephony.SubscriptionInfo;
|
import android.telephony.SubscriptionInfo;
|
||||||
import android.telephony.SubscriptionManager;
|
import android.telephony.SubscriptionManager;
|
||||||
@ -19,6 +20,8 @@ import androidx.annotation.NonNull;
|
|||||||
import androidx.annotation.Nullable;
|
import androidx.annotation.Nullable;
|
||||||
import androidx.core.app.ActivityCompat;
|
import androidx.core.app.ActivityCompat;
|
||||||
|
|
||||||
|
import com.idormy.sms.forwarder.model.CallInfo;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
@ -527,6 +530,37 @@ public class PhoneUtils {
|
|||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取后一条通话记录
|
||||||
|
*/
|
||||||
|
public static CallInfo getLastCallInfo() {
|
||||||
|
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP_MR1) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
String[] columns = {CallLog.Calls.CACHED_NAME// 通话记录的联系人
|
||||||
|
, CallLog.Calls.NUMBER// 通话记录的电话号码
|
||||||
|
, CallLog.Calls.DATE// 通话记录的日期
|
||||||
|
, CallLog.Calls.DURATION// 通话时长
|
||||||
|
, CallLog.Calls.TYPE// 通话类型
|
||||||
|
, CallLog.Calls.PHONE_ACCOUNT_ID
|
||||||
|
};
|
||||||
|
|
||||||
|
@SuppressLint("Recycle") Cursor cursor = context.getContentResolver().query(CallLog.Calls.CONTENT_URI, null, null, null, CallLog.Calls.DATE + " DESC");
|
||||||
|
while (cursor.moveToNext()) {
|
||||||
|
return new CallInfo(
|
||||||
|
cursor.getString(cursor.getColumnIndex(CallLog.Calls.CACHED_NAME)), //姓名
|
||||||
|
cursor.getString(cursor.getColumnIndex(CallLog.Calls.NUMBER)), //号码
|
||||||
|
cursor.getLong(cursor.getColumnIndex(CallLog.Calls.DATE)), //获取通话日期
|
||||||
|
cursor.getInt(cursor.getColumnIndex(CallLog.Calls.DURATION)),//获取通话时长,值为多少秒
|
||||||
|
cursor.getInt(cursor.getColumnIndex(CallLog.Calls.TYPE)), //获取通话类型:1.呼入2.呼出3.未接
|
||||||
|
cursor.getInt(cursor.getColumnIndex(CallLog.Calls.PHONE_ACCOUNT_ID))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* SIM 卡信息
|
* SIM 卡信息
|
||||||
*/
|
*/
|
||||||
|
@ -1,3 +1,3 @@
|
|||||||
#Fri Jul 16 10:33:23 CST 2021
|
#Fri Jul 16 10:33:23 CST 2021
|
||||||
versionName=2.2.0
|
versionName=2.2.1
|
||||||
versionCode=30
|
versionCode=31
|
||||||
|
Loading…
x
Reference in New Issue
Block a user