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