关闭

Uicc之UiccCard(原)

标签: UiccTelephonyAndroidFramework
1904人阅读 评论(0) 收藏 举报
分类:

        UiccCard本身并不实现具体的功能,只是作为间接接口向UiccController提供UiccCardApplication对象和完成CatService的创建工作,以及当SIM卡被插入或者拔出时弹出提示框是否需要重启设备。


一、UiccCard的主要功能


        我们从UiccCard提供的主要方法来看其功能。
        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的主要功能为;
        1、创建和向外提供UiccCardApplication对象

        2、接受注册SIM卡是否存在的监听器

        3、得到当前的SIM状态对象:CardState


二、UiccCard的初始化过程


        在介绍UiccController的更新机制中介绍到,当UiccController接收到SIM卡上报的状态更新消息后,就会更新(update)或者创建UiccCard对象:
    @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()方法更新自己的状态。


三、UiccCard的更新过程


        UiccCard不仅在构造函数中会调用update()方法,而且在UiccController检测到SIM卡状态改变后也会调用,所以这里的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的初始化过程中,完成了非常重要的三个任务:
        1、创建和更新UiccCardApplication对象;
        2、初始化CatService
        3、当检测到SIM卡的拔出或插入动作后,通过mHandler对象弹出提示框是否重启手机

        这就是UiccCard的更新过程。


四、UiccCard检测的SIM卡移除/插入操作


        下面简要看以下UiccCard对SIM卡移除/插入的操作。
        前面分析到,UiccCard的update()过程中可以接收到SIM卡状态的改变,当UiccCard检测到SIM卡插入或者拔出的操作后,就会弹出一个提示框,提示是否重启设备。
        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相关内容。

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:303498次
    • 积分:3627
    • 等级:
    • 排名:第9394名
    • 原创:72篇
    • 转载:0篇
    • 译文:0篇
    • 评论:112条
    最新评论