Implement PSTN call services
Change-Id: I6239f0d9c68a3c08b63553b3fca266631211c759
diff --git a/src/com/android/phone/PhoneUtils.java b/src/com/android/phone/PhoneUtils.java
index 69a7995..4ceefe1 100644
--- a/src/com/android/phone/PhoneUtils.java
+++ b/src/com/android/phone/PhoneUtils.java
@@ -2627,7 +2627,7 @@
/**
* @return true if this device supports voice calls using the built-in SIP stack.
*/
- static boolean isVoipSupported() {
+ public static boolean isVoipSupported() {
return sVoipSupported;
}
diff --git a/src/com/android/services/telephony/BaseTelephonyCallService.java b/src/com/android/services/telephony/BaseTelephonyCallService.java
new file mode 100644
index 0000000..fe7402d
--- /dev/null
+++ b/src/com/android/services/telephony/BaseTelephonyCallService.java
@@ -0,0 +1,112 @@
+/*
+ * 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.net.Uri;
+import android.os.RemoteException;
+import android.telecomm.CallInfo;
+import android.telecomm.CallService;
+import android.telecomm.ICallServiceAdapter;
+import android.text.TextUtils;
+import android.util.Log;
+
+import com.android.internal.telephony.CallStateException;
+import com.android.internal.telephony.Connection;
+import com.android.internal.telephony.Phone;
+
+import java.util.HashMap;
+
+/**
+ * The parent class for telephony-based call services. Subclasses provide the specific phone (GSM,
+ * CDMA, etc...) to use.
+ */
+public abstract class BaseTelephonyCallService extends CallService {
+ private static final String TAG = "BaseTeleCallService";
+
+ protected ICallServiceAdapter mCallServiceAdapter;
+
+ /** Map of all call connections keyed by the call ID. */
+ private static HashMap<String, TelephonyCallConnection> sCallConnections =
+ new HashMap<String, TelephonyCallConnection>();
+
+ /**
+ * Clears the connection from the list of connections. Called when a phone call disconnects.
+ */
+ static void onCallConnectionClosing(TelephonyCallConnection callConnection) {
+ sCallConnections.remove(callConnection.getCallId());
+ }
+
+ /** {@inheritDoc} */
+ @Override
+ public void setCallServiceAdapter(ICallServiceAdapter callServiceAdapter) {
+ mCallServiceAdapter = callServiceAdapter;
+ }
+
+ /** {@inheritDoc} */
+ @Override
+ public void disconnect(String callId) {
+ // Maybe null if the connection has already disconnected.
+ if (sCallConnections.containsKey(callId)) {
+ sCallConnections.get(callId).disconnect();
+ }
+ }
+
+ /** {@inheritDoc} */
+ @Override
+ public void confirmIncomingCall(String callId, String callToken) {
+ // Incoming calls not implemented yet.
+ }
+
+ /**
+ * Initiates the call, should be called by the subclass.
+ */
+ protected void startCallWithPhone(Phone phone, CallInfo callInfo) {
+ try {
+ if (phone == null) {
+ mCallServiceAdapter.handleFailedOutgoingCall(callInfo.getId(), "Phone is null");
+ return;
+ }
+
+ Uri uri = Uri.parse(callInfo.getHandle());
+ String number = null;
+ if (uri != null) {
+ number = uri.getSchemeSpecificPart();
+ }
+ if (TextUtils.isEmpty(number)) {
+ mCallServiceAdapter.handleFailedOutgoingCall(callInfo.getId(),
+ "Unable to parse number");
+ return;
+ }
+
+ Connection connection;
+ try {
+ connection = phone.dial(number);
+ } catch (CallStateException e) {
+ Log.e(TAG, "Call to Phone.dial failed with exception", e);
+ mCallServiceAdapter.handleFailedOutgoingCall(callInfo.getId(), e.getMessage());
+ return;
+ }
+
+ TelephonyCallConnection callConnection =
+ new TelephonyCallConnection(mCallServiceAdapter, callInfo.getId(), connection);
+ sCallConnections.put(callInfo.getId(), callConnection);
+ mCallServiceAdapter.handleSuccessfulOutgoingCall(callInfo.getId());
+ } catch (RemoteException e) {
+ Log.e(TAG, "Got RemoteException", e);
+ }
+ }
+}
diff --git a/src/com/android/services/telephony/CdmaCallService.java b/src/com/android/services/telephony/CdmaCallService.java
new file mode 100644
index 0000000..0971ac1
--- /dev/null
+++ b/src/com/android/services/telephony/CdmaCallService.java
@@ -0,0 +1,59 @@
+/*
+ * 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.Context;
+import android.os.RemoteException;
+import android.telecomm.CallInfo;
+import android.telephony.TelephonyManager;
+import android.util.Log;
+
+import com.android.internal.telephony.Phone;
+import com.android.internal.telephony.PhoneFactory;
+
+/**
+ * Call service that uses the CDMA phone.
+ */
+public class CdmaCallService extends BaseTelephonyCallService {
+ private static final String TAG = CdmaCallService.class.getSimpleName();
+ private static Phone sCdmaPhone;
+
+ static boolean shouldSelect(Context context, CallInfo callInfo) {
+ TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(
+ Context.TELEPHONY_SERVICE);
+ return telephonyManager.getPhoneType() == TelephonyManager.PHONE_TYPE_CDMA;
+ }
+
+ /** {@inheritDoc} */
+ @Override
+ public void isCompatibleWith(CallInfo callInfo) {
+ try {
+ mCallServiceAdapter.setCompatibleWith(callInfo.getId(), shouldSelect(this, callInfo));
+ } catch (RemoteException e) {
+ Log.e(TAG, "Call to setCompatibleWith failed with exception", e);
+ }
+ }
+
+ /** {@inheritDoc} */
+ @Override
+ public void call(CallInfo callInfo) {
+ if (sCdmaPhone == null) {
+ sCdmaPhone = PhoneFactory.getCdmaPhone();
+ }
+ startCallWithPhone(sCdmaPhone, callInfo);
+ }
+}
diff --git a/src/com/android/services/telephony/GsmCallService.java b/src/com/android/services/telephony/GsmCallService.java
new file mode 100644
index 0000000..5d7440a
--- /dev/null
+++ b/src/com/android/services/telephony/GsmCallService.java
@@ -0,0 +1,59 @@
+/*
+ * 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.Context;
+import android.os.RemoteException;
+import android.telecomm.CallInfo;
+import android.telephony.TelephonyManager;
+import android.util.Log;
+
+import com.android.internal.telephony.Phone;
+import com.android.internal.telephony.PhoneFactory;
+
+/**
+ * Call service that uses the GSM phone.
+ */
+public class GsmCallService extends BaseTelephonyCallService {
+ private static final String TAG = GsmCallService.class.getSimpleName();
+ private static Phone sGsmPhone;
+
+ static boolean shouldSelect(Context context, CallInfo callInfo) {
+ TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(
+ Context.TELEPHONY_SERVICE);
+ return telephonyManager.getPhoneType() == TelephonyManager.PHONE_TYPE_GSM;
+ }
+
+ /** {@inheritDoc} */
+ @Override
+ public void isCompatibleWith(CallInfo callInfo) {
+ try {
+ mCallServiceAdapter.setCompatibleWith(callInfo.getId(), shouldSelect(this, callInfo));
+ } catch (RemoteException e) {
+ Log.e(TAG, "Call to setCompatibleWith failed with exception", e);
+ }
+ }
+
+ /** {@inheritDoc} */
+ @Override
+ public void call(CallInfo callInfo) {
+ if (sGsmPhone == null) {
+ sGsmPhone = PhoneFactory.getGsmPhone();
+ }
+ startCallWithPhone(sGsmPhone, callInfo);
+ }
+}
diff --git a/src/com/android/services/telephony/SipCallService.java b/src/com/android/services/telephony/SipCallService.java
new file mode 100644
index 0000000..37589fd
--- /dev/null
+++ b/src/com/android/services/telephony/SipCallService.java
@@ -0,0 +1,150 @@
+/*
+ * 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.Context;
+import android.os.RemoteException;
+import android.net.sip.SipException;
+import android.net.sip.SipManager;
+import android.net.sip.SipProfile;
+import android.net.Uri;
+import android.os.AsyncTask;
+import android.provider.Settings;
+import android.telecomm.CallInfo;
+import android.telephony.PhoneNumberUtils;
+import android.util.Log;
+
+import com.android.internal.telephony.Phone;
+import com.android.internal.telephony.PhoneFactory;
+import com.android.internal.telephony.sip.SipPhone;
+import com.android.phone.Constants;
+import com.android.phone.PhoneUtils;
+import com.android.phone.sip.SipProfileDb;
+import com.android.phone.sip.SipSharedPreferences;
+
+import java.util.HashMap;
+
+/**
+ * Call service that uses the SIP phone.
+ */
+public class SipCallService extends BaseTelephonyCallService {
+ private static final String TAG = SipCallService.class.getSimpleName();
+ private static HashMap<String, SipPhone> sSipPhones = new HashMap<String, SipPhone>();
+
+ static boolean shouldSelect(Context context, CallInfo callInfo) {
+ Uri uri = Uri.parse(callInfo.getHandle());
+ return shouldUseSipPhone(context, uri.getScheme(), uri.getSchemeSpecificPart());
+ }
+
+ private static boolean shouldUseSipPhone(Context context, String scheme, String number) {
+ // Scheme must be "sip" or "tel".
+ boolean isKnownCallScheme = Constants.SCHEME_TEL.equals(scheme)
+ || Constants.SCHEME_SIP.equals(scheme);
+ if (!isKnownCallScheme) {
+ return false;
+ }
+
+ // Is voip supported
+ boolean voipSupported = PhoneUtils.isVoipSupported();
+ if (!voipSupported) {
+ return false;
+ }
+
+ // Check SIP address only
+ SipSharedPreferences sipSharedPreferences = new SipSharedPreferences(context);
+ String callOption = sipSharedPreferences.getSipCallOption();
+ boolean isRegularNumber = Constants.SCHEME_TEL.equals(scheme)
+ && !PhoneNumberUtils.isUriNumber(number);
+ if (callOption.equals(Settings.System.SIP_ADDRESS_ONLY) && isRegularNumber) {
+ return false;
+ }
+
+ // Check if no SIP profiles.
+ SipProfileDb sipProfileDb = new SipProfileDb(context);
+ if (sipProfileDb.getProfilesCount() == 0 && isRegularNumber) {
+ return false;
+ }
+
+ return true;
+ }
+
+ /** {@inheritDoc} */
+ @Override
+ public void isCompatibleWith(CallInfo callInfo) {
+ try {
+ mCallServiceAdapter.setCompatibleWith(callInfo.getId(), shouldSelect(this, callInfo));
+ } catch (RemoteException e) {
+ Log.e(TAG, "Call to setCompatibleWith failed with exception", e);
+ }
+ }
+
+ /** {@inheritDoc} */
+ @Override
+ public void call(CallInfo callInfo) {
+ new GetSipProfileTask(this, callInfo).execute();
+ }
+
+ /**
+ * Asynchronously looks up the SIP profile to use for the given call.
+ */
+ private class GetSipProfileTask extends AsyncTask<Void, Void, SipProfile> {
+ private final CallInfo mCallInfo;
+ private final SipProfileDb mSipProfileDb;
+ private final SipSharedPreferences mSipSharedPreferences;
+
+ GetSipProfileTask(Context context, CallInfo callInfo) {
+ mCallInfo = callInfo;
+ mSipProfileDb = new SipProfileDb(context);
+ mSipSharedPreferences = new SipSharedPreferences(context);
+ }
+
+ @Override
+ protected SipProfile doInBackground(Void... params) {
+ String primarySipUri = mSipSharedPreferences.getPrimaryAccount();
+ for (SipProfile profile : mSipProfileDb.retrieveSipProfileList()) {
+ if (profile.getUriString().equals(primarySipUri)) {
+ return profile;
+ }
+ }
+ // TODO(sail): Handle non-primary profiles by showing dialog.
+ return null;
+ }
+
+ @Override
+ protected void onPostExecute(SipProfile profile) {
+ onSipProfileChosen(profile, mCallInfo);
+ }
+ }
+
+ private void onSipProfileChosen(SipProfile profile, CallInfo callInfo) {
+ SipPhone phone = null;
+ if (profile != null) {
+ String sipUri = profile.getUriString();
+ phone = sSipPhones.get(sipUri);
+ if (phone == null) {
+ try {
+ SipManager.newInstance(this).open(profile);
+ phone = (SipPhone) PhoneFactory.makeSipPhone(sipUri);
+ sSipPhones.put(sipUri, phone);
+ } catch (SipException e) {
+ Log.e(TAG, "Failed to make a SIP phone", e);
+ }
+ }
+ }
+ startCallWithPhone(phone, callInfo);
+ }
+}
diff --git a/src/com/android/services/telephony/TelephonyCallConnection.java b/src/com/android/services/telephony/TelephonyCallConnection.java
new file mode 100644
index 0000000..278943b
--- /dev/null
+++ b/src/com/android/services/telephony/TelephonyCallConnection.java
@@ -0,0 +1,132 @@
+/*
+ * 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.os.Handler;
+import android.os.Message;
+import android.os.RemoteException;
+import android.telecomm.ICallServiceAdapter;
+import android.util.Log;
+
+import com.android.internal.telephony.Call;
+import com.android.internal.telephony.CallStateException;
+import com.android.internal.telephony.Connection;
+
+/**
+ * Manages a single phone call. Listens to the call's state changes and updates the
+ * ICallServiceAdapter.
+ */
+class TelephonyCallConnection {
+ private static final String TAG = TelephonyCallConnection.class.getSimpleName();
+ private static final int EVENT_PRECISE_CALL_STATE_CHANGED = 1;
+
+ private final ICallServiceAdapter mCallServiceAdapter;
+ private final String mCallId;
+ private final StateHandler mHandler = new StateHandler();
+
+ private Connection mConnection;
+ private Call.State mOldState = Call.State.IDLE;
+
+ TelephonyCallConnection(ICallServiceAdapter callServiceAdapter, String callId,
+ Connection connection) {
+ mCallServiceAdapter = callServiceAdapter;
+ mCallId = callId;
+ mConnection = connection;
+ mConnection.getCall().getPhone().registerForPreciseCallStateChanged(mHandler,
+ EVENT_PRECISE_CALL_STATE_CHANGED, null);
+ updateState();
+ }
+
+ String getCallId() {
+ return mCallId;
+ }
+
+ void disconnect() {
+ if (mConnection != null) {
+ try {
+ mConnection.hangup();
+ } catch (CallStateException e) {
+ Log.e(TAG, "Call to Connection.hangup failed with exception", e);
+ }
+ }
+ }
+
+ private void updateState() {
+ if (mConnection == null) {
+ return;
+ }
+
+ Call.State newState = mConnection.getState();
+ if (mOldState == newState) {
+ return;
+ }
+
+ mOldState = newState;
+ try {
+ switch (newState) {
+ case IDLE:
+ break;
+ case ACTIVE:
+ mCallServiceAdapter.setActive(mCallId);
+ break;
+ case HOLDING:
+ break;
+ case DIALING:
+ mCallServiceAdapter.setDialing(mCallId);
+ break;
+ case ALERTING:
+ mCallServiceAdapter.setDialing(mCallId);
+ break;
+ case INCOMING:
+ // Incoming calls not implemented.
+ break;
+ case WAITING:
+ break;
+ case DISCONNECTED:
+ mCallServiceAdapter.setDisconnected(mCallId);
+ close();
+ break;
+ case DISCONNECTING:
+ break;
+ }
+ } catch (RemoteException e) {
+ Log.e(TAG, "Remote exception", e);
+ }
+ }
+
+ private void close() {
+ if (mConnection != null) {
+ Call call = mConnection.getCall();
+ if (call != null) {
+ call.getPhone().unregisterForPreciseCallStateChanged(mHandler);
+ }
+ mConnection = null;
+ }
+ BaseTelephonyCallService.onCallConnectionClosing(this);
+ }
+
+ private class StateHandler extends Handler {
+ @Override
+ public void handleMessage(Message msg) {
+ switch (msg.what) {
+ case EVENT_PRECISE_CALL_STATE_CHANGED:
+ updateState();
+ break;
+ }
+ }
+ }
+}
diff --git a/src/com/android/services/telephony/TelephonyCallServiceProvider.java b/src/com/android/services/telephony/TelephonyCallServiceProvider.java
new file mode 100644
index 0000000..9ff1401
--- /dev/null
+++ b/src/com/android/services/telephony/TelephonyCallServiceProvider.java
@@ -0,0 +1,57 @@
+/*
+ * 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.os.IBinder;
+import android.os.RemoteException;
+import android.telecomm.CallServiceDescriptor;
+import android.telecomm.CallServiceProvider;
+import android.telecomm.ICallServiceLookupResponse;
+import android.util.Log;
+
+import java.util.ArrayList;
+
+/**
+ * This class is used to get a list of all CallServices.
+ */
+public class TelephonyCallServiceProvider extends CallServiceProvider {
+ private static final String TAG = "TeleCallServiceProvider";
+
+ /** {@inheritDoc} */
+ @Override
+ public void lookupCallServices(ICallServiceLookupResponse response) {
+ ArrayList<CallServiceDescriptor> descriptors = new ArrayList<CallServiceDescriptor>();
+ descriptors.add(CallServiceDescriptor.newBuilder(this)
+ .setCallService(GsmCallService.class)
+ .setNetworkType(CallServiceDescriptor.FLAG_PSTN)
+ .build());
+ descriptors.add(CallServiceDescriptor.newBuilder(this)
+ .setCallService(CdmaCallService.class)
+ .setNetworkType(CallServiceDescriptor.FLAG_PSTN)
+ .build());
+ descriptors.add(CallServiceDescriptor.newBuilder(this)
+ .setCallService(SipCallService.class)
+ .setNetworkType(CallServiceDescriptor.FLAG_WIFI |
+ CallServiceDescriptor.FLAG_MOBILE)
+ .build());
+ try {
+ response.setCallServiceDescriptors(descriptors);
+ } catch (RemoteException e) {
+ Log.e(TAG, "Call to setCallServices failed with exception", e);
+ }
+ }
+}
diff --git a/src/com/android/services/telephony/TelephonyCallServiceSelector.java b/src/com/android/services/telephony/TelephonyCallServiceSelector.java
new file mode 100644
index 0000000..af7ac71
--- /dev/null
+++ b/src/com/android/services/telephony/TelephonyCallServiceSelector.java
@@ -0,0 +1,65 @@
+/*
+ * 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.telecomm.CallInfo;
+import android.telecomm.CallServiceDescriptor;
+import android.telecomm.CallServiceSelector;;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Decides which call service should be used to place outgoing calls or to switch the call to.
+ */
+public class TelephonyCallServiceSelector extends CallServiceSelector {
+ /** {@inheritDoc} */
+ @Override
+ protected void isSwitchable(CallInfo callInfo, CallSwitchabilityResponse response) {
+ response.setSwitchable(false);
+ }
+
+ /** {@inheritDoc} */
+ @Override
+ protected void select(CallInfo callInfo, List<CallServiceDescriptor> descriptors,
+ CallServiceSelectionResponse response) {
+
+ ArrayList<CallServiceDescriptor> selectedDescriptors =
+ new ArrayList<CallServiceDescriptor>();
+ for (CallServiceDescriptor descriptor : descriptors) {
+ if (!getPackageName().equals(descriptor.getServiceComponent().getPackageName())) {
+ continue;
+ }
+
+ String name = descriptor.getServiceComponent().getClassName();
+ if (name.equals(CdmaCallService.class.getName())) {
+ if (CdmaCallService.shouldSelect(this, callInfo)) {
+ selectedDescriptors.add(descriptor);
+ }
+ } else if (name.equals(GsmCallService.class.getName())) {
+ if (GsmCallService.shouldSelect(this, callInfo)) {
+ selectedDescriptors.add(descriptor);
+ }
+ } else if (name.equals(SipCallService.class.getName())) {
+ if (SipCallService.shouldSelect(this, callInfo)) {
+ selectedDescriptors.add(descriptor);
+ }
+ }
+ }
+ response.setSelectedCallServices(selectedDescriptors);
+ }
+}