本文最近更新于 2019 年 7 月 9 日「 星期二 」

IFW 全称为 Intent Firewall,简单来说就是安卓自带的系统防火墙。

基础知识

安卓系统的 Activity、服务和广播接收器,是通过名为 Intent 的消息进行启动的,可以将 Intent 视为从其他组件请求操作的信使,无论组件属于这个应用还是其他应用。

  • 对于 Activity 和服务,Intent 传达要执行的操作。例如,Intent 传达的请求可以是打开应用设置界面的 Activity,或是启动下载某文件的服务。

  • 对于广播接收器,Intent 只会定义要广播的通知,可以响应这个广播的接收器会被启动以执行下一步操作。

Intent 防火墙不是 Android 框架官方支持的功能,没有官方文档,只能通过阅读源代码了解。

每一个在 Android 框架中启动的 Intent,包括由操作系统创建的 Intent,都会通过 Intent 防火墙。这意味着 Intent 防火墙有权允许或拒绝任何 Intent。Intent 防火墙在决定如何处理传入 Intent 时不考虑发送方,只考虑 Intent 的细节和其预期的接收者。

IFW 配置文件存储于 /data/system/ifw/*.xml,记录着什么样的 Intent 会被终止传递。XML 文件被写入到 /data/system/ifw/ 目录且可以被更新和删除,这使得 Intent 防火墙可以动态更新其规则集。

在系统匹配到存在于规则内的 Intent 时就会丢弃这个 Intent 使之失效,即利用系统防火墙来进行限制,达到阻止唤醒的目的。

「IFW」,「写轮眼」和「绿色守护」

写轮眼」是通过关闭组件来达到效果,而 IFW 配置文件则是拦截了启动组件。

「写轮眼」(My Android Tools)使用的是非单纯的杀后台抑制唤醒的方法,深受 Android 折腾党的喜爱。但任何 App 都可以使用公开的接口 Android Developer 来重新激活自己的组件。「写轮眼」作者也考虑到了这个问题,同时也推出了一个 Xposed 模块阻止这个 API 的调用。

绿色守护」在 V3.0 时新推出的 “处方” 功能,使用了 IFW 特性。

Intent Firewall 的编写规则?

下面是一个实例,broadcast 为广播,service 为服务,activity 为活动,我们应该很容易理解其书写规则。

注意:使用 IFW 规则时,组件会被始终拦截。所以需要结合自身实际使用情况进行 IFW 编写。例如拦截了 location 相关的某个组件试图达到省电目的,但是该应用的「定位」功能就会无法使用,这和手机是否开启 GPS 定位无关。

 1 <rules>
 2 <broadcast block="true" log="false">
 3 <component-filter name="com.tencent.mobileqq/cooperation.dingdong.DingdongPluginProxyBroadcastReceiver" />
 4 <component-filter name="com.tencent.mobileqq/cooperation.qzone.QzoneProxyReceiver" />
 5 <component-filter name="com.tencent.mobileqq/com.tencent.open.downloadnew.common.DownloadReceiver" />
 6 <component-filter name="com.tencent.mobileqq/com.tencent.open.downloadnew.common.DownloadReceiverWebProcess" />
 7 <component-filter name="com.tencent.mobileqq/com.tencent.open.business.base.appreport.AppReportReceiver" />
 8 <component-filter name="com.tencent.mobileqq/cooperation.weiyun.WeiyunProxyBroadcastReceiver" />
 9 <component-filter name="com.tencent.mobileqq/cooperation.weiyun.WeiyunBroadcastReceiver" />
10 <component-filter name="com.tencent.mobileqq/com.tencent.mobileqq.msf.core.NetConnInfoCenter" />
11 </broadcast>
12 <service block="true" log="false">
13 <component-filter name="com.tencent.mobileqq/com.tencent.mobileqq.app.CoreService" />
14 <component-filter name="com.tencent.mobileqq/com.tencent.mobileqq.app.CoreService$KernelService" />
15 <component-filter name="com.tencent.mobileqq/cooperation.qzone.remote.logic.QzoneWebPluginProxyService" />
16 <component-filter name="com.tencent.mobileqq/com.tencent.tmdownloader.TMAssistantDownloadService" />
17 </service>
18 <activity block="true" log="false">
19 <component-filter name="com.tencent.mobileqq/com.tencent.mobileqq.activity.UpgradeActivity" />
20 <component-filter name="com.tencent.mobileqq/com.tencent.mobileqq.activity.UpgradeDetailActivity" />
21 </activity>
22 </rules>

要进一步了解 IFW 可以访问:http://www.cis.syr.edu/~wedu/android/IntentFirewall/

附:安卓果酱的 IFW 规则

扩展阅读: