Android P 9.0 MTK平台 增加以太网静态IP功能

2024-08-22 06:32

本文主要是介绍Android P 9.0 MTK平台 增加以太网静态IP功能,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

前言

朋友们,最近又开始搞 Android P了,同样的以太网静态 IP 是少不了的功能,今天我们就开始来整一下。之前弄6.0 和 8.1 的都 ok 了。

没想到 9.0 改动还是略微有点大的。来来来,先看图。

效果图

在这里插入图片描述

上代码

app层

Settings 中的代码和之前的一样就不贴了,可以去看之前的这篇中代码 Android8.1 MTK平台 增加以太网静态IP功能

或者下载这篇的源码资源

framework 层

驱动大哥已经把驱动都搞定了,现在直接插上网线,设备就能上网,网卡图标也正常显示。我们需要控制网卡的开关,先来简单看下流程。

frameworks\opt\net\ethernet\java\com\android\server\ethernet\EthernetService.java

public final class EthernetService extends SystemService {private static final String TAG = "EthernetService";final EthernetServiceImpl mImpl;public EthernetService(Context context) {super(context);mImpl = new EthernetServiceImpl(context);}@Overridepublic void onStart() {Log.i(TAG, "Registering service " + Context.ETHERNET_SERVICE);publishBinderService(Context.ETHERNET_SERVICE, mImpl);}@Overridepublic void onBootPhase(int phase) {if (phase == SystemService.PHASE_SYSTEM_SERVICES_READY) {mImpl.start();}}
}

EthernetService 继承了系统服务,那自然也就是系统服务,如果挂掉会自动重新创建,严重情况会导致系统重启,我就试出来过。看下当 PHASE_SYSTEM_SERVICES_READY 准备完成,调用 EthernetServiceImpl 的 start()

来看下这个 start() 都干了啥

frameworks\opt\net\ethernet\java\com\android\server\ethernet\EthernetServiceImpl.java

    public void start() {Log.i(TAG, "Starting Ethernet service");HandlerThread handlerThread = new HandlerThread("EthernetServiceThread");handlerThread.start();mHandler = new Handler(handlerThread.getLooper());mTracker = new EthernetTracker(mContext, mHandler);mTracker.start();mStarted.set(true);
}

主要创建了 EthernetTracker,这个类是 9.0 中新增出来的,用于监听以太网的切换、以太网判断当前网络是否可用等一系列操作。之前 8.1 中都集成在 EthernetNetworkFactory 中,继续跟进

frameworks\opt\net\ethernet\java\com\android\server\ethernet\EthernetTracker.java

void start() {mConfigStore.read();// Default interface is just the first one we want to track.mIpConfigForDefaultInterface = mConfigStore.getIpConfigurationForDefaultInterface();final ArrayMap<String, IpConfiguration> configs = mConfigStore.getIpConfigurations();Log.e(TAG, "mIpConfigForDefaultInterface== " + mIpConfigForDefaultInterface);Log.i(TAG, "IpConfiguration size== " + configs.size());for (int i = 0; i < configs.size(); i++) {mIpConfigurations.put(configs.keyAt(i), configs.valueAt(i));}try {mNMService.registerObserver(new InterfaceObserver());} catch (RemoteException e) {Log.e(TAG, "Could not register InterfaceObserver " + e);}mHandler.post(this::trackAvailableInterfaces);}

mConfigStore 对象用来管理保存的 IpConfigStore 信息,EthernetConfigStore 中通过读取 /misc/ethernet/ipconfig.txt 中保存的信息进行维护一个 ArrayMap<String, IpConfiguration>,根据打印的日志看,start() 中每次获取到的 size 都为 0,基本上没起作用。值得一提的是,在 EthernetTracker 的构造方法中通过解析 config_ethernet_interfaces 字符串也可向 map 中添加初始信息。

EthernetTracker(Context context, Handler handler) {mHandler = handler;// The services we use.IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE);mNMService = INetworkManagementService.Stub.asInterface(b);// Interface match regex.mIfaceMatch = context.getResources().getString(com.android.internal.R.string.config_ethernet_iface_regex);// Read default Ethernet interface configuration from resourcesfinal String[] interfaceConfigs = context.getResources().getStringArray(com.android.internal.R.array.config_ethernet_interfaces);for (String strConfig : interfaceConfigs) {parseEthernetConfig(strConfig);}mConfigStore = new EthernetConfigStore();NetworkCapabilities nc = createNetworkCapabilities(true /* clear default capabilities */);mFactory = new EthernetNetworkFactory(handler, context, nc);mFactory.register();}private void parseEthernetConfig(String configString) {String[] tokens = configString.split(";");String name = tokens[0];String capabilities = tokens.length > 1 ? tokens[1] : null;NetworkCapabilities nc = createNetworkCapabilities(!TextUtils.isEmpty(capabilities)  /* clear default capabilities */, capabilities);mNetworkCapabilities.put(name, nc);if (tokens.length > 2 && !TextUtils.isEmpty(tokens[2])) {IpConfiguration ipConfig = parseStaticIpConfiguration(tokens[2]);mIpConfigurations.put(name, ipConfig);}}

config_ethernet_interfaces 的初始值位置在

frameworks\base\core\res\res\values\config.xml

<!-- Regex of wired ethernet ifaces --><string translatable="false" name="config_ethernet_iface_regex">eth\\d</string><!-- Configuration of Ethernet interfaces in the following format:<interface name|mac address>;[Network Capabilities];[IP config]Where[Network Capabilities] Optional. A comma seprated list of network capabilities.Values must be from NetworkCapabilities#NET_CAPABILITIES_* constants.[IP config] Optional. If empty or not specified - DHCP will be used, otherwiseuse the following format to specify static IP configuration:ip=<ip-address/mask> gateway=<ip-address> dns=<comma-sep-ip-addresses>domains=<comma-sep-domains> --><string-array translatable="false" name="config_ethernet_interfaces"><!--<item>eth1;12,13,14,15;ip=192.168.0.10/24 gateway=192.168.0.1 dns=4.4.4.4,8.8.8.8</item><item>eth2;;ip=192.168.0.11/24</item>--></string-array>

好了继续回到刚刚的 start() 中,接下来应该调用 trackAvailableInterfaces(),来看下完整的流程,maybeTrackInterface 中先进行 iface 判断,这个指代要使用的网口名称,通过命令 ifconfig -a 可以看到你设备下的所有网口名称。

KlxJ6e.png

mIfaceMatch 对应刚刚的 config.xml 中配置的 eth\d, 所以只有是 eth 打头并且 mFactory 中不存在的才能设置以太网连接,这很关键

继续调用 addInterface(), 将 iface 对应的 ipConfiguration 通过 mFactory addInterface,再然后 updateInterfaceState 广播通知当前以太网连接状态 CONNECTED/CONNECTED

private void trackAvailableInterfaces() {try {final String[] ifaces = mNMService.listInterfaces();for (String iface : ifaces) {maybeTrackInterface(iface);}} catch (RemoteException | IllegalStateException e) {Log.e(TAG, "Could not get list of interfaces " + e);}}private void maybeTrackInterface(String iface) {if (DBG) Log.i(TAG, "maybeTrackInterface " + iface);// If we don't already track this interface, and if this interface matches// our regex, start tracking it.if (!iface.matches(mIfaceMatch) || mFactory.hasInterface(iface)) {Log.d(TAG, iface + "  return ");return;}Log.e(TAG, "maybeTrackInterface " + iface);if (mIpConfigForDefaultInterface != null) {updateIpConfiguration(iface, mIpConfigForDefaultInterface);mIpConfigForDefaultInterface = null;}addInterface(iface);}private void addInterface(String iface) {InterfaceConfiguration config = null;// Bring up the interface so we get link status indications.try {mNMService.setInterfaceUp(iface);config = mNMService.getInterfaceConfig(iface);} catch (RemoteException | IllegalStateException e) {// Either the system is crashing or the interface has disappeared. Just ignore the// error; we haven't modified any state because we only do that if our calls succeed.Log.e(TAG, "Error upping interface " + iface, e);}if (config == null) {Log.e(TAG, "Null interface config for " + iface + ". Bailing out.");return;}final String hwAddress = config.getHardwareAddress();NetworkCapabilities nc = mNetworkCapabilities.get(iface);if (nc == null) {// Try to resolve using mac addressnc = mNetworkCapabilities.get(hwAddress);if (nc == null) {nc = createDefaultNetworkCapabilities();}}IpConfiguration ipConfiguration = mIpConfigurations.get(iface);if (ipConfiguration == null) {ipConfiguration = createDefaultIpConfiguration();}Log.d(TAG, "Started tracking interface " + iface);mFactory.addInterface(iface, hwAddress, nc, ipConfiguration);// Note: if the interface already has link (e.g., if we crashed and got// restarted while it was running), we need to fake a link up notification so we// start configuring it.if (config.hasFlag("running")) {updateInterfaceState(iface, true);}}private void updateInterfaceState(String iface, boolean up) {Log.e(TAG, "updateInterfaceState up==" + up);boolean modified = mFactory.updateInterfaceLinkState(iface, up);if (modified) {boolean restricted = isRestrictedInterface(iface);int n = mListeners.beginBroadcast();for (int i = 0; i < n; i++) {try {if (restricted) {ListenerInfo listenerInfo = (ListenerInfo) mListeners.getBroadcastCookie(i);if (!listenerInfo.canUseRestrictedNetworks) {continue;}}mListeners.getBroadcastItem(i).onAvailabilityChanged(iface, up);} catch (RemoteException e) {// Do nothing here.}}mListeners.finishBroadcast();}}

Kl6fv4.png

知道了 EthernetTracker 的 start() 去连接以太网,那么我们在 EthernetServiceImpl 中增加布尔判断就能控制以太网开关状态。

frameworks\opt\net\ethernet\java\com\android\server\ethernet\EthernetServiceImpl.java

public class EthernetServiceImpl extends IEthernetManager.Stub {private static final String TAG = "EthernetServiceImpl";public static final String IS_ETHERNET_OPEN = Settings.IS_ETHERNET_OPEN;public static final String ETHERNET_USE_STATIC_IP = Settings.IS_ETHERNET_STATUC_OPEN; private final Context mContext;private final AtomicBoolean mStarted = new AtomicBoolean(false);private IpConfiguration mIpConfiguration;private final EthernetOpenedObserver mOpenObserver = new EthernetOpenedObserver();private final EthernetStaticObserver mStaticObserver = new EthernetStaticObserver(); private Handler mHandler;private EthernetTracker mTracker;public EthernetServiceImpl(Context context) {mContext = context;Log.i(TAG, "Creating EthernetConfigStore");mContext.getContentResolver().registerContentObserver(System.getUriFor(IS_ETHERNET_OPEN), false, mOpenObserver);mContext.getContentResolver().registerContentObserver(System.getUriFor(ETHERNET_USE_STATIC_IP), false, mStaticObserver); }....public void start() {Log.i(TAG, "Starting Ethernet service");HandlerThread handlerThread = new HandlerThread("EthernetServiceThread");handlerThread.start();mHandler = new Handler(handlerThread.getLooper());mTracker = new EthernetTracker(mContext, mHandler);mIpConfiguration = mTracker.getDefaultIpConfiguration();if (getState() == 1) {                 if (isStatic()) {StaticIpConfiguration staticIpConfiguration = new StaticIpConfiguration();staticIpConfiguration.domains = Settings.System.getString(mContext.getContentResolver(),Settings.ETHERNET_STATIC_NETMASK);try {staticIpConfiguration.gateway = InetAddress.getByName(Settings.System.getString(mContext.getContentResolver(),Settings.ETHERNET_STATIC_GATEWAY));staticIpConfiguration.ipAddress = new LinkAddress(InetAddress.getByName(Settings.System.getString(mContext.getContentResolver(),Settings.ETHERNET_STATIC_IP)), 24);staticIpConfiguration.dnsServers.add(InetAddress.getByName(Settings.System.getString(mContext.getContentResolver(),Settings.ETHERNET_STATIC_DNS1)));staticIpConfiguration.dnsServers.add(InetAddress.getByName(Settings.System.getString(mContext.getContentResolver(),Settings.ETHERNET_STATIC_DNS2)));}catch (Exception e){e.printStackTrace();}mIpConfiguration.ipAssignment = IpAssignment.STATIC;mIpConfiguration.proxySettings = ProxySettings.STATIC;mIpConfiguration.staticIpConfiguration = staticIpConfiguration;}mTracker.start();mStarted.set(true);}  }private boolean isStatic(){Log.e(TAG, "EthernetServiceImpl isStatic()  " + Settings.System.getInt(mContext.getContentResolver(),ETHERNET_USE_STATIC_IP,0));return Settings.System.getInt(mContext.getContentResolver(),ETHERNET_USE_STATIC_IP,0) ==1;}   private int getState(){int state = Settings.System.getInt(mContext.getContentResolver(), IS_ETHERNET_OPEN,0);Log.e(TAG, "EthernetServiceImpl getState()  " + state);return state;}....@Overridepublic void setConfiguration(String iface, IpConfiguration config) {if (!mStarted.get()) {Log.w(TAG, "System isn't ready enough to change ethernet configuration");}enforceConnectivityInternalPermission();if (mTracker.isRestrictedInterface(iface)) {enforceUseRestrictedNetworksPermission();}Log.e(TAG, "setConfiguration iface="+iface);// TODO: this does not check proxy settings, gateways, etc.// Fix this by making IpConfiguration a complete representation of static configuration.mTracker.updateIpConfiguration(iface, new IpConfiguration(config));//addmTracker.removeInterface(iface);mTracker.start();}....private final class EthernetOpenedObserver extends ContentObserver {public EthernetOpenedObserver() {super(new Handler());}@Overridepublic void onChange(boolean selfChange, Uri uri, int userId) {super.onChange(selfChange, uri, userId);Log.i(TAG, "EthernetServiceImpl isEthernetOpen onChange....");if (getState() == 1) {if (isStatic()) {StaticIpConfiguration staticIpConfiguration = new StaticIpConfiguration();staticIpConfiguration.domains = Settings.System.getString(mContext.getContentResolver(),Settings.ETHERNET_STATIC_NETMASK);try {staticIpConfiguration.gateway = InetAddress.getByName(Settings.System.getString(mContext.getContentResolver(),Settings.ETHERNET_STATIC_GATEWAY));staticIpConfiguration.ipAddress = new LinkAddress(InetAddress.getByName(Settings.System.getString(mContext.getContentResolver(),Settings.ETHERNET_STATIC_IP)), 24);staticIpConfiguration.dnsServers.add(InetAddress.getByName(Settings.System.getString(mContext.getContentResolver(),Settings.ETHERNET_STATIC_DNS1)));staticIpConfiguration.dnsServers.add(InetAddress.getByName(Settings.System.getString(mContext.getContentResolver(),Settings.ETHERNET_STATIC_DNS2)));}catch (Exception e){e.printStackTrace();}mIpConfiguration.ipAssignment = IpAssignment.STATIC;mIpConfiguration.proxySettings = ProxySettings.STATIC;mIpConfiguration.staticIpConfiguration = staticIpConfiguration;}mTracker.start();mStarted.set(true);}else {    mTracker.stop();        }}}private final class EthernetStaticObserver extends ContentObserver {public EthernetStaticObserver() {super(new Handler());}@Overridepublic void onChange(boolean selfChange, Uri uri, int userId) {super.onChange(selfChange, uri, userId);Log.i(TAG, "EthernetServiceImpl isEthernetStaticOpen onChange....");if (!isStatic()) {Log.e(TAG, " no static stop and start");mTracker.recoverDHCPIpConfiguration();mTracker.stop();mTracker.start();           mStarted.set(true);}  }}

根据 settings 中设置的值 IS_ETHERNET_OPEN 和 ETHERNET_USE_STATIC_IP 判断是否加载,在 EthernetTracker 中新增如下几个方法

frameworks\opt\net\ethernet\java\com\android\server\ethernet\EthernetTracker.java

//关闭网卡,先更新状态为 false, 再从 mFactory 中移除 eth0, 不然关闭后无法再次打开,因为上面提到的判断
public void stop() {Log.d(TAG, "EthernetTracker stop ethernet...");updateInterfaceState("eth0", false);android.os.SystemClock.sleep(200);removeInterface("eth0");}//获取默认的 IpConfiguration,如果不存在则新建一个 DHCP 类型的,根据实际情况修改 ipAssignment 和 proxySettings
public IpConfiguration getDefaultIpConfiguration(){IpConfiguration ipConfiguration = mIpConfigurations.get("eth0");return ipConfiguration != null ? ipConfiguration : new IpConfiguration(IpAssignment.DHCP, ProxySettings.NONE, null, null);
}//从静态 IP 切换
public void recoverDHCPIpConfiguration(){mIpConfigurations.put("eth0", new IpConfiguration(IpAssignment.DHCP, ProxySettings.NONE, null, null));
}

测试发现频繁点击 静态IP 开关时,出现了数组角标越界的情况,应该是 add 和 remove iface导致的,直接将打印 try 一下就可以

2019-10-21 17:01:38.675 1075-1285/? E/AndroidRuntime: *** FATAL EXCEPTION IN SYSTEM PROCESS: EthernetServiceThreadjava.lang.ArrayIndexOutOfBoundsException: length=2; index=-1at com.android.internal.util.StateMachine$SmHandler.getCurrentState(StateMachine.java:1151)at com.android.internal.util.StateMachine$SmHandler.access$1300(StateMachine.java:681)at com.android.internal.util.StateMachine.toString(StateMachine.java:2088)at java.lang.String.valueOf(String.java:2896)at java.lang.StringBuilder.append(StringBuilder.java:132)at com.android.server.ethernet.EthernetNetworkFactory$NetworkInterfaceState.toString(EthernetNetworkFactory.java:422)at java.lang.String.valueOf(String.java:2896)at java.lang.StringBuilder.append(StringBuilder.java:132)at com.android.server.ethernet.EthernetNetworkFactory.networkForRequest(EthernetNetworkFactory.java:213)at com.android.server.ethernet.EthernetNetworkFactory.acceptRequest(EthernetNetworkFactory.java:78)at android.net.NetworkFactory.evalRequest(NetworkFactory.java:234)at android.net.NetworkFactory.evalRequests(NetworkFactory.java:253)at android.net.NetworkFactory.handleSetFilter(NetworkFactory.java:204)at android.net.NetworkFactory.handleMessage(NetworkFactory.java:149)at android.os.Handler.dispatchMessage(Handler.java:106)at android.os.Looper.loop(Looper.java:193)at android.os.HandlerThread.run(HandlerThread.java:65)

frameworks\opt\net\ethernet\java\com\android\server\ethernet\EthernetNetworkFactory.java

@Overridepublic String toString() {try{return getClass().getSimpleName() + "{ "+ "iface: " + name + ", "+ "up: " + mLinkUp + ", "+ "hwAddress: " + mHwAddress + ", "+ "networkInfo: " + mNetworkInfo + ", "+ "networkAgent: " + mNetworkAgent + ", "+ "ipClient: " + mIpClient + ","+ "linkProperties: " + mLinkProperties+ "}";}catch(Exception e){e.printStackTrace();return getClass().getSimpleName() + "{ }";}}

这样就能实现开头的动图效果了

修改文件源码下载

android 9.0 添加Ethernet功能(settings+framework).zip

这篇关于Android P 9.0 MTK平台 增加以太网静态IP功能的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1095502

相关文章

SpringKafka消息发布之KafkaTemplate与事务支持功能

《SpringKafka消息发布之KafkaTemplate与事务支持功能》通过本文介绍的基本用法、序列化选项、事务支持、错误处理和性能优化技术,开发者可以构建高效可靠的Kafka消息发布系统,事务支... 目录引言一、KafkaTemplate基础二、消息序列化三、事务支持机制四、错误处理与重试五、性能优

SpringIntegration消息路由之Router的条件路由与过滤功能

《SpringIntegration消息路由之Router的条件路由与过滤功能》本文详细介绍了Router的基础概念、条件路由实现、基于消息头的路由、动态路由与路由表、消息过滤与选择性路由以及错误处理... 目录引言一、Router基础概念二、条件路由实现三、基于消息头的路由四、动态路由与路由表五、消息过滤

Spring Boot 3.4.3 基于 Spring WebFlux 实现 SSE 功能(代码示例)

《SpringBoot3.4.3基于SpringWebFlux实现SSE功能(代码示例)》SpringBoot3.4.3结合SpringWebFlux实现SSE功能,为实时数据推送提供... 目录1. SSE 简介1.1 什么是 SSE?1.2 SSE 的优点1.3 适用场景2. Spring WebFlu

基于SpringBoot实现文件秒传功能

《基于SpringBoot实现文件秒传功能》在开发Web应用时,文件上传是一个常见需求,然而,当用户需要上传大文件或相同文件多次时,会造成带宽浪费和服务器存储冗余,此时可以使用文件秒传技术通过识别重复... 目录前言文件秒传原理代码实现1. 创建项目基础结构2. 创建上传存储代码3. 创建Result类4.

Python+PyQt5实现多屏幕协同播放功能

《Python+PyQt5实现多屏幕协同播放功能》在现代会议展示、数字广告、展览展示等场景中,多屏幕协同播放已成为刚需,下面我们就来看看如何利用Python和PyQt5开发一套功能强大的跨屏播控系统吧... 目录一、项目概述:突破传统播放限制二、核心技术解析2.1 多屏管理机制2.2 播放引擎设计2.3 专

一文详解SpringBoot响应压缩功能的配置与优化

《一文详解SpringBoot响应压缩功能的配置与优化》SpringBoot的响应压缩功能基于智能协商机制,需同时满足很多条件,本文主要为大家详细介绍了SpringBoot响应压缩功能的配置与优化,需... 目录一、核心工作机制1.1 自动协商触发条件1.2 压缩处理流程二、配置方案详解2.1 基础YAML

Android Kotlin 高阶函数详解及其在协程中的应用小结

《AndroidKotlin高阶函数详解及其在协程中的应用小结》高阶函数是Kotlin中的一个重要特性,它能够将函数作为一等公民(First-ClassCitizen),使得代码更加简洁、灵活和可... 目录1. 引言2. 什么是高阶函数?3. 高阶函数的基础用法3.1 传递函数作为参数3.2 Lambda

Android自定义Scrollbar的两种实现方式

《Android自定义Scrollbar的两种实现方式》本文介绍两种实现自定义滚动条的方法,分别通过ItemDecoration方案和独立View方案实现滚动条定制化,文章通过代码示例讲解的非常详细,... 目录方案一:ItemDecoration实现(推荐用于RecyclerView)实现原理完整代码实现

使用PyTorch实现手写数字识别功能

《使用PyTorch实现手写数字识别功能》在人工智能的世界里,计算机视觉是最具魅力的领域之一,通过PyTorch这一强大的深度学习框架,我们将在经典的MNIST数据集上,见证一个神经网络从零开始学会识... 目录当计算机学会“看”数字搭建开发环境MNIST数据集解析1. 认识手写数字数据库2. 数据预处理的

Python实战之屏幕录制功能的实现

《Python实战之屏幕录制功能的实现》屏幕录制,即屏幕捕获,是指将计算机屏幕上的活动记录下来,生成视频文件,本文主要为大家介绍了如何使用Python实现这一功能,希望对大家有所帮助... 目录屏幕录制原理图像捕获音频捕获编码压缩输出保存完整的屏幕录制工具高级功能实时预览增加水印多平台支持屏幕录制原理屏幕