blob: 72273ee5cd1fd6724c53e7113f2e8b8747998763 [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 Chenee09a492013-08-06 16:02:29 -070083 Log.v(TAG, "Hanging up");
Santos Cordon249efd02013-08-05 03:33:56 -070084 PhoneUtils.hangupRingingCall(result.getConnection().getCall());
Yorke Lee814da302013-08-30 16:01:07 -070085 if (rejectWithMessage) {
86 if (message != null) {
87 mRejectWithTextMessageManager.rejectCallWithMessage(number, message);
88 } else {
89 mRejectWithTextMessageManager.rejectCallWithNewMessage(number);
90 }
91 }
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 ||
108 Call.State.DIALING == state ||
109 Call.State.CONFERENCED == state) {
Santos Cordon249efd02013-08-05 03:33:56 -0700110 result.getConnection().getCall().hangup();
111 }
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
Santos Cordon2b65bf02013-07-29 14:09:44 -0700119 public void hold(int callId, boolean hold) {
120 try {
Santos Cordon249efd02013-08-05 03:33:56 -0700121 CallResult result = mCallModeler.getCallWithId(callId);
122 if (result != null) {
123 int state = result.getCall().getState();
Santos Cordon2eaff902013-08-05 04:37:55 -0700124 if (hold && Call.State.ACTIVE == state) {
Santos Cordon249efd02013-08-05 03:33:56 -0700125 PhoneUtils.switchHoldingAndActive(mCallManager.getFirstActiveBgCall());
126 } else if (!hold && Call.State.ONHOLD == state) {
127 PhoneUtils.switchHoldingAndActive(result.getConnection().getCall());
128 }
129 }
Santos Cordon2b65bf02013-07-29 14:09:44 -0700130 } catch (Exception e) {
131 Log.e(TAG, "Error trying to place call on hold.", e);
132 }
133 }
134
135 @Override
Santos Cordoneead6ec2013-08-07 22:16:33 -0700136 public void merge() {
137 if (PhoneUtils.okToMergeCalls(mCallManager)) {
138 PhoneUtils.mergeCalls(mCallManager);
139 }
140 }
141
142 @Override
143 public void addCall() {
144 // start new call checks okToAddCall() already
145 PhoneUtils.startNewCall(mCallManager);
146 }
147
148
149 @Override
150 public void swap() {
151 if (!PhoneUtils.okToSwapCalls(mCallManager)) {
152 // TODO: throw an error instead?
153 return;
154 }
155
156 // Swap the fg and bg calls.
157 // In the future we may provides some way for user to choose among
158 // multiple background calls, for now, always act on the first background calll.
159 PhoneUtils.switchHoldingAndActive(mCallManager.getFirstActiveBgCall());
160
161 final PhoneGlobals mApp = PhoneGlobals.getInstance();
162
163 // If we have a valid BluetoothPhoneService then since CDMA network or
164 // Telephony FW does not send us information on which caller got swapped
165 // we need to update the second call active state in BluetoothPhoneService internally
166 if (mCallManager.getBgPhone().getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
167 final IBluetoothHeadsetPhone btPhone = mApp.getBluetoothPhoneService();
168 if (btPhone != null) {
169 try {
170 btPhone.cdmaSwapSecondCallState();
171 } catch (RemoteException e) {
172 Log.e(TAG, Log.getStackTraceString(new Throwable()));
173 }
174 }
175 }
176 }
177
178 @Override
Chiao Chenge41661c2013-07-23 13:28:26 -0700179 public void mute(boolean onOff) {
180 try {
Santos Cordoneead6ec2013-08-07 22:16:33 -0700181 PhoneUtils.setMute(onOff);
Chiao Chenge41661c2013-07-23 13:28:26 -0700182 } catch (Exception e) {
183 Log.e(TAG, "Error during mute().", e);
184 }
185 }
186
187 @Override
188 public void speaker(boolean onOff) {
189 try {
190 // TODO(klp): add bluetooth logic from InCallScreen.toggleSpeaker()
191 PhoneUtils.turnOnSpeaker(mContext, onOff, true);
192 } catch (Exception e) {
193 Log.e(TAG, "Error during speaker().", e);
194 }
Santos Cordoncba1b442013-07-18 12:43:58 -0700195 }
Santos Cordon2eaff902013-08-05 04:37:55 -0700196
197 @Override
Christine Chendaf7bf62013-08-05 19:12:31 -0700198 public void playDtmfTone(char digit, boolean timedShortTone) {
Santos Cordon2eaff902013-08-05 04:37:55 -0700199 try {
Christine Chendaf7bf62013-08-05 19:12:31 -0700200 mDtmfTonePlayer.playDtmfTone(digit, timedShortTone);
Santos Cordon2eaff902013-08-05 04:37:55 -0700201 } catch (Exception e) {
202 Log.e(TAG, "Error playing DTMF tone.", e);
203 }
204 }
205
206 @Override
207 public void stopDtmfTone() {
208 try {
209 mDtmfTonePlayer.stopDtmfTone();
210 } catch (Exception e) {
211 Log.e(TAG, "Error stopping DTMF tone.", e);
212 }
213 }
Santos Cordon9b7bac72013-08-06 08:04:52 -0700214
215 @Override
216 public void setAudioMode(int mode) {
217 try {
218 mAudioRouter.setAudioMode(mode);
219 } catch (Exception e) {
220 Log.e(TAG, "Error setting the audio mode.", e);
221 }
222 }
Santos Cordoncba1b442013-07-18 12:43:58 -0700223}