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
177cc70b4f0ad1064a4a0dce6056ad82b205887160Tyler Gunnpackage com.android.server.telecom;
1833501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee
19973c4e1545581f7cb8a1bf8a7b3cdf5a8f38d01aSvetoslavimport android.app.AppOpsManager;
208d5d9ddc66b55b6906364ab3c0e244dab4d58f13Ihab Awad
2133501635c2cd21f40793041eff3b8ce3a5710d49Yorke Leeimport android.app.Activity;
2233501635c2cd21f40793041eff3b8ce3a5710d49Yorke Leeimport android.content.BroadcastReceiver;
2333501635c2cd21f40793041eff3b8ce3a5710d49Yorke Leeimport android.content.Context;
2433501635c2cd21f40793041eff3b8ce3a5710d49Yorke Leeimport android.content.Intent;
2533501635c2cd21f40793041eff3b8ce3a5710d49Yorke Leeimport android.content.res.Resources;
2633501635c2cd21f40793041eff3b8ce3a5710d49Yorke Leeimport android.net.Uri;
27ad18dc9e336c73922192e1693a836781cff6c929Hall Liuimport android.os.Bundle;
28e4a9c412f5c6f76f006659f0b4e171ef4547dc45Yorke Leeimport android.os.Trace;
2933501635c2cd21f40793041eff3b8ce3a5710d49Yorke Leeimport android.os.UserHandle;
307cc70b4f0ad1064a4a0dce6056ad82b205887160Tyler Gunnimport android.telecom.GatewayInfo;
317cc70b4f0ad1064a4a0dce6056ad82b205887160Tyler Gunnimport android.telecom.PhoneAccount;
327cc70b4f0ad1064a4a0dce6056ad82b205887160Tyler Gunnimport android.telecom.TelecomManager;
337cc70b4f0ad1064a4a0dce6056ad82b205887160Tyler Gunnimport android.telecom.VideoProfile;
34d7255872e2204a074bc5dfb5dceb4da13574864cYorke Leeimport android.telephony.DisconnectCause;
3533501635c2cd21f40793041eff3b8ce3a5710d49Yorke Leeimport android.text.TextUtils;
3633501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee
37220b419bb5d1b4d1df08415c8e52cb7f0decbf7cHall Liuimport com.android.internal.annotations.VisibleForTesting;
38220b419bb5d1b4d1df08415c8e52cb7f0decbf7cHall Liu
3991d43cf9c985cc5a83795f256ef5c46ebb8fbdc1Tyler Gunn// TODO: Needed for move to system service: import com.android.internal.R;
4091d43cf9c985cc5a83795f256ef5c46ebb8fbdc1Tyler Gunn
4133501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee/**
4233501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee * OutgoingCallIntentBroadcaster receives CALL and CALL_PRIVILEGED Intents, and broadcasts the
4333501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee * ACTION_NEW_OUTGOING_CALL intent. ACTION_NEW_OUTGOING_CALL is an ordered broadcast intent which
4433501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee * contains the phone number being dialed. Applications can use this intent to (1) see which numbers
4533501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee * are being dialed, (2) redirect a call (change the number being dialed), or (3) prevent a call
4633501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee * from being placed.
4733501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee *
4833501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee * After the other applications have had a chance to see the ACTION_NEW_OUTGOING_CALL intent, it
4933501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee * finally reaches the {@link NewOutgoingCallBroadcastIntentReceiver}.
5033501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee *
5133501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee * Calls where no number is present (like for a CDMA "empty flash" or a nonexistent voicemail
5233501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee * number) are exempt from being broadcast.
5333501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee *
5433501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee * Calls to emergency numbers are still broadcast for informative purposes. The call is placed
5533501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee * prior to sending ACTION_NEW_OUTGOING_CALL and cannot be redirected nor prevented.
5633501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee */
57220b419bb5d1b4d1df08415c8e52cb7f0decbf7cHall Liu@VisibleForTesting
58220b419bb5d1b4d1df08415c8e52cb7f0decbf7cHall Liupublic class NewOutgoingCallIntentBroadcaster {
5933501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee    /**
6033501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee     * Legacy string constants used to retrieve gateway provider extras from intents. These still
6133501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee     * need to be copied from the source call intent to the destination intent in order to
6233501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee     * support third party gateway providers that are still using old string constants in
6333501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee     * Telephony.
6433501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee     */
6533501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee    public static final String EXTRA_GATEWAY_PROVIDER_PACKAGE =
6633501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee            "com.android.phone.extra.GATEWAY_PROVIDER_PACKAGE";
6733501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee    public static final String EXTRA_GATEWAY_URI = "com.android.phone.extra.GATEWAY_URI";
6833501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee
6933501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee    private final CallsManager mCallsManager;
700d3076c3dfa54f1102a843735ca774759e52d8b8Nancy Chen    private final Call mCall;
7133501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee    private final Intent mIntent;
7291d43cf9c985cc5a83795f256ef5c46ebb8fbdc1Tyler Gunn    private final Context mContext;
73220b419bb5d1b4d1df08415c8e52cb7f0decbf7cHall Liu    private final PhoneNumberUtilsAdapter mPhoneNumberUtilsAdapter;
74c9cf54407720d32fdb1b69e0254657a6061b9f56Hall Liu    private final TelecomSystem.SyncRoot mLock;
7591d43cf9c985cc5a83795f256ef5c46ebb8fbdc1Tyler Gunn
76cce5deb2bc998d6385152292153a9423f5206c28Yorke Lee    /*
77cce5deb2bc998d6385152292153a9423f5206c28Yorke Lee     * Whether or not the outgoing call intent originated from the default phone application. If
78cce5deb2bc998d6385152292153a9423f5206c28Yorke Lee     * so, it will be allowed to make emergency calls, even with the ACTION_CALL intent.
79cce5deb2bc998d6385152292153a9423f5206c28Yorke Lee     */
80cce5deb2bc998d6385152292153a9423f5206c28Yorke Lee    private final boolean mIsDefaultOrSystemPhoneApp;
8133501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee
82220b419bb5d1b4d1df08415c8e52cb7f0decbf7cHall Liu    @VisibleForTesting
83220b419bb5d1b4d1df08415c8e52cb7f0decbf7cHall Liu    public NewOutgoingCallIntentBroadcaster(Context context, CallsManager callsManager, Call call,
84220b419bb5d1b4d1df08415c8e52cb7f0decbf7cHall Liu            Intent intent, PhoneNumberUtilsAdapter phoneNumberUtilsAdapter,
85220b419bb5d1b4d1df08415c8e52cb7f0decbf7cHall Liu            boolean isDefaultPhoneApp) {
8691d43cf9c985cc5a83795f256ef5c46ebb8fbdc1Tyler Gunn        mContext = context;
8733501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee        mCallsManager = callsManager;
880d3076c3dfa54f1102a843735ca774759e52d8b8Nancy Chen        mCall = call;
8933501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee        mIntent = intent;
90220b419bb5d1b4d1df08415c8e52cb7f0decbf7cHall Liu        mPhoneNumberUtilsAdapter = phoneNumberUtilsAdapter;
91cce5deb2bc998d6385152292153a9423f5206c28Yorke Lee        mIsDefaultOrSystemPhoneApp = isDefaultPhoneApp;
92c9cf54407720d32fdb1b69e0254657a6061b9f56Hall Liu        mLock = mCallsManager.getLock();
9333501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee    }
9433501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee
9533501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee    /**
9633501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee     * Processes the result of the outgoing call broadcast intent, and performs callbacks to
9733501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee     * the OutgoingCallIntentBroadcasterListener as necessary.
9833501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee     */
99220b419bb5d1b4d1df08415c8e52cb7f0decbf7cHall Liu    public class NewOutgoingCallBroadcastIntentReceiver extends BroadcastReceiver {
10033501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee
10133501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee        @Override
10233501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee        public void onReceive(Context context, Intent intent) {
10311623a354be47205bf3bc686ed8fdfc278958983Brad Ebinger            try {
10411623a354be47205bf3bc686ed8fdfc278958983Brad Ebinger                Log.startSession("NOCBIR.oR");
10511623a354be47205bf3bc686ed8fdfc278958983Brad Ebinger                Trace.beginSection("onReceiveNewOutgoingCallBroadcast");
106c9cf54407720d32fdb1b69e0254657a6061b9f56Hall Liu                synchronized (mLock) {
107c9cf54407720d32fdb1b69e0254657a6061b9f56Hall Liu                    Log.v(this, "onReceive: %s", intent);
108c9cf54407720d32fdb1b69e0254657a6061b9f56Hall Liu
109c9cf54407720d32fdb1b69e0254657a6061b9f56Hall Liu                    // Once the NEW_OUTGOING_CALL broadcast is finished, the resultData is
110c9cf54407720d32fdb1b69e0254657a6061b9f56Hall Liu                    // used as the actual number to call. (If null, no call will be placed.)
111c9cf54407720d32fdb1b69e0254657a6061b9f56Hall Liu                    String resultNumber = getResultData();
112c9cf54407720d32fdb1b69e0254657a6061b9f56Hall Liu                    Log.i(this, "Received new-outgoing-call-broadcast for %s with data %s", mCall,
113c9cf54407720d32fdb1b69e0254657a6061b9f56Hall Liu                            Log.pii(resultNumber));
114c9cf54407720d32fdb1b69e0254657a6061b9f56Hall Liu
115c9cf54407720d32fdb1b69e0254657a6061b9f56Hall Liu                    boolean endEarly = false;
116ad18dc9e336c73922192e1693a836781cff6c929Hall Liu                    long disconnectTimeout =
117ad18dc9e336c73922192e1693a836781cff6c929Hall Liu                            Timeouts.getNewOutgoingCallCancelMillis(mContext.getContentResolver());
118c9cf54407720d32fdb1b69e0254657a6061b9f56Hall Liu                    if (resultNumber == null) {
119c9cf54407720d32fdb1b69e0254657a6061b9f56Hall Liu                        Log.v(this, "Call cancelled (null number), returning...");
120ad18dc9e336c73922192e1693a836781cff6c929Hall Liu                        disconnectTimeout = getDisconnectTimeoutFromApp(
121ad18dc9e336c73922192e1693a836781cff6c929Hall Liu                                getResultExtras(false), disconnectTimeout);
122c9cf54407720d32fdb1b69e0254657a6061b9f56Hall Liu                        endEarly = true;
123c9cf54407720d32fdb1b69e0254657a6061b9f56Hall Liu                    } else if (mPhoneNumberUtilsAdapter.isPotentialLocalEmergencyNumber(
124c9cf54407720d32fdb1b69e0254657a6061b9f56Hall Liu                            mContext, resultNumber)) {
125c9cf54407720d32fdb1b69e0254657a6061b9f56Hall Liu                        Log.w(this, "Cannot modify outgoing call to emergency number %s.",
126c9cf54407720d32fdb1b69e0254657a6061b9f56Hall Liu                                resultNumber);
127ad18dc9e336c73922192e1693a836781cff6c929Hall Liu                        disconnectTimeout = 0;
128c9cf54407720d32fdb1b69e0254657a6061b9f56Hall Liu                        endEarly = true;
12911623a354be47205bf3bc686ed8fdfc278958983Brad Ebinger                    }
13033501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee
131c9cf54407720d32fdb1b69e0254657a6061b9f56Hall Liu                    if (endEarly) {
132c9cf54407720d32fdb1b69e0254657a6061b9f56Hall Liu                        if (mCall != null) {
133ad18dc9e336c73922192e1693a836781cff6c929Hall Liu                            mCall.disconnect(disconnectTimeout);
134c9cf54407720d32fdb1b69e0254657a6061b9f56Hall Liu                        }
135c9cf54407720d32fdb1b69e0254657a6061b9f56Hall Liu                        return;
136c9cf54407720d32fdb1b69e0254657a6061b9f56Hall Liu                    }
137e6143f57856972ba21b5c6fb711c93ac2e477cc7Sanket Agarwal
138c9cf54407720d32fdb1b69e0254657a6061b9f56Hall Liu                    // If this call is already disconnected then we have nothing more to do.
139c9cf54407720d32fdb1b69e0254657a6061b9f56Hall Liu                    if (mCall.isDisconnected()) {
140c9cf54407720d32fdb1b69e0254657a6061b9f56Hall Liu                        Log.w(this, "Call has already been disconnected," +
141c9cf54407720d32fdb1b69e0254657a6061b9f56Hall Liu                                        " ignore the broadcast Call %s", mCall);
142c9cf54407720d32fdb1b69e0254657a6061b9f56Hall Liu                        return;
143c9cf54407720d32fdb1b69e0254657a6061b9f56Hall Liu                    }
14433501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee
145c9cf54407720d32fdb1b69e0254657a6061b9f56Hall Liu                    Uri resultHandleUri = Uri.fromParts(
146c9cf54407720d32fdb1b69e0254657a6061b9f56Hall Liu                            mPhoneNumberUtilsAdapter.isUriNumber(resultNumber) ?
147c9cf54407720d32fdb1b69e0254657a6061b9f56Hall Liu                                    PhoneAccount.SCHEME_SIP : PhoneAccount.SCHEME_TEL,
148c9cf54407720d32fdb1b69e0254657a6061b9f56Hall Liu                            resultNumber, null);
149c9cf54407720d32fdb1b69e0254657a6061b9f56Hall Liu
150c9cf54407720d32fdb1b69e0254657a6061b9f56Hall Liu                    Uri originalUri = mIntent.getData();
151c9cf54407720d32fdb1b69e0254657a6061b9f56Hall Liu
152c9cf54407720d32fdb1b69e0254657a6061b9f56Hall Liu                    if (originalUri.getSchemeSpecificPart().equals(resultNumber)) {
153c9cf54407720d32fdb1b69e0254657a6061b9f56Hall Liu                        Log.v(this, "Call number unmodified after" +
154c9cf54407720d32fdb1b69e0254657a6061b9f56Hall Liu                                " new outgoing call intent broadcast.");
155c9cf54407720d32fdb1b69e0254657a6061b9f56Hall Liu                    } else {
156c9cf54407720d32fdb1b69e0254657a6061b9f56Hall Liu                        Log.v(this, "Retrieved modified handle after outgoing call intent" +
157c9cf54407720d32fdb1b69e0254657a6061b9f56Hall Liu                                " broadcast: Original: %s, Modified: %s",
158c9cf54407720d32fdb1b69e0254657a6061b9f56Hall Liu                                Log.pii(originalUri),
159c9cf54407720d32fdb1b69e0254657a6061b9f56Hall Liu                                Log.pii(resultHandleUri));
160c9cf54407720d32fdb1b69e0254657a6061b9f56Hall Liu                    }
16133501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee
162c9cf54407720d32fdb1b69e0254657a6061b9f56Hall Liu                    GatewayInfo gatewayInfo = getGateWayInfoFromIntent(intent, resultHandleUri);
163c9cf54407720d32fdb1b69e0254657a6061b9f56Hall Liu                    mCall.setNewOutgoingCallIntentBroadcastIsDone();
164c9cf54407720d32fdb1b69e0254657a6061b9f56Hall Liu                    mCallsManager.placeOutgoingCall(mCall, resultHandleUri, gatewayInfo,
165c9cf54407720d32fdb1b69e0254657a6061b9f56Hall Liu                            mIntent.getBooleanExtra(
166c9cf54407720d32fdb1b69e0254657a6061b9f56Hall Liu                                    TelecomManager.EXTRA_START_CALL_WITH_SPEAKERPHONE, false),
167c9cf54407720d32fdb1b69e0254657a6061b9f56Hall Liu                            mIntent.getIntExtra(TelecomManager.EXTRA_START_CALL_WITH_VIDEO_STATE,
168c9cf54407720d32fdb1b69e0254657a6061b9f56Hall Liu                                    VideoProfile.STATE_AUDIO_ONLY));
16911623a354be47205bf3bc686ed8fdfc278958983Brad Ebinger                }
17011623a354be47205bf3bc686ed8fdfc278958983Brad Ebinger            } finally {
17111623a354be47205bf3bc686ed8fdfc278958983Brad Ebinger                Trace.endSection();
17211623a354be47205bf3bc686ed8fdfc278958983Brad Ebinger                Log.endSession();
17311623a354be47205bf3bc686ed8fdfc278958983Brad Ebinger            }
17433501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee        }
17533501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee    }
17633501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee
17733501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee    /**
17833501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee     * Processes the supplied intent and starts the outgoing call broadcast process relevant to the
17933501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee     * intent.
18033501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee     *
18133501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee     * This method will handle three kinds of actions:
18233501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee     *
18333501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee     * - CALL (intent launched by all third party dialers)
18433501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee     * - CALL_PRIVILEGED (intent launched by system apps e.g. system Dialer, voice Dialer)
18533501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee     * - CALL_EMERGENCY (intent launched by lock screen emergency dialer)
186cce5deb2bc998d6385152292153a9423f5206c28Yorke Lee     *
1878d5d9ddc66b55b6906364ab3c0e244dab4d58f13Ihab Awad     * @return {@link DisconnectCause#NOT_DISCONNECTED} if the call succeeded, and an appropriate
1888d5d9ddc66b55b6906364ab3c0e244dab4d58f13Ihab Awad     *         {@link DisconnectCause} if the call did not, describing why it failed.
18933501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee     */
190220b419bb5d1b4d1df08415c8e52cb7f0decbf7cHall Liu    @VisibleForTesting
191220b419bb5d1b4d1df08415c8e52cb7f0decbf7cHall Liu    public int processIntent() {
19233501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee        Log.v(this, "Processing call intent in OutgoingCallIntentBroadcaster.");
19333501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee
19433501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee        Intent intent = mIntent;
195308ab8bd691f4a1bc4c7c094bcffc84f5fe345aaNancy Chen        String action = intent.getAction();
196308ab8bd691f4a1bc4c7c094bcffc84f5fe345aaNancy Chen        final Uri handle = intent.getData();
197308ab8bd691f4a1bc4c7c094bcffc84f5fe345aaNancy Chen
198308ab8bd691f4a1bc4c7c094bcffc84f5fe345aaNancy Chen        if (handle == null) {
199308ab8bd691f4a1bc4c7c094bcffc84f5fe345aaNancy Chen            Log.w(this, "Empty handle obtained from the call intent.");
200308ab8bd691f4a1bc4c7c094bcffc84f5fe345aaNancy Chen            return DisconnectCause.INVALID_NUMBER;
201308ab8bd691f4a1bc4c7c094bcffc84f5fe345aaNancy Chen        }
20233501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee
20356a76b79e2a03eb0fe070d3ef17e474ae27bb6f3Jay Shrauner        boolean isVoicemailNumber = PhoneAccount.SCHEME_VOICEMAIL.equals(handle.getScheme());
204308ab8bd691f4a1bc4c7c094bcffc84f5fe345aaNancy Chen        if (isVoicemailNumber) {
205fe1ce0ac4f1496dc8f77a4c738fb47a8ef8676aeYorke Lee            if (Intent.ACTION_CALL.equals(action)
206fe1ce0ac4f1496dc8f77a4c738fb47a8ef8676aeYorke Lee                    || Intent.ACTION_CALL_PRIVILEGED.equals(action)) {
207308ab8bd691f4a1bc4c7c094bcffc84f5fe345aaNancy Chen                // Voicemail calls will be handled directly by the telephony connection manager
208308ab8bd691f4a1bc4c7c094bcffc84f5fe345aaNancy Chen                Log.i(this, "Placing call immediately instead of waiting for "
209308ab8bd691f4a1bc4c7c094bcffc84f5fe345aaNancy Chen                        + " OutgoingCallBroadcastReceiver: %s", intent);
21033501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee
2115f048fe6746f261498803629a21682999913532fSantos Cordon                // Since we are not going to go through "Outgoing call broadcast", make sure
2125f048fe6746f261498803629a21682999913532fSantos Cordon                // we mark it as ready.
2135f048fe6746f261498803629a21682999913532fSantos Cordon                mCall.setNewOutgoingCallIntentBroadcastIsDone();
2145f048fe6746f261498803629a21682999913532fSantos Cordon
215308ab8bd691f4a1bc4c7c094bcffc84f5fe345aaNancy Chen                boolean speakerphoneOn = mIntent.getBooleanExtra(
2167cc70b4f0ad1064a4a0dce6056ad82b205887160Tyler Gunn                        TelecomManager.EXTRA_START_CALL_WITH_SPEAKERPHONE, false);
217308ab8bd691f4a1bc4c7c094bcffc84f5fe345aaNancy Chen                mCallsManager.placeOutgoingCall(mCall, handle, null, speakerphoneOn,
2185b8824979e929250a46791c785b8459512236585Tyler Gunn                        VideoProfile.STATE_AUDIO_ONLY);
219308ab8bd691f4a1bc4c7c094bcffc84f5fe345aaNancy Chen
220308ab8bd691f4a1bc4c7c094bcffc84f5fe345aaNancy Chen                return DisconnectCause.NOT_DISCONNECTED;
221d7255872e2204a074bc5dfb5dceb4da13574864cYorke Lee            } else {
222308ab8bd691f4a1bc4c7c094bcffc84f5fe345aaNancy Chen                Log.i(this, "Unhandled intent %s. Ignoring and not placing call.", intent);
223308ab8bd691f4a1bc4c7c094bcffc84f5fe345aaNancy Chen                return DisconnectCause.OUTGOING_CANCELED;
224d7255872e2204a074bc5dfb5dceb4da13574864cYorke Lee            }
22533501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee        }
22633501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee
227220b419bb5d1b4d1df08415c8e52cb7f0decbf7cHall Liu        String number = mPhoneNumberUtilsAdapter.getNumberFromIntent(intent, mContext);
228308ab8bd691f4a1bc4c7c094bcffc84f5fe345aaNancy Chen        if (TextUtils.isEmpty(number)) {
229308ab8bd691f4a1bc4c7c094bcffc84f5fe345aaNancy Chen            Log.w(this, "Empty number obtained from the call intent.");
230308ab8bd691f4a1bc4c7c094bcffc84f5fe345aaNancy Chen            return DisconnectCause.NO_PHONE_NUMBER_SUPPLIED;
231308ab8bd691f4a1bc4c7c094bcffc84f5fe345aaNancy Chen        }
232b58f453c46a695347a69f8c973a77796e5c1fdf7Santos Cordon
233220b419bb5d1b4d1df08415c8e52cb7f0decbf7cHall Liu        boolean isUriNumber = mPhoneNumberUtilsAdapter.isUriNumber(number);
234b58f453c46a695347a69f8c973a77796e5c1fdf7Santos Cordon        if (!isUriNumber) {
235220b419bb5d1b4d1df08415c8e52cb7f0decbf7cHall Liu            number = mPhoneNumberUtilsAdapter.convertKeypadLettersToDigits(number);
236220b419bb5d1b4d1df08415c8e52cb7f0decbf7cHall Liu            number = mPhoneNumberUtilsAdapter.stripSeparators(number);
23733501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee        }
23833501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee
23991d43cf9c985cc5a83795f256ef5c46ebb8fbdc1Tyler Gunn        final boolean isPotentialEmergencyNumber = isPotentialEmergencyNumber(number);
24033501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee        Log.v(this, "isPotentialEmergencyNumber = %s", isPotentialEmergencyNumber);
24133501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee
24233501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee        rewriteCallIntentAction(intent, isPotentialEmergencyNumber);
243e05257c9cd0b86f0c161bc12fc913c76e7b8ae6cYorke Lee        action = intent.getAction();
24433501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee        // True for certain types of numbers that are not intended to be intercepted or modified
24533501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee        // by third parties (e.g. emergency numbers).
24633501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee        boolean callImmediately = false;
24733501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee
24833501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee        if (Intent.ACTION_CALL.equals(action)) {
24933501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee            if (isPotentialEmergencyNumber) {
250cce5deb2bc998d6385152292153a9423f5206c28Yorke Lee                if (!mIsDefaultOrSystemPhoneApp) {
251cce5deb2bc998d6385152292153a9423f5206c28Yorke Lee                    Log.w(this, "Cannot call potential emergency number %s with CALL Intent %s "
252308ab8bd691f4a1bc4c7c094bcffc84f5fe345aaNancy Chen                            + "unless caller is system or default dialer.", number, intent);
25391d43cf9c985cc5a83795f256ef5c46ebb8fbdc1Tyler Gunn                    launchSystemDialer(intent.getData());
254d7255872e2204a074bc5dfb5dceb4da13574864cYorke Lee                    return DisconnectCause.OUTGOING_CANCELED;
255cce5deb2bc998d6385152292153a9423f5206c28Yorke Lee                } else {
256cce5deb2bc998d6385152292153a9423f5206c28Yorke Lee                    callImmediately = true;
257cce5deb2bc998d6385152292153a9423f5206c28Yorke Lee                }
25833501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee            }
25933501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee        } else if (Intent.ACTION_CALL_EMERGENCY.equals(action)) {
26033501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee            if (!isPotentialEmergencyNumber) {
26133501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee                Log.w(this, "Cannot call non-potential-emergency number %s with EMERGENCY_CALL "
262308ab8bd691f4a1bc4c7c094bcffc84f5fe345aaNancy Chen                        + "Intent %s.", number, intent);
263d7255872e2204a074bc5dfb5dceb4da13574864cYorke Lee                return DisconnectCause.OUTGOING_CANCELED;
26433501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee            }
26533501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee            callImmediately = true;
26633501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee        } else {
26733501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee            Log.w(this, "Unhandled Intent %s. Ignoring and not placing call.", intent);
268d7255872e2204a074bc5dfb5dceb4da13574864cYorke Lee            return DisconnectCause.INVALID_NUMBER;
26933501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee        }
27033501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee
27133501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee        if (callImmediately) {
27233501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee            Log.i(this, "Placing call immediately instead of waiting for "
27333501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee                    + " OutgoingCallBroadcastReceiver: %s", intent);
27456a76b79e2a03eb0fe070d3ef17e474ae27bb6f3Jay Shrauner            String scheme = isUriNumber ? PhoneAccount.SCHEME_SIP : PhoneAccount.SCHEME_TEL;
275905dfba7883666f45a0c6958d8bc6c19d68972d9Sailesh Nepal            boolean speakerphoneOn = mIntent.getBooleanExtra(
2767cc70b4f0ad1064a4a0dce6056ad82b205887160Tyler Gunn                    TelecomManager.EXTRA_START_CALL_WITH_SPEAKERPHONE, false);
277905dfba7883666f45a0c6958d8bc6c19d68972d9Sailesh Nepal            int videoState = mIntent.getIntExtra(
2787cc70b4f0ad1064a4a0dce6056ad82b205887160Tyler Gunn                    TelecomManager.EXTRA_START_CALL_WITH_VIDEO_STATE,
2795b8824979e929250a46791c785b8459512236585Tyler Gunn                    VideoProfile.STATE_AUDIO_ONLY);
280308ab8bd691f4a1bc4c7c094bcffc84f5fe345aaNancy Chen            mCallsManager.placeOutgoingCall(mCall, Uri.fromParts(scheme, number, null), null,
2810d3076c3dfa54f1102a843735ca774759e52d8b8Nancy Chen                    speakerphoneOn, videoState);
28233501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee
28333501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee            // Don't return but instead continue and send the ACTION_NEW_OUTGOING_CALL broadcast
28433501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee            // so that third parties can still inspect (but not intercept) the outgoing call. When
28533501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee            // the broadcast finally reaches the OutgoingCallBroadcastReceiver, we'll know not to
28633501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee            // initiate the call again because of the presence of the EXTRA_ALREADY_CALLED extra.
28733501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee        }
28833501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee
289d3bacb784f59ef75f4b17fe218d08eb28ad338fdTony Mak        UserHandle targetUser = mCall.getInitiatingUser();
290d3bacb784f59ef75f4b17fe218d08eb28ad338fdTony Mak        Log.i(this, "Sending NewOutgoingCallBroadcast for %s to %s", mCall, targetUser);
291d3bacb784f59ef75f4b17fe218d08eb28ad338fdTony Mak        broadcastIntent(intent, number, !callImmediately, targetUser);
292d7255872e2204a074bc5dfb5dceb4da13574864cYorke Lee        return DisconnectCause.NOT_DISCONNECTED;
29333501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee    }
29433501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee
29533501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee    /**
29633501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee     * Sends a new outgoing call ordered broadcast so that third party apps can cancel the
29733501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee     * placement of the call or redirect it to a different number.
29833501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee     *
29933501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee     * @param originalCallIntent The original call intent.
300308ab8bd691f4a1bc4c7c094bcffc84f5fe345aaNancy Chen     * @param number Call number that was stored in the original call intent.
30133501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee     * @param receiverRequired Whether or not the result from the ordered broadcast should be
302d3bacb784f59ef75f4b17fe218d08eb28ad338fdTony Mak     *                         processed using a {@link NewOutgoingCallIntentBroadcaster}.
303d3bacb784f59ef75f4b17fe218d08eb28ad338fdTony Mak     * @param targetUser User that the broadcast sent to.
30433501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee     */
30533501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee    private void broadcastIntent(
30633501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee            Intent originalCallIntent,
307308ab8bd691f4a1bc4c7c094bcffc84f5fe345aaNancy Chen            String number,
308d3bacb784f59ef75f4b17fe218d08eb28ad338fdTony Mak            boolean receiverRequired,
309d3bacb784f59ef75f4b17fe218d08eb28ad338fdTony Mak            UserHandle targetUser) {
31033501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee        Intent broadcastIntent = new Intent(Intent.ACTION_NEW_OUTGOING_CALL);
311308ab8bd691f4a1bc4c7c094bcffc84f5fe345aaNancy Chen        if (number != null) {
312308ab8bd691f4a1bc4c7c094bcffc84f5fe345aaNancy Chen            broadcastIntent.putExtra(Intent.EXTRA_PHONE_NUMBER, number);
31333501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee        }
31433501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee
31533501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee        // Force receivers of this broadcast intent to run at foreground priority because we
31633501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee        // want to finish processing the broadcast intent as soon as possible.
31733501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee        broadcastIntent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
31833501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee        Log.v(this, "Broadcasting intent: %s.", broadcastIntent);
31933501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee
32077d2d0e90bc2a67d526743b8e32f920a0d755fbaNancy Chen        checkAndCopyProviderExtras(originalCallIntent, broadcastIntent);
32133501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee
32291d43cf9c985cc5a83795f256ef5c46ebb8fbdc1Tyler Gunn        mContext.sendOrderedBroadcastAsUser(
32333501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee                broadcastIntent,
324d3bacb784f59ef75f4b17fe218d08eb28ad338fdTony Mak                targetUser,
325973c4e1545581f7cb8a1bf8a7b3cdf5a8f38d01aSvetoslav                android.Manifest.permission.PROCESS_OUTGOING_CALLS,
326973c4e1545581f7cb8a1bf8a7b3cdf5a8f38d01aSvetoslav                AppOpsManager.OP_PROCESS_OUTGOING_CALLS,
32733501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee                receiverRequired ? new NewOutgoingCallBroadcastIntentReceiver() : null,
32833501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee                null,  // scheduler
32933501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee                Activity.RESULT_OK,  // initialCode
330308ab8bd691f4a1bc4c7c094bcffc84f5fe345aaNancy Chen                number,  // initialData: initial value for the result data (number to be modified)
33133501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee                null);  // initialExtras
33233501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee    }
33333501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee
33433501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee    /**
33533501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee     * Copy all the expected extras set when a 3rd party gateway provider is to be used, from the
33633501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee     * source intent to the destination one.
33733501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee     *
33833501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee     * @param src Intent which may contain the provider's extras.
33933501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee     * @param dst Intent where a copy of the extras will be added if applicable.
34033501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee     */
34177d2d0e90bc2a67d526743b8e32f920a0d755fbaNancy Chen    public void checkAndCopyProviderExtras(Intent src, Intent dst) {
34277d2d0e90bc2a67d526743b8e32f920a0d755fbaNancy Chen        if (src == null) {
34377d2d0e90bc2a67d526743b8e32f920a0d755fbaNancy Chen            return;
34477d2d0e90bc2a67d526743b8e32f920a0d755fbaNancy Chen        }
34533501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee        if (hasGatewayProviderExtras(src)) {
34633501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee            dst.putExtra(EXTRA_GATEWAY_PROVIDER_PACKAGE,
34733501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee                    src.getStringExtra(EXTRA_GATEWAY_PROVIDER_PACKAGE));
34833501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee            dst.putExtra(EXTRA_GATEWAY_URI,
34933501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee                    src.getStringExtra(EXTRA_GATEWAY_URI));
35033501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee            Log.d(this, "Found and copied gateway provider extras to broadcast intent.");
35133501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee            return;
35233501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee        }
35333501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee
35477d2d0e90bc2a67d526743b8e32f920a0d755fbaNancy Chen        Log.d(this, "No provider extras found in call intent.");
35533501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee    }
35633501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee
35733501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee    /**
35833501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee     * Check if valid gateway provider information is stored as extras in the intent
35933501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee     *
36033501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee     * @param intent to check for
36133501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee     * @return true if the intent has all the gateway information extras needed.
36233501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee     */
36333501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee    private boolean hasGatewayProviderExtras(Intent intent) {
36433501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee        final String name = intent.getStringExtra(EXTRA_GATEWAY_PROVIDER_PACKAGE);
36533501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee        final String uriString = intent.getStringExtra(EXTRA_GATEWAY_URI);
36633501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee
36733501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee        return !TextUtils.isEmpty(name) && !TextUtils.isEmpty(uriString);
36833501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee    }
36933501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee
37033501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee    private static Uri getGatewayUriFromString(String gatewayUriString) {
37133501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee        return TextUtils.isEmpty(gatewayUriString) ? null : Uri.parse(gatewayUriString);
37233501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee    }
37333501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee
37433501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee    /**
37533501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee     * Extracts gateway provider information from a provided intent..
37633501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee     *
37733501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee     * @param intent to extract gateway provider information from.
37833501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee     * @param trueHandle The actual call handle that the user is trying to dial
37933501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee     * @return GatewayInfo object containing extracted gateway provider information as well as
38033501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee     *     the actual handle the user is trying to dial.
38133501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee     */
38233501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee    public static GatewayInfo getGateWayInfoFromIntent(Intent intent, Uri trueHandle) {
38333501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee        if (intent == null) {
38433501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee            return null;
38533501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee        }
38633501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee
38733501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee        // Check if gateway extras are present.
38833501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee        String gatewayPackageName = intent.getStringExtra(EXTRA_GATEWAY_PROVIDER_PACKAGE);
38933501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee        Uri gatewayUri = getGatewayUriFromString(intent.getStringExtra(EXTRA_GATEWAY_URI));
39033501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee        if (!TextUtils.isEmpty(gatewayPackageName) && gatewayUri != null) {
39133501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee            return new GatewayInfo(gatewayPackageName, gatewayUri, trueHandle);
39233501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee        }
39333501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee
39433501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee        return null;
39533501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee    }
39633501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee
39791d43cf9c985cc5a83795f256ef5c46ebb8fbdc1Tyler Gunn    private void launchSystemDialer(Uri handle) {
39833501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee        Intent systemDialerIntent = new Intent();
39991d43cf9c985cc5a83795f256ef5c46ebb8fbdc1Tyler Gunn        final Resources resources = mContext.getResources();
40033501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee        systemDialerIntent.setClassName(
40133501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee                resources.getString(R.string.ui_default_package),
40233501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee                resources.getString(R.string.dialer_default_class));
40333501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee        systemDialerIntent.setAction(Intent.ACTION_DIAL);
40433501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee        systemDialerIntent.setData(handle);
40533501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee        systemDialerIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
40633501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee        Log.v(this, "calling startActivity for default dialer: %s", systemDialerIntent);
40739d94c1b821e7da3a551f487b052ee347e3377a7Yorke Lee        mContext.startActivityAsUser(systemDialerIntent, UserHandle.CURRENT);
40833501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee    }
40933501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee
41033501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee    /**
41133501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee     * Check whether or not this is an emergency number, in order to enforce the restriction
41233501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee     * that only the CALL_PRIVILEGED and CALL_EMERGENCY intents are allowed to make emergency
41333501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee     * calls.
41433501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee     *
41533501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee     * To prevent malicious 3rd party apps from making emergency calls by passing in an
41633501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee     * "invalid" number like "9111234" (that isn't technically an emergency number but might
41733501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee     * still result in an emergency call with some networks), we use
41833501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee     * isPotentialLocalEmergencyNumber instead of isLocalEmergencyNumber.
41933501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee     *
420308ab8bd691f4a1bc4c7c094bcffc84f5fe345aaNancy Chen     * @param number number to inspect in order to determine whether or not an emergency number
42133501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee     * is potentially being dialed
42233501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee     * @return True if the handle is potentially an emergency number.
42333501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee     */
42491d43cf9c985cc5a83795f256ef5c46ebb8fbdc1Tyler Gunn    private boolean isPotentialEmergencyNumber(String number) {
425308ab8bd691f4a1bc4c7c094bcffc84f5fe345aaNancy Chen        Log.v(this, "Checking restrictions for number : %s", Log.pii(number));
426220b419bb5d1b4d1df08415c8e52cb7f0decbf7cHall Liu        return (number != null)
427220b419bb5d1b4d1df08415c8e52cb7f0decbf7cHall Liu                && mPhoneNumberUtilsAdapter.isPotentialLocalEmergencyNumber(mContext, number);
42833501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee    }
42933501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee
43033501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee    /**
43133501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee     * Given a call intent and whether or not the number to dial is an emergency number, rewrite
43233501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee     * the call intent action to an appropriate one.
43333501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee     *
43433501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee     * @param intent Intent to rewrite the action for
435308ab8bd691f4a1bc4c7c094bcffc84f5fe345aaNancy Chen     * @param isPotentialEmergencyNumber Whether or not the number is potentially an emergency
43633501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee     * number.
43733501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee     */
43833501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee    private void rewriteCallIntentAction(Intent intent, boolean isPotentialEmergencyNumber) {
43933501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee        String action = intent.getAction();
44033501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee
44133501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee        /* Change CALL_PRIVILEGED into CALL or CALL_EMERGENCY as needed. */
44233501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee        if (Intent.ACTION_CALL_PRIVILEGED.equals(action)) {
44333501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee            if (isPotentialEmergencyNumber) {
44433501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee                Log.i(this, "ACTION_CALL_PRIVILEGED is used while the number is a potential"
44533501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee                        + " emergency number. Using ACTION_CALL_EMERGENCY as an action instead.");
44633501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee                action = Intent.ACTION_CALL_EMERGENCY;
44733501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee            } else {
44833501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee                action = Intent.ACTION_CALL;
44933501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee            }
45033501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee            Log.v(this, " - updating action from CALL_PRIVILEGED to %s", action);
45133501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee            intent.setAction(action);
45233501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee        }
45333501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee    }
454ad18dc9e336c73922192e1693a836781cff6c929Hall Liu
455ad18dc9e336c73922192e1693a836781cff6c929Hall Liu    private long getDisconnectTimeoutFromApp(Bundle resultExtras, long defaultTimeout) {
456ad18dc9e336c73922192e1693a836781cff6c929Hall Liu        if (resultExtras != null) {
457ad18dc9e336c73922192e1693a836781cff6c929Hall Liu            long disconnectTimeout = resultExtras.getLong(
458ad18dc9e336c73922192e1693a836781cff6c929Hall Liu                    TelecomManager.EXTRA_NEW_OUTGOING_CALL_CANCEL_TIMEOUT, defaultTimeout);
459ad18dc9e336c73922192e1693a836781cff6c929Hall Liu            if (disconnectTimeout < 0) {
460ad18dc9e336c73922192e1693a836781cff6c929Hall Liu                disconnectTimeout = 0;
461ad18dc9e336c73922192e1693a836781cff6c929Hall Liu            }
462ad18dc9e336c73922192e1693a836781cff6c929Hall Liu            return Math.min(disconnectTimeout,
463ad18dc9e336c73922192e1693a836781cff6c929Hall Liu                    Timeouts.getMaxNewOutgoingCallCancelMillis(mContext.getContentResolver()));
464ad18dc9e336c73922192e1693a836781cff6c929Hall Liu        } else {
465ad18dc9e336c73922192e1693a836781cff6c929Hall Liu            return defaultTimeout;
466ad18dc9e336c73922192e1693a836781cff6c929Hall Liu        }
467ad18dc9e336c73922192e1693a836781cff6c929Hall Liu    }
46833501635c2cd21f40793041eff3b8ce3a5710d49Yorke Lee}
469