blob: 1668891503db297bb7779196616403cbda29dd4c [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() {
Gabriel Peal36ebb0d2014-03-20 09:20:43 -0700171 try {
172 PhoneUtils.swap();
173 } catch (Exception e) {
174 Log.e(TAG, "Error during swap().", e);
Santos Cordoneead6ec2013-08-07 22:16:33 -0700175 }
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 {
Chiao Chenge41661c2013-07-23 13:28:26 -0700190 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 }
Chiao Cheng3f015c92013-09-06 15:56:27 -0700222
223 @Override
224 public void postDialCancel(int callId) throws RemoteException {
225 final CallResult result = mCallModeler.getCallWithId(callId);
226 if (result != null) {
227 result.getConnection().cancelPostDial();
228 }
229 }
230
231 @Override
232 public void postDialWaitContinue(int callId) throws RemoteException {
233 final CallResult result = mCallModeler.getCallWithId(callId);
234 if (result != null) {
235 result.getConnection().proceedAfterWaitChar();
236 }
237 }
Yorke Lee362cec22013-09-18 15:20:26 -0700238
239 @Override
240 public void setSystemBarNavigationEnabled(boolean enable) {
241 try {
242 final StatusBarHelper statusBarHelper = PhoneGlobals.getInstance().notificationMgr.
243 statusBarHelper;
244 statusBarHelper.enableSystemBarNavigation(enable);
245 statusBarHelper.enableExpandedView(enable);
246 } catch (Exception e) {
247 Log.e(TAG, "Error enabling or disabling system bar navigation", e);
248 }
249 }
250
Sailesh Nepal9658a9e2013-12-17 17:15:44 -0800251 @Override
252 public void videoHandoff(int callId) {
253 // TODO(sail): Implement this.
254 }
255
256 @Override
257 public void connectionHandoff(int callId) {
258 // TODO(sail): Implement this.
259 }
Santos Cordoncba1b442013-07-18 12:43:58 -0700260}