blob: 560e78c9393383641374939ab7f069a1a337123e [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;
24import com.android.services.telephony.common.Call;
Santos Cordoncba1b442013-07-18 12:43:58 -070025import com.android.services.telephony.common.ICallCommandService;
26
27/**
Chiao Chenge41661c2013-07-23 13:28:26 -070028 * Service interface used by in-call ui to control phone calls using commands exposed as methods.
29 * Instances of this class are handed to in-call UI via CallMonitorService.
Santos Cordoncba1b442013-07-18 12:43:58 -070030 */
31class CallCommandService extends ICallCommandService.Stub {
32
Chiao Chenge41661c2013-07-23 13:28:26 -070033 private static final String TAG = CallCommandService.class.getSimpleName();
34
Santos Cordon249efd02013-08-05 03:33:56 -070035 private final Context mContext;
36 private final CallManager mCallManager;
37 private final CallModeler mCallModeler;
Santos Cordon2eaff902013-08-05 04:37:55 -070038 private final DTMFTonePlayer mDtmfTonePlayer;
Santos Cordoncba1b442013-07-18 12:43:58 -070039
Santos Cordon2eaff902013-08-05 04:37:55 -070040 public CallCommandService(Context context, CallManager callManager, CallModeler callModeler,
41 DTMFTonePlayer dtmfTonePlayer) {
Chiao Chenge41661c2013-07-23 13:28:26 -070042 mContext = context;
Santos Cordoncba1b442013-07-18 12:43:58 -070043 mCallManager = callManager;
Santos Cordon249efd02013-08-05 03:33:56 -070044 mCallModeler = callModeler;
Santos Cordon2eaff902013-08-05 04:37:55 -070045 mDtmfTonePlayer = dtmfTonePlayer;
Santos Cordoncba1b442013-07-18 12:43:58 -070046 }
47
48 /**
49 * TODO(klp): Add a confirmation callback parameter.
50 */
51 @Override
52 public void answerCall(int callId) {
Chiao Chenge41661c2013-07-23 13:28:26 -070053 try {
Santos Cordon249efd02013-08-05 03:33:56 -070054 CallResult result = mCallModeler.getCallWithId(callId);
55 if (result != null) {
56 PhoneUtils.answerCall(result.getConnection().getCall());
57 }
Chiao Chenge41661c2013-07-23 13:28:26 -070058 } catch (Exception e) {
59 Log.e(TAG, "Error during answerCall().", e);
60 }
Santos Cordoncba1b442013-07-18 12:43:58 -070061 }
62
63 /**
64 * TODO(klp): Add a confirmation callback parameter.
65 */
66 @Override
67 public void rejectCall(int callId) {
Chiao Chenge41661c2013-07-23 13:28:26 -070068 try {
Santos Cordon249efd02013-08-05 03:33:56 -070069 CallResult result = mCallModeler.getCallWithId(callId);
70 if (result != null) {
71 PhoneUtils.hangupRingingCall(result.getConnection().getCall());
72 }
Chiao Chenge41661c2013-07-23 13:28:26 -070073 } catch (Exception e) {
74 Log.e(TAG, "Error during rejectCall().", e);
75 }
Santos Cordoncba1b442013-07-18 12:43:58 -070076 }
77
78 @Override
79 public void disconnectCall(int callId) {
Chiao Chenge41661c2013-07-23 13:28:26 -070080 try {
Santos Cordon249efd02013-08-05 03:33:56 -070081 CallResult result = mCallModeler.getCallWithId(callId);
82 if (result != null) {
83 int state = result.getCall().getState();
84 if (Call.State.ACTIVE == state || Call.State.ONHOLD == state) {
85 result.getConnection().getCall().hangup();
86 }
87 }
Chiao Chenge41661c2013-07-23 13:28:26 -070088 } catch (Exception e) {
89 Log.e(TAG, "Error during disconnectCall().", e);
90 }
91 }
92
93 @Override
Santos Cordon2b65bf02013-07-29 14:09:44 -070094 public void hold(int callId, boolean hold) {
95 try {
Santos Cordon249efd02013-08-05 03:33:56 -070096 CallResult result = mCallModeler.getCallWithId(callId);
97 if (result != null) {
98 int state = result.getCall().getState();
Santos Cordon2eaff902013-08-05 04:37:55 -070099 if (hold && Call.State.ACTIVE == state) {
Santos Cordon249efd02013-08-05 03:33:56 -0700100 PhoneUtils.switchHoldingAndActive(mCallManager.getFirstActiveBgCall());
101 } else if (!hold && Call.State.ONHOLD == state) {
102 PhoneUtils.switchHoldingAndActive(result.getConnection().getCall());
103 }
104 }
Santos Cordon2b65bf02013-07-29 14:09:44 -0700105 } catch (Exception e) {
106 Log.e(TAG, "Error trying to place call on hold.", e);
107 }
108 }
109
110 @Override
Chiao Chenge41661c2013-07-23 13:28:26 -0700111 public void mute(boolean onOff) {
112 try {
113 PhoneUtils.setMute(onOff);
114 } catch (Exception e) {
115 Log.e(TAG, "Error during mute().", e);
116 }
117 }
118
119 @Override
120 public void speaker(boolean onOff) {
121 try {
122 // TODO(klp): add bluetooth logic from InCallScreen.toggleSpeaker()
123 PhoneUtils.turnOnSpeaker(mContext, onOff, true);
124 } catch (Exception e) {
125 Log.e(TAG, "Error during speaker().", e);
126 }
Santos Cordoncba1b442013-07-18 12:43:58 -0700127 }
Santos Cordon2eaff902013-08-05 04:37:55 -0700128
129 @Override
Christine Chendaf7bf62013-08-05 19:12:31 -0700130 public void playDtmfTone(char digit, boolean timedShortTone) {
Santos Cordon2eaff902013-08-05 04:37:55 -0700131 try {
Christine Chendaf7bf62013-08-05 19:12:31 -0700132 mDtmfTonePlayer.playDtmfTone(digit, timedShortTone);
Santos Cordon2eaff902013-08-05 04:37:55 -0700133 } catch (Exception e) {
134 Log.e(TAG, "Error playing DTMF tone.", e);
135 }
136 }
137
138 @Override
139 public void stopDtmfTone() {
140 try {
141 mDtmfTonePlayer.stopDtmfTone();
142 } catch (Exception e) {
143 Log.e(TAG, "Error stopping DTMF tone.", e);
144 }
145 }
Santos Cordoncba1b442013-07-18 12:43:58 -0700146}