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