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) { |
Christine Chen | ee09a49 | 2013-08-06 16:02:29 -0700 | [diff] [blame] | 82 | if (rejectWithMessage) { |
| 83 | if (message != null) { |
| 84 | mRejectWithTextMessageManager.rejectCallWithMessage( |
| 85 | result.getConnection().getCall(), message); |
| 86 | } else { |
| 87 | mRejectWithTextMessageManager.rejectCallWithNewMessage( |
| 88 | result.getConnection().getCall()); |
| 89 | } |
| 90 | } |
| 91 | Log.v(TAG, "Hanging up"); |
Santos Cordon | 249efd0 | 2013-08-05 03:33:56 -0700 | [diff] [blame] | 92 | PhoneUtils.hangupRingingCall(result.getConnection().getCall()); |
| 93 | } |
Chiao Cheng | e41661c | 2013-07-23 13:28:26 -0700 | [diff] [blame] | 94 | } catch (Exception e) { |
| 95 | Log.e(TAG, "Error during rejectCall().", e); |
| 96 | } |
Santos Cordon | cba1b44 | 2013-07-18 12:43:58 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | @Override |
| 100 | public void disconnectCall(int callId) { |
Chiao Cheng | e41661c | 2013-07-23 13:28:26 -0700 | [diff] [blame] | 101 | try { |
Santos Cordon | 249efd0 | 2013-08-05 03:33:56 -0700 | [diff] [blame] | 102 | CallResult result = mCallModeler.getCallWithId(callId); |
Santos Cordon | b01deb5 | 2013-08-06 23:46:06 -0700 | [diff] [blame] | 103 | if (DBG) Log.d(TAG, "disconnectCall " + result.getCall()); |
| 104 | |
Santos Cordon | 249efd0 | 2013-08-05 03:33:56 -0700 | [diff] [blame] | 105 | if (result != null) { |
| 106 | int state = result.getCall().getState(); |
Santos Cordon | b01deb5 | 2013-08-06 23:46:06 -0700 | [diff] [blame] | 107 | if (Call.State.ACTIVE == state || Call.State.ONHOLD == state || |
| 108 | Call.State.DIALING == state) { |
Santos Cordon | 249efd0 | 2013-08-05 03:33:56 -0700 | [diff] [blame] | 109 | result.getConnection().getCall().hangup(); |
| 110 | } |
| 111 | } |
Chiao Cheng | e41661c | 2013-07-23 13:28:26 -0700 | [diff] [blame] | 112 | } catch (Exception e) { |
| 113 | Log.e(TAG, "Error during disconnectCall().", e); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | @Override |
Santos Cordon | 2b65bf0 | 2013-07-29 14:09:44 -0700 | [diff] [blame] | 118 | public void hold(int callId, boolean hold) { |
| 119 | try { |
Santos Cordon | 249efd0 | 2013-08-05 03:33:56 -0700 | [diff] [blame] | 120 | CallResult result = mCallModeler.getCallWithId(callId); |
| 121 | if (result != null) { |
| 122 | int state = result.getCall().getState(); |
Santos Cordon | 2eaff90 | 2013-08-05 04:37:55 -0700 | [diff] [blame] | 123 | if (hold && Call.State.ACTIVE == state) { |
Santos Cordon | 249efd0 | 2013-08-05 03:33:56 -0700 | [diff] [blame] | 124 | PhoneUtils.switchHoldingAndActive(mCallManager.getFirstActiveBgCall()); |
| 125 | } else if (!hold && Call.State.ONHOLD == state) { |
| 126 | PhoneUtils.switchHoldingAndActive(result.getConnection().getCall()); |
| 127 | } |
| 128 | } |
Santos Cordon | 2b65bf0 | 2013-07-29 14:09:44 -0700 | [diff] [blame] | 129 | } catch (Exception e) { |
| 130 | Log.e(TAG, "Error trying to place call on hold.", e); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | @Override |
Santos Cordon | eead6ec | 2013-08-07 22:16:33 -0700 | [diff] [blame^] | 135 | public void merge() { |
| 136 | if (PhoneUtils.okToMergeCalls(mCallManager)) { |
| 137 | PhoneUtils.mergeCalls(mCallManager); |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | @Override |
| 142 | public void addCall() { |
| 143 | // start new call checks okToAddCall() already |
| 144 | PhoneUtils.startNewCall(mCallManager); |
| 145 | } |
| 146 | |
| 147 | |
| 148 | @Override |
| 149 | public void swap() { |
| 150 | if (!PhoneUtils.okToSwapCalls(mCallManager)) { |
| 151 | // TODO: throw an error instead? |
| 152 | return; |
| 153 | } |
| 154 | |
| 155 | // Swap the fg and bg calls. |
| 156 | // In the future we may provides some way for user to choose among |
| 157 | // multiple background calls, for now, always act on the first background calll. |
| 158 | PhoneUtils.switchHoldingAndActive(mCallManager.getFirstActiveBgCall()); |
| 159 | |
| 160 | final PhoneGlobals mApp = PhoneGlobals.getInstance(); |
| 161 | |
| 162 | // If we have a valid BluetoothPhoneService then since CDMA network or |
| 163 | // Telephony FW does not send us information on which caller got swapped |
| 164 | // we need to update the second call active state in BluetoothPhoneService internally |
| 165 | if (mCallManager.getBgPhone().getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) { |
| 166 | final IBluetoothHeadsetPhone btPhone = mApp.getBluetoothPhoneService(); |
| 167 | if (btPhone != null) { |
| 168 | try { |
| 169 | btPhone.cdmaSwapSecondCallState(); |
| 170 | } catch (RemoteException e) { |
| 171 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
| 172 | } |
| 173 | } |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | @Override |
Chiao Cheng | e41661c | 2013-07-23 13:28:26 -0700 | [diff] [blame] | 178 | public void mute(boolean onOff) { |
| 179 | try { |
Santos Cordon | eead6ec | 2013-08-07 22:16:33 -0700 | [diff] [blame^] | 180 | PhoneUtils.setMute(onOff); |
Chiao Cheng | e41661c | 2013-07-23 13:28:26 -0700 | [diff] [blame] | 181 | } catch (Exception e) { |
| 182 | Log.e(TAG, "Error during mute().", e); |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | @Override |
| 187 | public void speaker(boolean onOff) { |
| 188 | try { |
| 189 | // TODO(klp): add bluetooth logic from InCallScreen.toggleSpeaker() |
| 190 | PhoneUtils.turnOnSpeaker(mContext, onOff, true); |
| 191 | } catch (Exception e) { |
| 192 | Log.e(TAG, "Error during speaker().", e); |
| 193 | } |
Santos Cordon | cba1b44 | 2013-07-18 12:43:58 -0700 | [diff] [blame] | 194 | } |
Santos Cordon | 2eaff90 | 2013-08-05 04:37:55 -0700 | [diff] [blame] | 195 | |
| 196 | @Override |
Christine Chen | daf7bf6 | 2013-08-05 19:12:31 -0700 | [diff] [blame] | 197 | public void playDtmfTone(char digit, boolean timedShortTone) { |
Santos Cordon | 2eaff90 | 2013-08-05 04:37:55 -0700 | [diff] [blame] | 198 | try { |
Christine Chen | daf7bf6 | 2013-08-05 19:12:31 -0700 | [diff] [blame] | 199 | mDtmfTonePlayer.playDtmfTone(digit, timedShortTone); |
Santos Cordon | 2eaff90 | 2013-08-05 04:37:55 -0700 | [diff] [blame] | 200 | } catch (Exception e) { |
| 201 | Log.e(TAG, "Error playing DTMF tone.", e); |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | @Override |
| 206 | public void stopDtmfTone() { |
| 207 | try { |
| 208 | mDtmfTonePlayer.stopDtmfTone(); |
| 209 | } catch (Exception e) { |
| 210 | Log.e(TAG, "Error stopping DTMF tone.", e); |
| 211 | } |
| 212 | } |
Santos Cordon | 9b7bac7 | 2013-08-06 08:04:52 -0700 | [diff] [blame] | 213 | |
| 214 | @Override |
| 215 | public void setAudioMode(int mode) { |
| 216 | try { |
| 217 | mAudioRouter.setAudioMode(mode); |
| 218 | } catch (Exception e) { |
| 219 | Log.e(TAG, "Error setting the audio mode.", e); |
| 220 | } |
| 221 | } |
Santos Cordon | cba1b44 | 2013-07-18 12:43:58 -0700 | [diff] [blame] | 222 | } |