Chiao Cheng | e41661c | 2013-07-23 13:28:26 -0700 | [diff] [blame] | 1 | /* |
Santos Cordon | cba1b44 | 2013-07-18 12:43:58 -0700 | [diff] [blame] | 2 | * Copyright (C) 2013 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
Chiao Cheng | e41661c | 2013-07-23 13:28:26 -0700 | [diff] [blame] | 14 | * limitations under the License |
Santos Cordon | cba1b44 | 2013-07-18 12:43:58 -0700 | [diff] [blame] | 15 | */ |
| 16 | |
| 17 | package com.android.phone; |
| 18 | |
Santos Cordon | eead6ec | 2013-08-07 22:16:33 -0700 | [diff] [blame] | 19 | import android.bluetooth.IBluetoothHeadsetPhone; |
Chiao Cheng | e41661c | 2013-07-23 13:28:26 -0700 | [diff] [blame] | 20 | import android.content.Context; |
Santos Cordon | eead6ec | 2013-08-07 22:16:33 -0700 | [diff] [blame] | 21 | import android.os.RemoteException; |
Santos Cordon | b01deb5 | 2013-08-06 23:46:06 -0700 | [diff] [blame] | 22 | import android.os.SystemProperties; |
Chiao Cheng | e41661c | 2013-07-23 13:28:26 -0700 | [diff] [blame] | 23 | import android.util.Log; |
| 24 | |
Santos Cordon | cba1b44 | 2013-07-18 12:43:58 -0700 | [diff] [blame] | 25 | import com.android.internal.telephony.CallManager; |
Santos Cordon | eead6ec | 2013-08-07 22:16:33 -0700 | [diff] [blame] | 26 | import com.android.internal.telephony.PhoneConstants; |
Santos Cordon | 249efd0 | 2013-08-05 03:33:56 -0700 | [diff] [blame] | 27 | import com.android.phone.CallModeler.CallResult; |
Santos Cordon | 9b7bac7 | 2013-08-06 08:04:52 -0700 | [diff] [blame] | 28 | import com.android.services.telephony.common.AudioMode; |
Santos Cordon | 249efd0 | 2013-08-05 03:33:56 -0700 | [diff] [blame] | 29 | import com.android.services.telephony.common.Call; |
Santos Cordon | cba1b44 | 2013-07-18 12:43:58 -0700 | [diff] [blame] | 30 | import com.android.services.telephony.common.ICallCommandService; |
| 31 | |
| 32 | /** |
Chiao Cheng | e41661c | 2013-07-23 13:28:26 -0700 | [diff] [blame] | 33 | * Service interface used by in-call ui to control phone calls using commands exposed as methods. |
| 34 | * Instances of this class are handed to in-call UI via CallMonitorService. |
Santos Cordon | cba1b44 | 2013-07-18 12:43:58 -0700 | [diff] [blame] | 35 | */ |
| 36 | class CallCommandService extends ICallCommandService.Stub { |
Chiao Cheng | e41661c | 2013-07-23 13:28:26 -0700 | [diff] [blame] | 37 | private static final String TAG = CallCommandService.class.getSimpleName(); |
Santos Cordon | b01deb5 | 2013-08-06 23:46:06 -0700 | [diff] [blame] | 38 | private static final boolean DBG = |
| 39 | (PhoneGlobals.DBG_LEVEL >= 1) && (SystemProperties.getInt("ro.debuggable", 0) == 1); |
Chiao Cheng | e41661c | 2013-07-23 13:28:26 -0700 | [diff] [blame] | 40 | |
Santos Cordon | 249efd0 | 2013-08-05 03:33:56 -0700 | [diff] [blame] | 41 | private final Context mContext; |
| 42 | private final CallManager mCallManager; |
| 43 | private final CallModeler mCallModeler; |
Santos Cordon | 2eaff90 | 2013-08-05 04:37:55 -0700 | [diff] [blame] | 44 | private final DTMFTonePlayer mDtmfTonePlayer; |
Santos Cordon | 9b7bac7 | 2013-08-06 08:04:52 -0700 | [diff] [blame] | 45 | private final AudioRouter mAudioRouter; |
Christine Chen | ee09a49 | 2013-08-06 16:02:29 -0700 | [diff] [blame] | 46 | private final RejectWithTextMessageManager mRejectWithTextMessageManager; |
Santos Cordon | cba1b44 | 2013-07-18 12:43:58 -0700 | [diff] [blame] | 47 | |
Santos Cordon | 2eaff90 | 2013-08-05 04:37:55 -0700 | [diff] [blame] | 48 | public CallCommandService(Context context, CallManager callManager, CallModeler callModeler, |
Christine Chen | ee09a49 | 2013-08-06 16:02:29 -0700 | [diff] [blame] | 49 | DTMFTonePlayer dtmfTonePlayer, AudioRouter audioRouter, |
| 50 | RejectWithTextMessageManager rejectWithTextMessageManager) { |
Chiao Cheng | e41661c | 2013-07-23 13:28:26 -0700 | [diff] [blame] | 51 | mContext = context; |
Santos Cordon | cba1b44 | 2013-07-18 12:43:58 -0700 | [diff] [blame] | 52 | mCallManager = callManager; |
Santos Cordon | 249efd0 | 2013-08-05 03:33:56 -0700 | [diff] [blame] | 53 | mCallModeler = callModeler; |
Santos Cordon | 2eaff90 | 2013-08-05 04:37:55 -0700 | [diff] [blame] | 54 | mDtmfTonePlayer = dtmfTonePlayer; |
Santos Cordon | 9b7bac7 | 2013-08-06 08:04:52 -0700 | [diff] [blame] | 55 | mAudioRouter = audioRouter; |
Christine Chen | ee09a49 | 2013-08-06 16:02:29 -0700 | [diff] [blame] | 56 | mRejectWithTextMessageManager = rejectWithTextMessageManager; |
Santos Cordon | cba1b44 | 2013-07-18 12:43:58 -0700 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | /** |
| 60 | * TODO(klp): Add a confirmation callback parameter. |
| 61 | */ |
| 62 | @Override |
| 63 | public void answerCall(int callId) { |
Chiao Cheng | e41661c | 2013-07-23 13:28:26 -0700 | [diff] [blame] | 64 | try { |
Santos Cordon | 249efd0 | 2013-08-05 03:33:56 -0700 | [diff] [blame] | 65 | CallResult result = mCallModeler.getCallWithId(callId); |
| 66 | if (result != null) { |
| 67 | PhoneUtils.answerCall(result.getConnection().getCall()); |
| 68 | } |
Chiao Cheng | e41661c | 2013-07-23 13:28:26 -0700 | [diff] [blame] | 69 | } catch (Exception e) { |
| 70 | Log.e(TAG, "Error during answerCall().", e); |
| 71 | } |
Santos Cordon | cba1b44 | 2013-07-18 12:43:58 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | /** |
| 75 | * TODO(klp): Add a confirmation callback parameter. |
| 76 | */ |
| 77 | @Override |
Christine Chen | ee09a49 | 2013-08-06 16:02:29 -0700 | [diff] [blame] | 78 | public void rejectCall(int callId, boolean rejectWithMessage, String message) { |
Chiao Cheng | e41661c | 2013-07-23 13:28:26 -0700 | [diff] [blame] | 79 | try { |
Santos Cordon | 249efd0 | 2013-08-05 03:33:56 -0700 | [diff] [blame] | 80 | CallResult result = mCallModeler.getCallWithId(callId); |
| 81 | if (result != null) { |
Yorke Lee | 814da30 | 2013-08-30 16:01:07 -0700 | [diff] [blame] | 82 | final String number = result.getConnection().getAddress(); |
Christine Chen | 0ce0e85 | 2013-08-09 18:26:31 -0700 | [diff] [blame^] | 83 | |
Christine Chen | ee09a49 | 2013-08-06 16:02:29 -0700 | [diff] [blame] | 84 | Log.v(TAG, "Hanging up"); |
Santos Cordon | 249efd0 | 2013-08-05 03:33:56 -0700 | [diff] [blame] | 85 | PhoneUtils.hangupRingingCall(result.getConnection().getCall()); |
Christine Chen | 0ce0e85 | 2013-08-09 18:26:31 -0700 | [diff] [blame^] | 86 | |
Yorke Lee | 814da30 | 2013-08-30 16:01:07 -0700 | [diff] [blame] | 87 | if (rejectWithMessage) { |
Christine Chen | 0ce0e85 | 2013-08-09 18:26:31 -0700 | [diff] [blame^] | 88 | mRejectWithTextMessageManager.rejectCallWithMessage( |
| 89 | result.getConnection().getCall(), message); |
Yorke Lee | 814da30 | 2013-08-30 16:01:07 -0700 | [diff] [blame] | 90 | } |
Santos Cordon | 249efd0 | 2013-08-05 03:33:56 -0700 | [diff] [blame] | 91 | } |
Chiao Cheng | e41661c | 2013-07-23 13:28:26 -0700 | [diff] [blame] | 92 | } catch (Exception e) { |
| 93 | Log.e(TAG, "Error during rejectCall().", e); |
| 94 | } |
Santos Cordon | cba1b44 | 2013-07-18 12:43:58 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | @Override |
| 98 | public void disconnectCall(int callId) { |
Chiao Cheng | e41661c | 2013-07-23 13:28:26 -0700 | [diff] [blame] | 99 | try { |
Santos Cordon | 249efd0 | 2013-08-05 03:33:56 -0700 | [diff] [blame] | 100 | CallResult result = mCallModeler.getCallWithId(callId); |
Santos Cordon | b01deb5 | 2013-08-06 23:46:06 -0700 | [diff] [blame] | 101 | if (DBG) Log.d(TAG, "disconnectCall " + result.getCall()); |
| 102 | |
Santos Cordon | 249efd0 | 2013-08-05 03:33:56 -0700 | [diff] [blame] | 103 | if (result != null) { |
| 104 | int state = result.getCall().getState(); |
Santos Cordon | 4ad64cd | 2013-08-15 00:36:14 -0700 | [diff] [blame] | 105 | if (Call.State.ACTIVE == state || |
| 106 | Call.State.ONHOLD == state || |
Christine Chen | 4527702 | 2013-09-05 10:55:37 -0700 | [diff] [blame] | 107 | Call.State.DIALING == state) { |
Santos Cordon | 249efd0 | 2013-08-05 03:33:56 -0700 | [diff] [blame] | 108 | result.getConnection().getCall().hangup(); |
Christine Chen | 4527702 | 2013-09-05 10:55:37 -0700 | [diff] [blame] | 109 | } else if (Call.State.CONFERENCED == state) { |
| 110 | result.getConnection().hangup(); |
Santos Cordon | 249efd0 | 2013-08-05 03:33:56 -0700 | [diff] [blame] | 111 | } |
| 112 | } |
Chiao Cheng | e41661c | 2013-07-23 13:28:26 -0700 | [diff] [blame] | 113 | } catch (Exception e) { |
| 114 | Log.e(TAG, "Error during disconnectCall().", e); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | @Override |
Christine Chen | 4527702 | 2013-09-05 10:55:37 -0700 | [diff] [blame] | 119 | public void separateCall(int callId) { |
| 120 | try { |
| 121 | CallResult result = mCallModeler.getCallWithId(callId); |
| 122 | if (DBG) Log.d(TAG, "disconnectCall " + result.getCall()); |
| 123 | |
| 124 | if (result != null) { |
| 125 | int state = result.getCall().getState(); |
| 126 | if (Call.State.CONFERENCED == state) { |
| 127 | result.getConnection().separate(); |
| 128 | } |
| 129 | } |
| 130 | } catch (Exception e) { |
| 131 | Log.e(TAG, "Error trying to separate call.", e); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | @Override |
Santos Cordon | 2b65bf0 | 2013-07-29 14:09:44 -0700 | [diff] [blame] | 136 | public void hold(int callId, boolean hold) { |
| 137 | try { |
Santos Cordon | 249efd0 | 2013-08-05 03:33:56 -0700 | [diff] [blame] | 138 | CallResult result = mCallModeler.getCallWithId(callId); |
| 139 | if (result != null) { |
| 140 | int state = result.getCall().getState(); |
Santos Cordon | 2eaff90 | 2013-08-05 04:37:55 -0700 | [diff] [blame] | 141 | if (hold && Call.State.ACTIVE == state) { |
Santos Cordon | 249efd0 | 2013-08-05 03:33:56 -0700 | [diff] [blame] | 142 | PhoneUtils.switchHoldingAndActive(mCallManager.getFirstActiveBgCall()); |
| 143 | } else if (!hold && Call.State.ONHOLD == state) { |
| 144 | PhoneUtils.switchHoldingAndActive(result.getConnection().getCall()); |
| 145 | } |
| 146 | } |
Santos Cordon | 2b65bf0 | 2013-07-29 14:09:44 -0700 | [diff] [blame] | 147 | } catch (Exception e) { |
| 148 | Log.e(TAG, "Error trying to place call on hold.", e); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | @Override |
Santos Cordon | eead6ec | 2013-08-07 22:16:33 -0700 | [diff] [blame] | 153 | public void merge() { |
| 154 | if (PhoneUtils.okToMergeCalls(mCallManager)) { |
| 155 | PhoneUtils.mergeCalls(mCallManager); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | @Override |
| 160 | public void addCall() { |
| 161 | // start new call checks okToAddCall() already |
| 162 | PhoneUtils.startNewCall(mCallManager); |
| 163 | } |
| 164 | |
| 165 | |
| 166 | @Override |
| 167 | public void swap() { |
| 168 | if (!PhoneUtils.okToSwapCalls(mCallManager)) { |
| 169 | // TODO: throw an error instead? |
| 170 | return; |
| 171 | } |
| 172 | |
| 173 | // Swap the fg and bg calls. |
| 174 | // In the future we may provides some way for user to choose among |
| 175 | // multiple background calls, for now, always act on the first background calll. |
| 176 | PhoneUtils.switchHoldingAndActive(mCallManager.getFirstActiveBgCall()); |
| 177 | |
| 178 | final PhoneGlobals mApp = PhoneGlobals.getInstance(); |
| 179 | |
| 180 | // If we have a valid BluetoothPhoneService then since CDMA network or |
| 181 | // Telephony FW does not send us information on which caller got swapped |
| 182 | // we need to update the second call active state in BluetoothPhoneService internally |
| 183 | if (mCallManager.getBgPhone().getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) { |
| 184 | final IBluetoothHeadsetPhone btPhone = mApp.getBluetoothPhoneService(); |
| 185 | if (btPhone != null) { |
| 186 | try { |
| 187 | btPhone.cdmaSwapSecondCallState(); |
| 188 | } catch (RemoteException e) { |
| 189 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
| 190 | } |
| 191 | } |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | @Override |
Chiao Cheng | e41661c | 2013-07-23 13:28:26 -0700 | [diff] [blame] | 196 | public void mute(boolean onOff) { |
| 197 | try { |
Santos Cordon | eead6ec | 2013-08-07 22:16:33 -0700 | [diff] [blame] | 198 | PhoneUtils.setMute(onOff); |
Chiao Cheng | e41661c | 2013-07-23 13:28:26 -0700 | [diff] [blame] | 199 | } catch (Exception e) { |
| 200 | Log.e(TAG, "Error during mute().", e); |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | @Override |
| 205 | public void speaker(boolean onOff) { |
| 206 | try { |
| 207 | // TODO(klp): add bluetooth logic from InCallScreen.toggleSpeaker() |
| 208 | PhoneUtils.turnOnSpeaker(mContext, onOff, true); |
| 209 | } catch (Exception e) { |
| 210 | Log.e(TAG, "Error during speaker().", e); |
| 211 | } |
Santos Cordon | cba1b44 | 2013-07-18 12:43:58 -0700 | [diff] [blame] | 212 | } |
Santos Cordon | 2eaff90 | 2013-08-05 04:37:55 -0700 | [diff] [blame] | 213 | |
| 214 | @Override |
Christine Chen | daf7bf6 | 2013-08-05 19:12:31 -0700 | [diff] [blame] | 215 | public void playDtmfTone(char digit, boolean timedShortTone) { |
Santos Cordon | 2eaff90 | 2013-08-05 04:37:55 -0700 | [diff] [blame] | 216 | try { |
Christine Chen | daf7bf6 | 2013-08-05 19:12:31 -0700 | [diff] [blame] | 217 | mDtmfTonePlayer.playDtmfTone(digit, timedShortTone); |
Santos Cordon | 2eaff90 | 2013-08-05 04:37:55 -0700 | [diff] [blame] | 218 | } catch (Exception e) { |
| 219 | Log.e(TAG, "Error playing DTMF tone.", e); |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | @Override |
| 224 | public void stopDtmfTone() { |
| 225 | try { |
| 226 | mDtmfTonePlayer.stopDtmfTone(); |
| 227 | } catch (Exception e) { |
| 228 | Log.e(TAG, "Error stopping DTMF tone.", e); |
| 229 | } |
| 230 | } |
Santos Cordon | 9b7bac7 | 2013-08-06 08:04:52 -0700 | [diff] [blame] | 231 | |
| 232 | @Override |
| 233 | public void setAudioMode(int mode) { |
| 234 | try { |
| 235 | mAudioRouter.setAudioMode(mode); |
| 236 | } catch (Exception e) { |
| 237 | Log.e(TAG, "Error setting the audio mode.", e); |
| 238 | } |
| 239 | } |
Santos Cordon | cba1b44 | 2013-07-18 12:43:58 -0700 | [diff] [blame] | 240 | } |