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.CALL_PRIVILEGED; 21 import static android.Manifest.permission.DUMP; 22 import static android.Manifest.permission.MANAGE_OWN_CALLS; 23 import static android.Manifest.permission.MODIFY_PHONE_STATE; 24 import static android.Manifest.permission.READ_PHONE_NUMBERS; 25 import static android.Manifest.permission.READ_PHONE_STATE; 26 import static android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE; 27 import static android.Manifest.permission.READ_SMS; 28 import static android.Manifest.permission.REGISTER_SIM_SUBSCRIPTION; 29 import static android.Manifest.permission.WRITE_SECURE_SETTINGS; 30 import static android.telecom.CallAttributes.DIRECTION_INCOMING; 31 import static android.telecom.CallAttributes.DIRECTION_OUTGOING; 32 import static android.telecom.CallException.CODE_ERROR_UNKNOWN; 33 import static android.telecom.TelecomManager.TELECOM_TRANSACTION_SUCCESS; 34 35 import android.Manifest; 36 import android.app.ActivityManager; 37 import android.app.AppOpsManager; 38 import android.app.UiModeManager; 39 import android.app.compat.CompatChanges; 40 import android.content.AttributionSource; 41 import android.content.ComponentName; 42 import android.content.ContentResolver; 43 import android.content.Context; 44 import android.content.Intent; 45 import android.content.PermissionChecker; 46 import android.content.pm.ApplicationInfo; 47 import android.content.pm.PackageManager; 48 import android.content.pm.ParceledListSlice; 49 import android.content.pm.ResolveInfo; 50 import android.graphics.drawable.Icon; 51 import android.net.Uri; 52 import android.os.Binder; 53 import android.os.Build; 54 import android.os.Bundle; 55 import android.os.OutcomeReceiver; 56 import android.os.Process; 57 import android.os.RemoteException; 58 import android.os.UserHandle; 59 import android.provider.BlockedNumberContract; 60 import android.provider.Settings; 61 import android.telecom.CallAttributes; 62 import android.telecom.CallException; 63 import android.telecom.Log; 64 import android.telecom.PhoneAccount; 65 import android.telecom.PhoneAccountHandle; 66 import android.telecom.TelecomAnalytics; 67 import android.telecom.TelecomManager; 68 import android.telecom.VideoProfile; 69 import android.telephony.SubscriptionManager; 70 import android.telephony.TelephonyManager; 71 import android.text.TextUtils; 72 import android.util.EventLog; 73 74 import androidx.annotation.NonNull; 75 76 import com.android.internal.annotations.VisibleForTesting; 77 import com.android.internal.telecom.ICallControl; 78 import com.android.internal.telecom.ICallEventCallback; 79 import com.android.internal.telecom.ITelecomService; 80 import com.android.internal.util.IndentingPrintWriter; 81 import com.android.server.telecom.components.UserCallIntentProcessorFactory; 82 import com.android.server.telecom.settings.BlockedNumbersActivity; 83 import com.android.server.telecom.voip.IncomingCallTransaction; 84 import com.android.server.telecom.voip.OutgoingCallTransaction; 85 import com.android.server.telecom.voip.TransactionManager; 86 import com.android.server.telecom.voip.VoipCallTransaction; 87 import com.android.server.telecom.voip.VoipCallTransactionResult; 88 89 import java.io.FileDescriptor; 90 import java.io.PrintWriter; 91 import java.util.HashSet; 92 import java.util.List; 93 import java.util.Objects; 94 import java.util.Set; 95 import java.util.UUID; 96 97 // TODO: Needed for move to system service: import com.android.internal.R; 98 99 /** 100 * Implementation of the ITelecom interface. 101 */ 102 public class TelecomServiceImpl { 103 104 public interface SubscriptionManagerAdapter { getDefaultVoiceSubId()105 int getDefaultVoiceSubId(); 106 } 107 108 static class SubscriptionManagerAdapterImpl implements SubscriptionManagerAdapter { 109 @Override getDefaultVoiceSubId()110 public int getDefaultVoiceSubId() { 111 return SubscriptionManager.getDefaultVoiceSubscriptionId(); 112 } 113 } 114 115 public interface SettingsSecureAdapter { putStringForUser(ContentResolver resolver, String name, String value, int userHandle)116 void putStringForUser(ContentResolver resolver, String name, String value, int userHandle); 117 getStringForUser(ContentResolver resolver, String name, int userHandle)118 String getStringForUser(ContentResolver resolver, String name, int userHandle); 119 } 120 121 static class SettingsSecureAdapterImpl implements SettingsSecureAdapter { 122 @Override putStringForUser(ContentResolver resolver, String name, String value, int userHandle)123 public void putStringForUser(ContentResolver resolver, String name, String value, 124 int userHandle) { 125 Settings.Secure.putStringForUser(resolver, name, value, userHandle); 126 } 127 128 @Override getStringForUser(ContentResolver resolver, String name, int userHandle)129 public String getStringForUser(ContentResolver resolver, String name, int userHandle) { 130 return Settings.Secure.getStringForUser(resolver, name, userHandle); 131 } 132 } 133 134 private static final String TAG = "TelecomServiceImpl"; 135 private static final String TIME_LINE_ARG = "timeline"; 136 private static final int DEFAULT_VIDEO_STATE = -1; 137 private static final String PERMISSION_HANDLE_CALL_INTENT = 138 "android.permission.HANDLE_CALL_INTENT"; 139 private static final String ADD_CALL_ERR_MSG = "Call could not be created or found. " 140 + "Retry operation."; 141 private AnomalyReporterAdapter mAnomalyReporter = new AnomalyReporterAdapterImpl(); 142 143 /** 144 * Anomaly Report UUIDs and corresponding error descriptions specific to TelecomServiceImpl. 145 */ 146 public static final UUID REGISTER_PHONE_ACCOUNT_ERROR_UUID = 147 UUID.fromString("0e49f82e-6acc-48a9-b088-66c8296c1eb5"); 148 public static final String REGISTER_PHONE_ACCOUNT_ERROR_MSG = 149 "Exception thrown while registering phone account."; 150 public static final UUID SET_USER_PHONE_ACCOUNT_ERROR_UUID = 151 UUID.fromString("80866066-7818-4869-bd44-1f7f689543e2"); 152 public static final String SET_USER_PHONE_ACCOUNT_ERROR_MSG = 153 "Exception thrown while setting the user selected outgoing phone account."; 154 public static final UUID GET_CALL_CAPABLE_ACCOUNTS_ERROR_UUID = 155 UUID.fromString("4f39b865-01f2-4c1f-83a5-37ce52807e83"); 156 public static final String GET_CALL_CAPABLE_ACCOUNTS_ERROR_MSG = 157 "Exception thrown while getting the call capable phone accounts"; 158 public static final UUID GET_PHONE_ACCOUNT_ERROR_UUID = 159 UUID.fromString("b653c1f0-91b4-45c8-ad05-3ee4d1006c7f"); 160 public static final String GET_PHONE_ACCOUNT_ERROR_MSG = 161 "Exception thrown while retrieving the phone account."; 162 public static final UUID GET_SIM_MANAGER_ERROR_UUID = 163 UUID.fromString("4244cb3f-bd02-4cc5-9f90-f41ea62ce0bb"); 164 public static final String GET_SIM_MANAGER_ERROR_MSG = 165 "Exception thrown while retrieving the SIM CallManager."; 166 public static final UUID GET_SIM_MANAGER_FOR_USER_ERROR_UUID = 167 UUID.fromString("5d347ce7-7527-40d3-b98a-09b423ad031c"); 168 public static final String GET_SIM_MANAGER_FOR_USER_ERROR_MSG = 169 "Exception thrown while retrieving the SIM CallManager based on the provided user."; 170 public static final UUID PLACE_CALL_SECURITY_EXCEPTION_ERROR_UUID = 171 UUID.fromString("4edf6c8d-1e43-4c94-b0fc-a40c8d80cfe8"); 172 public static final String PLACE_CALL_SECURITY_EXCEPTION_ERROR_MSG = 173 "Security exception thrown while placing an outgoing call."; 174 175 @VisibleForTesting setAnomalyReporterAdapter(AnomalyReporterAdapter mAnomalyReporterAdapter)176 public void setAnomalyReporterAdapter(AnomalyReporterAdapter mAnomalyReporterAdapter){ 177 mAnomalyReporter = mAnomalyReporterAdapter; 178 } 179 180 private final ITelecomService.Stub mBinderImpl = new ITelecomService.Stub() { 181 182 @Override 183 public void addCall(CallAttributes callAttributes, ICallEventCallback callEventCallback, 184 String callId, String callingPackage) { 185 try { 186 Log.startSession("TSI.aC", Log.getPackageAbbreviation(callingPackage)); 187 Log.i(TAG, "addCall: id=[%s], attributes=[%s]", callId, callAttributes); 188 PhoneAccountHandle handle = callAttributes.getPhoneAccountHandle(); 189 190 // enforce permissions and arguments 191 enforcePermission(android.Manifest.permission.MANAGE_OWN_CALLS); 192 enforceUserHandleMatchesCaller(handle); 193 enforcePhoneAccountIsNotManaged(handle);// only allow self-managed packages (temp.) 194 enforcePhoneAccountIsRegisteredEnabled(handle, handle.getUserHandle()); 195 enforceCallingPackage(callingPackage, "addCall"); 196 197 // add extras about info used for FGS delegation 198 Bundle extras = new Bundle(); 199 extras.putInt(CallAttributes.CALLER_UID_KEY, Binder.getCallingUid()); 200 extras.putInt(CallAttributes.CALLER_PID_KEY, Binder.getCallingPid()); 201 202 VoipCallTransaction transaction = null; 203 // create transaction based on the call direction 204 switch (callAttributes.getDirection()) { 205 case DIRECTION_OUTGOING: 206 transaction = new OutgoingCallTransaction(callId, mContext, callAttributes, 207 mCallsManager, extras); 208 break; 209 case DIRECTION_INCOMING: 210 transaction = new IncomingCallTransaction(callId, callAttributes, 211 mCallsManager, extras); 212 break; 213 default: 214 throw new IllegalArgumentException(String.format("Invalid Call Direction. " 215 + "Was [%d] but should be within [%d,%d]", 216 callAttributes.getDirection(), DIRECTION_INCOMING, 217 DIRECTION_OUTGOING)); 218 } 219 220 mTransactionManager.addTransaction(transaction, new OutcomeReceiver<>() { 221 @Override 222 public void onResult(VoipCallTransactionResult result) { 223 Log.d(TAG, "addCall: onResult"); 224 Call call = result.getCall(); 225 226 if (call == null || !call.getId().equals(callId)) { 227 Log.i(TAG, "addCall: onResult: call is null or id mismatch"); 228 onAddCallControl(callId, callEventCallback, null, 229 new CallException(ADD_CALL_ERR_MSG, CODE_ERROR_UNKNOWN)); 230 return; 231 } 232 233 TransactionalServiceWrapper serviceWrapper = 234 mTransactionalServiceRepository 235 .addNewCallForTransactionalServiceWrapper(handle, 236 callEventCallback, mCallsManager, call); 237 238 call.setTransactionServiceWrapper(serviceWrapper); 239 ICallControl clientCallControl = serviceWrapper.getICallControl(); 240 241 if (clientCallControl == null) { 242 throw new IllegalStateException("TransactionalServiceWrapper" 243 + "#ICallControl is null."); 244 } 245 246 // finally, send objects back to the client 247 onAddCallControl(callId, callEventCallback, clientCallControl, null); 248 } 249 250 @Override 251 public void onError(@NonNull CallException exception) { 252 Log.d(TAG, "addCall: onError: e=[%s]", exception.toString()); 253 onAddCallControl(callId, callEventCallback, null, exception); 254 } 255 }); 256 } finally { 257 Log.endSession(); 258 } 259 } 260 261 private void onAddCallControl(String callId, ICallEventCallback callEventCallback, 262 ICallControl callControl, CallException callException) { 263 try { 264 if (callException == null) { 265 callEventCallback.onAddCallControl(callId, TELECOM_TRANSACTION_SUCCESS, 266 callControl, null); 267 } else { 268 callEventCallback.onAddCallControl(callId, 269 CallException.CODE_ERROR_UNKNOWN, 270 null, callException); 271 } 272 } catch (RemoteException remoteException) { 273 throw remoteException.rethrowAsRuntimeException(); 274 } 275 } 276 277 @Override 278 public PhoneAccountHandle getDefaultOutgoingPhoneAccount(String uriScheme, 279 String callingPackage, String callingFeatureId) { 280 try { 281 Log.startSession("TSI.gDOPA", Log.getPackageAbbreviation(callingPackage)); 282 synchronized (mLock) { 283 PhoneAccountHandle phoneAccountHandle = null; 284 final UserHandle callingUserHandle = Binder.getCallingUserHandle(); 285 long token = Binder.clearCallingIdentity(); 286 try { 287 phoneAccountHandle = mPhoneAccountRegistrar 288 .getOutgoingPhoneAccountForScheme(uriScheme, callingUserHandle); 289 } catch (Exception e) { 290 Log.e(this, e, "getDefaultOutgoingPhoneAccount"); 291 throw e; 292 } finally { 293 Binder.restoreCallingIdentity(token); 294 } 295 if (isCallerSimCallManager(phoneAccountHandle) 296 || canReadPhoneState( 297 callingPackage, 298 callingFeatureId, 299 "getDefaultOutgoingPhoneAccount")) { 300 return phoneAccountHandle; 301 } 302 return null; 303 } 304 } finally { 305 Log.endSession(); 306 } 307 } 308 309 @Override 310 public PhoneAccountHandle getUserSelectedOutgoingPhoneAccount(String callingPackage) { 311 synchronized (mLock) { 312 try { 313 Log.startSession("TSI.gUSOPA", Log.getPackageAbbreviation(callingPackage)); 314 if (!isDialerOrPrivileged(callingPackage, "getDefaultOutgoingPhoneAccount")) { 315 throw new SecurityException("Only the default dialer, or caller with " 316 + "READ_PRIVILEGED_PHONE_STATE can call this method."); 317 } 318 final UserHandle callingUserHandle = Binder.getCallingUserHandle(); 319 return mPhoneAccountRegistrar.getUserSelectedOutgoingPhoneAccount( 320 callingUserHandle); 321 } catch (Exception e) { 322 Log.e(this, e, "getUserSelectedOutgoingPhoneAccount"); 323 throw e; 324 } finally { 325 Log.endSession(); 326 } 327 } 328 } 329 330 @Override 331 public void setUserSelectedOutgoingPhoneAccount(PhoneAccountHandle accountHandle) { 332 try { 333 Log.startSession("TSI.sUSOPA"); 334 synchronized (mLock) { 335 enforceModifyPermission(); 336 UserHandle callingUserHandle = Binder.getCallingUserHandle(); 337 long token = Binder.clearCallingIdentity(); 338 try { 339 mPhoneAccountRegistrar.setUserSelectedOutgoingPhoneAccount( 340 accountHandle, callingUserHandle); 341 } catch (Exception e) { 342 Log.e(this, e, "setUserSelectedOutgoingPhoneAccount"); 343 mAnomalyReporter.reportAnomaly(SET_USER_PHONE_ACCOUNT_ERROR_UUID, 344 SET_USER_PHONE_ACCOUNT_ERROR_MSG); 345 throw e; 346 } finally { 347 Binder.restoreCallingIdentity(token); 348 } 349 } 350 } finally { 351 Log.endSession(); 352 } 353 } 354 355 @Override 356 public ParceledListSlice<PhoneAccountHandle> getCallCapablePhoneAccounts( 357 boolean includeDisabledAccounts, String callingPackage, String callingFeatureId) { 358 try { 359 Log.startSession("TSI.gCCPA", Log.getPackageAbbreviation(callingPackage)); 360 if (includeDisabledAccounts && 361 !canReadPrivilegedPhoneState( 362 callingPackage, "getCallCapablePhoneAccounts")) { 363 return ParceledListSlice.emptyList(); 364 } 365 if (!canReadPhoneState(callingPackage, callingFeatureId, 366 "getCallCapablePhoneAccounts")) { 367 return ParceledListSlice.emptyList(); 368 } 369 synchronized (mLock) { 370 final UserHandle callingUserHandle = Binder.getCallingUserHandle(); 371 boolean crossUserAccess = hasInAppCrossUserPermission(); 372 long token = Binder.clearCallingIdentity(); 373 try { 374 return new ParceledListSlice<>( 375 mPhoneAccountRegistrar.getCallCapablePhoneAccounts(null, 376 includeDisabledAccounts, callingUserHandle, 377 crossUserAccess)); 378 } catch (Exception e) { 379 Log.e(this, e, "getCallCapablePhoneAccounts"); 380 mAnomalyReporter.reportAnomaly(GET_CALL_CAPABLE_ACCOUNTS_ERROR_UUID, 381 GET_CALL_CAPABLE_ACCOUNTS_ERROR_MSG); 382 throw e; 383 } finally { 384 Binder.restoreCallingIdentity(token); 385 } 386 } 387 } finally { 388 Log.endSession(); 389 } 390 } 391 392 @Override 393 public ParceledListSlice<PhoneAccountHandle> getSelfManagedPhoneAccounts( 394 String callingPackage, String callingFeatureId) { 395 try { 396 Log.startSession("TSI.gSMPA", Log.getPackageAbbreviation(callingPackage)); 397 if (!canReadPhoneState(callingPackage, callingFeatureId, 398 "Requires READ_PHONE_STATE permission.")) { 399 throw new SecurityException("Requires READ_PHONE_STATE permission."); 400 } 401 synchronized (mLock) { 402 final UserHandle callingUserHandle = Binder.getCallingUserHandle(); 403 long token = Binder.clearCallingIdentity(); 404 try { 405 return new ParceledListSlice<>(mPhoneAccountRegistrar 406 .getSelfManagedPhoneAccounts(callingUserHandle)); 407 } catch (Exception e) { 408 Log.e(this, e, "getSelfManagedPhoneAccounts"); 409 throw e; 410 } finally { 411 Binder.restoreCallingIdentity(token); 412 } 413 } 414 } finally { 415 Log.endSession(); 416 } 417 } 418 419 @Override 420 public ParceledListSlice<PhoneAccountHandle> getOwnSelfManagedPhoneAccounts( 421 String callingPackage, String callingFeatureId) { 422 try { 423 Log.startSession("TSI.gOSMPA", Log.getPackageAbbreviation(callingPackage)); 424 try { 425 enforceCallingPackage(callingPackage, "getOwnSelfManagedPhoneAccounts"); 426 } catch (SecurityException se) { 427 EventLog.writeEvent(0x534e4554, "231986341", Binder.getCallingUid(), 428 "getOwnSelfManagedPhoneAccounts: invalid calling package"); 429 throw se; 430 } 431 if (!canReadMangeOwnCalls("Requires MANAGE_OWN_CALLS permission.")) { 432 throw new SecurityException("Requires MANAGE_OWN_CALLS permission."); 433 } 434 synchronized (mLock) { 435 final UserHandle callingUserHandle = Binder.getCallingUserHandle(); 436 long token = Binder.clearCallingIdentity(); 437 try { 438 return new ParceledListSlice<>(mPhoneAccountRegistrar 439 .getSelfManagedPhoneAccountsForPackage(callingPackage, 440 callingUserHandle)); 441 } catch (Exception e) { 442 Log.e(this, e, 443 "getSelfManagedPhoneAccountsForPackage"); 444 throw e; 445 } finally { 446 Binder.restoreCallingIdentity(token); 447 } 448 } 449 } finally { 450 Log.endSession(); 451 } 452 } 453 454 @Override 455 public ParceledListSlice<PhoneAccountHandle> getPhoneAccountsSupportingScheme( 456 String uriScheme, String callingPackage) { 457 try { 458 Log.startSession("TSI.gPASS", Log.getPackageAbbreviation(callingPackage)); 459 try { 460 enforceModifyPermission( 461 "getPhoneAccountsSupportingScheme requires MODIFY_PHONE_STATE"); 462 } catch (SecurityException e) { 463 EventLog.writeEvent(0x534e4554, "62347125", Binder.getCallingUid(), 464 "getPhoneAccountsSupportingScheme: " + callingPackage); 465 return ParceledListSlice.emptyList(); 466 } 467 468 synchronized (mLock) { 469 final UserHandle callingUserHandle = Binder.getCallingUserHandle(); 470 long token = Binder.clearCallingIdentity(); 471 try { 472 return new ParceledListSlice<>(mPhoneAccountRegistrar 473 .getCallCapablePhoneAccounts(uriScheme, false, 474 callingUserHandle, false)); 475 } catch (Exception e) { 476 Log.e(this, e, "getPhoneAccountsSupportingScheme %s", uriScheme); 477 throw e; 478 } finally { 479 Binder.restoreCallingIdentity(token); 480 } 481 } 482 } finally { 483 Log.endSession(); 484 } 485 } 486 487 @Override 488 public ParceledListSlice<PhoneAccountHandle> getPhoneAccountsForPackage( 489 String packageName) { 490 //TODO: Deprecate this in S 491 try { 492 enforceCallingPackage(packageName, "getPhoneAccountsForPackage"); 493 } catch (SecurityException se1) { 494 EventLog.writeEvent(0x534e4554, "153995334", Binder.getCallingUid(), 495 "getPhoneAccountsForPackage: invalid calling package"); 496 throw se1; 497 } 498 499 try { 500 enforcePermission(READ_PRIVILEGED_PHONE_STATE); 501 } catch (SecurityException se2) { 502 EventLog.writeEvent(0x534e4554, "153995334", Binder.getCallingUid(), 503 "getPhoneAccountsForPackage: no permission"); 504 throw se2; 505 } 506 507 synchronized (mLock) { 508 final UserHandle callingUserHandle = Binder.getCallingUserHandle(); 509 long token = Binder.clearCallingIdentity(); 510 try { 511 Log.startSession("TSI.gPAFP"); 512 return new ParceledListSlice<>(mPhoneAccountRegistrar 513 .getAllPhoneAccountHandlesForPackage(callingUserHandle, packageName)); 514 } catch (Exception e) { 515 Log.e(this, e, "getPhoneAccountsForPackage %s", packageName); 516 throw e; 517 } finally { 518 Binder.restoreCallingIdentity(token); 519 Log.endSession(); 520 } 521 } 522 } 523 524 @Override 525 public PhoneAccount getPhoneAccount(PhoneAccountHandle accountHandle, 526 String callingPackage) { 527 try { 528 Log.startSession("TSI.gPA", Log.getPackageAbbreviation(callingPackage)); 529 try { 530 enforceCallingPackage(callingPackage, "getPhoneAccount"); 531 } catch (SecurityException se) { 532 EventLog.writeEvent(0x534e4554, "196406138", Binder.getCallingUid(), 533 "getPhoneAccount: invalid calling package"); 534 throw se; 535 } 536 synchronized (mLock) { 537 final UserHandle callingUserHandle = Binder.getCallingUserHandle(); 538 if (CompatChanges.isChangeEnabled( 539 TelecomManager.ENABLE_GET_PHONE_ACCOUNT_PERMISSION_PROTECTION, 540 callingPackage, Binder.getCallingUserHandle())) { 541 if (Binder.getCallingUid() != Process.SHELL_UID && 542 !canGetPhoneAccount(callingPackage, accountHandle)) { 543 SecurityException e = new SecurityException( 544 "getPhoneAccount API requires" + 545 "READ_PHONE_NUMBERS"); 546 Log.e(this, e, "getPhoneAccount %s", accountHandle); 547 throw e; 548 } 549 } 550 Set<String> permissions = computePermissionsForBoundPackage( 551 Set.of(MODIFY_PHONE_STATE), null); 552 long token = Binder.clearCallingIdentity(); 553 try { 554 // In ideal case, we should not resolve the handle across profiles. But 555 // given the fact that profile's call is handled by its parent user's 556 // in-call UI, parent user's in call UI need to be able to get phone account 557 // from the profile's phone account handle. 558 PhoneAccount account = mPhoneAccountRegistrar 559 .getPhoneAccount(accountHandle, callingUserHandle, 560 /* acrossProfiles */ true); 561 return maybeCleansePhoneAccount(account, permissions); 562 } catch (Exception e) { 563 Log.e(this, e, "getPhoneAccount %s", accountHandle); 564 mAnomalyReporter.reportAnomaly(GET_PHONE_ACCOUNT_ERROR_UUID, 565 GET_PHONE_ACCOUNT_ERROR_MSG); 566 throw e; 567 } finally { 568 Binder.restoreCallingIdentity(token); 569 } 570 } 571 } finally { 572 Log.endSession(); 573 } 574 } 575 576 @Override 577 public int getAllPhoneAccountsCount() { 578 try { 579 Log.startSession("TSI.gAPAC"); 580 try { 581 enforceModifyPermission( 582 "getAllPhoneAccountsCount requires MODIFY_PHONE_STATE permission."); 583 } catch (SecurityException e) { 584 EventLog.writeEvent(0x534e4554, "62347125", Binder.getCallingUid(), 585 "getAllPhoneAccountsCount"); 586 throw e; 587 } 588 589 synchronized (mLock) { 590 try { 591 // This list is pre-filtered for the calling user. 592 return getAllPhoneAccounts().getList().size(); 593 } catch (Exception e) { 594 Log.e(this, e, "getAllPhoneAccountsCount"); 595 throw e; 596 597 } 598 } 599 } finally { 600 Log.endSession(); 601 } 602 } 603 604 @Override 605 public ParceledListSlice<PhoneAccount> getAllPhoneAccounts() { 606 synchronized (mLock) { 607 try { 608 Log.startSession("TSI.gAPA"); 609 try { 610 enforceModifyPermission( 611 "getAllPhoneAccounts requires MODIFY_PHONE_STATE permission."); 612 } catch (SecurityException e) { 613 EventLog.writeEvent(0x534e4554, "62347125", Binder.getCallingUid(), 614 "getAllPhoneAccounts"); 615 throw e; 616 } 617 618 final UserHandle callingUserHandle = Binder.getCallingUserHandle(); 619 long token = Binder.clearCallingIdentity(); 620 try { 621 return new ParceledListSlice<>(mPhoneAccountRegistrar 622 .getAllPhoneAccounts(callingUserHandle, false)); 623 } catch (Exception e) { 624 Log.e(this, e, "getAllPhoneAccounts"); 625 throw e; 626 } finally { 627 Binder.restoreCallingIdentity(token); 628 } 629 } finally { 630 Log.endSession(); 631 } 632 } 633 } 634 635 @Override 636 public ParceledListSlice<PhoneAccountHandle> getAllPhoneAccountHandles() { 637 try { 638 Log.startSession("TSI.gAPAH"); 639 try { 640 enforceModifyPermission( 641 "getAllPhoneAccountHandles requires MODIFY_PHONE_STATE permission."); 642 } catch (SecurityException e) { 643 EventLog.writeEvent(0x534e4554, "62347125", Binder.getCallingUid(), 644 "getAllPhoneAccountHandles"); 645 throw e; 646 } 647 648 synchronized (mLock) { 649 final UserHandle callingUserHandle = Binder.getCallingUserHandle(); 650 boolean crossUserAccess = hasInAppCrossUserPermission(); 651 long token = Binder.clearCallingIdentity(); 652 try { 653 return new ParceledListSlice<>(mPhoneAccountRegistrar 654 .getAllPhoneAccountHandles(callingUserHandle, 655 crossUserAccess)); 656 } catch (Exception e) { 657 Log.e(this, e, "getAllPhoneAccounts"); 658 throw e; 659 } finally { 660 Binder.restoreCallingIdentity(token); 661 } 662 } 663 } finally { 664 Log.endSession(); 665 } 666 } 667 668 @Override 669 public PhoneAccountHandle getSimCallManager(int subId, String callingPackage) { 670 synchronized (mLock) { 671 try { 672 Log.startSession("TSI.gSCM", Log.getPackageAbbreviation(callingPackage)); 673 final int callingUid = Binder.getCallingUid(); 674 final int user = UserHandle.getUserId(callingUid); 675 long token = Binder.clearCallingIdentity(); 676 try { 677 if (user != ActivityManager.getCurrentUser()) { 678 enforceCrossUserPermission(callingUid); 679 } 680 return mPhoneAccountRegistrar.getSimCallManager(subId, UserHandle.of(user)); 681 } finally { 682 Binder.restoreCallingIdentity(token); 683 } 684 } catch (Exception e) { 685 Log.e(this, e, "getSimCallManager"); 686 mAnomalyReporter.reportAnomaly(GET_SIM_MANAGER_ERROR_UUID, 687 GET_SIM_MANAGER_ERROR_MSG); 688 throw e; 689 } finally { 690 Log.endSession(); 691 } 692 } 693 } 694 695 @Override 696 public PhoneAccountHandle getSimCallManagerForUser(int user, String callingPackage) { 697 synchronized (mLock) { 698 try { 699 Log.startSession("TSI.gSCMFU", Log.getPackageAbbreviation(callingPackage)); 700 final int callingUid = Binder.getCallingUid(); 701 if (user != ActivityManager.getCurrentUser()) { 702 enforceCrossUserPermission(callingUid); 703 } 704 long token = Binder.clearCallingIdentity(); 705 try { 706 return mPhoneAccountRegistrar.getSimCallManager(UserHandle.of(user)); 707 } finally { 708 Binder.restoreCallingIdentity(token); 709 } 710 } catch (Exception e) { 711 Log.e(this, e, "getSimCallManager"); 712 mAnomalyReporter.reportAnomaly(GET_SIM_MANAGER_FOR_USER_ERROR_UUID, 713 GET_SIM_MANAGER_FOR_USER_ERROR_MSG); 714 throw e; 715 } finally { 716 Log.endSession(); 717 } 718 } 719 } 720 721 @Override 722 public void registerPhoneAccount(PhoneAccount account, String callingPackage) { 723 try { 724 Log.startSession("TSI.rPA", Log.getPackageAbbreviation(callingPackage)); 725 synchronized (mLock) { 726 try { 727 enforcePhoneAccountModificationForPackage( 728 account.getAccountHandle().getComponentName().getPackageName()); 729 if (account.hasCapabilities(PhoneAccount.CAPABILITY_SELF_MANAGED)) { 730 enforceRegisterSelfManaged(); 731 if (account.hasCapabilities(PhoneAccount.CAPABILITY_CALL_PROVIDER) || 732 account.hasCapabilities( 733 PhoneAccount.CAPABILITY_CONNECTION_MANAGER) || 734 account.hasCapabilities( 735 PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION)) { 736 throw new SecurityException("Self-managed ConnectionServices " + 737 "cannot also be call capable, connection managers, or " + 738 "SIM accounts."); 739 } 740 741 // For self-managed CS, the phone account registrar will override the 742 // label the user has set for the phone account. This ensures the 743 // self-managed cs implementation can't spoof their app name. 744 } 745 if (account.hasCapabilities(PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION)) { 746 enforceRegisterSimSubscriptionPermission(); 747 } 748 if (account.hasCapabilities(PhoneAccount.CAPABILITY_MULTI_USER)) { 749 enforceRegisterMultiUser(); 750 } 751 // These capabilities are for SIM-based accounts only, so only the platform 752 // and carrier-designated SIM call manager can register accounts with these 753 // capabilities. 754 if (account.hasCapabilities( 755 PhoneAccount.CAPABILITY_SUPPORTS_VOICE_CALLING_INDICATIONS) 756 || account.hasCapabilities( 757 PhoneAccount.CAPABILITY_VOICE_CALLING_AVAILABLE)) { 758 enforceRegisterVoiceCallingIndicationCapabilities(account); 759 } 760 Bundle extras = account.getExtras(); 761 if (extras != null 762 && extras.getBoolean(PhoneAccount.EXTRA_SKIP_CALL_FILTERING)) { 763 enforceRegisterSkipCallFiltering(); 764 } 765 final int callingUid = Binder.getCallingUid(); 766 if (callingUid != Process.SHELL_UID) { 767 enforceUserHandleMatchesCaller(account.getAccountHandle()); 768 } 769 770 if (TextUtils.isEmpty(account.getGroupId()) 771 && mContext.checkCallingOrSelfPermission(MODIFY_PHONE_STATE) 772 != PackageManager.PERMISSION_GRANTED) { 773 Log.w(this, "registerPhoneAccount - attempt to set a" 774 + " group from a non-system caller."); 775 // Not permitted to set group, so null it out. 776 account = new PhoneAccount.Builder(account) 777 .setGroupId(null) 778 .build(); 779 } 780 781 // Validate the profile boundary of the given image URI. 782 validateAccountIconUserBoundary(account.getIcon()); 783 784 final long token = Binder.clearCallingIdentity(); 785 try { 786 Log.i(this, "registerPhoneAccount: account=%s", 787 account); 788 mPhoneAccountRegistrar.registerPhoneAccount(account); 789 } finally { 790 Binder.restoreCallingIdentity(token); 791 } 792 } catch (Exception e) { 793 Log.e(this, e, "registerPhoneAccount %s", account); 794 mAnomalyReporter.reportAnomaly(REGISTER_PHONE_ACCOUNT_ERROR_UUID, 795 REGISTER_PHONE_ACCOUNT_ERROR_MSG); 796 throw e; 797 } 798 } 799 } finally { 800 Log.endSession(); 801 } 802 } 803 804 @Override 805 public void unregisterPhoneAccount(PhoneAccountHandle accountHandle, 806 String callingPackage) { 807 synchronized (mLock) { 808 try { 809 Log.startSession("TSI.uPA", Log.getPackageAbbreviation(callingPackage)); 810 enforcePhoneAccountModificationForPackage( 811 accountHandle.getComponentName().getPackageName()); 812 enforceUserHandleMatchesCaller(accountHandle); 813 final long token = Binder.clearCallingIdentity(); 814 try { 815 mPhoneAccountRegistrar.unregisterPhoneAccount(accountHandle); 816 } finally { 817 Binder.restoreCallingIdentity(token); 818 } 819 } catch (Exception e) { 820 Log.e(this, e, "unregisterPhoneAccount %s", accountHandle); 821 throw e; 822 } finally { 823 Log.endSession(); 824 } 825 } 826 } 827 828 @Override 829 public void clearAccounts(String packageName) { 830 synchronized (mLock) { 831 try { 832 Log.startSession("TSI.cA"); 833 enforcePhoneAccountModificationForPackage(packageName); 834 mPhoneAccountRegistrar 835 .clearAccounts(packageName, Binder.getCallingUserHandle()); 836 } catch (Exception e) { 837 Log.e(this, e, "clearAccounts %s", packageName); 838 throw e; 839 } finally { 840 Log.endSession(); 841 } 842 } 843 } 844 845 /** 846 * @see android.telecom.TelecomManager#isVoiceMailNumber 847 */ 848 @Override 849 public boolean isVoiceMailNumber(PhoneAccountHandle accountHandle, String number, 850 String callingPackage, String callingFeatureId) { 851 try { 852 Log.startSession("TSI.iVMN", Log.getPackageAbbreviation(callingPackage)); 853 synchronized (mLock) { 854 if (!canReadPhoneState(callingPackage, callingFeatureId, "isVoiceMailNumber")) { 855 return false; 856 } 857 final UserHandle callingUserHandle = Binder.getCallingUserHandle(); 858 if (!isPhoneAccountHandleVisibleToCallingUser(accountHandle, 859 callingUserHandle)) { 860 Log.d(this, "%s is not visible for the calling user [iVMN]", accountHandle); 861 return false; 862 } 863 long token = Binder.clearCallingIdentity(); 864 try { 865 return mPhoneAccountRegistrar.isVoiceMailNumber(accountHandle, number); 866 } catch (Exception e) { 867 Log.e(this, e, "getSubscriptionIdForPhoneAccount"); 868 throw e; 869 } finally { 870 Binder.restoreCallingIdentity(token); 871 } 872 } 873 } finally { 874 Log.endSession(); 875 } 876 } 877 878 /** 879 * @see android.telecom.TelecomManager#getVoiceMailNumber 880 */ 881 @Override 882 public String getVoiceMailNumber(PhoneAccountHandle accountHandle, String callingPackage, 883 String callingFeatureId) { 884 try { 885 Log.startSession("TSI.gVMN", Log.getPackageAbbreviation(callingPackage)); 886 if (!canReadPhoneState(callingPackage, callingFeatureId, "getVoiceMailNumber")) { 887 return null; 888 } 889 try { 890 final UserHandle callingUserHandle = Binder.getCallingUserHandle(); 891 if (!isPhoneAccountHandleVisibleToCallingUser(accountHandle, 892 callingUserHandle)) { 893 Log.d(this, "%s is not visible for the calling user [gVMN]", 894 accountHandle); 895 return null; 896 } 897 int subId = mSubscriptionManagerAdapter.getDefaultVoiceSubId(); 898 synchronized (mLock) { 899 if (accountHandle != null) { 900 subId = mPhoneAccountRegistrar 901 .getSubscriptionIdForPhoneAccount(accountHandle); 902 } 903 } 904 return getTelephonyManager(subId).getVoiceMailNumber(); 905 } catch (Exception e) { 906 Log.e(this, e, "getSubscriptionIdForPhoneAccount"); 907 throw e; 908 } 909 } finally { 910 Log.endSession(); 911 } 912 } 913 914 /** 915 * @see android.telecom.TelecomManager#getLine1Number 916 */ 917 @Override 918 public String getLine1Number(PhoneAccountHandle accountHandle, String callingPackage, 919 String callingFeatureId) { 920 try { 921 Log.startSession("getL1N", Log.getPackageAbbreviation(callingPackage)); 922 if (!canReadPhoneNumbers(callingPackage, callingFeatureId, "getLine1Number")) { 923 return null; 924 } 925 926 final UserHandle callingUserHandle = Binder.getCallingUserHandle(); 927 if (!isPhoneAccountHandleVisibleToCallingUser(accountHandle, 928 callingUserHandle)) { 929 Log.d(this, "%s is not visible for the calling user [gL1N]", accountHandle); 930 return null; 931 } 932 933 long token = Binder.clearCallingIdentity(); 934 try { 935 int subId; 936 synchronized (mLock) { 937 subId = mPhoneAccountRegistrar.getSubscriptionIdForPhoneAccount( 938 accountHandle); 939 } 940 return getTelephonyManager(subId).getLine1Number(); 941 } catch (Exception e) { 942 Log.e(this, e, "getSubscriptionIdForPhoneAccount"); 943 throw e; 944 } finally { 945 Binder.restoreCallingIdentity(token); 946 } 947 } finally { 948 Log.endSession(); 949 } 950 } 951 952 /** 953 * @see android.telecom.TelecomManager#silenceRinger 954 */ 955 @Override 956 public void silenceRinger(String callingPackage) { 957 try { 958 Log.startSession("TSI.sR", Log.getPackageAbbreviation(callingPackage)); 959 synchronized (mLock) { 960 enforcePermissionOrPrivilegedDialer(MODIFY_PHONE_STATE, callingPackage); 961 UserHandle callingUserHandle = Binder.getCallingUserHandle(); 962 boolean crossUserAccess = hasInAppCrossUserPermission(); 963 long token = Binder.clearCallingIdentity(); 964 try { 965 Log.i(this, "Silence Ringer requested by %s", callingPackage); 966 Set<UserHandle> userHandles = mCallsManager.getCallAudioManager(). 967 silenceRingers(mContext, callingUserHandle, 968 crossUserAccess); 969 mCallsManager.getInCallController().silenceRinger(userHandles); 970 } finally { 971 Binder.restoreCallingIdentity(token); 972 } 973 } 974 } finally { 975 Log.endSession(); 976 } 977 } 978 979 /** 980 * @see android.telecom.TelecomManager#getDefaultPhoneApp 981 * @deprecated - Use {@link android.telecom.TelecomManager#getDefaultDialerPackage()} 982 * instead. 983 */ 984 @Override 985 public ComponentName getDefaultPhoneApp() { 986 try { 987 Log.startSession("TSI.gDPA"); 988 return mDefaultDialerCache.getDialtactsSystemDialerComponent(); 989 } finally { 990 Log.endSession(); 991 } 992 } 993 994 /** 995 * @return the package name of the current user-selected default dialer. If no default 996 * has been selected, the package name of the system dialer is returned. If 997 * neither exists, then {@code null} is returned. 998 * @see android.telecom.TelecomManager#getDefaultDialerPackage 999 */ 1000 @Override 1001 public String getDefaultDialerPackage(String callingPackage) { 1002 try { 1003 Log.startSession("TSI.gDDP", Log.getPackageAbbreviation(callingPackage)); 1004 int callerUserId = UserHandle.getCallingUserId(); 1005 final long token = Binder.clearCallingIdentity(); 1006 try { 1007 return mDefaultDialerCache.getDefaultDialerApplication( 1008 callerUserId); 1009 } finally { 1010 Binder.restoreCallingIdentity(token); 1011 } 1012 } finally { 1013 Log.endSession(); 1014 } 1015 } 1016 1017 /** 1018 * @param userId user id to get the default dialer package for 1019 * @return the package name of the current user-selected default dialer. If no default 1020 * has been selected, the package name of the system dialer is returned. If 1021 * neither exists, then {@code null} is returned. 1022 * @see android.telecom.TelecomManager#getDefaultDialerPackage 1023 */ 1024 @Override 1025 public String getDefaultDialerPackageForUser(int userId) { 1026 try { 1027 Log.startSession("TSI.gDDPU"); 1028 mContext.enforceCallingOrSelfPermission(READ_PRIVILEGED_PHONE_STATE, 1029 "READ_PRIVILEGED_PHONE_STATE permission required."); 1030 1031 final long token = Binder.clearCallingIdentity(); 1032 try { 1033 return mDefaultDialerCache.getDefaultDialerApplication(userId); 1034 } finally { 1035 Binder.restoreCallingIdentity(token); 1036 } 1037 } finally { 1038 Log.endSession(); 1039 } 1040 } 1041 1042 /** 1043 * @see android.telecom.TelecomManager#getSystemDialerPackage 1044 */ 1045 @Override 1046 public String getSystemDialerPackage(String callingPackage) { 1047 try { 1048 Log.startSession("TSI.gSDP", Log.getPackageAbbreviation(callingPackage)); 1049 return mDefaultDialerCache.getSystemDialerApplication(); 1050 } finally { 1051 Log.endSession(); 1052 } 1053 } 1054 1055 public void setSystemDialer(ComponentName testComponentName) { 1056 try { 1057 Log.startSession("TSI.sSD"); 1058 enforceModifyPermission(); 1059 enforceShellOnly(Binder.getCallingUid(), "setSystemDialer"); 1060 synchronized (mLock) { 1061 long token = Binder.clearCallingIdentity(); 1062 try { 1063 mDefaultDialerCache.setSystemDialerComponentName(testComponentName); 1064 } finally { 1065 Binder.restoreCallingIdentity(token); 1066 } 1067 } 1068 } finally { 1069 Log.endSession(); 1070 } 1071 } 1072 1073 /** 1074 * @see android.telecom.TelecomManager#isInCall 1075 */ 1076 @Override 1077 public boolean isInCall(String callingPackage, String callingFeatureId) { 1078 try { 1079 Log.startSession("TSI.iIC", Log.getPackageAbbreviation(callingPackage)); 1080 if (!canReadPhoneState(callingPackage, callingFeatureId, "isInCall")) { 1081 return false; 1082 } 1083 1084 synchronized (mLock) { 1085 return mCallsManager.hasOngoingCalls(Binder.getCallingUserHandle(), 1086 hasInAppCrossUserPermission()); 1087 } 1088 } finally { 1089 Log.endSession(); 1090 } 1091 } 1092 1093 /** 1094 * @see android.telecom.TelecomManager#hasManageOngoingCallsPermission 1095 */ 1096 @Override 1097 public boolean hasManageOngoingCallsPermission(String callingPackage) { 1098 try { 1099 Log.startSession("TSI.hMOCP", Log.getPackageAbbreviation(callingPackage)); 1100 enforceCallingPackage(callingPackage, "hasManageOngoingCallsPermission"); 1101 return PermissionChecker.checkPermissionForDataDeliveryFromDataSource( 1102 mContext, Manifest.permission.MANAGE_ONGOING_CALLS, 1103 Binder.getCallingPid(), 1104 new AttributionSource(mContext.getAttributionSource(), 1105 new AttributionSource(Binder.getCallingUid(), 1106 callingPackage, /*attributionTag*/ null)), 1107 "Checking whether the caller has MANAGE_ONGOING_CALLS permission") 1108 == PermissionChecker.PERMISSION_GRANTED; 1109 } finally { 1110 Log.endSession(); 1111 } 1112 } 1113 1114 /** 1115 * @see android.telecom.TelecomManager#isInManagedCall 1116 */ 1117 @Override 1118 public boolean isInManagedCall(String callingPackage, String callingFeatureId) { 1119 try { 1120 Log.startSession("TSI.iIMC", Log.getPackageAbbreviation(callingPackage)); 1121 if (!canReadPhoneState(callingPackage, callingFeatureId, "isInManagedCall")) { 1122 throw new SecurityException("Only the default dialer or caller with " + 1123 "READ_PHONE_STATE permission can use this method."); 1124 } 1125 1126 synchronized (mLock) { 1127 return mCallsManager.hasOngoingManagedCalls(Binder.getCallingUserHandle(), 1128 hasInAppCrossUserPermission()); 1129 } 1130 } finally { 1131 Log.endSession(); 1132 } 1133 } 1134 1135 /** 1136 * @see android.telecom.TelecomManager#isRinging 1137 */ 1138 @Override 1139 public boolean isRinging(String callingPackage) { 1140 try { 1141 Log.startSession("TSI.iR"); 1142 if (!isPrivilegedDialerCalling(callingPackage)) { 1143 try { 1144 enforceModifyPermission( 1145 "isRinging requires MODIFY_PHONE_STATE permission."); 1146 } catch (SecurityException e) { 1147 EventLog.writeEvent(0x534e4554, "62347125", "isRinging: " + callingPackage); 1148 throw e; 1149 } 1150 } 1151 1152 synchronized (mLock) { 1153 // Note: We are explicitly checking the calls telecom is tracking rather than 1154 // relying on mCallsManager#getCallState(). Since getCallState() relies on the 1155 // current state as tracked by PhoneStateBroadcaster, any failure to properly 1156 // track the current call state there could result in the wrong ringing state 1157 // being reported by this API. 1158 return mCallsManager.hasRingingOrSimulatedRingingCall(); 1159 } 1160 } finally { 1161 Log.endSession(); 1162 } 1163 } 1164 1165 /** 1166 * @see TelecomManager#getCallState() 1167 * @deprecated this is only being kept due to an @UnsupportedAppUsage tag. Apps targeting 1168 * API 31+ must use {@link #getCallStateUsingPackage(String, String)} below. 1169 */ 1170 @Deprecated 1171 @Override 1172 public int getCallState() { 1173 try { 1174 Log.startSession("TSI.getCallState(DEPRECATED)"); 1175 if (CompatChanges.isChangeEnabled( 1176 TelecomManager.ENABLE_GET_CALL_STATE_PERMISSION_PROTECTION, 1177 Binder.getCallingUid())) { 1178 // Do not allow this API to be called on API version 31+, it should only be 1179 // called on old apps using this Binder call directly. 1180 throw new SecurityException("This method can only be used for applications " 1181 + "targeting API version 30 or less."); 1182 } 1183 synchronized (mLock) { 1184 return mCallsManager.getCallState(); 1185 } 1186 } finally { 1187 Log.endSession(); 1188 } 1189 } 1190 1191 /** 1192 * @see TelecomManager#getCallState() 1193 */ 1194 @Override 1195 public int getCallStateUsingPackage(String callingPackage, String callingFeatureId) { 1196 try { 1197 Log.startSession("TSI.getCallStateUsingPackage"); 1198 1199 // ensure the callingPackage is not spoofed 1200 // skip check for privileged UIDs and throw SE if package does not match records 1201 if (!isPrivilegedUid(callingPackage) 1202 && !callingUidMatchesPackageManagerRecords(callingPackage)) { 1203 EventLog.writeEvent(0x534e4554, "236813210", Binder.getCallingUid(), 1204 "getCallStateUsingPackage"); 1205 Log.i(this, 1206 "getCallStateUsingPackage: packageName does not match records for " 1207 + "callingPackage=[%s], callingUid=[%d]", 1208 callingPackage, Binder.getCallingUid()); 1209 throw new SecurityException(String.format("getCallStateUsingPackage: " 1210 + "enforceCallingPackage: callingPackage=[%s], callingUid=[%d]", 1211 callingPackage, Binder.getCallingUid())); 1212 } 1213 1214 if (CompatChanges.isChangeEnabled( 1215 TelecomManager.ENABLE_GET_CALL_STATE_PERMISSION_PROTECTION, callingPackage, 1216 Binder.getCallingUserHandle())) { 1217 // Bypass canReadPhoneState check if this is being called from SHELL UID 1218 if (Binder.getCallingUid() != Process.SHELL_UID && !canReadPhoneState( 1219 callingPackage, callingFeatureId, "getCallState")) { 1220 throw new SecurityException("getCallState API requires READ_PHONE_STATE" 1221 + " for API version 31+"); 1222 } 1223 } 1224 synchronized (mLock) { 1225 return mCallsManager.getCallState(); 1226 } 1227 } finally { 1228 Log.endSession(); 1229 } 1230 } 1231 1232 private boolean isPrivilegedUid(String callingPackage) { 1233 int callingUid = Binder.getCallingUid(); 1234 boolean isPrivileged = false; 1235 switch (callingUid) { 1236 case Process.ROOT_UID: 1237 case Process.SYSTEM_UID: 1238 case Process.SHELL_UID: 1239 isPrivileged = true; 1240 break; 1241 } 1242 return isPrivileged; 1243 } 1244 1245 /** 1246 * @see android.telecom.TelecomManager#endCall 1247 */ 1248 @Override 1249 public boolean endCall(String callingPackage) { 1250 try { 1251 Log.startSession("TSI.eC", Log.getPackageAbbreviation(callingPackage)); 1252 synchronized (mLock) { 1253 if (!enforceAnswerCallPermission(callingPackage, Binder.getCallingUid())) { 1254 throw new SecurityException("requires ANSWER_PHONE_CALLS permission"); 1255 } 1256 1257 long token = Binder.clearCallingIdentity(); 1258 try { 1259 return endCallInternal(callingPackage); 1260 } finally { 1261 Binder.restoreCallingIdentity(token); 1262 } 1263 } 1264 } finally { 1265 Log.endSession(); 1266 } 1267 } 1268 1269 /** 1270 * @see android.telecom.TelecomManager#acceptRingingCall 1271 */ 1272 @Override 1273 public void acceptRingingCall(String packageName) { 1274 try { 1275 Log.startSession("TSI.aRC", Log.getPackageAbbreviation(packageName)); 1276 synchronized (mLock) { 1277 if (!enforceAnswerCallPermission(packageName, Binder.getCallingUid())) return; 1278 1279 long token = Binder.clearCallingIdentity(); 1280 try { 1281 acceptRingingCallInternal(DEFAULT_VIDEO_STATE, packageName); 1282 } finally { 1283 Binder.restoreCallingIdentity(token); 1284 } 1285 } 1286 } finally { 1287 Log.endSession(); 1288 } 1289 } 1290 1291 /** 1292 * @see android.telecom.TelecomManager#acceptRingingCall(int) 1293 */ 1294 @Override 1295 public void acceptRingingCallWithVideoState(String packageName, int videoState) { 1296 try { 1297 Log.startSession("TSI.aRCWVS", Log.getPackageAbbreviation(packageName)); 1298 synchronized (mLock) { 1299 if (!enforceAnswerCallPermission(packageName, Binder.getCallingUid())) return; 1300 1301 long token = Binder.clearCallingIdentity(); 1302 try { 1303 acceptRingingCallInternal(videoState, packageName); 1304 } finally { 1305 Binder.restoreCallingIdentity(token); 1306 } 1307 } 1308 } finally { 1309 Log.endSession(); 1310 } 1311 } 1312 1313 /** 1314 * @see android.telecom.TelecomManager#showInCallScreen 1315 */ 1316 @Override 1317 public void showInCallScreen(boolean showDialpad, String callingPackage, 1318 String callingFeatureId) { 1319 try { 1320 Log.startSession("TSI.sICS", Log.getPackageAbbreviation(callingPackage)); 1321 if (!canReadPhoneState(callingPackage, callingFeatureId, "showInCallScreen")) { 1322 return; 1323 } 1324 1325 synchronized (mLock) { 1326 1327 UserHandle callingUser = Binder.getCallingUserHandle(); 1328 long token = Binder.clearCallingIdentity(); 1329 try { 1330 mCallsManager.getInCallController().bringToForeground(showDialpad, callingUser); 1331 } finally { 1332 Binder.restoreCallingIdentity(token); 1333 } 1334 } 1335 } finally { 1336 Log.endSession(); 1337 } 1338 } 1339 1340 /** 1341 * @see android.telecom.TelecomManager#cancelMissedCallsNotification 1342 */ 1343 @Override 1344 public void cancelMissedCallsNotification(String callingPackage) { 1345 try { 1346 Log.startSession("TSI.cMCN", Log.getPackageAbbreviation(callingPackage)); 1347 synchronized (mLock) { 1348 enforcePermissionOrPrivilegedDialer(MODIFY_PHONE_STATE, callingPackage); 1349 UserHandle userHandle = Binder.getCallingUserHandle(); 1350 long token = Binder.clearCallingIdentity(); 1351 try { 1352 mCallsManager.getMissedCallNotifier().clearMissedCalls(userHandle); 1353 } finally { 1354 Binder.restoreCallingIdentity(token); 1355 } 1356 } 1357 } finally { 1358 Log.endSession(); 1359 } 1360 } 1361 1362 /** 1363 * @see android.telecom.TelecomManager#handleMmi 1364 */ 1365 @Override 1366 public boolean handlePinMmi(String dialString, String callingPackage) { 1367 try { 1368 Log.startSession("TSI.hPM", Log.getPackageAbbreviation(callingPackage)); 1369 enforcePermissionOrPrivilegedDialer(MODIFY_PHONE_STATE, callingPackage); 1370 1371 // Switch identity so that TelephonyManager checks Telecom's permissions 1372 // instead. 1373 long token = Binder.clearCallingIdentity(); 1374 boolean retval = false; 1375 try { 1376 retval = getTelephonyManager( 1377 SubscriptionManager.getDefaultVoiceSubscriptionId()) 1378 .handlePinMmi(dialString); 1379 } finally { 1380 Binder.restoreCallingIdentity(token); 1381 } 1382 1383 return retval; 1384 } finally { 1385 Log.endSession(); 1386 } 1387 } 1388 1389 /** 1390 * @see android.telecom.TelecomManager#handleMmi 1391 */ 1392 @Override 1393 public boolean handlePinMmiForPhoneAccount(PhoneAccountHandle accountHandle, 1394 String dialString, String callingPackage) { 1395 try { 1396 Log.startSession("TSI.hPMFPA", Log.getPackageAbbreviation(callingPackage)); 1397 1398 enforcePermissionOrPrivilegedDialer(MODIFY_PHONE_STATE, callingPackage); 1399 UserHandle callingUserHandle = Binder.getCallingUserHandle(); 1400 synchronized (mLock) { 1401 if (!isPhoneAccountHandleVisibleToCallingUser(accountHandle, 1402 callingUserHandle)) { 1403 Log.d(this, "%s is not visible for the calling user [hMMI]", 1404 accountHandle); 1405 return false; 1406 } 1407 } 1408 1409 // Switch identity so that TelephonyManager checks Telecom's permissions 1410 // instead. 1411 long token = Binder.clearCallingIdentity(); 1412 boolean retval = false; 1413 int subId; 1414 try { 1415 synchronized (mLock) { 1416 subId = mPhoneAccountRegistrar.getSubscriptionIdForPhoneAccount( 1417 accountHandle); 1418 } 1419 retval = getTelephonyManager(subId) 1420 .handlePinMmiForSubscriber(subId, dialString); 1421 } finally { 1422 Binder.restoreCallingIdentity(token); 1423 } 1424 return retval; 1425 } finally { 1426 Log.endSession(); 1427 } 1428 } 1429 1430 /** 1431 * @see android.telecom.TelecomManager#getAdnUriForPhoneAccount 1432 */ 1433 @Override 1434 public Uri getAdnUriForPhoneAccount(PhoneAccountHandle accountHandle, 1435 String callingPackage) { 1436 try { 1437 Log.startSession("TSI.aAUFPA", Log.getPackageAbbreviation(callingPackage)); 1438 enforcePermissionOrPrivilegedDialer(MODIFY_PHONE_STATE, callingPackage); 1439 synchronized (mLock) { 1440 if (!isPhoneAccountHandleVisibleToCallingUser(accountHandle, 1441 Binder.getCallingUserHandle())) { 1442 Log.d(this, "%s is not visible for the calling user [gA4PA]", 1443 accountHandle); 1444 return null; 1445 } 1446 } 1447 // Switch identity so that TelephonyManager checks Telecom's permissions 1448 // instead. 1449 long token = Binder.clearCallingIdentity(); 1450 String retval = "content://icc/adn/"; 1451 try { 1452 long subId = mPhoneAccountRegistrar 1453 .getSubscriptionIdForPhoneAccount(accountHandle); 1454 retval = retval + "subId/" + subId; 1455 } finally { 1456 Binder.restoreCallingIdentity(token); 1457 } 1458 1459 return Uri.parse(retval); 1460 } finally { 1461 Log.endSession(); 1462 } 1463 } 1464 1465 /** 1466 * @see android.telecom.TelecomManager#isTtySupported 1467 */ 1468 @Override 1469 public boolean isTtySupported(String callingPackage, String callingFeatureId) { 1470 try { 1471 Log.startSession("TSI.iTS", Log.getPackageAbbreviation(callingPackage)); 1472 if (!canReadPhoneState(callingPackage, callingFeatureId, "isTtySupported")) { 1473 throw new SecurityException("Only default dialer or an app with" + 1474 "READ_PRIVILEGED_PHONE_STATE or READ_PHONE_STATE can call this api"); 1475 } 1476 1477 synchronized (mLock) { 1478 return mCallsManager.isTtySupported(); 1479 } 1480 } finally { 1481 Log.endSession(); 1482 } 1483 } 1484 1485 /** 1486 * @see android.telecom.TelecomManager#getCurrentTtyMode 1487 */ 1488 @Override 1489 public int getCurrentTtyMode(String callingPackage, String callingFeatureId) { 1490 try { 1491 Log.startSession("TSI.gCTM", Log.getPackageAbbreviation(callingPackage)); 1492 if (!canReadPhoneState(callingPackage, callingFeatureId, "getCurrentTtyMode")) { 1493 return TelecomManager.TTY_MODE_OFF; 1494 } 1495 1496 synchronized (mLock) { 1497 return mCallsManager.getCurrentTtyMode(); 1498 } 1499 } finally { 1500 Log.endSession(); 1501 } 1502 } 1503 1504 /** 1505 * @see android.telecom.TelecomManager#addNewIncomingCall 1506 */ 1507 @Override 1508 public void addNewIncomingCall(PhoneAccountHandle phoneAccountHandle, Bundle extras, 1509 String callingPackage) { 1510 try { 1511 Log.startSession("TSI.aNIC", Log.getPackageAbbreviation(callingPackage)); 1512 synchronized (mLock) { 1513 Log.i(this, "Adding new incoming call with phoneAccountHandle %s", 1514 phoneAccountHandle); 1515 if (phoneAccountHandle != null && 1516 phoneAccountHandle.getComponentName() != null) { 1517 if (isCallerSimCallManager(phoneAccountHandle) 1518 && TelephonyUtil.isPstnComponentName( 1519 phoneAccountHandle.getComponentName())) { 1520 Log.v(this, "Allowing call manager to add incoming call with PSTN" + 1521 " handle"); 1522 } else { 1523 mAppOpsManager.checkPackage( 1524 Binder.getCallingUid(), 1525 phoneAccountHandle.getComponentName().getPackageName()); 1526 // Make sure it doesn't cross the UserHandle boundary 1527 enforceUserHandleMatchesCaller(phoneAccountHandle); 1528 enforcePhoneAccountIsRegisteredEnabled(phoneAccountHandle, 1529 phoneAccountHandle.getUserHandle()); 1530 if (isSelfManagedConnectionService(phoneAccountHandle)) { 1531 // Self-managed phone account, ensure it has MANAGE_OWN_CALLS. 1532 mContext.enforceCallingOrSelfPermission( 1533 android.Manifest.permission.MANAGE_OWN_CALLS, 1534 "Self-managed phone accounts must have MANAGE_OWN_CALLS " + 1535 "permission."); 1536 1537 // Self-managed ConnectionServices can ONLY add new incoming calls 1538 // using their own PhoneAccounts. The checkPackage(..) app opps 1539 // check above ensures this. 1540 } 1541 } 1542 long token = Binder.clearCallingIdentity(); 1543 try { 1544 Intent intent = new Intent(TelecomManager.ACTION_INCOMING_CALL); 1545 intent.putExtra(TelecomManager.EXTRA_PHONE_ACCOUNT_HANDLE, 1546 phoneAccountHandle); 1547 intent.putExtra(CallIntentProcessor.KEY_IS_INCOMING_CALL, true); 1548 if (extras != null) { 1549 extras.setDefusable(true); 1550 intent.putExtra(TelecomManager.EXTRA_INCOMING_CALL_EXTRAS, extras); 1551 } 1552 mCallIntentProcessorAdapter.processIncomingCallIntent( 1553 mCallsManager, intent); 1554 } finally { 1555 Binder.restoreCallingIdentity(token); 1556 } 1557 } else { 1558 Log.w(this, "Null phoneAccountHandle. Ignoring request to add new" + 1559 " incoming call"); 1560 } 1561 } 1562 } finally { 1563 Log.endSession(); 1564 } 1565 } 1566 1567 /** 1568 * @see android.telecom.TelecomManager#addNewIncomingConference 1569 */ 1570 @Override 1571 public void addNewIncomingConference(PhoneAccountHandle phoneAccountHandle, Bundle extras, 1572 String callingPackage) { 1573 try { 1574 Log.startSession("TSI.aNIC", Log.getPackageAbbreviation(callingPackage)); 1575 synchronized (mLock) { 1576 Log.i(this, "Adding new incoming conference with phoneAccountHandle %s", 1577 phoneAccountHandle); 1578 if (phoneAccountHandle != null && 1579 phoneAccountHandle.getComponentName() != null) { 1580 if (isCallerSimCallManager(phoneAccountHandle) 1581 && TelephonyUtil.isPstnComponentName( 1582 phoneAccountHandle.getComponentName())) { 1583 Log.v(this, "Allowing call manager to add incoming conference" + 1584 " with PSTN handle"); 1585 } else { 1586 mAppOpsManager.checkPackage( 1587 Binder.getCallingUid(), 1588 phoneAccountHandle.getComponentName().getPackageName()); 1589 // Make sure it doesn't cross the UserHandle boundary 1590 enforceUserHandleMatchesCaller(phoneAccountHandle); 1591 enforcePhoneAccountIsRegisteredEnabled(phoneAccountHandle, 1592 Binder.getCallingUserHandle()); 1593 if (isSelfManagedConnectionService(phoneAccountHandle)) { 1594 throw new SecurityException( 1595 "Self-Managed ConnectionServices cannot add " 1596 + "adhoc conference calls"); 1597 } 1598 } 1599 long token = Binder.clearCallingIdentity(); 1600 try { 1601 mCallsManager.processIncomingConference( 1602 phoneAccountHandle, extras); 1603 } finally { 1604 Binder.restoreCallingIdentity(token); 1605 } 1606 } else { 1607 Log.w(this, "Null phoneAccountHandle. Ignoring request to add new" + 1608 " incoming conference"); 1609 } 1610 } 1611 } finally { 1612 Log.endSession(); 1613 } 1614 } 1615 1616 1617 /** 1618 * @see android.telecom.TelecomManager#acceptHandover 1619 */ 1620 @Override 1621 public void acceptHandover(Uri srcAddr, int videoState, PhoneAccountHandle destAcct, 1622 String callingPackage) { 1623 try { 1624 Log.startSession("TSI.aHO", Log.getPackageAbbreviation(callingPackage)); 1625 synchronized (mLock) { 1626 Log.i(this, "acceptHandover; srcAddr=%s, videoState=%s, dest=%s", 1627 Log.pii(srcAddr), VideoProfile.videoStateToString(videoState), 1628 destAcct); 1629 1630 if (destAcct != null && destAcct.getComponentName() != null) { 1631 mAppOpsManager.checkPackage( 1632 Binder.getCallingUid(), 1633 destAcct.getComponentName().getPackageName()); 1634 enforceUserHandleMatchesCaller(destAcct); 1635 enforcePhoneAccountIsRegisteredEnabled(destAcct, 1636 Binder.getCallingUserHandle()); 1637 if (isSelfManagedConnectionService(destAcct)) { 1638 // Self-managed phone account, ensure it has MANAGE_OWN_CALLS. 1639 mContext.enforceCallingOrSelfPermission( 1640 android.Manifest.permission.MANAGE_OWN_CALLS, 1641 "Self-managed phone accounts must have MANAGE_OWN_CALLS " + 1642 "permission."); 1643 } 1644 if (!enforceAcceptHandoverPermission( 1645 destAcct.getComponentName().getPackageName(), 1646 Binder.getCallingUid())) { 1647 throw new SecurityException("App must be granted runtime " 1648 + "ACCEPT_HANDOVER permission."); 1649 } 1650 1651 long token = Binder.clearCallingIdentity(); 1652 try { 1653 mCallsManager.acceptHandover(srcAddr, videoState, destAcct); 1654 } finally { 1655 Binder.restoreCallingIdentity(token); 1656 } 1657 } else { 1658 Log.w(this, "Null phoneAccountHandle. Ignoring request " + 1659 "to handover the call"); 1660 } 1661 } 1662 } finally { 1663 Log.endSession(); 1664 } 1665 } 1666 1667 /** 1668 * @see android.telecom.TelecomManager#addNewUnknownCall 1669 */ 1670 @Override 1671 public void addNewUnknownCall(PhoneAccountHandle phoneAccountHandle, Bundle extras) { 1672 try { 1673 Log.startSession("TSI.aNUC"); 1674 try { 1675 enforceModifyPermission( 1676 "addNewUnknownCall requires MODIFY_PHONE_STATE permission."); 1677 } catch (SecurityException e) { 1678 EventLog.writeEvent(0x534e4554, "62347125", Binder.getCallingUid(), 1679 "addNewUnknownCall"); 1680 throw e; 1681 } 1682 1683 synchronized (mLock) { 1684 if (phoneAccountHandle != null && 1685 phoneAccountHandle.getComponentName() != null) { 1686 mAppOpsManager.checkPackage( 1687 Binder.getCallingUid(), 1688 phoneAccountHandle.getComponentName().getPackageName()); 1689 1690 // Make sure it doesn't cross the UserHandle boundary 1691 enforceUserHandleMatchesCaller(phoneAccountHandle); 1692 enforcePhoneAccountIsRegisteredEnabled(phoneAccountHandle, 1693 Binder.getCallingUserHandle()); 1694 long token = Binder.clearCallingIdentity(); 1695 1696 try { 1697 Intent intent = new Intent(TelecomManager.ACTION_NEW_UNKNOWN_CALL); 1698 if (extras != null) { 1699 extras.setDefusable(true); 1700 intent.putExtras(extras); 1701 } 1702 intent.putExtra(CallIntentProcessor.KEY_IS_UNKNOWN_CALL, true); 1703 intent.putExtra(TelecomManager.EXTRA_PHONE_ACCOUNT_HANDLE, 1704 phoneAccountHandle); 1705 mCallIntentProcessorAdapter.processUnknownCallIntent(mCallsManager, 1706 intent); 1707 } finally { 1708 Binder.restoreCallingIdentity(token); 1709 } 1710 } else { 1711 Log.i(this, 1712 "Null phoneAccountHandle or not initiated by Telephony. " + 1713 "Ignoring request to add new unknown call."); 1714 } 1715 } 1716 } finally { 1717 Log.endSession(); 1718 } 1719 } 1720 1721 /** 1722 * @see android.telecom.TelecomManager#startConference. 1723 */ 1724 @Override 1725 public void startConference(List<Uri> participants, Bundle extras, 1726 String callingPackage) { 1727 try { 1728 Log.startSession("TSI.sC", Log.getPackageAbbreviation(callingPackage)); 1729 if (!canCallPhone(callingPackage, "startConference")) { 1730 throw new SecurityException("Package " + callingPackage + " is not allowed" 1731 + " to start conference call"); 1732 } 1733 1734 long token = Binder.clearCallingIdentity(); 1735 try { 1736 mCallsManager.startConference(participants, extras, callingPackage, 1737 Binder.getCallingUserHandle()); 1738 } finally { 1739 Binder.restoreCallingIdentity(token); 1740 } 1741 } finally { 1742 Log.endSession(); 1743 } 1744 } 1745 1746 /** 1747 * @see android.telecom.TelecomManager#placeCall 1748 */ 1749 @Override 1750 public void placeCall(Uri handle, Bundle extras, String callingPackage, 1751 String callingFeatureId) { 1752 try { 1753 Log.startSession("TSI.pC", Log.getPackageAbbreviation(callingPackage)); 1754 enforceCallingPackage(callingPackage, "placeCall"); 1755 1756 PhoneAccountHandle phoneAccountHandle = null; 1757 if (extras != null) { 1758 phoneAccountHandle = extras.getParcelable( 1759 TelecomManager.EXTRA_PHONE_ACCOUNT_HANDLE); 1760 if (extras.containsKey(TelecomManager.EXTRA_IS_HANDOVER)) { 1761 // This extra is for Telecom use only so should never be passed in. 1762 extras.remove(TelecomManager.EXTRA_IS_HANDOVER); 1763 } 1764 } 1765 ComponentName phoneAccountComponentName = phoneAccountHandle != null 1766 ? phoneAccountHandle.getComponentName() : null; 1767 String phoneAccountPackageName = phoneAccountComponentName != null 1768 ? phoneAccountComponentName.getPackageName() : null; 1769 boolean isCallerOwnerOfPhoneAccount = 1770 callingPackage.equals(phoneAccountPackageName); 1771 boolean isSelfManagedPhoneAccount = 1772 isSelfManagedConnectionService(phoneAccountHandle); 1773 // Ensure the app's calling package matches the PhoneAccount package name before 1774 // checking self-managed status so that we do not leak installed package 1775 // information. 1776 boolean isSelfManagedRequest = isCallerOwnerOfPhoneAccount && 1777 isSelfManagedPhoneAccount; 1778 if (isSelfManagedRequest) { 1779 // The package name of the caller matches the package name of the 1780 // PhoneAccountHandle, so ensure the app has MANAGE_OWN_CALLS permission if 1781 // self-managed. 1782 mContext.enforceCallingOrSelfPermission( 1783 Manifest.permission.MANAGE_OWN_CALLS, 1784 "Self-managed ConnectionServices require MANAGE_OWN_CALLS permission."); 1785 } else if (!canCallPhone(callingPackage, callingFeatureId, 1786 "CALL_PHONE permission required to place calls.")) { 1787 // not self-managed, so CALL_PHONE is required. 1788 mAnomalyReporter.reportAnomaly(PLACE_CALL_SECURITY_EXCEPTION_ERROR_UUID, 1789 PLACE_CALL_SECURITY_EXCEPTION_ERROR_MSG); 1790 throw new SecurityException( 1791 "CALL_PHONE permission required to place calls."); 1792 } 1793 1794 // An application can not place a call with a self-managed PhoneAccount that 1795 // they do not own. If this is the case (and the app has CALL_PHONE permission), 1796 // remove the PhoneAccount from the request and place the call as if it was a 1797 // managed call request with no PhoneAccount specified. 1798 if (!isCallerOwnerOfPhoneAccount && isSelfManagedPhoneAccount) { 1799 // extras can not be null if isSelfManagedPhoneAccount is true 1800 extras.remove(TelecomManager.EXTRA_PHONE_ACCOUNT_HANDLE); 1801 } 1802 1803 // Note: we can still get here for the default/system dialer, even if the Phone 1804 // permission is turned off. This is because the default/system dialer is always 1805 // allowed to attempt to place a call (regardless of permission state), in case 1806 // it turns out to be an emergency call. If the permission is denied and the 1807 // call is being made to a non-emergency number, the call will be denied later on 1808 // by {@link UserCallIntentProcessor}. 1809 1810 final boolean hasCallAppOp = mAppOpsManager.noteOp(AppOpsManager.OP_CALL_PHONE, 1811 Binder.getCallingUid(), callingPackage, callingFeatureId, null) 1812 == AppOpsManager.MODE_ALLOWED; 1813 1814 final boolean hasCallPermission = mContext.checkCallingOrSelfPermission(CALL_PHONE) 1815 == PackageManager.PERMISSION_GRANTED; 1816 // The Emergency Dialer has call privileged permission and uses this to place 1817 // emergency calls. We ensure permission checks in 1818 // NewOutgoingCallIntentBroadcaster#process pass by sending this to 1819 // Telecom as an ACTION_CALL_PRIVILEGED intent (which makes sense since the 1820 // com.android.phone process has that permission). 1821 final boolean hasCallPrivilegedPermission = mContext.checkCallingOrSelfPermission( 1822 CALL_PRIVILEGED) == PackageManager.PERMISSION_GRANTED; 1823 1824 synchronized (mLock) { 1825 final UserHandle userHandle = Binder.getCallingUserHandle(); 1826 long token = Binder.clearCallingIdentity(); 1827 try { 1828 final Intent intent = new Intent(hasCallPrivilegedPermission ? 1829 Intent.ACTION_CALL_PRIVILEGED : Intent.ACTION_CALL, handle); 1830 if (extras != null) { 1831 extras.setDefusable(true); 1832 intent.putExtras(extras); 1833 } 1834 mUserCallIntentProcessorFactory.create(mContext, userHandle) 1835 .processIntent(intent, callingPackage, isSelfManagedRequest, 1836 (hasCallAppOp && hasCallPermission) 1837 || hasCallPrivilegedPermission, 1838 true /* isLocalInvocation */); 1839 } finally { 1840 Binder.restoreCallingIdentity(token); 1841 } 1842 } 1843 } finally { 1844 Log.endSession(); 1845 } 1846 } 1847 1848 /** 1849 * @see android.telecom.TelecomManager#enablePhoneAccount 1850 */ 1851 @Override 1852 public boolean enablePhoneAccount(PhoneAccountHandle accountHandle, boolean isEnabled) { 1853 try { 1854 Log.startSession("TSI.ePA"); 1855 enforceModifyPermission(); 1856 synchronized (mLock) { 1857 long token = Binder.clearCallingIdentity(); 1858 try { 1859 // enable/disable phone account 1860 return mPhoneAccountRegistrar.enablePhoneAccount(accountHandle, isEnabled); 1861 } finally { 1862 Binder.restoreCallingIdentity(token); 1863 } 1864 } 1865 } finally { 1866 Log.endSession(); 1867 } 1868 } 1869 1870 @Override 1871 public boolean setDefaultDialer(String packageName) { 1872 try { 1873 Log.startSession("TSI.sDD"); 1874 enforcePermission(MODIFY_PHONE_STATE); 1875 enforcePermission(WRITE_SECURE_SETTINGS); 1876 synchronized (mLock) { 1877 int callerUserId = UserHandle.getCallingUserId(); 1878 long token = Binder.clearCallingIdentity(); 1879 try { 1880 return mDefaultDialerCache.setDefaultDialer(packageName, 1881 callerUserId); 1882 } finally { 1883 Binder.restoreCallingIdentity(token); 1884 } 1885 } 1886 } finally { 1887 Log.endSession(); 1888 } 1889 } 1890 1891 @Override 1892 public void stopBlockSuppression() { 1893 try { 1894 Log.startSession("TSI.sBS"); 1895 enforceModifyPermission(); 1896 if (Binder.getCallingUid() != Process.SHELL_UID 1897 && Binder.getCallingUid() != Process.ROOT_UID) { 1898 throw new SecurityException("Shell-only API."); 1899 } 1900 synchronized (mLock) { 1901 long token = Binder.clearCallingIdentity(); 1902 try { 1903 BlockedNumberContract.SystemContract.endBlockSuppression(mContext); 1904 } finally { 1905 Binder.restoreCallingIdentity(token); 1906 } 1907 } 1908 } finally { 1909 Log.endSession(); 1910 } 1911 } 1912 1913 @Override 1914 public TelecomAnalytics dumpCallAnalytics() { 1915 try { 1916 Log.startSession("TSI.dCA"); 1917 enforcePermission(DUMP); 1918 return Analytics.dumpToParcelableAnalytics(); 1919 } finally { 1920 Log.endSession(); 1921 } 1922 } 1923 1924 /** 1925 * Dumps the current state of the TelecomService. Used when generating problem 1926 * reports. 1927 * 1928 * @param fd The file descriptor. 1929 * @param writer The print writer to dump the state to. 1930 * @param args Optional dump arguments. 1931 */ 1932 @Override 1933 protected void dump(FileDescriptor fd, final PrintWriter writer, String[] args) { 1934 if (mContext.checkCallingOrSelfPermission( 1935 android.Manifest.permission.DUMP) 1936 != PackageManager.PERMISSION_GRANTED) { 1937 writer.println("Permission Denial: can't dump TelecomService " + 1938 "from from pid=" + Binder.getCallingPid() + ", uid=" + 1939 Binder.getCallingUid()); 1940 return; 1941 } 1942 1943 1944 if (args != null && args.length > 0 && Analytics.ANALYTICS_DUMPSYS_ARG.equals( 1945 args[0])) { 1946 Binder.withCleanCallingIdentity(() -> 1947 Analytics.dumpToEncodedProto(mContext, writer, args)); 1948 return; 1949 } 1950 1951 boolean isTimeLineView = 1952 (args != null && args.length > 0 && TIME_LINE_ARG.equalsIgnoreCase(args[0])); 1953 1954 final IndentingPrintWriter pw = new IndentingPrintWriter(writer, " "); 1955 if (mCallsManager != null) { 1956 pw.println("CallsManager: "); 1957 pw.increaseIndent(); 1958 mCallsManager.dump(pw, args); 1959 pw.decreaseIndent(); 1960 1961 pw.println("PhoneAccountRegistrar: "); 1962 pw.increaseIndent(); 1963 mPhoneAccountRegistrar.dump(pw); 1964 pw.decreaseIndent(); 1965 1966 pw.println("Analytics:"); 1967 pw.increaseIndent(); 1968 Analytics.dump(pw); 1969 pw.decreaseIndent(); 1970 } 1971 if (isTimeLineView) { 1972 Log.dumpEventsTimeline(pw); 1973 } else { 1974 Log.dumpEvents(pw); 1975 } 1976 } 1977 1978 /** 1979 * @see android.telecom.TelecomManager#createManageBlockedNumbersIntent 1980 */ 1981 @Override 1982 public Intent createManageBlockedNumbersIntent(String callingPackage) { 1983 try { 1984 Log.startSession("TSI.cMBNI", Log.getPackageAbbreviation(callingPackage)); 1985 return BlockedNumbersActivity.getIntentForStartingActivity(); 1986 } finally { 1987 Log.endSession(); 1988 } 1989 } 1990 1991 1992 @Override 1993 public Intent createLaunchEmergencyDialerIntent(String number) { 1994 String packageName = mContext.getApplicationContext().getString( 1995 com.android.internal.R.string.config_emergency_dialer_package); 1996 Intent intent = new Intent(Intent.ACTION_DIAL_EMERGENCY) 1997 .setPackage(packageName); 1998 ResolveInfo resolveInfo = mPackageManager.resolveActivity(intent, 0 /* flags*/); 1999 if (resolveInfo == null) { 2000 // No matching activity from config, fallback to default platform implementation 2001 intent.setPackage(null); 2002 } 2003 if (!TextUtils.isEmpty(number) && TextUtils.isDigitsOnly(number)) { 2004 intent.setData(Uri.parse("tel:" + number)); 2005 } 2006 return intent; 2007 } 2008 2009 /** 2010 * @see android.telecom.TelecomManager#isIncomingCallPermitted(PhoneAccountHandle) 2011 */ 2012 @Override 2013 public boolean isIncomingCallPermitted(PhoneAccountHandle phoneAccountHandle, 2014 String callingPackage) { 2015 Log.startSession("TSI.iICP", Log.getPackageAbbreviation(callingPackage)); 2016 try { 2017 enforceCallingPackage(callingPackage, "isIncomingCallPermitted"); 2018 enforcePhoneAccountHandleMatchesCaller(phoneAccountHandle, callingPackage); 2019 enforcePermission(android.Manifest.permission.MANAGE_OWN_CALLS); 2020 enforceUserHandleMatchesCaller(phoneAccountHandle); 2021 synchronized (mLock) { 2022 long token = Binder.clearCallingIdentity(); 2023 try { 2024 return mCallsManager.isIncomingCallPermitted(phoneAccountHandle); 2025 } finally { 2026 Binder.restoreCallingIdentity(token); 2027 } 2028 } 2029 } finally { 2030 Log.endSession(); 2031 } 2032 } 2033 2034 /** 2035 * @see android.telecom.TelecomManager#isOutgoingCallPermitted(PhoneAccountHandle) 2036 */ 2037 @Override 2038 public boolean isOutgoingCallPermitted(PhoneAccountHandle phoneAccountHandle, 2039 String callingPackage) { 2040 Log.startSession("TSI.iOCP", Log.getPackageAbbreviation(callingPackage)); 2041 try { 2042 enforceCallingPackage(callingPackage, "isOutgoingCallPermitted"); 2043 enforcePhoneAccountHandleMatchesCaller(phoneAccountHandle, callingPackage); 2044 enforcePermission(android.Manifest.permission.MANAGE_OWN_CALLS); 2045 enforceUserHandleMatchesCaller(phoneAccountHandle); 2046 synchronized (mLock) { 2047 long token = Binder.clearCallingIdentity(); 2048 try { 2049 return mCallsManager.isOutgoingCallPermitted(phoneAccountHandle); 2050 } finally { 2051 Binder.restoreCallingIdentity(token); 2052 } 2053 } 2054 } finally { 2055 Log.endSession(); 2056 } 2057 } 2058 2059 /** 2060 * Blocks until all Telecom handlers have completed their current work. 2061 * 2062 * See {@link com.android.commands.telecom.Telecom}. 2063 */ 2064 @Override 2065 public void waitOnHandlers() { 2066 try { 2067 Log.startSession("TSI.wOH"); 2068 enforceModifyPermission(); 2069 synchronized (mLock) { 2070 long token = Binder.clearCallingIdentity(); 2071 try { 2072 Log.i(this, "waitOnHandlers"); 2073 mCallsManager.waitOnHandlers(); 2074 } finally { 2075 Binder.restoreCallingIdentity(token); 2076 } 2077 } 2078 } finally { 2079 Log.endSession(); 2080 } 2081 } 2082 2083 @Override 2084 public void setTestEmergencyPhoneAccountPackageNameFilter(String packageName) { 2085 try { 2086 Log.startSession("TSI.sTPAPNF"); 2087 enforceModifyPermission(); 2088 enforceShellOnly(Binder.getCallingUid(), 2089 "setTestEmergencyPhoneAccountPackageNameFilter"); 2090 synchronized (mLock) { 2091 long token = Binder.clearCallingIdentity(); 2092 try { 2093 mPhoneAccountRegistrar.setTestPhoneAccountPackageNameFilter(packageName); 2094 } finally { 2095 Binder.restoreCallingIdentity(token); 2096 } 2097 } 2098 } finally { 2099 Log.endSession(); 2100 } 2101 } 2102 2103 /** 2104 * See {@link TelecomManager#isInEmergencyCall()} 2105 */ 2106 @Override 2107 public boolean isInEmergencyCall() { 2108 try { 2109 Log.startSession("TSI.iIEC"); 2110 enforceModifyPermission(); 2111 synchronized (mLock) { 2112 long token = Binder.clearCallingIdentity(); 2113 try { 2114 boolean isInEmergencyCall = mCallsManager.isInEmergencyCall(); 2115 Log.i(this, "isInEmergencyCall: %b", isInEmergencyCall); 2116 return isInEmergencyCall; 2117 } finally { 2118 Binder.restoreCallingIdentity(token); 2119 } 2120 } 2121 } finally { 2122 Log.endSession(); 2123 } 2124 } 2125 2126 /** 2127 * See {@link TelecomManager#handleCallIntent(Intent, String)} 2128 */ 2129 @Override 2130 public void handleCallIntent(Intent intent, String callingPackage) { 2131 try { 2132 Log.startSession("TSI.hCI"); 2133 synchronized (mLock) { 2134 mContext.enforceCallingOrSelfPermission(PERMISSION_HANDLE_CALL_INTENT, 2135 "handleCallIntent is for internal use only."); 2136 2137 long token = Binder.clearCallingIdentity(); 2138 try { 2139 Log.i(this, "handleCallIntent: handling call intent"); 2140 mCallIntentProcessorAdapter.processOutgoingCallIntent(mContext, 2141 mCallsManager, intent, callingPackage); 2142 } finally { 2143 Binder.restoreCallingIdentity(token); 2144 } 2145 } 2146 } finally { 2147 Log.endSession(); 2148 } 2149 } 2150 2151 /** 2152 * A method intended for use in testing to clean up any calls that get stuck in the 2153 * {@link CallState#DISCONNECTED} or {@link CallState#DISCONNECTING} states. Stuck 2154 * calls 2155 * during CTS cause cascading failures, so if the CTS test detects such a state, it 2156 * should 2157 * call this method via a shell command to clean up before moving on to the next 2158 * test. 2159 * Also cleans up any pending futures related to 2160 * {@link android.telecom.CallDiagnosticService}s. 2161 */ 2162 @Override 2163 public void cleanupStuckCalls() { 2164 Log.startSession("TCI.cSC"); 2165 try { 2166 synchronized (mLock) { 2167 enforceShellOnly(Binder.getCallingUid(), "cleanupStuckCalls"); 2168 Binder.withCleanCallingIdentity(() -> { 2169 Set<UserHandle> userHandles = new HashSet<>(); 2170 for (Call call : mCallsManager.getCalls()) { 2171 call.cleanup(); 2172 if (call.getState() == CallState.DISCONNECTED 2173 || call.getState() == CallState.DISCONNECTING) { 2174 mCallsManager.markCallAsRemoved(call); 2175 } 2176 userHandles.add(call.getAssociatedUser()); 2177 } 2178 for (UserHandle userHandle : userHandles) { 2179 mCallsManager.getInCallController().unbindFromServices(userHandle); 2180 } 2181 }); 2182 } 2183 } finally { 2184 Log.endSession(); 2185 } 2186 } 2187 2188 /** 2189 * A method intended for test to clean up orphan {@link PhoneAccount}. An orphan 2190 * {@link PhoneAccount} is a phone account belongs to an invalid {@link UserHandle} 2191 * or a 2192 * deleted package. 2193 * 2194 * @return the number of orphan {@code PhoneAccount} deleted. 2195 */ 2196 @Override 2197 public int cleanupOrphanPhoneAccounts() { 2198 Log.startSession("TCI.cOPA"); 2199 try { 2200 synchronized (mLock) { 2201 enforceShellOnly(Binder.getCallingUid(), "cleanupOrphanPhoneAccounts"); 2202 long token = Binder.clearCallingIdentity(); 2203 try { 2204 return mPhoneAccountRegistrar.cleanupOrphanedPhoneAccounts(); 2205 } finally { 2206 Binder.restoreCallingIdentity(token); 2207 } 2208 } 2209 } finally { 2210 Log.endSession(); 2211 } 2212 } 2213 2214 /** 2215 * A method intended for use in testing to reset car mode at all priorities. 2216 * 2217 * Runs during setup to avoid cascading failures from failing car mode CTS. 2218 */ 2219 @Override 2220 public void resetCarMode() { 2221 Log.startSession("TCI.rCM"); 2222 try { 2223 synchronized (mLock) { 2224 enforceShellOnly(Binder.getCallingUid(), "resetCarMode"); 2225 Binder.withCleanCallingIdentity(() -> { 2226 UiModeManager uiModeManager = 2227 mContext.getSystemService(UiModeManager.class); 2228 uiModeManager.disableCarMode(UiModeManager.DISABLE_CAR_MODE_ALL_PRIORITIES); 2229 }); 2230 } 2231 } finally { 2232 Log.endSession(); 2233 } 2234 } 2235 2236 @Override 2237 public void setTestDefaultCallRedirectionApp(String packageName) { 2238 try { 2239 Log.startSession("TSI.sTDCRA"); 2240 enforceModifyPermission(); 2241 if (!Build.IS_USERDEBUG) { 2242 throw new SecurityException("Test-only API."); 2243 } 2244 synchronized (mLock) { 2245 long token = Binder.clearCallingIdentity(); 2246 try { 2247 mCallsManager.getRoleManagerAdapter().setTestDefaultCallRedirectionApp( 2248 packageName); 2249 } finally { 2250 Binder.restoreCallingIdentity(token); 2251 } 2252 } 2253 } finally { 2254 Log.endSession(); 2255 } 2256 } 2257 2258 @Override 2259 public void setTestDefaultCallScreeningApp(String packageName) { 2260 try { 2261 Log.startSession("TSI.sTDCSA"); 2262 enforceModifyPermission(); 2263 if (!Build.IS_USERDEBUG) { 2264 throw new SecurityException("Test-only API."); 2265 } 2266 synchronized (mLock) { 2267 long token = Binder.clearCallingIdentity(); 2268 try { 2269 mCallsManager.getRoleManagerAdapter().setTestDefaultCallScreeningApp( 2270 packageName); 2271 } finally { 2272 Binder.restoreCallingIdentity(token); 2273 } 2274 } 2275 } finally { 2276 Log.endSession(); 2277 } 2278 } 2279 2280 @Override 2281 public void addOrRemoveTestCallCompanionApp(String packageName, boolean isAdded) { 2282 try { 2283 Log.startSession("TSI.aORTCCA"); 2284 enforceModifyPermission(); 2285 enforceShellOnly(Binder.getCallingUid(), "addOrRemoveTestCallCompanionApp"); 2286 synchronized (mLock) { 2287 long token = Binder.clearCallingIdentity(); 2288 try { 2289 mCallsManager.getRoleManagerAdapter().addOrRemoveTestCallCompanionApp( 2290 packageName, isAdded); 2291 } finally { 2292 Binder.restoreCallingIdentity(token); 2293 } 2294 } 2295 } finally { 2296 Log.endSession(); 2297 } 2298 } 2299 2300 @Override 2301 public void requestLogMark(String message) { 2302 try { 2303 Log.startSession("TSI.rLM"); 2304 enforceShellOnly(Binder.getCallingUid(), "requestLogMark is for shell only"); 2305 synchronized (mLock) { 2306 long token = Binder.clearCallingIdentity(); 2307 try { 2308 mCallsManager.requestLogMark(message); 2309 } finally { 2310 Binder.restoreCallingIdentity(token); 2311 } 2312 } 2313 } finally { 2314 Log.endSession(); 2315 } 2316 } 2317 2318 @Override 2319 public void setTestPhoneAcctSuggestionComponent(String flattenedComponentName) { 2320 try { 2321 Log.startSession("TSI.sPASA"); 2322 enforceModifyPermission(); 2323 if (Binder.getCallingUid() != Process.SHELL_UID 2324 && Binder.getCallingUid() != Process.ROOT_UID) { 2325 throw new SecurityException("Shell-only API."); 2326 } 2327 synchronized (mLock) { 2328 PhoneAccountSuggestionHelper.setOverrideServiceName(flattenedComponentName); 2329 } 2330 } finally { 2331 Log.endSession(); 2332 } 2333 } 2334 2335 @Override 2336 public void setTestDefaultDialer(String packageName) { 2337 try { 2338 Log.startSession("TSI.sTDD"); 2339 enforceModifyPermission(); 2340 if (Binder.getCallingUid() != Process.SHELL_UID 2341 && Binder.getCallingUid() != Process.ROOT_UID) { 2342 throw new SecurityException("Shell-only API."); 2343 } 2344 synchronized (mLock) { 2345 long token = Binder.clearCallingIdentity(); 2346 try { 2347 mCallsManager.getRoleManagerAdapter().setTestDefaultDialer(packageName); 2348 } finally { 2349 Binder.restoreCallingIdentity(token); 2350 } 2351 } 2352 } finally { 2353 Log.endSession(); 2354 } 2355 } 2356 2357 @Override 2358 public void setTestCallDiagnosticService(String packageName) { 2359 try { 2360 Log.startSession("TSI.sTCDS"); 2361 enforceModifyPermission(); 2362 enforceShellOnly(Binder.getCallingUid(), "setTestCallDiagnosticService is for " 2363 + "shell use only."); 2364 synchronized (mLock) { 2365 long token = Binder.clearCallingIdentity(); 2366 try { 2367 CallDiagnosticServiceController controller = 2368 mCallsManager.getCallDiagnosticServiceController(); 2369 if (controller != null) { 2370 controller.setTestCallDiagnosticService(packageName); 2371 } 2372 } finally { 2373 Binder.restoreCallingIdentity(token); 2374 } 2375 } 2376 } finally { 2377 Log.endSession(); 2378 } 2379 } 2380 2381 /** 2382 * Determines whether there are any ongoing {@link PhoneAccount#CAPABILITY_SELF_MANAGED} 2383 * calls for a given {@code packageName} and {@code userHandle}. 2384 * 2385 * @param packageName the package name of the app to check calls for. 2386 * @param userHandle the user handle on which to check for calls. 2387 * @param callingPackage The caller's package name. 2388 * @return {@code true} if there are ongoing calls, {@code false} otherwise. 2389 */ 2390 @Override 2391 public boolean isInSelfManagedCall(String packageName, UserHandle userHandle, 2392 String callingPackage) { 2393 try { 2394 if (Binder.getCallingUid() != Process.SYSTEM_UID) { 2395 throw new SecurityException("Only the system can call this API"); 2396 } 2397 mContext.enforceCallingOrSelfPermission(READ_PRIVILEGED_PHONE_STATE, 2398 "READ_PRIVILEGED_PHONE_STATE required."); 2399 2400 Log.startSession("TSI.iISMC", Log.getPackageAbbreviation(callingPackage)); 2401 synchronized (mLock) { 2402 long token = Binder.clearCallingIdentity(); 2403 try { 2404 return mCallsManager.isInSelfManagedCall(packageName, userHandle); 2405 } finally { 2406 Binder.restoreCallingIdentity(token); 2407 } 2408 } 2409 } finally { 2410 Log.endSession(); 2411 } 2412 } 2413 }; 2414 enforceCallStreamingPermission(String packageName, PhoneAccountHandle handle, int uid)2415 private boolean enforceCallStreamingPermission(String packageName, PhoneAccountHandle handle, 2416 int uid) { 2417 // TODO: implement this permission check (make sure the calling package is the d2di package) 2418 PhoneAccount account = mPhoneAccountRegistrar.getPhoneAccount(handle, 2419 UserHandle.getUserHandleForUid(uid)); 2420 if (account == null 2421 || !account.hasCapabilities(PhoneAccount.CAPABILITY_SUPPORTS_CALL_STREAMING)) { 2422 throw new SecurityException( 2423 "The phone account handle in requesting can't support call streaming: " 2424 + handle); 2425 } 2426 return true; 2427 } 2428 2429 /** 2430 * @return whether to return early without doing the action/throwing 2431 * @throws SecurityException same as {@link Context#enforceCallingOrSelfPermission} 2432 */ enforceAnswerCallPermission(String packageName, int uid)2433 private boolean enforceAnswerCallPermission(String packageName, int uid) { 2434 try { 2435 enforceModifyPermission(); 2436 } catch (SecurityException e) { 2437 final String permission = Manifest.permission.ANSWER_PHONE_CALLS; 2438 enforcePermission(permission); 2439 2440 final int opCode = AppOpsManager.permissionToOpCode(permission); 2441 if (opCode != AppOpsManager.OP_NONE 2442 && mAppOpsManager.checkOp(opCode, uid, packageName) 2443 != AppOpsManager.MODE_ALLOWED) { 2444 return false; 2445 } 2446 } 2447 return true; 2448 } 2449 2450 /** 2451 * @return {@code true} if the app has the handover permission and has received runtime 2452 * permission to perform that operation, {@code false}. 2453 * @throws SecurityException same as {@link Context#enforceCallingOrSelfPermission} 2454 */ enforceAcceptHandoverPermission(String packageName, int uid)2455 private boolean enforceAcceptHandoverPermission(String packageName, int uid) { 2456 mContext.enforceCallingOrSelfPermission(Manifest.permission.ACCEPT_HANDOVER, 2457 "App requires ACCEPT_HANDOVER permission to accept handovers."); 2458 2459 final int opCode = AppOpsManager.permissionToOpCode(Manifest.permission.ACCEPT_HANDOVER); 2460 if (opCode != AppOpsManager.OP_ACCEPT_HANDOVER || ( 2461 mAppOpsManager.checkOp(opCode, uid, packageName) 2462 != AppOpsManager.MODE_ALLOWED)) { 2463 return false; 2464 } 2465 return true; 2466 } 2467 2468 private Context mContext; 2469 private AppOpsManager mAppOpsManager; 2470 private PackageManager mPackageManager; 2471 private CallsManager mCallsManager; 2472 private final PhoneAccountRegistrar mPhoneAccountRegistrar; 2473 private final CallIntentProcessor.Adapter mCallIntentProcessorAdapter; 2474 private final UserCallIntentProcessorFactory mUserCallIntentProcessorFactory; 2475 private final DefaultDialerCache mDefaultDialerCache; 2476 private final SubscriptionManagerAdapter mSubscriptionManagerAdapter; 2477 private final SettingsSecureAdapter mSettingsSecureAdapter; 2478 private final TelecomSystem.SyncRoot mLock; 2479 private TransactionManager mTransactionManager; 2480 private final TransactionalServiceRepository mTransactionalServiceRepository; 2481 TelecomServiceImpl( Context context, CallsManager callsManager, PhoneAccountRegistrar phoneAccountRegistrar, CallIntentProcessor.Adapter callIntentProcessorAdapter, UserCallIntentProcessorFactory userCallIntentProcessorFactory, DefaultDialerCache defaultDialerCache, SubscriptionManagerAdapter subscriptionManagerAdapter, SettingsSecureAdapter settingsSecureAdapter, TelecomSystem.SyncRoot lock)2482 public TelecomServiceImpl( 2483 Context context, 2484 CallsManager callsManager, 2485 PhoneAccountRegistrar phoneAccountRegistrar, 2486 CallIntentProcessor.Adapter callIntentProcessorAdapter, 2487 UserCallIntentProcessorFactory userCallIntentProcessorFactory, 2488 DefaultDialerCache defaultDialerCache, 2489 SubscriptionManagerAdapter subscriptionManagerAdapter, 2490 SettingsSecureAdapter settingsSecureAdapter, 2491 TelecomSystem.SyncRoot lock) { 2492 mContext = context; 2493 mAppOpsManager = (AppOpsManager) mContext.getSystemService(Context.APP_OPS_SERVICE); 2494 2495 mPackageManager = mContext.getPackageManager(); 2496 2497 mCallsManager = callsManager; 2498 mLock = lock; 2499 mPhoneAccountRegistrar = phoneAccountRegistrar; 2500 mUserCallIntentProcessorFactory = userCallIntentProcessorFactory; 2501 mDefaultDialerCache = defaultDialerCache; 2502 mCallIntentProcessorAdapter = callIntentProcessorAdapter; 2503 mSubscriptionManagerAdapter = subscriptionManagerAdapter; 2504 mSettingsSecureAdapter = settingsSecureAdapter; 2505 2506 mDefaultDialerCache.observeDefaultDialerApplication(mContext.getMainExecutor(), userId -> { 2507 String defaultDialer = mDefaultDialerCache.getDefaultDialerApplication(userId); 2508 if (defaultDialer == null) { 2509 // We are replacing the dialer, just wait for the upcoming callback. 2510 return; 2511 } 2512 final Intent intent = new Intent(TelecomManager.ACTION_DEFAULT_DIALER_CHANGED) 2513 .putExtra(TelecomManager.EXTRA_CHANGE_DEFAULT_DIALER_PACKAGE_NAME, 2514 defaultDialer); 2515 mContext.sendBroadcastAsUser(intent, UserHandle.of(userId)); 2516 }); 2517 2518 mTransactionManager = TransactionManager.getInstance(); 2519 mTransactionalServiceRepository = new TransactionalServiceRepository(); 2520 } 2521 2522 @VisibleForTesting setTransactionManager(TransactionManager transactionManager)2523 public void setTransactionManager(TransactionManager transactionManager){ 2524 mTransactionManager = transactionManager; 2525 } 2526 getBinder()2527 public ITelecomService.Stub getBinder() { 2528 return mBinderImpl; 2529 } 2530 2531 // 2532 // Supporting methods for the ITelecomService interface implementation. 2533 // 2534 isPhoneAccountHandleVisibleToCallingUser( PhoneAccountHandle phoneAccountUserHandle, UserHandle callingUser)2535 private boolean isPhoneAccountHandleVisibleToCallingUser( 2536 PhoneAccountHandle phoneAccountUserHandle, UserHandle callingUser) { 2537 synchronized (mLock) { 2538 return mPhoneAccountRegistrar.getPhoneAccount(phoneAccountUserHandle, callingUser) 2539 != null; 2540 } 2541 } 2542 isCallerSystemApp()2543 private boolean isCallerSystemApp() { 2544 int uid = Binder.getCallingUid(); 2545 String[] packages = mPackageManager.getPackagesForUid(uid); 2546 for (String packageName : packages) { 2547 if (isPackageSystemApp(packageName)) { 2548 return true; 2549 } 2550 } 2551 return false; 2552 } 2553 isPackageSystemApp(String packageName)2554 private boolean isPackageSystemApp(String packageName) { 2555 try { 2556 ApplicationInfo applicationInfo = mPackageManager.getApplicationInfo(packageName, 2557 PackageManager.GET_META_DATA); 2558 if ((applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) { 2559 return true; 2560 } 2561 } catch (PackageManager.NameNotFoundException e) { 2562 } 2563 return false; 2564 } 2565 acceptRingingCallInternal(int videoState, String packageName)2566 private void acceptRingingCallInternal(int videoState, String packageName) { 2567 Call call = mCallsManager.getFirstCallWithState(CallState.RINGING, 2568 CallState.SIMULATED_RINGING); 2569 if (call != null) { 2570 if (call.isSelfManaged()) { 2571 Log.addEvent(call, LogUtils.Events.REQUEST_ACCEPT, 2572 "self-mgd accept ignored from " + packageName); 2573 return; 2574 } 2575 2576 if (videoState == DEFAULT_VIDEO_STATE || !isValidAcceptVideoState(videoState)) { 2577 videoState = call.getVideoState(); 2578 } 2579 mCallsManager.answerCall(call, videoState); 2580 } 2581 } 2582 endCallInternal(String callingPackage)2583 private boolean endCallInternal(String callingPackage) { 2584 // Always operate on the foreground call if one exists, otherwise get the first call in 2585 // priority order by call-state. 2586 Call call = mCallsManager.getForegroundCall(); 2587 if (call == null) { 2588 call = mCallsManager.getFirstCallWithState( 2589 CallState.ACTIVE, 2590 CallState.DIALING, 2591 CallState.PULLING, 2592 CallState.RINGING, 2593 CallState.SIMULATED_RINGING, 2594 CallState.ON_HOLD); 2595 } 2596 2597 if (call != null) { 2598 if (call.isEmergencyCall()) { 2599 android.util.EventLog.writeEvent(0x534e4554, "132438333", -1, ""); 2600 return false; 2601 } 2602 2603 if (call.isSelfManaged()) { 2604 Log.addEvent(call, LogUtils.Events.REQUEST_DISCONNECT, 2605 "self-mgd disconnect ignored from " + callingPackage); 2606 return false; 2607 } 2608 2609 if (call.getState() == CallState.RINGING 2610 || call.getState() == CallState.SIMULATED_RINGING) { 2611 mCallsManager.rejectCall(call, false /* rejectWithMessage */, null); 2612 } else { 2613 mCallsManager.disconnectCall(call); 2614 } 2615 return true; 2616 } 2617 2618 return false; 2619 } 2620 2621 // Enforce that the PhoneAccountHandle being passed in is both registered to the current user 2622 // and enabled. enforcePhoneAccountIsRegisteredEnabled(PhoneAccountHandle phoneAccountHandle, UserHandle callingUserHandle)2623 private void enforcePhoneAccountIsRegisteredEnabled(PhoneAccountHandle phoneAccountHandle, 2624 UserHandle callingUserHandle) { 2625 PhoneAccount phoneAccount = mPhoneAccountRegistrar.getPhoneAccount(phoneAccountHandle, 2626 callingUserHandle); 2627 if(phoneAccount == null) { 2628 EventLog.writeEvent(0x534e4554, "26864502", Binder.getCallingUid(), "R"); 2629 throw new SecurityException("This PhoneAccountHandle is not registered for this user!"); 2630 } 2631 if(!phoneAccount.isEnabled()) { 2632 EventLog.writeEvent(0x534e4554, "26864502", Binder.getCallingUid(), "E"); 2633 throw new SecurityException("This PhoneAccountHandle is not enabled for this user!"); 2634 } 2635 } 2636 2637 // Enforce that the PhoneAccountHandle is tied to a self-managed package and not managed (aka 2638 // sim calling, etc.) enforcePhoneAccountIsNotManaged(PhoneAccountHandle phoneAccountHandle)2639 private void enforcePhoneAccountIsNotManaged(PhoneAccountHandle phoneAccountHandle) { 2640 PhoneAccount phoneAccount = mPhoneAccountRegistrar.getPhoneAccount(phoneAccountHandle, 2641 phoneAccountHandle.getUserHandle()); 2642 if (phoneAccount == null) { 2643 throw new IllegalArgumentException("enforcePhoneAccountIsNotManaged:" 2644 + " phoneAccount is null"); 2645 } 2646 if (phoneAccount.hasCapabilities(PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION)) { 2647 throw new IllegalArgumentException("enforcePhoneAccountIsNotManaged:" 2648 + " CAPABILITY_SIM_SUBSCRIPTION is not allowed"); 2649 } 2650 if (phoneAccount.hasCapabilities(PhoneAccount.CAPABILITY_CALL_PROVIDER)) { 2651 throw new IllegalArgumentException("enforcePhoneAccountIsNotManaged:" 2652 + " CAPABILITY_CALL_PROVIDER is not allowed"); 2653 } 2654 if (phoneAccount.hasCapabilities(PhoneAccount.CAPABILITY_CONNECTION_MANAGER)) { 2655 throw new IllegalArgumentException("enforcePhoneAccountIsNotManaged:" 2656 + " CAPABILITY_CONNECTION_MANAGER is not allowed"); 2657 } 2658 } 2659 enforcePhoneAccountModificationForPackage(String packageName)2660 private void enforcePhoneAccountModificationForPackage(String packageName) { 2661 // TODO: Use a new telecomm permission for this instead of reusing modify. 2662 2663 int result = mContext.checkCallingOrSelfPermission(MODIFY_PHONE_STATE); 2664 2665 // Callers with MODIFY_PHONE_STATE can use the PhoneAccount mechanism to implement 2666 // built-in behavior even when PhoneAccounts are not exposed as a third-part API. They 2667 // may also modify PhoneAccounts on behalf of any 'packageName'. 2668 2669 if (result != PackageManager.PERMISSION_GRANTED) { 2670 // Other callers are only allowed to modify PhoneAccounts if the relevant system 2671 // feature is enabled ... 2672 enforceTelecomFeature(); 2673 // ... and the PhoneAccounts they refer to are for their own package. 2674 enforceCallingPackage(packageName, "enforcePhoneAccountModificationForPackage"); 2675 } 2676 } 2677 enforcePermissionOrPrivilegedDialer(String permission, String packageName)2678 private void enforcePermissionOrPrivilegedDialer(String permission, String packageName) { 2679 if (!isPrivilegedDialerCalling(packageName)) { 2680 try { 2681 enforcePermission(permission); 2682 } catch (SecurityException e) { 2683 Log.e(this, e, "Caller must be the default or system dialer, or have the permission" 2684 + " %s to perform this operation.", permission); 2685 throw e; 2686 } 2687 } 2688 } 2689 enforceCallingPackage(String packageName, String message)2690 private void enforceCallingPackage(String packageName, String message) { 2691 int callingUid = Binder.getCallingUid(); 2692 2693 if (callingUid != Process.ROOT_UID && 2694 !callingUidMatchesPackageManagerRecords(packageName)) { 2695 throw new SecurityException(message + ": Package " + packageName 2696 + " does not belong to " + callingUid); 2697 } 2698 } 2699 2700 /** 2701 * helper method that compares the binder_uid to what the packageManager_uid reports for the 2702 * passed in packageName. 2703 * 2704 * returns true if the binder_uid matches the packageManager_uid records 2705 */ callingUidMatchesPackageManagerRecords(String packageName)2706 private boolean callingUidMatchesPackageManagerRecords(String packageName) { 2707 int packageUid = -1; 2708 int callingUid = Binder.getCallingUid(); 2709 PackageManager pm; 2710 long token = Binder.clearCallingIdentity(); 2711 try{ 2712 pm = mContext.createContextAsUser( 2713 UserHandle.getUserHandleForUid(callingUid), 0).getPackageManager(); 2714 } 2715 catch (Exception e){ 2716 Log.i(this, "callingUidMatchesPackageManagerRecords:" 2717 + " createContextAsUser hit exception=[%s]", e.toString()); 2718 return false; 2719 } finally { 2720 Binder.restoreCallingIdentity(token); 2721 } 2722 if (pm != null) { 2723 try { 2724 packageUid = pm.getPackageUid(packageName, 0); 2725 } catch (PackageManager.NameNotFoundException e) { 2726 // packageUid is -1. 2727 } 2728 } 2729 2730 if (packageUid != callingUid) { 2731 Log.i(this, "callingUidMatchesPackageManagerRecords: uid mismatch found for" 2732 + "packageName=[%s]. packageManager reports packageUid=[%d] but " 2733 + "binder reports callingUid=[%d]", packageName, packageUid, callingUid); 2734 } 2735 2736 return packageUid == callingUid; 2737 } 2738 2739 /** 2740 * Note: This method should be called BEFORE clearing the binder identity. 2741 * 2742 * @param permissionsToValidate set of permissions that should be checked 2743 * @param alreadyComputedPermissions a list of permissions that were already checked 2744 * @return all the permissions that 2745 */ computePermissionsForBoundPackage( Set<String> permissionsToValidate, Set<String> alreadyComputedPermissions)2746 private Set<String> computePermissionsForBoundPackage( 2747 Set<String> permissionsToValidate, 2748 Set<String> alreadyComputedPermissions) { 2749 Set<String> permissions = Objects.requireNonNullElseGet(alreadyComputedPermissions, 2750 HashSet::new); 2751 for (String permission : permissionsToValidate) { 2752 if (mContext.checkCallingPermission(permission) == PackageManager.PERMISSION_GRANTED) { 2753 permissions.add(permission); 2754 } 2755 } 2756 return permissions; 2757 } 2758 2759 /** 2760 * This method should be used to clear {@link PhoneAccount} properties based on a 2761 * callingPackages permissions. 2762 * 2763 * @param account to clear properties from 2764 * @param permissions the list of permissions the callingPackge has 2765 * @return the account that callingPackage will receive 2766 */ maybeCleansePhoneAccount(PhoneAccount account, Set<String> permissions)2767 private PhoneAccount maybeCleansePhoneAccount(PhoneAccount account, 2768 Set<String> permissions) { 2769 if (account == null) { 2770 return null; 2771 } 2772 PhoneAccount.Builder accountBuilder = new PhoneAccount.Builder(account); 2773 if (!permissions.contains(MODIFY_PHONE_STATE)) { 2774 accountBuilder.setGroupId("***"); 2775 } 2776 return accountBuilder.build(); 2777 } 2778 enforceTelecomFeature()2779 private void enforceTelecomFeature() { 2780 PackageManager pm = mContext.getPackageManager(); 2781 if (!pm.hasSystemFeature(PackageManager.FEATURE_TELECOM) 2782 && !pm.hasSystemFeature(PackageManager.FEATURE_CONNECTION_SERVICE)) { 2783 throw new UnsupportedOperationException( 2784 "System does not support feature " + PackageManager.FEATURE_TELECOM); 2785 } 2786 } 2787 enforceRegisterSimSubscriptionPermission()2788 private void enforceRegisterSimSubscriptionPermission() { 2789 enforcePermission(REGISTER_SIM_SUBSCRIPTION); 2790 } 2791 enforceModifyPermission()2792 private void enforceModifyPermission() { 2793 enforcePermission(MODIFY_PHONE_STATE); 2794 } 2795 enforceModifyPermission(String message)2796 private void enforceModifyPermission(String message) { 2797 mContext.enforceCallingOrSelfPermission(MODIFY_PHONE_STATE, message); 2798 } 2799 enforcePermission(String permission)2800 private void enforcePermission(String permission) { 2801 mContext.enforceCallingOrSelfPermission(permission, null); 2802 } 2803 enforceRegisterSelfManaged()2804 private void enforceRegisterSelfManaged() { 2805 mContext.enforceCallingPermission(android.Manifest.permission.MANAGE_OWN_CALLS, null); 2806 } 2807 enforceRegisterMultiUser()2808 private void enforceRegisterMultiUser() { 2809 if (!isCallerSystemApp()) { 2810 throw new SecurityException("CAPABILITY_MULTI_USER is only available to system apps."); 2811 } 2812 } 2813 enforceRegisterVoiceCallingIndicationCapabilities(PhoneAccount account)2814 private void enforceRegisterVoiceCallingIndicationCapabilities(PhoneAccount account) { 2815 // Caller must be able to register a SIM PhoneAccount or be the SIM call manager (as named 2816 // in carrier config) to declare the two voice indication capabilities. 2817 boolean prerequisiteCapabilitiesOk = 2818 account.hasCapabilities(PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION) 2819 || account.hasCapabilities(PhoneAccount.CAPABILITY_CONNECTION_MANAGER); 2820 boolean permissionsOk = 2821 isCallerSimCallManagerForAnySim(account.getAccountHandle()) 2822 || mContext.checkCallingOrSelfPermission(REGISTER_SIM_SUBSCRIPTION) 2823 == PackageManager.PERMISSION_GRANTED; 2824 if (!prerequisiteCapabilitiesOk || !permissionsOk) { 2825 throw new SecurityException( 2826 "Only SIM subscriptions and connection managers are allowed to declare " 2827 + "CAPABILITY_SUPPORTS_VOICE_CALLING_INDICATIONS and " 2828 + "CAPABILITY_VOICE_CALLING_AVAILABLE"); 2829 } 2830 } 2831 enforceRegisterSkipCallFiltering()2832 private void enforceRegisterSkipCallFiltering() { 2833 if (!isCallerSystemApp()) { 2834 throw new SecurityException( 2835 "EXTRA_SKIP_CALL_FILTERING is only available to system apps."); 2836 } 2837 } 2838 enforceUserHandleMatchesCaller(PhoneAccountHandle accountHandle)2839 private void enforceUserHandleMatchesCaller(PhoneAccountHandle accountHandle) { 2840 if (!Binder.getCallingUserHandle().equals(accountHandle.getUserHandle())) { 2841 // Enforce INTERACT_ACROSS_USERS if the calling user handle does not match 2842 // phone account's user handle 2843 enforceInAppCrossUserPermission(); 2844 } 2845 } 2846 enforcePhoneAccountHandleMatchesCaller(PhoneAccountHandle phoneAccountHandle, String callingPackage)2847 private void enforcePhoneAccountHandleMatchesCaller(PhoneAccountHandle phoneAccountHandle, 2848 String callingPackage) { 2849 if (!callingPackage.equals(phoneAccountHandle.getComponentName().getPackageName())) { 2850 throw new SecurityException("Caller does not own the PhoneAccountHandle"); 2851 } 2852 } 2853 enforceCrossUserPermission(int callingUid)2854 private void enforceCrossUserPermission(int callingUid) { 2855 if (callingUid != Process.SYSTEM_UID && callingUid != 0) { 2856 mContext.enforceCallingOrSelfPermission( 2857 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL, "Must be system or have" 2858 + " INTERACT_ACROSS_USERS_FULL permission"); 2859 } 2860 } 2861 enforceInAppCrossUserPermission()2862 private void enforceInAppCrossUserPermission() { 2863 mContext.enforceCallingOrSelfPermission( 2864 android.Manifest.permission.INTERACT_ACROSS_USERS, "Must be system or have" 2865 + " INTERACT_ACROSS_USERS permission"); 2866 } 2867 hasInAppCrossUserPermission()2868 private boolean hasInAppCrossUserPermission() { 2869 return mContext.checkCallingOrSelfPermission( 2870 Manifest.permission.INTERACT_ACROSS_USERS) 2871 == PackageManager.PERMISSION_GRANTED; 2872 } 2873 2874 // to be used for TestApi methods that can only be called with SHELL UID. enforceShellOnly(int callingUid, String message)2875 private void enforceShellOnly(int callingUid, String message) { 2876 if (callingUid == Process.SHELL_UID || callingUid == Process.ROOT_UID) { 2877 return; // okay 2878 } 2879 2880 throw new SecurityException(message + ": Only shell user can call it"); 2881 } 2882 canReadPhoneState(String callingPackage, String callingFeatureId, String message)2883 private boolean canReadPhoneState(String callingPackage, String callingFeatureId, 2884 String message) { 2885 // The system/default dialer can always read phone state - so that emergency calls will 2886 // still work. 2887 if (isPrivilegedDialerCalling(callingPackage)) { 2888 return true; 2889 } 2890 2891 try { 2892 mContext.enforceCallingOrSelfPermission(READ_PRIVILEGED_PHONE_STATE, message); 2893 // SKIP checking run-time OP_READ_PHONE_STATE since caller or self has PRIVILEGED 2894 // permission 2895 return true; 2896 } catch (SecurityException e) { 2897 // Accessing phone state is gated by a special permission. 2898 mContext.enforceCallingOrSelfPermission(READ_PHONE_STATE, message); 2899 2900 // Some apps that have the permission can be restricted via app ops. 2901 return mAppOpsManager.noteOp(AppOpsManager.OP_READ_PHONE_STATE, Binder.getCallingUid(), 2902 callingPackage, callingFeatureId, message) == AppOpsManager.MODE_ALLOWED; 2903 } 2904 } 2905 canReadMangeOwnCalls(String message)2906 private boolean canReadMangeOwnCalls(String message) { 2907 try { 2908 mContext.enforceCallingOrSelfPermission(MANAGE_OWN_CALLS, message); 2909 return true; 2910 } catch (SecurityException e) { 2911 return false; 2912 } 2913 } 2914 canReadPhoneNumbers(String callingPackage, String callingFeatureId, String message)2915 private boolean canReadPhoneNumbers(String callingPackage, String callingFeatureId, 2916 String message) { 2917 boolean targetSdkPreR = false; 2918 int uid = Binder.getCallingUid(); 2919 try { 2920 ApplicationInfo applicationInfo = mPackageManager.getApplicationInfoAsUser( 2921 callingPackage, 0, UserHandle.getUserHandleForUid(Binder.getCallingUid())); 2922 targetSdkPreR = applicationInfo != null 2923 && applicationInfo.targetSdkVersion < Build.VERSION_CODES.R; 2924 } catch (PackageManager.NameNotFoundException e) { 2925 // In the case that the PackageManager cannot find the specified calling package apply 2926 // the more restrictive target R+ requirements. 2927 } 2928 // Apps targeting pre-R can access phone numbers via READ_PHONE_STATE 2929 if (targetSdkPreR) { 2930 try { 2931 return canReadPhoneState(callingPackage, callingFeatureId, message); 2932 } catch (SecurityException e) { 2933 // Apps targeting pre-R can still access phone numbers via the additional checks 2934 // below. 2935 } 2936 } else { 2937 // The system/default dialer can always read phone state - so that emergency calls will 2938 // still work. 2939 if (isPrivilegedDialerCalling(callingPackage)) { 2940 return true; 2941 } 2942 if (mContext.checkCallingOrSelfPermission(READ_PRIVILEGED_PHONE_STATE) 2943 == PackageManager.PERMISSION_GRANTED) { 2944 return true; 2945 } 2946 } 2947 if (mContext.checkCallingOrSelfPermission(READ_PHONE_NUMBERS) 2948 == PackageManager.PERMISSION_GRANTED && mAppOpsManager.noteOpNoThrow( 2949 AppOpsManager.OPSTR_READ_PHONE_NUMBERS, uid, callingPackage, callingFeatureId, 2950 message) == AppOpsManager.MODE_ALLOWED) { 2951 return true; 2952 } 2953 if (mContext.checkCallingOrSelfPermission(READ_SMS) == PackageManager.PERMISSION_GRANTED 2954 && mAppOpsManager.noteOpNoThrow(AppOpsManager.OPSTR_READ_SMS, uid, callingPackage, 2955 callingFeatureId, message) == AppOpsManager.MODE_ALLOWED) { 2956 return true; 2957 } 2958 // The default SMS app with the WRITE_SMS appop granted can access phone numbers. 2959 if (mAppOpsManager.noteOpNoThrow(AppOpsManager.OPSTR_WRITE_SMS, uid, callingPackage, 2960 callingFeatureId, message) == AppOpsManager.MODE_ALLOWED) { 2961 return true; 2962 } 2963 throw new SecurityException("Package " + callingPackage 2964 + " does not meet the requirements to access the phone number"); 2965 } 2966 2967 2968 private boolean canReadPrivilegedPhoneState(String callingPackage, String message) { 2969 // The system/default dialer can always read phone state - so that emergency calls will 2970 // still work. 2971 if (isPrivilegedDialerCalling(callingPackage)) { 2972 return true; 2973 } 2974 2975 mContext.enforceCallingOrSelfPermission(READ_PRIVILEGED_PHONE_STATE, message); 2976 return true; 2977 } 2978 2979 private boolean isDialerOrPrivileged(String callingPackage, String message) { 2980 // The system/default dialer can always read phone state - so that emergency calls will 2981 // still work. 2982 if (isPrivilegedDialerCalling(callingPackage)) { 2983 return true; 2984 } 2985 2986 mContext.enforceCallingOrSelfPermission(READ_PRIVILEGED_PHONE_STATE, message); 2987 // SKIP checking run-time OP_READ_PHONE_STATE since caller or self has PRIVILEGED 2988 // permission 2989 return true; 2990 } 2991 2992 private boolean isSelfManagedConnectionService(PhoneAccountHandle phoneAccountHandle) { 2993 if (phoneAccountHandle != null) { 2994 PhoneAccount phoneAccount = mPhoneAccountRegistrar.getPhoneAccountUnchecked( 2995 phoneAccountHandle); 2996 return phoneAccount != null && phoneAccount.isSelfManaged(); 2997 } 2998 return false; 2999 } 3000 3001 private boolean canCallPhone(String callingPackage, String message) { 3002 return canCallPhone(callingPackage, null /* featureId */, message); 3003 } 3004 3005 private boolean canCallPhone(String callingPackage, String callingFeatureId, String message) { 3006 // The system/default dialer can always read phone state - so that emergency calls will 3007 // still work. 3008 if (isPrivilegedDialerCalling(callingPackage)) { 3009 return true; 3010 } 3011 3012 if (mContext.checkCallingOrSelfPermission(CALL_PRIVILEGED) 3013 == PackageManager.PERMISSION_GRANTED) { 3014 return true; 3015 } 3016 3017 // Accessing phone state is gated by a special permission. 3018 mContext.enforceCallingOrSelfPermission(CALL_PHONE, message); 3019 3020 // Some apps that have the permission can be restricted via app ops. 3021 return mAppOpsManager.noteOp(AppOpsManager.OP_CALL_PHONE, 3022 Binder.getCallingUid(), callingPackage, callingFeatureId, message) 3023 == AppOpsManager.MODE_ALLOWED; 3024 } 3025 3026 private boolean canGetPhoneAccount(String callingPackage, PhoneAccountHandle accountHandle) { 3027 // Allow default dialer, system dialer and sim call manager to be able to do this without 3028 // extra permission 3029 try { 3030 if (isPrivilegedDialerCalling(callingPackage) || isCallerSimCallManager( 3031 accountHandle)) { 3032 return true; 3033 } 3034 } catch (SecurityException e) { 3035 // ignore 3036 } 3037 3038 try { 3039 mContext.enforceCallingOrSelfPermission(READ_PRIVILEGED_PHONE_STATE, null); 3040 return true; 3041 } catch (SecurityException e) { 3042 // Accessing phone state is gated by a special permission. 3043 mContext.enforceCallingOrSelfPermission(READ_PHONE_NUMBERS, null); 3044 return true; 3045 } 3046 } 3047 3048 private boolean isCallerSimCallManager(PhoneAccountHandle targetPhoneAccount) { 3049 long token = Binder.clearCallingIdentity(); 3050 PhoneAccountHandle accountHandle = null; 3051 try { 3052 accountHandle = mPhoneAccountRegistrar.getSimCallManagerFromHandle(targetPhoneAccount, 3053 mCallsManager.getCurrentUserHandle()); 3054 } finally { 3055 Binder.restoreCallingIdentity(token); 3056 } 3057 3058 if (accountHandle != null) { 3059 try { 3060 mAppOpsManager.checkPackage( 3061 Binder.getCallingUid(), accountHandle.getComponentName().getPackageName()); 3062 return true; 3063 } catch (SecurityException e) { 3064 } 3065 } 3066 return false; 3067 } 3068 3069 /** 3070 * Similar to {@link #isCallerSimCallManager}, but works for all SIMs and does not require 3071 * {@code accountHandle} to be registered yet. 3072 */ 3073 private boolean isCallerSimCallManagerForAnySim(PhoneAccountHandle accountHandle) { 3074 if (isCallerSimCallManager(accountHandle)) { 3075 // The caller has already registered a CONNECTION_MANAGER PhoneAccount, so let them pass 3076 // (this allows the SIM call manager through in case of SIM switches, where carrier 3077 // config may be in a transient state) 3078 return true; 3079 } 3080 // If the caller isn't already registered, then we have to look at the active PSTN 3081 // PhoneAccounts and check their carrier configs to see if any point to this one's component 3082 final long token = Binder.clearCallingIdentity(); 3083 try { 3084 return !mPhoneAccountRegistrar 3085 .getSimPhoneAccountsFromSimCallManager(accountHandle) 3086 .isEmpty(); 3087 } finally { 3088 Binder.restoreCallingIdentity(token); 3089 } 3090 } 3091 3092 private boolean isPrivilegedDialerCalling(String callingPackage) { 3093 mAppOpsManager.checkPackage(Binder.getCallingUid(), callingPackage); 3094 3095 // Note: Important to clear the calling identity since the code below calls into RoleManager 3096 // to check who holds the dialer role, and that requires MANAGE_ROLE_HOLDERS permission 3097 // which is a system permission. 3098 long token = Binder.clearCallingIdentity(); 3099 try { 3100 return mDefaultDialerCache.isDefaultOrSystemDialer( 3101 callingPackage, Binder.getCallingUserHandle().getIdentifier()); 3102 } finally { 3103 Binder.restoreCallingIdentity(token); 3104 } 3105 } 3106 3107 private TelephonyManager getTelephonyManager(int subId) { 3108 return ((TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE)) 3109 .createForSubscriptionId(subId); 3110 } 3111 3112 /** 3113 * Determines if a video state is valid for accepting an incoming call. 3114 * For the purpose of accepting a call, states {@link VideoProfile#STATE_AUDIO_ONLY}, and 3115 * any combination of {@link VideoProfile#STATE_RX_ENABLED} and 3116 * {@link VideoProfile#STATE_TX_ENABLED} are considered valid. 3117 * 3118 * @param videoState The video state. 3119 * @return {@code true} if the video state is valid, {@code false} otherwise. 3120 */ 3121 private boolean isValidAcceptVideoState(int videoState) { 3122 // Given a video state input, turn off TX and RX so that we can determine if those were the 3123 // only bits set. 3124 int remainingState = videoState & ~VideoProfile.STATE_TX_ENABLED; 3125 remainingState = remainingState & ~VideoProfile.STATE_RX_ENABLED; 3126 3127 // If only TX or RX were set (or neither), the video state is valid. 3128 return remainingState == 0; 3129 } 3130 3131 private void broadcastCallScreeningAppChangedIntent(String componentName, 3132 boolean isDefault) { 3133 if (TextUtils.isEmpty(componentName)) { 3134 return; 3135 } 3136 3137 ComponentName broadcastComponentName = ComponentName.unflattenFromString(componentName); 3138 3139 if (broadcastComponentName != null) { 3140 Intent intent = new Intent(TelecomManager 3141 .ACTION_DEFAULT_CALL_SCREENING_APP_CHANGED); 3142 intent.putExtra(TelecomManager 3143 .EXTRA_IS_DEFAULT_CALL_SCREENING_APP, isDefault); 3144 intent.putExtra(TelecomManager 3145 .EXTRA_DEFAULT_CALL_SCREENING_APP_COMPONENT_NAME, componentName); 3146 intent.setPackage(broadcastComponentName.getPackageName()); 3147 mContext.sendBroadcast(intent); 3148 } 3149 } 3150 3151 private void validateAccountIconUserBoundary(Icon icon) { 3152 // Refer to Icon#getUriString for context. The URI string is invalid for icons of 3153 // incompatible types. 3154 if (icon != null && (icon.getType() == Icon.TYPE_URI 3155 || icon.getType() == Icon.TYPE_URI_ADAPTIVE_BITMAP)) { 3156 String encodedUser = icon.getUri().getEncodedUserInfo(); 3157 // If there is no encoded user, the URI is calling into the calling user space 3158 if (encodedUser != null) { 3159 int userId = Integer.parseInt(encodedUser); 3160 if (userId != UserHandle.getUserId(Binder.getCallingUid())) { 3161 // If we are transcending the profile boundary, throw an error. 3162 throw new IllegalArgumentException("Attempting to register a phone account with" 3163 + " an image icon belonging to another user."); 3164 } 3165 } 3166 } 3167 } 3168 } 3169