Android6.0 源码修改之屏蔽系统短信功能和来电功能
2019-12-01

一、屏蔽系统短信功能

1、屏蔽所有短信

android 4.2 短信发送流程分析可参考这篇 戳这

源码位置 vendormediatekproprietarypackagesappsMmssrccomandroidmmsransactionSmsReceiverService.java

private void handleSmsReceived(Intent intent, int error) { //2018-10-09 cczheng add for intercept mms notifications start if (true) { Log.i("SmsReceived", "handleSmsReceived"); return; } //2018-10-09 cczheng add for intercept mms notifications end SmsMessage[] msgs = Intents.getMessagesFromIntent(intent); /// M:Code analyze 022, check null @{ if (msgs == null) { MmsLog.e(MmsApp.TXN_TAG, "getMessagesFromIntent return null."); return; } MmsLog.d(MmsApp.TXN_TAG, "handleSmsReceived SmsReceiverService"); /// ......}

在handleSmsReceived()方法中直接return即可,不去解析和分发短信消息,同时这样操作 短信将不会记录到短信数据库中,插入短信消息到数据库的方法见下文insertMessage()方法。

2、屏蔽特定的短信(特定的短信号码或者短信内容)

源码位置同上

SmsMessage.getOriginatingAddress() 获取短信号码

SmsMessage.getMessageBody() 获取短信内容

private void handleSmsReceived(Intent intent, int error) { SmsMessage[] msgs = Intents.getMessagesFromIntent(intent); ..... /// M:Code analyze 024, print log @{ SmsMessage tmpsms = msgs[0]; MmsLog.d(MmsApp.TXN_TAG, "handleSmsReceived" + (tmpsms.isReplace() ? "(replace)" : "") + " messageUri: " + messageUri + ", address: " + tmpsms.getOriginatingAddress() + ", body: " + tmpsms.getMessageBody()); /// @ //2018-10-09 cczheng add for intercept mms notifications start if ("10010".equals(tmpsms.getOriginatingAddress()) || "话费".contains(tmpsms.getMessageBody())) { Log.i("SmsReceived", "handleSmsReceived"); return; } //2018-10-09 cczheng add for intercept mms notifications end .... }

是否插入短信消息到数据库,insertMessage()方法在handleSmsReceived()中调用

private Uri insertMessage(Context context, SmsMessage[] msgs, int error, String format) { // Build the helper classes to parse the messages. if (msgs == null) { MmsLog.e(MmsApp.TXN_TAG, "insertMessage:getMessagesFromIntent return null."); return null; } /// @} SmsMessage sms = msgs[0]; if (sms.getMessageClass() == SmsMessage.MessageClass.CLASS_0) { MmsLog.d(MmsApp.TXN_TAG, "insertMessage: display class 0 message!"); displayClassZeroMessage(context, msgs, format); return null; } else if (sms.isReplace()) { MmsLog.d(MmsApp.TXN_TAG, "insertMessage: is replace message!"); return replaceMessage(context, msgs, error); } else { MmsLog.d(MmsApp.TXN_TAG, "insertMessage: stored directly!"); return storeMessage(context, msgs, error); }}

3、应用层拦截短信(不用修改android源码,原理就是用你的app去替代系统默认的短信app,过程略繁琐)

需要添加SmsReceiver,MmsReceiver,ComposeSmsActivity,HeadlessSmsSendService这几个类,并在AndroidManifest中进行相应配置,具体流程可参考这篇 戳这

二、屏蔽系统来电响铃和通知提示

屏蔽系统来电可分为三个步骤

1.来电静音,不响铃

2.来电挂断,不出现IncallActivity

3、拦截未接来电通知,不显示在状态栏StatusBar中

ps:此种修改方式的弊端在于来电时网络数据会离线2s左右

好,现在我们开始按这三个步骤来修改源码

1.来电静音,不响铃

源码位置 packages/services/Telecomm/src/com/android/server/telecom/Ringer.java

private void updateRinging(Call call) { if (mRingingCalls.isEmpty()) { stopRinging(call, "No more ringing calls found"); stopCallWaiting(call); } else { //2018-10-10 cczheng add anotation function startRingingOrCallWaiting() for silent call start Log.d("callRinging", "silent call, will not play ringtone"); // startRingingOrCallWaiting(call); //2018-10-10 cczheng add anotation function startRingingOrCallWaiting() for silent call end }}

是的,注释掉startRingingOrCallWaiting(call);方法就ok啦

2.来电挂断,不出现IncallActivity

思路:监听PhoneState,当监听到响铃时,直接通过反射调用endcall方法挂断电话。监听PhoneStateListener可以写到广播中,当收到开机广播时,开始监听phoneState,这样和系统保持同步。以下是参考代码

public class PhoneStartReceiver extends BroadcastReceiver { private static final String TAG = "PhoneStartReceiver"; private PhoneCallListener mPhoneCallListener; private TelephonyManager mTelephonyManager; @Override public void onReceive(final Context context, final Intent intent) { String action = intent.getAction(); if (action.equals(Intent.ACTION_BOOT_COMPLETED)) { // endCall when CALL_STATE_RINGING initPhoneCallListener(context); } } private void initPhoneCallListener(Context context){ mPhoneCallListener = new PhoneCallListener(); mTelephonyManager = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE); mTelephonyManager.listen(mPhoneCallListener, PhoneCallListener.LISTEN_CALL_STATE); } public class PhoneCallListener extends PhoneStateListener { @Override public void onCallStateChanged(int state, String incomingNumber) { Log.v(TAG, "onCallStateChanged-state: " + state); Log.v(TAG, "onCallStateChanged-incomingNumber: " + incomingNumber); switch (state) { case TelephonyManager.CALL_STATE_RINGING: endCall(); break; default: break; } super.onCallStateChanged(state, incomingNumber); } } private void endCall() { try { Method m1 = mTelephonyManager.getClass().getDeclaredMethod("getITelephony"); if (m1 != null) { m1.setAccessible(true); Object iTelephony = m1.invoke(mTelephonyManager); if (iTelephony != null) { Method m2 = iTelephony.getClass().getDeclaredMethod("silenceRinger"); if (m2 != null) { m2.invoke(iTelephony); Log.v(TAG, "silenceRinger......"); } Method m3 = iTelephony.getClass().getDeclaredMethod("endCall"); if (m3 != null) { m3.invoke(iTelephony); Log.v(TAG, "endCall......"); } } } } catch (Exception e) { e.printStackTrace(); Log.e(TAG, "endCallError", e); } }}

3.拦截未接来电通知,不显示在状态栏StatusBarr中

源码位置 packages/apps/InCallUI/src/com/android/incallui/StatusBarNotifier.java

private void updateInCallNotification(final InCallState state, CallList callList) { ... final Call call = getCallToShow(callList); //2018-10-10 cczheng add intercept incoming notification start if (true) { if (call != null) { Log.v("InCallNotification", "phoneNumber = " + call.getNumber()); } return; } //2018-10-10 cczheng add intercept incoming notification end if (call != null) { showNotification(call); } else { cancelNotification(); } ...}

其实核心方法就是showNotification(call),发送通知当statusBar收到通知就处理并显示在状态栏。

当你发现这样处理完后,重新mm,然后push替换Dialer.apk重启后,你会坑爹的发现状态栏那个未接来电图标依旧显示,无fa可说,继续跟踪日志揪出罪魁祸首,最终发现另一处奇葩的地方。

源码位置 packages/services/Telecomm/src/com/android/server/telecom/ui/MissedCallNotifierImpl.java

诺,就是这了,看注释就明白了吧 Create a system notification for the missed call

/** * Create a system notification for the missed call. * * @param call The missed call. */@Overridepublic void showMissedCallNotification(Call call) { ////2018-10-10 cczheng hide missed call notification [S] if (true) { android.util.Log.i("misscall", "showMissedCallNotification......"); return; } ///2018-10-10 cczheng hide missed call notification [E] mMissedCallCount++; final int titleResId; final String expandedText; // The text in the notification"s line 1 and 2. ....}

ok,这样我们就搞定了来电功能。

三、隐藏短信应用和电话应用在launcher中显示(去除AndroidManifest中的category)

<category android:name="android.intent.category.LAUNCHER" />

四、总结

Android源码修改没有大家想象的那么难,毕竟Google工程师都已经给我们标注了详细的注释说明,只是框架和封装的思路难以理解,这就考验我们的耐心了,Log是个好东西,多加日志,多分析,这样很容易上手。