There are a lot of articles on porting Ethernet frameworks on the Internet, so I will not talk about it here. I will mainly talk about how to modify the default status. As follows, I have not studied it clearly.
First, the service is built on:
Frameworks/base/services/Java/COM/Android/Server/connectivityservice. Java
The following code is available:
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();
This should be run at the beginning of execution. That is to say, the EST. startmonitoring () is executed after the service is created ();
This function is in:
Frameworks/base/Ethernet/Java/Android/NET/Ethernet/ethernetstatetracker. Java
The Code is as follows:
@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 ();
The getstate function in ethernetmanager is called in:
Frameworks/base/Ethernet/Java/Android/NET/Ethernet/ethernetmanager. Java
The Code is as follows:
/** * Get ethernet service state * @return the state of the ethernet service */ public int getState( ) { try { return mService.getState(); } catch (RemoteException e) { return 0; } }
Here, the getstate function in the service is called in:
Frameworks/base/services/Java/COM/Android/Server/ethernetservice. Java
The Code is as follows:
/** * Get ethernet service state * @return the state of the ethernet service */ public int getState( ) { return mEthState; }
The initial methstate value is:
Private int methstate = ethernetmanager. ethernet_state_unknown;
That is to say, the system will run mem. setenabled (true) in startmonitoring for the first time. The Code is as follows:
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"); } }
Here, the setstate code in the service is called as follows:
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"); } } } }
At this time, it is passed to the internal ethernetmanager. ethernet_state_enabled status.
In the Ethernet settings, the status is retrieved from the internal device, that is, the status is enable. This is the first time, that is, the default status. Therefore
Modify startmonitoring in 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(false);//modi hclydao //mEm.setEnabled(true); } else { try { resetInterface(); } catch (UnknownHostException e) { Slog.e(TAG, "Wrong ethernet configuration"); } } } } }
Compiled and run successfully. At the same time, this does not affect the Ethernet status you set.
As for the subsequent Ethernet setting process, I will not analyze it here, because I am also a bit confused. If there are any errors above, I hope you can point them out.