Android 13 有线以太网静态ip保存逻辑梳理分析

2024-04-18 01:52

本文主要是介绍Android 13 有线以太网静态ip保存逻辑梳理分析,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

源码环境:高通Android 13

这里特别说明,Android13中,ipconfig.txt配置文件目录有改变

以前:/data/misc/ethernet/ipconfig.txt
最新的有线网配置文件保存目录:
/data/misc/apexdata/com.android.tethering/misc/ethernet/ipconfig.txt

一、Android 版本影响
首先就是Android 11 与 12 的有线网络版本差距还是比较大的,源码目录也变了,之前目录frameworks/opt/net/ethernet/java/com/android/server/ethernet,现在改成了packages/modules/Connectivity/service-t/src/com/android/server/ethernet,所以如果之前只在11上面适配过,那么对于12来说,适配还是需要花费一点功夫,具体的差异在之后的部分会记录,但是12与13的有线网络差异就较小了,并且看起来,13的以太网接口以及逻辑比12来说更为完善。

二、配置以太网所需要的类
配置以太网所需要的java类大概有以下几个,其实这几个类从Android9开始就是以太网配置的主要java类了

(1)、frameworks/base/core/java/android/net/EthernetManager.java

EthernetManager.java此是上层管理以太网的类,我们常通过context.getSystemService(Context.ETHERNET_SERVICE)获得他的实例对象

(2)、packages/modules/Connectivity/framework/src/android/net/IpConfiguration.java

这个类主要就是用来配置IP状态的,包括动态和静态

(3)、packages/modules/Connectivity/framework/src/android/net/StaticIpConfiguration.java

这个类主要就是用来配置静态IP的,这个类之前也是在frameworks/base/core/java/android/net/路径下,12里面也移到了packages/modules/Connectivity/framework/src/android/net/下

三、有线以太网静态ip保存逻辑从源码逐步分析    

1、packages/modules/Connectivity/framework-t/src/android/net/EthernetManager.java public void setConfiguration(@NonNull String iface, @NonNull IpConfiguration config) {try {mService.setConfiguration(iface, config);//这里调用的是EthernetServiceImpl.java中的setConfiguration} catch (RemoteException e) {throw e.rethrowFromSystemServer();}}2、packages/modules/Connectivity/service-t/src/com/android/server/ethernet/EthernetServiceImpl.java/*** Set Ethernet configuration*/@Overridepublic void setConfiguration(String iface, IpConfiguration config) {throwIfEthernetNotStarted();PermissionUtils.enforceNetworkStackPermission(mContext);if (mTracker.isRestrictedInterface(iface)) {PermissionUtils.enforceRestrictedNetworkPermission(mContext, TAG);}// 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));//这里更新保存}3、packages/modules/Connectivity/service-t/src/com/android/server/ethernet/EthernetTracker.java
EthernetTracker中主要做了以下两件事 :
(1). 首先更新 ip config的,这个和静态ip相关;
(2). 根据iface,调用addInterface创建interfacevoid updateIpConfiguration(String iface, IpConfiguration ipConfiguration) {if (DBG) {Log.i(TAG, "updateIpConfiguration, iface: " + iface + ", cfg: " + ipConfiguration);}writeIpConfiguration(iface, ipConfiguration);mHandler.post(() -> {mFactory.updateInterface(iface, ipConfiguration, null, null);broadcastInterfaceStateChange(iface);});}private void writeIpConfiguration(@NonNull final String iface,@NonNull final IpConfiguration ipConfig) {mConfigStore.write(iface, ipConfig);//这里调用了EthernetConfigStore.java中的writemIpConfigurations.put(iface, ipConfig);}4、packages/modules/Connectivity/service-t/src/com/android/server/ethernet/EthernetConfigStore.javaprivate static final String CONFIG_FILE = "ipconfig.txt";private static final String FILE_PATH = "/misc/ethernet/";private static final String APEX_IP_CONFIG_FILE_PATH = ApexEnvironment.getApexEnvironment(TETHERING_MODULE_NAME).getDeviceProtectedDataDir() + FILE_PATH;public void write(String iface, IpConfiguration config) {write(iface, config, APEX_IP_CONFIG_FILE_PATH + CONFIG_FILE);//注意:这里Android13调整了保存路径,全路径:/data/misc/apexdata/com.android.tethering/misc/ethernet/ipconfig.txt//Android 10版本保存路径为:/data/misc/ethernet/ipconfig.txt}void write(String iface, IpConfiguration config, String filepath) {boolean modified;synchronized (mSync) {if (config == null) {modified = mIpConfigurations.remove(iface) != null;} else {IpConfiguration oldConfig = mIpConfigurations.put(iface, config);modified = !config.equals(oldConfig);}if (modified) {mStore.writeIpConfigurations(filepath, mIpConfigurations);//这里调用的是IpConfigStore.java}}}5、packages/modules/Connectivity/service-t/src/com/android/server/net/IpConfigStore.java/***  Write the IP configuration associated to the target networks to the destination path.*/public void writeIpConfigurations(String filePath,ArrayMap<String, IpConfiguration> networks) {mWriter.write(filePath, out -> {out.writeInt(IPCONFIG_FILE_VERSION);for (int i = 0; i < networks.size(); i++) {writeConfig(out, networks.keyAt(i), networks.valueAt(i));}});}private static boolean writeConfig(DataOutputStream out, String configKey,IpConfiguration config) throws IOException {return writeConfig(out, configKey, config, IPCONFIG_FILE_VERSION);}//这里最终完成静态ip写到配置文件:/data/misc/apexdata/com.android.tethering/misc/ethernet/ipconfig.txtpublic static boolean writeConfig(DataOutputStream out, String configKey,IpConfiguration config, int version) throws IOException {boolean written = false;try {switch (config.getIpAssignment()) {case STATIC:out.writeUTF(IP_ASSIGNMENT_KEY);out.writeUTF(config.getIpAssignment().toString());StaticIpConfiguration staticIpConfiguration = config.getStaticIpConfiguration();if (staticIpConfiguration != null) {if (staticIpConfiguration.getIpAddress() != null) {LinkAddress ipAddress = staticIpConfiguration.getIpAddress();out.writeUTF(LINK_ADDRESS_KEY);out.writeUTF(ipAddress.getAddress().getHostAddress());out.writeInt(ipAddress.getPrefixLength());}if (staticIpConfiguration.getGateway() != null) {out.writeUTF(GATEWAY_KEY);out.writeInt(0);  // Default route.out.writeInt(1);  // Have a gateway.out.writeUTF(staticIpConfiguration.getGateway().getHostAddress());}for (InetAddress inetAddr : staticIpConfiguration.getDnsServers()) {out.writeUTF(DNS_KEY);out.writeUTF(inetAddr.getHostAddress());}}written = true;break;case DHCP:out.writeUTF(IP_ASSIGNMENT_KEY);out.writeUTF(config.getIpAssignment().toString());written = true;break;case UNASSIGNED:/* Ignore */break;default:loge("Ignore invalid ip assignment while writing");break;}switch (config.getProxySettings()) {case STATIC:ProxyInfo proxyProperties = config.getHttpProxy();String exclusionList = ProxyUtils.exclusionListAsString(proxyProperties.getExclusionList());out.writeUTF(PROXY_SETTINGS_KEY);out.writeUTF(config.getProxySettings().toString());out.writeUTF(PROXY_HOST_KEY);out.writeUTF(proxyProperties.getHost());out.writeUTF(PROXY_PORT_KEY);out.writeInt(proxyProperties.getPort());if (exclusionList != null) {out.writeUTF(EXCLUSION_LIST_KEY);out.writeUTF(exclusionList);}written = true;break;case PAC:ProxyInfo proxyPacProperties = config.getHttpProxy();out.writeUTF(PROXY_SETTINGS_KEY);out.writeUTF(config.getProxySettings().toString());out.writeUTF(PROXY_PAC_FILE);out.writeUTF(proxyPacProperties.getPacFileUrl().toString());written = true;break;case NONE:out.writeUTF(PROXY_SETTINGS_KEY);out.writeUTF(config.getProxySettings().toString());written = true;break;case UNASSIGNED:/* Ignore */break;default:loge("Ignore invalid proxy settings while writing");break;}if (written) {out.writeUTF(ID_KEY);if (version < 3) {out.writeInt(Integer.valueOf(configKey));} else {out.writeUTF(configKey);}}} catch (NullPointerException e) {loge("Failure in writing " + config + e);}out.writeUTF(EOS);return written;}

这篇关于Android 13 有线以太网静态ip保存逻辑梳理分析的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!


原文地址:
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.chinasem.cn/article/913456

相关文章

Olingo分析和实践之EDM 辅助序列化器详解(最佳实践)

《Olingo分析和实践之EDM辅助序列化器详解(最佳实践)》EDM辅助序列化器是ApacheOlingoOData框架中无需完整EDM模型的智能序列化工具,通过运行时类型推断实现灵活数据转换,适用... 目录概念与定义什么是 EDM 辅助序列化器?核心概念设计目标核心特点1. EDM 信息可选2. 智能类

Olingo分析和实践之OData框架核心组件初始化(关键步骤)

《Olingo分析和实践之OData框架核心组件初始化(关键步骤)》ODataSpringBootService通过初始化OData实例和服务元数据,构建框架核心能力与数据模型结构,实现序列化、URI... 目录概述第一步:OData实例创建1.1 OData.newInstance() 详细分析1.1.1

Olingo分析和实践之ODataImpl详细分析(重要方法详解)

《Olingo分析和实践之ODataImpl详细分析(重要方法详解)》ODataImpl.java是ApacheOlingoOData框架的核心工厂类,负责创建序列化器、反序列化器和处理器等组件,... 目录概述主要职责类结构与继承关系核心功能分析1. 序列化器管理2. 反序列化器管理3. 处理器管理重要方

MySQL逻辑删除与唯一索引冲突解决方案

《MySQL逻辑删除与唯一索引冲突解决方案》本文探讨MySQL逻辑删除与唯一索引冲突问题,提出四种解决方案:复合索引+时间戳、修改唯一字段、历史表、业务层校验,推荐方案1和方案3,适用于不同场景,感兴... 目录问题背景问题复现解决方案解决方案1.复合唯一索引 + 时间戳删除字段解决方案2:删除后修改唯一字

SpringBoot中六种批量更新Mysql的方式效率对比分析

《SpringBoot中六种批量更新Mysql的方式效率对比分析》文章比较了MySQL大数据量批量更新的多种方法,指出REPLACEINTO和ONDUPLICATEKEY效率最高但存在数据风险,MyB... 目录效率比较测试结构数据库初始化测试数据批量修改方案第一种 for第二种 case when第三种

解决1093 - You can‘t specify target table报错问题及原因分析

《解决1093-Youcan‘tspecifytargettable报错问题及原因分析》MySQL1093错误因UPDATE/DELETE语句的FROM子句直接引用目标表或嵌套子查询导致,... 目录报js错原因分析具体原因解决办法方法一:使用临时表方法二:使用JOIN方法三:使用EXISTS示例总结报错原

MySQL中的LENGTH()函数用法详解与实例分析

《MySQL中的LENGTH()函数用法详解与实例分析》MySQLLENGTH()函数用于计算字符串的字节长度,区别于CHAR_LENGTH()的字符长度,适用于多字节字符集(如UTF-8)的数据验证... 目录1. LENGTH()函数的基本语法2. LENGTH()函数的返回值2.1 示例1:计算字符串

Android kotlin中 Channel 和 Flow 的区别和选择使用场景分析

《Androidkotlin中Channel和Flow的区别和选择使用场景分析》Kotlin协程中,Flow是冷数据流,按需触发,适合响应式数据处理;Channel是热数据流,持续发送,支持... 目录一、基本概念界定FlowChannel二、核心特性对比数据生产触发条件生产与消费的关系背压处理机制生命周期

Android ClassLoader加载机制详解

《AndroidClassLoader加载机制详解》Android的ClassLoader负责加载.dex文件,基于双亲委派模型,支持热修复和插件化,需注意类冲突、内存泄漏和兼容性问题,本文给大家介... 目录一、ClassLoader概述1.1 类加载的基本概念1.2 android与Java Class

怎样通过分析GC日志来定位Java进程的内存问题

《怎样通过分析GC日志来定位Java进程的内存问题》:本文主要介绍怎样通过分析GC日志来定位Java进程的内存问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、GC 日志基础配置1. 启用详细 GC 日志2. 不同收集器的日志格式二、关键指标与分析维度1.