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; |
Yorke Lee | 362cec2 | 2013-09-18 15:20:26 -0700 | [diff] [blame] | 28 | import com.android.phone.NotificationMgr.StatusBarHelper; |
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; |
Santos Cordon | cba1b44 | 2013-07-18 12:43:58 -0700 | [diff] [blame] | 46 | |
Santos Cordon | 2eaff90 | 2013-08-05 04:37:55 -0700 | [diff] [blame] | 47 | public CallCommandService(Context context, CallManager callManager, CallModeler callModeler, |
David Braun | 3527207 | 2013-09-24 17:19:26 -0700 | [diff] [blame] | 48 | DTMFTonePlayer dtmfTonePlayer, AudioRouter audioRouter) { |
Chiao Cheng | e41661c | 2013-07-23 13:28:26 -0700 | [diff] [blame] | 49 | mContext = context; |
Santos Cordon | cba1b44 | 2013-07-18 12:43:58 -0700 | [diff] [blame] | 50 | mCallManager = callManager; |
Santos Cordon | 249efd0 | 2013-08-05 03:33:56 -0700 | [diff] [blame] | 51 | mCallModeler = callModeler; |
Santos Cordon | 2eaff90 | 2013-08-05 04:37:55 -0700 | [diff] [blame] | 52 | mDtmfTonePlayer = dtmfTonePlayer; |
Santos Cordon | 9b7bac7 | 2013-08-06 08:04:52 -0700 | [diff] [blame] | 53 | mAudioRouter = audioRouter; |
Santos Cordon | cba1b44 | 2013-07-18 12:43:58 -0700 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | /** |
Christine Chen | 91db67d | 2013-09-18 12:01:11 -0700 | [diff] [blame] | 57 | * TODO: Add a confirmation callback parameter. |
Santos Cordon | cba1b44 | 2013-07-18 12:43:58 -0700 | [diff] [blame] | 58 | */ |
| 59 | @Override |
| 60 | public void answerCall(int callId) { |
Chiao Cheng | e41661c | 2013-07-23 13:28:26 -0700 | [diff] [blame] | 61 | try { |
Santos Cordon | 249efd0 | 2013-08-05 03:33:56 -0700 | [diff] [blame] | 62 | CallResult result = mCallModeler.getCallWithId(callId); |
| 63 | if (result != null) { |
| 64 | PhoneUtils.answerCall(result.getConnection().getCall()); |
| 65 | } |
Chiao Cheng | e41661c | 2013-07-23 13:28:26 -0700 | [diff] [blame] | 66 | } catch (Exception e) { |
| 67 | Log.e(TAG, "Error during answerCall().", e); |
| 68 | } |
Santos Cordon | cba1b44 | 2013-07-18 12:43:58 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | /** |
Christine Chen | 91db67d | 2013-09-18 12:01:11 -0700 | [diff] [blame] | 72 | * TODO: Add a confirmation callback parameter. |
Santos Cordon | cba1b44 | 2013-07-18 12:43:58 -0700 | [diff] [blame] | 73 | */ |
| 74 | @Override |
Christine Chen | 89ee472 | 2013-10-07 16:10:36 -0700 | [diff] [blame] | 75 | public void rejectCall(Call call, boolean rejectWithMessage, String message) { |
Chiao Cheng | e41661c | 2013-07-23 13:28:26 -0700 | [diff] [blame] | 76 | try { |
Christine Chen | 89ee472 | 2013-10-07 16:10:36 -0700 | [diff] [blame] | 77 | int callId = Call.INVALID_CALL_ID; |
| 78 | String phoneNumber = ""; |
| 79 | if (call != null) { |
| 80 | callId = call.getCallId(); |
| 81 | phoneNumber = call.getNumber(); |
| 82 | } |
Santos Cordon | 249efd0 | 2013-08-05 03:33:56 -0700 | [diff] [blame] | 83 | CallResult result = mCallModeler.getCallWithId(callId); |
Christine Chen | 0ce0e85 | 2013-08-09 18:26:31 -0700 | [diff] [blame] | 84 | |
Christine Chen | 89ee472 | 2013-10-07 16:10:36 -0700 | [diff] [blame] | 85 | if (result != null) { |
| 86 | phoneNumber = result.getConnection().getAddress(); |
David Braun | 3527207 | 2013-09-24 17:19:26 -0700 | [diff] [blame] | 87 | |
| 88 | Log.v(TAG, "Hanging up"); |
| 89 | PhoneUtils.hangupRingingCall(result.getConnection().getCall()); |
Santos Cordon | 249efd0 | 2013-08-05 03:33:56 -0700 | [diff] [blame] | 90 | } |
Christine Chen | 89ee472 | 2013-10-07 16:10:36 -0700 | [diff] [blame] | 91 | |
| 92 | if (rejectWithMessage && !phoneNumber.isEmpty()) { |
| 93 | RejectWithTextMessageManager.rejectCallWithMessage(phoneNumber, message); |
| 94 | } |
Chiao Cheng | e41661c | 2013-07-23 13:28:26 -0700 | [diff] [blame] | 95 | } catch (Exception e) { |
| 96 | Log.e(TAG, "Error during rejectCall().", e); |
| 97 | } |
Santos Cordon | cba1b44 | 2013-07-18 12:43:58 -0700 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | @Override |
| 101 | public void disconnectCall(int callId) { |
Chiao Cheng | e41661c | 2013-07-23 13:28:26 -0700 | [diff] [blame] | 102 | try { |
Santos Cordon | 249efd0 | 2013-08-05 03:33:56 -0700 | [diff] [blame] | 103 | CallResult result = mCallModeler.getCallWithId(callId); |
Santos Cordon | b01deb5 | 2013-08-06 23:46:06 -0700 | [diff] [blame] | 104 | if (DBG) Log.d(TAG, "disconnectCall " + result.getCall()); |
| 105 | |
Santos Cordon | 249efd0 | 2013-08-05 03:33:56 -0700 | [diff] [blame] | 106 | if (result != null) { |
| 107 | int state = result.getCall().getState(); |
Santos Cordon | 4ad64cd | 2013-08-15 00:36:14 -0700 | [diff] [blame] | 108 | if (Call.State.ACTIVE == state || |
| 109 | Call.State.ONHOLD == state || |
Christine Chen | 4527702 | 2013-09-05 10:55:37 -0700 | [diff] [blame] | 110 | Call.State.DIALING == state) { |
Santos Cordon | 249efd0 | 2013-08-05 03:33:56 -0700 | [diff] [blame] | 111 | result.getConnection().getCall().hangup(); |
Christine Chen | 4527702 | 2013-09-05 10:55:37 -0700 | [diff] [blame] | 112 | } else if (Call.State.CONFERENCED == state) { |
| 113 | result.getConnection().hangup(); |
Santos Cordon | 249efd0 | 2013-08-05 03:33:56 -0700 | [diff] [blame] | 114 | } |
| 115 | } |
Chiao Cheng | e41661c | 2013-07-23 13:28:26 -0700 | [diff] [blame] | 116 | } catch (Exception e) { |
| 117 | Log.e(TAG, "Error during disconnectCall().", e); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | @Override |
Christine Chen | 4527702 | 2013-09-05 10:55:37 -0700 | [diff] [blame] | 122 | public void separateCall(int callId) { |
| 123 | try { |
| 124 | CallResult result = mCallModeler.getCallWithId(callId); |
| 125 | if (DBG) Log.d(TAG, "disconnectCall " + result.getCall()); |
| 126 | |
| 127 | if (result != null) { |
| 128 | int state = result.getCall().getState(); |
| 129 | if (Call.State.CONFERENCED == state) { |
| 130 | result.getConnection().separate(); |
| 131 | } |
| 132 | } |
| 133 | } catch (Exception e) { |
| 134 | Log.e(TAG, "Error trying to separate call.", e); |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | @Override |
Santos Cordon | 2b65bf0 | 2013-07-29 14:09:44 -0700 | [diff] [blame] | 139 | public void hold(int callId, boolean hold) { |
| 140 | try { |
Santos Cordon | 249efd0 | 2013-08-05 03:33:56 -0700 | [diff] [blame] | 141 | CallResult result = mCallModeler.getCallWithId(callId); |
| 142 | if (result != null) { |
| 143 | int state = result.getCall().getState(); |
Santos Cordon | 2eaff90 | 2013-08-05 04:37:55 -0700 | [diff] [blame] | 144 | if (hold && Call.State.ACTIVE == state) { |
Santos Cordon | 249efd0 | 2013-08-05 03:33:56 -0700 | [diff] [blame] | 145 | PhoneUtils.switchHoldingAndActive(mCallManager.getFirstActiveBgCall()); |
| 146 | } else if (!hold && Call.State.ONHOLD == state) { |
| 147 | PhoneUtils.switchHoldingAndActive(result.getConnection().getCall()); |
| 148 | } |
| 149 | } |
Santos Cordon | 2b65bf0 | 2013-07-29 14:09:44 -0700 | [diff] [blame] | 150 | } catch (Exception e) { |
| 151 | Log.e(TAG, "Error trying to place call on hold.", e); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | @Override |
Santos Cordon | eead6ec | 2013-08-07 22:16:33 -0700 | [diff] [blame] | 156 | public void merge() { |
| 157 | if (PhoneUtils.okToMergeCalls(mCallManager)) { |
| 158 | PhoneUtils.mergeCalls(mCallManager); |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | @Override |
| 163 | public void addCall() { |
| 164 | // start new call checks okToAddCall() already |
| 165 | PhoneUtils.startNewCall(mCallManager); |
| 166 | } |
| 167 | |
| 168 | |
| 169 | @Override |
| 170 | public void swap() { |
| 171 | if (!PhoneUtils.okToSwapCalls(mCallManager)) { |
| 172 | // TODO: throw an error instead? |
| 173 | return; |
| 174 | } |
| 175 | |
| 176 | // Swap the fg and bg calls. |
| 177 | // In the future we may provides some way for user to choose among |
| 178 | // multiple background calls, for now, always act on the first background calll. |
| 179 | PhoneUtils.switchHoldingAndActive(mCallManager.getFirstActiveBgCall()); |
| 180 | |
| 181 | final PhoneGlobals mApp = PhoneGlobals.getInstance(); |
| 182 | |
| 183 | // If we have a valid BluetoothPhoneService then since CDMA network or |
| 184 | // Telephony FW does not send us information on which caller got swapped |
| 185 | // we need to update the second call active state in BluetoothPhoneService internally |
| 186 | if (mCallManager.getBgPhone().getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) { |
| 187 | final IBluetoothHeadsetPhone btPhone = mApp.getBluetoothPhoneService(); |
| 188 | if (btPhone != null) { |
| 189 | try { |
| 190 | btPhone.cdmaSwapSecondCallState(); |
| 191 | } catch (RemoteException e) { |
| 192 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
| 193 | } |
| 194 | } |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | @Override |
Chiao Cheng | e41661c | 2013-07-23 13:28:26 -0700 | [diff] [blame] | 199 | public void mute(boolean onOff) { |
| 200 | try { |
Santos Cordon | eead6ec | 2013-08-07 22:16:33 -0700 | [diff] [blame] | 201 | PhoneUtils.setMute(onOff); |
Chiao Cheng | e41661c | 2013-07-23 13:28:26 -0700 | [diff] [blame] | 202 | } catch (Exception e) { |
| 203 | Log.e(TAG, "Error during mute().", e); |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | @Override |
| 208 | public void speaker(boolean onOff) { |
| 209 | try { |
Chiao Cheng | e41661c | 2013-07-23 13:28:26 -0700 | [diff] [blame] | 210 | PhoneUtils.turnOnSpeaker(mContext, onOff, true); |
| 211 | } catch (Exception e) { |
| 212 | Log.e(TAG, "Error during speaker().", e); |
| 213 | } |
Santos Cordon | cba1b44 | 2013-07-18 12:43:58 -0700 | [diff] [blame] | 214 | } |
Santos Cordon | 2eaff90 | 2013-08-05 04:37:55 -0700 | [diff] [blame] | 215 | |
| 216 | @Override |
Christine Chen | daf7bf6 | 2013-08-05 19:12:31 -0700 | [diff] [blame] | 217 | public void playDtmfTone(char digit, boolean timedShortTone) { |
Santos Cordon | 2eaff90 | 2013-08-05 04:37:55 -0700 | [diff] [blame] | 218 | try { |
Christine Chen | daf7bf6 | 2013-08-05 19:12:31 -0700 | [diff] [blame] | 219 | mDtmfTonePlayer.playDtmfTone(digit, timedShortTone); |
Santos Cordon | 2eaff90 | 2013-08-05 04:37:55 -0700 | [diff] [blame] | 220 | } catch (Exception e) { |
| 221 | Log.e(TAG, "Error playing DTMF tone.", e); |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | @Override |
| 226 | public void stopDtmfTone() { |
| 227 | try { |
| 228 | mDtmfTonePlayer.stopDtmfTone(); |
| 229 | } catch (Exception e) { |
| 230 | Log.e(TAG, "Error stopping DTMF tone.", e); |
| 231 | } |
| 232 | } |
Santos Cordon | 9b7bac7 | 2013-08-06 08:04:52 -0700 | [diff] [blame] | 233 | |
| 234 | @Override |
| 235 | public void setAudioMode(int mode) { |
| 236 | try { |
| 237 | mAudioRouter.setAudioMode(mode); |
| 238 | } catch (Exception e) { |
| 239 | Log.e(TAG, "Error setting the audio mode.", e); |
| 240 | } |
| 241 | } |
Chiao Cheng | 3f015c9 | 2013-09-06 15:56:27 -0700 | [diff] [blame] | 242 | |
| 243 | @Override |
| 244 | public void postDialCancel(int callId) throws RemoteException { |
| 245 | final CallResult result = mCallModeler.getCallWithId(callId); |
| 246 | if (result != null) { |
| 247 | result.getConnection().cancelPostDial(); |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | @Override |
| 252 | public void postDialWaitContinue(int callId) throws RemoteException { |
| 253 | final CallResult result = mCallModeler.getCallWithId(callId); |
| 254 | if (result != null) { |
| 255 | result.getConnection().proceedAfterWaitChar(); |
| 256 | } |
| 257 | } |
Yorke Lee | 362cec2 | 2013-09-18 15:20:26 -0700 | [diff] [blame] | 258 | |
| 259 | @Override |
| 260 | public void setSystemBarNavigationEnabled(boolean enable) { |
| 261 | try { |
| 262 | final StatusBarHelper statusBarHelper = PhoneGlobals.getInstance().notificationMgr. |
| 263 | statusBarHelper; |
| 264 | statusBarHelper.enableSystemBarNavigation(enable); |
| 265 | statusBarHelper.enableExpandedView(enable); |
| 266 | } catch (Exception e) { |
| 267 | Log.e(TAG, "Error enabling or disabling system bar navigation", e); |
| 268 | } |
| 269 | } |
| 270 | |
Santos Cordon | cba1b44 | 2013-07-18 12:43:58 -0700 | [diff] [blame] | 271 | } |