1 /* 2 * Copyright (C) 2014 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.server.telecom; 18 19 import static android.Manifest.permission.CALL_PHONE; 20 import static android.Manifest.permission.DUMP; 21 import static android.Manifest.permission.MODIFY_PHONE_STATE; 22 import static android.Manifest.permission.READ_PHONE_STATE; 23 import static android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE; 24 import static android.Manifest.permission.REGISTER_SIM_SUBSCRIPTION; 25 import static android.Manifest.permission.WRITE_SECURE_SETTINGS; 26 27 import android.app.ActivityManager; 28 import android.app.AppOpsManager; 29 import android.content.ComponentName; 30 import android.content.Context; 31 import android.content.Intent; 32 import android.content.pm.ApplicationInfo; 33 import android.content.pm.PackageManager; 34 import android.content.res.Resources; 35 import android.net.Uri; 36 import android.os.Binder; 37 import android.os.Bundle; 38 import android.os.Process; 39 import android.os.UserHandle; 40 import android.os.UserManager; 41 import android.telecom.DefaultDialerManager; 42 import android.telecom.ParcelableCallAnalytics; 43 import android.telecom.PhoneAccount; 44 import android.telecom.PhoneAccountHandle; 45 import android.telecom.TelecomAnalytics; 46 import android.telecom.TelecomManager; 47 import android.telecom.VideoProfile; 48 import android.telephony.SubscriptionManager; 49 import android.telephony.TelephonyManager; 50 import android.util.EventLog; 51 52 // TODO: Needed for move to system service: import com.android.internal.R; 53 import com.android.internal.telecom.ITelecomService; 54 import com.android.internal.util.IndentingPrintWriter; 55 import com.android.server.telecom.components.UserCallIntentProcessorFactory; 56 import com.android.server.telecom.settings.BlockedNumbersActivity; 57 58 import java.io.FileDescriptor; 59 import java.io.PrintWriter; 60 import java.util.ArrayList; 61 import java.util.Arrays; 62 import java.util.Collections; 63 import java.util.List; 64 65 /** 66 * Implementation of the ITelecom interface. 67 */ 68 public class TelecomServiceImpl { 69 public interface DefaultDialerManagerAdapter { 70 String getDefaultDialerApplication(Context context); 71 String getDefaultDialerApplication(Context context, int userId); 72 boolean setDefaultDialerApplication(Context context, String packageName); 73 boolean isDefaultOrSystemDialer(Context context, String packageName); 74 } 75 76 static class DefaultDialerManagerAdapterImpl implements DefaultDialerManagerAdapter { 77 @Override 78 public String getDefaultDialerApplication(Context context) { 79 return DefaultDialerManager.getDefaultDialerApplication(context); 80 } 81 82 @Override 83 public String getDefaultDialerApplication(Context context, int userId) { 84 return DefaultDialerManager.getDefaultDialerApplication(context, userId); 85 } 86 87 @Override 88 public boolean setDefaultDialerApplication(Context context, String packageName) { 89 return DefaultDialerManager.setDefaultDialerApplication(context, packageName); 90 } 91 92 @Override 93 public boolean isDefaultOrSystemDialer(Context context, String packageName) { 94 return DefaultDialerManager.isDefaultOrSystemDialer(context, packageName); 95 } 96 } 97 98 public interface SubscriptionManagerAdapter { 99 int getDefaultVoiceSubId(); 100 } 101 102 static class SubscriptionManagerAdapterImpl implements SubscriptionManagerAdapter { 103 @Override 104 public int getDefaultVoiceSubId() { 105 return SubscriptionManager.getDefaultVoiceSubscriptionId(); 106 } 107 } 108 109 private static final String PERMISSION_PROCESS_PHONE_ACCOUNT_REGISTRATION = 110 "android.permission.PROCESS_PHONE_ACCOUNT_REGISTRATION"; 111 private static final int DEFAULT_VIDEO_STATE = -1; 112 113 private final ITelecomService.Stub mBinderImpl = new ITelecomService.Stub() { 114 @Override 115 public PhoneAccountHandle getDefaultOutgoingPhoneAccount(String uriScheme, 116 String callingPackage) { 117 try { 118 Log.startSession("TSI.gDOPA"); 119 synchronized (mLock) { 120 if (!canReadPhoneState(callingPackage, "getDefaultOutgoingPhoneAccount")) { 121 return null; 122 } 123 124 final UserHandle callingUserHandle = Binder.getCallingUserHandle(); 125 long token = Binder.clearCallingIdentity(); 126 try { 127 return mPhoneAccountRegistrar 128 .getOutgoingPhoneAccountForScheme(uriScheme, callingUserHandle); 129 } catch (Exception e) { 130 Log.e(this, e, "getDefaultOutgoingPhoneAccount"); 131 throw e; 132 } finally { 133 Binder.restoreCallingIdentity(token); 134 } 135 } 136 } finally { 137 Log.endSession(); 138 } 139 } 140 141 @Override 142 public PhoneAccountHandle getUserSelectedOutgoingPhoneAccount() { 143 synchronized (mLock) { 144 try { 145 Log.startSession("TSI.gUSOPA"); 146 final UserHandle callingUserHandle = Binder.getCallingUserHandle(); 147 return mPhoneAccountRegistrar.getUserSelectedOutgoingPhoneAccount( 148 callingUserHandle); 149 } catch (Exception e) { 150 Log.e(this, e, "getUserSelectedOutgoingPhoneAccount"); 151 throw e; 152 } finally { 153 Log.endSession(); 154 } 155 } 156 } 157 158 @Override 159 public void setUserSelectedOutgoingPhoneAccount(PhoneAccountHandle accountHandle) { 160 try { 161 Log.startSession("TSI.sUSOPA"); 162 synchronized (mLock) { 163 enforceModifyPermission(); 164 UserHandle callingUserHandle = Binder.getCallingUserHandle(); 165 long token = Binder.clearCallingIdentity(); 166 try { 167 mPhoneAccountRegistrar.setUserSelectedOutgoingPhoneAccount( 168 accountHandle, callingUserHandle); 169 } catch (Exception e) { 170 Log.e(this, e, "setUserSelectedOutgoingPhoneAccount"); 171 throw e; 172 } finally { 173 Binder.restoreCallingIdentity(token); 174 } 175 } 176 } finally { 177 Log.endSession(); 178 } 179 } 180 181 @Override 182 public List<PhoneAccountHandle> getCallCapablePhoneAccounts( 183 boolean includeDisabledAccounts, String callingPackage) { 184 try { 185 Log.startSession("TSI.gCCPA"); 186 if (!canReadPhoneState(callingPackage, "getDefaultOutgoingPhoneAccount")) { 187 return Collections.emptyList(); 188 } 189 synchronized (mLock) { 190 final UserHandle callingUserHandle = Binder.getCallingUserHandle(); 191 long token = Binder.clearCallingIdentity(); 192 try { 193 return mPhoneAccountRegistrar.getCallCapablePhoneAccounts(null, 194 includeDisabledAccounts, callingUserHandle); 195 } catch (Exception e) { 196 Log.e(this, e, "getCallCapablePhoneAccounts"); 197 throw e; 198 } finally { 199 Binder.restoreCallingIdentity(token); 200 } 201 } 202 } finally { 203 Log.endSession(); 204 } 205 } 206 207 @Override 208 public List<PhoneAccountHandle> getPhoneAccountsSupportingScheme(String uriScheme, 209 String callingPackage) { 210 try { 211 Log.startSession("TSI.gPASS"); 212 synchronized (mLock) { 213 if (!canReadPhoneState(callingPackage, "getPhoneAccountsSupportingScheme")) { 214 return Collections.emptyList(); 215 } 216 final UserHandle callingUserHandle = Binder.getCallingUserHandle(); 217 long token = Binder.clearCallingIdentity(); 218 try { 219 return mPhoneAccountRegistrar.getCallCapablePhoneAccounts(uriScheme, false, 220 callingUserHandle); 221 } catch (Exception e) { 222 Log.e(this, e, "getPhoneAccountsSupportingScheme %s", uriScheme); 223 throw e; 224 } finally { 225 Binder.restoreCallingIdentity(token); 226 } 227 } 228 } finally { 229 Log.endSession(); 230 } 231 } 232 233 @Override 234 public List<PhoneAccountHandle> getPhoneAccountsForPackage(String packageName) { 235 synchronized (mLock) { 236 final UserHandle callingUserHandle = Binder.getCallingUserHandle(); 237 long token = Binder.clearCallingIdentity(); 238 try { 239 Log.startSession("TSI.gPAFP"); 240 return mPhoneAccountRegistrar.getPhoneAccountsForPackage(packageName, 241 callingUserHandle); 242 } catch (Exception e) { 243 Log.e(this, e, "getPhoneAccountsForPackage %s", packageName); 244 throw e; 245 } finally { 246 Binder.restoreCallingIdentity(token); 247 Log.endSession(); 248 } 249 } 250 } 251 252 @Override 253 public PhoneAccount getPhoneAccount(PhoneAccountHandle accountHandle) { 254 synchronized (mLock) { 255 final UserHandle callingUserHandle = Binder.getCallingUserHandle(); 256 long token = Binder.clearCallingIdentity(); 257 try { 258 Log.startSession("TSI.gPA"); 259 // In ideal case, we should not resolve the handle across profiles. But given 260 // the fact that profile's call is handled by its parent user's in-call UI, 261 // parent user's in call UI need to be able to get phone account from the 262 // profile's phone account handle. 263 return mPhoneAccountRegistrar 264 .getPhoneAccount(accountHandle, callingUserHandle, 265 /* acrossProfiles */ true); 266 } catch (Exception e) { 267 Log.e(this, e, "getPhoneAccount %s", accountHandle); 268 throw e; 269 } finally { 270 Binder.restoreCallingIdentity(token); 271 Log.endSession(); 272 } 273 } 274 } 275 276 @Override 277 public int getAllPhoneAccountsCount() { 278 synchronized (mLock) { 279 try { 280 Log.startSession("TSI.gAPAC"); 281 // This list is pre-filtered for the calling user. 282 return getAllPhoneAccounts().size(); 283 } catch (Exception e) { 284 Log.e(this, e, "getAllPhoneAccountsCount"); 285 throw e; 286 } finally { 287 Log.endSession(); 288 } 289 } 290 } 291 292 @Override 293 public List<PhoneAccount> getAllPhoneAccounts() { 294 synchronized (mLock) { 295 final UserHandle callingUserHandle = Binder.getCallingUserHandle(); 296 long token = Binder.clearCallingIdentity(); 297 try { 298 Log.startSession("TSI.gAPA"); 299 return mPhoneAccountRegistrar.getAllPhoneAccounts(callingUserHandle); 300 } catch (Exception e) { 301 Log.e(this, e, "getAllPhoneAccounts"); 302 throw e; 303 } finally { 304 Binder.restoreCallingIdentity(token); 305 Log.endSession(); 306 } 307 } 308 } 309 310 @Override 311 public List<PhoneAccountHandle> getAllPhoneAccountHandles() { 312 synchronized (mLock) { 313 final UserHandle callingUserHandle = Binder.getCallingUserHandle(); 314 long token = Binder.clearCallingIdentity(); 315 try { 316 Log.startSession("TSI.gAPAH"); 317 return mPhoneAccountRegistrar.getAllPhoneAccountHandles(callingUserHandle); 318 } catch (Exception e) { 319 Log.e(this, e, "getAllPhoneAccounts"); 320 throw e; 321 } finally { 322 Binder.restoreCallingIdentity(token); 323 Log.endSession(); 324 } 325 } 326 } 327 328 @Override 329 public PhoneAccountHandle getSimCallManager() { 330 try { 331 Log.startSession("TSI.gSCM"); 332 long token = Binder.clearCallingIdentity(); 333 int user; 334 try { 335 user = ActivityManager.getCurrentUser(); 336 return getSimCallManagerForUser(user); 337 } finally { 338 Binder.restoreCallingIdentity(token); 339 } 340 } finally { 341 Log.endSession(); 342 } 343 } 344 345 @Override 346 public PhoneAccountHandle getSimCallManagerForUser(int user) { 347 synchronized (mLock) { 348 try { 349 Log.startSession("TSI.gSCMFU"); 350 final int callingUid = Binder.getCallingUid(); 351 long token = Binder.clearCallingIdentity(); 352 try { 353 if (user != ActivityManager.getCurrentUser()) { 354 enforceCrossUserPermission(callingUid); 355 } 356 return mPhoneAccountRegistrar.getSimCallManager(UserHandle.of(user)); 357 } finally { 358 Binder.restoreCallingIdentity(token); 359 } 360 } catch (Exception e) { 361 Log.e(this, e, "getSimCallManager"); 362 throw e; 363 } finally { 364 Log.endSession(); 365 } 366 } 367 } 368 369 @Override 370 public void registerPhoneAccount(PhoneAccount account) { 371 try { 372 Log.startSession("TSI.rPA"); 373 synchronized (mLock) { 374 if (!mContext.getApplicationContext().getResources().getBoolean( 375 com.android.internal.R.bool.config_voice_capable)) { 376 Log.w(this, 377 "registerPhoneAccount not allowed on non-voice capable device."); 378 return; 379 } 380 try { 381 enforcePhoneAccountModificationForPackage( 382 account.getAccountHandle().getComponentName().getPackageName()); 383 if (account.hasCapabilities(PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION)) { 384 enforceRegisterSimSubscriptionPermission(); 385 } 386 if (account.hasCapabilities(PhoneAccount.CAPABILITY_MULTI_USER)) { 387 enforceRegisterMultiUser(); 388 } 389 enforceUserHandleMatchesCaller(account.getAccountHandle()); 390 mPhoneAccountRegistrar.registerPhoneAccount(account); 391 // Broadcast an intent indicating the phone account which was registered. 392 long token = Binder.clearCallingIdentity(); 393 try { 394 Intent intent = new Intent( 395 TelecomManager.ACTION_PHONE_ACCOUNT_REGISTERED); 396 intent.putExtra(TelecomManager.EXTRA_PHONE_ACCOUNT_HANDLE, 397 account.getAccountHandle()); 398 Log.i(this, "Sending phone-account registered intent as user"); 399 mContext.sendBroadcastAsUser(intent, UserHandle.ALL, 400 PERMISSION_PROCESS_PHONE_ACCOUNT_REGISTRATION); 401 } finally { 402 Binder.restoreCallingIdentity(token); 403 } 404 } catch (Exception e) { 405 Log.e(this, e, "registerPhoneAccount %s", account); 406 throw e; 407 } 408 } 409 } finally { 410 Log.endSession(); 411 } 412 } 413 414 @Override 415 public void unregisterPhoneAccount(PhoneAccountHandle accountHandle) { 416 synchronized (mLock) { 417 try { 418 Log.startSession("TSI.uPA"); 419 enforcePhoneAccountModificationForPackage( 420 accountHandle.getComponentName().getPackageName()); 421 enforceUserHandleMatchesCaller(accountHandle); 422 mPhoneAccountRegistrar.unregisterPhoneAccount(accountHandle); 423 424 // Broadcast an intent indicating the phone account which was unregistered. 425 long token = Binder.clearCallingIdentity(); 426 try { 427 Intent intent = 428 new Intent(TelecomManager.ACTION_PHONE_ACCOUNT_UNREGISTERED); 429 intent.putExtra( 430 TelecomManager.EXTRA_PHONE_ACCOUNT_HANDLE, accountHandle); 431 Log.i(this, "Sending phone-account unregistered intent as user"); 432 mContext.sendBroadcastAsUser(intent, UserHandle.ALL, 433 PERMISSION_PROCESS_PHONE_ACCOUNT_REGISTRATION); 434 } finally { 435 Binder.restoreCallingIdentity(token); 436 } 437 } catch (Exception e) { 438 Log.e(this, e, "unregisterPhoneAccount %s", accountHandle); 439 throw e; 440 } finally { 441 Log.endSession(); 442 } 443 } 444 } 445 446 @Override 447 public void clearAccounts(String packageName) { 448 synchronized (mLock) { 449 try { 450 Log.startSession("TSI.cA"); 451 enforcePhoneAccountModificationForPackage(packageName); 452 mPhoneAccountRegistrar 453 .clearAccounts(packageName, Binder.getCallingUserHandle()); 454 } catch (Exception e) { 455 Log.e(this, e, "clearAccounts %s", packageName); 456 throw e; 457 } finally { 458 Log.endSession(); 459 } 460 } 461 } 462 463 /** 464 * @see android.telecom.TelecomManager#isVoiceMailNumber 465 */ 466 @Override 467 public boolean isVoiceMailNumber(PhoneAccountHandle accountHandle, String number, 468 String callingPackage) { 469 try { 470 Log.startSession("TSI.iVMN"); 471 synchronized (mLock) { 472 if (!canReadPhoneState(callingPackage, "isVoiceMailNumber")) { 473 return false; 474 } 475 final UserHandle callingUserHandle = Binder.getCallingUserHandle(); 476 if (!isPhoneAccountHandleVisibleToCallingUser(accountHandle, 477 callingUserHandle)) { 478 Log.d(this, "%s is not visible for the calling user [iVMN]", accountHandle); 479 return false; 480 } 481 long token = Binder.clearCallingIdentity(); 482 try { 483 return mPhoneAccountRegistrar.isVoiceMailNumber(accountHandle, number); 484 } catch (Exception e) { 485 Log.e(this, e, "getSubscriptionIdForPhoneAccount"); 486 throw e; 487 } finally { 488 Binder.restoreCallingIdentity(token); 489 } 490 } 491 } finally { 492 Log.endSession(); 493 } 494 } 495 496 /** 497 * @see android.telecom.TelecomManager#getVoiceMailNumber 498 */ 499 @Override 500 public String getVoiceMailNumber(PhoneAccountHandle accountHandle, String callingPackage) { 501 try { 502 Log.startSession("TSI.gVMN"); 503 synchronized (mLock) { 504 if (!canReadPhoneState(callingPackage, "getVoiceMailNumber")) { 505 return null; 506 } 507 try { 508 final UserHandle callingUserHandle = Binder.getCallingUserHandle(); 509 if (!isPhoneAccountHandleVisibleToCallingUser(accountHandle, 510 callingUserHandle)) { 511 Log.d(this, "%s is not visible for the calling user [gVMN]", 512 accountHandle); 513 return null; 514 } 515 int subId = mSubscriptionManagerAdapter.getDefaultVoiceSubId(); 516 if (accountHandle != null) { 517 subId = mPhoneAccountRegistrar 518 .getSubscriptionIdForPhoneAccount(accountHandle); 519 } 520 return getTelephonyManager().getVoiceMailNumber(subId); 521 } catch (Exception e) { 522 Log.e(this, e, "getSubscriptionIdForPhoneAccount"); 523 throw e; 524 } 525 } 526 } finally { 527 Log.endSession(); 528 } 529 } 530 531 /** 532 * @see android.telecom.TelecomManager#getLine1Number 533 */ 534 @Override 535 public String getLine1Number(PhoneAccountHandle accountHandle, String callingPackage) { 536 try { 537 Log.startSession("getL1N"); 538 if (!canReadPhoneState(callingPackage, "getLine1Number")) { 539 return null; 540 } 541 542 synchronized (mLock) { 543 final UserHandle callingUserHandle = Binder.getCallingUserHandle(); 544 if (!isPhoneAccountHandleVisibleToCallingUser(accountHandle, 545 callingUserHandle)) { 546 Log.d(this, "%s is not visible for the calling user [gL1N]", accountHandle); 547 return null; 548 } 549 550 long token = Binder.clearCallingIdentity(); 551 try { 552 int subId = mPhoneAccountRegistrar.getSubscriptionIdForPhoneAccount( 553 accountHandle); 554 return getTelephonyManager().getLine1Number(subId); 555 } catch (Exception e) { 556 Log.e(this, e, "getSubscriptionIdForPhoneAccount"); 557 throw e; 558 } finally { 559 Binder.restoreCallingIdentity(token); 560 } 561 } 562 } finally { 563 Log.endSession(); 564 } 565 } 566 567 /** 568 * @see android.telecom.TelecomManager#silenceRinger 569 */ 570 @Override 571 public void silenceRinger(String callingPackage) { 572 try { 573 Log.startSession("TSI.sR"); 574 synchronized (mLock) { 575 enforcePermissionOrPrivilegedDialer(MODIFY_PHONE_STATE, callingPackage); 576 577 long token = Binder.clearCallingIdentity(); 578 try { 579 Log.i(this, "Silence Ringer requested by %s", callingPackage); 580 mCallsManager.getCallAudioManager().silenceRingers(); 581 mCallsManager.getInCallController().silenceRinger(); 582 } finally { 583 Binder.restoreCallingIdentity(token); 584 } 585 } 586 } finally { 587 Log.endSession(); 588 } 589 } 590 591 /** 592 * @see android.telecom.TelecomManager#getDefaultPhoneApp 593 * @deprecated - Use {@link android.telecom.TelecomManager#getDefaultDialerPackage()} 594 * instead. 595 */ 596 @Override 597 public ComponentName getDefaultPhoneApp() { 598 try { 599 Log.startSession("TSI.gDPA"); 600 // No need to synchronize 601 Resources resources = mContext.getResources(); 602 return new ComponentName( 603 resources.getString(R.string.ui_default_package), 604 resources.getString(R.string.dialer_default_class)); 605 } finally { 606 Log.endSession(); 607 } 608 } 609 610 /** 611 * @return the package name of the current user-selected default dialer. If no default 612 * has been selected, the package name of the system dialer is returned. If 613 * neither exists, then {@code null} is returned. 614 * @see android.telecom.TelecomManager#getDefaultDialerPackage 615 */ 616 @Override 617 public String getDefaultDialerPackage() { 618 try { 619 Log.startSession("TSI.gDDP"); 620 final long token = Binder.clearCallingIdentity(); 621 try { 622 return mDefaultDialerManagerAdapter.getDefaultDialerApplication(mContext); 623 } finally { 624 Binder.restoreCallingIdentity(token); 625 } 626 } finally { 627 Log.endSession(); 628 } 629 } 630 631 /** 632 * @see android.telecom.TelecomManager#getSystemDialerPackage 633 */ 634 @Override 635 public String getSystemDialerPackage() { 636 try { 637 Log.startSession("TSI.gSDP"); 638 return mContext.getResources().getString(R.string.ui_default_package); 639 } finally { 640 Log.endSession(); 641 } 642 } 643 644 /** 645 * @see android.telecom.TelecomManager#isInCall 646 */ 647 @Override 648 public boolean isInCall(String callingPackage) { 649 try { 650 Log.startSession("TSI.iIC"); 651 if (!canReadPhoneState(callingPackage, "isInCall")) { 652 return false; 653 } 654 655 synchronized (mLock) { 656 final int callState = mCallsManager.getCallState(); 657 return callState == TelephonyManager.CALL_STATE_OFFHOOK 658 || callState == TelephonyManager.CALL_STATE_RINGING; 659 } 660 } finally { 661 Log.endSession(); 662 } 663 } 664 665 /** 666 * @see android.telecom.TelecomManager#isRinging 667 */ 668 @Override 669 public boolean isRinging(String callingPackage) { 670 try { 671 Log.startSession("TSI.iR"); 672 if (!canReadPhoneState(callingPackage, "isRinging")) { 673 return false; 674 } 675 676 synchronized (mLock) { 677 // Note: We are explicitly checking the calls telecom is tracking rather than 678 // relying on mCallsManager#getCallState(). Since getCallState() relies on the 679 // current state as tracked by PhoneStateBroadcaster, any failure to properly 680 // track the current call state there could result in the wrong ringing state 681 // being reported by this API. 682 return mCallsManager.hasRingingCall(); 683 } 684 } finally { 685 Log.endSession(); 686 } 687 } 688 689 /** 690 * @see TelecomManager#getCallState 691 */ 692 @Override 693 public int getCallState() { 694 try { 695 Log.startSession("TSI.getCallState"); 696 synchronized (mLock) { 697 return mCallsManager.getCallState(); 698 } 699 } finally { 700 Log.endSession(); 701 } 702 } 703 704 /** 705 * @see android.telecom.TelecomManager#endCall 706 */ 707 @Override 708 public boolean endCall() { 709 try { 710 Log.startSession("TSI.eC"); 711 synchronized (mLock) { 712 enforceModifyPermission(); 713 714 long token = Binder.clearCallingIdentity(); 715 try { 716 return endCallInternal(); 717 } finally { 718 Binder.restoreCallingIdentity(token); 719 } 720 } 721 } finally { 722 Log.endSession(); 723 } 724 } 725 726 /** 727 * @see android.telecom.TelecomManager#acceptRingingCall 728 */ 729 @Override 730 public void acceptRingingCall() { 731 try { 732 Log.startSession("TSI.aRC"); 733 synchronized (mLock) { 734 enforceModifyPermission(); 735 736 long token = Binder.clearCallingIdentity(); 737 try { 738 acceptRingingCallInternal(DEFAULT_VIDEO_STATE); 739 } finally { 740 Binder.restoreCallingIdentity(token); 741 } 742 } 743 } finally { 744 Log.endSession(); 745 } 746 } 747 748 /** 749 * @see android.telecom.TelecomManager#acceptRingingCall(int) 750 * 751 */ 752 @Override 753 public void acceptRingingCallWithVideoState(int videoState) { 754 try { 755 Log.startSession("TSI.aRCWVS"); 756 synchronized (mLock) { 757 enforceModifyPermission(); 758 759 long token = Binder.clearCallingIdentity(); 760 try { 761 acceptRingingCallInternal(videoState); 762 } finally { 763 Binder.restoreCallingIdentity(token); 764 } 765 } 766 } finally { 767 Log.endSession(); 768 } 769 } 770 771 /** 772 * @see android.telecom.TelecomManager#showInCallScreen 773 */ 774 @Override 775 public void showInCallScreen(boolean showDialpad, String callingPackage) { 776 try { 777 Log.startSession("TSI.sICS"); 778 if (!canReadPhoneState(callingPackage, "showInCallScreen")) { 779 return; 780 } 781 782 synchronized (mLock) { 783 784 long token = Binder.clearCallingIdentity(); 785 try { 786 mCallsManager.getInCallController().bringToForeground(showDialpad); 787 } finally { 788 Binder.restoreCallingIdentity(token); 789 } 790 } 791 } finally { 792 Log.endSession(); 793 } 794 } 795 796 /** 797 * @see android.telecom.TelecomManager#cancelMissedCallsNotification 798 */ 799 @Override 800 public void cancelMissedCallsNotification(String callingPackage) { 801 try { 802 Log.startSession("TSI.cMCN"); 803 synchronized (mLock) { 804 enforcePermissionOrPrivilegedDialer(MODIFY_PHONE_STATE, callingPackage); 805 UserHandle userHandle = Binder.getCallingUserHandle(); 806 long token = Binder.clearCallingIdentity(); 807 try { 808 mCallsManager.getMissedCallNotifier().clearMissedCalls(userHandle); 809 } finally { 810 Binder.restoreCallingIdentity(token); 811 } 812 } 813 } finally { 814 Log.endSession(); 815 } 816 } 817 /** 818 * @see android.telecom.TelecomManager#handleMmi 819 */ 820 @Override 821 public boolean handlePinMmi(String dialString, String callingPackage) { 822 try { 823 Log.startSession("TSI.hPM"); 824 synchronized (mLock) { 825 enforcePermissionOrPrivilegedDialer(MODIFY_PHONE_STATE, callingPackage); 826 827 // Switch identity so that TelephonyManager checks Telecom's permissions 828 // instead. 829 long token = Binder.clearCallingIdentity(); 830 boolean retval = false; 831 try { 832 retval = getTelephonyManager().handlePinMmi(dialString); 833 } finally { 834 Binder.restoreCallingIdentity(token); 835 } 836 837 return retval; 838 } 839 }finally { 840 Log.endSession(); 841 } 842 } 843 844 /** 845 * @see android.telecom.TelecomManager#handleMmi 846 */ 847 @Override 848 public boolean handlePinMmiForPhoneAccount(PhoneAccountHandle accountHandle, 849 String dialString, String callingPackage) { 850 try { 851 Log.startSession("TSI.hPMFPA"); 852 synchronized (mLock) { 853 enforcePermissionOrPrivilegedDialer(MODIFY_PHONE_STATE, callingPackage); 854 855 UserHandle callingUserHandle = Binder.getCallingUserHandle(); 856 if (!isPhoneAccountHandleVisibleToCallingUser(accountHandle, 857 callingUserHandle)) { 858 Log.d(this, "%s is not visible for the calling user [hMMI]", accountHandle); 859 return false; 860 } 861 862 // Switch identity so that TelephonyManager checks Telecom's permissions 863 // instead. 864 long token = Binder.clearCallingIdentity(); 865 boolean retval = false; 866 try { 867 int subId = mPhoneAccountRegistrar.getSubscriptionIdForPhoneAccount( 868 accountHandle); 869 retval = getTelephonyManager().handlePinMmiForSubscriber(subId, dialString); 870 } finally { 871 Binder.restoreCallingIdentity(token); 872 } 873 return retval; 874 } 875 }finally { 876 Log.endSession(); 877 } 878 } 879 880 /** 881 * @see android.telecom.TelecomManager#getAdnUriForPhoneAccount 882 */ 883 @Override 884 public Uri getAdnUriForPhoneAccount(PhoneAccountHandle accountHandle, 885 String callingPackage) { 886 try { 887 Log.startSession("TSI.aAUFPA"); 888 synchronized (mLock) { 889 enforcePermissionOrPrivilegedDialer(MODIFY_PHONE_STATE, callingPackage); 890 if (!isPhoneAccountHandleVisibleToCallingUser(accountHandle, 891 Binder.getCallingUserHandle())) { 892 Log.d(this, "%s is not visible for the calling user [gA4PA]", 893 accountHandle); 894 return null; 895 } 896 // Switch identity so that TelephonyManager checks Telecom's permissions 897 // instead. 898 long token = Binder.clearCallingIdentity(); 899 String retval = "content://icc/adn/"; 900 try { 901 long subId = mPhoneAccountRegistrar 902 .getSubscriptionIdForPhoneAccount(accountHandle); 903 retval = retval + "subId/" + subId; 904 } finally { 905 Binder.restoreCallingIdentity(token); 906 } 907 908 return Uri.parse(retval); 909 } 910 } finally { 911 Log.endSession(); 912 } 913 } 914 915 /** 916 * @see android.telecom.TelecomManager#isTtySupported 917 */ 918 @Override 919 public boolean isTtySupported(String callingPackage) { 920 try { 921 Log.startSession("TSI.iTS"); 922 if (!canReadPhoneState(callingPackage, "hasVoiceMailNumber")) { 923 return false; 924 } 925 926 synchronized (mLock) { 927 return mCallsManager.isTtySupported(); 928 } 929 } finally { 930 Log.endSession(); 931 } 932 } 933 934 /** 935 * @see android.telecom.TelecomManager#getCurrentTtyMode 936 */ 937 @Override 938 public int getCurrentTtyMode(String callingPackage) { 939 try { 940 Log.startSession("TSI.gCTM"); 941 if (!canReadPhoneState(callingPackage, "getCurrentTtyMode")) { 942 return TelecomManager.TTY_MODE_OFF; 943 } 944 945 synchronized (mLock) { 946 return mCallsManager.getCurrentTtyMode(); 947 } 948 } finally { 949 Log.endSession(); 950 } 951 } 952 953 /** 954 * @see android.telecom.TelecomManager#addNewIncomingCall 955 */ 956 @Override 957 public void addNewIncomingCall(PhoneAccountHandle phoneAccountHandle, Bundle extras) { 958 try { 959 Log.startSession("TSI.aNIC"); 960 synchronized (mLock) { 961 Log.i(this, "Adding new incoming call with phoneAccountHandle %s", 962 phoneAccountHandle); 963 if (phoneAccountHandle != null && 964 phoneAccountHandle.getComponentName() != null) { 965 // TODO(sail): Add unit tests for adding incoming calls from a SIM call 966 // manager. 967 if (isCallerSimCallManager() && TelephonyUtil.isPstnComponentName( 968 phoneAccountHandle.getComponentName())) { 969 Log.v(this, "Allowing call manager to add incoming call with PSTN" + 970 " handle"); 971 } else { 972 mAppOpsManager.checkPackage( 973 Binder.getCallingUid(), 974 phoneAccountHandle.getComponentName().getPackageName()); 975 // Make sure it doesn't cross the UserHandle boundary 976 enforceUserHandleMatchesCaller(phoneAccountHandle); 977 enforcePhoneAccountIsRegisteredEnabled(phoneAccountHandle, 978 Binder.getCallingUserHandle()); 979 } 980 long token = Binder.clearCallingIdentity(); 981 try { 982 Intent intent = new Intent(TelecomManager.ACTION_INCOMING_CALL); 983 intent.putExtra(TelecomManager.EXTRA_PHONE_ACCOUNT_HANDLE, 984 phoneAccountHandle); 985 intent.putExtra(CallIntentProcessor.KEY_IS_INCOMING_CALL, true); 986 if (extras != null) { 987 extras.setDefusable(true); 988 intent.putExtra(TelecomManager.EXTRA_INCOMING_CALL_EXTRAS, extras); 989 } 990 mCallIntentProcessorAdapter.processIncomingCallIntent( 991 mCallsManager, intent); 992 } finally { 993 Binder.restoreCallingIdentity(token); 994 } 995 } else { 996 Log.w(this, "Null phoneAccountHandle. Ignoring request to add new" + 997 " incoming call"); 998 } 999 } 1000 } finally { 1001 Log.endSession(); 1002 } 1003 } 1004 1005 /** 1006 * @see android.telecom.TelecomManager#addNewUnknownCall 1007 */ 1008 @Override 1009 public void addNewUnknownCall(PhoneAccountHandle phoneAccountHandle, Bundle extras) { 1010 try { 1011 Log.startSession("TSI.aNUC"); 1012 synchronized (mLock) { 1013 if (phoneAccountHandle != null && 1014 phoneAccountHandle.getComponentName() != null) { 1015 mAppOpsManager.checkPackage( 1016 Binder.getCallingUid(), 1017 phoneAccountHandle.getComponentName().getPackageName()); 1018 1019 // Make sure it doesn't cross the UserHandle boundary 1020 enforceUserHandleMatchesCaller(phoneAccountHandle); 1021 enforcePhoneAccountIsRegisteredEnabled(phoneAccountHandle, 1022 Binder.getCallingUserHandle()); 1023 long token = Binder.clearCallingIdentity(); 1024 1025 try { 1026 Intent intent = new Intent(TelecomManager.ACTION_NEW_UNKNOWN_CALL); 1027 if (extras != null) { 1028 extras.setDefusable(true); 1029 intent.putExtras(extras); 1030 } 1031 intent.putExtra(CallIntentProcessor.KEY_IS_UNKNOWN_CALL, true); 1032 intent.putExtra(TelecomManager.EXTRA_PHONE_ACCOUNT_HANDLE, 1033 phoneAccountHandle); 1034 mCallIntentProcessorAdapter.processUnknownCallIntent(mCallsManager, intent); 1035 } finally { 1036 Binder.restoreCallingIdentity(token); 1037 } 1038 } else { 1039 Log.i(this, 1040 "Null phoneAccountHandle or not initiated by Telephony. " + 1041 "Ignoring request to add new unknown call."); 1042 } 1043 } 1044 } finally { 1045 Log.endSession(); 1046 } 1047 } 1048 1049 /** 1050 * @see android.telecom.TelecomManager#placeCall 1051 */ 1052 @Override 1053 public void placeCall(Uri handle, Bundle extras, String callingPackage) { 1054 try { 1055 Log.startSession("TSI.pC"); 1056 enforceCallingPackage(callingPackage); 1057 if (!canCallPhone(callingPackage, "placeCall")) { 1058 throw new SecurityException("Package " + callingPackage 1059 + " is not allowed to place phone calls"); 1060 } 1061 1062 // Note: we can still get here for the default/system dialer, even if the Phone 1063 // permission is turned off. This is because the default/system dialer is always 1064 // allowed to attempt to place a call (regardless of permission state), in case 1065 // it turns out to be an emergency call. If the permission is denied and the 1066 // call is being made to a non-emergency number, the call will be denied later on 1067 // by {@link UserCallIntentProcessor}. 1068 1069 final boolean hasCallAppOp = mAppOpsManager.noteOp(AppOpsManager.OP_CALL_PHONE, 1070 Binder.getCallingUid(), callingPackage) == AppOpsManager.MODE_ALLOWED; 1071 1072 final boolean hasCallPermission = mContext.checkCallingPermission(CALL_PHONE) == 1073 PackageManager.PERMISSION_GRANTED; 1074 1075 synchronized (mLock) { 1076 final UserHandle userHandle = Binder.getCallingUserHandle(); 1077 long token = Binder.clearCallingIdentity(); 1078 try { 1079 final Intent intent = new Intent(Intent.ACTION_CALL, handle); 1080 if (extras != null) { 1081 extras.setDefusable(true); 1082 intent.putExtras(extras); 1083 } 1084 mUserCallIntentProcessorFactory.create(mContext, userHandle) 1085 .processIntent( 1086 intent, callingPackage, hasCallAppOp && hasCallPermission); 1087 } finally { 1088 Binder.restoreCallingIdentity(token); 1089 } 1090 } 1091 } finally { 1092 Log.endSession(); 1093 } 1094 } 1095 1096 /** 1097 * @see android.telecom.TelecomManager#enablePhoneAccount 1098 */ 1099 @Override 1100 public boolean enablePhoneAccount(PhoneAccountHandle accountHandle, boolean isEnabled) { 1101 try { 1102 Log.startSession("TSI.ePA"); 1103 enforceModifyPermission(); 1104 synchronized (mLock) { 1105 long token = Binder.clearCallingIdentity(); 1106 try { 1107 // enable/disable phone account 1108 return mPhoneAccountRegistrar.enablePhoneAccount(accountHandle, isEnabled); 1109 } finally { 1110 Binder.restoreCallingIdentity(token); 1111 } 1112 } 1113 } finally { 1114 Log.endSession(); 1115 } 1116 } 1117 1118 @Override 1119 public boolean setDefaultDialer(String packageName) { 1120 try { 1121 Log.startSession("TSI.sDD"); 1122 enforcePermission(MODIFY_PHONE_STATE); 1123 enforcePermission(WRITE_SECURE_SETTINGS); 1124 synchronized (mLock) { 1125 long token = Binder.clearCallingIdentity(); 1126 try { 1127 final boolean result = 1128 mDefaultDialerManagerAdapter 1129 .setDefaultDialerApplication(mContext, packageName); 1130 if (result) { 1131 final Intent intent = 1132 new Intent(TelecomManager.ACTION_DEFAULT_DIALER_CHANGED); 1133 intent.putExtra(TelecomManager.EXTRA_CHANGE_DEFAULT_DIALER_PACKAGE_NAME, 1134 packageName); 1135 mContext.sendBroadcastAsUser(intent, 1136 new UserHandle(ActivityManager.getCurrentUser())); 1137 } 1138 return result; 1139 } finally { 1140 Binder.restoreCallingIdentity(token); 1141 } 1142 } 1143 } finally { 1144 Log.endSession(); 1145 } 1146 } 1147 1148 @Override 1149 public TelecomAnalytics dumpCallAnalytics() { 1150 try { 1151 Log.startSession("TSI.dCA"); 1152 enforcePermission(DUMP); 1153 return Analytics.dumpToParcelableAnalytics(); 1154 } finally { 1155 Log.endSession(); 1156 } 1157 } 1158 1159 /** 1160 * Dumps the current state of the TelecomService. Used when generating problem reports. 1161 * 1162 * @param fd The file descriptor. 1163 * @param writer The print writer to dump the state to. 1164 * @param args Optional dump arguments. 1165 */ 1166 @Override 1167 protected void dump(FileDescriptor fd, final PrintWriter writer, String[] args) { 1168 if (mContext.checkCallingOrSelfPermission( 1169 android.Manifest.permission.DUMP) 1170 != PackageManager.PERMISSION_GRANTED) { 1171 writer.println("Permission Denial: can't dump TelecomService " + 1172 "from from pid=" + Binder.getCallingPid() + ", uid=" + 1173 Binder.getCallingUid()); 1174 return; 1175 } 1176 1177 final IndentingPrintWriter pw = new IndentingPrintWriter(writer, " "); 1178 if (mCallsManager != null) { 1179 pw.println("CallsManager: "); 1180 pw.increaseIndent(); 1181 mCallsManager.dump(pw); 1182 pw.decreaseIndent(); 1183 1184 pw.println("PhoneAccountRegistrar: "); 1185 pw.increaseIndent(); 1186 mPhoneAccountRegistrar.dump(pw); 1187 pw.decreaseIndent(); 1188 1189 pw.println("Analytics:"); 1190 pw.increaseIndent(); 1191 Analytics.dump(pw); 1192 pw.decreaseIndent(); 1193 } 1194 1195 Log.dumpCallEvents(pw); 1196 } 1197 1198 /** 1199 * @see android.telecom.TelecomManager#createManageBlockedNumbersIntent 1200 */ 1201 @Override 1202 public Intent createManageBlockedNumbersIntent() { 1203 return BlockedNumbersActivity.getIntentForStartingActivity(); 1204 } 1205 }; 1206 1207 private Context mContext; 1208 private AppOpsManager mAppOpsManager; 1209 private UserManager mUserManager; 1210 private PackageManager mPackageManager; 1211 private CallsManager mCallsManager; 1212 private final PhoneAccountRegistrar mPhoneAccountRegistrar; 1213 private final CallIntentProcessor.Adapter mCallIntentProcessorAdapter; 1214 private final UserCallIntentProcessorFactory mUserCallIntentProcessorFactory; 1215 private final DefaultDialerManagerAdapter mDefaultDialerManagerAdapter; 1216 private final SubscriptionManagerAdapter mSubscriptionManagerAdapter; 1217 private final TelecomSystem.SyncRoot mLock; 1218 1219 public TelecomServiceImpl( 1220 Context context, 1221 CallsManager callsManager, 1222 PhoneAccountRegistrar phoneAccountRegistrar, 1223 CallIntentProcessor.Adapter callIntentProcessorAdapter, 1224 UserCallIntentProcessorFactory userCallIntentProcessorFactory, 1225 DefaultDialerManagerAdapter defaultDialerManagerAdapter, 1226 SubscriptionManagerAdapter subscriptionManagerAdapter, 1227 TelecomSystem.SyncRoot lock) { 1228 mContext = context; 1229 mAppOpsManager = (AppOpsManager) mContext.getSystemService(Context.APP_OPS_SERVICE); 1230 1231 mUserManager = (UserManager) mContext.getSystemService(Context.USER_SERVICE); 1232 mPackageManager = mContext.getPackageManager(); 1233 1234 mCallsManager = callsManager; 1235 mLock = lock; 1236 mPhoneAccountRegistrar = phoneAccountRegistrar; 1237 mUserCallIntentProcessorFactory = userCallIntentProcessorFactory; 1238 mDefaultDialerManagerAdapter = defaultDialerManagerAdapter; 1239 mCallIntentProcessorAdapter = callIntentProcessorAdapter; 1240 mSubscriptionManagerAdapter = subscriptionManagerAdapter; 1241 } 1242 1243 public ITelecomService.Stub getBinder() { 1244 return mBinderImpl; 1245 } 1246 1247 // 1248 // Supporting methods for the ITelecomService interface implementation. 1249 // 1250 1251 private boolean isPhoneAccountHandleVisibleToCallingUser( 1252 PhoneAccountHandle phoneAccountUserHandle, UserHandle callingUser) { 1253 return mPhoneAccountRegistrar.getPhoneAccount(phoneAccountUserHandle, callingUser) != null; 1254 } 1255 1256 private boolean isCallerSystemApp() { 1257 int uid = Binder.getCallingUid(); 1258 String[] packages = mPackageManager.getPackagesForUid(uid); 1259 for (String packageName : packages) { 1260 if (isPackageSystemApp(packageName)) { 1261 return true; 1262 } 1263 } 1264 return false; 1265 } 1266 1267 private boolean isPackageSystemApp(String packageName) { 1268 try { 1269 ApplicationInfo applicationInfo = mPackageManager.getApplicationInfo(packageName, 1270 PackageManager.GET_META_DATA); 1271 if ((applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) { 1272 return true; 1273 } 1274 } catch (PackageManager.NameNotFoundException e) { 1275 } 1276 return false; 1277 } 1278 1279 private void acceptRingingCallInternal(int videoState) { 1280 Call call = mCallsManager.getFirstCallWithState(CallState.RINGING); 1281 if (call != null) { 1282 if (videoState == DEFAULT_VIDEO_STATE || !isValidAcceptVideoState(videoState)) { 1283 videoState = call.getVideoState(); 1284 } 1285 call.answer(videoState); 1286 } 1287 } 1288 1289 private boolean endCallInternal() { 1290 // Always operate on the foreground call if one exists, otherwise get the first call in 1291 // priority order by call-state. 1292 Call call = mCallsManager.getForegroundCall(); 1293 if (call == null) { 1294 call = mCallsManager.getFirstCallWithState( 1295 CallState.ACTIVE, 1296 CallState.DIALING, 1297 CallState.PULLING, 1298 CallState.RINGING, 1299 CallState.ON_HOLD); 1300 } 1301 1302 if (call != null) { 1303 if (call.getState() == CallState.RINGING) { 1304 call.reject(false /* rejectWithMessage */, null); 1305 } else { 1306 call.disconnect(); 1307 } 1308 return true; 1309 } 1310 1311 return false; 1312 } 1313 1314 // Enforce that the PhoneAccountHandle being passed in is both registered to the current user 1315 // and enabled. 1316 private void enforcePhoneAccountIsRegisteredEnabled(PhoneAccountHandle phoneAccountHandle, 1317 UserHandle callingUserHandle) { 1318 PhoneAccount phoneAccount = mPhoneAccountRegistrar.getPhoneAccount(phoneAccountHandle, 1319 callingUserHandle); 1320 if(phoneAccount == null) { 1321 EventLog.writeEvent(0x534e4554, "26864502", Binder.getCallingUid(), "R"); 1322 throw new SecurityException("This PhoneAccountHandle is not registered for this user!"); 1323 } 1324 if(!phoneAccount.isEnabled()) { 1325 EventLog.writeEvent(0x534e4554, "26864502", Binder.getCallingUid(), "E"); 1326 throw new SecurityException("This PhoneAccountHandle is not enabled for this user!"); 1327 } 1328 } 1329 1330 private void enforcePhoneAccountModificationForPackage(String packageName) { 1331 // TODO: Use a new telecomm permission for this instead of reusing modify. 1332 1333 int result = mContext.checkCallingOrSelfPermission(MODIFY_PHONE_STATE); 1334 1335 // Callers with MODIFY_PHONE_STATE can use the PhoneAccount mechanism to implement 1336 // built-in behavior even when PhoneAccounts are not exposed as a third-part API. They 1337 // may also modify PhoneAccounts on behalf of any 'packageName'. 1338 1339 if (result != PackageManager.PERMISSION_GRANTED) { 1340 // Other callers are only allowed to modify PhoneAccounts if the relevant system 1341 // feature is enabled ... 1342 enforceConnectionServiceFeature(); 1343 // ... and the PhoneAccounts they refer to are for their own package. 1344 enforceCallingPackage(packageName); 1345 } 1346 } 1347 1348 private void enforcePermissionOrPrivilegedDialer(String permission, String packageName) { 1349 if (!isPrivilegedDialerCalling(packageName)) { 1350 try { 1351 enforcePermission(permission); 1352 } catch (SecurityException e) { 1353 Log.e(this, e, "Caller must be the default or system dialer, or have the permission" 1354 + " %s to perform this operation.", permission); 1355 throw e; 1356 } 1357 } 1358 } 1359 1360 private void enforceCallingPackage(String packageName) { 1361 mAppOpsManager.checkPackage(Binder.getCallingUid(), packageName); 1362 } 1363 1364 private void enforceConnectionServiceFeature() { 1365 enforceFeature(PackageManager.FEATURE_CONNECTION_SERVICE); 1366 } 1367 1368 private void enforceRegisterSimSubscriptionPermission() { 1369 enforcePermission(REGISTER_SIM_SUBSCRIPTION); 1370 } 1371 1372 private void enforceModifyPermission() { 1373 enforcePermission(MODIFY_PHONE_STATE); 1374 } 1375 1376 private void enforcePermission(String permission) { 1377 mContext.enforceCallingOrSelfPermission(permission, null); 1378 } 1379 1380 private void enforceRegisterMultiUser() { 1381 if (!isCallerSystemApp()) { 1382 throw new SecurityException("CAPABILITY_MULTI_USER is only available to system apps."); 1383 } 1384 } 1385 1386 private void enforceUserHandleMatchesCaller(PhoneAccountHandle accountHandle) { 1387 if (!Binder.getCallingUserHandle().equals(accountHandle.getUserHandle())) { 1388 throw new SecurityException("Calling UserHandle does not match PhoneAccountHandle's"); 1389 } 1390 } 1391 1392 private void enforceCrossUserPermission(int callingUid) { 1393 if (callingUid != Process.SYSTEM_UID && callingUid != 0) { 1394 mContext.enforceCallingOrSelfPermission( 1395 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL, "Must be system or have" 1396 + " INTERACT_ACROSS_USERS_FULL permission"); 1397 } 1398 } 1399 1400 private void enforceFeature(String feature) { 1401 PackageManager pm = mContext.getPackageManager(); 1402 if (!pm.hasSystemFeature(feature)) { 1403 throw new UnsupportedOperationException( 1404 "System does not support feature " + feature); 1405 } 1406 } 1407 1408 private boolean canReadPhoneState(String callingPackage, String message) { 1409 // The system/default dialer can always read phone state - so that emergency calls will 1410 // still work. 1411 if (isPrivilegedDialerCalling(callingPackage)) { 1412 return true; 1413 } 1414 1415 try { 1416 mContext.enforceCallingOrSelfPermission(READ_PRIVILEGED_PHONE_STATE, message); 1417 // SKIP checking run-time OP_READ_PHONE_STATE since caller or self has PRIVILEGED 1418 // permission 1419 return true; 1420 } catch (SecurityException e) { 1421 // Accessing phone state is gated by a special permission. 1422 mContext.enforceCallingOrSelfPermission(READ_PHONE_STATE, message); 1423 1424 // Some apps that have the permission can be restricted via app ops. 1425 return mAppOpsManager.noteOp(AppOpsManager.OP_READ_PHONE_STATE, 1426 Binder.getCallingUid(), callingPackage) == AppOpsManager.MODE_ALLOWED; 1427 } 1428 } 1429 1430 private boolean canCallPhone(String callingPackage, String message) { 1431 // The system/default dialer can always read phone state - so that emergency calls will 1432 // still work. 1433 if (isPrivilegedDialerCalling(callingPackage)) { 1434 return true; 1435 } 1436 1437 // Accessing phone state is gated by a special permission. 1438 mContext.enforceCallingOrSelfPermission(CALL_PHONE, message); 1439 1440 // Some apps that have the permission can be restricted via app ops. 1441 return mAppOpsManager.noteOp(AppOpsManager.OP_CALL_PHONE, 1442 Binder.getCallingUid(), callingPackage) == AppOpsManager.MODE_ALLOWED; 1443 } 1444 1445 private boolean isCallerSimCallManager() { 1446 long token = Binder.clearCallingIdentity(); 1447 PhoneAccountHandle accountHandle = null; 1448 try { 1449 accountHandle = mPhoneAccountRegistrar.getSimCallManagerOfCurrentUser(); 1450 } finally { 1451 Binder.restoreCallingIdentity(token); 1452 } 1453 1454 if (accountHandle != null) { 1455 try { 1456 mAppOpsManager.checkPackage( 1457 Binder.getCallingUid(), accountHandle.getComponentName().getPackageName()); 1458 return true; 1459 } catch (SecurityException e) { 1460 } 1461 } 1462 return false; 1463 } 1464 1465 private boolean isPrivilegedDialerCalling(String callingPackage) { 1466 mAppOpsManager.checkPackage(Binder.getCallingUid(), callingPackage); 1467 return mDefaultDialerManagerAdapter.isDefaultOrSystemDialer(mContext, callingPackage); 1468 } 1469 1470 private TelephonyManager getTelephonyManager() { 1471 return (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE); 1472 } 1473 1474 /** 1475 * Determines if a video state is valid for accepting an incoming call. 1476 * For the purpose of accepting a call, states {@link VideoProfile#STATE_AUDIO_ONLY}, and 1477 * any combination of {@link VideoProfile#STATE_RX_ENABLED} and 1478 * {@link VideoProfile#STATE_TX_ENABLED} are considered valid. 1479 * 1480 * @param videoState The video state. 1481 * @return {@code true} if the video state is valid, {@code false} otherwise. 1482 */ 1483 private boolean isValidAcceptVideoState(int videoState) { 1484 // Given a video state input, turn off TX and RX so that we can determine if those were the 1485 // only bits set. 1486 int remainingState = videoState & ~VideoProfile.STATE_TX_ENABLED; 1487 remainingState = remainingState & ~VideoProfile.STATE_RX_ENABLED; 1488 1489 // If only TX or RX were set (or neither), the video state is valid. 1490 return remainingState == 0; 1491 } 1492 } 1493