UiccController是整个UICC事务处理的入口,负责对外提供IccRecords、IccFileHandler、UiccCardApplication等对象,并完成整个UICC系统的初始化工作。
public UiccCard getUiccCard() {} public UiccCardApplication getUiccCardApplication(int family) {} public IccRecords getIccRecords(int family) {} public IccFileHandler getIccFileHandler(int family) {} public void registerForIccChanged(Handler h, int what, Object obj) {}
从上面提供的方法可以知道,UiccController的主要作用体现在两个方面:
1、创建并向外提供UiccCard、UiccCardApplication、IccRecords、IccFileHandler等对象2、提供对SIM状态的监听
我们先来看一下UiccController是在哪里被创建的。
我们知道,Phone对象是在Phone模块的初始化中通过PhoneFactory创建的:@PhoneGlobals.java public void onCreate() { if (phone == null) { //创建Phone对象 PhoneFactory.makeDefaultPhones(this); //得到Phone对象 phone = PhoneFactory.getDefaultPhone(); } }然后看makeDefaultPhones()的过程:
@PhoneFactory.java public static void makeDefaultPhones(Context context) { makeDefaultPhone(context); } public static void makeDefaultPhone(Context context) { //获取RILJ sCommandsInterface = new RIL(context, networkMode, cdmaSubscription); //创建UiccController UiccController.make(context, sCommandsInterface); //得到phone的Type int phoneType = TelephonyManager.getPhoneType(networkMode); //创建PhoneProxy对象 if (phoneType == PhoneConstants.PHONE_TYPE_GSM) { sProxyPhone = new PhoneProxy(new GSMPhone(context, sCommandsInterface, sPhoneNotifier)); } else if (phoneType == PhoneConstants.PHONE_TYPE_CDMA) { switch (TelephonyManager.getLteOnCdmaModeStatic()) { case PhoneConstants.LTE_ON_CDMA_TRUE: sProxyPhone = new PhoneProxy(new CDMALTEPhone(context, sCommandsInterface, sPhoneNotifier)); break; case PhoneConstants.LTE_ON_CDMA_FALSE: default: sProxyPhone = new PhoneProxy(new CDMAPhone(context, sCommandsInterface, sPhoneNotifier)); break; } } }这里看到,在new完RIL对象之后,就通过UiccController的make()方法对UiccController进行创建,而且传递进去的两个参数,其中一个是Context类型对象,另一个是CommandsInterface类型的RIL对象。
@UiccController.java public static UiccController make(Context c, CommandsInterface ci) { synchronized (mLock) { if (mInstance != null) { throw new RuntimeException("UiccController.make() should only be called once"); } mInstance = new UiccController(c, ci); return mInstance; } }这里需要注意,UiccController的make()方法只能调用一次,也就是说,在整个全局中, 只存在一个UiccController对象。
private UiccController(Context c, CommandsInterface ci) { mContext = c; mCi = ci; mCi.registerForIccStatusChanged(this, EVENT_ICC_STATUS_CHANGED, null); mCi.registerForOn(this, EVENT_ICC_STATUS_CHANGED, null); }
我们看到,在UiccController的构造函数中,只完成了一件事情,就是注册了两个监听器:registerForIccStatusChanged和registerForOn。这两个监听器监听的是SIM卡和Radio的状态,他们是UiccController更新的触发器。
刚才讲到,UiccController的初始化过程主要向RILJ注册了两个监听器,而这两个监听器就是UiccController更新机制的触发点,当监听的事件发生时,UiccController就会启动整个UICC框架的更新机制。
下面来看具体的更新过程。
对于registerForIccStatusChanged这个注册的动作,并没有直接调用到RIL类里面,而是调用到了RIL的父类BaseCommands中:
@BaseCommands.java public void registerForIccStatusChanged(Handler h, int what, Object obj) { Registrant r = new Registrant (h, what, obj); mIccStatusChangedRegistrants.add(r); }我们看到,这里的注册过程其实就是在mIccStatusChangedRegistrants对象List中添加当前的注册对象,然后当以下两种事件发生时就会遍历该List中的监听者进行通知:
private RILRequest processSolicited (Parcel p) { switch (rr.mRequest) { case RIL_REQUEST_ENTER_SIM_PUK: case RIL_REQUEST_ENTER_SIM_PUK2: if (mIccStatusChangedRegistrants != null) { mIccStatusChangedRegistrants.notifyRegistrants(); } break; } }2、当SIM卡状态发生改变时:
private void processUnsolicited (Parcel p) { switch(response) { case RIL_UNSOL_RESPONSE_SIM_STATUS_CHANGED: if (mIccStatusChangedRegistrants != null) { mIccStatusChangedRegistrants.notifyRegistrants(); } break; } }
@BaseCommands.java public void registerForOn(Handler h, int what, Object obj) { Registrant r = new Registrant (h, what, obj); synchronized (mStateMonitor) { //将注册信息保存在mOnRegistrants列表中 mOnRegistrants.add(r); if (mState.isOn()) { r.notifyRegistrant(new AsyncResult(null, null, null)); } } }注册的过程就是把监听者信息保存在mOnRegistrants变量的List列表中,然后当以下两种事件发生时就会遍历该List并通知监听者:
private void processUnsolicited (Parcel p) { switch(response) { case RIL_UNSOL_RESPONSE_RADIO_STATE_CHANGED: RadioState newState = getRadioStateFromInt(p.readInt()); switchToRadioState(newState); break; } } private void switchToRadioState(RadioState newState) { setRadioState(newState); } protected void setRadioState(RadioState newState) { synchronized (mStateMonitor) { //通知监听者 mRadioStateChangedRegistrants.notifyRegistrants(); } }2、RILJ与RILC通道断开时
class RILReceiver implements Runnable { @Override public void run() { //Socket通道断开,设置Radio状态为unavailable状态 setRadioState (RadioState.RADIO_UNAVAILABLE); } }
接下来我们查看当上述情况发生时,UiccController的动作:
@UiccController.java public void handleMessage (Message msg) { synchronized (mLock) { switch (msg.what) { case EVENT_ICC_STATUS_CHANGED: //无论是SIM卡的状态或者Radio的状态发生改变,都会进入该case中 mCi.getIccCardStatus(obtainMessage(EVENT_GET_ICC_STATUS_DONE)); break; case EVENT_GET_ICC_STATUS_DONE: AsyncResult ar = (AsyncResult)msg.obj; onGetIccCardStatusDone(ar); break; default: Rlog.e(LOG_TAG, " Unknown Event " + msg.what); } } }然后就会调用RIL.java的getIccCardStatus()方法,去查询最新的SIM和Radio状态。
@RIL.java public void getIccCardStatus(Message result) { //向Modem发送请求,获取当前的SIM卡状态 RILRequest rr = RILRequest.obtain(RIL_REQUEST_GET_SIM_STATUS, result); send(rr); }然后获取到最新状态后,将结果反馈给发起请求的对象,当前就是UiccController:
private RILRequest processSolicited (Parcel p) { switch (rr.mRequest) { case RIL_REQUEST_GET_SIM_STATUS: //根据Modem得到的返回值构建IccCardStatus对象 ret = responseIccCardStatus(p); break; } if (rr.mResult != null) { AsyncResult.forMessage(rr.mResult, ret, null); //将结果返回给UiccController rr.mResult.sendToTarget(); } }接下来,UiccController就再次通过handleMessage()方法来处理接下来的流程,只不过这次进入的是EVENT_GET_ICC_STATUS_DONE的case流程:
@UiccController.java public void handleMessage (Message msg) { synchronized (mLock) { switch (msg.what) { case EVENT_ICC_STATUS_CHANGED: //得到状态改变通知,发起请求 mCi.getIccCardStatus(obtainMessage(EVENT_GET_ICC_STATUS_DONE)); break; case EVENT_GET_ICC_STATUS_DONE: //获取最新的SIM卡状态 AsyncResult ar = (AsyncResult)msg.obj; onGetIccCardStatusDone(ar); break; default: Rlog.e(LOG_TAG, " Unknown Event " + msg.what); } } }继续看UiccController对最新状态的处理:
private synchronized void onGetIccCardStatusDone(AsyncResult ar) { if (ar.exception != null) { return; } //拿到RIL中创建的IccCardStatus对象 IccCardStatus status = (IccCardStatus)ar.result; //创建或更新UiccCard if (mUiccCard == null) { mUiccCard = new UiccCard(mContext, mCi, status); } else { mUiccCard.update(mContext, mCi , status); } //继续向其他对象派发消息 mIccChangedRegistrants.notifyRegistrants(); }到这里我们看到,UiccController获取到最新的SIM卡状态之后,做了两件事情: 1、创建或更新UiccCard;2、通知其他子监听对象。