blob: 01b4dd8293e4371fb99ff3c1c9b7bc3ae032ed41 [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) {
Christine Chenee09a492013-08-06 16:02:29 -070082 if (rejectWithMessage) {
83 if (message != null) {
84 mRejectWithTextMessageManager.rejectCallWithMessage(
85 result.getConnection().getCall(), message);
86 } else {
87 mRejectWithTextMessageManager.rejectCallWithNewMessage(
88 result.getConnection().getCall());
89 }
90 }
91 Log.v(TAG, "Hanging up");
Santos Cordon249efd02013-08-05 03:33:56 -070092 PhoneUtils.hangupRingingCall(result.getConnection().getCall());
93 }
Chiao Chenge41661c2013-07-23 13:28:26 -070094 } catch (Exception e) {
95 Log.e(TAG, "Error during rejectCall().", e);
96 }
Santos Cordoncba1b442013-07-18 12:43:58 -070097 }
98
99 @Override
100 public void disconnectCall(int callId) {
Chiao Chenge41661c2013-07-23 13:28:26 -0700101 try {
Santos Cordon249efd02013-08-05 03:33:56 -0700102 CallResult result = mCallModeler.getCallWithId(callId);
Santos Cordonb01deb52013-08-06 23:46:06 -0700103 if (DBG) Log.d(TAG, "disconnectCall " + result.getCall());
104
Santos Cordon249efd02013-08-05 03:33:56 -0700105 if (result != null) {
106 int state = result.getCall().getState();
Santos Cordonb01deb52013-08-06 23:46:06 -0700107 if (Call.State.ACTIVE == state || Call.State.ONHOLD == state ||
108 Call.State.DIALING == state) {
Santos Cordon249efd02013-08-05 03:33:56 -0700109 result.getConnection().getCall().hangup();
110 }
111 }
Chiao Chenge41661c2013-07-23 13:28:26 -0700112 } catch (Exception e) {
113 Log.e(TAG, "Error during disconnectCall().", e);
114 }
115 }
116
117 @Override
Santos Cordon2b65bf02013-07-29 14:09:44 -0700118 public void hold(int callId, boolean hold) {
119 try {
Santos Cordon249efd02013-08-05 03:33:56 -0700120 CallResult result = mCallModeler.getCallWithId(callId);
121 if (result != null) {
122 int state = result.getCall().getState();
Santos Cordon2eaff902013-08-05 04:37:55 -0700123 if (hold && Call.State.ACTIVE == state) {
Santos Cordon249efd02013-08-05 03:33:56 -0700124 PhoneUtils.switchHoldingAndActive(mCallManager.getFirstActiveBgCall());
125 } else if (!hold && Call.State.ONHOLD == state) {
126 PhoneUtils.switchHoldingAndActive(result.getConnection().getCall());
127 }
128 }
Santos Cordon2b65bf02013-07-29 14:09:44 -0700129 } catch (Exception e) {
130 Log.e(TAG, "Error trying to place call on hold.", e);
131 }
132 }
133
134 @Override
Santos Cordoneead6ec2013-08-07 22:16:33 -0700135 public void merge() {
136 if (PhoneUtils.okToMergeCalls(mCallManager)) {
137 PhoneUtils.mergeCalls(mCallManager);
138 }
139 }
140
141 @Override
142 public void addCall() {
143 // start new call checks okToAddCall() already
144 PhoneUtils.startNewCall(mCallManager);
145 }
146
147
148 @Override
149 public void swap() {
150 if (!PhoneUtils.okToSwapCalls(mCallManager)) {
151 // TODO: throw an error instead?
152 return;
153 }
154
155 // Swap the fg and bg calls.
156 // In the future we may provides some way for user to choose among
157 // multiple background calls, for now, always act on the first background calll.
158 PhoneUtils.switchHoldingAndActive(mCallManager.getFirstActiveBgCall());
159
160 final PhoneGlobals mApp = PhoneGlobals.getInstance();
161
162 // If we have a valid BluetoothPhoneService then since CDMA network or
163 // Telephony FW does not send us information on which caller got swapped
164 // we need to update the second call active state in BluetoothPhoneService internally
165 if (mCallManager.getBgPhone().getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
166 final IBluetoothHeadsetPhone btPhone = mApp.getBluetoothPhoneService();
167 if (btPhone != null) {
168 try {
169 btPhone.cdmaSwapSecondCallState();
170 } catch (RemoteException e) {
171 Log.e(TAG, Log.getStackTraceString(new Throwable()));
172 }
173 }
174 }
175 }
176
177 @Override
Chiao Chenge41661c2013-07-23 13:28:26 -0700178 public void mute(boolean onOff) {
179 try {
Santos Cordoneead6ec2013-08-07 22:16:33 -0700180 PhoneUtils.setMute(onOff);
Chiao Chenge41661c2013-07-23 13:28:26 -0700181 } catch (Exception e) {
182 Log.e(TAG, "Error during mute().", e);
183 }
184 }
185
186 @Override
187 public void speaker(boolean onOff) {
188 try {
189 // TODO(klp): add bluetooth logic from InCallScreen.toggleSpeaker()
190 PhoneUtils.turnOnSpeaker(mContext, onOff, true);
191 } catch (Exception e) {
192 Log.e(TAG, "Error during speaker().", e);
193 }
Santos Cordoncba1b442013-07-18 12:43:58 -0700194 }
Santos Cordon2eaff902013-08-05 04:37:55 -0700195
196 @Override
Christine Chendaf7bf62013-08-05 19:12:31 -0700197 public void playDtmfTone(char digit, boolean timedShortTone) {
Santos Cordon2eaff902013-08-05 04:37:55 -0700198 try {
Christine Chendaf7bf62013-08-05 19:12:31 -0700199 mDtmfTonePlayer.playDtmfTone(digit, timedShortTone);
Santos Cordon2eaff902013-08-05 04:37:55 -0700200 } catch (Exception e) {
201 Log.e(TAG, "Error playing DTMF tone.", e);
202 }
203 }
204
205 @Override
206 public void stopDtmfTone() {
207 try {
208 mDtmfTonePlayer.stopDtmfTone();
209 } catch (Exception e) {
210 Log.e(TAG, "Error stopping DTMF tone.", e);
211 }
212 }
Santos Cordon9b7bac72013-08-06 08:04:52 -0700213
214 @Override
215 public void setAudioMode(int mode) {
216 try {
217 mAudioRouter.setAudioMode(mode);
218 } catch (Exception e) {
219 Log.e(TAG, "Error setting the audio mode.", e);
220 }
221 }
Santos Cordoncba1b442013-07-18 12:43:58 -0700222}