blob: 6a3848c872db97967d6ff2703a2269a772c3fe6e [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
Chiao Chenge41661c2013-07-23 13:28:26 -070019import android.content.Context;
20import android.util.Log;
21
Santos Cordoncba1b442013-07-18 12:43:58 -070022import com.android.internal.telephony.CallManager;
Santos Cordon249efd02013-08-05 03:33:56 -070023import com.android.phone.CallModeler.CallResult;
Santos Cordon9b7bac72013-08-06 08:04:52 -070024import com.android.services.telephony.common.AudioMode;
Santos Cordon249efd02013-08-05 03:33:56 -070025import com.android.services.telephony.common.Call;
Santos Cordoncba1b442013-07-18 12:43:58 -070026import com.android.services.telephony.common.ICallCommandService;
27
28/**
Chiao Chenge41661c2013-07-23 13:28:26 -070029 * Service interface used by in-call ui to control phone calls using commands exposed as methods.
30 * Instances of this class are handed to in-call UI via CallMonitorService.
Santos Cordoncba1b442013-07-18 12:43:58 -070031 */
32class CallCommandService extends ICallCommandService.Stub {
33
Chiao Chenge41661c2013-07-23 13:28:26 -070034 private static final String TAG = CallCommandService.class.getSimpleName();
35
Santos Cordon249efd02013-08-05 03:33:56 -070036 private final Context mContext;
37 private final CallManager mCallManager;
38 private final CallModeler mCallModeler;
Santos Cordon2eaff902013-08-05 04:37:55 -070039 private final DTMFTonePlayer mDtmfTonePlayer;
Santos Cordon9b7bac72013-08-06 08:04:52 -070040 private final AudioRouter mAudioRouter;
Christine Chenee09a492013-08-06 16:02:29 -070041 private final RejectWithTextMessageManager mRejectWithTextMessageManager;
Santos Cordoncba1b442013-07-18 12:43:58 -070042
Santos Cordon2eaff902013-08-05 04:37:55 -070043 public CallCommandService(Context context, CallManager callManager, CallModeler callModeler,
Christine Chenee09a492013-08-06 16:02:29 -070044 DTMFTonePlayer dtmfTonePlayer, AudioRouter audioRouter,
45 RejectWithTextMessageManager rejectWithTextMessageManager) {
Chiao Chenge41661c2013-07-23 13:28:26 -070046 mContext = context;
Santos Cordoncba1b442013-07-18 12:43:58 -070047 mCallManager = callManager;
Santos Cordon249efd02013-08-05 03:33:56 -070048 mCallModeler = callModeler;
Santos Cordon2eaff902013-08-05 04:37:55 -070049 mDtmfTonePlayer = dtmfTonePlayer;
Santos Cordon9b7bac72013-08-06 08:04:52 -070050 mAudioRouter = audioRouter;
Christine Chenee09a492013-08-06 16:02:29 -070051 mRejectWithTextMessageManager = rejectWithTextMessageManager;
Santos Cordoncba1b442013-07-18 12:43:58 -070052 }
53
54 /**
55 * TODO(klp): Add a confirmation callback parameter.
56 */
57 @Override
58 public void answerCall(int callId) {
Chiao Chenge41661c2013-07-23 13:28:26 -070059 try {
Santos Cordon249efd02013-08-05 03:33:56 -070060 CallResult result = mCallModeler.getCallWithId(callId);
61 if (result != null) {
62 PhoneUtils.answerCall(result.getConnection().getCall());
63 }
Chiao Chenge41661c2013-07-23 13:28:26 -070064 } catch (Exception e) {
65 Log.e(TAG, "Error during answerCall().", e);
66 }
Santos Cordoncba1b442013-07-18 12:43:58 -070067 }
68
69 /**
70 * TODO(klp): Add a confirmation callback parameter.
71 */
72 @Override
Christine Chenee09a492013-08-06 16:02:29 -070073 public void rejectCall(int callId, boolean rejectWithMessage, String message) {
Chiao Chenge41661c2013-07-23 13:28:26 -070074 try {
Santos Cordon249efd02013-08-05 03:33:56 -070075 CallResult result = mCallModeler.getCallWithId(callId);
76 if (result != null) {
Christine Chenee09a492013-08-06 16:02:29 -070077 if (rejectWithMessage) {
78 if (message != null) {
79 mRejectWithTextMessageManager.rejectCallWithMessage(
80 result.getConnection().getCall(), message);
81 } else {
82 mRejectWithTextMessageManager.rejectCallWithNewMessage(
83 result.getConnection().getCall());
84 }
85 }
86 Log.v(TAG, "Hanging up");
Santos Cordon249efd02013-08-05 03:33:56 -070087 PhoneUtils.hangupRingingCall(result.getConnection().getCall());
88 }
Chiao Chenge41661c2013-07-23 13:28:26 -070089 } catch (Exception e) {
90 Log.e(TAG, "Error during rejectCall().", e);
91 }
Santos Cordoncba1b442013-07-18 12:43:58 -070092 }
93
94 @Override
95 public void disconnectCall(int callId) {
Chiao Chenge41661c2013-07-23 13:28:26 -070096 try {
Santos Cordon249efd02013-08-05 03:33:56 -070097 CallResult result = mCallModeler.getCallWithId(callId);
98 if (result != null) {
99 int state = result.getCall().getState();
100 if (Call.State.ACTIVE == state || Call.State.ONHOLD == state) {
101 result.getConnection().getCall().hangup();
102 }
103 }
Chiao Chenge41661c2013-07-23 13:28:26 -0700104 } catch (Exception e) {
105 Log.e(TAG, "Error during disconnectCall().", e);
106 }
107 }
108
109 @Override
Santos Cordon2b65bf02013-07-29 14:09:44 -0700110 public void hold(int callId, boolean hold) {
111 try {
Santos Cordon249efd02013-08-05 03:33:56 -0700112 CallResult result = mCallModeler.getCallWithId(callId);
113 if (result != null) {
114 int state = result.getCall().getState();
Santos Cordon2eaff902013-08-05 04:37:55 -0700115 if (hold && Call.State.ACTIVE == state) {
Santos Cordon249efd02013-08-05 03:33:56 -0700116 PhoneUtils.switchHoldingAndActive(mCallManager.getFirstActiveBgCall());
117 } else if (!hold && Call.State.ONHOLD == state) {
118 PhoneUtils.switchHoldingAndActive(result.getConnection().getCall());
119 }
120 }
Santos Cordon2b65bf02013-07-29 14:09:44 -0700121 } catch (Exception e) {
122 Log.e(TAG, "Error trying to place call on hold.", e);
123 }
124 }
125
126 @Override
Chiao Chenge41661c2013-07-23 13:28:26 -0700127 public void mute(boolean onOff) {
128 try {
Santos Cordon9b7bac72013-08-06 08:04:52 -0700129 //PhoneUtils.setMute(onOff);
130 mAudioRouter.setAudioMode(onOff ? AudioMode.BLUETOOTH : AudioMode.EARPIECE);
Chiao Chenge41661c2013-07-23 13:28:26 -0700131 } catch (Exception e) {
132 Log.e(TAG, "Error during mute().", e);
133 }
134 }
135
136 @Override
137 public void speaker(boolean onOff) {
138 try {
139 // TODO(klp): add bluetooth logic from InCallScreen.toggleSpeaker()
140 PhoneUtils.turnOnSpeaker(mContext, onOff, true);
141 } catch (Exception e) {
142 Log.e(TAG, "Error during speaker().", e);
143 }
Santos Cordoncba1b442013-07-18 12:43:58 -0700144 }
Santos Cordon2eaff902013-08-05 04:37:55 -0700145
146 @Override
Christine Chendaf7bf62013-08-05 19:12:31 -0700147 public void playDtmfTone(char digit, boolean timedShortTone) {
Santos Cordon2eaff902013-08-05 04:37:55 -0700148 try {
Christine Chendaf7bf62013-08-05 19:12:31 -0700149 mDtmfTonePlayer.playDtmfTone(digit, timedShortTone);
Santos Cordon2eaff902013-08-05 04:37:55 -0700150 } catch (Exception e) {
151 Log.e(TAG, "Error playing DTMF tone.", e);
152 }
153 }
154
155 @Override
156 public void stopDtmfTone() {
157 try {
158 mDtmfTonePlayer.stopDtmfTone();
159 } catch (Exception e) {
160 Log.e(TAG, "Error stopping DTMF tone.", e);
161 }
162 }
Santos Cordon9b7bac72013-08-06 08:04:52 -0700163
164 @Override
165 public void setAudioMode(int mode) {
166 try {
167 mAudioRouter.setAudioMode(mode);
168 } catch (Exception e) {
169 Log.e(TAG, "Error setting the audio mode.", e);
170 }
171 }
Santos Cordoncba1b442013-07-18 12:43:58 -0700172}