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 | |
Chiao Cheng | e41661c | 2013-07-23 13:28:26 -0700 | [diff] [blame] | 19 | import android.content.Context; |
| 20 | import android.util.Log; |
| 21 | |
Santos Cordon | cba1b44 | 2013-07-18 12:43:58 -0700 | [diff] [blame] | 22 | import com.android.internal.telephony.CallManager; |
Santos Cordon | 249efd0 | 2013-08-05 03:33:56 -0700 | [diff] [blame] | 23 | import com.android.phone.CallModeler.CallResult; |
Santos Cordon | 9b7bac7 | 2013-08-06 08:04:52 -0700 | [diff] [blame] | 24 | import com.android.services.telephony.common.AudioMode; |
Santos Cordon | 249efd0 | 2013-08-05 03:33:56 -0700 | [diff] [blame] | 25 | import com.android.services.telephony.common.Call; |
Santos Cordon | cba1b44 | 2013-07-18 12:43:58 -0700 | [diff] [blame] | 26 | import com.android.services.telephony.common.ICallCommandService; |
| 27 | |
| 28 | /** |
Chiao Cheng | e41661c | 2013-07-23 13:28:26 -0700 | [diff] [blame] | 29 | * Service interface used by in-call ui to control phone calls using commands exposed as methods. |
| 30 | * Instances of this class are handed to in-call UI via CallMonitorService. |
Santos Cordon | cba1b44 | 2013-07-18 12:43:58 -0700 | [diff] [blame] | 31 | */ |
| 32 | class CallCommandService extends ICallCommandService.Stub { |
| 33 | |
Chiao Cheng | e41661c | 2013-07-23 13:28:26 -0700 | [diff] [blame] | 34 | private static final String TAG = CallCommandService.class.getSimpleName(); |
| 35 | |
Santos Cordon | 249efd0 | 2013-08-05 03:33:56 -0700 | [diff] [blame] | 36 | private final Context mContext; |
| 37 | private final CallManager mCallManager; |
| 38 | private final CallModeler mCallModeler; |
Santos Cordon | 2eaff90 | 2013-08-05 04:37:55 -0700 | [diff] [blame] | 39 | private final DTMFTonePlayer mDtmfTonePlayer; |
Santos Cordon | 9b7bac7 | 2013-08-06 08:04:52 -0700 | [diff] [blame] | 40 | private final AudioRouter mAudioRouter; |
Christine Chen | ee09a49 | 2013-08-06 16:02:29 -0700 | [diff] [blame] | 41 | private final RejectWithTextMessageManager mRejectWithTextMessageManager; |
Santos Cordon | cba1b44 | 2013-07-18 12:43:58 -0700 | [diff] [blame] | 42 | |
Santos Cordon | 2eaff90 | 2013-08-05 04:37:55 -0700 | [diff] [blame] | 43 | public CallCommandService(Context context, CallManager callManager, CallModeler callModeler, |
Christine Chen | ee09a49 | 2013-08-06 16:02:29 -0700 | [diff] [blame] | 44 | DTMFTonePlayer dtmfTonePlayer, AudioRouter audioRouter, |
| 45 | RejectWithTextMessageManager rejectWithTextMessageManager) { |
Chiao Cheng | e41661c | 2013-07-23 13:28:26 -0700 | [diff] [blame] | 46 | mContext = context; |
Santos Cordon | cba1b44 | 2013-07-18 12:43:58 -0700 | [diff] [blame] | 47 | mCallManager = callManager; |
Santos Cordon | 249efd0 | 2013-08-05 03:33:56 -0700 | [diff] [blame] | 48 | mCallModeler = callModeler; |
Santos Cordon | 2eaff90 | 2013-08-05 04:37:55 -0700 | [diff] [blame] | 49 | mDtmfTonePlayer = dtmfTonePlayer; |
Santos Cordon | 9b7bac7 | 2013-08-06 08:04:52 -0700 | [diff] [blame] | 50 | mAudioRouter = audioRouter; |
Christine Chen | ee09a49 | 2013-08-06 16:02:29 -0700 | [diff] [blame] | 51 | mRejectWithTextMessageManager = rejectWithTextMessageManager; |
Santos Cordon | cba1b44 | 2013-07-18 12:43:58 -0700 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | /** |
| 55 | * TODO(klp): Add a confirmation callback parameter. |
| 56 | */ |
| 57 | @Override |
| 58 | public void answerCall(int callId) { |
Chiao Cheng | e41661c | 2013-07-23 13:28:26 -0700 | [diff] [blame] | 59 | try { |
Santos Cordon | 249efd0 | 2013-08-05 03:33:56 -0700 | [diff] [blame] | 60 | CallResult result = mCallModeler.getCallWithId(callId); |
| 61 | if (result != null) { |
| 62 | PhoneUtils.answerCall(result.getConnection().getCall()); |
| 63 | } |
Chiao Cheng | e41661c | 2013-07-23 13:28:26 -0700 | [diff] [blame] | 64 | } catch (Exception e) { |
| 65 | Log.e(TAG, "Error during answerCall().", e); |
| 66 | } |
Santos Cordon | cba1b44 | 2013-07-18 12:43:58 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | /** |
| 70 | * TODO(klp): Add a confirmation callback parameter. |
| 71 | */ |
| 72 | @Override |
Christine Chen | ee09a49 | 2013-08-06 16:02:29 -0700 | [diff] [blame] | 73 | public void rejectCall(int callId, boolean rejectWithMessage, String message) { |
Chiao Cheng | e41661c | 2013-07-23 13:28:26 -0700 | [diff] [blame] | 74 | try { |
Santos Cordon | 249efd0 | 2013-08-05 03:33:56 -0700 | [diff] [blame] | 75 | CallResult result = mCallModeler.getCallWithId(callId); |
| 76 | if (result != null) { |
Christine Chen | ee09a49 | 2013-08-06 16:02:29 -0700 | [diff] [blame] | 77 | if (rejectWithMessage) { |
| 78 | if (message != null) { |
| 79 | mRejectWithTextMessageManager.rejectCallWithMessage( |
| 80 | result.getConnection().getCall(), message); |
| 81 | } else { |
| 82 | mRejectWithTextMessageManager.rejectCallWithNewMessage( |
| 83 | result.getConnection().getCall()); |
| 84 | } |
| 85 | } |
| 86 | Log.v(TAG, "Hanging up"); |
Santos Cordon | 249efd0 | 2013-08-05 03:33:56 -0700 | [diff] [blame] | 87 | PhoneUtils.hangupRingingCall(result.getConnection().getCall()); |
| 88 | } |
Chiao Cheng | e41661c | 2013-07-23 13:28:26 -0700 | [diff] [blame] | 89 | } catch (Exception e) { |
| 90 | Log.e(TAG, "Error during rejectCall().", e); |
| 91 | } |
Santos Cordon | cba1b44 | 2013-07-18 12:43:58 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | @Override |
| 95 | public void disconnectCall(int callId) { |
Chiao Cheng | e41661c | 2013-07-23 13:28:26 -0700 | [diff] [blame] | 96 | try { |
Santos Cordon | 249efd0 | 2013-08-05 03:33:56 -0700 | [diff] [blame] | 97 | CallResult result = mCallModeler.getCallWithId(callId); |
| 98 | if (result != null) { |
| 99 | int state = result.getCall().getState(); |
| 100 | if (Call.State.ACTIVE == state || Call.State.ONHOLD == state) { |
| 101 | result.getConnection().getCall().hangup(); |
| 102 | } |
| 103 | } |
Chiao Cheng | e41661c | 2013-07-23 13:28:26 -0700 | [diff] [blame] | 104 | } catch (Exception e) { |
| 105 | Log.e(TAG, "Error during disconnectCall().", e); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | @Override |
Santos Cordon | 2b65bf0 | 2013-07-29 14:09:44 -0700 | [diff] [blame] | 110 | public void hold(int callId, boolean hold) { |
| 111 | try { |
Santos Cordon | 249efd0 | 2013-08-05 03:33:56 -0700 | [diff] [blame] | 112 | CallResult result = mCallModeler.getCallWithId(callId); |
| 113 | if (result != null) { |
| 114 | int state = result.getCall().getState(); |
Santos Cordon | 2eaff90 | 2013-08-05 04:37:55 -0700 | [diff] [blame] | 115 | if (hold && Call.State.ACTIVE == state) { |
Santos Cordon | 249efd0 | 2013-08-05 03:33:56 -0700 | [diff] [blame] | 116 | PhoneUtils.switchHoldingAndActive(mCallManager.getFirstActiveBgCall()); |
| 117 | } else if (!hold && Call.State.ONHOLD == state) { |
| 118 | PhoneUtils.switchHoldingAndActive(result.getConnection().getCall()); |
| 119 | } |
| 120 | } |
Santos Cordon | 2b65bf0 | 2013-07-29 14:09:44 -0700 | [diff] [blame] | 121 | } catch (Exception e) { |
| 122 | Log.e(TAG, "Error trying to place call on hold.", e); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | @Override |
Chiao Cheng | e41661c | 2013-07-23 13:28:26 -0700 | [diff] [blame] | 127 | public void mute(boolean onOff) { |
| 128 | try { |
Santos Cordon | 9b7bac7 | 2013-08-06 08:04:52 -0700 | [diff] [blame] | 129 | //PhoneUtils.setMute(onOff); |
| 130 | mAudioRouter.setAudioMode(onOff ? AudioMode.BLUETOOTH : AudioMode.EARPIECE); |
Chiao Cheng | e41661c | 2013-07-23 13:28:26 -0700 | [diff] [blame] | 131 | } catch (Exception e) { |
| 132 | Log.e(TAG, "Error during mute().", e); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | @Override |
| 137 | public void speaker(boolean onOff) { |
| 138 | try { |
| 139 | // TODO(klp): add bluetooth logic from InCallScreen.toggleSpeaker() |
| 140 | PhoneUtils.turnOnSpeaker(mContext, onOff, true); |
| 141 | } catch (Exception e) { |
| 142 | Log.e(TAG, "Error during speaker().", e); |
| 143 | } |
Santos Cordon | cba1b44 | 2013-07-18 12:43:58 -0700 | [diff] [blame] | 144 | } |
Santos Cordon | 2eaff90 | 2013-08-05 04:37:55 -0700 | [diff] [blame] | 145 | |
| 146 | @Override |
Christine Chen | daf7bf6 | 2013-08-05 19:12:31 -0700 | [diff] [blame] | 147 | public void playDtmfTone(char digit, boolean timedShortTone) { |
Santos Cordon | 2eaff90 | 2013-08-05 04:37:55 -0700 | [diff] [blame] | 148 | try { |
Christine Chen | daf7bf6 | 2013-08-05 19:12:31 -0700 | [diff] [blame] | 149 | mDtmfTonePlayer.playDtmfTone(digit, timedShortTone); |
Santos Cordon | 2eaff90 | 2013-08-05 04:37:55 -0700 | [diff] [blame] | 150 | } catch (Exception e) { |
| 151 | Log.e(TAG, "Error playing DTMF tone.", e); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | @Override |
| 156 | public void stopDtmfTone() { |
| 157 | try { |
| 158 | mDtmfTonePlayer.stopDtmfTone(); |
| 159 | } catch (Exception e) { |
| 160 | Log.e(TAG, "Error stopping DTMF tone.", e); |
| 161 | } |
| 162 | } |
Santos Cordon | 9b7bac7 | 2013-08-06 08:04:52 -0700 | [diff] [blame] | 163 | |
| 164 | @Override |
| 165 | public void setAudioMode(int mode) { |
| 166 | try { |
| 167 | mAudioRouter.setAudioMode(mode); |
| 168 | } catch (Exception e) { |
| 169 | Log.e(TAG, "Error setting the audio mode.", e); |
| 170 | } |
| 171 | } |
Santos Cordon | cba1b44 | 2013-07-18 12:43:58 -0700 | [diff] [blame] | 172 | } |