需求:可以控制某个应用访问WIFI或移动网络的功能。
Android自带防火墙原理是:一旦开启防火墙,所有的应用都不能访问网络(包括WIFI和移动网络),所以不能满足需求,故需要在原生基础上新增两个方法来实现该功能,方法声明在frameworks/base/core/java/android/os目录下的INetworkManagementService.aidl中:
interface INetworkManagementService
{
. . .
void setMobileDataUidRule(int uid, boolean allow); //根据应用的uid设置是否允许它访问移动网络
void setWifiDataUidRule(int uid, boolean allow); //根据应用的uid设置是否允许它访问WIFI网络
. . .
}
方法的实现是在frameworks/base/services/core/java/com/android/server目录下的NetworkManagementService.java中:
public class NetworkManagementService extends INetworkManagementService.Stub implements Watchdog.Monitor {
. . .
@Override
public void setMobileDataUidRule(int uid, boolean allow) {
enforceSystemUid(); // 校验调用者是否是系统uid
try {
mConnector.execute("firewall", "set_mobile_data_uid_rule", uid, rule);
} catch (NativeDaemonConnectorException e) {
throw e.rethrowAsParcelableException();
}
}
@Override
public void setWifiDataUidRule(int uid, boolean allow) {
enforceSystemUid();
try {
mConnector.execute("firewall", "set_wifi_data_uid_rule", uid, rule);
} catch (NativeDae