android中sim卡相關操作

轉載:http://topic.csdn.net/u/20110417/14/edcbbcd9-44f0-4175-a156-0231d12cba47.html 

1,sim卡聯繫人的增,刪,修改
Uri是content://icc/adn/
具體使用跟操作其他的contentprovider一樣,傳遞的參數可以參照\base\telephony\java\com\android\internal\telephony\IccProvider.java

2,存入聯繫人到USIM卡的問題
這個問題的實質是普通sim卡文件的EF ID跟USIM卡中的EF ID不同造成,SIM卡中的使用的是是EF_ADN = 0x6F3A,而USIM卡中存儲聯繫人的EFID 則爲EF_PBR = 0x4F30

修改辦法:
1,在\base\telephony\java\com\android\internal\telephony\IccPhoneBookInterfaceManager.java 中的updateAdnRecordsInEfBySearch函數當中檢查是不是使用USIM卡
加入下面一行
  if (phone.getContext().checkCallingOrSelfPermission(
  android.Manifest.permission.WRITE_CONTACTS)
  != PackageManager.PERMISSION_GRANTED) {
  throw new SecurityException(
  "Requires android.permission.WRITE_CONTACTS permission");
  }
  // modified by leon at 5/11/2010 change sim card ef to USIM card ef if user use USIM card
  efid = updateEfForIccType(efid);
  // modified end;
   
  if (DBG) logd("updateAdnRecordsInEfBySearch: efid=" + efid +
  " ("+ oldTag + "," + oldPhoneNumber + ")"+ "==>" +
  " ("+ newTag + "," + newPhoneNumber + ")"+ " pin2=" + pin2);

updateEfForIccType(efid)的實現爲:
  private int updateEfForIccType(int efid) {
  // Check if we are trying to read ADN records
  if (efid == IccConstants.EF_ADN) {
  if (phone.getIccCard().isApplicationOnIcc(IccCardApplication.AppType.APPTYPE_USIM)) {
  return IccConstants.EF_PBR;
  }
  }
  return efid;
  }


在\base\telephony\java\com\android\internal\telephony\AdnRecordCache.java中我修改之後的updateAdnBySearch函數爲

public void updateAdnBySearch(int efid, AdnRecord oldAdn, AdnRecord newAdn,
  String pin2, Message response) {

  int extensionEF;
  extensionEF = extensionEfForEf(efid);

  if (extensionEF < 0) {
  sendErrorResponse(response, "EF is not known ADN-like EF:" + efid);
  return;
  }

// modified by leon 5/11/2010
  // if user use USIM card,the change the phonebook ef
  ArrayList<AdnRecord> oldAdnList = null;
  if(efid == EF_PBR){
  int usimEf[] = mUsimPhoneBookManager.getUsimEf();
  int usimExtensionEF[] = mUsimPhoneBookManager.getUsimExtensionEF();
  int size = usimEf.length;
 
  for(int i =0;i<size;i++){
  oldAdnList = getRecordsIfLoaded(usimEf[i]);
  efid = usimEf[i];
  extensionEF = usimExtensionEF[i];
  boolean bBreak = false;
  if (oldAdnList != null){
  for (Iterator<AdnRecord> it = oldAdnList.iterator(); it.hasNext();) {
  if (oldAdn.isEqual(it.next())) {
  bBreak = true;
  break;
  }
  }
  }
  if(bBreak)
  break;
  }
  }else{
  oldAdnList = getRecordsIfLoaded(efid);
  }
  // modified end

   

  if (oldAdnList == null) {
  sendErrorResponse(response, "Adn list not exist for EF:" + efid);
  return;
  }

  int index = -1;
  int count = 1;
  for (Iterator<AdnRecord> it = oldAdnList.iterator(); it.hasNext(); ) {
  if (oldAdn.isEqual(it.next())) {
  index = count;
  break;
  }
  count++;
  }

  if (index == -1) {
  sendErrorResponse(response, "Adn record don't exist for " + oldAdn);
  return;
  }

  Message pendingResponse = userWriteResponse.get(efid);

  if (pendingResponse != null) {
  sendErrorResponse(response, "Have pending update for EF:" + efid);
  return;
  }

  userWriteResponse.put(efid, response);

  new AdnRecordLoader(phone).updateEF(newAdn, efid, extensionEF,
  index, pin2,
  obtainMessage(EVENT_UPDATE_ADN_DONE, efid, index, newAdn));
  }

其中getUsimEf跟getUsimExtensionEF兩個函數的實現如下:
  // add by leon at 5/11/2010 
   
 
  public int[] getUsimEf(){
  if (mPbrFile == null) return null;
  int numRecs = mPbrFile.mFileIds.size();
  int[] result = new int[numRecs];
  for (int i = 0; i < numRecs; i++) {
  Map <Integer,Integer> fileIds;
  fileIds = mPbrFile.mFileIds.get(i);
  if (fileIds == null || fileIds.isEmpty()){
  result[i] = 0;
  }else{
  result[i] = fileIds.get(USIM_EFADN_TAG);
  }
 
  }
  return result;
  }
   
   
 
  public int[] getUsimExtensionEF(){
  if (mPbrFile == null) return null;
  int numRecs = mPbrFile.mFileIds.size();
  int[] result = new int[numRecs];
  for (int i = 0; i < numRecs; i++) {
  Map <Integer,Integer> fileIds;
  fileIds = mPbrFile.mFileIds.get(i);
  if (fileIds == null || fileIds.isEmpty()){
  result[i] = 0;
  }else{
  result[i] = fileIds.get(USIM_EFEXT1_TAG);
  }
 
  }
  return result;
  }
   
 
  public void addUsimAdn(AdnRecord adn){
  mPhoneBookRecords.add(adn);
  }
   
  // add end

這裏還有一個addUsimAdn函數調用是在這個文件中handleMessage函數中,具體幹啥好像是在寫入USIM卡的同時也在保留一份在cache文件中
  handleMessage(Message msg) {
  AsyncResult ar;
  int efid;

  switch(msg.what) 
  case EVENT_UPDATE_ADN_DONE:
  ar = (AsyncResult)msg.obj;
  efid = msg.arg1;
  int index = msg.arg2;
  AdnRecord adn = (AdnRecord) (ar.userObj);

  if (ar.exception == null) {
  adnLikeFiles.get(efid).set(index - 1, adn);
  // add by leon to update UsimPhoneBookManager,when export a contacts to usim card.
  if(efid!=IccConstants.EF_ADN){
  mUsimPhoneBookManager.addUsimAdn(adn);
  }
  // add end
  }


這些修改應該能把聯繫人存入USIM卡中了

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章