接收电话时预先静音模式调试完成
This commit is contained in:
		| @@ -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 | ||||
|   | ||||
| @@ -38,7 +38,6 @@ | ||||
|         <activity | ||||
|             android:name=".MainActivity" | ||||
|             android:label="@string/app_name" | ||||
|             android:launchMode="singleTask" | ||||
|             android:exported="true"> | ||||
|  | ||||
|             <intent-filter> | ||||
| @@ -50,7 +49,6 @@ | ||||
|             </intent-filter> | ||||
|  | ||||
|         </activity> | ||||
|          | ||||
|  | ||||
|         <activity | ||||
|             android:name=".activities.CallActivity" | ||||
| @@ -89,6 +87,8 @@ | ||||
|  | ||||
|         </activity> | ||||
|  | ||||
|         <activity android:name="cc.winboll.studio.contacts.activities.SettingsActivity"/> | ||||
|          | ||||
|         <service | ||||
|             android:name="cc.winboll.studio.contacts.services.MainService" | ||||
|             android:exported="true"/> | ||||
| @@ -164,7 +164,7 @@ | ||||
|             </intent-filter> | ||||
|  | ||||
|         </receiver> | ||||
|  | ||||
|          | ||||
|         <provider | ||||
|             android:name="androidx.core.content.FileProvider" | ||||
|             android:authorities="${applicationId}.fileprovider" | ||||
| @@ -177,8 +177,6 @@ | ||||
|  | ||||
|         </provider> | ||||
|  | ||||
|         <activity android:name="cc.winboll.studio.contacts.activities.SettingsActivity"/> | ||||
|  | ||||
|     </application> | ||||
|  | ||||
| </manifest> | ||||
|   | ||||
| @@ -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); | ||||
|     } | ||||
| } | ||||
| @@ -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(); | ||||
|         } | ||||
|   | ||||
| @@ -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 { | ||||
|   | ||||
| @@ -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); | ||||
| //            } | ||||
| //        } | ||||
| //    } | ||||
| //} | ||||
| @@ -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; | ||||
|     } | ||||
| } | ||||
| @@ -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 | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 ZhanGSKen
					ZhanGSKen