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