blob: 523891179b9f7f65574af09ed13799b9e54b6769 [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;
Yorke Lee362cec22013-09-18 15:20:26 -070028import com.android.phone.NotificationMgr.StatusBarHelper;
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;
Santos Cordoncba1b442013-07-18 12:43:58 -070046
Santos Cordon2eaff902013-08-05 04:37:55 -070047 public CallCommandService(Context context, CallManager callManager, CallModeler callModeler,
David Braun35272072013-09-24 17:19:26 -070048 DTMFTonePlayer dtmfTonePlayer, AudioRouter audioRouter) {
Chiao Chenge41661c2013-07-23 13:28:26 -070049 mContext = context;
Santos Cordoncba1b442013-07-18 12:43:58 -070050 mCallManager = callManager;
Santos Cordon249efd02013-08-05 03:33:56 -070051 mCallModeler = callModeler;
Santos Cordon2eaff902013-08-05 04:37:55 -070052 mDtmfTonePlayer = dtmfTonePlayer;
Santos Cordon9b7bac72013-08-06 08:04:52 -070053 mAudioRouter = audioRouter;
Santos Cordoncba1b442013-07-18 12:43:58 -070054 }
55
56 /**
Christine Chen91db67d2013-09-18 12:01:11 -070057 * TODO: Add a confirmation callback parameter.
Santos Cordoncba1b442013-07-18 12:43:58 -070058 */
59 @Override
60 public void answerCall(int callId) {
Chiao Chenge41661c2013-07-23 13:28:26 -070061 try {
Santos Cordon249efd02013-08-05 03:33:56 -070062 CallResult result = mCallModeler.getCallWithId(callId);
63 if (result != null) {
64 PhoneUtils.answerCall(result.getConnection().getCall());
65 }
Chiao Chenge41661c2013-07-23 13:28:26 -070066 } catch (Exception e) {
67 Log.e(TAG, "Error during answerCall().", e);
68 }
Santos Cordoncba1b442013-07-18 12:43:58 -070069 }
70
71 /**
Christine Chen91db67d2013-09-18 12:01:11 -070072 * TODO: Add a confirmation callback parameter.
Santos Cordoncba1b442013-07-18 12:43:58 -070073 */
74 @Override
Christine Chen89ee4722013-10-07 16:10:36 -070075 public void rejectCall(Call call, boolean rejectWithMessage, String message) {
Chiao Chenge41661c2013-07-23 13:28:26 -070076 try {
Christine Chen89ee4722013-10-07 16:10:36 -070077 int callId = Call.INVALID_CALL_ID;
78 String phoneNumber = "";
79 if (call != null) {
80 callId = call.getCallId();
81 phoneNumber = call.getNumber();
82 }
Santos Cordon249efd02013-08-05 03:33:56 -070083 CallResult result = mCallModeler.getCallWithId(callId);
Christine Chen0ce0e852013-08-09 18:26:31 -070084
Christine Chen89ee4722013-10-07 16:10:36 -070085 if (result != null) {
86 phoneNumber = result.getConnection().getAddress();
David Braun35272072013-09-24 17:19:26 -070087
88 Log.v(TAG, "Hanging up");
89 PhoneUtils.hangupRingingCall(result.getConnection().getCall());
Santos Cordon249efd02013-08-05 03:33:56 -070090 }
Christine Chen89ee4722013-10-07 16:10:36 -070091
92 if (rejectWithMessage && !phoneNumber.isEmpty()) {
93 RejectWithTextMessageManager.rejectCallWithMessage(phoneNumber, message);
94 }
Chiao Chenge41661c2013-07-23 13:28:26 -070095 } catch (Exception e) {
96 Log.e(TAG, "Error during rejectCall().", e);
97 }
Santos Cordoncba1b442013-07-18 12:43:58 -070098 }
99
100 @Override
101 public void disconnectCall(int callId) {
Chiao Chenge41661c2013-07-23 13:28:26 -0700102 try {
Santos Cordon249efd02013-08-05 03:33:56 -0700103 CallResult result = mCallModeler.getCallWithId(callId);
Santos Cordonb01deb52013-08-06 23:46:06 -0700104 if (DBG) Log.d(TAG, "disconnectCall " + result.getCall());
105
Santos Cordon249efd02013-08-05 03:33:56 -0700106 if (result != null) {
107 int state = result.getCall().getState();
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700108 if (Call.State.ACTIVE == state ||
109 Call.State.ONHOLD == state ||
Christine Chen45277022013-09-05 10:55:37 -0700110 Call.State.DIALING == state) {
Santos Cordon249efd02013-08-05 03:33:56 -0700111 result.getConnection().getCall().hangup();
Christine Chen45277022013-09-05 10:55:37 -0700112 } else if (Call.State.CONFERENCED == state) {
113 result.getConnection().hangup();
Santos Cordon249efd02013-08-05 03:33:56 -0700114 }
115 }
Chiao Chenge41661c2013-07-23 13:28:26 -0700116 } catch (Exception e) {
117 Log.e(TAG, "Error during disconnectCall().", e);
118 }
119 }
120
121 @Override
Christine Chen45277022013-09-05 10:55:37 -0700122 public void separateCall(int callId) {
123 try {
124 CallResult result = mCallModeler.getCallWithId(callId);
125 if (DBG) Log.d(TAG, "disconnectCall " + result.getCall());
126
127 if (result != null) {
128 int state = result.getCall().getState();
129 if (Call.State.CONFERENCED == state) {
130 result.getConnection().separate();
131 }
132 }
133 } catch (Exception e) {
134 Log.e(TAG, "Error trying to separate call.", e);
135 }
136 }
137
138 @Override
Santos Cordon2b65bf02013-07-29 14:09:44 -0700139 public void hold(int callId, boolean hold) {
140 try {
Santos Cordon249efd02013-08-05 03:33:56 -0700141 CallResult result = mCallModeler.getCallWithId(callId);
142 if (result != null) {
143 int state = result.getCall().getState();
Santos Cordon2eaff902013-08-05 04:37:55 -0700144 if (hold && Call.State.ACTIVE == state) {
Santos Cordon249efd02013-08-05 03:33:56 -0700145 PhoneUtils.switchHoldingAndActive(mCallManager.getFirstActiveBgCall());
146 } else if (!hold && Call.State.ONHOLD == state) {
147 PhoneUtils.switchHoldingAndActive(result.getConnection().getCall());
148 }
149 }
Santos Cordon2b65bf02013-07-29 14:09:44 -0700150 } catch (Exception e) {
151 Log.e(TAG, "Error trying to place call on hold.", e);
152 }
153 }
154
155 @Override
Santos Cordoneead6ec2013-08-07 22:16:33 -0700156 public void merge() {
157 if (PhoneUtils.okToMergeCalls(mCallManager)) {
158 PhoneUtils.mergeCalls(mCallManager);
159 }
160 }
161
162 @Override
163 public void addCall() {
164 // start new call checks okToAddCall() already
165 PhoneUtils.startNewCall(mCallManager);
166 }
167
168
169 @Override
170 public void swap() {
171 if (!PhoneUtils.okToSwapCalls(mCallManager)) {
172 // TODO: throw an error instead?
173 return;
174 }
175
176 // Swap the fg and bg calls.
177 // In the future we may provides some way for user to choose among
178 // multiple background calls, for now, always act on the first background calll.
179 PhoneUtils.switchHoldingAndActive(mCallManager.getFirstActiveBgCall());
180
181 final PhoneGlobals mApp = PhoneGlobals.getInstance();
182
183 // If we have a valid BluetoothPhoneService then since CDMA network or
184 // Telephony FW does not send us information on which caller got swapped
185 // we need to update the second call active state in BluetoothPhoneService internally
186 if (mCallManager.getBgPhone().getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
187 final IBluetoothHeadsetPhone btPhone = mApp.getBluetoothPhoneService();
188 if (btPhone != null) {
189 try {
190 btPhone.cdmaSwapSecondCallState();
191 } catch (RemoteException e) {
192 Log.e(TAG, Log.getStackTraceString(new Throwable()));
193 }
194 }
195 }
196 }
197
198 @Override
Chiao Chenge41661c2013-07-23 13:28:26 -0700199 public void mute(boolean onOff) {
200 try {
Santos Cordoneead6ec2013-08-07 22:16:33 -0700201 PhoneUtils.setMute(onOff);
Chiao Chenge41661c2013-07-23 13:28:26 -0700202 } catch (Exception e) {
203 Log.e(TAG, "Error during mute().", e);
204 }
205 }
206
207 @Override
208 public void speaker(boolean onOff) {
209 try {
Chiao Chenge41661c2013-07-23 13:28:26 -0700210 PhoneUtils.turnOnSpeaker(mContext, onOff, true);
211 } catch (Exception e) {
212 Log.e(TAG, "Error during speaker().", e);
213 }
Santos Cordoncba1b442013-07-18 12:43:58 -0700214 }
Santos Cordon2eaff902013-08-05 04:37:55 -0700215
216 @Override
Christine Chendaf7bf62013-08-05 19:12:31 -0700217 public void playDtmfTone(char digit, boolean timedShortTone) {
Santos Cordon2eaff902013-08-05 04:37:55 -0700218 try {
Christine Chendaf7bf62013-08-05 19:12:31 -0700219 mDtmfTonePlayer.playDtmfTone(digit, timedShortTone);
Santos Cordon2eaff902013-08-05 04:37:55 -0700220 } catch (Exception e) {
221 Log.e(TAG, "Error playing DTMF tone.", e);
222 }
223 }
224
225 @Override
226 public void stopDtmfTone() {
227 try {
228 mDtmfTonePlayer.stopDtmfTone();
229 } catch (Exception e) {
230 Log.e(TAG, "Error stopping DTMF tone.", e);
231 }
232 }
Santos Cordon9b7bac72013-08-06 08:04:52 -0700233
234 @Override
235 public void setAudioMode(int mode) {
236 try {
237 mAudioRouter.setAudioMode(mode);
238 } catch (Exception e) {
239 Log.e(TAG, "Error setting the audio mode.", e);
240 }
241 }
Chiao Cheng3f015c92013-09-06 15:56:27 -0700242
243 @Override
244 public void postDialCancel(int callId) throws RemoteException {
245 final CallResult result = mCallModeler.getCallWithId(callId);
246 if (result != null) {
247 result.getConnection().cancelPostDial();
248 }
249 }
250
251 @Override
252 public void postDialWaitContinue(int callId) throws RemoteException {
253 final CallResult result = mCallModeler.getCallWithId(callId);
254 if (result != null) {
255 result.getConnection().proceedAfterWaitChar();
256 }
257 }
Yorke Lee362cec22013-09-18 15:20:26 -0700258
259 @Override
260 public void setSystemBarNavigationEnabled(boolean enable) {
261 try {
262 final StatusBarHelper statusBarHelper = PhoneGlobals.getInstance().notificationMgr.
263 statusBarHelper;
264 statusBarHelper.enableSystemBarNavigation(enable);
265 statusBarHelper.enableExpandedView(enable);
266 } catch (Exception e) {
267 Log.e(TAG, "Error enabling or disabling system bar navigation", e);
268 }
269 }
270
Santos Cordoncba1b442013-07-18 12:43:58 -0700271}