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