UiccCard本身并不实现具体的功能,只是作为间接接口向UiccController提供UiccCardApplication对象和完成CatService的创建工作,以及当SIM卡被插入或者拔出时弹出提示框是否需要重启设备。
public boolean isApplicationOnIcc(IccCardApplicationStatus.AppType type) {} public UiccCardApplication getApplication(int family) {} public UiccCardApplication getApplicationIndex(int index) {} public void registerForAbsent(Handler h, int what, Object obj) {} public CardState getCardState() {}由此我们可以看到,UiccCard的主要功能为;
2、接受注册SIM卡是否存在的监听器
3、得到当前的SIM状态对象:CardState
@UiccController.java private synchronized void onGetIccCardStatusDone(AsyncResult ar) { IccCardStatus status = (IccCardStatus)ar.result; if (mUiccCard == null) { //创建UiccCard mUiccCard = new UiccCard(mContext, mCi, status); } else { //更新UiccCard mUiccCard.update(mContext, mCi , status); } mIccChangedRegistrants.notifyRegistrants(); }接下来我们来看UiccCard这个对象的本质,通过其继承关系可以看出他是独立的类,没有继承其他的父类:
public class UiccCard {}然后看他的构造函数:
public UiccCard(Context c, CommandsInterface ci, IccCardStatus ics) { mCardState = ics.mCardState; update(c, ci, ics); }
构造函数比较简单,主要就是初始化UiccCard中的SIM卡状态mCardState,然后就调用update()方法更新自己的状态。
public void update(Context c, CommandsInterface ci, IccCardStatus ics) { synchronized (mLock) { if (mDestroyed) { return; } CardState oldState = mCardState; mCardState = ics.mCardState; mUniversalPinState = ics.mUniversalPinState; mGsmUmtsSubscriptionAppIndex = ics.mGsmUmtsSubscriptionAppIndex; mCdmaSubscriptionAppIndex = ics.mCdmaSubscriptionAppIndex; mImsSubscriptionAppIndex = ics.mImsSubscriptionAppIndex; mContext = c; mCi = ci; //update applications if (DBG) log(ics.mApplications.length + " applications"); for ( int i = 0; i < mUiccApplications.length; i++) { if (mUiccApplications[i] == null) { if (i < ics.mApplications.length) { //创建UiccCardApplication对象 mUiccApplications[i] = new UiccCardApplication(this, ics.mApplications[i], mContext, mCi); } } else if (i >= ics.mApplications.length) { mUiccApplications[i].dispose(); mUiccApplications[i] = null; } else { //更新UiccCardApplication对象 mUiccApplications[i].update(ics.mApplications[i], mContext, mCi); } } if (mUiccApplications.length > 0 && mUiccApplications[0] != null) { //创建CatService mCatService = CatService.getInstance(mCi, mContext, this); } else { if (mCatService != null) { mCatService.dispose(); } mCatService = null; } sanitizeApplicationIndexes(); RadioState radioState = mCi.getRadioState(); if (radioState == RadioState.RADIO_ON && mLastRadioState == RadioState.RADIO_ON) { if (oldState != CardState.CARDSTATE_ABSENT && mCardState == CardState.CARDSTATE_ABSENT) { //SIM卡被拔出,通知监听者,并弹出提示框是否重启手机 mAbsentRegistrants.notifyRegistrants(); mHandler.sendMessage(mHandler.obtainMessage(EVENT_CARD_REMOVED, null)); } else if (oldState == CardState.CARDSTATE_ABSENT && mCardState != CardState.CARDSTATE_ABSENT) { //插入新的SIM卡,并弹出提示框是否重启手机 mHandler.sendMessage(mHandler.obtainMessage(EVENT_CARD_ADDED, null)); } } mLastRadioState = radioState; } }在UiccCard的初始化过程中,完成了非常重要的三个任务:
这就是UiccCard的更新过程。
public void update(Context c, CommandsInterface ci, IccCardStatus ics) { synchronized (mLock) { if (radioState == RadioState.RADIO_ON && mLastRadioState == RadioState.RADIO_ON) { if (oldState != CardState.CARDSTATE_ABSENT && mCardState == CardState.CARDSTATE_ABSENT) { mAbsentRegistrants.notifyRegistrants(); //拔出SIM卡的操作 mHandler.sendMessage(mHandler.obtainMessage(EVENT_CARD_REMOVED, null)); } else if (oldState == CardState.CARDSTATE_ABSENT && mCardState != CardState.CARDSTATE_ABSENT) { //插入SIM卡的操作 mHandler.sendMessage(mHandler.obtainMessage(EVENT_CARD_ADDED, null)); } } mLastRadioState = radioState; } }然后进入Handler中处理:
protected Handler mHandler = new Handler() { @Override public void handleMessage(Message msg){ switch (msg.what) { case EVENT_CARD_REMOVED: onIccSwap(false); break; case EVENT_CARD_ADDED: onIccSwap(true); break; default: loge("Unknown Event " + msg.what); } } };然后进入onIccSwap()中弹出对话框:
private void onIccSwap(boolean isAdded) { synchronized (mLock) { DialogInterface.OnClickListener listener = null; listener = new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { synchronized (mLock) { if (which == DialogInterface.BUTTON_POSITIVE) { //点击按钮,重启设备 PowerManager pm = (PowerManager) mContext .getSystemService(Context.POWER_SERVICE); pm.reboot("SIM is added."); } } } }; Resources r = Resources.getSystem(); //加载不同的资源 String title = (isAdded) ? r.getString(R.string.sim_added_title) : r.getString(R.string.sim_removed_title); String message = (isAdded) ? r.getString(R.string.sim_added_message) : r.getString(R.string.sim_removed_message); String buttonTxt = r.getString(R.string.sim_restart_button); //弹出Dialog AlertDialog dialog = new AlertDialog.Builder(mContext) .setTitle(title) .setMessage(message) .setPositiveButton(buttonTxt, listener) .create(); dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT); dialog.show(); } }
这就是弹出提示框的过程。
下面一个章节来介绍UiccCardApplication相关内容。