ServiceStateTracker與RIL對象的交互
兩種方式:
- ServiceStateTracker對象主動發起的
- ServiceStateTracker對象被動接收的
1.被動接收RIL上報
- ServiceStateTracker對象和Phone對象有相同的生命週期,在建立時會調用RIL對象的registerForXXX和setOnXXX方法,完成7種類型註冊:
//ServiceStateTracker
mCi.setOnSignalStrengthUpdate(this, EVENT_SIGNAL_STRENGTH_UPDATE, null);
mCi.registerForCellInfoList(this, EVENT_UNSOL_CELL_INFO_LIST, null);
//GsmServiceStateTracker mCi.registerForAvailable(this, EVENT_RADIO_AVAILABLE, null); mCi.registerForRadioStateChanged(this, EVENT_RADIO_STATE_CHANGED, null); mCi.registerForVoiceNetworkStateChanged(this, EVENT_NETWORK_STATE_CHANGED, null); mCi.setOnNITZTime(this, EVENT_NITZ_TIME, null); mCi.setOnRestrictedStateChanged(this, EVENT_RESTRICTED_STATE_CHANGED, null); mIccRecords.registerForRecordsLoaded(this, EVENT_SIM_RECORDS_LOADED, null); mUiccApplcation.registerForReady(this, EVENT_SIM_READY, null);
- 註冊網絡成功後RIL會發出EVENT_SIM_READY消息,ServiceStateTracker的handleMessage接收,接着調用 pollState方法:
case EVENT_SIM_READY:
// Set the network type, in case the radio does not restore it.
// mCi.setCurrentPreferredNetworkType();
boolean skipRestoringSelection = mPhone.getContext().getResources().getBoolean(
com.android.internal.R.bool.skip_restoring_network_selection);
if (!skipRestoringSelection) {
// restore the previous network selection.
mPhone.restoreSavedNetworkSelection(null);
}
pollState();
// Signal strength polling stops when radio is off
queueNextSignalStrengthPoll();
break;
- pollState方法首先判斷radio是否可用,然後會連續向RIL發出4個請求:
<pre name="code" class="java"> mPollingContext[0]++;
mCi.getOperator( obtainMessage( EVENT_POLL_STATE_OPERATOR, mPollingContext)); mPollingContext[0]++; mCi.getDataRegistrationState( obtainMessage( EVENT_POLL_STATE_GPRS, mPollingContext)); mPollingContext[0]++; mCi.getVoiceRegistrationState(
obtainMessage( EVENT_POLL_STATE_REGISTRATION, mPollingContext)); mPollingContext[0]++; mCi.getNetworkSelectionMode( obtainMessage( EVENT_POLL_STATE_NETWORK_SELECTION_MODE, mPollingContext));
- 會向MODEM發出對應的AT指令,handleMessage接受返回的結果,然後都會調用handlePollStateResult方法:
case EVENT_POLL_STATE_REGISTRATION:
case EVENT_POLL_STATE_GPRS:
case EVENT_POLL_STATE_OPERATOR:
case EVENT_POLL_STATE_NETWORK_SELECTION_MODE:
ar = (AsyncResult) msg.obj;
handlePollStateResult(msg.what, ar);
break;
- 在handlePollStateResult方法中會具體處理4種消息,主要是更新mNewSS(ServiceState)對象和mNewXXX對象的一些屬性,最終調用pollStateDone方法,跟新屬性,如果網絡基本信息或狀態改變了,發起對應的handler消息。
2.向服務對象主動發起服務狀態控制請求:
主要通過setRadioPower方法,打開關閉radio無線通信模塊。
@Override
protected void setPowerStateToDesired() {
if (mAlarmSwitch) {
if(DBG) log("mAlarmSwitch == true");
Context context = mPhone.getContext();
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
am.cancel(mRadioOffIntent);
mAlarmSwitch = false;
}
// If we want it on and it's off, turn it on
if (mDesiredPowerState
&& mCi.getRadioState() == CommandsInterface.RadioState.RADIO_OFF) {
mCi.setRadioPower(true, null);
} else if (!mDesiredPowerState && mCi.getRadioState().isOn()) {
// If it's on and available and we want it off gracefully
if (mPowerOffDelayNeed) {
if (mImsRegistrationOnOff && !mAlarmSwitch) {
if(DBG) log("mImsRegistrationOnOff == true");
Context context = mPhone.getContext();
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(ACTION_RADIO_OFF);
mRadioOffIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
mAlarmSwitch = true;
if (DBG) log("Alarm setting");
am.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime() + 3000, mRadioOffIntent);
} else {
DcTrackerBase dcTracker = mPhone.mDcTracker;
powerOffRadioSafely(dcTracker);
}
} else {
DcTrackerBase dcTracker = mPhone.mDcTracker;
powerOffRadioSafely(dcTracker);
}
} else if (mDeviceShuttingDown && mCi.getRadioState().isAvailable()) {
mCi.requestShutdown(null);
}
}