NewOutgoingCallIntentBroadcaster.java revision 905dfba7883666f45a0c6958d8bc6c19d68972d9
133501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee/*
233501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee * Copyright (C) 2014 The Android Open Source Project
333501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee *
433501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee * Licensed under the Apache License, Version 2.0 (the "License");
533501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee * you may not use this file except in compliance with the License.
633501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee * You may obtain a copy of the License at
733501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee *
833501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee *      http://www.apache.org/licenses/LICENSE-2.0
933501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee *
1033501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee * Unless required by applicable law or agreed to in writing, software
1133501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee * distributed under the License is distributed on an "AS IS" BASIS,
1233501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1333501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee * See the License for the specific language governing permissions and
1433501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee * limitations under the License.
1533501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee */
1633501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee
1733501635c2cd21f40793041eff3b8ce3a5710d49Yorke Leepackage com.android.telecomm;
1833501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee
1933501635c2cd21f40793041eff3b8ce3a5710d49Yorke Leeimport android.app.Activity;
2033501635c2cd21f40793041eff3b8ce3a5710d49Yorke Leeimport android.content.BroadcastReceiver;
2133501635c2cd21f40793041eff3b8ce3a5710d49Yorke Leeimport android.content.Context;
2233501635c2cd21f40793041eff3b8ce3a5710d49Yorke Leeimport android.content.Intent;
2333501635c2cd21f40793041eff3b8ce3a5710d49Yorke Leeimport android.content.res.Resources;
2433501635c2cd21f40793041eff3b8ce3a5710d49Yorke Leeimport android.net.Uri;
2533501635c2cd21f40793041eff3b8ce3a5710d49Yorke Leeimport android.os.UserHandle;
2633501635c2cd21f40793041eff3b8ce3a5710d49Yorke Leeimport android.telecomm.GatewayInfo;
2798a556026109f816501fa887efdddae78d0c2d65Ihab Awadimport android.telecomm.PhoneAccount;
2833501635c2cd21f40793041eff3b8ce3a5710d49Yorke Leeimport android.telecomm.TelecommConstants;
29c4abd91cd8a67b530ebafe146af10136db8e6605Tyler Gunnimport android.telecomm.VideoCallProfile;
3033501635c2cd21f40793041eff3b8ce3a5710d49Yorke Leeimport android.telephony.PhoneNumberUtils;
3133501635c2cd21f40793041eff3b8ce3a5710d49Yorke Leeimport android.text.TextUtils;
3233501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee
3333501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee/**
3433501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee * OutgoingCallIntentBroadcaster receives CALL and CALL_PRIVILEGED Intents, and broadcasts the
3533501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee * ACTION_NEW_OUTGOING_CALL intent. ACTION_NEW_OUTGOING_CALL is an ordered broadcast intent which
3633501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee * contains the phone number being dialed. Applications can use this intent to (1) see which numbers
3733501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee * are being dialed, (2) redirect a call (change the number being dialed), or (3) prevent a call
3833501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee * from being placed.
3933501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee *
4033501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee * After the other applications have had a chance to see the ACTION_NEW_OUTGOING_CALL intent, it
4133501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee * finally reaches the {@link NewOutgoingCallBroadcastIntentReceiver}.
4233501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee *
4333501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee * Calls where no number is present (like for a CDMA "empty flash" or a nonexistent voicemail
4433501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee * number) are exempt from being broadcast.
4533501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee *
4633501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee * Calls to emergency numbers are still broadcast for informative purposes. The call is placed
4733501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee * prior to sending ACTION_NEW_OUTGOING_CALL and cannot be redirected nor prevented.
4833501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee */
4933501635c2cd21f40793041eff3b8ce3a5710d49Yorke Leeclass NewOutgoingCallIntentBroadcaster {
5033501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee    /** Required permission for any app that wants to consume ACTION_NEW_OUTGOING_CALL. */
5133501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee    private static final String PERMISSION = android.Manifest.permission.PROCESS_OUTGOING_CALLS;
5233501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee
5333501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee    private static final String EXTRA_ACTUAL_NUMBER_TO_DIAL =
5433501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee            "android.telecomm.extra.ACTUAL_NUMBER_TO_DIAL";
5533501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee
5633501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee    /**
5733501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee     * Legacy string constants used to retrieve gateway provider extras from intents. These still
5833501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee     * need to be copied from the source call intent to the destination intent in order to
5933501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee     * support third party gateway providers that are still using old string constants in
6033501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee     * Telephony.
6133501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee     */
6233501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee    public static final String EXTRA_GATEWAY_PROVIDER_PACKAGE =
6333501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee            "com.android.phone.extra.GATEWAY_PROVIDER_PACKAGE";
6433501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee    public static final String EXTRA_GATEWAY_URI = "com.android.phone.extra.GATEWAY_URI";
65571f0737002251f09b9ef696dc8b4c9ce02abbaeSantos Cordon    public static final String EXTRA_GATEWAY_ORIGINAL_URI =
66571f0737002251f09b9ef696dc8b4c9ce02abbaeSantos Cordon            "com.android.phone.extra.GATEWAY_ORIGINAL_URI";
6733501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee
68b9d4b3612c4ba389a66ab95644d64a444c158aaaYorke Lee    private static final String SCHEME_TEL = "tel";
69b9d4b3612c4ba389a66ab95644d64a444c158aaaYorke Lee    private static final String SCHEME_SIP = "sip";
70b9d4b3612c4ba389a66ab95644d64a444c158aaaYorke Lee
7133501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee    private final CallsManager mCallsManager;
7233501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee    private final Intent mIntent;
7333501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee
746f3f7affb13e93cb70868d3114f02f1ed593194eYorke Lee    NewOutgoingCallIntentBroadcaster(CallsManager callsManager, Intent intent) {
7533501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee        mCallsManager = callsManager;
7633501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee        mIntent = intent;
7733501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee    }
7833501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee
7933501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee    /**
8033501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee     * Processes the result of the outgoing call broadcast intent, and performs callbacks to
8133501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee     * the OutgoingCallIntentBroadcasterListener as necessary.
8233501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee     */
8333501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee    private class NewOutgoingCallBroadcastIntentReceiver extends BroadcastReceiver {
8433501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee
8533501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee        @Override
8633501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee        public void onReceive(Context context, Intent intent) {
87b33087514fb354c9d10c29f9cb071ed2ae2a6124Sailesh Nepal            Log.v(this, "onReceive: %s", intent);
8833501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee
8933501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee            // Once the NEW_OUTGOING_CALL broadcast is finished, the resultData is used as the
9033501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee            // actual number to call. (If null, no call will be placed.)
9133501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee            String resultHandle = getResultData();
9233501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee            Log.v(this, "- got number from resultData: %s", Log.pii(resultHandle));
9333501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee
9433501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee            if (resultHandle == null) {
9533501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee                Log.v(this, "Call cancelled (null number), returning...");
9633501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee                return;
976625545769bd9c5aa15b7e1ee9402f75ad799e81Yorke Lee            } else if (PhoneNumberUtils.isPotentialLocalEmergencyNumber(context, resultHandle)) {
9833501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee                Log.w(this, "Cannot modify outgoing call to emergency number %s.", resultHandle);
9933501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee                return;
10033501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee            }
10133501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee
102b9d4b3612c4ba389a66ab95644d64a444c158aaaYorke Lee            Uri resultHandleUri = Uri.fromParts(
103b9d4b3612c4ba389a66ab95644d64a444c158aaaYorke Lee                    PhoneNumberUtils.isUriNumber(resultHandle) ? SCHEME_SIP : SCHEME_TEL,
104b9d4b3612c4ba389a66ab95644d64a444c158aaaYorke Lee                    resultHandle,
105b9d4b3612c4ba389a66ab95644d64a444c158aaaYorke Lee                    null);
10633501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee
10733501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee            Uri originalUri = mIntent.getData();
10833501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee
10933501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee            if (originalUri.getSchemeSpecificPart().equals(resultHandle)) {
11033501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee                Log.v(this, "Call handle unmodified after new outgoing call intent broadcast.");
11133501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee            } else {
11233501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee                Log.v(this, "Retrieved modified handle after outgoing call intent broadcast: "
11333501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee                        + "Original: %s, Modified: %s",
11433501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee                        Log.pii(originalUri),
11533501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee                        Log.pii(resultHandleUri));
11633501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee            }
11733501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee
11833501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee            GatewayInfo gatewayInfo = getGateWayInfoFromIntent(intent, resultHandleUri);
11998a556026109f816501fa887efdddae78d0c2d65Ihab Awad            PhoneAccount account = getAccountFromIntent(intent);
1206f3f7affb13e93cb70868d3114f02f1ed593194eYorke Lee            mCallsManager.placeOutgoingCall(resultHandleUri, gatewayInfo, account,
121b7157e9f7dbf151c4c6ff4ebcc171632af00616eSai Cheemalapati                    mIntent.getBooleanExtra(TelecommConstants.EXTRA_START_CALL_WITH_SPEAKERPHONE,
122c4abd91cd8a67b530ebafe146af10136db8e6605Tyler Gunn                            false),
123c4abd91cd8a67b530ebafe146af10136db8e6605Tyler Gunn                    mIntent.getIntExtra(TelecommConstants.EXTRA_START_CALL_WITH_VIDEO_STATE,
124c4abd91cd8a67b530ebafe146af10136db8e6605Tyler Gunn                            VideoCallProfile.VIDEO_STATE_AUDIO_ONLY));
12533501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee        }
12633501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee    }
12733501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee
12833501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee    /**
12933501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee     * Processes the supplied intent and starts the outgoing call broadcast process relevant to the
13033501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee     * intent.
13133501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee     *
13233501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee     * This method will handle three kinds of actions:
13333501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee     *
13433501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee     * - CALL (intent launched by all third party dialers)
13533501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee     * - CALL_PRIVILEGED (intent launched by system apps e.g. system Dialer, voice Dialer)
13633501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee     * - CALL_EMERGENCY (intent launched by lock screen emergency dialer)
13733501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee     */
138d362fe3e5d15301ca1d3612c7caa0c4ed05f128aYorke Lee    void processIntent() {
13933501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee        Log.v(this, "Processing call intent in OutgoingCallIntentBroadcaster.");
14033501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee
14133501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee        final Context context = TelecommApp.getInstance();
14233501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee        Intent intent = mIntent;
14333501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee
14433501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee        String handle = PhoneNumberUtils.getNumberFromIntent(intent, context);
14533501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee
14633501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee        if (TextUtils.isEmpty(handle)) {
14733501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee            Log.w(this, "Empty handle obtained from the call intent.");
148d362fe3e5d15301ca1d3612c7caa0c4ed05f128aYorke Lee            return;
14933501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee        }
15033501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee
151b58f453c46a695347a69f8c973a77796e5c1fdf7Santos Cordon        boolean isUriNumber = PhoneNumberUtils.isUriNumber(handle);
152b58f453c46a695347a69f8c973a77796e5c1fdf7Santos Cordon
153b58f453c46a695347a69f8c973a77796e5c1fdf7Santos Cordon        if (!isUriNumber) {
15433501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee            handle = PhoneNumberUtils.convertKeypadLettersToDigits(handle);
15533501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee            handle = PhoneNumberUtils.stripSeparators(handle);
15633501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee        }
15733501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee
15833501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee        final boolean isPotentialEmergencyNumber = isPotentialEmergencyNumber(context, handle);
15933501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee        Log.v(this, "isPotentialEmergencyNumber = %s", isPotentialEmergencyNumber);
16033501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee
16133501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee        rewriteCallIntentAction(intent, isPotentialEmergencyNumber);
16233501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee        // True for certain types of numbers that are not intended to be intercepted or modified
16333501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee        // by third parties (e.g. emergency numbers).
16433501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee        boolean callImmediately = false;
16533501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee
16633501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee        String action = intent.getAction();
16733501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee        if (Intent.ACTION_CALL.equals(action)) {
16833501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee            if (isPotentialEmergencyNumber) {
169d362fe3e5d15301ca1d3612c7caa0c4ed05f128aYorke Lee                Log.w(this, "Cannot call potential emergency number %s with CALL Intent %s.",
170d362fe3e5d15301ca1d3612c7caa0c4ed05f128aYorke Lee                        handle, intent);
171d362fe3e5d15301ca1d3612c7caa0c4ed05f128aYorke Lee                launchSystemDialer(context, intent.getData());
17233501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee            }
173d362fe3e5d15301ca1d3612c7caa0c4ed05f128aYorke Lee            callImmediately = false;
17433501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee        } else if (Intent.ACTION_CALL_EMERGENCY.equals(action)) {
17533501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee            if (!isPotentialEmergencyNumber) {
17633501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee                Log.w(this, "Cannot call non-potential-emergency number %s with EMERGENCY_CALL "
17733501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee                        + "Intent %s.", handle, intent);
178d362fe3e5d15301ca1d3612c7caa0c4ed05f128aYorke Lee                return;
17933501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee            }
18033501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee            callImmediately = true;
18133501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee        } else {
18233501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee            Log.w(this, "Unhandled Intent %s. Ignoring and not placing call.", intent);
183d362fe3e5d15301ca1d3612c7caa0c4ed05f128aYorke Lee            return;
18433501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee        }
18533501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee
18633501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee        if (callImmediately) {
18733501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee            Log.i(this, "Placing call immediately instead of waiting for "
18833501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee                    + " OutgoingCallBroadcastReceiver: %s", intent);
189b58f453c46a695347a69f8c973a77796e5c1fdf7Santos Cordon            String scheme = isUriNumber ? SCHEME_SIP : SCHEME_TEL;
190905dfba7883666f45a0c6958d8bc6c19d68972d9Sailesh Nepal            boolean speakerphoneOn = mIntent.getBooleanExtra(
191905dfba7883666f45a0c6958d8bc6c19d68972d9Sailesh Nepal                    TelecommConstants.EXTRA_START_CALL_WITH_SPEAKERPHONE, false);
192905dfba7883666f45a0c6958d8bc6c19d68972d9Sailesh Nepal            int videoState = mIntent.getIntExtra(
193905dfba7883666f45a0c6958d8bc6c19d68972d9Sailesh Nepal                    TelecommConstants.EXTRA_START_CALL_WITH_VIDEO_STATE,
194905dfba7883666f45a0c6958d8bc6c19d68972d9Sailesh Nepal                    VideoCallProfile.VIDEO_STATE_AUDIO_ONLY);
195b58f453c46a695347a69f8c973a77796e5c1fdf7Santos Cordon            mCallsManager.placeOutgoingCall(
196905dfba7883666f45a0c6958d8bc6c19d68972d9Sailesh Nepal                    Uri.fromParts(scheme, handle, null), null, null, speakerphoneOn, videoState);
19733501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee
19833501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee            // Don't return but instead continue and send the ACTION_NEW_OUTGOING_CALL broadcast
19933501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee            // so that third parties can still inspect (but not intercept) the outgoing call. When
20033501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee            // the broadcast finally reaches the OutgoingCallBroadcastReceiver, we'll know not to
20133501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee            // initiate the call again because of the presence of the EXTRA_ALREADY_CALLED extra.
20233501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee        }
20333501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee
20433501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee        broadcastIntent(intent, handle, context, !callImmediately);
20533501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee    }
20633501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee
20733501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee    /**
20833501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee     * Sends a new outgoing call ordered broadcast so that third party apps can cancel the
20933501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee     * placement of the call or redirect it to a different number.
21033501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee     *
21133501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee     * @param originalCallIntent The original call intent.
21233501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee     * @param handle Call handle that was stored in the original call intent.
21333501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee     * @param context Valid context to send the ordered broadcast using.
21433501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee     * @param receiverRequired Whether or not the result from the ordered broadcast should be
21533501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee     *     processed using a {@link NewOutgoingCallIntentBroadcaster}.
21633501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee     */
21733501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee    private void broadcastIntent(
21833501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee            Intent originalCallIntent,
21933501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee            String handle,
22033501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee            Context context,
22133501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee            boolean receiverRequired) {
22233501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee        Intent broadcastIntent = new Intent(Intent.ACTION_NEW_OUTGOING_CALL);
22333501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee        if (handle != null) {
22433501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee            broadcastIntent.putExtra(Intent.EXTRA_PHONE_NUMBER, handle);
22533501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee        }
22633501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee
22733501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee        // Force receivers of this broadcast intent to run at foreground priority because we
22833501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee        // want to finish processing the broadcast intent as soon as possible.
22933501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee        broadcastIntent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
23033501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee        Log.v(this, "Broadcasting intent: %s.", broadcastIntent);
23133501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee
23277d2d0e90bc2a67d526743b8e32f920a0d755fbaNancy Chen        checkAndCopyProviderExtras(originalCallIntent, broadcastIntent);
23333501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee
23433501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee        context.sendOrderedBroadcastAsUser(
23533501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee                broadcastIntent,
23633501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee                UserHandle.OWNER,
23733501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee                PERMISSION,
23833501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee                receiverRequired ? new NewOutgoingCallBroadcastIntentReceiver() : null,
23933501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee                null,  // scheduler
24033501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee                Activity.RESULT_OK,  // initialCode
24133501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee                handle,  // initialData: initial value for the result data (number to be modified)
24233501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee                null);  // initialExtras
24333501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee    }
24433501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee
24533501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee    /**
24633501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee     * Copy all the expected extras set when a 3rd party gateway provider is to be used, from the
24733501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee     * source intent to the destination one.
24833501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee     *
24933501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee     * @param src Intent which may contain the provider's extras.
25033501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee     * @param dst Intent where a copy of the extras will be added if applicable.
25133501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee     */
25277d2d0e90bc2a67d526743b8e32f920a0d755fbaNancy Chen    public void checkAndCopyProviderExtras(Intent src, Intent dst) {
25377d2d0e90bc2a67d526743b8e32f920a0d755fbaNancy Chen        if (src == null) {
25477d2d0e90bc2a67d526743b8e32f920a0d755fbaNancy Chen            return;
25577d2d0e90bc2a67d526743b8e32f920a0d755fbaNancy Chen        }
25633501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee        if (hasGatewayProviderExtras(src)) {
25733501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee            dst.putExtra(EXTRA_GATEWAY_PROVIDER_PACKAGE,
25833501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee                    src.getStringExtra(EXTRA_GATEWAY_PROVIDER_PACKAGE));
25933501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee            dst.putExtra(EXTRA_GATEWAY_URI,
26033501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee                    src.getStringExtra(EXTRA_GATEWAY_URI));
26133501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee            Log.d(this, "Found and copied gateway provider extras to broadcast intent.");
26233501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee            return;
26333501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee        }
264905dfba7883666f45a0c6958d8bc6c19d68972d9Sailesh Nepal        PhoneAccount extraAccount = src.getParcelableExtra(TelecommConstants.EXTRA_PHONE_ACCOUNT);
26598a556026109f816501fa887efdddae78d0c2d65Ihab Awad        if (extraAccount != null) {
266905dfba7883666f45a0c6958d8bc6c19d68972d9Sailesh Nepal            dst.putExtra(TelecommConstants.EXTRA_PHONE_ACCOUNT, extraAccount);
26798a556026109f816501fa887efdddae78d0c2d65Ihab Awad            Log.d(this, "Found and copied account extra to broadcast intent.");
26877d2d0e90bc2a67d526743b8e32f920a0d755fbaNancy Chen        }
26933501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee
27077d2d0e90bc2a67d526743b8e32f920a0d755fbaNancy Chen        Log.d(this, "No provider extras found in call intent.");
27133501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee    }
27233501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee
27333501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee    /**
27433501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee     * Check if valid gateway provider information is stored as extras in the intent
27533501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee     *
27633501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee     * @param intent to check for
27733501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee     * @return true if the intent has all the gateway information extras needed.
27833501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee     */
27933501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee    private boolean hasGatewayProviderExtras(Intent intent) {
28033501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee        final String name = intent.getStringExtra(EXTRA_GATEWAY_PROVIDER_PACKAGE);
28133501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee        final String uriString = intent.getStringExtra(EXTRA_GATEWAY_URI);
28233501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee
28333501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee        return !TextUtils.isEmpty(name) && !TextUtils.isEmpty(uriString);
28433501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee    }
28533501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee
28633501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee    private static Uri getGatewayUriFromString(String gatewayUriString) {
28733501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee        return TextUtils.isEmpty(gatewayUriString) ? null : Uri.parse(gatewayUriString);
28833501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee    }
28933501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee
29033501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee    /**
29133501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee     * Extracts gateway provider information from a provided intent..
29233501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee     *
29333501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee     * @param intent to extract gateway provider information from.
29433501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee     * @param trueHandle The actual call handle that the user is trying to dial
29533501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee     * @return GatewayInfo object containing extracted gateway provider information as well as
29633501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee     *     the actual handle the user is trying to dial.
29733501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee     */
29833501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee    public static GatewayInfo getGateWayInfoFromIntent(Intent intent, Uri trueHandle) {
29933501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee        if (intent == null) {
30033501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee            return null;
30133501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee        }
30233501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee
30333501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee        // Check if gateway extras are present.
30433501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee        String gatewayPackageName = intent.getStringExtra(EXTRA_GATEWAY_PROVIDER_PACKAGE);
30533501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee        Uri gatewayUri = getGatewayUriFromString(intent.getStringExtra(EXTRA_GATEWAY_URI));
30633501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee        if (!TextUtils.isEmpty(gatewayPackageName) && gatewayUri != null) {
30733501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee            return new GatewayInfo(gatewayPackageName, gatewayUri, trueHandle);
30833501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee        }
30933501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee
31033501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee        return null;
31133501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee    }
31233501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee
31377d2d0e90bc2a67d526743b8e32f920a0d755fbaNancy Chen    /**
31498a556026109f816501fa887efdddae78d0c2d65Ihab Awad     * Extracts account/connection provider information from a provided intent..
31577d2d0e90bc2a67d526743b8e32f920a0d755fbaNancy Chen     *
31698a556026109f816501fa887efdddae78d0c2d65Ihab Awad     * @param intent to extract account information from.
31798a556026109f816501fa887efdddae78d0c2d65Ihab Awad     * @return Account object containing extracted account information
31877d2d0e90bc2a67d526743b8e32f920a0d755fbaNancy Chen     */
31998a556026109f816501fa887efdddae78d0c2d65Ihab Awad    public static PhoneAccount getAccountFromIntent(Intent intent) {
32077d2d0e90bc2a67d526743b8e32f920a0d755fbaNancy Chen        if (intent == null) {
32177d2d0e90bc2a67d526743b8e32f920a0d755fbaNancy Chen            return null;
32277d2d0e90bc2a67d526743b8e32f920a0d755fbaNancy Chen        }
32377d2d0e90bc2a67d526743b8e32f920a0d755fbaNancy Chen
324905dfba7883666f45a0c6958d8bc6c19d68972d9Sailesh Nepal        return intent.getParcelableExtra(TelecommConstants.EXTRA_PHONE_ACCOUNT);
32577d2d0e90bc2a67d526743b8e32f920a0d755fbaNancy Chen    }
32677d2d0e90bc2a67d526743b8e32f920a0d755fbaNancy Chen
32733501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee    private void launchSystemDialer(Context context, Uri handle) {
32833501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee        Intent systemDialerIntent = new Intent();
32933501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee        final Resources resources = context.getResources();
33033501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee        systemDialerIntent.setClassName(
33133501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee                resources.getString(R.string.ui_default_package),
33233501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee                resources.getString(R.string.dialer_default_class));
33333501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee        systemDialerIntent.setAction(Intent.ACTION_DIAL);
33433501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee        systemDialerIntent.setData(handle);
33533501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee        systemDialerIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
33633501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee        Log.v(this, "calling startActivity for default dialer: %s", systemDialerIntent);
33733501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee        context.startActivity(systemDialerIntent);
33833501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee    }
33933501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee
34033501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee    /**
34133501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee     * Check whether or not this is an emergency number, in order to enforce the restriction
34233501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee     * that only the CALL_PRIVILEGED and CALL_EMERGENCY intents are allowed to make emergency
34333501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee     * calls.
34433501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee     *
34533501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee     * To prevent malicious 3rd party apps from making emergency calls by passing in an
34633501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee     * "invalid" number like "9111234" (that isn't technically an emergency number but might
34733501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee     * still result in an emergency call with some networks), we use
34833501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee     * isPotentialLocalEmergencyNumber instead of isLocalEmergencyNumber.
34933501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee     *
35033501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee     * @param context Valid context
35133501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee     * @param handle Handle to inspect in order to determine whether or not an emergency number
35233501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee     * is potentially being dialed
35333501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee     * @return True if the handle is potentially an emergency number.
35433501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee     */
35533501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee    private boolean isPotentialEmergencyNumber(Context context, String handle) {
35633501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee        Log.v(this, "Checking restrictions for number : %s", Log.pii(handle));
3576625545769bd9c5aa15b7e1ee9402f75ad799e81Yorke Lee        return (handle != null) && PhoneNumberUtils.isPotentialLocalEmergencyNumber(context,handle);
35833501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee    }
35933501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee
36033501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee    /**
36133501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee     * Given a call intent and whether or not the number to dial is an emergency number, rewrite
36233501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee     * the call intent action to an appropriate one.
36333501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee     *
36433501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee     * @param intent Intent to rewrite the action for
36533501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee     * @param isPotentialEmergencyNumber Whether or not the handle is potentially an emergency
36633501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee     * number.
36733501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee     */
36833501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee    private void rewriteCallIntentAction(Intent intent, boolean isPotentialEmergencyNumber) {
36933501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee        if (CallActivity.class.getName().equals(intent.getComponent().getClassName())) {
37033501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee            // If we were launched directly from the CallActivity, not one of its more privileged
37133501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee            // aliases, then make sure that only the non-privileged actions are allowed.
37233501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee            if (!Intent.ACTION_CALL.equals(intent.getAction())) {
37333501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee                Log.w(this, "Attempt to deliver non-CALL action; forcing to CALL");
37433501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee                intent.setAction(Intent.ACTION_CALL);
37533501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee            }
37633501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee        }
37733501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee
37833501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee        String action = intent.getAction();
37933501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee
38033501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee        /* Change CALL_PRIVILEGED into CALL or CALL_EMERGENCY as needed. */
38133501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee        if (Intent.ACTION_CALL_PRIVILEGED.equals(action)) {
38233501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee            if (isPotentialEmergencyNumber) {
38333501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee                Log.i(this, "ACTION_CALL_PRIVILEGED is used while the number is a potential"
38433501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee                        + " emergency number. Using ACTION_CALL_EMERGENCY as an action instead.");
38533501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee                action = Intent.ACTION_CALL_EMERGENCY;
38633501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee            } else {
38733501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee                action = Intent.ACTION_CALL;
38833501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee            }
38933501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee            Log.v(this, " - updating action from CALL_PRIVILEGED to %s", action);
39033501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee            intent.setAction(action);
39133501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee        }
39233501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee    }
39333501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee}
394