网上移植ethernet框架的文章很多,这里就不多说了,主要说下如何修改默认状态。如下就简单的说下,我也还没有研究的很清楚。
首先服务的建在:
frameworks/base/services/java/com/android/server/ConnectivityService.java
有如下代码:
EthernetStateTracker est = new EthernetStateTracker(context, mHandler);
EthernetService ethService = new EthernetService(context, est);
ServiceManager.addService(Context.ETHERNET_SERVICE, ethService);
mNetTrackers[ConnectivityManager.TYPE_ETHERNET] = est;
est.startMonitoring();
这个应该是最开始执行的时候运行的。也就是说服务建后执行了这个est.startMonitoring();
这个函数在:
frameworks/base/ethernet/java/android/net/ethernet/EthernetStateTracker.java
代码如下:
@Override
public void startMonitoring() {
if (localLOGV) Slog.v(TAG,"start to monitor the ethernet devices");
if (mServiceStarted) {
mEM = (EthernetManager)mContext.getSystemService(Context.ETHERNET_SERVICE);
int state = mEM.getState();
if (state != mEM.ETHERNET_STATE_DISABLED) {
if (state == mEM.ETHERNET_STATE_UNKNOWN) {
// maybe this is the first time we run, so set it to enabled
mEm.setEnabled(true);
} else {
try {
resetInterface();
} catch (UnknownHostException e) {
Slog.e(TAG, "Wrong ethernet configuration");
}
}
}
}
}
其中state=mEm.getState();
调用的是EthernetManager里面的getState这个函数在:
frameworks/base/ethernet/java/android/net/ethernet/EthernetManager.java
代码如下:
/**
* Get ethernet service state
* @return the state of the ethernet service
*/
public int getState( ) {
try {
return mService.getState();
} catch (RemoteException e) {
return 0;
}
}
而这里又调用的Service里面的getState函数在:
frameworks/base/services/java/com/android/server/EthernetService.java
代码如下:
/**
* Get ethernet service state
* @return the state of the ethernet service
*/
public int getState( ) {
return mEthState;
}
而MethState初始化的值为:
private int mEthState= EthernetManager.ETHERNET_STATE_UNKNOWN;
也就是说系统第一次运行到startMonitoring里会运行mEm.setEnabled(true);代码如下:
EthernetManager.java
/**
* Enable or Disable a ethernet service
* @param enable {@code true} to enable, {@code false} to disable
* @hide
*/
public void setEnabled(boolean enable) {
try {
mService.setState(enable ? ETHERNET_STATE_ENABLED:ETHERNET_STATE_DISABLED);
} catch (RemoteException e) {
Slog.i(TAG,"Can not set new state");
}
}
而这里又会调用到service里面的setState代码如下:这里会传入enable给setState
EthernetService.java
private synchronized void persistEnabled(boolean enabled) {
final ContentResolver cr = mContext.getContentResolver();
Settings.Secure.putInt(cr, Settings.Secure.ETHERNET_ON, enabled ? EthernetManager.ETHERNET_STATE_ENABLED : EthernetManager.ETHERNET_STATE_DISABLED);
}
/**
* Enable or Disable a ethernet service
* @param enable {@code true} to enable, {@code false} to disable
*/
public synchronized void setState(int state) {
if (mEthState != state) {
mEthState = state;
if (state == EthernetManager.ETHERNET_STATE_DISABLED) {
persistEnabled(false);
mTracker.stopInterface(false);
} else {
persistEnabled(true);
if (!isConfigured()) {
// If user did not configure any interfaces yet, pick the first one
// and enable it.
setMode(EthernetDevInfo.ETHERNET_CONN_MODE_DHCP);
}
try {
mTracker.resetInterface();
} catch (UnknownHostException e) {
Slog.e(TAG, "Wrong ethernet configuration");
}
}
}
}
此时会传给内部内EthernetManager.ETHERNET_STATE_ENABLED状态。
而在设置ethernet的里面会从内部内取出他的状态,即此时为enable状态,这里是第一次也就是默认的。所以把
EthernetStateTracker.java里面的startMonitoring修改为:
@Override
public void startMonitoring() {
if (localLOGV) Slog.v(TAG,"start to monitor the ethernet devices");
if (mServiceStarted) {
mEM = (EthernetManager)mContext.getSystemService(Context.ETHERNET_SERVICE);
int state = mEM.getState();
if (state != mEM.ETHERNET_STATE_DISABLED) {
if (state == mEM.ETHERNET_STATE_UNKNOWN) {
// maybe this is the first time we run, so set it to enabled
mEM.setEnabled(false);//modi hclydao
//mEm.setEnabled(true);
} else {
try {
resetInterface();
} catch (UnknownHostException e) {
Slog.e(TAG, "Wrong ethernet configuration");
}
}
}
}
}
编译运行,成功。同时这不影响你设置的ethernet的状态。
至于后面的设置ethernet的流程这里就不分析了,因为我也是一知半解。前面如果有什么错误的地方希望大家指出。