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