blob: 60c5802deb78ace92be9d2578ac89af5aa323510 [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 Cordon9b7bac72013-08-06 08:04:52 -070029import com.android.services.telephony.common.AudioMode;
Santos Cordon249efd02013-08-05 03:33:56 -070030import com.android.services.telephony.common.Call;
Santos Cordoncba1b442013-07-18 12:43:58 -070031import com.android.services.telephony.common.ICallCommandService;
32
33/**
Chiao Chenge41661c2013-07-23 13:28:26 -070034 * Service interface used by in-call ui to control phone calls using commands exposed as methods.
35 * Instances of this class are handed to in-call UI via CallMonitorService.
Santos Cordoncba1b442013-07-18 12:43:58 -070036 */
37class CallCommandService extends ICallCommandService.Stub {
Chiao Chenge41661c2013-07-23 13:28:26 -070038 private static final String TAG = CallCommandService.class.getSimpleName();
Santos Cordonb01deb52013-08-06 23:46:06 -070039 private static final boolean DBG =
40 (PhoneGlobals.DBG_LEVEL >= 1) && (SystemProperties.getInt("ro.debuggable", 0) == 1);
Chiao Chenge41661c2013-07-23 13:28:26 -070041
Santos Cordon249efd02013-08-05 03:33:56 -070042 private final Context mContext;
43 private final CallManager mCallManager;
44 private final CallModeler mCallModeler;
Santos Cordon2eaff902013-08-05 04:37:55 -070045 private final DTMFTonePlayer mDtmfTonePlayer;
Santos Cordon9b7bac72013-08-06 08:04:52 -070046 private final AudioRouter mAudioRouter;
Christine Chenee09a492013-08-06 16:02:29 -070047 private final RejectWithTextMessageManager mRejectWithTextMessageManager;
Santos Cordoncba1b442013-07-18 12:43:58 -070048
Santos Cordon2eaff902013-08-05 04:37:55 -070049 public CallCommandService(Context context, CallManager callManager, CallModeler callModeler,
Christine Chenee09a492013-08-06 16:02:29 -070050 DTMFTonePlayer dtmfTonePlayer, AudioRouter audioRouter,
51 RejectWithTextMessageManager rejectWithTextMessageManager) {
Chiao Chenge41661c2013-07-23 13:28:26 -070052 mContext = context;
Santos Cordoncba1b442013-07-18 12:43:58 -070053 mCallManager = callManager;
Santos Cordon249efd02013-08-05 03:33:56 -070054 mCallModeler = callModeler;
Santos Cordon2eaff902013-08-05 04:37:55 -070055 mDtmfTonePlayer = dtmfTonePlayer;
Santos Cordon9b7bac72013-08-06 08:04:52 -070056 mAudioRouter = audioRouter;
Christine Chenee09a492013-08-06 16:02:29 -070057 mRejectWithTextMessageManager = rejectWithTextMessageManager;
Santos Cordoncba1b442013-07-18 12:43:58 -070058 }
59
60 /**
61 * TODO(klp): Add a confirmation callback parameter.
62 */
63 @Override
64 public void answerCall(int callId) {
Chiao Chenge41661c2013-07-23 13:28:26 -070065 try {
Santos Cordon249efd02013-08-05 03:33:56 -070066 CallResult result = mCallModeler.getCallWithId(callId);
67 if (result != null) {
68 PhoneUtils.answerCall(result.getConnection().getCall());
69 }
Chiao Chenge41661c2013-07-23 13:28:26 -070070 } catch (Exception e) {
71 Log.e(TAG, "Error during answerCall().", e);
72 }
Santos Cordoncba1b442013-07-18 12:43:58 -070073 }
74
75 /**
76 * TODO(klp): Add a confirmation callback parameter.
77 */
78 @Override
Christine Chenee09a492013-08-06 16:02:29 -070079 public void rejectCall(int callId, boolean rejectWithMessage, String message) {
Chiao Chenge41661c2013-07-23 13:28:26 -070080 try {
Santos Cordon249efd02013-08-05 03:33:56 -070081 CallResult result = mCallModeler.getCallWithId(callId);
82 if (result != null) {
Yorke Lee814da302013-08-30 16:01:07 -070083 final String number = result.getConnection().getAddress();
Christine Chen0ce0e852013-08-09 18:26:31 -070084
Christine Chenee09a492013-08-06 16:02:29 -070085 Log.v(TAG, "Hanging up");
Santos Cordon249efd02013-08-05 03:33:56 -070086 PhoneUtils.hangupRingingCall(result.getConnection().getCall());
Christine Chen0ce0e852013-08-09 18:26:31 -070087
Yorke Lee814da302013-08-30 16:01:07 -070088 if (rejectWithMessage) {
Christine Chen0ce0e852013-08-09 18:26:31 -070089 mRejectWithTextMessageManager.rejectCallWithMessage(
90 result.getConnection().getCall(), message);
Yorke Lee814da302013-08-30 16:01:07 -070091 }
Santos Cordon249efd02013-08-05 03:33:56 -070092 }
Chiao Chenge41661c2013-07-23 13:28:26 -070093 } catch (Exception e) {
94 Log.e(TAG, "Error during rejectCall().", e);
95 }
Santos Cordoncba1b442013-07-18 12:43:58 -070096 }
97
98 @Override
99 public void disconnectCall(int callId) {
Chiao Chenge41661c2013-07-23 13:28:26 -0700100 try {
Santos Cordon249efd02013-08-05 03:33:56 -0700101 CallResult result = mCallModeler.getCallWithId(callId);
Santos Cordonb01deb52013-08-06 23:46:06 -0700102 if (DBG) Log.d(TAG, "disconnectCall " + result.getCall());
103
Santos Cordon249efd02013-08-05 03:33:56 -0700104 if (result != null) {
105 int state = result.getCall().getState();
Santos Cordon4ad64cd2013-08-15 00:36:14 -0700106 if (Call.State.ACTIVE == state ||
107 Call.State.ONHOLD == state ||
Christine Chen45277022013-09-05 10:55:37 -0700108 Call.State.DIALING == state) {
Santos Cordon249efd02013-08-05 03:33:56 -0700109 result.getConnection().getCall().hangup();
Christine Chen45277022013-09-05 10:55:37 -0700110 } else if (Call.State.CONFERENCED == state) {
111 result.getConnection().hangup();
Santos Cordon249efd02013-08-05 03:33:56 -0700112 }
113 }
Chiao Chenge41661c2013-07-23 13:28:26 -0700114 } catch (Exception e) {
115 Log.e(TAG, "Error during disconnectCall().", e);
116 }
117 }
118
119 @Override
Christine Chen45277022013-09-05 10:55:37 -0700120 public void separateCall(int callId) {
121 try {
122 CallResult result = mCallModeler.getCallWithId(callId);
123 if (DBG) Log.d(TAG, "disconnectCall " + result.getCall());
124
125 if (result != null) {
126 int state = result.getCall().getState();
127 if (Call.State.CONFERENCED == state) {
128 result.getConnection().separate();
129 }
130 }
131 } catch (Exception e) {
132 Log.e(TAG, "Error trying to separate call.", e);
133 }
134 }
135
136 @Override
Santos Cordon2b65bf02013-07-29 14:09:44 -0700137 public void hold(int callId, boolean hold) {
138 try {
Santos Cordon249efd02013-08-05 03:33:56 -0700139 CallResult result = mCallModeler.getCallWithId(callId);
140 if (result != null) {
141 int state = result.getCall().getState();
Santos Cordon2eaff902013-08-05 04:37:55 -0700142 if (hold && Call.State.ACTIVE == state) {
Santos Cordon249efd02013-08-05 03:33:56 -0700143 PhoneUtils.switchHoldingAndActive(mCallManager.getFirstActiveBgCall());
144 } else if (!hold && Call.State.ONHOLD == state) {
145 PhoneUtils.switchHoldingAndActive(result.getConnection().getCall());
146 }
147 }
Santos Cordon2b65bf02013-07-29 14:09:44 -0700148 } catch (Exception e) {
149 Log.e(TAG, "Error trying to place call on hold.", e);
150 }
151 }
152
153 @Override
Santos Cordoneead6ec2013-08-07 22:16:33 -0700154 public void merge() {
155 if (PhoneUtils.okToMergeCalls(mCallManager)) {
156 PhoneUtils.mergeCalls(mCallManager);
157 }
158 }
159
160 @Override
161 public void addCall() {
162 // start new call checks okToAddCall() already
163 PhoneUtils.startNewCall(mCallManager);
164 }
165
166
167 @Override
168 public void swap() {
169 if (!PhoneUtils.okToSwapCalls(mCallManager)) {
170 // TODO: throw an error instead?
171 return;
172 }
173
174 // Swap the fg and bg calls.
175 // In the future we may provides some way for user to choose among
176 // multiple background calls, for now, always act on the first background calll.
177 PhoneUtils.switchHoldingAndActive(mCallManager.getFirstActiveBgCall());
178
179 final PhoneGlobals mApp = PhoneGlobals.getInstance();
180
181 // If we have a valid BluetoothPhoneService then since CDMA network or
182 // Telephony FW does not send us information on which caller got swapped
183 // we need to update the second call active state in BluetoothPhoneService internally
184 if (mCallManager.getBgPhone().getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
185 final IBluetoothHeadsetPhone btPhone = mApp.getBluetoothPhoneService();
186 if (btPhone != null) {
187 try {
188 btPhone.cdmaSwapSecondCallState();
189 } catch (RemoteException e) {
190 Log.e(TAG, Log.getStackTraceString(new Throwable()));
191 }
192 }
193 }
194 }
195
196 @Override
Chiao Chenge41661c2013-07-23 13:28:26 -0700197 public void mute(boolean onOff) {
198 try {
Santos Cordoneead6ec2013-08-07 22:16:33 -0700199 PhoneUtils.setMute(onOff);
Chiao Chenge41661c2013-07-23 13:28:26 -0700200 } catch (Exception e) {
201 Log.e(TAG, "Error during mute().", e);
202 }
203 }
204
205 @Override
206 public void speaker(boolean onOff) {
207 try {
208 // TODO(klp): add bluetooth logic from InCallScreen.toggleSpeaker()
209 PhoneUtils.turnOnSpeaker(mContext, onOff, true);
210 } catch (Exception e) {
211 Log.e(TAG, "Error during speaker().", e);
212 }
Santos Cordoncba1b442013-07-18 12:43:58 -0700213 }
Santos Cordon2eaff902013-08-05 04:37:55 -0700214
215 @Override
Christine Chendaf7bf62013-08-05 19:12:31 -0700216 public void playDtmfTone(char digit, boolean timedShortTone) {
Santos Cordon2eaff902013-08-05 04:37:55 -0700217 try {
Christine Chendaf7bf62013-08-05 19:12:31 -0700218 mDtmfTonePlayer.playDtmfTone(digit, timedShortTone);
Santos Cordon2eaff902013-08-05 04:37:55 -0700219 } catch (Exception e) {
220 Log.e(TAG, "Error playing DTMF tone.", e);
221 }
222 }
223
224 @Override
225 public void stopDtmfTone() {
226 try {
227 mDtmfTonePlayer.stopDtmfTone();
228 } catch (Exception e) {
229 Log.e(TAG, "Error stopping DTMF tone.", e);
230 }
231 }
Santos Cordon9b7bac72013-08-06 08:04:52 -0700232
233 @Override
234 public void setAudioMode(int mode) {
235 try {
236 mAudioRouter.setAudioMode(mode);
237 } catch (Exception e) {
238 Log.e(TAG, "Error setting the audio mode.", e);
239 }
240 }
Chiao Cheng3f015c92013-09-06 15:56:27 -0700241
242 @Override
243 public void postDialCancel(int callId) throws RemoteException {
244 final CallResult result = mCallModeler.getCallWithId(callId);
245 if (result != null) {
246 result.getConnection().cancelPostDial();
247 }
248 }
249
250 @Override
251 public void postDialWaitContinue(int callId) throws RemoteException {
252 final CallResult result = mCallModeler.getCallWithId(callId);
253 if (result != null) {
254 result.getConnection().proceedAfterWaitChar();
255 }
256 }
Yorke Lee362cec22013-09-18 15:20:26 -0700257
258 @Override
259 public void setSystemBarNavigationEnabled(boolean enable) {
260 try {
261 final StatusBarHelper statusBarHelper = PhoneGlobals.getInstance().notificationMgr.
262 statusBarHelper;
263 statusBarHelper.enableSystemBarNavigation(enable);
264 statusBarHelper.enableExpandedView(enable);
265 } catch (Exception e) {
266 Log.e(TAG, "Error enabling or disabling system bar navigation", e);
267 }
268 }
269
Santos Cordoncba1b442013-07-18 12:43:58 -0700270}