android来电拦截及去电禁止

本文详细解析了在Android系统中如何通过修改PstnIncomingCallNotifier.java和UserCallIntentProcessor.java文件来实现对特定来电号码的拦截以及对去电权限的控制。文章深入探讨了代码中用于检查来电号码并进行拦截的逻辑,以及如何根据用户限制和白名单判断去电请求的有效性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Z:\wangdh\ALPS-MP-N0.MP1-V1.0.2_AEON6737M_65_D_N\alps\packages\services\Telephony\src\com\android\services\telephony\PstnIncomingCallNotifier.java

 

    /**
     * Used to listen to events from {@link #mPhone}.
     */
    private final Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            /// M: CC: OP01 Plugin to block MT call from black number @{
            if (ExtensionManager.getIncomingCallExt().handlePhoneEvent(msg, mPhone)) {
                return;
            }
            /// @}
            
			boolean intercept = false;//是否要拦截
			Log.d(this, "wdh--------00000000----1------");
		
			
			
            switch(msg.what) {
                case EVENT_NEW_RINGING_CONNECTION:
					//DaMi_wdh add
					Log.d(this, "wdh--------1----------EVENT_NEW_RINGING_CONNECTION");
					AsyncResult asyncResult = (AsyncResult) msg.obj;
					Connection connection = (Connection) asyncResult.result;
					String callerId = connection.getAddress();//获取来电号码
					Log.d(this, "wdh--------00000000----------"+connection.getAddress());
					intercept = callerId.equals("176********");//要拦截的号码
				
					if(intercept){
						new Thread(new Runnable() {
							@Override
							public void run() {
								Log.d(this, "wdh--------2----------EVENT_NEW_RINGING_CONNECTION");
								PhoneInterfaceManager.sInstance.endCall();
							}
						}).start();
						}else{
							Log.d(this, "wdh--------3----------EVENT_NEW_RINGING_CONNECTION");
							 handleNewRingingConnection((AsyncResult) msg.obj);
					}	
                    break;
                case EVENT_CDMA_CALL_WAITING:
					if(intercept){
						Log.d(this, "wdh--------4----22------EVENT_NEW_RINGING_CONNECTION");
						}else{
							Log.d(this, "wdh--------4---11-------EVENT_NEW_RINGING_CONNECTION");
							handleCdmaCallWaiting((AsyncResult) msg.obj);
						}
                    break;
                case EVENT_UNKNOWN_CONNECTION:
				Log.d(this, "wdh--------5----------EVENT_NEW_RINGING_CONNECTION");
                    handleNewUnknownConnection((AsyncResult) msg.obj);
                    break;
                /// M: CC: Proprietary incoming call handling @{
                case EVENT_VOICE_CALL_INCOMING_INDICATION:
                    Log.i(this, "EVENT_VOICE_CALL_INCOMING_INDICATION");
					Log.d(this, "wdh--------6----------EVENT_NEW_RINGING_CONNECTION");
                    TelephonyConnectionServiceUtil.getInstance()
                    .setIncomingCallIndicationResponse(
                            (GsmCdmaPhone) ((AsyncResult) msg.obj).result);	
                    break;
                /// @}
                default:
                    break;
            }
        }
    };

Q版本同样是在PstnIncomingCallNotifier.java文件里面修改,只不过此文件变化较大,但方法不变,依然是修改handleNewRingingConnection方法

    private void handleNewRingingConnection(AsyncResult asyncResult) {
        Log.d(this, "handleNewRingingConnection");
        Connection connection = (Connection) asyncResult.result;
        if (connection != null) {
            Call call = connection.getCall();
            // Check if we have a pending number verification request.
            if (connection.getAddress() != null) {
				Log.i(this, "wdh isCallWhiteListNum "+BlockChecker.isCallWhiteListNum(mPhone.getContext(), connection.getAddress())+"  getAddress "+connection.getAddress());
				//wangdonghai add for block_calling begin
				 if ( !BlockChecker.isCallWhiteListNum(mPhone.getContext(), connection.getAddress())/*for block call/sms white list*/) {
                    try {
                        connection.hangup();
                    } catch (CallStateException e) {
                        Log.i(this, "[HMD_BLOCK]processOutgoingCallIntent failed:not allow calling.");
                    }
                    Log.i(this, "[HMD_BLOCK]processOutgoingCallIntent failed:not allow calling.");
                    return;
                }
				//wangdonghai add for block_calling end
				
                if (NumberVerificationManager.getInstance()
                        .checkIncomingCall(connection.getAddress())) {
                    // Disconnect the call if it matches
                    try {
                        connection.hangup();
                    } catch (CallStateException e) {
                        Log.e(this, e, "Error hanging up potential number verification call");
                    }
                    return;
                }
            }

            // Final verification of the ringing state before sending the intent to Telecom.
            if (call != null && call.getState().isRinging()) {
                sendIncomingCallIntent(connection);
            }
        }
    }

挂断电话用connection.hangup();

------------下面说下去电-------------

packages\services\Telecomm\src\com\android\server\telecom\components\UserCallIntentProcessor.java
 private void processOutgoingCallIntent(Intent intent, String callingPackageName,
            boolean canCallNonEmergency, boolean isLocalInvocation) {
        Uri handle = intent.getData();
        String scheme = handle.getScheme();
        String uriString = handle.getSchemeSpecificPart();

        // Ensure sip URIs dialed using TEL scheme get converted to SIP scheme.
        if (PhoneAccount.SCHEME_TEL.equals(scheme) && PhoneNumberUtils.isUriNumber(uriString)) {
            handle = Uri.fromParts(PhoneAccount.SCHEME_SIP, uriString, null);
        }

        // Check DISALLOW_OUTGOING_CALLS restriction. Note: We are skipping this check in a managed
        // profile user because this check can always be bypassed by copying and pasting the phone
        // number into the personal dialer.
        if (!UserUtil.isManagedProfile(mContext, mUserHandle)) {
            // Only emergency calls are allowed for users with the DISALLOW_OUTGOING_CALLS
            // restriction.
            if (!TelephonyUtil.shouldProcessAsEmergency(mContext, handle)) {
                final UserManager userManager = (UserManager) mContext.getSystemService(
                        Context.USER_SERVICE);
						
                //add for CAP-64 HMD enterprise API by wangdonghai  start
                //block outgoing call
				Log.i(this, "wdh  222 uriString  == "+uriString +"  isCallWhiteListNum  "+BlockChecker.isCallWhiteListNum(mContext, uriString));
                if (!BlockChecker.isCallWhiteListNum(mContext, uriString)/*for block call/sms white list*/) {
                    showErrorDialogForRestrictedOutgoingCall(mContext,
                            R.string.outgoing_call_not_allowed_user_restriction);
                    Log.i(this, "[HMD_BLOCK]processOutgoingCallIntent failed:not allow calling.");
                    return;
                }
                //add for CAP-64 HMD enterprise API by wangdonghai  end
                if (userManager.hasBaseUserRestriction(UserManager.DISALLOW_OUTGOING_CALLS,
                        mUserHandle)) {
                    showErrorDialogForRestrictedOutgoingCall(mContext,
                            R.string.outgoing_call_not_allowed_user_restriction);
                    Log.w(this, "Rejecting non-emergency phone call due to DISALLOW_OUTGOING_CALLS "
                            + "restriction");
                    return;
                } else if (userManager.hasUserRestriction(UserManager.DISALLOW_OUTGOING_CALLS,
                        mUserHandle)) {
                    final DevicePolicyManager dpm =
                            mContext.getSystemService(DevicePolicyManager.class);
                    if (dpm == null) {
                        return;
                    }
                    final Intent adminSupportIntent = dpm.createAdminSupportIntent(
                            UserManager.DISALLOW_OUTGOING_CALLS);
                    if (adminSupportIntent != null) {
                        mContext.startActivity(adminSupportIntent);
                    }
                    return;
                }
            }
        }

        if (!canCallNonEmergency && !TelephonyUtil.shouldProcessAsEmergency(mContext, handle)) {
            showErrorDialogForRestrictedOutgoingCall(mContext,
                    R.string.outgoing_call_not_allowed_no_permission);
            Log.w(this, "Rejecting non-emergency phone call because "
                    + android.Manifest.permission.CALL_PHONE + " permission is not granted.");
            return;
        }

        int videoState = intent.getIntExtra(
                TelecomManager.EXTRA_START_CALL_WITH_VIDEO_STATE,
                VideoProfile.STATE_AUDIO_ONLY);
        Log.d(this, "processOutgoingCallIntent videoState = " + videoState);

        intent.putExtra(CallIntentProcessor.KEY_IS_PRIVILEGED_DIALER,
                isDefaultOrSystemDialer(callingPackageName));

        // Save the user handle of current user before forwarding the intent to primary user.
        intent.putExtra(CallIntentProcessor.KEY_INITIATING_USER, mUserHandle);

        sendIntentToDestination(intent, isLocalInvocation, callingPackageName);
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值