/*
* Copyright (C) 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.server.ethernet;
import android.annotation.Nullable;
import android.content.Context;
import android.net.IEthernetServiceListener;
import android.net.InterfaceConfiguration;
import android.net.IpConfiguration;
import android.net.IpConfiguration.IpAssignment;
import android.net.IpConfiguration.ProxySettings;
import android.net.LinkAddress;
import android.net.NetworkCapabilities;
import android.net.StaticIpConfiguration;
import android.os.Handler;
import android.os.IBinder;
import android.os.INetworkManagementService;
import android.os.RemoteCallbackList;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.text.TextUtils;
import android.util.ArrayMap;
import android.util.Log;
import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.util.IndentingPrintWriter;
import com.android.server.net.BaseNetworkObserver;
import java.io.FileDescriptor;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.concurrent.ConcurrentHashMap;
/**
* Tracks Ethernet interfaces and manages interface configurations.
*
* <p>Interfaces may have different {@link android.net.NetworkCapabilities}. This mapping is defined
* in {@code config_ethernet_interfaces}. Notably, some interfaces could be marked as restricted by
* not specifying {@link android.net.NetworkCapabilities.NET_CAPABILITY_NOT_RESTRICTED} flag.
* Interfaces could have associated {@link android.net.IpConfiguration}.
* Ethernet Interfaces may be present at boot time or appear after boot (e.g., for Ethernet adapters
* connected over USB). This class supports multiple interfaces. When an interface appears on the
* system (or is present at boot time) this class will start tracking it and bring it up. Only
* interfaces whose names match the {@code config_ethernet_iface_regex} regular expression are
* tracked.
*
* <p>All public or package private methods must be thread-safe unless stated otherwise.
*/
final class EthernetTracker {
private final static String TAG = EthernetTracker.class.getSimpleName();
private final static boolean DBG = EthernetNetworkFactory.DBG;
/** Product-dependent regular expression of interface names we track. */
private final String mIfaceMatch;
/** Mapping between {iface name | mac address} -> {NetworkCapabilities} */
private final ConcurrentHashMap<String, NetworkCapabilities> mNetworkCapabilities =
new ConcurrentHashMap<>();
private final ConcurrentHashMap<String, IpConfiguration> mIpConfigurations =
new ConcurrentHashMap<>();
private final INetworkManagementService mNMService;
private final Handler mHandler;
private final EthernetNetworkFactory mFactory;
private final EthernetConfigStore mConfigStore;
private final RemoteCallbackList<IEthernetServiceListener> mListeners =
new RemoteCallbackList<>();
private volatile IpConfiguration mIpConfigForDefaultInterface;
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 resources
final 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();
}
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);
}
//add for closr ethernet
public void stop() {
Log.d(TAG, "EthernetTracker stop ethernet...");
updateInterfaceState("eth0", false);
android.os.SystemClock.sleep(200);
removeInterface("eth0");
}
public IpConfiguration getDefaultIpConfiguration(){
IpConfiguration ipConfiguration = mIpConfigurations.get("eth0");
return ipConfiguration != null ? ipConfiguration :
new IpConfiguration(IpAssignment.DHCP, ProxySettings.NONE, null, null);
}
public void recoverDHCPIpConfiguration(){
mIpConfigurations.put("eth0", new IpConfiguration(IpAssignment.DHCP, ProxySettings.NONE, null, null));
}
//E
void updateIpConfiguration(String iface, IpConfiguration ipConfiguration) {
if (DBG) {
Log.i(TAG, "updateIpConfiguration, iface: " + iface + ", cfg: " + ipConfiguration);
}
mConfigStore.write(iface, ipConfiguration);
mIpConfigurations.put(iface, ipConfiguration);
mHandler.post(() -> mFactory.updateIpConfiguration(iface, ipConfiguration));
}
IpConfiguration getIpConfiguration(String iface) {
return mIpConfigurations.get(iface);
}
boolean isTrackingInterface(String iface) {
return mFactory.hasInterface(iface);
}
String[] getInterfaces(boolean includeRestricted) {
return mFactory.getAvailableInterfaces(includeRestricted);
}
/**
* Returns true if given interface was configured as restricted (doesn't have
* NET_CAPABILITY_NOT_RESTRICTED) capability. Otherwise, returns false.
*/
boolean isRestrictedInterface(String iface) {
final NetworkCapabilities nc = mNetworkCapabilities.get(iface);
return nc != null && !nc.hasCapability(NetworkCapabilities.NET_CAPABILITY_NOT_RESTRICTED);
}
void addListener(IEthernetServiceListener listener, boolean canUseRestrictedNetworks) {
mListeners.register(listener, new ListenerInfo(canUseRestrictedNetworks));
}
void removeListener(IEthernetServiceListener listener) {
mListeners.unregister(listener);
}
public void removeInterface(String iface) {
mFactory.removeInterface(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 (RemoteExce
没有合适的资源?快使用搜索试试~ 我知道了~
android P 9.0添加Ethernet功能(settings+framework).zip
共11个文件
java:5个
xml:5个
proto:1个
4星 · 超过85%的资源 需积分: 2 134 下载量 47 浏览量
2019-10-22
08:28:27
上传
评论 6
收藏 60KB ZIP 举报
温馨提示
文件列表 <br>res\xml\network_and_internet.xml res\drawable\ic_ethernet_cell.xml <br>res\values\strings.xml res\xml\ethernet_settings.xml <br>res\xml\ethernet_static_ip.xml <br>src\com\android\settings\ethernet\EthernetSettings.java <br>src\com\android\settings\ethernet\EthernetStaticIP.java <br>frameworks/opt/net/ethernet/java/com/android/server/ethernet/Ethe rnetNetworkFactory.java <br>frameworks/opt/net/ethernet/java/com/android/server/ethernet/EthernetServiceImpl.java <br>frameworks/opt/net/ethernet/java/com/android/server/ethernet/EthernetTracker.java <br>博客地址 https://blog.csdn.net/u012932409/article/details/102669395
资源推荐
资源详情
资源评论
收起资源包目录
android P 9.0添加Ethernet功能(settings+framework).zip (11个子文件)
settings
network_and_internet.xml 4KB
EthernetSettings.java 5KB
ethernet_static_ip.xml 2KB
EthernetStaticIP.java 12KB
ethernet_settings.xml 1KB
ic_ethernet_cell.xml 1KB
strings.xml 2KB
framework
EthernetTracker.java 17KB
base
proto
src
metrics_constants.proto 204KB
EthernetServiceImpl.java 12KB
EthernetNetworkFactory.java 16KB
共 11 条
- 1
资源评论
- zmc040883352020-07-25很不错的资源
- 安静的林哥2020-10-11不会用啊,报错,提示找不到ETHERNET服务
- glunoy2021-03-18解决了我的问题。感谢!
cczhengv
- 粉丝: 8843
- 资源: 16
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功