Android 2.3 dial-up Internet access process from the source code perspective

Source: Internet
Author: User

In general, if we want to use the SIM card dial-up Internet access function, we need to make a simple configuration in the settings, The procedure is as follows::
Set-wireless and network-mobile network-(data/data roaming/Access Point Name Enabled/only use 2G network/network operator)
We must select the "data enabled" option, and then configure the Access Point name to access the Internet. Of course, some settings have set the access point according to your SIM card type by default, in this case, you can set the Internet function only after selecting "data enabled.
What have these setup steps been done? Now we will analyze the source code.

1. First, find "mobile network" Settings UI ------- Settings. java (/packages/apps/Phone/src/com/android/phone/Settings. java)
Settings. java:
The code for the "data enabled" option is as follows:Copy codeThe Code is as follows :......
Else if (preference = mButtonDataEnabled ){
If (DBG) log ("onPreferenceTreeClick: preference = mButtonDataEnabled .");
ConnectivityManager cm =
(ConnectivityManager) getSystemService (Context. CONNECTIVITY_SERVICE );
Cm. setMobileDataEnabled (mButtonDataEnabled. isChecked ());
Return true;
}
......

In the code, we get a ConnectivityManager object and call the setMobileDataEnable (boolean B) method of this object. We can set it based on the input parameters. Let's take a look at the ConnectivityManager class.

2. ConnectivityManager. java (/frameworks/base/core/java/android/net/ConnectivityManager. java)
At this time, the data has entered the frameworks layer.
The setMobileDataEnable () method code is as follows:Copy codeThe Code is as follows: IConnectivityManager mService;
......
Public ConnectivityManager (IConnectivityManager service ){
If (service = null ){
Throw new IllegalArgumentException (
"ConnectivityManager () cannot be constructed with null service ");
}
MService = service;
}
......
Public void setMobileDataEnabled (boolean enabled ){
Try {
MService. setMobileDataEnabled (enabled );
} Catch (RemoteException e ){
}
}

Here we need to know that the IConnectivityManager class is based on IConnectivityManager. A java class automatically generated by the aidl interface, and one of our own services inherits the internal class of this class: Stub. In our own implementation for dial-up Internet access, this Service is ConnectivityService, therefore, according to AIDL, we know that the mService in the Code is actually the ConnectivityService class object, so the Code actually calls the setMobileDataEnable () method of the ConnectivityService object.

3. ConnectivityService. java (/frameworks/./base/services/java/com/android/server/ConnectivityService. java)
The setMobileDataEnable () method code is as follows:Copy codeThe Code is as follows: public void setMobileDataEnabled (boolean enabled ){
EnforceChangePermission ();
If (DBG) Slog. d (TAG, "setMobileDataEnabled (" + enabled + ")");
MHandler. sendMessage (mHandler. obtainMessage (EVENT_SET_MOBILE_DATA,
(Enabled? ENABLED: DISABLED), 0 ));
}

A message is sent here. After mHandler receives the message:Copy codeThe Code is as follows: case EVENT_SET_MOBILE_DATA:
{
Boolean enabled = (msg. arg1 = ENABLED );
HandleSetMobileData (enabled );
Break;
}

After receiving the message, call the handleSetMobileData () method:Copy codeThe Code is as follows: private NetworkStateTracker mNetTrackers [];
......
Private void handleSetMobileData (boolean enabled ){
......
If (enabled ){
If (mNetTrackers [ConnectivityManager. TYPE_MOBILE]! = Null ){
If (DBG ){
Slog. d (TAG, "starting up" + mNetTrackers [ConnectivityManager. TYPE_MOBILE]);
}
MNetTrackers [ConnectivityManager. TYPE_MOBILE]. reconnect ();
}
......
}
}

If the "enabled data" option has been selected, the "enabled" parameter passed in at this time should be "true", so the if statement block in the code will be processed, that is, the execution:Copy codeThe Code is as follows: mNetTrackers [ConnectivityManager. TYPE_MOBILE]. reconnect ();

In ConnectivityManager, TYPE_MOBILE is 0, so it is equivalent to callingCopy codeThe Code is as follows: mNetTracker [0]. reconnect ()

However, NetworkStateTracker is an abstract class. Therefore, the specific task must be handled by its subclass MobileDataStateTracker. java.

4. MobileDataStateTracker. java (/frameworks/base/core/java/android/net/MobileDataStateTracker. java)
This class includes multiple data connections, including MMS, SUPL, DUN, etc,
The Calling process in MobileDataStateTracker. java is as follows:Copy codeThe code is as follows: <PRE class = java name = "code"> mPhoneService = ITelephony. stub. asInterface (ServiceManager. getService ("phone"); </PRE> ...... <BR>
Reconnect-> mPhoneService. enableApnType (apnType); <P> </P>
<PRE> </PRE>
MPhoneService is the client of the telephone service, and its server is actually a PhoneInterfaceManager object
<P> </P>
<P> 5. PhoneInterfaceManager. java (/packages/apps/Phone/src/com/android/phone/PhoneInterfaceManager. java) <BR>
</P>
<P> check the enableApnType method of PhoneInterfaceManager: </P>
<P> <PRE class = java name = "code"> public int enableApnType (String type ){
EnforceModifyPermission ();
Return mPhone. enableApnType (type );
}
</PRE> <P> </P>
In this way, the request to connect to the apn is sent to the telephony framework layer. Apn is specified in the setting application, usually in the system/etc/apns-conf.xml file under your project directory <BR>
<BR>
<P> 6. The above mPhone is a PhoneProxy object. </P>
<P> call process: </P>
<P> PhoneProxy. java: <BR>
</P>
<P> <PRE class = java name = "code"> mActivePhone. enableApnType (type) </PRE> mActivePhone is the PhoneBase object of the upstream interface of GSMPhone or CDMAPhone. <BR>
<P> </P>
<P> PhoneBase. java: </P>
<P> <PRE class = java name = "code"> mDataConnection. enableApnType (type); </PRE> <P> </P>
<P> call the enableApnType method of DataConnectionTracker </P>
<P> DataConnectionTracker. java: <BR>
</P>
<P> enableApnType (String type)-> setEnabled-> onEnableApn-> onEnableNewApn <BR>
</P>
<BR>
<P> the onEnableNewApn method is implemented in the DataConnectionTracker and CdmaDataConnectionTracker Derived classes of DataConnectionTracker to differentiate the data connection processes of different PHONE types. <BR>
</P>
<P> take GSM as an example. Call procedure: onEnableNewApn-> cleanUpConnection-> conn. disconnect <BR>
<BR>
</P>
Conn is a DataConnection object that identifies a data connection within one minute. It can be seen that a data connection state machine is actually implemented here. <BR>
<P> the data connection status in the DataConnection object is divided into: </P>
<P> <PRE class = java name = "code"> DcDefaultState, default state.
DcInactiveState: inactive.
DcActivatingState, active
DcActiveState, activation status
DcDisconnectingState, disconnected
DcDisconnectingBadDnsState, disconnected (due to wrong DNS)
</PRE> <P> </P>
<P> after the connection is successful, notifydefadata calls yphphonenotifier's notifyDataConnection method. </P>
<P> DefaultPhoneNotifier is the client of the ITelephonyRegistry interface, and its server is TelephonyRegistry (com. android. server. TelephonyRegistry) </P>
<P> use the following statement to call TelephonyRegistry's notifyDataConnection method <BR>
<PRE class = java name = "code"> r. callback. onDataConnectionStateChanged (state, networkType); </PRE> <P> </P>
<P> r is an element in the current mRecords and contains the implementation callback of the IPhoneStateListener interface. Each call in TelephonyRegistry traverses the elements in mRecords. If an element registers a corresponding answer, </P>
<P> call a function of callback. </P>
<P> the client calls the listener to obtain the phone status in the following way. Take mPhoneStateListener in StatusBarPolicy. java as an example: </P>
<P> (TelephonyManager) mContext. getSystemService (Context. TELEPHONY_SERVICE) </P>
<P>. listen (mPhoneStateListener, <BR>
PhoneStateListener. LISTEN_SERVICE_STATE <BR>
| PhoneStateListener. LISTEN_SIGNAL_STRENGTHS <BR>
| PhoneStateListener. LISTEN_CALL_STATE <BR>
| PhoneStateListener. LISTEN_DATA_CONNECTION_STATE <BR>
| PhoneStateListener. LISTEN_DATA_ACTIVITY); <BR>
</P>
<P> mPhoneStateListener is a PhoneStateListener instance. PhoneStateListener implements the IPhoneStateListener interface. If you inherit the PhoneStateListener subclass, you must first determine the listener you are interested in. </P>
<P> event, and then override the corresponding method. Call the listen method as above. </P>
<P> the correspondence between the TelephonyRegistry method, listener action, and method you want to override is as follows: </P>
<P> TelephonyRegistry method --------------------- listener action --------------------------------------------------------- callback in the PhoneStateListener subclass <BR>
</P>
<P> policyservicestate ---------- PhoneStateListener. LISTEN_SERVICE_STATE --------------- public void onServiceStateChanged (ServiceState state) <BR>
</P>
<P> policysignalstrength ------- PhoneStateListener. LISTEN_SIGNAL_STRENGTHS --------- -- public void onSignalStrengthsChanged (SignalStrength signalStrength) <BR>
</P>
<P> policycallstate -------------- PhoneStateListener. LISTEN_CALL_STATE ------------------------- public void onCallStateChanged (int state, String incomingNumber) <BR>
</P>
<P> policydataconnection ------- PhoneStateListener. LISTEN_DATA_CONNECTION_STATE --- public void onDataConnectionStateChanged (int state, int networkType) <BR>
</P>
<P> policydataactivity ------------ PhoneStateListener. LISTEN_DATA_ACTIVITY ----------------------- public void onDataActivity (int direction) <BR>
</P>
<P> ........ </P>
<P> therefore, the entire call chain is DefaultPhoneNotifier: yydataconnection --------- TelephonyRegistry: policydataconnection ---------. </P>
<P> PhoneStateListener. callback: onDataConnectionStateChanged -------------- onDataConnectionStateChanged of the PhoneStateListener subclass </P>
<P> In addition, TelephonyRegistry sends an ACTION_ANY_DATA_CONNECTION_STATE_CHANGED message, which contains detailed information about the data connection. </P>
<P> <BR>
The MobileDataStateTracker in Mobile Data Service receives this action. The BoadcastReceiver class MobileDataStateReceiver extracts the Data connection information and sets the status. </P>
<PRE class = java name = "code"> setDetailedState (DetailedState. CONNECTING, reason, apnName );
</PRE>
<P> MobileDataStateTracker sends the EVENT_STATE_CHANGED message to ConnectivityService Based on the status change. </P>
<P> ConnectivityService calls handleConnect to execute related hype, including disabling data connections with lower priority and updating the status bar. <BR>
</P>
<P> I still haven't figured out many other things. I will continue later. <BR>
</P>
<P> <BR>
</P>
<P> <BR>
</P>
<BR>
<P> <BR>
<BR>
</P>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.