From c16c6bba74ddb46f7ff750f7691ed3102083b19f Mon Sep 17 00:00:00 2001 From: ZhanGSKen Date: Fri, 21 Feb 2025 09:47:43 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8E=A5=E6=94=B6=E7=94=B5=E8=AF=9D=E6=97=B6?= =?UTF-8?q?=E9=A2=84=E5=85=88=E9=9D=99=E9=9F=B3=E6=A8=A1=E5=BC=8F=E8=B0=83?= =?UTF-8?q?=E8=AF=95=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- contacts/build.properties | 4 +- contacts/src/main/AndroidManifest.xml | 8 +- .../cc/winboll/studio/contacts/dun/Rules.java | 23 ++++++ .../phonecallui/PhoneCallManager.java | 5 +- .../phonecallui/PhoneCallService.java | 50 ++++++++----- .../contacts/receivers/PhoneReceiver.java | 34 --------- .../contacts/services/PhoneCallService.java | 75 ------------------- winboll-shared/build.properties | 4 +- 8 files changed, 64 insertions(+), 139 deletions(-) create mode 100644 contacts/src/main/java/cc/winboll/studio/contacts/dun/Rules.java delete mode 100644 contacts/src/main/java/cc/winboll/studio/contacts/receivers/PhoneReceiver.java delete mode 100644 contacts/src/main/java/cc/winboll/studio/contacts/services/PhoneCallService.java diff --git a/contacts/build.properties b/contacts/build.properties index 7638e8d..d73febf 100644 --- a/contacts/build.properties +++ b/contacts/build.properties @@ -1,8 +1,8 @@ #Created by .winboll/winboll_app_build.gradle -#Thu Feb 20 22:09:07 GMT 2025 +#Fri Feb 21 01:46:02 GMT 2025 stageCount=0 libraryProject=winboll-shared baseVersion=1.0 publishVersion=1.0.0 -buildCount=126 +buildCount=175 baseBetaVersion=1.0.1 diff --git a/contacts/src/main/AndroidManifest.xml b/contacts/src/main/AndroidManifest.xml index bf357cb..cd9ce54 100644 --- a/contacts/src/main/AndroidManifest.xml +++ b/contacts/src/main/AndroidManifest.xml @@ -38,7 +38,6 @@ @@ -50,7 +49,6 @@ - + + @@ -164,7 +164,7 @@ - + - - diff --git a/contacts/src/main/java/cc/winboll/studio/contacts/dun/Rules.java b/contacts/src/main/java/cc/winboll/studio/contacts/dun/Rules.java new file mode 100644 index 0000000..8f78c1f --- /dev/null +++ b/contacts/src/main/java/cc/winboll/studio/contacts/dun/Rules.java @@ -0,0 +1,23 @@ +package cc.winboll.studio.contacts.dun; + +/** + * @Author ZhanGSKen@AliYun.Com + * @Date 2025/02/21 06:15:10 + * @Describe 云盾防御规则 + */ +import java.util.regex.Pattern; + +public class Rules { + + public static final String TAG = "Rules"; + + public static boolean isValidPhoneNumber(String phoneNumber) { + // 中国手机号码正则表达式,以1开头,第二位可以是3、4、5、6、7、8、9,后面跟9位数字 + String regex = "^1[3-9]\\d{9}$"; + return Pattern.matches(regex, phoneNumber); + } + + public static boolean isAllowed(String phoneNumber) { + return isValidPhoneNumber(phoneNumber); + } +} diff --git a/contacts/src/main/java/cc/winboll/studio/contacts/phonecallui/PhoneCallManager.java b/contacts/src/main/java/cc/winboll/studio/contacts/phonecallui/PhoneCallManager.java index 848a9f5..5cb6e9b 100644 --- a/contacts/src/main/java/cc/winboll/studio/contacts/phonecallui/PhoneCallManager.java +++ b/contacts/src/main/java/cc/winboll/studio/contacts/phonecallui/PhoneCallManager.java @@ -5,8 +5,8 @@ import android.media.AudioManager; import android.os.Build; import android.telecom.Call; import android.telecom.VideoProfile; - import androidx.annotation.RequiresApi; +import cc.winboll.studio.contacts.dun.Rules; @RequiresApi(api = Build.VERSION_CODES.M) @@ -19,7 +19,6 @@ public class PhoneCallManager { public PhoneCallManager(Context context) { this.context = context; - audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); } @@ -28,6 +27,8 @@ public class PhoneCallManager { */ public void answer() { if (call != null) { + Call.Details details = call.getDetails(); + String phoneNumber = details.getHandle().getSchemeSpecificPart(); call.answer(VideoProfile.STATE_AUDIO_ONLY); openSpeaker(); } diff --git a/contacts/src/main/java/cc/winboll/studio/contacts/phonecallui/PhoneCallService.java b/contacts/src/main/java/cc/winboll/studio/contacts/phonecallui/PhoneCallService.java index 0418425..67f3e3f 100644 --- a/contacts/src/main/java/cc/winboll/studio/contacts/phonecallui/PhoneCallService.java +++ b/contacts/src/main/java/cc/winboll/studio/contacts/phonecallui/PhoneCallService.java @@ -1,14 +1,5 @@ package cc.winboll.studio.contacts.phonecallui; -import android.os.Build; -import android.telecom.Call; -import android.telecom.InCallService; - -import androidx.annotation.RequiresApi; - -import cc.winboll.studio.contacts.ActivityStack; - - /** * 监听电话通信状态的服务,实现该类的同时必须提供电话管理的 UI * @@ -16,24 +7,32 @@ import cc.winboll.studio.contacts.ActivityStack; * @see PhoneCallActivity * @see android.telecom.InCallService */ +import android.media.AudioManager; +import android.os.Build; +import android.telecom.Call; +import android.telecom.InCallService; +import androidx.annotation.RequiresApi; +import cc.winboll.studio.contacts.ActivityStack; +import cc.winboll.studio.contacts.dun.Rules; + @RequiresApi(api = Build.VERSION_CODES.M) public class PhoneCallService extends InCallService { - + + private int originalRingVolume; + private final Call.Callback callback = new Call.Callback() { @Override public void onStateChanged(Call call, int state) { super.onStateChanged(call, state); - switch (state) { case Call.STATE_ACTIVE: { - - break; - } + break; + } case Call.STATE_DISCONNECTED: { - ActivityStack.getInstance().finishActivity(PhoneCallActivity.class); - break; - } + ActivityStack.getInstance().finishActivity(PhoneCallActivity.class); + break; + } } } @@ -42,10 +41,9 @@ public class PhoneCallService extends InCallService { @Override public void onCallAdded(Call call) { super.onCallAdded(call); - + call.registerCallback(callback); PhoneCallManager.call = call; - CallType callType = null; if (call.getState() == Call.STATE_RINGING) { @@ -57,6 +55,17 @@ public class PhoneCallService extends InCallService { if (callType != null) { Call.Details details = call.getDetails(); String phoneNumber = details.getHandle().getSchemeSpecificPart(); + + // 记录原始铃声音量 + AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE); + originalRingVolume = audioManager.getStreamVolume(AudioManager.STREAM_RING); + if (!Rules.isAllowed(phoneNumber)) { + // 预先静音 + audioManager.setStreamVolume(AudioManager.STREAM_RING, 0, 0); + call.disconnect(); + return; + } + PhoneCallActivity.actionStart(this, phoneNumber, callType); } } @@ -67,6 +76,9 @@ public class PhoneCallService extends InCallService { call.unregisterCallback(callback); PhoneCallManager.call = null; + AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE); + // 恢复铃声音量 + audioManager.setStreamVolume(AudioManager.STREAM_RING, originalRingVolume, 0); } public enum CallType { diff --git a/contacts/src/main/java/cc/winboll/studio/contacts/receivers/PhoneReceiver.java b/contacts/src/main/java/cc/winboll/studio/contacts/receivers/PhoneReceiver.java deleted file mode 100644 index d7647a3..0000000 --- a/contacts/src/main/java/cc/winboll/studio/contacts/receivers/PhoneReceiver.java +++ /dev/null @@ -1,34 +0,0 @@ -package cc.winboll.studio.contacts.receivers; - -/** - * @Author ZhanGSKen@AliYun.Com - * @Date 2025/02/20 17:54:55 - * @Describe PhoneCallReceiver - */ -//import android.content.BroadcastReceiver; -//import android.content.Context; -//import android.content.Intent; -//import android.telephony.TelephonyManager; -//import android.util.Log; -//import cc.winboll.studio.contacts.services.PhoneCallService; -// -//public class PhoneReceiver extends BroadcastReceiver { -// -// public static final String TAG = "PhoneReceiver"; -// -// @Override -// public void onReceive(Context context, Intent intent) { -// if (intent.getAction().equals(TelephonyManager.ACTION_PHONE_STATE_CHANGED)) { -// String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE); -// if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) { -// String phoneNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER); -// Log.d(TAG, "Incoming call from: " + phoneNumber); -// -// // 启动服务来处理电话接听 -// Intent serviceIntent = new Intent(context, PhoneCallService.class); -// serviceIntent.putExtra("phoneNumber", phoneNumber); -// context.startService(serviceIntent); -// } -// } -// } -//} diff --git a/contacts/src/main/java/cc/winboll/studio/contacts/services/PhoneCallService.java b/contacts/src/main/java/cc/winboll/studio/contacts/services/PhoneCallService.java deleted file mode 100644 index 5a3921a..0000000 --- a/contacts/src/main/java/cc/winboll/studio/contacts/services/PhoneCallService.java +++ /dev/null @@ -1,75 +0,0 @@ -package cc.winboll.studio.contacts.services; - -/** - * @Author ZhanGSKen@AliYun.Com - * @Date 2025/02/20 19:58:02 - * @Describe MyPhoneCallService - */ -import android.telecom.Call; -import android.telecom.InCallService; -import cc.winboll.studio.contacts.PhoneCallManager; - -public class PhoneCallService extends InCallService { - - public static final String TAG = "PhoneCallService"; - - private Call.Callback callback = new Call.Callback() { - @Override - public void onStateChanged(Call call, int state) { - super.onStateChanged(call, state); - switch (state) { - case Call.STATE_ACTIVE: { - break; // 通话中 - } - case Call.STATE_DISCONNECTED: { - break; // 通话结束 - } - } - } - }; - -// @Override -// public void onCallAdded(Call call) { -// super.onCallAdded(call); -// -// call.registerCallback(callback); -// } -// -// @Override -// public void onCallRemoved(Call call) { -// super.onCallRemoved(call); -// -// call.unregisterCallback(callback); -// } - - @Override - public void onCallAdded(Call call) { - super.onCallAdded(call); - - call.registerCallback(callback); - PhoneCallManager.call = call; // 传入call - -// CallType callType = null; -// -// if (call.getState() == Call.STATE_RINGING) { -// callType = CallType.CALL_IN; -// } else if (call.getState() == Call.STATE_CONNECTING) { -// callType = CallType.CALL_OUT; -// } -// -// if (callType != null) { -// Call.Details details = call.getDetails(); -// String phoneNumber = details.getHandle().toString().substring(4) -// .replaceAll("%20", ""); // 去除拨出电话中的空格 -// PhoneCallActivity.actionStart(this, phoneNumber, callType); -// } - } - - @Override - public void onCallRemoved(Call call) { - super.onCallRemoved(call); - - call.unregisterCallback(callback); - PhoneCallManager.call = null; - } -} diff --git a/winboll-shared/build.properties b/winboll-shared/build.properties index 7638e8d..d73febf 100644 --- a/winboll-shared/build.properties +++ b/winboll-shared/build.properties @@ -1,8 +1,8 @@ #Created by .winboll/winboll_app_build.gradle -#Thu Feb 20 22:09:07 GMT 2025 +#Fri Feb 21 01:46:02 GMT 2025 stageCount=0 libraryProject=winboll-shared baseVersion=1.0 publishVersion=1.0.0 -buildCount=126 +buildCount=175 baseBetaVersion=1.0.1