Android Source code Analysis: Telephony part –gsmphone

Source: Internet
Author: User

Android Source code Analysis: Telephony part –gsmphone
Red Wolf Blog

Phoneproxy/gsmphone/cdmaphone

If Rilj provides a tool or pipeline, then the subclass and Phonefactory of the phone interface are packages/app/phone this application process to use RILJ this tool or pipeline to provide great convenience, They are a telephony function that manages the entire phone.

Gsmphone and Cdmaphone implement the interfaces defined in the phone. The interface class phone defines a set of APIs that are used to send an AT command request using RILJ (see the Ril class below), as well as a register and unregister function, and the corresponding register function can be called when the caller is interested in some internal state. When the state changes, you can get timely notification.

Phonebase implements some of the functions defined in the phone interface, some of which are implemented by its subclasses Gsmphone and Cdmaphone. PhoneProxy is the agent of Gsmphone and Cdmaphone, so that users do not have to focus on whether the phone is GSM or CDMA, it complies with the API interface defined by the phone, so inherit the phone.

Phonefactory when you create a phone object, you have a PhoneProxy object that PhoneProxy create a corresponding gsmphone or cdmaphone based on the actual network type.

Phonefactory also has an Commandinterface interface object, which is an instance of Ril, which is passed to Gsmphone or Cdmaphone, that is, gsmphone or cdmaphone references it, and implements interaction with Rild.

Gsmphone and Cdmaphone inherit from Phonebase, and they (including PhoneProxy) are a handler. In this way, they can process the various messages from Rilj or themselves sent in the loop looper.loop of the thread (as long as the destination handler is specified in the message).

Phonefactory is responsible for creating individual phone instances whose members and member functions are static, which guarantees the uniqueness of the created instance when the system is running.

Phonefactory owns the PhoneProxy object (the function of the actual agent Gsmphone or Cdmaphone), the Ril object, the Phonenotofier object, and the Looper object, and the context object is made up of the upper application Java The app passes over, that's an activity. Android apps can use Phonefactory.getdefaultphone to get the phone object, making some call operations.

The code snippets for instances such as Phonenotifier and Ril are created when phonefactory creates some kind of phone:

Sphonenotifier = new Defaultphonenotifier ();

Get network type
Get Preferrednetworkmode from Settings.system
int networkmode = Settings.Secure.getInt (Context.getcontentresolver (),
Settings.Secure.PREFERRED_NETWORK_MODE, Preferrednetworkmode);

LOG.I (Log_tag, "Network Mode set to" + integer.tostring (Networkmode));

Get Preferrednetworkmode from Settings.system
int cdmasubscription = Settings.Secure.getInt (Context.getcontentresolver (), Settings.Secure.PREFERRED_CDMA_ SUBSCRIPTION, preferredcdmasubscription);

LOG.I (Log_tag, "CDMA Subscription set to" + integer.tostring (cdmasubscription));

Reads the system properties and makes Commandsinterface
Scommandsinterface = new RIL (context, Networkmode, cdmasubscription);//Create RIL Object

Get mobile phone Phone type GSM or CDMA based on network type
int phonetype = Getphonetype (Networkmode);

The PhoneProxy object, which proxies different phone objects based on the phone type
if (Phonetype = = Rilconstants.gsm_phone) {
Sproxyphone = new PhoneProxy (new Gsmphone (context,
Scommandsinterface, Sphonenotifier));
LOG.I (Log_tag, "Creating gsmphone");
} else if (Phonetype = = Rilconstants.cdma_phone) {
Sproxyphone = new PhoneProxy (new Cdmaphone (context,
Scommandsinterface, Sphonenotifier));
LOG.I (Log_tag, "Creating cdmaphone");
}

In Gsmphone and Cdmaphone, you create individual modules that are related to telephony features, and the following code snippet is from the constructor of Gsmphone:

The preceding section of the code is used to create various objects, such as the Gsmcalltracker object MCT, the following register function is used to register Gsmphone is interested in a state, when the state changes will be notified, by handler (i.e., itself this) to handle, Gsmpphone itself is a handler that can handle the message sent over these state changes.

Mcm.setphonetype (PHONE.PHONE_TYPE_GSM);

MCT = new Gsmcalltracker (this);
Msst = new Gsmservicestatetracker (this);
MSMS = new Gsmsmsdispatcher (this);
Miccfilehandler = new Simfilehandler (this);
Msimrecords = new Simrecords (this);
Mdataconnection = new Gsmdataconnectiontracker (this);
Msimcard = new Simcard (this);
if (!unittestmode) {
Msimphonebookintmanager = new
Simphonebookinterfacemanager (this);
Msimsmsintmanager = new Simsmsinterfacemanager (this);
Msubinfo = new Phonesubinfo (this);
}
Mstkservice = Stkservice.getinstance (MCM, Msimrecords, Mcontext,
(Simfilehandler) Miccfilehandler, Msimcard);

Mcm.registerforavailable (this, event_radio_available, null);
Msimrecords.registerforrecordsloaded (This,
event_sim_records_loaded, NULL);
Mcm.registerforoffornotavailable (This,
Event_radio_off_or_not_available, NULL);
Mcm.registerforon (this, event_radio_on, null);
MCM.SETONUSSD (this, EVENT_USSD, null);
Mcm.setonsuppservicenotification (this, event_ssn, null);
Msst.registerfornetworkattach (this, event_registered_to_network, null);

The class instances owned by Gsmphone and Cdmaphone are revealed, as well as the inheritance relationships of classes of those instances. The upper half of the graph is handler, so you can handle the message on various thread queues, see their handlermessage function. The next half of the classes inherit from Xxx.stub, which is a concrete implementation of the server side. Callers such as the Java APP (client) through the Aidl interface will eventually invoke the functions they implement.

Code Analysis Example –gsmcalltracker

Gsmcalltracker realizes the call (Dial), answer/Reject (Accept/reject), Hang Up (hangup), hold, switch and conference call, etc., it is also responsible for querying the modem how many road calls, Maintain phone status and other functions.

The Gsmcalltracker contains object instances of classes such as Gsmconnection, Registrantlist, Gsmcall, and Phone.state.

The gsmcall contains the ability to support multiple calls, each calling means a call connection gsmconnection (up to 5) when they are in disconnected state, the gsmcall is idle.

Registrantlist is registered with the Ril class instance in the Gsmcalltracker constructor, and Gsmcalltracker is notified when the call state and the radio frequency Radio state change:

Gsmcalltracker (Gsmphone phone) {
This.phone = phone;
CM = PHONE.MCM;

Cm.registerforcallstatechanged (this, event_call_state_change, null);
Cm.registerforon (this, event_radio_available, null);
Cm.registerfornotavailable (this, event_radio_not_available, null);

And Gsmcalltracker's handlemessage will be dealt with accordingly:

Case Event_call_state_change:
Pollcallswhensafe ();
Break
Case Event_radio_available:
Handleradioavailable ();
Break
Case Event_radio_not_available:
Handleradionotavailable ();

They eventually call the Pollcallswhensafe to query the current call state.

Dial: Make a call. It first cleardisconnected () and Candial () empties the past non-connected state of connections, and then checks to see if the phone can be dialed. Then check if Foregroundcall is in active state, and if you call switchwaitingorholdingandactive to switch them to the background, Call fakeholdforegroundbeforedial to empty the connection in the foreground to the background and the status to Holding. After these preliminary checks and preparations, create a gsmconnection instance that is Pendingmo, check that the delivered phone number is valid, and if it is not legal, call Pollcallswhensafe () to mark it as dropped If the law is set to non-muted, call Ril. dial to dial. Finally, update the phone status and notify the registrant.

Acceptcall: Answer the phone. If Ringingcall is in incoming, call Ril.acceptcall to answer the call, and if the waiting state, call switchwaitingorholdingandactive to switch it to the foreground.

Rejectcall: Refuse to answer the phone. When Ringingcall is in incoming, the call Ril.rejectcall is rejected, otherwise an exception is thrown, which means that there is no call but to answer it.

Hangup: Hang up the phone of a gsmcall. It distinguishes between Ringingcall, Foregroundcall or Backgroundcall. If ringingcall, call Ril.hangupwaitingorbackground if Foregroundcall, and call alerting in dialing or Hangup state ( Gsmconnection conn) hang up or call Hangupforegroundresumebackground to resume back-office calls after the front-end call is hung, if backgroundcall and ringingcall are ringing, Call Hangupallconnections to hang up all call connections in Backgroundcall, otherwise call Hangupwaitingorbackground to hang up call waiting and background calls.

When the above function API functions are complete, they are handled by the following case branch:

Case Event_operation_complete:
AR = (AsyncResult) msg.obj;
Operationcomplete ();
Break

Explicitcalltransfer: Call Ril.explicitcalltransfer for Exchange, the message returned by the at execution completes by the case branch in Handlermessage event_ect_ Result is processed.

Switchwaitingorholdingandactive () : Call ril.witchwaitingorholdingandactive to switch, and the message returned by the at execution is handled by the case branch Event_switch_result in Handlermessage.

Conference (): Call ril.conference for a conference call, and the message returned by the at execution is processed by the case branch Event_conference_result in Handlermessage.

Separate: Call Ril.separateconnection to detach a call, and the message returned by the at execution is processed by the case branch Event_separate_result in Handlermessage.

The results of their execution are handled as follows:

Case Event_switch_result:
Case Event_conference_result:
Case Event_separate_result:
Case Event_ect_result:
AR = (AsyncResult) msg.obj;
if (ar.exception! = null) {
Phone.notifysuppservicefailed (Getfailedservice (msg.what));
}
Operationcomplete ();
Break

In the case where no exception occurs, Operationcomplete () is called to check if it is the last operation, and if the Event_poll_calls_result type message is sent, it continues to be handled by Handlermessage:

Case Event_poll_calls_result:
AR = (AsyncResult) msg.obj;//get loopback results
if (msg = = Lastrelevantpoll) {
if (dbg_poll) log (
"Handle Event_poll_call_result:set needspoll=f");
Needspoll = false;
Lastrelevantpoll = null;
Handlepollcalls ((AsyncResult) msg.obj);//Processing results
}
Break

Handlepollcalls processing the query results, the call list in the loopback modem is resolved one by one.

Other functions explained:

Fakeholdforegroundbeforedial () After the phone connection (gsmconnections) in the foreground phone (foregroundcall) in the background, the connection is deleted, the foreground is set to the idle state, Resets the background phone backgroundcall to the holding state.

Cleardisconnected (): Clears the disconnected phone connection and updates the phone status, then notifies the registrant of the current state.

Internalcleardisconnected (): Clears disconnected Ringingcall, Foregroundcall and backgroundcall in connections, If there is no connection, the Gsmcall is set to the idle state.

Updatephonestate (): Updates the phone status and notifies the registrant of the start or end of the voice call.

Candial (): Check if you can make a call, Only the following conditions can be met: (1) RF Raido not shut down (2) Pendingmo This connection is empty (3) Ringcall This gsmcall is not in the ring State (4) system does not prohibit the telephone dialing function (5) Foregroundcall and Backgrou Ndcall none of the 2 Gsmcall are active. Where the incoming and waiting are expressed in the ring, when the dialing and alerting are dialed, when the active state is not idle, disconnected, and disconnecting, the ringing, dialing, Active and holding are active.

Canconference (): conference call is available when Foregroundcall is active, Backgroundcall is in holding, and they have fewer than 5 connections

Cantransfer (): Can be exchanged when Foregroundcall is active, Backgroundcall is holding.

Hangup (Gsmconnection conn): Hang up a phone connection

Hangupwaitingorbackground (): Hang up call waiting and back phone

Hangupforegroundresumebackground (): Hang up the front desk phone and resume the back office phone

Hangupconnectionbyindex (gsmcall call, int index): Hangs up the specified front-end calls for an index

Hangupallconnections (Gsmcall Call): Hang up all telephone channels

Get Call status

Android-provided interfaces: Telephonymanager and Phonestatelistener

Provides such things as data connection status (connected, not connected) and transmission direction (upload or download, etc.), phone status (free idle, off-hook offhook, ringing ringing). The Offhook status starts at the time period when the call is started (including not yet connected) or when the phone is started to be answered. The API does not differentiate between the start dialing to the other side of the reply.

This way, if only the API is used, it is not possible to obtain accurate dialing timings (from the moment the connection starts), and only the timing information from the moment the start of the call can be obtained.

However, in the underlying library, the phone state details these states, see the call class. It defines a more detailed state:

IDLE, ACTIVE, HOLDING, dialing, alerting, INCOMING, waiting, disconnected;

The call class is just an abstract class, and GSM and CDMA handsets have their own implementations. The implementation of GSM is Gsmcall, and its status update depends on the Drivercall class. In Drivercall, you can tell the status of a call by using Atresponseparser to parse the execution results of the AT command CLCC:

+clcc:1,0,2,0,0, "+18005551212″,145 index,ismt,state,mode,ismpty (, Number,toa)

See DRIVERCALL.STATEFROMCLCC function as follows:

Switch (state) {

Case 0:return state.active;

Case 1:return state.holding;

Case 2:return state.dialing;

Case 3:return state.alerting;

Case 4:return state.incoming;

Case 5:return state.waiting;

Default

throw new Atparseex ("illegal call State" + state);

}

This way, if you want to know the change in call status from the start of the call to the call, you can only use the underlying open API:

Phone phone = Phonefactory.getdefaultphone ();

Call.state state = Phone.getforegroundcall (). GetState ();

Therefore, programs that need to invoke the underlying API cannot be developed on the SDK. It must be compiled with the Android source code. The cost of using it is likely to cause such programs to be incompatible on different Android platforms, such as running incorrectly or not running at all.

Data connection Management

Data connection management is done by Gsmdataconnectiontracker.

A Boolean trysetupdata (String reason) attempts to establish a data connection. It just checks that the current phone status is a condition for establishing a data connection, and when the condition is met, the connection is established. These conditions include whether you are currently in a normal non-connected state, whether the phone is in an idle state, whether the SIM card is ready, whether the data connection is allowed, whether the data connection is limited, and where the power state is allowed. Then call Buildwaitingapns to build a list of available APN (user-Set preferred APN, if not set, the type of APN can also, see Buildwaitingapns instructions), if the list is not empty, If you are calling SetupData, establish a data connection.

Boolean SetupData (String reason)

SetupData uses the Connect function of Pdpconnection (final call to Ril Setupdatacall function), from the alternative APN list Waitingapns, select the first APN as the configuration to establish a data connection, See the Buildwaitingapns function.

APN the Use

Createallapnlist: Queries all APN of the system with the operator of the current SIM card and calls Createapnlist

Create the APN list and locate the user-set preferred APN

Createapnlist: Create an APN list with the data set from the query

Onapnchanged: When the APN is changed by the user, it is called to this function to reestablish the data connection

Buildwaitingapns: Use the user-set preferred APN to build an alternative APN list that can be used for data connections, which is the Waitingapns list (only one if there is a preferred APN). If the user does not have the preferred APN set, all types of APN that match are added to the Waitingapns list (such as the default type). When the Trysetupdata function checks for an APN that is available in the Waitingapns list, it tries to establish a connection.

GETNEXTAPN (): When pdpconnection needs to use APN to establish a data connection, it calls the function to obtain the first APN from the Waitingapns alternate list until the data connection is successfully established.

SETPREFERREDAPN: When the user does not have the preferred APN set, the APN with the current data connection success is set to preferred APN. See Ondatasetupcomplete.

GETPREFERREDAPN: User gets user-set preferred APN

Network Information

including IP addresses, gateways, and DNS information, you should use the Gsmphone

Getipaddress

Getgateway

Getdnsservers

Upper-level applications should use Gsmphone to work with these APIs

This article link address: http://www.redwolf-blog.com/?p=1100

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.