//–frameworks/base/telephony/java/android/telephony/TelephonyManager.java
public String getLine1Number() { return getLine1Number(getSubId()); } public String getLine1Number(int subId) { String number = null; try { ITelephony telephony = getITelephony(); if (telephony != null) number = telephony.getLine1NumberForDisplay(subId, mContext.getOpPackageName()); } catch (RemoteException ex) { } catch (NullPointerException ex) { } if (number != null) { return number; } try { //手上测试卡执行的是这部分代码: IPhoneSubInfo info = getSubscriberInfo(); if (info == null) return null; return info.getLine1NumberForSubscriber(subId, mContext.getOpPackageName()); } catch (RemoteException ex) { return null; } catch (NullPointerException ex) { // This could happen before phone restarts due to crashing return null; } }//–frameworks/opt/telephony/src/java/com/android/internal/telephony/PhoneSubInfoController.java
public String getLine1Number(String callingPackage) { return getLine1NumberForSubscriber(getDefaultSubscription(), callingPackage); } public String getLine1NumberForSubscriber(int subId, String callingPackage) { Phone phone = getPhone(subId); if (phone != null) { // This is open to apps with WRITE_SMS. if (!checkReadPhoneNumber(callingPackage, "getLine1Number")) { return null; } return phone.getLine1Number(); } else { loge("getLine1Number phone is null for Subscription:" + subId); return null; } }//–frameworks/opt/telephony/src/java/com/android/internal/telephony/GsmCdmaPhone.java
@Override public String getLine1Number() { d("getLine1Number " + isPhoneTypeGsm()); if (isPhoneTypeGsm()) { IccRecords r = mIccRecords.get(); return (r != null) ? r.getMsisdnNumber() : null; } else { return mSST.getMdnNumber(); } }//–frameworks/opt/telephony/src/java/com/android/internal/telephony/uicc/SIMRecords.java
//直接返回mMsisdn @Override public String getMsisdnNumber() { return mMsisdn; } //mMsisdn的赋值: @Override public void handleMessage(Message msg) { //... case EVENT_GET_MSISDN_DONE: isRecordLoadResponse = true; ar = (AsyncResult)msg.obj; if (ar.exception != null) { log("Invalid or missing EF[MSISDN]"); break; } adn = (AdnRecord)ar.result; mMsisdn = adn.getNumber(); mMsisdnTag = adn.getAlphaTag(); log("MSISDN: " + /*mMsisdn*/ Rlog.pii(LOG_TAG, mMsisdn)); break; //... }//–frameworks/opt/telephony/src/java/com/android/internal/telephony/uicc/SIMRecords.java
//发起获取SIM卡卡号请求: protected void fetchSimRecords() { d("fetchSimRecords"); mRecordsRequested = true; if (DBG) log("fetchSimRecords " + mRecordsToLoad); mCi.getIMSIForApp(mParentApp.getAid(), obtainMessage(EVENT_GET_IMSI_DONE)); mRecordsToLoad++; mFh.loadEFTransparent(EF_ICCID, obtainMessage(EVENT_GET_ICCID_DONE)); mRecordsToLoad++; // FIXME should examine EF[MSISDN]'s capability configuration // to determine which is the voice/data/fax line new AdnRecordLoader(mFh).loadFromEF(EF_MSISDN, getExtFromEf(EF_MSISDN), 1, obtainMessage(EVENT_GET_MSISDN_DONE)); }//–frameworks/opt/telephony/src/java/com/android/internal/telephony/uicc/AdnRecordLoader.java
class AdnRecordLoader extends Handler /** * Resulting AdnRecord is placed in response.obj.result * or response.obj.exception is set */ public void loadFromEF(int ef, int extensionEF, int recordNumber, Message response) { mEf = ef; mExtensionEF = extensionEF; mRecordNumber = recordNumber; mUserResponse = response; mFh.loadEFLinearFixed( ef, getEFPath(ef), recordNumber, obtainMessage(EVENT_ADN_LOAD_DONE)); }//–frameworks/opt/telephony/src/java/com/android/internal/telephony/uicc/IccFileHandler.java
public void loadEFLinearFixed(int fileid, String path, int recordNum, Message onLoaded) { String efPath = (path == null) ? getEFPath(fileid) : path; Message response = obtainMessage(EVENT_GET_RECORD_SIZE_DONE, new LoadLinearFixedContext(fileid, recordNum, efPath, onLoaded)); d("loadEFLinearFixed fileid(" + fileid + "), path(" + path + ")"); mCi.iccIOForApp(COMMAND_GET_RESPONSE, fileid, efPath, 0, 0, GET_RESPONSE_EF_SIZE_BYTES, null, null, mAid, response); }//–frameworks/opt/telephony/src/java/com/android/internal/telephony/RIL.java
@Override public void iccIOForApp (int command, int fileid, String path, int p1, int p2, int p3, String data, String pin2, String aid, Message result) { //Note: This RIL request has not been renamed to ICC, // but this request is also valid for SIM and RUIM RILRequest rr = RILRequest.obtain(RIL_REQUEST_SIM_IO, result); rr.mParcel.writeInt(command); rr.mParcel.writeInt(fileid); rr.mParcel.writeString(path); rr.mParcel.writeInt(p1); rr.mParcel.writeInt(p2); rr.mParcel.writeInt(p3); rr.mParcel.writeString(data); rr.mParcel.writeString(pin2); rr.mParcel.writeString(aid); if (RILJ_LOGD) riljLog(rr.serialString() + "> iccIO: " + requestToString(rr.mRequest) + " 0x" + Integer.toHexString(command) + " 0x" + Integer.toHexString(fileid) + " " + " path: " + path + "," + p1 + "," + p2 + "," + p3 + " aid: " + aid); send(rr); }发送的过程消息顺序: SIMRecords -> AdnRecordLoader -> IccFileHandler -> RIL
//–frameworks/opt/telephony/src/java/com/android/internal/telephony/RIL.java
private void processResponse (Parcel p) { int type; type = p.readInt(); if (type == RESPONSE_UNSOLICITED || type == RESPONSE_UNSOLICITED_ACK_EXP) { processUnsolicited (p, type); } else if (type == RESPONSE_SOLICITED || type == RESPONSE_SOLICITED_ACK_EXP) { RILRequest rr = processSolicited (p, type); if (rr != null) { if (type == RESPONSE_SOLICITED) { decrementWakeLock(rr); } rr.release(); return; } } else if (type == RESPONSE_SOLICITED_ACK) { int serial; serial = p.readInt(); RILRequest rr; synchronized (mRequestList) { rr = mRequestList.get(serial); } if (rr == null) { Rlog.w(RILJ_LOG_TAG, "Unexpected solicited ack response! sn: " + serial); } else { decrementWakeLock(rr); if (RILJ_LOGD) { riljLog(rr.serialString() + " Ack < " + requestToString(rr.mRequest)); } } } } private RILRequest processSolicited (Parcel p, int type) { // ... if (error == 0 || p.dataAvail() > 0) { // either command succeeds or command fails but with data payload try {switch (rr.mRequest) { // ... case RIL_REQUEST_SIM_IO: ret = responseICC_IO(p); break; } //... if (error == 0) { //IccIoResult ret. if (RILJ_LOGD) riljLog(rr.serialString() + "< " + requestToString(rr.mRequest) + " " + retToString(rr.mRequest, ret)); if (rr.mResult != null) { if(rr.mRequest == RIL_REQUEST_SIM_IO){ Rlog.d("RIL", "SIM_IO response send " + rr.mResult.what + "{" + ret.toString() + "}"); } AsyncResult.forMessage(rr.mResult, ret, null); rr.mResult.sendToTarget(); } } } private Object responseICC_IO(Parcel p) { int sw1, sw2; Message ret; sw1 = p.readInt(); sw2 = p.readInt(); String s = p.readString(); Rlog.d("RIL", "responseICC_IO 0x" + Integer.toHexString(sw1) + " 0x" + Integer.toHexString(sw2) + " " + s); if (true || RILJ_LOGV) riljLog("< iccIO: " + " 0x" + Integer.toHexString(sw1) + " 0x" + Integer.toHexString(sw2) + " " + s); return new IccIoResult(sw1, sw2, s); }//–frameworks/opt/telephony/src/java/com/android/internal/telephony/uicc/IccFileHandler.java
@Override public void handleMessage(Message msg) { AsyncResult ar; IccIoResult result; Message response = null; String str; LoadLinearFixedContext lc; byte data[]; int size; int fileid; int recordSize[]; String path = null; try { switch (msg.what) case EVENT_GET_RECORD_SIZE_IMG_DONE: case EVENT_GET_RECORD_SIZE_DONE: ar = (AsyncResult)msg.obj; lc = (LoadLinearFixedContext) ar.userObj; result = (IccIoResult) ar.result; response = lc.mOnLoaded; if (processException(response, (AsyncResult) msg.obj)) { break; } data = result.payload; path = lc.mPath; if (TYPE_EF != data[RESPONSE_DATA_FILE_TYPE]) { throw new IccFileTypeMismatch(); } if (EF_TYPE_LINEAR_FIXED != data[RESPONSE_DATA_STRUCTURE]) { throw new IccFileTypeMismatch(); } lc.mRecordSize = data[RESPONSE_DATA_RECORD_LENGTH] & 0xFF; size = ((data[RESPONSE_DATA_FILE_SIZE_1] & 0xff) << 8) + (data[RESPONSE_DATA_FILE_SIZE_2] & 0xff); lc.mCountRecords = size / lc.mRecordSize; if (lc.mLoadAll) { lc.results = new ArrayList<byte[]>(lc.mCountRecords); } if (path == null) { path = getEFPath(lc.mEfid); } mCi.iccIOForApp(COMMAND_READ_RECORD, lc.mEfid, path, lc.mRecordNum, READ_RECORD_MODE_ABSOLUTE, lc.mRecordSize, null, null, mAid, obtainMessage(EVENT_READ_RECORD_DONE, lc)); break; }//–frameworks/opt/telephony/src/java/com/android/internal/telephony/uicc/AdnRecordLoader.java
void handleMessage(){ case EVENT_ADN_LOAD_DONE: ar = (AsyncResult)(msg.obj); data = (byte[])(ar.result); if (ar.exception != null) { throw new RuntimeException("load failed", ar.exception); } if (VDBG) { Rlog.d(LOG_TAG,"ADN EF: 0x" + Integer.toHexString(mEf) + ":" + mRecordNumber + "\n" + IccUtils.bytesToHexString(data)); } adn = new AdnRecord(mEf, mRecordNumber, data); mResult = adn; if (adn.hasExtendedRecord()) { // If we have a valid value in the ext record field, // we're not done yet: we need to read the corresponding // ext record and append it mPendingExtLoads = 1; mFh.loadEFLinearFixed( mExtensionEF, adn.mExtRecord, obtainMessage(EVENT_EXT_RECORD_LOAD_DONE, adn)); } break; }处理响应的顺序: RIL -> IccFileHandler -> AdnRecordLoader -> SIMRecords
以上代码及相关逻辑并非通用, RIL本身存在一定的复杂性, 具体情况需具体分析.
仅供参考