blob: 04c26d02e738d3349704417eda00c5d7b177ab56 [file] [log] [blame]
Chiao Chenge41661c2013-07-23 13:28:26 -07001/*
Santos Cordoncba1b442013-07-18 12:43:58 -07002 * 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 Chenge41661c2013-07-23 13:28:26 -070014 * limitations under the License
Santos Cordoncba1b442013-07-18 12:43:58 -070015 */
16
17package com.android.phone;
18
Santos Cordoneead6ec2013-08-07 22:16:33 -070019import android.bluetooth.IBluetoothHeadsetPhone;
Chiao Chenge41661c2013-07-23 13:28:26 -070020import android.content.Context;
Santos Cordoneead6ec2013-08-07 22:16:33 -070021import android.os.RemoteException;
Santos Cordonb01deb52013-08-06 23:46:06 -070022import android.os.SystemProperties;
Chiao Chenge41661c2013-07-23 13:28:26 -070023import android.util.Log;
24
Santos Cordoncba1b442013-07-18 12:43:58 -070025import com.android.internal.telephony.CallManager;
Santos Cordoneead6ec2013-08-07 22:16:33 -070026import com.android.internal.telephony.PhoneConstants;
Santos Cordon249efd02013-08-05 03:33:56 -070027import com.android.phone.CallModeler.CallResult;
Santos Cordon9b7bac72013-08-06 08:04:52 -070028import com.android.services.telephony.common.AudioMode;
Santos Cordon249efd02013-08-05 03:33:56 -070029import com.android.services.telephony.common.Call;
Santos Cordoncba1b442013-07-18 12:43:58 -070030import com.android.services.telephony.common.ICallCommandService;
31
32/**
Chiao Chenge41661c2013-07-23 13:28:26 -070033 * 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 Cordoncba1b442013-07-18 12:43:58 -070035 */
36class CallCommandService extends ICallCommandService.Stub {
Chiao Chenge41661c2013-07-23 13:28:26 -070037 private static final String TAG = CallCommandService.class.getSimpleName();
Santos Cordonb01deb52013-08-06 23:46:06 -070038 private static final boolean DBG =
39 (PhoneGlobals.DBG_LEVEL >= 1) && (SystemProperties.getInt("ro.debuggable", 0) == 1);
Chiao Chenge41661c2013-07-23 13:28:26 -070040
Santos Cordon249efd02013-08-05 03:33:56 -070041 private final Context mContext;
42 private final CallManager mCallManager;
43 private final CallModeler mCallModeler;
Santos Cordon2eaff902013-08-05 04:37:55 -070044 private final DTMFTonePlayer mDtmfTonePlayer;
Santos Cordon9b7bac72013-08-06 08:04:52 -070045 private final AudioRouter mAudioRouter;
Christine Chenee09a492013-08-06 16:02:29 -070046 private final RejectWithTextMessageManager mRejectWithTextMessageManager;
Santos Cordoncba1b442013-07-18 12:43:58 -070047
Santos Cordon2eaff902013-08-05 04:37:55 -070048 public CallCommandService(Context context, CallManager callManager, CallModeler callModeler,
Christine Chenee09a492013-08-06 16:02:29 -070049 DTMFTonePlayer dtmfTonePlayer, AudioRouter audioRouter,
50 RejectWithTextMessageManager rejectWithTextMessageManager) {
Chiao Chenge41661c2013-07-23 13:28:26 -070051 mContext = context;
Santos Cordoncba1b442013-07-18 12:43:58 -070052 mCallManager = callManager;
Santos Cordon249efd02013-08-05 03:33:56 -070053 mCallModeler = callModeler;
Santos Cordon2eaff902013-08-05 04:37:55 -070054 mDtmfTonePlayer = dtmfTonePlayer;
Santos Cordon9b7bac72013-08-06 08:04:52 -070055 mAudioRouter = audioRouter;
Christine Chenee09a492013-08-06 16:02:29 -070056 mRejectWithTextMessageManager = rejectWithTextMessageManager;
Santos Cordoncba1b442013-07-18 12:43:58 -070057 }
58
59 /**
60 * TODO(klp): Add a confirmation callback parameter.
61 */
62 @Override
63 public void answerCall(int callId) {
Chiao Chenge41661c2013-07-23 13:28:26 -070064 try {
Santos Cordon249efd02013-08-05 03:33:56 -070065 CallResult result = mCallModeler.getCallWithId(callId);
66 if (result != null) {
67 PhoneUtils.answerCall(result.getConnection().getCall());
68 }
Chiao Chenge41661c2013-07-23 13:28:26 -070069 } catch (Exception e) {
70 Log.e(TAG, "Error during answerCall().", e);
71 }
Santos Cordoncba1b442013-07-18 12:43:58 -070072 }
73
74 /**
75 * TODO(klp): Add a confirmation callback parameter.
76 */
77 @Override
Christine Chenee09a492013-08-06 16:02:29 -070078 public void rejectCall(int callId, boolean rejectWithMessage, String message) {
Chiao Chenge41661c2013-07-23 13:28:26 -070079 try {
Santos Cordon249efd02013-08-05 03:33:56 -070080 CallResult result = mCallModeler.getCallWithId(callId);
81 if (result != null) {
Yorke Lee814da302013-08-30 16:01:07 -070082 final String number = result.getConnection().getAddress();
Christine Chen0ce0e852013-08-09 18:26:31 -070083
Christine Chenee09a492013-08-06 16:02:29 -070084 Log.v(TAG, "Hanging up");
Santos Cordon249efd02013-08-05 03:33:56 -070085 PhoneUtils.hangupRingingCall(result.getConnection().getCall());
Christine Chen0ce0e852013-08-09 18:26:31 -070086
Yorke Lee814da302013-08-30 16:01:07 -070087 if (rejectWithMessage) {
Christine Chen0ce0e852013-08-09 18:26:31 -070088 mRejectWithTextMessageManager.rejectCallWithMessage(
89 result.getConnection().getCall(), message);
Yorke Lee814da302013-08-30 16:01:07 -070090 }
Santos Cordon249efd02013-08-05 03:33:56 -070091 }
Chiao Chenge41661c2013-07-23 13:28:26 -070092 } catch (Exception e) {
93 Log.e(TAG, "Error during rejectCall().", e);
94 }
Santos Cordoncba1b442013-07-18 12:43:58 -070095 }
96
97 @Override
98 public void disconnectCall(int callId) {
Chiao Chenge41661c2013-07-23 13:28:26 -070099 try {
Santos Cordon249efd02013-08-05 03:33:56 -0700100 CallResult result = mCallModeler.getCallWithId(callId);
Santos Cordonb01deb52013-08-06 23:46:06 -0700101 if (DBG) Log.d(TAG, "disconnectCall " + result.getCall());
102
Santos Cordon249efd02013-08-05 03:33:56 -0700103 if (result != null) {
104 int state = result.getCall().getState();
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700105 if (Call.State.ACTIVE == state ||
106 Call.State.ONHOLD == state ||
Christine Chen45277022013-09-05 10:55:37 -0700107 Call.State.DIALING == state) {
Santos Cordon249efd02013-08-05 03:33:56 -0700108 result.getConnection().getCall().hangup();
Christine Chen45277022013-09-05 10:55:37 -0700109 } else if (Call.State.CONFERENCED == state) {
110 result.getConnection().hangup();
Santos Cordon249efd02013-08-05 03:33:56 -0700111 }
112 }
Chiao Chenge41661c2013-07-23 13:28:26 -0700113 } catch (Exception e) {
114 Log.e(TAG, "Error during disconnectCall().", e);
115 }
116 }
117
118 @Override
Christine Chen45277022013-09-05 10:55:37 -0700119 public void separateCall(int callId) {
120 try {
121 CallResult result = mCallModeler.getCallWithId(callId);
122 if (DBG) Log.d(TAG, "disconnectCall " + result.getCall());
123
124 if (result != null) {
125 int state = result.getCall().getState();
126 if (Call.State.CONFERENCED == state) {
127 result.getConnection().separate();
128 }
129 }
130 } catch (Exception e) {
131 Log.e(TAG, "Error trying to separate call.", e);
132 }
133 }
134
135 @Override
Santos Cordon2b65bf02013-07-29 14:09:44 -0700136 public void hold(int callId, boolean hold) {
137 try {
Santos Cordon249efd02013-08-05 03:33:56 -0700138 CallResult result = mCallModeler.getCallWithId(callId);
139 if (result != null) {
140 int state = result.getCall().getState();
Santos Cordon2eaff902013-08-05 04:37:55 -0700141 if (hold && Call.State.ACTIVE == state) {
Santos Cordon249efd02013-08-05 03:33:56 -0700142 PhoneUtils.switchHoldingAndActive(mCallManager.getFirstActiveBgCall());
143 } else if (!hold && Call.State.ONHOLD == state) {
144 PhoneUtils.switchHoldingAndActive(result.getConnection().getCall());
145 }
146 }
Santos Cordon2b65bf02013-07-29 14:09:44 -0700147 } catch (Exception e) {
148 Log.e(TAG, "Error trying to place call on hold.", e);
149 }
150 }
151
152 @Override
Santos Cordoneead6ec2013-08-07 22:16:33 -0700153 public void merge() {
154 if (PhoneUtils.okToMergeCalls(mCallManager)) {
155 PhoneUtils.mergeCalls(mCallManager);
156 }
157 }
158
159 @Override
160 public void addCall() {
161 // start new call checks okToAddCall() already
162 PhoneUtils.startNewCall(mCallManager);
163 }
164
165
166 @Override
167 public void swap() {
168 if (!PhoneUtils.okToSwapCalls(mCallManager)) {
169 // TODO: throw an error instead?
170 return;
171 }
172
173 // Swap the fg and bg calls.
174 // In the future we may provides some way for user to choose among
175 // multiple background calls, for now, always act on the first background calll.
176 PhoneUtils.switchHoldingAndActive(mCallManager.getFirstActiveBgCall());
177
178 final PhoneGlobals mApp = PhoneGlobals.getInstance();
179
180 // If we have a valid BluetoothPhoneService then since CDMA network or
181 // Telephony FW does not send us information on which caller got swapped
182 // we need to update the second call active state in BluetoothPhoneService internally
183 if (mCallManager.getBgPhone().getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
184 final IBluetoothHeadsetPhone btPhone = mApp.getBluetoothPhoneService();
185 if (btPhone != null) {
186 try {
187 btPhone.cdmaSwapSecondCallState();
188 } catch (RemoteException e) {
189 Log.e(TAG, Log.getStackTraceString(new Throwable()));
190 }
191 }
192 }
193 }
194
195 @Override
Chiao Chenge41661c2013-07-23 13:28:26 -0700196 public void mute(boolean onOff) {
197 try {
Santos Cordoneead6ec2013-08-07 22:16:33 -0700198 PhoneUtils.setMute(onOff);
Chiao Chenge41661c2013-07-23 13:28:26 -0700199 } catch (Exception e) {
200 Log.e(TAG, "Error during mute().", e);
201 }
202 }
203
204 @Override
205 public void speaker(boolean onOff) {
206 try {
207 // TODO(klp): add bluetooth logic from InCallScreen.toggleSpeaker()
208 PhoneUtils.turnOnSpeaker(mContext, onOff, true);
209 } catch (Exception e) {
210 Log.e(TAG, "Error during speaker().", e);
211 }
Santos Cordoncba1b442013-07-18 12:43:58 -0700212 }
Santos Cordon2eaff902013-08-05 04:37:55 -0700213
214 @Override
Christine Chendaf7bf62013-08-05 19:12:31 -0700215 public void playDtmfTone(char digit, boolean timedShortTone) {
Santos Cordon2eaff902013-08-05 04:37:55 -0700216 try {
Christine Chendaf7bf62013-08-05 19:12:31 -0700217 mDtmfTonePlayer.playDtmfTone(digit, timedShortTone);
Santos Cordon2eaff902013-08-05 04:37:55 -0700218 } 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 Cordon9b7bac72013-08-06 08:04:52 -0700231
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 }
Santos Cordoncba1b442013-07-18 12:43:58 -0700240}