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