blob: 35c507a154bc0ba8626fa9608f13101053588625 [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;
Santos Cordoncba1b442013-07-18 12:43:58 -070041
Santos Cordon2eaff902013-08-05 04:37:55 -070042 public CallCommandService(Context context, CallManager callManager, CallModeler callModeler,
Santos Cordon9b7bac72013-08-06 08:04:52 -070043 DTMFTonePlayer dtmfTonePlayer, AudioRouter audioRouter) {
Chiao Chenge41661c2013-07-23 13:28:26 -070044 mContext = context;
Santos Cordoncba1b442013-07-18 12:43:58 -070045 mCallManager = callManager;
Santos Cordon249efd02013-08-05 03:33:56 -070046 mCallModeler = callModeler;
Santos Cordon2eaff902013-08-05 04:37:55 -070047 mDtmfTonePlayer = dtmfTonePlayer;
Santos Cordon9b7bac72013-08-06 08:04:52 -070048 mAudioRouter = audioRouter;
Santos Cordoncba1b442013-07-18 12:43:58 -070049 }
50
51 /**
52 * TODO(klp): Add a confirmation callback parameter.
53 */
54 @Override
55 public void answerCall(int callId) {
Chiao Chenge41661c2013-07-23 13:28:26 -070056 try {
Santos Cordon249efd02013-08-05 03:33:56 -070057 CallResult result = mCallModeler.getCallWithId(callId);
58 if (result != null) {
59 PhoneUtils.answerCall(result.getConnection().getCall());
60 }
Chiao Chenge41661c2013-07-23 13:28:26 -070061 } catch (Exception e) {
62 Log.e(TAG, "Error during answerCall().", e);
63 }
Santos Cordoncba1b442013-07-18 12:43:58 -070064 }
65
66 /**
67 * TODO(klp): Add a confirmation callback parameter.
68 */
69 @Override
70 public void rejectCall(int callId) {
Chiao Chenge41661c2013-07-23 13:28:26 -070071 try {
Santos Cordon249efd02013-08-05 03:33:56 -070072 CallResult result = mCallModeler.getCallWithId(callId);
73 if (result != null) {
74 PhoneUtils.hangupRingingCall(result.getConnection().getCall());
75 }
Chiao Chenge41661c2013-07-23 13:28:26 -070076 } catch (Exception e) {
77 Log.e(TAG, "Error during rejectCall().", e);
78 }
Santos Cordoncba1b442013-07-18 12:43:58 -070079 }
80
81 @Override
82 public void disconnectCall(int callId) {
Chiao Chenge41661c2013-07-23 13:28:26 -070083 try {
Santos Cordon249efd02013-08-05 03:33:56 -070084 CallResult result = mCallModeler.getCallWithId(callId);
85 if (result != null) {
86 int state = result.getCall().getState();
87 if (Call.State.ACTIVE == state || Call.State.ONHOLD == state) {
88 result.getConnection().getCall().hangup();
89 }
90 }
Chiao Chenge41661c2013-07-23 13:28:26 -070091 } catch (Exception e) {
92 Log.e(TAG, "Error during disconnectCall().", e);
93 }
94 }
95
96 @Override
Santos Cordon2b65bf02013-07-29 14:09:44 -070097 public void hold(int callId, boolean hold) {
98 try {
Santos Cordon249efd02013-08-05 03:33:56 -070099 CallResult result = mCallModeler.getCallWithId(callId);
100 if (result != null) {
101 int state = result.getCall().getState();
Santos Cordon2eaff902013-08-05 04:37:55 -0700102 if (hold && Call.State.ACTIVE == state) {
Santos Cordon249efd02013-08-05 03:33:56 -0700103 PhoneUtils.switchHoldingAndActive(mCallManager.getFirstActiveBgCall());
104 } else if (!hold && Call.State.ONHOLD == state) {
105 PhoneUtils.switchHoldingAndActive(result.getConnection().getCall());
106 }
107 }
Santos Cordon2b65bf02013-07-29 14:09:44 -0700108 } catch (Exception e) {
109 Log.e(TAG, "Error trying to place call on hold.", e);
110 }
111 }
112
113 @Override
Chiao Chenge41661c2013-07-23 13:28:26 -0700114 public void mute(boolean onOff) {
115 try {
Santos Cordon9b7bac72013-08-06 08:04:52 -0700116 //PhoneUtils.setMute(onOff);
117 mAudioRouter.setAudioMode(onOff ? AudioMode.BLUETOOTH : AudioMode.EARPIECE);
Chiao Chenge41661c2013-07-23 13:28:26 -0700118 } catch (Exception e) {
119 Log.e(TAG, "Error during mute().", e);
120 }
121 }
122
123 @Override
124 public void speaker(boolean onOff) {
125 try {
126 // TODO(klp): add bluetooth logic from InCallScreen.toggleSpeaker()
127 PhoneUtils.turnOnSpeaker(mContext, onOff, true);
128 } catch (Exception e) {
129 Log.e(TAG, "Error during speaker().", e);
130 }
Santos Cordoncba1b442013-07-18 12:43:58 -0700131 }
Santos Cordon2eaff902013-08-05 04:37:55 -0700132
133 @Override
Christine Chendaf7bf62013-08-05 19:12:31 -0700134 public void playDtmfTone(char digit, boolean timedShortTone) {
Santos Cordon2eaff902013-08-05 04:37:55 -0700135 try {
Christine Chendaf7bf62013-08-05 19:12:31 -0700136 mDtmfTonePlayer.playDtmfTone(digit, timedShortTone);
Santos Cordon2eaff902013-08-05 04:37:55 -0700137 } catch (Exception e) {
138 Log.e(TAG, "Error playing DTMF tone.", e);
139 }
140 }
141
142 @Override
143 public void stopDtmfTone() {
144 try {
145 mDtmfTonePlayer.stopDtmfTone();
146 } catch (Exception e) {
147 Log.e(TAG, "Error stopping DTMF tone.", e);
148 }
149 }
Santos Cordon9b7bac72013-08-06 08:04:52 -0700150
151 @Override
152 public void setAudioMode(int mode) {
153 try {
154 mAudioRouter.setAudioMode(mode);
155 } catch (Exception e) {
156 Log.e(TAG, "Error setting the audio mode.", e);
157 }
158 }
Santos Cordoncba1b442013-07-18 12:43:58 -0700159}