IccRecords.java revision a8467dd0c524787104b1ccdddc5e8af10ba729ed
1/* 2 * Copyright (C) 2006 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package com.android.internal.telephony.uicc; 18 19import android.content.Context; 20import android.os.AsyncResult; 21import android.os.Handler; 22import android.os.Message; 23import android.os.Registrant; 24import android.os.RegistrantList; 25 26import com.android.internal.telephony.CommandsInterface; 27import com.android.internal.telephony.uicc.IccCardApplicationStatus.AppState; 28 29import java.io.FileDescriptor; 30import java.io.PrintWriter; 31import java.util.concurrent.atomic.AtomicBoolean; 32 33/** 34 * {@hide} 35 */ 36public abstract class IccRecords extends Handler implements IccConstants { 37 protected static final boolean DBG = true; 38 39 // ***** Instance Variables 40 protected AtomicBoolean mDestroyed = new AtomicBoolean(false); 41 protected Context mContext; 42 protected CommandsInterface mCi; 43 protected IccFileHandler mFh; 44 protected UiccCardApplication mParentApp; 45 46 protected RegistrantList mRecordsLoadedRegistrants = new RegistrantList(); 47 protected RegistrantList mImsiReadyRegistrants = new RegistrantList(); 48 protected RegistrantList mRecordsEventsRegistrants = new RegistrantList(); 49 protected RegistrantList mNewSmsRegistrants = new RegistrantList(); 50 protected RegistrantList mNetworkSelectionModeAutomaticRegistrants = new RegistrantList(); 51 52 protected int mRecordsToLoad; // number of pending load requests 53 54 protected AdnRecordCache mAdnCache; 55 56 // ***** Cached SIM State; cleared on channel close 57 58 protected boolean mRecordsRequested = false; // true if we've made requests for the sim records 59 60 protected String mIccId; 61 protected String mMsisdn = null; // My mobile number 62 protected String mMsisdnTag = null; 63 protected String mVoiceMailNum = null; 64 protected String mVoiceMailTag = null; 65 protected String mNewVoiceMailNum = null; 66 protected String mNewVoiceMailTag = null; 67 protected boolean mIsVoiceMailFixed = false; 68 protected int mCountVoiceMessages = 0; 69 protected String mImsi; 70 private String auth_rsp; 71 72 protected int mMncLength = UNINITIALIZED; 73 protected int mMailboxIndex = 0; // 0 is no mailbox dailing number associated 74 75 protected String mSpn; 76 77 protected String mGid1; 78 79 private final Object mLock = new Object(); 80 81 // ***** Constants 82 83 // Markers for mncLength 84 protected static final int UNINITIALIZED = -1; 85 protected static final int UNKNOWN = 0; 86 87 // Bitmasks for SPN display rules. 88 public static final int SPN_RULE_SHOW_SPN = 0x01; 89 public static final int SPN_RULE_SHOW_PLMN = 0x02; 90 91 // ***** Event Constants 92 protected static final int EVENT_SET_MSISDN_DONE = 30; 93 public static final int EVENT_MWI = 0; // Message Waiting indication 94 public static final int EVENT_CFI = 1; // Call Forwarding indication 95 public static final int EVENT_SPN = 2; // Service Provider Name 96 97 public static final int EVENT_GET_ICC_RECORD_DONE = 100; 98 protected static final int EVENT_APP_READY = 1; 99 private static final int EVENT_AKA_AUTHENTICATE_DONE = 90; 100 101 @Override 102 public String toString() { 103 return "mDestroyed=" + mDestroyed 104 + " mContext=" + mContext 105 + " mCi=" + mCi 106 + " mFh=" + mFh 107 + " mParentApp=" + mParentApp 108 + " recordsLoadedRegistrants=" + mRecordsLoadedRegistrants 109 + " mImsiReadyRegistrants=" + mImsiReadyRegistrants 110 + " mRecordsEventsRegistrants=" + mRecordsEventsRegistrants 111 + " mNewSmsRegistrants=" + mNewSmsRegistrants 112 + " mNetworkSelectionModeAutomaticRegistrants=" 113 + mNetworkSelectionModeAutomaticRegistrants 114 + " recordsToLoad=" + mRecordsToLoad 115 + " adnCache=" + mAdnCache 116 + " recordsRequested=" + mRecordsRequested 117 + " iccid=" + mIccId 118 + " msisdn=" + mMsisdn 119 + " msisdnTag=" + mMsisdnTag 120 + " voiceMailNum=" + mVoiceMailNum 121 + " voiceMailTag=" + mVoiceMailTag 122 + " newVoiceMailNum=" + mNewVoiceMailNum 123 + " newVoiceMailTag=" + mNewVoiceMailTag 124 + " isVoiceMailFixed=" + mIsVoiceMailFixed 125 + " countVoiceMessages=" + mCountVoiceMessages 126 + " mImsi=" + mImsi 127 + " mncLength=" + mMncLength 128 + " mailboxIndex=" + mMailboxIndex 129 + " spn=" + mSpn; 130 131 } 132 133 /** 134 * Generic ICC record loaded callback. Subclasses can call EF load methods on 135 * {@link IccFileHandler} passing a Message for onLoaded with the what field set to 136 * {@link #EVENT_GET_ICC_RECORD_DONE} and the obj field set to an instance 137 * of this interface. The {@link #handleMessage} method in this class will print a 138 * log message using {@link #getEfName()} and decrement {@link #mRecordsToLoad}. 139 * 140 * If the record load was successful, {@link #onRecordLoaded} will be called with the result. 141 * Otherwise, an error log message will be output by {@link #handleMessage} and 142 * {@link #onRecordLoaded} will not be called. 143 */ 144 public interface IccRecordLoaded { 145 String getEfName(); 146 void onRecordLoaded(AsyncResult ar); 147 } 148 149 // ***** Constructor 150 public IccRecords(UiccCardApplication app, Context c, CommandsInterface ci) { 151 mContext = c; 152 mCi = ci; 153 mFh = app.getIccFileHandler(); 154 mParentApp = app; 155 } 156 157 /** 158 * Call when the IccRecords object is no longer going to be used. 159 */ 160 public void dispose() { 161 mDestroyed.set(true); 162 mParentApp = null; 163 mFh = null; 164 mCi = null; 165 mContext = null; 166 } 167 168 public abstract void onReady(); 169 170 //***** Public Methods 171 public AdnRecordCache getAdnCache() { 172 return mAdnCache; 173 } 174 175 public String getIccId() { 176 return mIccId; 177 } 178 179 public void registerForRecordsLoaded(Handler h, int what, Object obj) { 180 if (mDestroyed.get()) { 181 return; 182 } 183 184 Registrant r = new Registrant(h, what, obj); 185 mRecordsLoadedRegistrants.add(r); 186 187 if (mRecordsToLoad == 0 && mRecordsRequested == true) { 188 r.notifyRegistrant(new AsyncResult(null, null, null)); 189 } 190 } 191 public void unregisterForRecordsLoaded(Handler h) { 192 mRecordsLoadedRegistrants.remove(h); 193 } 194 195 public void registerForImsiReady(Handler h, int what, Object obj) { 196 if (mDestroyed.get()) { 197 return; 198 } 199 200 Registrant r = new Registrant(h, what, obj); 201 mImsiReadyRegistrants.add(r); 202 203 if (mImsi != null) { 204 r.notifyRegistrant(new AsyncResult(null, null, null)); 205 } 206 } 207 public void unregisterForImsiReady(Handler h) { 208 mImsiReadyRegistrants.remove(h); 209 } 210 211 public void registerForRecordsEvents(Handler h, int what, Object obj) { 212 Registrant r = new Registrant (h, what, obj); 213 mRecordsEventsRegistrants.add(r); 214 } 215 public void unregisterForRecordsEvents(Handler h) { 216 mRecordsEventsRegistrants.remove(h); 217 } 218 219 public void registerForNewSms(Handler h, int what, Object obj) { 220 Registrant r = new Registrant (h, what, obj); 221 mNewSmsRegistrants.add(r); 222 } 223 public void unregisterForNewSms(Handler h) { 224 mNewSmsRegistrants.remove(h); 225 } 226 227 public void registerForNetworkSelectionModeAutomatic( 228 Handler h, int what, Object obj) { 229 Registrant r = new Registrant (h, what, obj); 230 mNetworkSelectionModeAutomaticRegistrants.add(r); 231 } 232 public void unregisterForNetworkSelectionModeAutomatic(Handler h) { 233 mNetworkSelectionModeAutomaticRegistrants.remove(h); 234 } 235 236 /** 237 * Get the International Mobile Subscriber ID (IMSI) on a SIM 238 * for GSM, UMTS and like networks. Default is null if IMSI is 239 * not supported or unavailable. 240 * 241 * @return null if SIM is not yet ready or unavailable 242 */ 243 public String getIMSI() { 244 return null; 245 } 246 247 /** 248 * Imsi could be set by ServiceStateTrackers in case of cdma 249 * @param imsi 250 */ 251 public void setImsi(String imsi) { 252 mImsi = imsi; 253 mImsiReadyRegistrants.notifyRegistrants(); 254 } 255 256 public String getMsisdnNumber() { 257 return mMsisdn; 258 } 259 260 /** 261 * Get the Group Identifier Level 1 (GID1) on a SIM for GSM. 262 * @return null if SIM is not yet ready 263 */ 264 public String getGid1() { 265 return null; 266 } 267 268 /** 269 * Set subscriber number to SIM record 270 * 271 * The subscriber number is stored in EF_MSISDN (TS 51.011) 272 * 273 * When the operation is complete, onComplete will be sent to its handler 274 * 275 * @param alphaTag alpha-tagging of the dailing nubmer (up to 10 characters) 276 * @param number dailing nubmer (up to 20 digits) 277 * if the number starts with '+', then set to international TOA 278 * @param onComplete 279 * onComplete.obj will be an AsyncResult 280 * ((AsyncResult)onComplete.obj).exception == null on success 281 * ((AsyncResult)onComplete.obj).exception != null on fail 282 */ 283 public void setMsisdnNumber(String alphaTag, String number, 284 Message onComplete) { 285 286 mMsisdn = number; 287 mMsisdnTag = alphaTag; 288 289 if (DBG) log("Set MSISDN: " + mMsisdnTag +" " + mMsisdn); 290 291 292 AdnRecord adn = new AdnRecord(mMsisdnTag, mMsisdn); 293 294 new AdnRecordLoader(mFh).updateEF(adn, EF_MSISDN, EF_EXT1, 1, null, 295 obtainMessage(EVENT_SET_MSISDN_DONE, onComplete)); 296 } 297 298 public String getMsisdnAlphaTag() { 299 return mMsisdnTag; 300 } 301 302 public String getVoiceMailNumber() { 303 return mVoiceMailNum; 304 } 305 306 /** 307 * Return Service Provider Name stored in SIM (EF_SPN=0x6F46) or in RUIM (EF_RUIM_SPN=0x6F41) 308 * @return null if SIM is not yet ready or no RUIM entry 309 */ 310 public String getServiceProviderName() { 311 return mSpn; 312 } 313 314 /** 315 * Set voice mail number to SIM record 316 * 317 * The voice mail number can be stored either in EF_MBDN (TS 51.011) or 318 * EF_MAILBOX_CPHS (CPHS 4.2) 319 * 320 * If EF_MBDN is available, store the voice mail number to EF_MBDN 321 * 322 * If EF_MAILBOX_CPHS is enabled, store the voice mail number to EF_CHPS 323 * 324 * So the voice mail number will be stored in both EFs if both are available 325 * 326 * Return error only if both EF_MBDN and EF_MAILBOX_CPHS fail. 327 * 328 * When the operation is complete, onComplete will be sent to its handler 329 * 330 * @param alphaTag alpha-tagging of the dailing nubmer (upto 10 characters) 331 * @param voiceNumber dailing nubmer (upto 20 digits) 332 * if the number is start with '+', then set to international TOA 333 * @param onComplete 334 * onComplete.obj will be an AsyncResult 335 * ((AsyncResult)onComplete.obj).exception == null on success 336 * ((AsyncResult)onComplete.obj).exception != null on fail 337 */ 338 public abstract void setVoiceMailNumber(String alphaTag, String voiceNumber, 339 Message onComplete); 340 341 public String getVoiceMailAlphaTag() { 342 return mVoiceMailTag; 343 } 344 345 /** 346 * Sets the SIM voice message waiting indicator records 347 * @param line GSM Subscriber Profile Number, one-based. Only '1' is supported 348 * @param countWaiting The number of messages waiting, if known. Use 349 * -1 to indicate that an unknown number of 350 * messages are waiting 351 */ 352 public abstract void setVoiceMessageWaiting(int line, int countWaiting); 353 354 /** @return true if there are messages waiting, false otherwise. */ 355 public boolean getVoiceMessageWaiting() { 356 return mCountVoiceMessages != 0; 357 } 358 359 /** 360 * Returns number of voice messages waiting, if available 361 * If not available (eg, on an older CPHS SIM) -1 is returned if 362 * getVoiceMessageWaiting() is true 363 */ 364 public int getVoiceMessageCount() { 365 return mCountVoiceMessages; 366 } 367 368 /** 369 * Called by STK Service when REFRESH is received. 370 * @param fileChanged indicates whether any files changed 371 * @param fileList if non-null, a list of EF files that changed 372 */ 373 public abstract void onRefresh(boolean fileChanged, int[] fileList); 374 375 /** 376 * Called by subclasses (SimRecords and RuimRecords) whenever 377 * IccRefreshResponse.REFRESH_RESULT_INIT event received 378 */ 379 protected void onIccRefreshInit() { 380 mAdnCache.reset(); 381 if (mParentApp.getState() == AppState.APPSTATE_READY) { 382 // This will cause files to be reread 383 sendMessage(obtainMessage(EVENT_APP_READY)); 384 } 385 } 386 387 public boolean getRecordsLoaded() { 388 if (mRecordsToLoad == 0 && mRecordsRequested == true) { 389 return true; 390 } else { 391 return false; 392 } 393 } 394 395 //***** Overridden from Handler 396 @Override 397 public void handleMessage(Message msg) { 398 AsyncResult ar; 399 400 switch (msg.what) { 401 case EVENT_GET_ICC_RECORD_DONE: 402 try { 403 ar = (AsyncResult) msg.obj; 404 IccRecordLoaded recordLoaded = (IccRecordLoaded) ar.userObj; 405 if (DBG) log(recordLoaded.getEfName() + " LOADED"); 406 407 if (ar.exception != null) { 408 loge("Record Load Exception: " + ar.exception); 409 } else { 410 recordLoaded.onRecordLoaded(ar); 411 } 412 }catch (RuntimeException exc) { 413 // I don't want these exceptions to be fatal 414 loge("Exception parsing SIM record: " + exc); 415 } finally { 416 // Count up record load responses even if they are fails 417 onRecordLoaded(); 418 } 419 break; 420 421 case EVENT_AKA_AUTHENTICATE_DONE: 422 ar = (AsyncResult)msg.obj; 423 auth_rsp = null; 424 if (DBG) log("EVENT_AKA_AUTHENTICATE_DONE"); 425 if (ar.exception != null) { 426 loge("Exception ICC SIM AKA: " + ar.exception); 427 break; 428 } else { 429 try { 430 auth_rsp = (String)ar.result; 431 if (DBG) log("ICC SIM AKA: auth_rsp = " + auth_rsp); 432 } catch (Exception e) { 433 loge("Failed to parse ICC SIM AKA contents: " + e); 434 } 435 } 436 synchronized (mLock) { 437 mLock.notifyAll(); 438 } 439 440 break; 441 442 default: 443 super.handleMessage(msg); 444 } 445 } 446 447 protected abstract void onRecordLoaded(); 448 449 protected abstract void onAllRecordsLoaded(); 450 451 /** 452 * Returns the SpnDisplayRule based on settings on the SIM and the 453 * specified plmn (currently-registered PLMN). See TS 22.101 Annex A 454 * and TS 51.011 10.3.11 for details. 455 * 456 * If the SPN is not found on the SIM, the rule is always PLMN_ONLY. 457 * Generally used for GSM/UMTS and the like SIMs. 458 */ 459 public abstract int getDisplayRule(String plmn); 460 461 /** 462 * Return true if "Restriction of menu options for manual PLMN selection" 463 * bit is set or EF_CSP data is unavailable, return false otherwise. 464 * Generally used for GSM/UMTS and the like SIMs. 465 */ 466 public boolean isCspPlmnEnabled() { 467 return false; 468 } 469 470 /** 471 * Returns the 5 or 6 digit MCC/MNC of the operator that 472 * provided the SIM card. Returns null of SIM is not yet ready 473 * or is not valid for the type of IccCard. Generally used for 474 * GSM/UMTS and the like SIMS 475 */ 476 public String getOperatorNumeric() { 477 return null; 478 } 479 480 /** 481 * Get the current Voice call forwarding flag for GSM/UMTS and the like SIMs 482 * 483 * @return true if enabled 484 */ 485 public boolean getVoiceCallForwardingFlag() { 486 return false; 487 } 488 489 /** 490 * Set the voice call forwarding flag for GSM/UMTS and the like SIMs 491 * 492 * @param line to enable/disable 493 * @param enable 494 * @param number to which CFU is enabled 495 */ 496 public void setVoiceCallForwardingFlag(int line, boolean enable, String number) { 497 } 498 499 /** 500 * Indicates wether SIM is in provisioned state or not. 501 * Overridden only if SIM can be dynamically provisioned via OTA. 502 * 503 * @return true if provisioned 504 */ 505 public boolean isProvisioned () { 506 return true; 507 } 508 509 /** 510 * Write string to log file 511 * 512 * @param s is the string to write 513 */ 514 protected abstract void log(String s); 515 516 /** 517 * Write error string to log file. 518 * 519 * @param s is the string to write 520 */ 521 protected abstract void loge(String s); 522 523 /** 524 * Return an interface to retrieve the ISIM records for IMS, if available. 525 * @return the interface to retrieve the ISIM records, or null if not supported 526 */ 527 public IsimRecords getIsimRecords() { 528 return null; 529 } 530 531 public UsimServiceTable getUsimServiceTable() { 532 return null; 533 } 534 535 /** 536 * Returns the response of the SIM application on the UICC to authentication 537 * challenge/response algorithm. The data string and challenge response are 538 * Base64 encoded Strings. 539 * Can support EAP-SIM, EAP-AKA with results encoded per 3GPP TS 31.102. 540 * 541 * @param data authentication challenge data 542 * @return challenge response 543 */ 544 public String getIccSimChallengeResponse(String data) { 545 if (DBG) log("getIccSimChallengeResponse-data: (original) " + data); 546 547 data = data + mParentApp.getAid(); 548 549 if (DBG) log("getIccSimChallengeResponse-data: (with AID) " + data); 550 551 try { 552 synchronized(mLock) { 553 mCi.requestIccSimAuthentication(data, obtainMessage(EVENT_AKA_AUTHENTICATE_DONE)); 554 try { 555 mLock.wait(); 556 } catch (InterruptedException e) { 557 loge("interrupted while trying to request Icc Sim Auth"); 558 } 559 } 560 } catch(Exception e) { 561 loge( "Fail while trying to request Icc Sim Auth"); 562 return null; 563 } 564 565 if (DBG) log("getIccSimChallengeResponse-auth_rsp" + auth_rsp); 566 567 return auth_rsp; 568 } 569 570 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { 571 pw.println("IccRecords: " + this); 572 pw.println(" mDestroyed=" + mDestroyed); 573 pw.println(" mCi=" + mCi); 574 pw.println(" mFh=" + mFh); 575 pw.println(" mParentApp=" + mParentApp); 576 pw.println(" recordsLoadedRegistrants: size=" + mRecordsLoadedRegistrants.size()); 577 for (int i = 0; i < mRecordsLoadedRegistrants.size(); i++) { 578 pw.println(" recordsLoadedRegistrants[" + i + "]=" 579 + ((Registrant)mRecordsLoadedRegistrants.get(i)).getHandler()); 580 } 581 pw.println(" mImsiReadyRegistrants: size=" + mImsiReadyRegistrants.size()); 582 for (int i = 0; i < mImsiReadyRegistrants.size(); i++) { 583 pw.println(" mImsiReadyRegistrants[" + i + "]=" 584 + ((Registrant)mImsiReadyRegistrants.get(i)).getHandler()); 585 } 586 pw.println(" mRecordsEventsRegistrants: size=" + mRecordsEventsRegistrants.size()); 587 for (int i = 0; i < mRecordsEventsRegistrants.size(); i++) { 588 pw.println(" mRecordsEventsRegistrants[" + i + "]=" 589 + ((Registrant)mRecordsEventsRegistrants.get(i)).getHandler()); 590 } 591 pw.println(" mNewSmsRegistrants: size=" + mNewSmsRegistrants.size()); 592 for (int i = 0; i < mNewSmsRegistrants.size(); i++) { 593 pw.println(" mNewSmsRegistrants[" + i + "]=" 594 + ((Registrant)mNewSmsRegistrants.get(i)).getHandler()); 595 } 596 pw.println(" mNetworkSelectionModeAutomaticRegistrants: size=" 597 + mNetworkSelectionModeAutomaticRegistrants.size()); 598 for (int i = 0; i < mNetworkSelectionModeAutomaticRegistrants.size(); i++) { 599 pw.println(" mNetworkSelectionModeAutomaticRegistrants[" + i + "]=" 600 + ((Registrant)mNetworkSelectionModeAutomaticRegistrants.get(i)).getHandler()); 601 } 602 pw.println(" mRecordsRequested=" + mRecordsRequested); 603 pw.println(" mRecordsToLoad=" + mRecordsToLoad); 604 pw.println(" mRdnCache=" + mAdnCache); 605 pw.println(" iccid=" + mIccId); 606 pw.println(" mMsisdn=" + mMsisdn); 607 pw.println(" mMsisdnTag=" + mMsisdnTag); 608 pw.println(" mVoiceMailNum=" + mVoiceMailNum); 609 pw.println(" mVoiceMailTag=" + mVoiceMailTag); 610 pw.println(" mNewVoiceMailNum=" + mNewVoiceMailNum); 611 pw.println(" mNewVoiceMailTag=" + mNewVoiceMailTag); 612 pw.println(" mIsVoiceMailFixed=" + mIsVoiceMailFixed); 613 pw.println(" mCountVoiceMessages=" + mCountVoiceMessages); 614 pw.println(" mImsi=" + mImsi); 615 pw.println(" mMncLength=" + mMncLength); 616 pw.println(" mMailboxIndex=" + mMailboxIndex); 617 pw.println(" mSpn=" + mSpn); 618 pw.flush(); 619 } 620} 621