blob: e5adb52e06bdc8f374feb8b4160526d45a8602e9 [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;
Santos Cordonb01deb52013-08-06 23:46:06 -070020import android.os.SystemProperties;
Chiao Chenge41661c2013-07-23 13:28:26 -070021import android.util.Log;
22
Santos Cordoncba1b442013-07-18 12:43:58 -070023import com.android.internal.telephony.CallManager;
Santos Cordon249efd02013-08-05 03:33:56 -070024import com.android.phone.CallModeler.CallResult;
Santos Cordon9b7bac72013-08-06 08:04:52 -070025import com.android.services.telephony.common.AudioMode;
Santos Cordon249efd02013-08-05 03:33:56 -070026import com.android.services.telephony.common.Call;
Santos Cordoncba1b442013-07-18 12:43:58 -070027import com.android.services.telephony.common.ICallCommandService;
28
29/**
Chiao Chenge41661c2013-07-23 13:28:26 -070030 * Service interface used by in-call ui to control phone calls using commands exposed as methods.
31 * Instances of this class are handed to in-call UI via CallMonitorService.
Santos Cordoncba1b442013-07-18 12:43:58 -070032 */
33class CallCommandService extends ICallCommandService.Stub {
Chiao Chenge41661c2013-07-23 13:28:26 -070034 private static final String TAG = CallCommandService.class.getSimpleName();
Santos Cordonb01deb52013-08-06 23:46:06 -070035 private static final boolean DBG =
36 (PhoneGlobals.DBG_LEVEL >= 1) && (SystemProperties.getInt("ro.debuggable", 0) == 1);
Chiao Chenge41661c2013-07-23 13:28:26 -070037
Santos Cordon249efd02013-08-05 03:33:56 -070038 private final Context mContext;
39 private final CallManager mCallManager;
40 private final CallModeler mCallModeler;
Santos Cordon2eaff902013-08-05 04:37:55 -070041 private final DTMFTonePlayer mDtmfTonePlayer;
Santos Cordon9b7bac72013-08-06 08:04:52 -070042 private final AudioRouter mAudioRouter;
Christine Chenee09a492013-08-06 16:02:29 -070043 private final RejectWithTextMessageManager mRejectWithTextMessageManager;
Santos Cordoncba1b442013-07-18 12:43:58 -070044
Santos Cordon2eaff902013-08-05 04:37:55 -070045 public CallCommandService(Context context, CallManager callManager, CallModeler callModeler,
Christine Chenee09a492013-08-06 16:02:29 -070046 DTMFTonePlayer dtmfTonePlayer, AudioRouter audioRouter,
47 RejectWithTextMessageManager rejectWithTextMessageManager) {
Chiao Chenge41661c2013-07-23 13:28:26 -070048 mContext = context;
Santos Cordoncba1b442013-07-18 12:43:58 -070049 mCallManager = callManager;
Santos Cordon249efd02013-08-05 03:33:56 -070050 mCallModeler = callModeler;
Santos Cordon2eaff902013-08-05 04:37:55 -070051 mDtmfTonePlayer = dtmfTonePlayer;
Santos Cordon9b7bac72013-08-06 08:04:52 -070052 mAudioRouter = audioRouter;
Christine Chenee09a492013-08-06 16:02:29 -070053 mRejectWithTextMessageManager = rejectWithTextMessageManager;
Santos Cordoncba1b442013-07-18 12:43:58 -070054 }
55
56 /**
57 * TODO(klp): Add a confirmation callback parameter.
58 */
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 /**
72 * TODO(klp): Add a confirmation callback parameter.
73 */
74 @Override
Christine Chenee09a492013-08-06 16:02:29 -070075 public void rejectCall(int callId, boolean rejectWithMessage, String message) {
Chiao Chenge41661c2013-07-23 13:28:26 -070076 try {
Santos Cordon249efd02013-08-05 03:33:56 -070077 CallResult result = mCallModeler.getCallWithId(callId);
78 if (result != null) {
Christine Chenee09a492013-08-06 16:02:29 -070079 if (rejectWithMessage) {
80 if (message != null) {
81 mRejectWithTextMessageManager.rejectCallWithMessage(
82 result.getConnection().getCall(), message);
83 } else {
84 mRejectWithTextMessageManager.rejectCallWithNewMessage(
85 result.getConnection().getCall());
86 }
87 }
88 Log.v(TAG, "Hanging up");
Santos Cordon249efd02013-08-05 03:33:56 -070089 PhoneUtils.hangupRingingCall(result.getConnection().getCall());
90 }
Chiao Chenge41661c2013-07-23 13:28:26 -070091 } catch (Exception e) {
92 Log.e(TAG, "Error during rejectCall().", e);
93 }
Santos Cordoncba1b442013-07-18 12:43:58 -070094 }
95
96 @Override
97 public void disconnectCall(int callId) {
Chiao Chenge41661c2013-07-23 13:28:26 -070098 try {
Santos Cordon249efd02013-08-05 03:33:56 -070099 CallResult result = mCallModeler.getCallWithId(callId);
Santos Cordonb01deb52013-08-06 23:46:06 -0700100 if (DBG) Log.d(TAG, "disconnectCall " + result.getCall());
101
Santos Cordon249efd02013-08-05 03:33:56 -0700102 if (result != null) {
103 int state = result.getCall().getState();
Santos Cordonb01deb52013-08-06 23:46:06 -0700104 if (Call.State.ACTIVE == state || Call.State.ONHOLD == state ||
105 Call.State.DIALING == state) {
Santos Cordon249efd02013-08-05 03:33:56 -0700106 result.getConnection().getCall().hangup();
107 }
108 }
Chiao Chenge41661c2013-07-23 13:28:26 -0700109 } catch (Exception e) {
110 Log.e(TAG, "Error during disconnectCall().", e);
111 }
112 }
113
114 @Override
Santos Cordon2b65bf02013-07-29 14:09:44 -0700115 public void hold(int callId, boolean hold) {
116 try {
Santos Cordon249efd02013-08-05 03:33:56 -0700117 CallResult result = mCallModeler.getCallWithId(callId);
118 if (result != null) {
119 int state = result.getCall().getState();
Santos Cordon2eaff902013-08-05 04:37:55 -0700120 if (hold && Call.State.ACTIVE == state) {
Santos Cordon249efd02013-08-05 03:33:56 -0700121 PhoneUtils.switchHoldingAndActive(mCallManager.getFirstActiveBgCall());
122 } else if (!hold && Call.State.ONHOLD == state) {
123 PhoneUtils.switchHoldingAndActive(result.getConnection().getCall());
124 }
125 }
Santos Cordon2b65bf02013-07-29 14:09:44 -0700126 } catch (Exception e) {
127 Log.e(TAG, "Error trying to place call on hold.", e);
128 }
129 }
130
131 @Override
Chiao Chenge41661c2013-07-23 13:28:26 -0700132 public void mute(boolean onOff) {
133 try {
Santos Cordon9b7bac72013-08-06 08:04:52 -0700134 //PhoneUtils.setMute(onOff);
135 mAudioRouter.setAudioMode(onOff ? AudioMode.BLUETOOTH : AudioMode.EARPIECE);
Chiao Chenge41661c2013-07-23 13:28:26 -0700136 } catch (Exception e) {
137 Log.e(TAG, "Error during mute().", e);
138 }
139 }
140
141 @Override
142 public void speaker(boolean onOff) {
143 try {
144 // TODO(klp): add bluetooth logic from InCallScreen.toggleSpeaker()
145 PhoneUtils.turnOnSpeaker(mContext, onOff, true);
146 } catch (Exception e) {
147 Log.e(TAG, "Error during speaker().", e);
148 }
Santos Cordoncba1b442013-07-18 12:43:58 -0700149 }
Santos Cordon2eaff902013-08-05 04:37:55 -0700150
151 @Override
Christine Chendaf7bf62013-08-05 19:12:31 -0700152 public void playDtmfTone(char digit, boolean timedShortTone) {
Santos Cordon2eaff902013-08-05 04:37:55 -0700153 try {
Christine Chendaf7bf62013-08-05 19:12:31 -0700154 mDtmfTonePlayer.playDtmfTone(digit, timedShortTone);
Santos Cordon2eaff902013-08-05 04:37:55 -0700155 } catch (Exception e) {
156 Log.e(TAG, "Error playing DTMF tone.", e);
157 }
158 }
159
160 @Override
161 public void stopDtmfTone() {
162 try {
163 mDtmfTonePlayer.stopDtmfTone();
164 } catch (Exception e) {
165 Log.e(TAG, "Error stopping DTMF tone.", e);
166 }
167 }
Santos Cordon9b7bac72013-08-06 08:04:52 -0700168
169 @Override
170 public void setAudioMode(int mode) {
171 try {
172 mAudioRouter.setAudioMode(mode);
173 } catch (Exception e) {
174 Log.e(TAG, "Error setting the audio mode.", e);
175 }
176 }
Santos Cordoncba1b442013-07-18 12:43:58 -0700177}