RegistrantList mechanism implements registerForXXX,
GsmCdmaCallTracker constructor to register events with RIL
/** GsmCdmaCallTracker constructor, which is strongly associated with GsmCdmaPhone RIL. Register handle Message Processing registerForXXX mCi. registerForCallStateChanged ==>> BaseCommands. registerForCallStateChanged (RIL parent class) */public GsmCdmaCallTracker (GsmCdmaPhone phone) {this. mPhone = phone; mCi = phone. mCi; // The RIL object mCi in phone. registerForCallStateChanged (this, EVENT_CALL_STATE_CHANGE, null); mCi. registerForOn (this, EVENT_RADIO_AVAILABLE, null); mCi. registerForNotAvailable (this, EVENT_RADIO_NOT_AVAILABLE, null); // Register recorder ER for ECM exit IntentFilter filter = new IntentFilter (); filter. addAction (TelephonyIntents. ACTION_EMERGENCY_CALLBACK_MODE_CHANGED); mPhone. getContext (). registerReceiver (mEcmExitReceiver, filter); updatePhoneType (true );}
RIL's parent class BaseCommands implements registerForXXX
class RIL extends BaseCommands implements CommandsInterface
@ Overridepublic void registerForCallStateChanged (Handler h, int what, Object obj) {// create the observer Registrant and build the handler Registrant r = new Registrant (h, what, obj ); // The notifier. Set RegistrantList mCallStateRegistrants = new RegistrantList (); // add mCallStateRegistrants. add (r );}
Registrant observer
publicRegistrant(Handler h, int what, Object obj){ refH = new WeakReference(h); this.what = what; userObj = obj;}
RegistrantList: the set of registrants.
public class RegistrantList{ ArrayList registrants = new ArrayList(); // of Registrant public synchronized void add(Registrant r) { removeCleared(); registrants.add(r); }}
This completes the registration of an event listener.
Event Notification
RIL. processUnsolicited
private voidprocessUnsolicited (Parcel p, int type) { case RIL_UNSOL_RESPONSE_CALL_STATE_CHANGED: if (RILJ_LOGD) unsljLog(response); mCallStateRegistrants .notifyRegistrants(new AsyncResult(null, null, null)); break;
The notifier RegistrantList is used to notify the observer Registrant of registration-related events.
MCallStateRegistrants. policyregistrants
RegistrantList
Public/* synchronized */voidpolicyregistrants (AsyncResult ar) // initiate a notification {internalpolicyregistrants (ar. result, ar. exception);} private synchronized voidinternalpolicyregistrants (Object result, Throwable exception) {for (int I = 0, s = registrants. size (); I <s; I ++) {Registrant r = (Registrant) registrants. get (I); // notify the observer Registrant r. internalpolicyregistrant (result, exception );}}
Registrant
/* Package */voidinternalpolicyregistrant (Object result, Throwable exception) {Handler h = getHandler (); if (h = null) {clear ();} else {Message msg = Message. obtain (); // mCi. registerForCallStateChanged (this, EVENT_CALL_STATE_CHANGE, null); // register the created Registrant r = new Registrant (h, what, obj); msg. what = what; // EVENT_CALL_STATE_CHANGE msg. obj = new AsyncResult (userObj, result, exception); // Handler is GsmCdmaCallTracker, which is composed of GsmCdmaCallTracker. handleMessage for processing h. sendMessage (msg );}}
Registrant sends a Message notification, which is handled by Handler, that is, GsmCdmaCallTracker
@Overridepublic void handleMessage(Message msg) { case EVENT_CALL_STATE_CHANGE: pollCallsWhenSafe(); break;
// Implemented in the GsmCdmaCallTracker parent class to query the current Call list protected void pollCallsWhenSafe () {mNeedsPoll = true; if (checkNoOperationsPending () {response = obtainMessage (messages); mCi. getcurrentcils (mLastRelevantPoll); // get the current list from RIL, handlepollcils, and update }}
If RIL processing is omitted, a callback is triggered when RIL processing is complete.
Continue to process handleMessage of GsmCdmaCallTracker
Case EVENT_POLL_CALLS_RESULT: Rlog. d (LOG_TAG, "Event EVENT_POLL_CALLS_RESULT Received"); if (msg = success) {if (DBG_POLL) log ("handle EVENT_POLL_CALL_RESULT: set needsPoll = F"); mNeedsPoll = false; mLastRelevantPoll = null; // in RIL. in processSolicited, construct an AsyncResult from the data returned by the underlying layer and add it to msg. obj handlepollcballs (AsyncResult) msg. obj); // when will the msg obj be initialized? When the msg is created, the obj is not initialized, only what} break;