blob: f216a4861aa470febcdf8d773de663e97749a06a [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 Cordoncba1b442013-07-18 12:43:58 -070038
Santos Cordon249efd02013-08-05 03:33:56 -070039 public CallCommandService(Context context, CallManager callManager, CallModeler callModeler) {
Chiao Chenge41661c2013-07-23 13:28:26 -070040 mContext = context;
Santos Cordoncba1b442013-07-18 12:43:58 -070041 mCallManager = callManager;
Santos Cordon249efd02013-08-05 03:33:56 -070042 mCallModeler = callModeler;
Santos Cordoncba1b442013-07-18 12:43:58 -070043 }
44
45 /**
46 * TODO(klp): Add a confirmation callback parameter.
47 */
48 @Override
49 public void answerCall(int callId) {
Chiao Chenge41661c2013-07-23 13:28:26 -070050 try {
Santos Cordon249efd02013-08-05 03:33:56 -070051 CallResult result = mCallModeler.getCallWithId(callId);
52 if (result != null) {
53 PhoneUtils.answerCall(result.getConnection().getCall());
54 }
Chiao Chenge41661c2013-07-23 13:28:26 -070055 } catch (Exception e) {
56 Log.e(TAG, "Error during answerCall().", e);
57 }
Santos Cordoncba1b442013-07-18 12:43:58 -070058 }
59
60 /**
61 * TODO(klp): Add a confirmation callback parameter.
62 */
63 @Override
64 public void rejectCall(int callId) {
Chiao Chenge41661c2013-07-23 13:28:26 -070065 try {
Santos Cordon249efd02013-08-05 03:33:56 -070066 CallResult result = mCallModeler.getCallWithId(callId);
67 if (result != null) {
68 PhoneUtils.hangupRingingCall(result.getConnection().getCall());
69 }
Chiao Chenge41661c2013-07-23 13:28:26 -070070 } catch (Exception e) {
71 Log.e(TAG, "Error during rejectCall().", e);
72 }
Santos Cordoncba1b442013-07-18 12:43:58 -070073 }
74
75 @Override
76 public void disconnectCall(int callId) {
Chiao Chenge41661c2013-07-23 13:28:26 -070077 try {
Santos Cordon249efd02013-08-05 03:33:56 -070078 CallResult result = mCallModeler.getCallWithId(callId);
79 if (result != null) {
80 int state = result.getCall().getState();
81 if (Call.State.ACTIVE == state || Call.State.ONHOLD == state) {
82 result.getConnection().getCall().hangup();
83 }
84 }
Chiao Chenge41661c2013-07-23 13:28:26 -070085 } catch (Exception e) {
86 Log.e(TAG, "Error during disconnectCall().", e);
87 }
88 }
89
90 @Override
Santos Cordon2b65bf02013-07-29 14:09:44 -070091 public void hold(int callId, boolean hold) {
92 try {
Santos Cordon249efd02013-08-05 03:33:56 -070093 CallResult result = mCallModeler.getCallWithId(callId);
94 if (result != null) {
95 int state = result.getCall().getState();
96 if (hold && Call.State.ACTIVE == state ) {
97 PhoneUtils.switchHoldingAndActive(mCallManager.getFirstActiveBgCall());
98 } else if (!hold && Call.State.ONHOLD == state) {
99 PhoneUtils.switchHoldingAndActive(result.getConnection().getCall());
100 }
101 }
Santos Cordon2b65bf02013-07-29 14:09:44 -0700102 } catch (Exception e) {
103 Log.e(TAG, "Error trying to place call on hold.", e);
104 }
105 }
106
107 @Override
Chiao Chenge41661c2013-07-23 13:28:26 -0700108 public void mute(boolean onOff) {
109 try {
110 PhoneUtils.setMute(onOff);
111 } catch (Exception e) {
112 Log.e(TAG, "Error during mute().", e);
113 }
114 }
115
116 @Override
117 public void speaker(boolean onOff) {
118 try {
119 // TODO(klp): add bluetooth logic from InCallScreen.toggleSpeaker()
120 PhoneUtils.turnOnSpeaker(mContext, onOff, true);
121 } catch (Exception e) {
122 Log.e(TAG, "Error during speaker().", e);
123 }
Santos Cordoncba1b442013-07-18 12:43:58 -0700124 }
125}