欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2022-12-24 07:54:08
一、屏蔽系统短信功能 1、屏蔽所有短信 android 4.2 短信发送流程分析可参考这篇 [戳这][1] 源码位置 vendor\mediatek\proprietary\packages\apps\Mms\src\com\android\mms\transaction\SmsReceiverSe ......

一、屏蔽系统短信功能

1、屏蔽所有短信

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

源码位置 vendor\mediatek\proprietary\packages\apps\mms\src\com\android\mms\transaction\smsreceiverservice.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.
 */
@override
public 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是个好东西,多加日志,多分析,这样很容易上手。