From 519ca3db5a6350793ccf1648b930443f5d687be4 Mon Sep 17 00:00:00 2001 From: pppscn <35696959@qq.com> Date: Wed, 3 Nov 2021 17:14:42 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=EF=BC=9A=E6=9D=A5=E7=94=B5?= =?UTF-8?q?=E8=BD=AC=E5=8F=91=E5=A2=9E=E5=8A=A0=E8=8E=B7=E5=8F=96=E5=8D=A1?= =?UTF-8?q?=E6=A7=BD=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../receiver/PhoneStateReceiver.java | 162 ++++++++++-------- 1 file changed, 95 insertions(+), 67 deletions(-) diff --git a/app/src/main/java/com/idormy/sms/forwarder/receiver/PhoneStateReceiver.java b/app/src/main/java/com/idormy/sms/forwarder/receiver/PhoneStateReceiver.java index b4d4a4d9..771c123c 100644 --- a/app/src/main/java/com/idormy/sms/forwarder/receiver/PhoneStateReceiver.java +++ b/app/src/main/java/com/idormy/sms/forwarder/receiver/PhoneStateReceiver.java @@ -1,67 +1,95 @@ -package com.idormy.sms.forwarder.receiver; - -import android.content.BroadcastReceiver; -import android.content.Context; -import android.content.Intent; -import android.telephony.TelephonyManager; -import android.text.TextUtils; -import android.util.Log; - -import com.idormy.sms.forwarder.R; -import com.idormy.sms.forwarder.model.PhoneBookEntity; -import com.idormy.sms.forwarder.model.vo.SmsVo; -import com.idormy.sms.forwarder.sender.SendUtil; -import com.idormy.sms.forwarder.utils.ContactHelper; -import com.idormy.sms.forwarder.utils.SettingUtil; - -import java.util.Date; -import java.util.List; - -public class PhoneStateReceiver extends BroadcastReceiver { - private static final String TAG = "PhoneStateReceiver"; - private TelephonyManager mTelephonyManager; - - @Override - public void onReceive(Context context, Intent intent) { - if (!SettingUtil.getSwitchEnablePhone()) { - return; - } - String action = intent.getAction(); - if (TelephonyManager.ACTION_PHONE_STATE_CHANGED.equals(action)) { - String phoneNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER); - if (mTelephonyManager == null) { - mTelephonyManager = - (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); - } - int state = mTelephonyManager.getCallState(); - Log.d(TAG, "onReceive state=" + state + " phoneNumber = " + phoneNumber); - switch (state) { - case TelephonyManager.CALL_STATE_RINGING: - if (!TextUtils.isEmpty(phoneNumber)) { - sendReceiveCallMsg(context, phoneNumber); - } - break; - case TelephonyManager.CALL_STATE_IDLE: - case TelephonyManager.CALL_STATE_OFFHOOK: - break; - } - - } - - } - - private void sendReceiveCallMsg(Context context, String phoneNumber) { - List contacts = ContactHelper.getInstance().getContactByNumber(context, phoneNumber); - String name = ""; - if (contacts != null && contacts.size() > 0) { - PhoneBookEntity phoneBookEntity = contacts.get(0); - name = phoneBookEntity.getName(); - } - if (TextUtils.isEmpty(name)) { - name = context.getString(R.string.unknown_number); - } - SmsVo smsVo = new SmsVo(phoneNumber, name + context.getString(R.string.calling), new Date(), name); - Log.d(TAG, "send_msg" + smsVo.toString()); - SendUtil.send_msg(context, smsVo, 1); - } -} +package com.idormy.sms.forwarder.receiver; + +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.os.Bundle; +import android.telephony.TelephonyManager; +import android.text.TextUtils; +import android.util.Log; + +import com.idormy.sms.forwarder.R; +import com.idormy.sms.forwarder.model.PhoneBookEntity; +import com.idormy.sms.forwarder.model.vo.SmsVo; +import com.idormy.sms.forwarder.sender.SendUtil; +import com.idormy.sms.forwarder.utils.ContactHelper; +import com.idormy.sms.forwarder.utils.SettingUtil; +import com.idormy.sms.forwarder.utils.SimUtil; + +import java.util.Date; +import java.util.List; + +public class PhoneStateReceiver extends BroadcastReceiver { + private static final String TAG = "PhoneStateReceiver"; + private TelephonyManager mTelephonyManager; + + @Override + public void onReceive(Context context, Intent intent) { + if (!SettingUtil.getSwitchEnablePhone()) { + return; + } + + String action = intent.getAction(); + 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); + if (mTelephonyManager == null) { + mTelephonyManager = + (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); + } + int state = mTelephonyManager.getCallState(); + Log.d(TAG, "来电信息:state=" + state + " phoneNumber = " + phoneNumber); + switch (state) { + case TelephonyManager.CALL_STATE_RINGING: + if (!TextUtils.isEmpty(phoneNumber)) { + sendReceiveCallMsg(context, phoneNumber, simId, simInfo); + } + break; + case TelephonyManager.CALL_STATE_IDLE: + case TelephonyManager.CALL_STATE_OFFHOOK: + break; + } + + } + + } + + private void sendReceiveCallMsg(Context context, String phoneNumber, int simId, String simInfo) { + List contacts = ContactHelper.getInstance().getContactByNumber(context, phoneNumber); + String name = ""; + if (contacts != null && contacts.size() > 0) { + PhoneBookEntity phoneBookEntity = contacts.get(0); + name = phoneBookEntity.getName(); + } + if (TextUtils.isEmpty(name)) { + name = context.getString(R.string.unknown_number); + } + SmsVo smsVo = new SmsVo(phoneNumber, name + context.getString(R.string.calling), new Date(), simInfo); + Log.d(TAG, "send_msg" + smsVo.toString()); + SendUtil.send_msg(context, smsVo, simId); + } +}