Telephony框架分析
引子
無論手機如何改變,功能如何豐富,它最核心最關鍵的功能依舊是通訊,通訊按現(xiàn)在的情況來說主要就是打電話發(fā)短信和上網,Android的通訊框架從上往下可以分為4個部分:
- Modem 這是整個通訊的硬件基礎,需要Modem芯片,不同制式需要采用不同的Modem;
- RIL 為了適配不同的Modem芯片而抽象出來的中間層,用于將Modem指令轉換為Java可用的數據流;
- Telephony 這是在Framework層搭建的通訊框架,面向開發(fā)者提供操作通訊事務的能力;
- Application 這是最上層的應用,直接面向用戶,提供撥號、上網、發(fā)短信的界面;
這篇文章主要對Telephony框架做一個小小的概述與總結
Telephony進程與實體
進程
整個Framework層的Telephony框架運行在一個叫做Phone(com.android.phone)的進程中。而這個進程是在packages\services\Telephony模塊中被創(chuàng)建的(Android 8.0.0平臺)。并且該模塊在AndroidManifest.xml中有如下關鍵的聲明:
<application android:name="PhoneApp"android:persistent="true"android:label="@string/phoneAppLabel"android:icon="@mipmap/ic_launcher_phone"android:allowBackup="false"android:supportsRtl="true"android:usesCleartextTraffic="true"android:defaultToDeviceProtectedStorage="true"android:directBootAware="true"//從 Android N 開始,在首次開機時,在用戶尚未來得及解鎖設備之前,設備可直接啟動到一種名為 Direct Boot(直接啟動)的新模式中。在此模式下,操作系統(tǒng)可以全功能運行,但不允許訪問私有應用數據,只能運行經過更新、可支持直接啟動功能的應用。//該屬性使得用戶在加密狀態(tài)(未解鎖)下能夠正常使用一些手機功能,如鬧鐘,接電話等>這個聲明創(chuàng)建了一個名叫“PhoneApp”的application,并且確定了他的name、label、icon等信息,而且將該application的persistent屬性置為true。那么這個persistent屬性的作用是什么呢?這里的persistent屬性具備兩個關鍵作用:
+ 該模塊會在開機時被系統(tǒng)自動初始化;
+ 該模塊所在的進程(com.android.phone)由于任何原因被kill掉之后,都會自動重啟(這種情況只針對系統(tǒng)內置app,第三方安裝的app不會被重啟);
以上兩點是十分必要的,他保證/導致了兩個效果:
- 所有Application層和Framework層中與Telephony相關的操作,包括各種Service的創(chuàng)建、與RIL層交互的RILJ的初始化等,都是通過Phone進程創(chuàng)建的;
- Phone進程由于任何原因被kill掉后,都會發(fā)生重新搜網的動作;
實體對象
前面介紹了Telephony的框架和進程,那么當發(fā)生具體的某個通訊請求,比如打電話、發(fā)短信時,該如何操作呢?
這里要引入一個非常重要的對象:Phone對象。該對象可以看做是Telephony框架的實體,可以向該對象發(fā)起各種通訊相關的請求,可以說,Phone對象是Telephony整個框架的核心,他負責與RIL層的交互。
而這個Phone對象就是在PhoneApp這個application初始化過程中被創(chuàng)建的。我們就從入口開始,來查看Phone進程的創(chuàng)建過程:
在PhoneApp的onCreate()方法中,會new出PhoneGlobals的對象,并接著調用該對象的onCreate方法:
public void onCreate() {if (UserHandle.myUserId() == 0) {// We are running as the primary user, so should bring up the// global phone state.mPhoneGlobals = new PhoneGlobals(this);mPhoneGlobals.onCreate();mTelephonyGlobals = new TelephonyGlobals(this);mTelephonyGlobals.onCreate();}}先看PhoneGlobals的onCreate過程:
public void onCreate() {if (VDBG) Log.v(LOG_TAG, "onCreate()...");ContentResolver resolver = getContentResolver();// Cache the "voice capable" flag.// This flag currently comes from a resource (which is// overrideable on a per-product basis):sVoiceCapable =getResources().getBoolean(com.android.internal.R.bool.config_voice_capable);// ...but this might eventually become a PackageManager "system// feature" instead, in which case we'd do something like:// sVoiceCapable =// getPackageManager().hasSystemFeature(PackageManager.FEATURE_TELEPHONY_VOICE_CALLS);if (mCM == null) {// Initialize the telephony framework// 創(chuàng)建Phone對象PhoneFactory.makeDefaultPhones(this);// Start TelephonyDebugService After the default phone is created.Intent intent = new Intent(this, TelephonyDebugService.class);startService(intent);// 初始化CallManagermCM = CallManager.getInstance();for (Phone phone : PhoneFactory.getPhones()) {mCM.registerPhone(phone);}// Create the NotificationMgr singleton, which is used to display// status bar icons and control other status bar behavior.// 初始化NotificationMgr,用于狀態(tài)欄通知notificationMgr = NotificationMgr.init(this);// If PhoneGlobals has crashed and is being restarted, then restart.mHandler.sendEmptyMessage(EVENT_RESTART_SIP);// Create an instance of CdmaPhoneCallState and initialize it to IDLEcdmaPhoneCallState = new CdmaPhoneCallState();cdmaPhoneCallState.CdmaPhoneCallStateInit();// before registering for phone state changesmPowerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);mWakeLock = mPowerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, LOG_TAG);// lock used to keep the processor awake, when we don't care for the display.mPartialWakeLock = mPowerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK| PowerManager.ON_AFTER_RELEASE, LOG_TAG);mKeyguardManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);// Get UpdateLock to suppress system-update related events (e.g. dialog show-up)// during phone calls.mUpdateLock = new UpdateLock("phone");if (DBG) Log.d(LOG_TAG, "onCreate: mUpdateLock: " + mUpdateLock);CallLogger callLogger = new CallLogger(this, new CallLogAsync());callGatewayManager = CallGatewayManager.getInstance();// Create the CallController singleton, which is the interface// to the telephony layer for user-initiated telephony functionality// (like making outgoing calls.)// 初始化CallControllercallController = CallController.init(this, callLogger, callGatewayManager);// Create the CallerInfoCache singleton, which remembers custom ring tone and// send-to-voicemail settings.//// The asynchronous caching will start just after this call.callerInfoCache = CallerInfoCache.init(this);// 初始化PhoneInterfaceManagerphoneMgr = PhoneInterfaceManager.init(this, PhoneFactory.getDefaultPhone());configLoader = CarrierConfigLoader.init(this);// Create the CallNotifer singleton, which handles// asynchronous events from the telephony layer (like// launching the incoming-call UI when an incoming call comes// in.)// 初始化CallNotifer, 響鈴等動作在這里面完成notifier = CallNotifier.init(this);PhoneUtils.registerIccStatus(mHandler, EVENT_SIM_NETWORK_LOCKED);// register for MMI/USSDmCM.registerForMmiComplete(mHandler, MMI_COMPLETE, null);// register connection tracking to PhoneUtilsPhoneUtils.initializeConnectionHandler(mCM);// Register for misc other intent broadcasts.IntentFilter intentFilter =new IntentFilter(Intent.ACTION_AIRPLANE_MODE_CHANGED);intentFilter.addAction(TelephonyIntents.ACTION_ANY_DATA_CONNECTION_STATE_CHANGED);intentFilter.addAction(TelephonyIntents.ACTION_SIM_STATE_CHANGED);intentFilter.addAction(TelephonyIntents.ACTION_RADIO_TECHNOLOGY_CHANGED);intentFilter.addAction(TelephonyIntents.ACTION_SERVICE_STATE_CHANGED);intentFilter.addAction(TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED);registerReceiver(mReceiver, intentFilter);mCarrierVvmPackageInstalledReceiver.register(this);//set the default values for the preferences in the phone.PreferenceManager.setDefaultValues(this, R.xml.network_setting_fragment, false);PreferenceManager.setDefaultValues(this, R.xml.call_feature_setting, false);// Make sure the audio mode (along with some// audio-mode-related state of our own) is initialized// correctly, given the current state of the phone.PhoneUtils.setAudioMode(mCM);}// XXX pre-load the SimProvider so that it's readyresolver.getType(Uri.parse("content://icc/adn"));// TODO: Register for Cdma Information Records// phone.registerCdmaInformationRecord(mHandler, EVENT_UNSOL_CDMA_INFO_RECORD, null);// Read HAC settings and configure audio hardwareif (getResources().getBoolean(R.bool.hac_enabled)) {int hac = android.provider.Settings.System.getInt(getContentResolver(),android.provider.Settings.System.HEARING_AID,0);AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);audioManager.setParameter(SettingsConstants.HAC_KEY,hac == SettingsConstants.HAC_ENABLED? SettingsConstants.HAC_VAL_ON : SettingsConstants.HAC_VAL_OFF);}}從以上代碼可以看出,PhoneGlobals的初始化過程中要先通過PhoneFactory的makeDefaultPhones()方法創(chuàng)建Phone對象,接著完成了一系列與Telephony相關的重要服務的初始化,比如CallManager、NotificationMgr、CallCommandService、PhoneInterfaceManager、CallNotifier等。
我們現(xiàn)在只關心Phone對象的創(chuàng)建過程,也就是PhoneFactory的makeDefaultPhones過程:
public static void makeDefaultPhones(Context context) {makeDefaultPhone(context);}/*** FIXME replace this with some other way of making these* instances*/public static void makeDefaultPhone(Context context) {synchronized (sLockProxyPhones) {if (!sMadeDefaults) {sContext = context;// create the telephony device controller.TelephonyDevController.create();int retryCount = 0;for(;;) {boolean hasException = false;retryCount ++;try {// use UNIX domain socket to// prevent subsequent initializationnew LocalServerSocket("com.android.internal.telephony");} catch (java.io.IOException ex) {hasException = true;}if ( !hasException ) {break;} else if (retryCount > SOCKET_OPEN_MAX_RETRY) {throw new RuntimeException("PhoneFactory probably already running");} else {try {Thread.sleep(SOCKET_OPEN_RETRY_MILLIS);} catch (InterruptedException er) {}}}// 創(chuàng)建DefaultPhoneNotifier,負責通知Phone的狀態(tài)sPhoneNotifier = new DefaultPhoneNotifier();int cdmaSubscription = CdmaSubscriptionSourceManager.getDefault(context);Rlog.i(LOG_TAG, "Cdma Subscription set to " + cdmaSubscription);/* In case of multi SIM mode two instances of Phone, RIL are created,where as in single SIM mode only instance. isMultiSimEnabled() function checkswhether it is single SIM or multi SIM mode */int numPhones = TelephonyManager.getDefault().getPhoneCount();// Start ImsResolver and bind to ImsServices.String defaultImsPackage = sContext.getResources().getString(com.android.internal.R.string.config_ims_package);Rlog.i(LOG_TAG, "ImsResolver: defaultImsPackage: " + defaultImsPackage);sImsResolver = new ImsResolver(sContext, defaultImsPackage, numPhones);sImsResolver.populateCacheAndStartBind();// 獲取當前網絡類型int[] networkModes = new int[numPhones];sPhones = new Phone[numPhones];// 根據網絡類型創(chuàng)建RILJ,負責Framework與RIL層的交互sCommandsInterfaces = new RIL[numPhones];sTelephonyNetworkFactories = new TelephonyNetworkFactory[numPhones];for (int i = 0; i < numPhones; i++) {// reads the system properties and makes commandsinterface// Get preferred network type.networkModes[i] = RILConstants.PREFERRED_NETWORK_MODE;Rlog.i(LOG_TAG, "Network Mode set to " + Integer.toString(networkModes[i]));sCommandsInterfaces[i] = new RIL(context, networkModes[i],cdmaSubscription, i);}Rlog.i(LOG_TAG, "Creating SubscriptionController");SubscriptionController.init(context, sCommandsInterfaces);// Instantiate UiccController so that all other classes can just// call getInstance()// 創(chuàng)建UiccController,間接創(chuàng)建UiccCard、UiccCardApplication、IccFileHandler、IccRecords、CatService服務// (手機中使用的卡SIM,USIM,UIM等統(tǒng)稱為:UICC)sUiccController = UiccController.make(context, sCommandsInterfaces);// 根據當前Phone類型創(chuàng)建不同的PhoneProxyfor (int i = 0; i < numPhones; i++) {Phone phone = null;int phoneType = TelephonyManager.getPhoneType(networkModes[i]);if (phoneType == PhoneConstants.PHONE_TYPE_GSM) {phone = new GsmCdmaPhone(context,sCommandsInterfaces[i], sPhoneNotifier, i,PhoneConstants.PHONE_TYPE_GSM,TelephonyComponentFactory.getInstance());} else if (phoneType == PhoneConstants.PHONE_TYPE_CDMA) {phone = new GsmCdmaPhone(context,sCommandsInterfaces[i], sPhoneNotifier, i,PhoneConstants.PHONE_TYPE_CDMA_LTE,TelephonyComponentFactory.getInstance());}Rlog.i(LOG_TAG, "Creating Phone with type = " + phoneType + " sub = " + i);sPhones[i] = phone;}// Set the default phone in base class.// FIXME: This is a first best guess at what the defaults will be. It// FIXME: needs to be done in a more controlled manner in the future.sPhone = sPhones[0];sCommandsInterface = sCommandsInterfaces[0];// Ensure that we have a default SMS app. Requesting the app with// updateIfNeeded set to true is enough to configure a default SMS app.ComponentName componentName =SmsApplication.getDefaultSmsApplication(context, true /* updateIfNeeded */);String packageName = "NONE";if (componentName != null) {packageName = componentName.getPackageName();}Rlog.i(LOG_TAG, "defaultSmsApplication: " + packageName);// Set up monitor to watch for changes to SMS packagesSmsApplication.initSmsPackageMonitor(context);sMadeDefaults = true;Rlog.i(LOG_TAG, "Creating SubInfoRecordUpdater ");sSubInfoRecordUpdater = new SubscriptionInfoUpdater(context,sPhones, sCommandsInterfaces);SubscriptionController.getInstance().updatePhonesAvailability(sPhones);// Start monitoring after defaults have been made.// Default phone must be ready before ImsPhone is created because ImsService might// need it when it is being opened. This should initialize multiple ImsPhones for// ImsResolver implementations of ImsService.for (int i = 0; i < numPhones; i++) {sPhones[i].startMonitoringImsService();}ITelephonyRegistry tr = ITelephonyRegistry.Stub.asInterface(ServiceManager.getService("telephony.registry"));SubscriptionController sc = SubscriptionController.getInstance();sSubscriptionMonitor = new SubscriptionMonitor(tr, sContext, sc, numPhones);sPhoneSwitcher = new PhoneSwitcher(MAX_ACTIVE_PHONES, numPhones,sContext, sc, Looper.myLooper(), tr, sCommandsInterfaces,sPhones);sProxyController = ProxyController.getInstance(context, sPhones,sUiccController, sCommandsInterfaces, sPhoneSwitcher);sNotificationChannelController = new NotificationChannelController(context);sTelephonyNetworkFactories = new TelephonyNetworkFactory[numPhones];for (int i = 0; i < numPhones; i++) {sTelephonyNetworkFactories[i] = new TelephonyNetworkFactory(sPhoneSwitcher, sc, sSubscriptionMonitor, Looper.myLooper(),sContext, i, sPhones[i].mDcTracker);}}}}經過上面兩段代碼,我們看到了Phone對象的創(chuàng)建過程:
+ 先創(chuàng)建DefaultPhoneNotifier對象,該對象的作用是監(jiān)聽RIL層發(fā)過來的Phone狀態(tài)變化;
+ 針對當前的網絡類型創(chuàng)建不同的Java層RIL,即RILJ;
+ 拿到RILJ之后,就要利用DefaultPhoneNotifier和RILJ并根據當前的Phone類型(GSM/CDMA)來創(chuàng)建不同GsmCdmaPhone對象;
Telephony之GsmCdmaCallTracker
Application如果要發(fā)起通話相關的動作,可以通過Telephony的實體對象,也就是Phone對象來發(fā)起請求,而Phone對象就會通話相關的請求通過GsmCallTracker轉發(fā)給RILJ,然后傳遞給Modem。所以,GsmCallTracker是Phone對象和RILJ之間通話相關事務的接力者。
GsmCdmaCallTracker的作用及創(chuàng)建過程
首先看GsmCdmaCallTracker提供的功能
synchronized Connection dial (){} void acceptCall () throws CallStateException {} void rejectCall () throws CallStateException {} void switchWaitingOrHoldingAndActive() throws CallStateException {} void clearDisconnected() {} boolean canDial() {} private void updatePhoneState() {} void hangup (GsmCdmaConnection conn) throws CallStateException {} void hangup (GsmCdmaCall call) throws CallStateException {}上述方法表明GsmCdmaCallTracker的作用包括兩方面:
+ 對通話線路進行操作,包括接聽、掛斷、切換、設置靜音等;
+ 對當前的通話狀態(tài)進行通知(IDEL、RINGING、OFFHOOK);
下面看初始化過程:
創(chuàng)建過程在GsmCdmaPhone中完成
GsmCdmaCallTracker構造方法
public GsmCdmaCallTracker (GsmCdmaPhone phone) {this.mPhone = phone;// 拿到RILJmCi = phone.mCi;// 監(jiān)聽通話、Radio狀態(tài)mCi.registerForCallStateChanged(this, EVENT_CALL_STATE_CHANGE, null);mCi.registerForOn(this, EVENT_RADIO_AVAILABLE, null);mCi.registerForNotAvailable(this, EVENT_RADIO_NOT_AVAILABLE, null);// Register receiver for ECM exitIntentFilter filter = new IntentFilter();filter.addAction(TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED);mPhone.getContext().registerReceiver(mEcmExitReceiver, filter);updatePhoneType(true);}在構造函數中GsmCdmaCallTracker拿到RILJ對象,當需要對當前通話連接操作時,就會直接調用RILJ去實現(xiàn),同時在構造方法中又注冊了通話狀態(tài)和Radio的狀態(tài)監(jiān)聽器,用于向其他對象通知當前Radio狀態(tài)的改變。
GsmCdmaCallTracker對通話動作的處理
通話動作包含接聽、掛斷、切換、靜音等,這些事件在APP層被請求后,最終都會發(fā)送給當前的Phone對象,也就是PhoneProxy,然后再轉交給當前的mActivePhone,也就是某個GSMCdmaPhone,此時GSMCdmaPhone對象就會把請求轉交給GsmCdmaCallTracker來處理
撥號:
//GSM/*** clirMode is one of the CLIR_ constants*/public synchronized Connection dial(String dialString, int clirMode, UUSInfo uusInfo,Bundle intentExtras)throws CallStateException {// note that this triggers call state changed notif// 清楚鏈接clearDisconnected();// 條件判斷if (!canDial()) {throw new CallStateException("cannot dial in current state");}String origNumber = dialString;dialString = convertNumberIfNecessary(mPhone, dialString);// The new call must be assigned to the foreground call.// That call must be idle, so place anything that's// there on hold// 是否需要切換通話if (mForegroundCall.getState() == GsmCdmaCall.State.ACTIVE) {// this will probably be done by the radio anyway// but the dial might fail before this happens// and we need to make sure the foreground call is clear// for the newly dialed connectionswitchWaitingOrHoldingAndActive();// This is a hack to delay DIAL so that it is sent out to RIL only after// EVENT_SWITCH_RESULT is received. We've seen failures when adding a new call to// multi-way conference calls due to DIAL being sent out before SWITCH is processedtry {Thread.sleep(500);} catch (InterruptedException e) {// do nothing}// Fake local state so that// a) foregroundCall is empty for the newly dialed connection// b) hasNonHangupStateChanged remains false in the// next poll, so that we don't clear a failed dialing callfakeHoldForegroundBeforeDial();}if (mForegroundCall.getState() != GsmCdmaCall.State.IDLE) {//we should have failed in !canDial() above before we get herethrow new CallStateException("cannot dial in current state");}boolean isEmergencyCall = PhoneNumberUtils.isLocalEmergencyNumber(mPhone.getContext(),dialString);// 準備新的通話連接mPendingMO = new GsmCdmaConnection(mPhone, checkForTestEmergencyNumber(dialString),this, mForegroundCall, isEmergencyCall);mHangupPendingMO = false;mMetrics.writeRilDial(mPhone.getPhoneId(), mPendingMO, clirMode, uusInfo);if ( mPendingMO.getAddress() == null || mPendingMO.getAddress().length() == 0|| mPendingMO.getAddress().indexOf(PhoneNumberUtils.WILD) >= 0) {// Phone number is invalidmPendingMO.mCause = DisconnectCause.INVALID_NUMBER;// handlePollCalls() will notice this call not present// and will mark it as dropped.pollCallsWhenSafe();} else {// Always unmute when initiating a new call// 設置非靜音模式setMute(false);// 向RIL層發(fā)送撥號請求mCi.dial(mPendingMO.getAddress(), clirMode, uusInfo, obtainCompleteMessage());}if (mNumberConverted) {mPendingMO.setConverted(origNumber);mNumberConverted = false;}// 更新通話狀態(tài)updatePhoneState();mPhone.notifyPreciseCallStateChanged();return mPendingMO;}接聽動作:
public void acceptCall() throws CallStateException {// FIXME if SWITCH fails, should retry with ANSWER// in case the active/holding call disappeared and this// is no longer call waitingif (mRingingCall.getState() == GsmCdmaCall.State.INCOMING) {Rlog.i("phone", "acceptCall: incoming...");// Always unmute when answering a new callsetMute(false);// 向RIL層發(fā)送接聽的請求mCi.acceptCall(obtainCompleteMessage());} else if (mRingingCall.getState() == GsmCdmaCall.State.WAITING) {if (isPhoneTypeGsm()) {setMute(false);} else {GsmCdmaConnection cwConn = (GsmCdmaConnection)(mRingingCall.getLatestConnection());// Since there is no network response for supplimentary// service for CDMA, we assume call waiting is answered.// ringing Call state change to idle is in GsmCdmaCall.detach// triggered by updateParent.cwConn.updateParent(mRingingCall, mForegroundCall);cwConn.onConnectedInOrOut();updatePhoneState();}// 切換通話switchWaitingOrHoldingAndActive();} else {throw new CallStateException("phone not ringing");}}拒接動作:
public void rejectCall() throws CallStateException {// AT+CHLD=0 means "release held or UDUB"// so if the phone isn't ringing, this could hang up heldif (mRingingCall.getState().isRinging()) {// 拒接mCi.rejectCall(obtainCompleteMessage());} else {throw new CallStateException("phone not ringing");}}以上三個動作最后都要調用mCi對象來處理,這個對象就是RILJ,他會把請求發(fā)送到RIL層來處理
GsmCdmaCallTracker對通話狀態(tài)的處理
在GsmCdmaCallTracker中完成了通話相關動作之后,就立刻更新當前的狀態(tài)并發(fā)送給Radio狀態(tài)監(jiān)聽者。
例如,接聽電話時,當發(fā)送了mCi.dial()的請求之后,就立刻調用updatePhoneState()進行狀態(tài)更新:
private void updatePhoneState() {PhoneConstants.State oldState = mState;// 獲取當前狀態(tài)if (mRingingCall.isRinging()) {mState = PhoneConstants.State.RINGING;} else if (mPendingMO != null ||!(mForegroundCall.isIdle() && mBackgroundCall.isIdle())) {mState = PhoneConstants.State.OFFHOOK;} else {Phone imsPhone = mPhone.getImsPhone();if ( mState == PhoneConstants.State.OFFHOOK && (imsPhone != null)){imsPhone.callEndCleanupHandOverCallIfAny();}mState = PhoneConstants.State.IDLE;}if (mState == PhoneConstants.State.IDLE && oldState != mState) {mVoiceCallEndedRegistrants.notifyRegistrants(new AsyncResult(null, null, null));} else if (oldState == PhoneConstants.State.IDLE && oldState != mState) {mVoiceCallStartedRegistrants.notifyRegistrants (new AsyncResult(null, null, null));}if (Phone.DEBUG_PHONE) {log("update phone state, old=" + oldState + " new="+ mState);}if (mState != oldState) {// 通知GsmCdmaPhone進行狀態(tài)廣播mPhone.notifyPhoneStateChanged();mMetrics.writePhoneState(mPhone.getPhoneId(), mState);}}在這個過程中,最后要通過GsmCdmaPhone的notifyPhoneStateChanged()方法來通知其他對象
這樣一來,DefaultPhoneNotifier就將RILJ與TelephonyRegistry聯(lián)系起來了,當RILJ接收到RIL上報的Phone狀態(tài)時,就會通過DefaultPhoneNotifier發(fā)送給TelephonyRegistry。
GsmCdmaCallTracker的更新機制
手機通話功能可以支持多路通話。比如最基本的情況是,在和A通話過程中(線路A),有新的來電時(線路B),如果選擇接聽B,那么A線路將處于“呼叫保持”狀態(tài),此時如果B線路被掛斷,那么A線路重新被激活。
而GsmCdmaCallTracker的更新機制核心任務就是維護這些不同線路,包括對各個線路的操作(比如接聽、掛斷、保持),以及各個線路狀態(tài)的維護。為了達到這個目的,GsmCallTracker內部創(chuàng)建了兩個非常重要的對象:GsmCdmaConnection和GsmCdmaCall。
GsmCdmaConnection
為了管理不同的線路,Android定義了GsmCdmaConnection類,簡單來說,就是一條通話線路,就是一個GsmCdmaConnection類型的對象。
在GsmCdmaCallTracker的成員變量中,創(chuàng)建了GsmCdmaConnection類型的數組變量來維護所有的線路
public static final int MAX_CONNECTIONS_GSM = 19; //7 allowed in GSM + 12 from IMS for SRVCCprivate GsmCdmaConnection mConnections[];mConnections = new GsmCdmaConnection[MAX_CONNECTIONS_GSM];GsmCdmaCall
這個對象和GsmCdmaConnection的作用類似,每一個通話線路都可以是一個GsmCall對象,但實際上并不是一個GsmCdmaConnection對應一個GsmCdmaCall。
一個通話線路狀態(tài)分為以下9種
| IDEL | 沒有通話 |
| ACTIVE | 被激活狀態(tài) |
| HOLDING | 被保持狀態(tài) |
| DIALING | 正在呼出狀態(tài) |
| ALERTING | 正在呼出已經處于響鈴的狀態(tài) |
| INCOMING | 正在來電狀態(tài) |
| WAITING | 已經通話中,又有新的來電 |
| DISCONNECTED | 被掛斷 |
| DISCONNECTING | 正在掛斷 |
在GsmCdmaCallTracker中,又將不同的線路狀態(tài)分為3種
- ForegroundCall
- BackgroundCall
- RingingCall
然后創(chuàng)建三個GsmCdmaCall對象
+ mForegroundCall
+ mBackgroundCall
+ mRingingCall
對應關系如下:
| mForegroundCall | ACTIVE、DIALING、ALERTING |
| mBackgroundCall | HOLDING |
| mRingingCall | INCOMING、WAITING |
這樣做的好處是,GsmCdmaCall不再面對具體的線路,而是面對當前Phone的狀態(tài),被激活的線路就是mForegroundCall,被保持的線路就是mBackgroundCall,而正處于響鈴狀態(tài)的線路就是mRingingCall,從這里我們可以想到,他和GsmConnection的區(qū)別在于,一個GsmCdmaCall可能包含多個GsmCdmaConnection對象(比如同時有兩通電話處于被保持狀態(tài))。
而GsmCdmaCall要做的主要功能就是維護不同GsmCall的狀態(tài)。
GsmCdmaCallTracker的更新機制
GsmCdmaCallTracker運行機制的核心就是要及時更新GsmCdmaConnection和GsmCall的狀態(tài),因此弄明白這兩個對象的更新機制就會明白GsmCdmaCallTracker的更新機制。
現(xiàn)在回到GsmCdmaCallTracker的構造方法中,剛才我們看到,GsmCdmaCallTracker在構造方法的最后注冊了對通話和Radio狀態(tài)的監(jiān)聽器,下面我們從這些監(jiān)聽器入手分析GsmCdmaCallTracker的運行機制。
第一個監(jiān)聽器
mCi.registerForCallStateChanged(this, EVENT_CALL_STATE_CHANGE, null);這個監(jiān)聽器監(jiān)聽的是RIL層通話的狀態(tài),當有新的狀態(tài)到來時(比如新的來電),就會通過EVENT_CALL_STATE_CHANGE消息通知到GsmCdmaCallTracker,然后就會在handleMessage中進行處理:
public void handleMessage (Message msg) {AsyncResult ar;switch (msg.what) {case EVENT_REPOLL_AFTER_DELAY:case EVENT_CALL_STATE_CHANGE://得到RIL層消息,通話狀態(tài)有變pollCallsWhenSafe();break;} }然后就會調用pollCallsWhenSafe()方法去獲取當前最新的通話狀態(tài)。
然后看看第二個監(jiān)聽器
mCi.registerForOn(this, EVENT_RADIO_AVAILABLE, null);這個監(jiān)聽器是用來監(jiān)聽Radio的可用狀態(tài),當Radio的狀態(tài)上來后處理
public void handleMessage (Message msg) {AsyncResult ar;switch (msg.what) {case EVENT_RADIO_AVAILABLE:handleRadioAvailable();break;} }然后進入handleRadioAvailable()中處理:
protected void handleRadioAvailable() {pollCallsWhenSafe(); }這里就與第一個監(jiān)聽器一樣了,然后看看第三個監(jiān)聽器:
mCi.registerForNotAvailable(this, EVENT_RADIO_NOT_AVAILABLE, null);這個監(jiān)聽器用來監(jiān)聽Radio的不可用狀態(tài),當監(jiān)聽的消息上來后,在handleMessage中處理:
public void handleMessage (Message msg) {AsyncResult ar;switch (msg.what) {case EVENT_RADIO_NOT_AVAILABLE:handleRadioNotAvailable();break;} }然后會進入handleRadioNotAvailable()的流程:
private void handleRadioNotAvailable() {pollCallsWhenSafe(); }接下來又是pollCallsWhenSafe()的操作,到這里我們發(fā)現(xiàn),在GsmCdmaCallTracker構造函數中注冊的三個監(jiān)聽器,無論哪一個被觸發(fā)都會進入pollCallsWhenSafe的流程,接下來的分析我們將會看到,GsmCdmaCallTracker將會主動請求最新的通話狀態(tài),然后根據當前狀態(tài)去更新GsmCdmaConnection和GsmCdmaCall對象。
然后看看pollCallsWhenSafe()的流程:
protected void pollCallsWhenSafe() {mNeedsPoll = true;if (checkNoOperationsPending()) {mLastRelevantPoll = obtainMessage(EVENT_POLL_CALLS_RESULT);// 通過RILJ獲取當前的最新通話狀態(tài)mCi.getCurrentCalls(mLastRelevantPoll);}}這里看到,在pollCallsWhenSafe中通過RILJ(也就是mCi)去向Modem查詢當前的通話狀態(tài),并注冊了回調的消息EVENT_POLL_CALLS_RESULT,當拿到Modem返回值后,就會再次通過handleMessage()來處理最新的通話狀態(tài):
public void handleMessage (Message msg) {AsyncResult ar;switch (msg.what) {case EVENT_POLL_CALLS_RESULT:// 拿到最新通話狀態(tài)ar = (AsyncResult)msg.obj;if (msg == mLastRelevantPoll) {mNeedsPoll = false;mLastRelevantPoll = null;handlePollCalls((AsyncResult)msg.obj);}break;} }然后將數據拿到后,交由handlePollCalls()來處理,在這個方法里,就需要將當前的Modem通話狀態(tài)數據進行解析,更新GsmCdmaConnection和GsmCdmaCall對象:
protected synchronized void handlePollCalls(AsyncResult ar) {List polledCalls;Connection newRinging = null; //or waitingboolean hasNonHangupStateChanged = false; // Any change besidesboolean hasAnyCallDisconnected = false;boolean needsPollDelay = false;boolean unknownConnectionAppeared = false;for (int i = 0, curDC = 0, dcSize = polledCalls.size() ; i < mConnections.length; i++) {//拿到當前GsmCallTracker中的通話線路GsmConnection conn = mConnections[i];DriverCall dc = null;if (curDC < dcSize) {//拿到當前的Modem中的通話線路狀態(tài)dc = (DriverCall) polledCalls.get(curDC);if (dc.index == i+1) {curDC++;} else {dc = null;}}if (conn == null && dc != null) {if (mPendingMO != null && mPendingMO.compareTo(dc)) {//mConnections中沒有當前線路,而且當前線路是匹配mPendingMO的,說明是最新發(fā)起的呼出線路mConnections[i] = mPendingMO;mPendingMO.mIndex = i;mPendingMO.update(dc);mPendingMO = null;if (mHangupPendingMO) {//是否在呼出之后用戶立刻掛斷了線路mHangupPendingMO = false;try {//掛斷這通線路hangup(mConnections[i]);} catch (CallStateException ex) {}return;}} else {//Modem中有該線路,而GsmConnection中沒有該線路,說明有新的通話來臨,需要創(chuàng)建新的線路連接mConnections[i] = new GsmConnection(mPhone.getContext(), dc, this, i);if (mConnections[i].getCall() == mRingingCall) {//新來電newRinging = mConnections[i];} else {//異常通話線路if (dc.state != DriverCall.State.ALERTING && dc.state != DriverCall.State.DIALING) {mConnections[i].onConnectedInOrOut();if (dc.state == DriverCall.State.HOLDING) {mConnections[i].onStartedHolding();}}unknownConnectionAppeared = true;}}hasNonHangupStateChanged = true;} else if (conn != null && dc == null) {//Modem中已經沒有當前的鏈接,說明該線路已經被掛斷,需要從mConnections中刪除(置為null)mDroppedDuringPoll.add(conn);mConnections[i] = null;} else if (conn != null && dc != null && !conn.compareTo(dc)) {//Modem中的鏈接信息與當前的不匹配,可能發(fā)生了掉話或者新的通話mDroppedDuringPoll.add(conn);//需要創(chuàng)建新的鏈接mConnections[i] = new GsmConnection (mPhone.getContext(), dc, this, i);if (mConnections[i].getCall() == mRingingCall) {newRinging = mConnections[i];}hasNonHangupStateChanged = true;} else if (conn != null && dc != null) {//當前線路與Modem匹配,更新當前的鏈路信息boolean changed;changed = conn.update(dc);hasNonHangupStateChanged = hasNonHangupStateChanged || changed;}}//異常if (mPendingMO != null) {mDroppedDuringPoll.add(mPendingMO);mPendingMO = null;mHangupPendingMO = false;}if (newRinging != null) {//新的來電,需要通知registerForNewRingingConnection的監(jiān)聽者mPhone.notifyNewRingingConnection(newRinging);}//對于掛斷的鏈接,需要標明掛斷的原因for (int i = mDroppedDuringPoll.size() - 1; i >= 0 ; i--) {GsmConnection conn = mDroppedDuringPoll.get(i);if (conn.isIncoming() && conn.getConnectTime() == 0) {// Missed or rejected callConnection.DisconnectCause cause;if (conn.mCause == Connection.DisconnectCause.LOCAL) {//被拒掉cause = Connection.DisconnectCause.INCOMING_REJECTED;} else {//未接來電cause = Connection.DisconnectCause.INCOMING_MISSED;}mDroppedDuringPoll.remove(i);hasAnyCallDisconnected |= conn.onDisconnect(cause);} else if (conn.mCause == Connection.DisconnectCause.LOCAL|| conn.mCause == Connection.DisconnectCause.INVALID_NUMBER) {mDroppedDuringPoll.remove(i);hasAnyCallDisconnected |= conn.onDisconnect(conn.mCause);}}if (mDroppedDuringPoll.size() > 0) {mCi.getLastCallFailCause( obtainNoPollCompleteMessage(EVENT_GET_LAST_CALL_FAIL_CAUSE));}if (needsPollDelay) {pollCallsAfterDelay();}if (newRinging != null || hasNonHangupStateChanged || hasAnyCallDisconnected) {internalClearDisconnected();}//更新通話狀態(tài)updatePhoneState();if (unknownConnectionAppeared) {mPhone.notifyUnknownConnection();}if (hasNonHangupStateChanged || newRinging != null || hasAnyCallDisconnected) {mPhone.notifyPreciseCallStateChanged();}}上面的更新過程中,用從Modem獲取到的通話線路信息與mConnections中存儲的信息做對比,從而更新mConnections中線路的狀態(tài),比如:
- Modem中存在,而mConnections中不存在,則說明是新來電,或者新的去電,需要在mConnections中創(chuàng)建新的GsmConnection對象;
- Modem中不存在,而mConnections中存在,說明該線路已經被掛斷,需要從mConnections中刪除該線路的GsmConnection對象;
- Modem中和mConnections都存在,但是信息不匹配,則說明該線路的狀態(tài)有改變,需要在mConnections中更新信息;
更新線路之后,對于最新掛斷的線路,還需要更新掛斷的原因,比如是被對方拒接還是未接的來電,然后在更新的最后,通知所有監(jiān)聽者,Radio狀態(tài)已經改變。我們簡單看一下通知的過程:
private void updatePhoneState() {PhoneConstants.State oldState = mState;if (mRingingCall.isRinging()) {// 響鈴狀態(tài)mState = PhoneConstants.State.RINGING;} else if (mPendingMO != null ||!(mForegroundCall.isIdle() && mBackgroundCall.isIdle())) {// 通話狀態(tài)mState = PhoneConstants.State.OFFHOOK;} else {Phone imsPhone = mPhone.getImsPhone();if ( mState == PhoneConstants.State.OFFHOOK && (imsPhone != null)){imsPhone.callEndCleanupHandOverCallIfAny();}// 待機狀態(tài)mState = PhoneConstants.State.IDLE;}if (mState == PhoneConstants.State.IDLE && oldState != mState) {mVoiceCallEndedRegistrants.notifyRegistrants(new AsyncResult(null, null, null));} else if (oldState == PhoneConstants.State.IDLE && oldState != mState) {mVoiceCallStartedRegistrants.notifyRegistrants (new AsyncResult(null, null, null));}if (Phone.DEBUG_PHONE) {log("update phone state, old=" + oldState + " new="+ mState);}if (mState != oldState) {// 狀態(tài)有更新,通過Phone對象發(fā)送給監(jiān)聽者mPhone.notifyPhoneStateChanged();mMetrics.writePhoneState(mPhone.getPhoneId(), mState);}}其實通知的過程就是通過mPhone的notifyPhoneStateChanged()方法來實現(xiàn),這里的mPhone,也就是GsmCdmaPhone對象,會把該廣播發(fā)送給監(jiān)聽者們。
Telephony之TelephonyRegistry
TelephonyRegistry概述
TelephonyRegistry的作用是檢測當前Radio的狀態(tài),包括通話、短信、數據連接等狀態(tài),當這些狀態(tài)發(fā)生改變時,通知所有向他注冊過的客戶端。也就是說,他負責Radio狀態(tài)的通知。
注冊過程:
telephonyRegistry = new TelephonyRegistry(context); ServiceManager.addService("telephony.registry", telephonyRegistry);TelephonyRegistry通知機制
TelephonyRegistry負責狀態(tài)的通知,需要完成兩個工作:
- 從Radio拿到通知
- 將通知發(fā)送給相應的監(jiān)聽者
從Radio拿到通知消息
TelephonyRegistry的通知,是從他的客戶端得到的,這個客戶端就是DefaultPhoneNotifier。
創(chuàng)建GsmCdmaPhone時需要傳遞兩個重要參數,其中一個是RILJ對象,另一個就是DefaultPhoneNotifier對象,這里的DefaultPhoneNotifier就是TelephonyRegistry的Client。
DefaultPhoneNotifier的構造函數:
public DefaultPhoneNotifier() {mRegistry = ITelephonyRegistry.Stub.asInterface(ServiceManager.getService("telephony.registry"));}在這里,我們看到DefaultPhoneNotifier從ServiceManager中獲取了TelephonyRegistry的服務,也就是說,DefaultPhoneNotifier是TelephonyRegistry的Client。
這樣一來,DefaultPhoneNotifier就將RILJ與TelephonyRegistry聯(lián)系起來了,當RILJ接收到RIL上報的Phone狀態(tài)時,就會通過DefaultPhoneNotifier發(fā)送給TelephonyRegistry。
例如對于通話狀態(tài)的改變,GsmCdmaCallTracker會通過如下方式將狀態(tài)發(fā)送給GsmCdmaPhone
private void updatePhoneState() {PhoneConstants.State oldState = mState;if (mRingingCall.isRinging()) {// 響鈴狀態(tài)mState = PhoneConstants.State.RINGING;} else if (mPendingMO != null ||!(mForegroundCall.isIdle() && mBackgroundCall.isIdle())) {// 通話狀態(tài)mState = PhoneConstants.State.OFFHOOK;} else {Phone imsPhone = mPhone.getImsPhone();if ( mState == PhoneConstants.State.OFFHOOK && (imsPhone != null)){imsPhone.callEndCleanupHandOverCallIfAny();}// 待機狀態(tài)mState = PhoneConstants.State.IDLE;}if (mState == PhoneConstants.State.IDLE && oldState != mState) {mVoiceCallEndedRegistrants.notifyRegistrants(new AsyncResult(null, null, null));} else if (oldState == PhoneConstants.State.IDLE && oldState != mState) {mVoiceCallStartedRegistrants.notifyRegistrants (new AsyncResult(null, null, null));}if (Phone.DEBUG_PHONE) {log("update phone state, old=" + oldState + " new="+ mState);}if (mState != oldState) {// 狀態(tài)有更新,通過Phone對象發(fā)送給監(jiān)聽者mPhone.notifyPhoneStateChanged();mMetrics.writePhoneState(mPhone.getPhoneId(), mState);}}mPhone就是GsmCdmaPhone,接下來,它會把通知轉交給DefaultPhoneNotifier來處理:
void notifyPhoneStateChanged() {mNotifier.notifyPhoneState(this); }這里的mNotifier就是創(chuàng)建GsmCdmaPhone對象時傳遞的DefaultPhoneNotifier對象,這樣的話,就將通知發(fā)送到了DefaultPhoneNotifier內部:
public void notifyPhoneState(Phone sender) {Call ringingCall = sender.getRingingCall();int subId = sender.getSubId();int phoneId = sender.getPhoneId();String incomingNumber = "";if (ringingCall != null && ringingCall.getEarliestConnection() != null) {incomingNumber = ringingCall.getEarliestConnection().getAddress();}try {if (mRegistry != null) {// 將狀態(tài)發(fā)送給TelephonyRegistrymRegistry.notifyCallStateForPhoneId(phoneId, subId,PhoneConstantConversions.convertCallState(sender.getState()), incomingNumber);}} catch (RemoteException ex) {// system process is dead}}從這里我們看到,DefaultPhoneNotifier將當前GsmCdmaCallTracker中的狀態(tài)(sender.getState())通過convertCallState()轉換后,傳遞給TelephonyRegistry,這個轉換的作用就是,將GsmCdmaCallTracker中的IDLE、RINGING、OFFHOOK狀態(tài)轉換為TelephonyManager中的對應狀態(tài):
public static final int CALL_STATE_IDLE = 0; public static final int CALL_STATE_RINGING = 1; public static final int CALL_STATE_OFFHOOK = 2;就這樣,DefaultPhoneNotifier作為TelephonyRegistry的Client,將當前通話狀態(tài)傳遞給了TelephonyRegistry。
將消息發(fā)送給其他客戶端
TelephonyRegistry拿到相應的通知后,是如何將消息發(fā)送給其他客戶端呢?我們繼續(xù)用通話狀態(tài)來跟蹤這一流程。
在TelephonyRegistry拿到消息后,需要向兩個渠道分發(fā)消息,一個是通過系統(tǒng)廣播,另一個是向自己注冊的監(jiān)聽者。我們主要來看向監(jiān)聽者發(fā)送消息的流程。
監(jiān)聽者注冊監(jiān)聽
首先我們來看如何成為TelephonyRegistry的監(jiān)聽者。
由于TelephonyRegistry提供關于Radio的多種狀態(tài)監(jiān)測,包括通話、信號、呼叫轉移、數據連接等狀態(tài),所以在向其申請監(jiān)聽時,需要說明監(jiān)聽那種狀態(tài),可以通過調用listen()方法來實現(xiàn),我們來看這個接口:
public void listen(String pkgForDebug, IPhoneStateListener callback, int events, boolean notifyNow) {//獲取調用者的UID,監(jiān)測權限int callerUid = UserHandle.getCallingUserId();int myUid = UserHandle.myUserId();if (events != 0) {//權限監(jiān)測checkListenerPermission(events);synchronized (mRecords) {Record r = null;find_and_add: {//獲取當前監(jiān)聽者信息IBinder b = callback.asBinder();final int N = mRecords.size();for (int i = 0; i < N; i++) {r = mRecords.get(i);if (b == r.binder) {break find_and_add;}}r = new Record();r.binder = b;r.callback = callback;r.pkgForDebug = pkgForDebug;r.callerUid = callerUid;//添加當前的監(jiān)聽者信息mRecords.add(r);}int send = events & (events ^ r.events);r.events = events;//需要立刻通知if (notifyNow) {if ((events & PhoneStateListener.LISTEN_SERVICE_STATE) != 0) {//監(jiān)聽通話狀態(tài)try {r.callback.onServiceStateChanged(new ServiceState(mServiceState));} catch (RemoteException ex) {remove(r.binder);}}if ((events & PhoneStateListener.LISTEN_SIGNAL_STRENGTH) != 0) {//監(jiān)聽信號改變try {int gsmSignalStrength = mSignalStrength.getGsmSignalStrength();r.callback.onSignalStrengthChanged((gsmSignalStrength == 99 ? -1 : gsmSignalStrength));} catch (RemoteException ex) {remove(r.binder);}}if ((events & PhoneStateListener.LISTEN_MESSAGE_WAITING_INDICATOR) != 0) {try {r.callback.onMessageWaitingIndicatorChanged(mMessageWaiting);} catch (RemoteException ex) {remove(r.binder);}}if ((events & PhoneStateListener.LISTEN_CALL_FORWARDING_INDICATOR) != 0) {//監(jiān)聽呼叫轉移狀態(tài)try {r.callback.onCallForwardingIndicatorChanged(mCallForwarding);} catch (RemoteException ex) {remove(r.binder);}}}}} else {remove(callback.asBinder());} }上面的listen()操作,依次做了:
- 通過checkListenerPermission()監(jiān)測調用者是否有監(jiān)聽權限;
- 將調用者的相關信息保存在mRecords中,保存信息包含IBinder、回調方法、UID、監(jiān)聽事件等;
- 如果listen的同時要求立刻通知,則會立刻調用監(jiān)聽者的回調方法;
經過這些操作,客戶端就完成了對TelephonyRegistry的監(jiān)聽注冊,等待接收通知。
如何將消息通知到監(jiān)聽者
DefaultPhoneNotifier作為TelephonyRegistry的Client,通過notifyCallState()將當前通話狀態(tài)通知到TelephonyRegistry,我們來看這個方法:
public void notifyCallState(int state, String incomingNumber) {// 權限檢查if (!checkNotifyPermission("notifyCallState()")) {return;}if (VDBG) {log("notifyCallState: state=" + state + " incomingNumber=" + incomingNumber);}synchronized (mRecords) {for (Record r : mRecords) {if (r.matchPhoneStateListenerEvent(PhoneStateListener.LISTEN_CALL_STATE) &&(r.subId == SubscriptionManager.DEFAULT_SUBSCRIPTION_ID)) {try {String incomingNumberOrEmpty = r.canReadPhoneState ? incomingNumber : "";// 通知所有監(jiān)聽者r.callback.onCallStateChanged(state, incomingNumberOrEmpty);} catch (RemoteException ex) {mRemoveList.add(r.binder);}}}// 發(fā)送廣播通知handleRemoveListLocked();}// Called only by Telecomm to communicate call state across different phone accounts. So// there is no need to add a valid subId or slotId.broadcastCallStateChanged(state, incomingNumber,SubscriptionManager.INVALID_PHONE_INDEX,SubscriptionManager.INVALID_SUBSCRIPTION_ID);}在上面這個方法中,先對通知者進行權限檢查,然后在mRecords中查找曾經注冊了該事件的監(jiān)聽者,并調用他們的回調方法。最后,又將該消息發(fā)送到系統(tǒng)廣播中。
至此,通話狀態(tài)改變的消息就從GsmCallTracker通過DefaultPhoneNotifier傳遞給了TelephonyRegistry,并從此擴散。類似的,對于數據連接狀態(tài)來說,將會由DcTracker通過DefaultPhoneNotifier傳遞給TelephonyRegistry,然后進行擴散。
小結
從以上的分析中我們看到,TelephonyRegistry作為一個Service,成為他的Client后可以擁有兩種功能:
+ Client可以將當前Radio的狀態(tài)發(fā)送給TelephonyRegistry,比如GsmCdmaCallTracker;
+ Client可以向TelephonyRegistry申請監(jiān)聽Radio的相關狀態(tài),比如TelephonyManager;
Telephony之PhoneInterfaceManager
概述
PhoneInterfaceManager是一個Service,在被創(chuàng)建時通過ServiceManager注冊自己,他作為Telephony對外的接口,可以接受其他進程向Telephony的請求,我們通過該Service所繼承的AIDL文件就能看到他所提供的具體功能:
interface ITelephony {//發(fā)起通話void dial(String number);void call(String callingPackage, String number);boolean showCallScreen();boolean showCallScreenWithDialpad(boolean showDialpad);//掛斷通話boolean endCall();//接聽通話void answerRingingCall();void silenceRinger();//通話狀態(tài)判斷boolean isOffhook();boolean isRinging();boolean isIdle();boolean isRadioOn();boolean isSimPinEnabled();void cancelMissedCallsNotification();//Pin/Puk碼查詢boolean supplyPin(String pin);int getIccPin1RetryCount();boolean supplyPuk(String puk, String pin);int[] supplyPinReportResult(String pin);int[] supplyPukReportResult(String puk, String pin);boolean handlePinMmi(String dialString);void toggleRadioOnOff();boolean setRadio(boolean turnOn);boolean setRadioPower(boolean turnOn);void updateServiceLocation();void enableLocationUpdates();void disableLocationUpdates();//數據連接業(yè)務int enableApnType(String type);int disableApnType(String type);boolean enableDataConnectivity();boolean disableDataConnectivity();boolean isDataConnectivityPossible();Bundle getCellLocation();List<NeighboringCellInfo> getNeighboringCellInfo(String callingPkg);//通話狀態(tài)獲取int getCallState();int getDataActivity();int getDataState();int getActivePhoneType();int getCdmaEriIconIndex();int getCdmaEriIconMode();String getCdmaEriText();boolean needsOtaServiceProvisioning();int getVoiceMessageCount();//網絡、數據類型int getNetworkType();int getDataNetworkType();int getVoiceNetworkType();boolean hasIccCard();int getLteOnCdmaMode();List<CellInfo> getAllCellInfo();void setCellInfoListRate(int rateInMillis);String transmitIccLogicalChannel(int cla, int command, int channel, int p1, int p2, int p3, String data);String transmitIccBasicChannel(int cla, int command, int p1, int p2, int p3, String data);int openIccLogicalChannel(String AID);boolean closeIccLogicalChannel(int channel);int getLastError();byte[] transmitIccSimIO(int fileID, int command, int p1, int p2, int p3, String filePath);byte[] getATR();//通話中合并、切換、靜音、Dtmf處理void toggleHold();void merge();void swap();void mute(boolean mute);void playDtmfTone(char digit, boolean timedShortCode);void stopDtmfTone();//添加、刪除監(jiān)聽器void addListener(ITelephonyListener listener);void removeListener(ITelephonyListener listener); }從他所提供的接口來看,其提供了Telephony比較全面的功能,包括:通話、Pin/Puk、Radio狀態(tài)、數據連接業(yè)務等功能的查詢或控制。
PhoneInterfaceManager的創(chuàng)建過程
PhoneInterfaceManager是在PhoneGlobals的onCreate()創(chuàng)建的:
phoneMgr = PhoneInterfaceManager.init(this, PhoneFactory.getDefaultPhone());具體創(chuàng)建過程:
static PhoneInterfaceManager init(PhoneGlobals app, Phone phone) {synchronized (PhoneInterfaceManager.class) {if (sInstance == null) {// 創(chuàng)建對象sInstance = new PhoneInterfaceManager(app, phone);} else {Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);}return sInstance;}}然后看構造方法:
private PhoneInterfaceManager(PhoneGlobals app, Phone phone) {mApp = app;mPhone = phone;mCM = PhoneGlobals.getInstance().mCM;mUserManager = (UserManager) app.getSystemService(Context.USER_SERVICE);mAppOps = (AppOpsManager)app.getSystemService(Context.APP_OPS_SERVICE);mMainThreadHandler = new MainThreadHandler();mTelephonySharedPreferences =PreferenceManager.getDefaultSharedPreferences(mPhone.getContext());mSubscriptionController = SubscriptionController.getInstance();publish();}在構造方法中除了將PhoneGlobals中初始化的Phone、CallManager等信息傳遞給PhoneInterfaceManager外,還將PhoneInterfaceManager通過publish()注冊給ServiceManager:
private void publish() {if (DBG) log("publish: " + this);// 注冊給ServiceManager, 名字是"Phone"ServiceManager.addService("phone", this);}這里看到,PhoneInterfaceManager將自己注冊為SystemServer,其他模塊可以通過ServiceManager來獲取他的服務。
PhoneInterfaceManager對客戶端請求的處理
客戶端通過ServiceManager獲取到PhoneInterfaceManager的代理對象后,就可以對其發(fā)起各種請求,我們挑選幾個比較重要的事務來簡要分析。
撥號
通過PhoneInterfaceManager撥號時,進入以下流程:
public void dial(String number) {dialForSubscriber(getPreferredVoiceSubscription(), number);}public void dialForSubscriber(int subId, String number) {if (DBG) log("dial: " + number);// No permission check needed here: This is just a wrapper around the// ACTION_DIAL intent, which is available to any app since it puts up// the UI before it does anything.String url = createTelUrl(number);if (url == null) {return;}// PENDING: should we just silently fail if phone is offhook or ringing?PhoneConstants.State state = mCM.getState(subId);if (state != PhoneConstants.State.OFFHOOK && state != PhoneConstants.State.RINGING) {// 發(fā)送intent實現(xiàn)撥號Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);mApp.startActivity(intent);}}PhoneInterfaceManager通過intent的形式發(fā)起撥號任務。
掛斷電話
掛斷電話相關:
public boolean endCall() {return endCallForSubscriber(getDefaultSubscription());}/*** End a call based on the call state of the subId* @return true is a call was ended*/public boolean endCallForSubscriber(int subId) {// 權限檢查enforceCallPermission();// 發(fā)送CMD_END_CALL的Messagereturn (Boolean) sendRequest(CMD_END_CALL, null, new Integer(subId));}private final class MainThreadHandler extends Handler {...@Overridepublic void handleMessage(Message msg) {...switch (msg.what) {case CMD_END_CALL:request = (MainThreadRequest) msg.obj;int end_subId = request.subId;final boolean hungUp;Phone phone = getPhone(end_subId);if (phone == null) {if (DBG) log("CMD_END_CALL: no phone for id: " + end_subId);break;}int phoneType = phone.getPhoneType();if (phoneType == PhoneConstants.PHONE_TYPE_CDMA) {// CDMA: If the user presses the Power button we treat it as// ending the complete call session// 通過PhoneUtils掛斷電話hungUp = PhoneUtils.hangupRingingAndActive(getPhone(end_subId));} else if (phoneType == PhoneConstants.PHONE_TYPE_GSM) {// GSM: End the call as per the Phone state// 通過PhoneUtils掛斷電話hungUp = PhoneUtils.hangup(mCM);} else {throw new IllegalStateException("Unexpected phone type: " + phoneType);}if (DBG) log("CMD_END_CALL: " + (hungUp ? "hung up!" : "no call to hang up"));request.result = hungUp;// Wake up the requesting threadsynchronized (request) {request.notifyAll();}break;...}...}...}在這里,PhoneInterfaceManager又將請求傳給了PhoneUtils來處理結束通話的操作。
查詢Pin碼
下面看Pin碼相關的操作:
public int[] supplyPinReportResultForSubscriber(int subId, String pin) {// 權限檢查enforceModifyPermission();// 通過IccCard來操作final UnlockSim checkSimPin = new UnlockSim(getPhone(subId).getIccCard());// 啟用查詢線程checkSimPin.start();return checkSimPin.unlockSim(null, pin);}數據連接業(yè)務
禁用數據連接:
@Overridepublic boolean disableDataConnectivity() {enforceModifyPermission();int subId = mSubscriptionController.getDefaultDataSubId();final Phone phone = getPhone(subId);if (phone != null) {phone.setDataEnabled(false);return true;} else {return false;}}通過ConnectivityManager禁用數據連接業(yè)務。
小結
經過以上幾個請求的解析,我們發(fā)現(xiàn),PhoneInterfaceManager自身并沒有完成請求的功能,而是把客戶端的請求分配給相應領域的管理者,也可以這樣理解,PhoneInterfaceManager作為Telephony框架對外的“接口人”,接收客戶的請求后,將請求發(fā)送給真正的“主人”去實現(xiàn)。
Telephony之TelephonyManager
TelephonyManager概述
TelephonyManager主要提供Telephony相關實務的處理能力,我們從他所提供的public方法來總覽一下其所能提供的功能:
//得到軟件版本 getDeviceSoftwareVersion() //得到設備的ID,IMEI或者MEID getDeviceId() //得到位置信息,主要是當前注冊小區(qū)的位置碼 getCellLocation() //得到附近小區(qū)信息 getNeighboringCellInfo() //得到當前Phone的類型,GSM/CDMA getCurrentPhoneType() //得到/proc/cmdline文件當前的內容 getProcCmdLine() //得到運營商名字 getNetworkOperatorName() //得到MCC+MNC getNetworkOperator() //得到是否漫游的狀態(tài) isNetworkRoaming() //得到網絡狀態(tài),NETWORK_TYPE_GPRS、NETWORK_TYPE_EDGE、NETWORK_TYPE_CDMA等等 getNetworkType() //得到SIM卡狀態(tài) getSimState() //得到SIM卡MCC+MNC getSimOperator() //得到SIM卡SPN getSimOperatorName() //得到SIM卡串號 getSimSerialNumber() //得到MSISDN getMsisdn() //得到語音信箱號碼 getVoiceMailNumber() //得到語音信箱短信條數 getVoiceMessageCount() //得到語音信箱名稱 getVoiceMailAlphaTag() //得到數據連接狀態(tài):DATA_DISCONNECTED、DATA_CONNECTING、DATA_CONNECTED、DATA_SUSPENDED等 getDataState() //注冊監(jiān)聽器監(jiān)聽Phone狀態(tài) listen() //得到所有Phone的信息 getAllCellInfo()從這些方法來看,TelephonyManager提供了與PhoneInterfaceManager類似的功能,但是又有本質的區(qū)別,其共同點是:都向其他模塊提供了全面的操作Telephony相關事務的能力,其他模塊可以在獲取到這兩個服務后,對Telephony進行各種操作。而區(qū)別在于:
+ 從本質上來講,TelephonyManager本質不是一個Service,沒有繼承任何類,而PhoneInterfaceManager的本質是一個Service
+ 從注冊方式上來講,TelephonyManager是在ContextImpl中通過registerService的形式進行注冊,而PhoneInterfaceManager是通過ServiceManager進行注冊
+ 從獲取方式上來講,需要TelephonyManager服務時,可以通過Context對象的getSystemService()方法來實現(xiàn),而PhoneInterfaceManager服務需要通過ServiceManager的getService()方法來實現(xiàn)
獲取TelephonyManager服務
如果調用者是系統(tǒng)應用,可以直接創(chuàng)建TelephonyManager的對象,只需要傳遞Context類型的參數就行:
boolean isPhone() {if (!mIsPhoneInitialized) {//創(chuàng)建TelephonyManager對象,并調用isVoiceCapable()方法mIsPhone = new TelephonyManager(getContext()).isVoiceCapable();mIsPhoneInitialized = true;}return mIsPhone; }或者通過TelephonyManager的getDefault()方法來獲取TelephonyManager對象:
private static TelephonyManager sInstance = new TelephonyManager();/** @hide/* @deprecated - use getSystemService as described above */public static TelephonyManager getDefault() {return sInstance;}如果調用者不是系統(tǒng)應用的話,如何獲取他的服務呢?
這里就要介紹TelephonyManager的注冊過程了。
ContextImpl在初始化時注冊了一些常用的Service,其中就包括TelephonyManager:
registerService(TELEPHONY_SERVICE, new ServiceFetcher() {public Object createService(ContextImpl ctx) {return new TelephonyManager(ctx.getOuterContext());}});經過這樣的注冊,其他進程就可以通過Context對象的getSystemService()方法來獲取其服務
TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);或者直接調用TelephonyManager的from()方法獲取:
public static TelephonyManager from(Context context) {return (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); }TelephonyManager的內部機制
TelephonyManager如何實現(xiàn)客戶端對他的請求。
TelephonyManager并沒有繼承任何的父類,那么他是如何實現(xiàn)各項功能的呢?
在TelephonyManager內部,獲取到了三個Service的客戶端,其中構造函數中獲取了TelephonyRegistry的服務:
public TelephonyManager(Context context, int subId) {mSubId = subId;Context appContext = context.getApplicationContext();if (appContext != null) {mContext = appContext;} else {mContext = context;}mSubscriptionManager = SubscriptionManager.from(mContext);if (sRegistry == null) {獲取TelephonyRegistry服務sRegistry = ITelephonyRegistry.Stub.asInterface(ServiceManager.getService("telephony.registry"));}}通過getSubscriberInfo()獲取了PhoneSubInfoProxy服務:
private IPhoneSubInfo getSubscriberInfo() {// get it each time because that process crashes a lotreturn IPhoneSubInfo.Stub.asInterface(ServiceManager.getService("iphonesubinfo"));}通過getITelephony()獲取PhoneInterfaceManager服務:
private ITelephony getITelephony() {return ITelephony.Stub.asInterface(ServiceManager.getService(Context.TELEPHONY_SERVICE));}TelephonyManager拿到這三個Service之后,就用三個Service提供的服務,以及SystemProperties提供的屬性,搭建了自己的public方法集合。
也就是說,TelephonyManager自己并不具備處理事務的能力,而是匯集了其他三個Service的功能,向他的所有請求者提供便利的處理Telephony事務的能力。
TelephonyManager小結
為什么需要構建這么一個東西來同時注冊3個SystemService?
假如現(xiàn)在有3個模塊A、B、C,都需要做一些Phone有關的操作,他們的需求如下:
- A 需要用到TelephonyRegistry和PhoneSubInfoProxy的服務,那么他就要去分別申請這兩個服務的代理對象
- B 需要用到TelephonyRegistry和PhoneInterfaceManager服務,他也需要分別申請代理對象。
- C 需要用到上面的3個服務,那么就需要申請3個代理對象
對于這樣的情況,我們當然可以在每個需要的模塊內部分別調用系統(tǒng)接口(ServiceManager.getService)去得到相應的代理對象。這種情況下我們需要調用7次getService方法得到7個SystemService的遠程對象。
如果通過TelephonyRegistry的方式去實現(xiàn),只需要在3個模塊中,分別調用Context的getSystemService方法就能同時得到3個SystemService遠程代理對象。而且我們得到的3個TelephonyManager對象是同一個對象,3個模塊公用了同一個SystemService。因此,我們實際上只調用了3此getService方法,得到了3個SystemService遠程對象。
TelephonyManager整合3個SystemService的意義就在于減輕系統(tǒng)負擔,特別是一些SystemService的負擔,提高了效率。
既然TelephonyManager大大減輕了一些SystemService的負擔,為什么只整合了3個SystemService呢?或者說,為什么選中了這3個SystemService來整合
我們再來梳理以下TelephonyManager的運行原理。經過TelephonyManager的整合,當我們通過Context去得到TelephonyManager對象時,得到的是同一個TelephonyManager對象,那么我們進一步得到的SystemService也是同一個,此時我們調用TelephonyManager中的方法時,得到的返回值也是完全相同的。
這就說明了,TelephonyManager整合的SystemService,有一個共同特點:這些服務無論誰去調用,方法的返回值都是相同的。比如SIM卡的狀態(tài)、當前的運營商信息、設備的ID號等。
而對于存在差異的SystemService,由于對于不同的客戶端需要返回不同的值,當然就無法放到TelephonyManager中處理了。
總結
關于Telephony的大體內容就先記到這里,實際上的內容不止這些,只能在后續(xù)的使用中多多積累了。
總結
以上是生活随笔為你收集整理的Telephony框架分析的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java方法详解(基础)
- 下一篇: 操作系统 文件换行符问题