1 /* 2 * Copyright (C) 2018 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 17 package com.android.internal.telephony.euicc; 18 19 import android.annotation.Nullable; 20 import android.app.AppOpsManager; 21 import android.content.BroadcastReceiver; 22 import android.content.Context; 23 import android.content.Intent; 24 import android.content.IntentFilter; 25 import android.content.SharedPreferences; 26 import android.content.pm.ComponentInfo; 27 import android.os.Binder; 28 import android.os.Handler; 29 import android.os.RemoteException; 30 import android.os.ServiceManager; 31 import android.preference.PreferenceManager; 32 import android.provider.Settings; 33 import android.service.euicc.EuiccProfileInfo; 34 import android.telephony.TelephonyManager; 35 import android.telephony.euicc.EuiccCardManager; 36 import android.telephony.euicc.EuiccNotification; 37 import android.telephony.euicc.EuiccRulesAuthTable; 38 import android.text.TextUtils; 39 import android.util.Log; 40 41 import com.android.internal.annotations.VisibleForTesting; 42 import com.android.internal.telephony.SubscriptionController; 43 import com.android.internal.telephony.uicc.UiccController; 44 import com.android.internal.telephony.uicc.UiccSlot; 45 import com.android.internal.telephony.uicc.euicc.EuiccCard; 46 import com.android.internal.telephony.uicc.euicc.EuiccCardErrorException; 47 import com.android.internal.telephony.uicc.euicc.async.AsyncResultCallback; 48 49 import java.io.FileDescriptor; 50 import java.io.PrintWriter; 51 52 /** Backing implementation of {@link EuiccCardManager}. */ 53 public class EuiccCardController extends IEuiccCardController.Stub { 54 private static final String TAG = "EuiccCardController"; 55 private static final String KEY_LAST_BOOT_COUNT = "last_boot_count"; 56 57 private final Context mContext; 58 private AppOpsManager mAppOps; 59 private String mCallingPackage; 60 private ComponentInfo mBestComponent; 61 private Handler mEuiccMainThreadHandler; 62 private SimSlotStatusChangedBroadcastReceiver mSimSlotStatusChangeReceiver; 63 private EuiccController mEuiccController; 64 private UiccController mUiccController; 65 66 private static EuiccCardController sInstance; 67 68 private class SimSlotStatusChangedBroadcastReceiver extends BroadcastReceiver { 69 @Override onReceive(Context context, Intent intent)70 public void onReceive(Context context, Intent intent) { 71 if (TelephonyManager.ACTION_SIM_SLOT_STATUS_CHANGED.equals(intent.getAction())) { 72 // We want to keep listening if card is not present yet since the first state might 73 // be an error state 74 if (!isEmbeddedCardPresent()) { 75 return; 76 } 77 if (isEmbeddedSlotActivated()) { 78 mEuiccController.startOtaUpdatingIfNecessary(); 79 } 80 mContext.unregisterReceiver(mSimSlotStatusChangeReceiver); 81 } 82 } 83 } 84 85 /** Initialize the instance. Should only be called once. */ init(Context context)86 public static EuiccCardController init(Context context) { 87 synchronized (EuiccCardController.class) { 88 if (sInstance == null) { 89 sInstance = new EuiccCardController(context); 90 } else { 91 Log.wtf(TAG, "init() called multiple times! sInstance = " + sInstance); 92 } 93 } 94 return sInstance; 95 } 96 97 /** Get an instance. Assumes one has already been initialized with {@link #init}. */ get()98 public static EuiccCardController get() { 99 if (sInstance == null) { 100 synchronized (EuiccCardController.class) { 101 if (sInstance == null) { 102 throw new IllegalStateException("get() called before init()"); 103 } 104 } 105 } 106 return sInstance; 107 } 108 EuiccCardController(Context context)109 private EuiccCardController(Context context) { 110 this(context, new Handler(), EuiccController.get(), UiccController.getInstance()); 111 ServiceManager.addService("euicc_card_controller", this); 112 } 113 114 @VisibleForTesting(visibility = VisibleForTesting.Visibility.PRIVATE) EuiccCardController( Context context, Handler handler, EuiccController euiccController, UiccController uiccController)115 public EuiccCardController( 116 Context context, 117 Handler handler, 118 EuiccController euiccController, 119 UiccController uiccController) { 120 mContext = context; 121 mAppOps = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE); 122 123 mEuiccMainThreadHandler = handler; 124 mUiccController = uiccController; 125 mEuiccController = euiccController; 126 127 if (isBootUp(mContext)) { 128 mSimSlotStatusChangeReceiver = new SimSlotStatusChangedBroadcastReceiver(); 129 mContext.registerReceiver( 130 mSimSlotStatusChangeReceiver, 131 new IntentFilter(TelephonyManager.ACTION_SIM_SLOT_STATUS_CHANGED)); 132 } 133 } 134 135 /** 136 * Check whether the restored boot count is the same as current one. If not, update the restored 137 * one. 138 */ 139 @VisibleForTesting(visibility = VisibleForTesting.Visibility.PRIVATE) isBootUp(Context context)140 public static boolean isBootUp(Context context) { 141 int bootCount = Settings.Global.getInt( 142 context.getContentResolver(), Settings.Global.BOOT_COUNT, -1); 143 SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context); 144 int lastBootCount = sp.getInt(KEY_LAST_BOOT_COUNT, -1); 145 if (bootCount == -1 || lastBootCount == -1 || bootCount != lastBootCount) { 146 sp.edit().putInt(KEY_LAST_BOOT_COUNT, bootCount).apply(); 147 return true; 148 } 149 return false; 150 } 151 152 /** Whether embedded slot is activated or not. */ 153 @VisibleForTesting(visibility = VisibleForTesting.Visibility.PRIVATE) isEmbeddedSlotActivated()154 public boolean isEmbeddedSlotActivated() { 155 UiccSlot[] slots = mUiccController.getUiccSlots(); 156 if (slots == null) { 157 return false; 158 } 159 for (int i = 0; i < slots.length; ++i) { 160 UiccSlot slotInfo = slots[i]; 161 if (slotInfo != null && !slotInfo.isRemovable() && slotInfo.isActive()) { 162 return true; 163 } 164 } 165 return false; 166 } 167 168 /** Whether embedded card is present or not */ 169 @VisibleForTesting(visibility = VisibleForTesting.Visibility.PRIVATE) isEmbeddedCardPresent()170 public boolean isEmbeddedCardPresent() { 171 UiccSlot[] slots = mUiccController.getUiccSlots(); 172 if (slots == null) { 173 return false; 174 } 175 for (UiccSlot slotInfo : slots) { 176 if (slotInfo != null 177 && !slotInfo.isRemovable() 178 && slotInfo.getCardState() != null 179 && slotInfo.getCardState().isCardPresent()) { 180 return true; 181 } 182 } 183 return false; 184 } 185 checkCallingPackage(String callingPackage)186 private void checkCallingPackage(String callingPackage) { 187 // Check the caller is LPA. 188 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage); 189 mCallingPackage = callingPackage; 190 mBestComponent = EuiccConnector.findBestComponent(mContext.getPackageManager()); 191 if (mBestComponent == null 192 || !TextUtils.equals(mCallingPackage, mBestComponent.packageName)) { 193 throw new SecurityException("The calling package can only be LPA."); 194 } 195 } 196 getEuiccCard(String cardId)197 private EuiccCard getEuiccCard(String cardId) { 198 UiccController controller = UiccController.getInstance(); 199 int slotId = controller.getUiccSlotForCardId(cardId); 200 if (slotId != UiccController.INVALID_SLOT_ID) { 201 UiccSlot slot = controller.getUiccSlot(slotId); 202 if (slot.isEuicc()) { 203 return (EuiccCard) controller.getUiccCardForSlot(slotId); 204 } 205 } 206 loge("EuiccCard is null. CardId : " + cardId); 207 return null; 208 } 209 getResultCode(Throwable e)210 private int getResultCode(Throwable e) { 211 if (e instanceof EuiccCardErrorException) { 212 return ((EuiccCardErrorException) e).getErrorCode(); 213 } 214 return EuiccCardManager.RESULT_UNKNOWN_ERROR; 215 } 216 217 @Override getAllProfiles(String callingPackage, String cardId, IGetAllProfilesCallback callback)218 public void getAllProfiles(String callingPackage, String cardId, 219 IGetAllProfilesCallback callback) { 220 try { 221 checkCallingPackage(callingPackage); 222 } catch (SecurityException se) { 223 try { 224 callback.onComplete(EuiccCardManager.RESULT_CALLER_NOT_ALLOWED, null); 225 } catch (RemoteException re) { 226 loge("callback onComplete failure after checkCallingPackage.", re); 227 } 228 return; 229 } 230 231 EuiccCard card = getEuiccCard(cardId); 232 if (card == null) { 233 try { 234 callback.onComplete(EuiccCardManager.RESULT_EUICC_NOT_FOUND, null); 235 } catch (RemoteException exception) { 236 loge("getAllProfiles callback failure.", exception); 237 } 238 return; 239 } 240 241 AsyncResultCallback<EuiccProfileInfo[]> cardCb = 242 new AsyncResultCallback<EuiccProfileInfo[]>() { 243 @Override 244 public void onResult(EuiccProfileInfo[] result) { 245 try { 246 callback.onComplete(EuiccCardManager.RESULT_OK, result); 247 } catch (RemoteException exception) { 248 loge("getAllProfiles callback failure.", exception); 249 } 250 } 251 252 @Override 253 public void onException(Throwable e) { 254 try { 255 loge("getAllProfiles callback onException: ", e); 256 callback.onComplete(getResultCode(e), null); 257 } catch (RemoteException exception) { 258 loge("getAllProfiles callback failure.", exception); 259 } 260 } 261 }; 262 263 card.getAllProfiles(cardCb, mEuiccMainThreadHandler); 264 } 265 266 @Override getProfile(String callingPackage, String cardId, String iccid, IGetProfileCallback callback)267 public void getProfile(String callingPackage, String cardId, String iccid, 268 IGetProfileCallback callback) { 269 try { 270 checkCallingPackage(callingPackage); 271 } catch (SecurityException se) { 272 try { 273 callback.onComplete(EuiccCardManager.RESULT_CALLER_NOT_ALLOWED, null); 274 } catch (RemoteException re) { 275 loge("callback onComplete failure after checkCallingPackage.", re); 276 } 277 return; 278 } 279 280 EuiccCard card = getEuiccCard(cardId); 281 if (card == null) { 282 try { 283 callback.onComplete(EuiccCardManager.RESULT_EUICC_NOT_FOUND, null); 284 } catch (RemoteException exception) { 285 loge("getProfile callback failure.", exception); 286 } 287 return; 288 } 289 290 AsyncResultCallback<EuiccProfileInfo> cardCb = new AsyncResultCallback<EuiccProfileInfo>() { 291 @Override 292 public void onResult(EuiccProfileInfo result) { 293 try { 294 callback.onComplete(EuiccCardManager.RESULT_OK, result); 295 } catch (RemoteException exception) { 296 loge("getProfile callback failure.", exception); 297 } 298 } 299 300 @Override 301 public void onException(Throwable e) { 302 try { 303 loge("getProfile callback onException: ", e); 304 callback.onComplete(getResultCode(e), null); 305 } catch (RemoteException exception) { 306 loge("getProfile callback failure.", exception); 307 } 308 } 309 }; 310 311 card.getProfile(iccid, cardCb, mEuiccMainThreadHandler); 312 } 313 314 @Override disableProfile(String callingPackage, String cardId, String iccid, boolean refresh, IDisableProfileCallback callback)315 public void disableProfile(String callingPackage, String cardId, String iccid, boolean refresh, 316 IDisableProfileCallback callback) { 317 try { 318 checkCallingPackage(callingPackage); 319 } catch (SecurityException se) { 320 try { 321 callback.onComplete(EuiccCardManager.RESULT_CALLER_NOT_ALLOWED); 322 } catch (RemoteException re) { 323 loge("callback onComplete failure after checkCallingPackage.", re); 324 } 325 return; 326 } 327 328 EuiccCard card = getEuiccCard(cardId); 329 if (card == null) { 330 try { 331 callback.onComplete(EuiccCardManager.RESULT_EUICC_NOT_FOUND); 332 } catch (RemoteException exception) { 333 loge("disableProfile callback failure.", exception); 334 } 335 return; 336 } 337 338 AsyncResultCallback<Void> cardCb = new AsyncResultCallback<Void>() { 339 @Override 340 public void onResult(Void result) { 341 try { 342 callback.onComplete(EuiccCardManager.RESULT_OK); 343 } catch (RemoteException exception) { 344 loge("disableProfile callback failure.", exception); 345 } 346 } 347 348 @Override 349 public void onException(Throwable e) { 350 try { 351 loge("disableProfile callback onException: ", e); 352 callback.onComplete(getResultCode(e)); 353 } catch (RemoteException exception) { 354 loge("disableProfile callback failure.", exception); 355 } 356 } 357 }; 358 359 card.disableProfile(iccid, refresh, cardCb, mEuiccMainThreadHandler); 360 } 361 362 @Override switchToProfile(String callingPackage, String cardId, String iccid, boolean refresh, ISwitchToProfileCallback callback)363 public void switchToProfile(String callingPackage, String cardId, String iccid, boolean refresh, 364 ISwitchToProfileCallback callback) { 365 try { 366 checkCallingPackage(callingPackage); 367 } catch (SecurityException se) { 368 try { 369 callback.onComplete(EuiccCardManager.RESULT_CALLER_NOT_ALLOWED, null); 370 } catch (RemoteException re) { 371 loge("callback onComplete failure after checkCallingPackage.", re); 372 } 373 return; 374 } 375 376 EuiccCard card = getEuiccCard(cardId); 377 if (card == null) { 378 try { 379 callback.onComplete(EuiccCardManager.RESULT_EUICC_NOT_FOUND, null); 380 } catch (RemoteException exception) { 381 loge("switchToProfile callback failure.", exception); 382 } 383 return; 384 } 385 386 AsyncResultCallback<EuiccProfileInfo> profileCb = 387 new AsyncResultCallback<EuiccProfileInfo>() { 388 @Override 389 public void onResult(EuiccProfileInfo profile) { 390 AsyncResultCallback<Void> switchCb = new AsyncResultCallback<Void>() { 391 @Override 392 public void onResult(Void result) { 393 try { 394 callback.onComplete(EuiccCardManager.RESULT_OK, profile); 395 } catch (RemoteException exception) { 396 loge("switchToProfile callback failure.", exception); 397 } 398 } 399 400 @Override 401 public void onException(Throwable e) { 402 try { 403 loge("switchToProfile callback onException: ", e); 404 callback.onComplete(getResultCode(e), profile); 405 } catch (RemoteException exception) { 406 loge("switchToProfile callback failure.", exception); 407 } 408 } 409 }; 410 411 card.switchToProfile(iccid, refresh, switchCb, mEuiccMainThreadHandler); 412 } 413 414 @Override 415 public void onException(Throwable e) { 416 try { 417 loge("getProfile in switchToProfile callback onException: ", e); 418 callback.onComplete(getResultCode(e), null); 419 } catch (RemoteException exception) { 420 loge("switchToProfile callback failure.", exception); 421 } 422 } 423 }; 424 425 card.getProfile(iccid, profileCb, mEuiccMainThreadHandler); 426 } 427 428 @Override setNickname(String callingPackage, String cardId, String iccid, String nickname, ISetNicknameCallback callback)429 public void setNickname(String callingPackage, String cardId, String iccid, String nickname, 430 ISetNicknameCallback callback) { 431 try { 432 checkCallingPackage(callingPackage); 433 } catch (SecurityException se) { 434 try { 435 callback.onComplete(EuiccCardManager.RESULT_CALLER_NOT_ALLOWED); 436 } catch (RemoteException re) { 437 loge("callback onComplete failure after checkCallingPackage.", re); 438 } 439 return; 440 } 441 442 EuiccCard card = getEuiccCard(cardId); 443 if (card == null) { 444 try { 445 callback.onComplete(EuiccCardManager.RESULT_EUICC_NOT_FOUND); 446 } catch (RemoteException exception) { 447 loge("setNickname callback failure.", exception); 448 } 449 return; 450 } 451 452 AsyncResultCallback<Void> cardCb = new AsyncResultCallback<Void>() { 453 @Override 454 public void onResult(Void result) { 455 try { 456 callback.onComplete(EuiccCardManager.RESULT_OK); 457 } catch (RemoteException exception) { 458 loge("setNickname callback failure.", exception); 459 } 460 } 461 462 @Override 463 public void onException(Throwable e) { 464 try { 465 loge("setNickname callback onException: ", e); 466 callback.onComplete(getResultCode(e)); 467 } catch (RemoteException exception) { 468 loge("setNickname callback failure.", exception); 469 } 470 } 471 }; 472 473 card.setNickname(iccid, nickname, cardCb, mEuiccMainThreadHandler); 474 } 475 476 @Override deleteProfile(String callingPackage, String cardId, String iccid, IDeleteProfileCallback callback)477 public void deleteProfile(String callingPackage, String cardId, String iccid, 478 IDeleteProfileCallback callback) { 479 try { 480 checkCallingPackage(callingPackage); 481 } catch (SecurityException se) { 482 try { 483 callback.onComplete(EuiccCardManager.RESULT_CALLER_NOT_ALLOWED); 484 } catch (RemoteException re) { 485 loge("callback onComplete failure after checkCallingPackage.", re); 486 } 487 return; 488 } 489 490 EuiccCard card = getEuiccCard(cardId); 491 if (card == null) { 492 try { 493 callback.onComplete(EuiccCardManager.RESULT_EUICC_NOT_FOUND); 494 } catch (RemoteException exception) { 495 loge("deleteProfile callback failure.", exception); 496 } 497 return; 498 } 499 500 AsyncResultCallback<Void> cardCb = new AsyncResultCallback<Void>() { 501 @Override 502 public void onResult(Void result) { 503 Log.i(TAG, "Request subscription info list refresh after delete."); 504 SubscriptionController.getInstance() 505 .requestEmbeddedSubscriptionInfoListRefresh( 506 mUiccController.convertToPublicCardId(cardId)); 507 try { 508 callback.onComplete(EuiccCardManager.RESULT_OK); 509 } catch (RemoteException exception) { 510 loge("deleteProfile callback failure.", exception); 511 } 512 }; 513 514 @Override 515 public void onException(Throwable e) { 516 try { 517 loge("deleteProfile callback onException: ", e); 518 callback.onComplete(getResultCode(e)); 519 } catch (RemoteException exception) { 520 loge("deleteProfile callback failure.", exception); 521 } 522 } 523 }; 524 525 card.deleteProfile(iccid, cardCb, mEuiccMainThreadHandler); 526 } 527 528 @Override resetMemory(String callingPackage, String cardId, @EuiccCardManager.ResetOption int options, IResetMemoryCallback callback)529 public void resetMemory(String callingPackage, String cardId, 530 @EuiccCardManager.ResetOption int options, IResetMemoryCallback callback) { 531 try { 532 checkCallingPackage(callingPackage); 533 } catch (SecurityException se) { 534 try { 535 callback.onComplete(EuiccCardManager.RESULT_CALLER_NOT_ALLOWED); 536 } catch (RemoteException re) { 537 loge("callback onComplete failure after checkCallingPackage.", re); 538 } 539 return; 540 } 541 542 EuiccCard card = getEuiccCard(cardId); 543 if (card == null) { 544 try { 545 callback.onComplete(EuiccCardManager.RESULT_EUICC_NOT_FOUND); 546 } catch (RemoteException exception) { 547 loge("resetMemory callback failure.", exception); 548 } 549 return; 550 } 551 552 AsyncResultCallback<Void> cardCb = new AsyncResultCallback<Void>() { 553 @Override 554 public void onResult(Void result) { 555 Log.i(TAG, "Request subscription info list refresh after reset memory."); 556 SubscriptionController.getInstance() 557 .requestEmbeddedSubscriptionInfoListRefresh( 558 mUiccController.convertToPublicCardId(cardId)); 559 try { 560 callback.onComplete(EuiccCardManager.RESULT_OK); 561 } catch (RemoteException exception) { 562 loge("resetMemory callback failure.", exception); 563 } 564 } 565 566 @Override 567 public void onException(Throwable e) { 568 try { 569 loge("resetMemory callback onException: ", e); 570 callback.onComplete(getResultCode(e)); 571 } catch (RemoteException exception) { 572 loge("resetMemory callback failure.", exception); 573 } 574 } 575 }; 576 577 card.resetMemory(options, cardCb, mEuiccMainThreadHandler); 578 } 579 580 @Override getDefaultSmdpAddress(String callingPackage, String cardId, IGetDefaultSmdpAddressCallback callback)581 public void getDefaultSmdpAddress(String callingPackage, String cardId, 582 IGetDefaultSmdpAddressCallback callback) { 583 try { 584 checkCallingPackage(callingPackage); 585 } catch (SecurityException se) { 586 try { 587 callback.onComplete(EuiccCardManager.RESULT_CALLER_NOT_ALLOWED, null); 588 } catch (RemoteException re) { 589 loge("callback onComplete failure after checkCallingPackage.", re); 590 } 591 return; 592 } 593 594 EuiccCard card = getEuiccCard(cardId); 595 if (card == null) { 596 try { 597 callback.onComplete(EuiccCardManager.RESULT_EUICC_NOT_FOUND, null); 598 } catch (RemoteException exception) { 599 loge("getDefaultSmdpAddress callback failure.", exception); 600 } 601 return; 602 } 603 604 AsyncResultCallback<String> cardCb = new AsyncResultCallback<String>() { 605 @Override 606 public void onResult(String result) { 607 try { 608 callback.onComplete(EuiccCardManager.RESULT_OK, result); 609 } catch (RemoteException exception) { 610 loge("getDefaultSmdpAddress callback failure.", exception); 611 } 612 } 613 614 @Override 615 public void onException(Throwable e) { 616 try { 617 loge("getDefaultSmdpAddress callback onException: ", e); 618 callback.onComplete(getResultCode(e), null); 619 } catch (RemoteException exception) { 620 loge("getDefaultSmdpAddress callback failure.", exception); 621 } 622 } 623 }; 624 625 card.getDefaultSmdpAddress(cardCb, mEuiccMainThreadHandler); 626 } 627 628 @Override getSmdsAddress(String callingPackage, String cardId, IGetSmdsAddressCallback callback)629 public void getSmdsAddress(String callingPackage, String cardId, 630 IGetSmdsAddressCallback callback) { 631 try { 632 checkCallingPackage(callingPackage); 633 } catch (SecurityException se) { 634 try { 635 callback.onComplete(EuiccCardManager.RESULT_CALLER_NOT_ALLOWED, null); 636 } catch (RemoteException re) { 637 loge("callback onComplete failure after checkCallingPackage.", re); 638 } 639 return; 640 } 641 642 EuiccCard card = getEuiccCard(cardId); 643 if (card == null) { 644 try { 645 callback.onComplete(EuiccCardManager.RESULT_EUICC_NOT_FOUND, null); 646 } catch (RemoteException exception) { 647 loge("getSmdsAddress callback failure.", exception); 648 } 649 return; 650 } 651 652 AsyncResultCallback<String> cardCb = new AsyncResultCallback<String>() { 653 @Override 654 public void onResult(String result) { 655 try { 656 callback.onComplete(EuiccCardManager.RESULT_OK, result); 657 } catch (RemoteException exception) { 658 loge("getSmdsAddress callback failure.", exception); 659 } 660 } 661 662 @Override 663 public void onException(Throwable e) { 664 try { 665 loge("getSmdsAddress callback onException: ", e); 666 callback.onComplete(getResultCode(e), null); 667 } catch (RemoteException exception) { 668 loge("getSmdsAddress callback failure.", exception); 669 } 670 } 671 }; 672 673 card.getSmdsAddress(cardCb, mEuiccMainThreadHandler); 674 } 675 676 @Override setDefaultSmdpAddress(String callingPackage, String cardId, String address, ISetDefaultSmdpAddressCallback callback)677 public void setDefaultSmdpAddress(String callingPackage, String cardId, String address, 678 ISetDefaultSmdpAddressCallback callback) { 679 try { 680 checkCallingPackage(callingPackage); 681 } catch (SecurityException se) { 682 try { 683 callback.onComplete(EuiccCardManager.RESULT_CALLER_NOT_ALLOWED); 684 } catch (RemoteException re) { 685 loge("callback onComplete failure after checkCallingPackage.", re); 686 } 687 return; 688 } 689 690 EuiccCard card = getEuiccCard(cardId); 691 if (card == null) { 692 try { 693 callback.onComplete(EuiccCardManager.RESULT_EUICC_NOT_FOUND); 694 } catch (RemoteException exception) { 695 loge("setDefaultSmdpAddress callback failure.", exception); 696 } 697 return; 698 } 699 700 AsyncResultCallback<Void> cardCb = new AsyncResultCallback<Void>() { 701 @Override 702 public void onResult(Void result) { 703 try { 704 callback.onComplete(EuiccCardManager.RESULT_OK); 705 } catch (RemoteException exception) { 706 loge("setDefaultSmdpAddress callback failure.", exception); 707 } 708 } 709 710 @Override 711 public void onException(Throwable e) { 712 try { 713 loge("setDefaultSmdpAddress callback onException: ", e); 714 callback.onComplete(getResultCode(e)); 715 } catch (RemoteException exception) { 716 loge("setDefaultSmdpAddress callback failure.", exception); 717 } 718 } 719 }; 720 721 card.setDefaultSmdpAddress(address, cardCb, mEuiccMainThreadHandler); 722 } 723 724 @Override getRulesAuthTable(String callingPackage, String cardId, IGetRulesAuthTableCallback callback)725 public void getRulesAuthTable(String callingPackage, String cardId, 726 IGetRulesAuthTableCallback callback) { 727 try { 728 checkCallingPackage(callingPackage); 729 } catch (SecurityException se) { 730 try { 731 callback.onComplete(EuiccCardManager.RESULT_CALLER_NOT_ALLOWED, null); 732 } catch (RemoteException re) { 733 loge("callback onComplete failure after checkCallingPackage.", re); 734 } 735 return; 736 } 737 738 EuiccCard card = getEuiccCard(cardId); 739 if (card == null) { 740 try { 741 callback.onComplete(EuiccCardManager.RESULT_EUICC_NOT_FOUND, null); 742 } catch (RemoteException exception) { 743 loge("getRulesAuthTable callback failure.", exception); 744 } 745 return; 746 } 747 748 AsyncResultCallback<EuiccRulesAuthTable> cardCb = 749 new AsyncResultCallback<EuiccRulesAuthTable>() { 750 @Override 751 public void onResult(EuiccRulesAuthTable result) { 752 try { 753 callback.onComplete(EuiccCardManager.RESULT_OK, result); 754 } catch (RemoteException exception) { 755 loge("getRulesAuthTable callback failure.", exception); 756 } 757 } 758 759 @Override 760 public void onException(Throwable e) { 761 try { 762 loge("getRulesAuthTable callback onException: ", e); 763 callback.onComplete(getResultCode(e), null); 764 } catch (RemoteException exception) { 765 loge("getRulesAuthTable callback failure.", exception); 766 } 767 } 768 }; 769 770 card.getRulesAuthTable(cardCb, mEuiccMainThreadHandler); 771 } 772 773 @Override getEuiccChallenge(String callingPackage, String cardId, IGetEuiccChallengeCallback callback)774 public void getEuiccChallenge(String callingPackage, String cardId, 775 IGetEuiccChallengeCallback callback) { 776 try { 777 checkCallingPackage(callingPackage); 778 } catch (SecurityException se) { 779 try { 780 callback.onComplete(EuiccCardManager.RESULT_CALLER_NOT_ALLOWED, null); 781 } catch (RemoteException re) { 782 loge("callback onComplete failure after checkCallingPackage.", re); 783 } 784 return; 785 } 786 787 EuiccCard card = getEuiccCard(cardId); 788 if (card == null) { 789 try { 790 callback.onComplete(EuiccCardManager.RESULT_EUICC_NOT_FOUND, null); 791 } catch (RemoteException exception) { 792 loge("getEuiccChallenge callback failure.", exception); 793 } 794 return; 795 } 796 797 AsyncResultCallback<byte[]> cardCb = new AsyncResultCallback<byte[]>() { 798 @Override 799 public void onResult(byte[] result) { 800 try { 801 callback.onComplete(EuiccCardManager.RESULT_OK, result); 802 } catch (RemoteException exception) { 803 loge("getEuiccChallenge callback failure.", exception); 804 } 805 } 806 807 @Override 808 public void onException(Throwable e) { 809 try { 810 loge("getEuiccChallenge callback onException: ", e); 811 callback.onComplete(getResultCode(e), null); 812 } catch (RemoteException exception) { 813 loge("getEuiccChallenge callback failure.", exception); 814 } 815 } 816 }; 817 818 card.getEuiccChallenge(cardCb, mEuiccMainThreadHandler); 819 } 820 821 @Override getEuiccInfo1(String callingPackage, String cardId, IGetEuiccInfo1Callback callback)822 public void getEuiccInfo1(String callingPackage, String cardId, 823 IGetEuiccInfo1Callback callback) { 824 try { 825 checkCallingPackage(callingPackage); 826 } catch (SecurityException se) { 827 try { 828 callback.onComplete(EuiccCardManager.RESULT_CALLER_NOT_ALLOWED, null); 829 } catch (RemoteException re) { 830 loge("callback onComplete failure after checkCallingPackage.", re); 831 } 832 return; 833 } 834 835 EuiccCard card = getEuiccCard(cardId); 836 if (card == null) { 837 try { 838 callback.onComplete(EuiccCardManager.RESULT_EUICC_NOT_FOUND, null); 839 } catch (RemoteException exception) { 840 loge("getEuiccInfo1 callback failure.", exception); 841 } 842 return; 843 } 844 845 AsyncResultCallback<byte[]> cardCb = new AsyncResultCallback<byte[]>() { 846 @Override 847 public void onResult(byte[] result) { 848 try { 849 callback.onComplete(EuiccCardManager.RESULT_OK, result); 850 } catch (RemoteException exception) { 851 loge("getEuiccInfo1 callback failure.", exception); 852 } 853 } 854 855 @Override 856 public void onException(Throwable e) { 857 try { 858 loge("getEuiccInfo1 callback onException: ", e); 859 callback.onComplete(getResultCode(e), null); 860 } catch (RemoteException exception) { 861 loge("getEuiccInfo1 callback failure.", exception); 862 } 863 } 864 }; 865 866 card.getEuiccInfo1(cardCb, mEuiccMainThreadHandler); 867 } 868 869 @Override getEuiccInfo2(String callingPackage, String cardId, IGetEuiccInfo2Callback callback)870 public void getEuiccInfo2(String callingPackage, String cardId, 871 IGetEuiccInfo2Callback callback) { 872 try { 873 checkCallingPackage(callingPackage); 874 } catch (SecurityException se) { 875 try { 876 callback.onComplete(EuiccCardManager.RESULT_CALLER_NOT_ALLOWED, null); 877 } catch (RemoteException re) { 878 loge("callback onComplete failure after checkCallingPackage.", re); 879 } 880 return; 881 } 882 883 EuiccCard card = getEuiccCard(cardId); 884 if (card == null) { 885 try { 886 callback.onComplete(EuiccCardManager.RESULT_EUICC_NOT_FOUND, null); 887 } catch (RemoteException exception) { 888 loge("getEuiccInfo2 callback failure.", exception); 889 } 890 return; 891 } 892 893 AsyncResultCallback<byte[]> cardCb = new AsyncResultCallback<byte[]>() { 894 @Override 895 public void onResult(byte[] result) { 896 try { 897 callback.onComplete(EuiccCardManager.RESULT_OK, result); 898 } catch (RemoteException exception) { 899 loge("getEuiccInfo2 callback failure.", exception); 900 } 901 } 902 903 @Override 904 public void onException(Throwable e) { 905 try { 906 loge("getEuiccInfo2 callback onException: ", e); 907 callback.onComplete(getResultCode(e), null); 908 } catch (RemoteException exception) { 909 loge("getEuiccInfo2 callback failure.", exception); 910 } 911 } 912 }; 913 914 card.getEuiccInfo2(cardCb, mEuiccMainThreadHandler); 915 } 916 917 @Override authenticateServer(String callingPackage, String cardId, String matchingId, byte[] serverSigned1, byte[] serverSignature1, byte[] euiccCiPkIdToBeUsed, byte[] serverCertificate, IAuthenticateServerCallback callback)918 public void authenticateServer(String callingPackage, String cardId, String matchingId, 919 byte[] serverSigned1, byte[] serverSignature1, byte[] euiccCiPkIdToBeUsed, 920 byte[] serverCertificate, IAuthenticateServerCallback callback) { 921 try { 922 checkCallingPackage(callingPackage); 923 } catch (SecurityException se) { 924 try { 925 callback.onComplete(EuiccCardManager.RESULT_CALLER_NOT_ALLOWED, null); 926 } catch (RemoteException re) { 927 loge("callback onComplete failure after checkCallingPackage.", re); 928 } 929 return; 930 } 931 932 EuiccCard card = getEuiccCard(cardId); 933 if (card == null) { 934 try { 935 callback.onComplete(EuiccCardManager.RESULT_EUICC_NOT_FOUND, null); 936 } catch (RemoteException exception) { 937 loge("authenticateServer callback failure.", exception); 938 } 939 return; 940 } 941 942 AsyncResultCallback<byte[]> cardCb = new AsyncResultCallback<byte[]>() { 943 @Override 944 public void onResult(byte[] result) { 945 try { 946 callback.onComplete(EuiccCardManager.RESULT_OK, result); 947 } catch (RemoteException exception) { 948 loge("authenticateServer callback failure.", exception); 949 } 950 } 951 952 @Override 953 public void onException(Throwable e) { 954 try { 955 loge("authenticateServer callback onException: ", e); 956 callback.onComplete(getResultCode(e), null); 957 } catch (RemoteException exception) { 958 loge("authenticateServer callback failure.", exception); 959 } 960 } 961 }; 962 963 card.authenticateServer(matchingId, serverSigned1, serverSignature1, euiccCiPkIdToBeUsed, 964 serverCertificate, cardCb, mEuiccMainThreadHandler); 965 } 966 967 @Override prepareDownload(String callingPackage, String cardId, @Nullable byte[] hashCc, byte[] smdpSigned2, byte[] smdpSignature2, byte[] smdpCertificate, IPrepareDownloadCallback callback)968 public void prepareDownload(String callingPackage, String cardId, @Nullable byte[] hashCc, 969 byte[] smdpSigned2, byte[] smdpSignature2, byte[] smdpCertificate, 970 IPrepareDownloadCallback callback) { 971 try { 972 checkCallingPackage(callingPackage); 973 } catch (SecurityException se) { 974 try { 975 callback.onComplete(EuiccCardManager.RESULT_CALLER_NOT_ALLOWED, null); 976 } catch (RemoteException re) { 977 loge("callback onComplete failure after checkCallingPackage.", re); 978 } 979 return; 980 } 981 982 EuiccCard card = getEuiccCard(cardId); 983 if (card == null) { 984 try { 985 callback.onComplete(EuiccCardManager.RESULT_EUICC_NOT_FOUND, null); 986 } catch (RemoteException exception) { 987 loge("prepareDownload callback failure.", exception); 988 } 989 return; 990 } 991 992 AsyncResultCallback<byte[]> cardCb = new AsyncResultCallback<byte[]>() { 993 @Override 994 public void onResult(byte[] result) { 995 try { 996 callback.onComplete(EuiccCardManager.RESULT_OK, result); 997 } catch (RemoteException exception) { 998 loge("prepareDownload callback failure.", exception); 999 } 1000 } 1001 1002 @Override 1003 public void onException(Throwable e) { 1004 try { 1005 loge("prepareDownload callback onException: ", e); 1006 callback.onComplete(getResultCode(e), null); 1007 } catch (RemoteException exception) { 1008 loge("prepareDownload callback failure.", exception); 1009 } 1010 } 1011 }; 1012 1013 card.prepareDownload(hashCc, smdpSigned2, smdpSignature2, smdpCertificate, cardCb, 1014 mEuiccMainThreadHandler); 1015 } 1016 1017 @Override loadBoundProfilePackage(String callingPackage, String cardId, byte[] boundProfilePackage, ILoadBoundProfilePackageCallback callback)1018 public void loadBoundProfilePackage(String callingPackage, String cardId, 1019 byte[] boundProfilePackage, ILoadBoundProfilePackageCallback callback) { 1020 try { 1021 checkCallingPackage(callingPackage); 1022 } catch (SecurityException se) { 1023 try { 1024 callback.onComplete(EuiccCardManager.RESULT_CALLER_NOT_ALLOWED, null); 1025 } catch (RemoteException re) { 1026 loge("callback onComplete failure after checkCallingPackage.", re); 1027 } 1028 return; 1029 } 1030 1031 EuiccCard card = getEuiccCard(cardId); 1032 if (card == null) { 1033 try { 1034 callback.onComplete(EuiccCardManager.RESULT_EUICC_NOT_FOUND, null); 1035 } catch (RemoteException exception) { 1036 loge("loadBoundProfilePackage callback failure.", exception); 1037 } 1038 return; 1039 } 1040 1041 AsyncResultCallback<byte[]> cardCb = new AsyncResultCallback<byte[]>() { 1042 @Override 1043 public void onResult(byte[] result) { 1044 Log.i(TAG, "Request subscription info list refresh after install."); 1045 SubscriptionController.getInstance() 1046 .requestEmbeddedSubscriptionInfoListRefresh( 1047 mUiccController.convertToPublicCardId(cardId)); 1048 try { 1049 callback.onComplete(EuiccCardManager.RESULT_OK, result); 1050 } catch (RemoteException exception) { 1051 loge("loadBoundProfilePackage callback failure.", exception); 1052 } 1053 } 1054 1055 @Override 1056 public void onException(Throwable e) { 1057 try { 1058 loge("loadBoundProfilePackage callback onException: ", e); 1059 callback.onComplete(getResultCode(e), null); 1060 } catch (RemoteException exception) { 1061 loge("loadBoundProfilePackage callback failure.", exception); 1062 } 1063 } 1064 }; 1065 1066 card.loadBoundProfilePackage(boundProfilePackage, cardCb, mEuiccMainThreadHandler); 1067 } 1068 1069 @Override cancelSession(String callingPackage, String cardId, byte[] transactionId, @EuiccCardManager.CancelReason int reason, ICancelSessionCallback callback)1070 public void cancelSession(String callingPackage, String cardId, byte[] transactionId, 1071 @EuiccCardManager.CancelReason int reason, ICancelSessionCallback callback) { 1072 try { 1073 checkCallingPackage(callingPackage); 1074 } catch (SecurityException se) { 1075 try { 1076 callback.onComplete(EuiccCardManager.RESULT_CALLER_NOT_ALLOWED, null); 1077 } catch (RemoteException re) { 1078 loge("callback onComplete failure after checkCallingPackage.", re); 1079 } 1080 return; 1081 } 1082 1083 EuiccCard card = getEuiccCard(cardId); 1084 if (card == null) { 1085 try { 1086 callback.onComplete(EuiccCardManager.RESULT_EUICC_NOT_FOUND, null); 1087 } catch (RemoteException exception) { 1088 loge("cancelSession callback failure.", exception); 1089 } 1090 return; 1091 } 1092 1093 AsyncResultCallback<byte[]> cardCb = new AsyncResultCallback<byte[]>() { 1094 @Override 1095 public void onResult(byte[] result) { 1096 try { 1097 callback.onComplete(EuiccCardManager.RESULT_OK, result); 1098 } catch (RemoteException exception) { 1099 loge("cancelSession callback failure.", exception); 1100 } 1101 } 1102 1103 @Override 1104 public void onException(Throwable e) { 1105 try { 1106 loge("cancelSession callback onException: ", e); 1107 callback.onComplete(getResultCode(e), null); 1108 } catch (RemoteException exception) { 1109 loge("cancelSession callback failure.", exception); 1110 } 1111 } 1112 }; 1113 1114 card.cancelSession(transactionId, reason, cardCb, mEuiccMainThreadHandler); 1115 } 1116 1117 @Override listNotifications(String callingPackage, String cardId, @EuiccNotification.Event int events, IListNotificationsCallback callback)1118 public void listNotifications(String callingPackage, String cardId, 1119 @EuiccNotification.Event int events, IListNotificationsCallback callback) { 1120 try { 1121 checkCallingPackage(callingPackage); 1122 } catch (SecurityException se) { 1123 try { 1124 callback.onComplete(EuiccCardManager.RESULT_CALLER_NOT_ALLOWED, null); 1125 } catch (RemoteException re) { 1126 loge("callback onComplete failure after checkCallingPackage.", re); 1127 } 1128 return; 1129 } 1130 1131 EuiccCard card = getEuiccCard(cardId); 1132 if (card == null) { 1133 try { 1134 callback.onComplete(EuiccCardManager.RESULT_EUICC_NOT_FOUND, null); 1135 } catch (RemoteException exception) { 1136 loge("listNotifications callback failure.", exception); 1137 } 1138 return; 1139 } 1140 1141 AsyncResultCallback<EuiccNotification[]> cardCb = 1142 new AsyncResultCallback<EuiccNotification[]>() { 1143 @Override 1144 public void onResult(EuiccNotification[] result) { 1145 try { 1146 callback.onComplete(EuiccCardManager.RESULT_OK, result); 1147 } catch (RemoteException exception) { 1148 loge("listNotifications callback failure.", exception); 1149 } 1150 } 1151 1152 @Override 1153 public void onException(Throwable e) { 1154 try { 1155 loge("listNotifications callback onException: ", e); 1156 callback.onComplete(getResultCode(e), null); 1157 } catch (RemoteException exception) { 1158 loge("listNotifications callback failure.", exception); 1159 } 1160 } 1161 }; 1162 1163 card.listNotifications(events, cardCb, mEuiccMainThreadHandler); 1164 } 1165 1166 @Override retrieveNotificationList(String callingPackage, String cardId, @EuiccNotification.Event int events, IRetrieveNotificationListCallback callback)1167 public void retrieveNotificationList(String callingPackage, String cardId, 1168 @EuiccNotification.Event int events, IRetrieveNotificationListCallback callback) { 1169 try { 1170 checkCallingPackage(callingPackage); 1171 } catch (SecurityException se) { 1172 try { 1173 callback.onComplete(EuiccCardManager.RESULT_CALLER_NOT_ALLOWED, null); 1174 } catch (RemoteException re) { 1175 loge("callback onComplete failure after checkCallingPackage.", re); 1176 } 1177 return; 1178 } 1179 1180 EuiccCard card = getEuiccCard(cardId); 1181 if (card == null) { 1182 try { 1183 callback.onComplete(EuiccCardManager.RESULT_EUICC_NOT_FOUND, null); 1184 } catch (RemoteException exception) { 1185 loge("retrieveNotificationList callback failure.", exception); 1186 } 1187 return; 1188 } 1189 1190 AsyncResultCallback<EuiccNotification[]> cardCb = 1191 new AsyncResultCallback<EuiccNotification[]>() { 1192 @Override 1193 public void onResult(EuiccNotification[] result) { 1194 try { 1195 callback.onComplete(EuiccCardManager.RESULT_OK, result); 1196 } catch (RemoteException exception) { 1197 loge("retrieveNotificationList callback failure.", exception); 1198 } 1199 } 1200 1201 @Override 1202 public void onException(Throwable e) { 1203 try { 1204 loge("retrieveNotificationList callback onException: ", e); 1205 callback.onComplete(getResultCode(e), null); 1206 } catch (RemoteException exception) { 1207 loge("retrieveNotificationList callback failure.", exception); 1208 } 1209 } 1210 }; 1211 1212 card.retrieveNotificationList(events, cardCb, mEuiccMainThreadHandler); 1213 } 1214 1215 @Override retrieveNotification(String callingPackage, String cardId, int seqNumber, IRetrieveNotificationCallback callback)1216 public void retrieveNotification(String callingPackage, String cardId, int seqNumber, 1217 IRetrieveNotificationCallback callback) { 1218 try { 1219 checkCallingPackage(callingPackage); 1220 } catch (SecurityException se) { 1221 try { 1222 callback.onComplete(EuiccCardManager.RESULT_CALLER_NOT_ALLOWED, null); 1223 } catch (RemoteException re) { 1224 loge("callback onComplete failure after checkCallingPackage.", re); 1225 } 1226 return; 1227 } 1228 1229 EuiccCard card = getEuiccCard(cardId); 1230 if (card == null) { 1231 try { 1232 callback.onComplete(EuiccCardManager.RESULT_EUICC_NOT_FOUND, null); 1233 } catch (RemoteException exception) { 1234 loge("retrieveNotification callback failure.", exception); 1235 } 1236 return; 1237 } 1238 1239 AsyncResultCallback<EuiccNotification> cardCb = 1240 new AsyncResultCallback<EuiccNotification>() { 1241 @Override 1242 public void onResult(EuiccNotification result) { 1243 try { 1244 callback.onComplete(EuiccCardManager.RESULT_OK, result); 1245 } catch (RemoteException exception) { 1246 loge("retrieveNotification callback failure.", exception); 1247 } 1248 } 1249 1250 @Override 1251 public void onException(Throwable e) { 1252 try { 1253 loge("retrieveNotification callback onException: ", e); 1254 callback.onComplete(getResultCode(e), null); 1255 } catch (RemoteException exception) { 1256 loge("retrieveNotification callback failure.", exception); 1257 } 1258 } 1259 }; 1260 1261 card.retrieveNotification(seqNumber, cardCb, mEuiccMainThreadHandler); 1262 } 1263 1264 @Override removeNotificationFromList(String callingPackage, String cardId, int seqNumber, IRemoveNotificationFromListCallback callback)1265 public void removeNotificationFromList(String callingPackage, String cardId, int seqNumber, 1266 IRemoveNotificationFromListCallback callback) { 1267 try { 1268 checkCallingPackage(callingPackage); 1269 } catch (SecurityException se) { 1270 try { 1271 callback.onComplete(EuiccCardManager.RESULT_CALLER_NOT_ALLOWED); 1272 } catch (RemoteException re) { 1273 loge("callback onComplete failure after checkCallingPackage.", re); 1274 } 1275 return; 1276 } 1277 1278 EuiccCard card = getEuiccCard(cardId); 1279 if (card == null) { 1280 try { 1281 callback.onComplete(EuiccCardManager.RESULT_EUICC_NOT_FOUND); 1282 } catch (RemoteException exception) { 1283 loge("removeNotificationFromList callback failure.", exception); 1284 } 1285 return; 1286 } 1287 1288 AsyncResultCallback<Void> cardCb = new AsyncResultCallback<Void>() { 1289 @Override 1290 public void onResult(Void result) { 1291 try { 1292 callback.onComplete(EuiccCardManager.RESULT_OK); 1293 } catch (RemoteException exception) { 1294 loge("removeNotificationFromList callback failure.", exception); 1295 } 1296 1297 } 1298 1299 @Override 1300 public void onException(Throwable e) { 1301 try { 1302 loge("removeNotificationFromList callback onException: ", e); 1303 callback.onComplete(getResultCode(e)); 1304 } catch (RemoteException exception) { 1305 loge("removeNotificationFromList callback failure.", exception); 1306 } 1307 } 1308 }; 1309 1310 card.removeNotificationFromList(seqNumber, cardCb, mEuiccMainThreadHandler); 1311 } 1312 1313 @Override dump(FileDescriptor fd, PrintWriter pw, String[] args)1314 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { 1315 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DUMP, "Requires DUMP"); 1316 final long token = Binder.clearCallingIdentity(); 1317 1318 super.dump(fd, pw, args); 1319 // TODO(b/38206971): dump more information. 1320 pw.println("mCallingPackage=" + mCallingPackage); 1321 pw.println("mBestComponent=" + mBestComponent); 1322 1323 Binder.restoreCallingIdentity(token); 1324 } 1325 loge(String message)1326 private static void loge(String message) { 1327 Log.e(TAG, message); 1328 } 1329 loge(String message, Throwable tr)1330 private static void loge(String message, Throwable tr) { 1331 Log.e(TAG, message, tr); 1332 } 1333 } 1334