blob: 0846c5962525cb8abd598d70a82cded0fb751ec4 [file] [log] [blame]
/*
* Copyright (C) 2014 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.services.telephony;
import android.content.ActivityNotFoundException;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.telecom.Conference;
import android.telecom.Connection;
import android.telecom.ConnectionRequest;
import android.telecom.ConnectionService;
import android.telecom.DisconnectCause;
import android.telecom.PhoneAccount;
import android.telecom.PhoneAccountHandle;
import android.telecom.TelecomManager;
import android.telecom.VideoProfile;
import android.telephony.CarrierConfigManager;
import android.telephony.PhoneNumberUtils;
import android.telephony.ServiceState;
import android.telephony.SubscriptionManager;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import com.android.internal.telephony.Call;
import com.android.internal.telephony.CallStateException;
import com.android.internal.telephony.IccCard;
import com.android.internal.telephony.IccCardConstants;
import com.android.internal.telephony.Phone;
import com.android.internal.telephony.PhoneConstants;
import com.android.internal.telephony.PhoneFactory;
import com.android.internal.telephony.SubscriptionController;
import com.android.internal.telephony.imsphone.ImsExternalCallTracker;
import com.android.internal.telephony.imsphone.ImsPhone;
import com.android.phone.MMIDialogActivity;
import com.android.phone.PhoneUtils;
import com.android.phone.R;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
/**
* Service for making GSM and CDMA connections.
*/
public class TelephonyConnectionService extends ConnectionService {
// If configured, reject attempts to dial numbers matching this pattern.
private static final Pattern CDMA_ACTIVATION_CODE_REGEX_PATTERN =
Pattern.compile("\\*228[0-9]{0,2}");
private final TelephonyConferenceController mTelephonyConferenceController =
new TelephonyConferenceController(this);
private final CdmaConferenceController mCdmaConferenceController =
new CdmaConferenceController(this);
private final ImsConferenceController mImsConferenceController =
new ImsConferenceController(this);
private ComponentName mExpectedComponentName = null;
private EmergencyCallHelper mEmergencyCallHelper;
private EmergencyTonePlayer mEmergencyTonePlayer;
/**
* A listener to actionable events specific to the TelephonyConnection.
*/
private final TelephonyConnection.TelephonyConnectionListener mTelephonyConnectionListener =
new TelephonyConnection.TelephonyConnectionListener() {
@Override
public void onOriginalConnectionConfigured(TelephonyConnection c) {
addConnectionToConferenceController(c);
}
};
@Override
public void onCreate() {
super.onCreate();
mExpectedComponentName = new ComponentName(this, this.getClass());
mEmergencyTonePlayer = new EmergencyTonePlayer(this);
TelecomAccountRegistry.getInstance(this).setTelephonyConnectionService(this);
}
@Override
public Connection onCreateOutgoingConnection(
PhoneAccountHandle connectionManagerPhoneAccount,
final ConnectionRequest request) {
Log.i(this, "onCreateOutgoingConnection, request: " + request);
Uri handle = request.getAddress();
if (handle == null) {
Log.d(this, "onCreateOutgoingConnection, handle is null");
return Connection.createFailedConnection(
DisconnectCauseUtil.toTelecomDisconnectCause(
android.telephony.DisconnectCause.NO_PHONE_NUMBER_SUPPLIED,
"No phone number supplied"));
}
String scheme = handle.getScheme();
final String number;
if (PhoneAccount.SCHEME_VOICEMAIL.equals(scheme)) {
// TODO: We don't check for SecurityException here (requires
// CALL_PRIVILEGED permission).
final Phone phone = getPhoneForAccount(request.getAccountHandle(), false);
if (phone == null) {
Log.d(this, "onCreateOutgoingConnection, phone is null");
return Connection.createFailedConnection(
DisconnectCauseUtil.toTelecomDisconnectCause(
android.telephony.DisconnectCause.OUT_OF_SERVICE,
"Phone is null"));
}
number = phone.getVoiceMailNumber();
if (TextUtils.isEmpty(number)) {
Log.d(this, "onCreateOutgoingConnection, no voicemail number set.");
return Connection.createFailedConnection(
DisconnectCauseUtil.toTelecomDisconnectCause(
android.telephony.DisconnectCause.VOICEMAIL_NUMBER_MISSING,
"Voicemail scheme provided but no voicemail number set."));
}
// Convert voicemail: to tel:
handle = Uri.fromParts(PhoneAccount.SCHEME_TEL, number, null);
} else {
if (!PhoneAccount.SCHEME_TEL.equals(scheme)) {
Log.d(this, "onCreateOutgoingConnection, Handle %s is not type tel", scheme);
return Connection.createFailedConnection(
DisconnectCauseUtil.toTelecomDisconnectCause(
android.telephony.DisconnectCause.INVALID_NUMBER,
"Handle scheme is not type tel"));
}
number = handle.getSchemeSpecificPart();
if (TextUtils.isEmpty(number)) {
Log.d(this, "onCreateOutgoingConnection, unable to parse number");
return Connection.createFailedConnection(
DisconnectCauseUtil.toTelecomDisconnectCause(
android.telephony.DisconnectCause.INVALID_NUMBER,
"Unable to parse number"));
}
final Phone phone = getPhoneForAccount(request.getAccountHandle(), false);
if (phone != null && CDMA_ACTIVATION_CODE_REGEX_PATTERN.matcher(number).matches()) {
// Obtain the configuration for the outgoing phone's SIM. If the outgoing number
// matches the *228 regex pattern, fail the call. This number is used for OTASP, and
// when dialed could lock LTE SIMs to 3G if not prohibited..
boolean disableActivation = false;
CarrierConfigManager cfgManager = (CarrierConfigManager)
phone.getContext().getSystemService(Context.CARRIER_CONFIG_SERVICE);
if (cfgManager != null) {
disableActivation = cfgManager.getConfigForSubId(phone.getSubId())
.getBoolean(CarrierConfigManager.KEY_DISABLE_CDMA_ACTIVATION_CODE_BOOL);
}
if (disableActivation) {
return Connection.createFailedConnection(
DisconnectCauseUtil.toTelecomDisconnectCause(
android.telephony.DisconnectCause
.CDMA_ALREADY_ACTIVATED,
"Tried to dial *228"));
}
}
}
boolean isEmergencyNumber = PhoneNumberUtils.isLocalEmergencyNumber(this, number);
// Get the right phone object from the account data passed in.
final Phone phone = getPhoneForAccount(request.getAccountHandle(), isEmergencyNumber);
if (phone == null) {
final Context context = getApplicationContext();
if (context.getResources().getBoolean(R.bool.config_checkSimStateBeforeOutgoingCall)) {
// Check SIM card state before the outgoing call.
// Start the SIM unlock activity if PIN_REQUIRED.
final Phone defaultPhone = PhoneFactory.getDefaultPhone();
final IccCard icc = defaultPhone.getIccCard();
IccCardConstants.State simState = IccCardConstants.State.UNKNOWN;
if (icc != null) {
simState = icc.getState();
}
if (simState == IccCardConstants.State.PIN_REQUIRED) {
final String simUnlockUiPackage = context.getResources().getString(
R.string.config_simUnlockUiPackage);
final String simUnlockUiClass = context.getResources().getString(
R.string.config_simUnlockUiClass);
if (simUnlockUiPackage != null && simUnlockUiClass != null) {
Intent simUnlockIntent = new Intent().setComponent(new ComponentName(
simUnlockUiPackage, simUnlockUiClass));
simUnlockIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
try {
context.startActivity(simUnlockIntent);
} catch (ActivityNotFoundException exception) {
Log.e(this, exception, "Unable to find SIM unlock UI activity.");
}
}
return Connection.createFailedConnection(
DisconnectCauseUtil.toTelecomDisconnectCause(
android.telephony.DisconnectCause.OUT_OF_SERVICE,
"SIM_STATE_PIN_REQUIRED"));
}
}
Log.d(this, "onCreateOutgoingConnection, phone is null");
return Connection.createFailedConnection(
DisconnectCauseUtil.toTelecomDisconnectCause(
android.telephony.DisconnectCause.OUT_OF_SERVICE, "Phone is null"));
}
// Check both voice & data RAT to enable normal CS call,
// when voice RAT is OOS but Data RAT is present.
int state = phone.getServiceState().getState();
if (state == ServiceState.STATE_OUT_OF_SERVICE) {
if (phone.getServiceState().getDataNetworkType() == TelephonyManager.NETWORK_TYPE_LTE) {
state = phone.getServiceState().getDataRegState();
}
}
boolean useEmergencyCallHelper = false;
// If we're dialing a non-emergency number and the phone is in ECM mode, reject the call if
// carrier configuration specifies that we cannot make non-emergency calls in ECM mode.
if (!isEmergencyNumber && phone.isInEcm()) {
boolean allowNonEmergencyCalls = true;
CarrierConfigManager cfgManager = (CarrierConfigManager)
phone.getContext().getSystemService(Context.CARRIER_CONFIG_SERVICE);
if (cfgManager != null) {
allowNonEmergencyCalls = cfgManager.getConfigForSubId(phone.getSubId())
.getBoolean(CarrierConfigManager.KEY_ALLOW_NON_EMERGENCY_CALLS_IN_ECM_BOOL);
}
if (!allowNonEmergencyCalls) {
return Connection.createFailedConnection(
DisconnectCauseUtil.toTelecomDisconnectCause(
android.telephony.DisconnectCause.CDMA_NOT_EMERGENCY,
"Cannot make non-emergency call in ECM mode."
));
}
}
if (isEmergencyNumber) {
if (!phone.isRadioOn()) {
useEmergencyCallHelper = true;
}
} else {
switch (state) {
case ServiceState.STATE_IN_SERVICE:
case ServiceState.STATE_EMERGENCY_ONLY:
break;
case ServiceState.STATE_OUT_OF_SERVICE:
if (phone.isUtEnabled() && number.endsWith("#")) {
Log.d(this, "onCreateOutgoingConnection dial for UT");
break;
} else {
return Connection.createFailedConnection(
DisconnectCauseUtil.toTelecomDisconnectCause(
android.telephony.DisconnectCause.OUT_OF_SERVICE,
"ServiceState.STATE_OUT_OF_SERVICE"));
}
case ServiceState.STATE_POWER_OFF:
return Connection.createFailedConnection(
DisconnectCauseUtil.toTelecomDisconnectCause(
android.telephony.DisconnectCause.POWER_OFF,
"ServiceState.STATE_POWER_OFF"));
default:
Log.d(this, "onCreateOutgoingConnection, unknown service state: %d", state);
return Connection.createFailedConnection(
DisconnectCauseUtil.toTelecomDisconnectCause(
android.telephony.DisconnectCause.OUTGOING_FAILURE,
"Unknown service state " + state));
}
}
final Context context = getApplicationContext();
if (VideoProfile.isVideo(request.getVideoState()) && isTtyModeEnabled(context) &&
!isEmergencyNumber) {
return Connection.createFailedConnection(DisconnectCauseUtil.toTelecomDisconnectCause(
android.telephony.DisconnectCause.VIDEO_CALL_NOT_ALLOWED_WHILE_TTY_ENABLED));
}
// Check for additional limits on CDMA phones.
final Connection failedConnection = checkAdditionalOutgoingCallLimits(phone);
if (failedConnection != null) {
return failedConnection;
}
final TelephonyConnection connection =
createConnectionFor(phone, null, true /* isOutgoing */, request.getAccountHandle(),
request.getTelecomCallId(), request.getAddress(), request.getVideoState());
if (connection == null) {
return Connection.createFailedConnection(
DisconnectCauseUtil.toTelecomDisconnectCause(
android.telephony.DisconnectCause.OUTGOING_FAILURE,
"Invalid phone type"));
}
connection.setAddress(handle, PhoneConstants.PRESENTATION_ALLOWED);
connection.setInitializing();
connection.setVideoState(request.getVideoState());
if (useEmergencyCallHelper) {
if (mEmergencyCallHelper == null) {
mEmergencyCallHelper = new EmergencyCallHelper(this);
}
mEmergencyCallHelper.enableEmergencyCalling(new EmergencyCallStateListener.Callback() {
@Override
public void onComplete(EmergencyCallStateListener listener,
boolean isRadioReady) {
if (connection.getState() == Connection.STATE_DISCONNECTED) {
// If the connection has already been disconnected, do nothing.
} else if (isRadioReady) {
connection.setInitialized();
placeOutgoingConnection(connection, phone, request);
} else {
Log.d(this, "onCreateOutgoingConnection, failed to turn on radio");
connection.setDisconnected(
DisconnectCauseUtil.toTelecomDisconnectCause(
android.telephony.DisconnectCause.POWER_OFF,
"Failed to turn on radio."));
connection.destroy();
}
}
});
} else {
placeOutgoingConnection(connection, phone, request);
}
return connection;
}
@Override
public Connection onCreateIncomingConnection(
PhoneAccountHandle connectionManagerPhoneAccount,
ConnectionRequest request) {
Log.i(this, "onCreateIncomingConnection, request: " + request);
// If there is an incoming emergency CDMA Call (while the phone is in ECBM w/ No SIM),
// make sure the PhoneAccount lookup retrieves the default Emergency Phone.
PhoneAccountHandle accountHandle = request.getAccountHandle();
boolean isEmergency = false;
if (accountHandle != null && PhoneUtils.EMERGENCY_ACCOUNT_HANDLE_ID.equals(
accountHandle.getId())) {
Log.i(this, "Emergency PhoneAccountHandle is being used for incoming call... " +
"Treat as an Emergency Call.");
isEmergency = true;
}
Phone phone = getPhoneForAccount(accountHandle, isEmergency);
if (phone == null) {
return Connection.createFailedConnection(
DisconnectCauseUtil.toTelecomDisconnectCause(
android.telephony.DisconnectCause.ERROR_UNSPECIFIED,
"Phone is null"));
}
Call call = phone.getRingingCall();
if (!call.getState().isRinging()) {
Log.i(this, "onCreateIncomingConnection, no ringing call");
return Connection.createFailedConnection(
DisconnectCauseUtil.toTelecomDisconnectCause(
android.telephony.DisconnectCause.INCOMING_MISSED,
"Found no ringing call"));
}
com.android.internal.telephony.Connection originalConnection =
call.getState() == Call.State.WAITING ?
call.getLatestConnection() : call.getEarliestConnection();
if (isOriginalConnectionKnown(originalConnection)) {
Log.i(this, "onCreateIncomingConnection, original connection already registered");
return Connection.createCanceledConnection();
}
// We should rely on the originalConnection to get the video state. The request coming
// from Telecom does not know the video state of the incoming call.
int videoState = originalConnection != null ? originalConnection.getVideoState() :
VideoProfile.STATE_AUDIO_ONLY;
Connection connection =
createConnectionFor(phone, originalConnection, false /* isOutgoing */,
request.getAccountHandle(), request.getTelecomCallId(),
request.getAddress(), videoState);
if (connection == null) {
return Connection.createCanceledConnection();
} else {
return connection;
}
}
@Override
public void triggerConferenceRecalculate() {
if (mTelephonyConferenceController.shouldRecalculate()) {
mTelephonyConferenceController.recalculate();
}
}
@Override
public Connection onCreateUnknownConnection(PhoneAccountHandle connectionManagerPhoneAccount,
ConnectionRequest request) {
Log.i(this, "onCreateUnknownConnection, request: " + request);
// Use the registered emergency Phone if the PhoneAccountHandle is set to Telephony's
// Emergency PhoneAccount
PhoneAccountHandle accountHandle = request.getAccountHandle();
boolean isEmergency = false;
if (accountHandle != null && PhoneUtils.EMERGENCY_ACCOUNT_HANDLE_ID.equals(
accountHandle.getId())) {
Log.i(this, "Emergency PhoneAccountHandle is being used for unknown call... " +
"Treat as an Emergency Call.");
isEmergency = true;
}
Phone phone = getPhoneForAccount(accountHandle, isEmergency);
if (phone == null) {
return Connection.createFailedConnection(
DisconnectCauseUtil.toTelecomDisconnectCause(
android.telephony.DisconnectCause.ERROR_UNSPECIFIED,
"Phone is null"));
}
Bundle extras = request.getExtras();
final List<com.android.internal.telephony.Connection> allConnections = new ArrayList<>();
// Handle the case where an unknown connection has an IMS external call ID specified; we can
// skip the rest of the guesswork and just grad that unknown call now.
if (phone.getImsPhone() != null && extras != null &&
extras.containsKey(ImsExternalCallTracker.EXTRA_IMS_EXTERNAL_CALL_ID)) {
ImsPhone imsPhone = (ImsPhone) phone.getImsPhone();
ImsExternalCallTracker externalCallTracker = imsPhone.getExternalCallTracker();
int externalCallId = extras.getInt(ImsExternalCallTracker.EXTRA_IMS_EXTERNAL_CALL_ID,
-1);
if (externalCallTracker != null) {
com.android.internal.telephony.Connection connection =
externalCallTracker.getConnectionById(externalCallId);
if (connection != null) {
allConnections.add(connection);
}
}
}
if (allConnections.isEmpty()) {
final Call ringingCall = phone.getRingingCall();
if (ringingCall.hasConnections()) {
allConnections.addAll(ringingCall.getConnections());
}
final Call foregroundCall = phone.getForegroundCall();
if ((foregroundCall.getState() != Call.State.DISCONNECTED)
&& (foregroundCall.hasConnections())) {
allConnections.addAll(foregroundCall.getConnections());
}
if (phone.getImsPhone() != null) {
final Call imsFgCall = phone.getImsPhone().getForegroundCall();
if ((imsFgCall.getState() != Call.State.DISCONNECTED) && imsFgCall
.hasConnections()) {
allConnections.addAll(imsFgCall.getConnections());
}
}
final Call backgroundCall = phone.getBackgroundCall();
if (backgroundCall.hasConnections()) {
allConnections.addAll(phone.getBackgroundCall().getConnections());
}
}
com.android.internal.telephony.Connection unknownConnection = null;
for (com.android.internal.telephony.Connection telephonyConnection : allConnections) {
if (!isOriginalConnectionKnown(telephonyConnection)) {
unknownConnection = telephonyConnection;
Log.d(this, "onCreateUnknownConnection: conn = " + unknownConnection);
break;
}
}
if (unknownConnection == null) {
Log.i(this, "onCreateUnknownConnection, did not find previously unknown connection.");
return Connection.createCanceledConnection();
}
// We should rely on the originalConnection to get the video state. The request coming
// from Telecom does not know the video state of the unknown call.
int videoState = unknownConnection != null ? unknownConnection.getVideoState() :
VideoProfile.STATE_AUDIO_ONLY;
TelephonyConnection connection =
createConnectionFor(phone, unknownConnection,
!unknownConnection.isIncoming() /* isOutgoing */,
request.getAccountHandle(), request.getTelecomCallId(),
request.getAddress(), videoState);
if (connection == null) {
return Connection.createCanceledConnection();
} else {
connection.updateState();
return connection;
}
}
@Override
public void onConference(Connection connection1, Connection connection2) {
if (connection1 instanceof TelephonyConnection &&
connection2 instanceof TelephonyConnection) {
((TelephonyConnection) connection1).performConference(
(TelephonyConnection) connection2);
}
}
private void placeOutgoingConnection(
TelephonyConnection connection, Phone phone, ConnectionRequest request) {
String number = connection.getAddress().getSchemeSpecificPart();
com.android.internal.telephony.Connection originalConnection;
try {
originalConnection =
phone.dial(number, null, request.getVideoState(), request.getExtras());
} catch (CallStateException e) {
Log.e(this, e, "placeOutgoingConnection, phone.dial exception: " + e);
int cause = android.telephony.DisconnectCause.OUTGOING_FAILURE;
if (e.getError() == CallStateException.ERROR_DISCONNECTED) {
cause = android.telephony.DisconnectCause.OUT_OF_SERVICE;
}
connection.setDisconnected(DisconnectCauseUtil.toTelecomDisconnectCause(
cause, e.getMessage()));
return;
}
if (originalConnection == null) {
int telephonyDisconnectCause = android.telephony.DisconnectCause.OUTGOING_FAILURE;
// On GSM phones, null connection means that we dialed an MMI code
if (phone.getPhoneType() == PhoneConstants.PHONE_TYPE_GSM) {
Log.d(this, "dialed MMI code");
telephonyDisconnectCause = android.telephony.DisconnectCause.DIALED_MMI;
final Intent intent = new Intent(this, MMIDialogActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
startActivity(intent);
}
Log.d(this, "placeOutgoingConnection, phone.dial returned null");
connection.setDisconnected(DisconnectCauseUtil.toTelecomDisconnectCause(
telephonyDisconnectCause, "Connection is null"));
} else {
connection.setOriginalConnection(originalConnection);
}
}
private TelephonyConnection createConnectionFor(
Phone phone,
com.android.internal.telephony.Connection originalConnection,
boolean isOutgoing,
PhoneAccountHandle phoneAccountHandle,
String telecomCallId,
Uri address,
int videoState) {
TelephonyConnection returnConnection = null;
int phoneType = phone.getPhoneType();
if (phoneType == TelephonyManager.PHONE_TYPE_GSM) {
returnConnection = new GsmConnection(originalConnection, telecomCallId);
} else if (phoneType == TelephonyManager.PHONE_TYPE_CDMA) {
boolean allowsMute = allowsMute(phone);
returnConnection = new CdmaConnection(originalConnection, mEmergencyTonePlayer,
allowsMute, isOutgoing, telecomCallId);
}
if (returnConnection != null) {
// Listen to Telephony specific callbacks from the connection
returnConnection.addTelephonyConnectionListener(mTelephonyConnectionListener);
returnConnection.setVideoPauseSupported(
TelecomAccountRegistry.getInstance(this).isVideoPauseSupported(
phoneAccountHandle));
boolean isEmergencyCall = (address != null && PhoneNumberUtils.isEmergencyNumber(
address.getSchemeSpecificPart()));
boolean isVideoCall = VideoProfile.isVideo(videoState);
boolean isConferencingSupported = TelecomAccountRegistry.getInstance(this)
.isMergeCallSupported(phoneAccountHandle);
boolean isVideoConferencingSupported = TelecomAccountRegistry.getInstance(this)
.isVideoConferencingSupported(phoneAccountHandle);
returnConnection.setConferenceSupported(!isEmergencyCall && isConferencingSupported
&& (!isVideoCall || (isVideoCall && isVideoConferencingSupported)));
}
return returnConnection;
}
private boolean isOriginalConnectionKnown(
com.android.internal.telephony.Connection originalConnection) {
for (Connection connection : getAllConnections()) {
if (connection instanceof TelephonyConnection) {
TelephonyConnection telephonyConnection = (TelephonyConnection) connection;
if (telephonyConnection.getOriginalConnection() == originalConnection) {
return true;
}
}
}
return false;
}
private Phone getPhoneForAccount(PhoneAccountHandle accountHandle, boolean isEmergency) {
Phone chosenPhone = null;
int subId = PhoneUtils.getSubIdForPhoneAccountHandle(accountHandle);
if (subId != SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
int phoneId = SubscriptionController.getInstance().getPhoneId(subId);
chosenPhone = PhoneFactory.getPhone(phoneId);
}
// If this is an emergency call and the phone we originally planned to make this call
// with is not in service or was invalid, try to find one that is in service, using the
// default as a last chance backup.
if (isEmergency && (chosenPhone == null || ServiceState.STATE_IN_SERVICE != chosenPhone
.getServiceState().getState())) {
Log.d(this, "getPhoneForAccount: phone for phone acct handle %s is out of service "
+ "or invalid for emergency call.", accountHandle);
chosenPhone = getFirstPhoneForEmergencyCall();
Log.d(this, "getPhoneForAccount: using subId: " +
(chosenPhone == null ? "null" : chosenPhone.getSubId()));
}
return chosenPhone;
}
/**
* Retrieves the most sensible Phone to use for an emergency call using the following Priority
* list (for multi-SIM devices):
* 1) The User's SIM preference for Voice calling
* 2) The First Phone that is currently IN_SERVICE or is available for emergency calling
* 3) The First Phone that has a SIM card in it (Starting from Slot 0...N)
* 4) The Default Phone (Currently set as Slot 0)
*/
private Phone getFirstPhoneForEmergencyCall() {
Phone firstPhoneWithSim = null;
// 1)
int phoneId = SubscriptionManager.getDefaultVoicePhoneId();
if (phoneId != SubscriptionManager.INVALID_PHONE_INDEX) {
Phone defaultPhone = PhoneFactory.getPhone(phoneId);
if (defaultPhone != null && isAvailableForEmergencyCalls(defaultPhone)) {
return defaultPhone;
}
}
for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
Phone phone = PhoneFactory.getPhone(i);
if (phone == null)
continue;
// 2)
if (isAvailableForEmergencyCalls(phone)) {
// the slot has the radio on & state is in service.
Log.d(this, "getFirstPhoneForEmergencyCall, radio on & in service, Phone Id:" + i);
return phone;
}
// 3)
if (firstPhoneWithSim == null && TelephonyManager.getDefault().hasIccCard(i)) {
// The slot has a SIM card inserted, but is not in service, so keep track of this
// Phone. Do not return because we want to make sure that none of the other Phones
// are in service (because that is always faster).
Log.d(this, "getFirstPhoneForEmergencyCall, SIM card inserted, Phone Id:" + i);
firstPhoneWithSim = phone;
}
}
// 4)
if (firstPhoneWithSim == null) {
// No SIMs inserted, get the default.
Log.d(this, "getFirstPhoneForEmergencyCall, return default phone");
return PhoneFactory.getDefaultPhone();
} else {
return firstPhoneWithSim;
}
}
/**
* Returns true if the state of the Phone is IN_SERVICE or available for emergency calling only.
*/
private boolean isAvailableForEmergencyCalls(Phone phone) {
return ServiceState.STATE_IN_SERVICE == phone.getServiceState().getState() ||
phone.getServiceState().isEmergencyOnly();
}
/**
* Determines if the connection should allow mute.
*
* @param phone The current phone.
* @return {@code True} if the connection should allow mute.
*/
private boolean allowsMute(Phone phone) {
// For CDMA phones, check if we are in Emergency Callback Mode (ECM). Mute is disallowed
// in ECM mode.
if (phone.getPhoneType() == TelephonyManager.PHONE_TYPE_CDMA) {
if (phone.isInEcm()) {
return false;
}
}
return true;
}
@Override
public void removeConnection(Connection connection) {
super.removeConnection(connection);
if (connection instanceof TelephonyConnection) {
TelephonyConnection telephonyConnection = (TelephonyConnection) connection;
telephonyConnection.removeTelephonyConnectionListener(mTelephonyConnectionListener);
}
}
/**
* When a {@link TelephonyConnection} has its underlying original connection configured,
* we need to add it to the correct conference controller.
*
* @param connection The connection to be added to the controller
*/
public void addConnectionToConferenceController(TelephonyConnection connection) {
// TODO: Do we need to handle the case of the original connection changing
// and triggering this callback multiple times for the same connection?
// If that is the case, we might want to remove this connection from all
// conference controllers first before re-adding it.
if (connection.isImsConnection()) {
Log.d(this, "Adding IMS connection to conference controller: " + connection);
mImsConferenceController.add(connection);
} else {
int phoneType = connection.getCall().getPhone().getPhoneType();
if (phoneType == TelephonyManager.PHONE_TYPE_GSM) {
Log.d(this, "Adding GSM connection to conference controller: " + connection);
mTelephonyConferenceController.add(connection);
} else if (phoneType == TelephonyManager.PHONE_TYPE_CDMA &&
connection instanceof CdmaConnection) {
Log.d(this, "Adding CDMA connection to conference controller: " + connection);
mCdmaConferenceController.add((CdmaConnection)connection);
}
Log.d(this, "Removing connection from IMS conference controller: " + connection);
mImsConferenceController.remove(connection);
}
}
/**
* Create a new CDMA connection. CDMA connections have additional limitations when creating
* additional calls which are handled in this method. Specifically, CDMA has a "FLASH" command
* that can be used for three purposes: merging a call, swapping unmerged calls, and adding
* a new outgoing call. The function of the flash command depends on the context of the current
* set of calls. This method will prevent an outgoing call from being made if it is not within
* the right circumstances to support adding a call.
*/
private Connection checkAdditionalOutgoingCallLimits(Phone phone) {
if (phone.getPhoneType() == TelephonyManager.PHONE_TYPE_CDMA) {
// Check to see if any CDMA conference calls exist, and if they do, check them for
// limitations.
for (Conference conference : getAllConferences()) {
if (conference instanceof CdmaConference) {
CdmaConference cdmaConf = (CdmaConference) conference;
// If the CDMA conference has not been merged, add-call will not work, so fail
// this request to add a call.
if (cdmaConf.can(Connection.CAPABILITY_MERGE_CONFERENCE)) {
return Connection.createFailedConnection(new DisconnectCause(
DisconnectCause.RESTRICTED,
null,
getResources().getString(R.string.callFailed_cdma_call_limit),
"merge-capable call exists, prevent flash command."));
}
}
}
}
return null; // null means nothing went wrong, and call should continue.
}
private boolean isTtyModeEnabled(Context context) {
return (android.provider.Settings.Secure.getInt(
context.getContentResolver(),
android.provider.Settings.Secure.PREFERRED_TTY_MODE,
TelecomManager.TTY_MODE_OFF) != TelecomManager.TTY_MODE_OFF);
}
}