blob: 7937e95c2b7c2bb68f6e76458ba63f3b9751fe63 [file] [log] [blame]
Santos Cordone3d76ab2014-01-28 17:25:20 -08001/*
2 * Copyright (C) 2014 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
14 * limitations under the License.
15 */
16
Ben Gilad9f2bed32013-12-12 17:43:26 -080017package com.android.telecomm;
18
Santos Cordone3d76ab2014-01-28 17:25:20 -080019import android.os.Handler;
Sailesh Nepal10ea4602014-04-01 17:23:32 -070020import android.os.Message;
Sailesh Nepala439e1b2014-03-11 18:19:58 -070021
Evan Charlton352105c2014-06-03 14:10:54 -070022import com.android.internal.os.SomeArgs;
Sailesh Nepala439e1b2014-03-11 18:19:58 -070023import com.android.internal.telecomm.IInCallAdapter;
Ben Gilad9f2bed32013-12-12 17:43:26 -080024
Santos Cordone3d76ab2014-01-28 17:25:20 -080025/**
26 * Receives call commands and updates from in-call app and passes them through to CallsManager.
27 * {@link InCallController} creates an instance of this class and passes it to the in-call app after
28 * binding to it. This adapter can receive commands and updates until the in-call app is unbound.
29 */
30class InCallAdapter extends IInCallAdapter.Stub {
Sailesh Nepal10ea4602014-04-01 17:23:32 -070031 private static final int MSG_ANSWER_CALL = 0;
32 private static final int MSG_REJECT_CALL = 1;
33 private static final int MSG_PLAY_DTMF_TONE = 2;
34 private static final int MSG_STOP_DTMF_TONE = 3;
35 private static final int MSG_POST_DIAL_CONTINUE = 4;
36 private static final int MSG_DISCONNECT_CALL = 5;
37 private static final int MSG_HOLD_CALL = 6;
38 private static final int MSG_UNHOLD_CALL = 7;
Sailesh Nepal84fa5f82014-04-02 11:01:11 -070039 private static final int MSG_HANDOFF_CALL = 8;
40 private static final int MSG_MUTE = 9;
41 private static final int MSG_SET_AUDIO_ROUTE = 10;
Santos Cordona1610702014-06-04 20:22:56 -070042 private static final int MSG_CONFERENCE = 11;
43 private static final int MSG_SPLIT_FROM_CONFERENCE = 12;
Sailesh Nepal10ea4602014-04-01 17:23:32 -070044
45 private final class InCallAdapterHandler extends Handler {
46 @Override
47 public void handleMessage(Message msg) {
Ihab Awadff7493a2014-06-10 13:47:44 -070048 Call call;
Sailesh Nepal10ea4602014-04-01 17:23:32 -070049 switch (msg.what) {
50 case MSG_ANSWER_CALL:
Ihab Awadff7493a2014-06-10 13:47:44 -070051 call = mCallIdMapper.getCall(msg.obj);
52 if (call != null) {
53 mCallsManager.answerCall(call);
54 } else {
55 Log.w(this, "answerCall, unknown call id: %s", msg.obj);
56 }
Sailesh Nepal10ea4602014-04-01 17:23:32 -070057 break;
58 case MSG_REJECT_CALL:
Ihab Awadff7493a2014-06-10 13:47:44 -070059 SomeArgs args = (SomeArgs) msg.obj;
60 try {
61 call = mCallIdMapper.getCall(args.arg1);
62 boolean rejectWithMessage = args.argi1 == 1;
63 String textMessage = (String) args.arg2;
64 if (call != null) {
65 mCallsManager.rejectCall(call, rejectWithMessage, textMessage);
66 } else {
67 Log.w(this, "setRingback, unknown call id: %s", args.arg1);
68 }
69 } finally {
70 args.recycle();
71 }
Sailesh Nepal10ea4602014-04-01 17:23:32 -070072 break;
73 case MSG_PLAY_DTMF_TONE:
Ihab Awadff7493a2014-06-10 13:47:44 -070074 call = mCallIdMapper.getCall(msg.obj);
75 if (call != null) {
76 mCallsManager.playDtmfTone(call, (char) msg.arg1);
77 } else {
78 Log.w(this, "playDtmfTone, unknown call id: %s", msg.obj);
79 }
Sailesh Nepal10ea4602014-04-01 17:23:32 -070080 break;
81 case MSG_STOP_DTMF_TONE:
Ihab Awadff7493a2014-06-10 13:47:44 -070082 call = mCallIdMapper.getCall(msg.obj);
83 if (call != null) {
84 mCallsManager.stopDtmfTone(call);
85 } else {
86 Log.w(this, "stopDtmfTone, unknown call id: %s", msg.obj);
87 }
Sailesh Nepal10ea4602014-04-01 17:23:32 -070088 break;
89 case MSG_POST_DIAL_CONTINUE:
Ihab Awadff7493a2014-06-10 13:47:44 -070090 call = mCallIdMapper.getCall(msg.obj);
Evan Charlton352105c2014-06-03 14:10:54 -070091 mCallsManager.postDialContinue(call, msg.arg1 == 1);
Ihab Awadff7493a2014-06-10 13:47:44 -070092 call = mCallIdMapper.getCall(msg.obj);
93 if (call != null) {
94 mCallsManager.postDialContinue(call, msg.arg1 == 1);
95 } else {
96 Log.w(this, "postDialContinue, unknown call id: %s", msg.obj);
97 }
Sailesh Nepal10ea4602014-04-01 17:23:32 -070098 break;
99 case MSG_DISCONNECT_CALL:
Ihab Awadff7493a2014-06-10 13:47:44 -0700100 call = mCallIdMapper.getCall(msg.obj);
101 if (call != null) {
102 mCallsManager.disconnectCall(call);
103 } else {
104 Log.w(this, "disconnectCall, unknown call id: %s", msg.obj);
105 }
Sailesh Nepal10ea4602014-04-01 17:23:32 -0700106 break;
107 case MSG_HOLD_CALL:
Ihab Awadff7493a2014-06-10 13:47:44 -0700108 call = mCallIdMapper.getCall(msg.obj);
109 if (call != null) {
110 mCallsManager.holdCall(call);
111 } else {
112 Log.w(this, "holdCall, unknown call id: %s", msg.obj);
113 }
Sailesh Nepal10ea4602014-04-01 17:23:32 -0700114 break;
115 case MSG_UNHOLD_CALL:
Ihab Awadff7493a2014-06-10 13:47:44 -0700116 call = mCallIdMapper.getCall(msg.obj);
117 if (call != null) {
118 mCallsManager.unholdCall(call);
119 } else {
120 Log.w(this, "unholdCall, unknown call id: %s", msg.obj);
121 }
Sailesh Nepal10ea4602014-04-01 17:23:32 -0700122 break;
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700123 case MSG_HANDOFF_CALL:
Ihab Awadff7493a2014-06-10 13:47:44 -0700124 call = mCallIdMapper.getCall(msg.obj);
125 if (call != null) {
126 mCallsManager.startHandoffForCall(call);
127 } else {
128 Log.w(this, "startHandoffForCall, unknown call id: %s", msg.obj);
129 }
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700130 break;
Sailesh Nepal10ea4602014-04-01 17:23:32 -0700131 case MSG_MUTE:
Ihab Awadff7493a2014-06-10 13:47:44 -0700132 mCallsManager.mute(msg.arg1 == 1);
Sailesh Nepal10ea4602014-04-01 17:23:32 -0700133 break;
134 case MSG_SET_AUDIO_ROUTE:
135 mCallsManager.setAudioRoute(msg.arg1);
136 break;
Santos Cordona1610702014-06-04 20:22:56 -0700137 case MSG_CONFERENCE:
Ihab Awadff7493a2014-06-10 13:47:44 -0700138 call = mCallIdMapper.getCall(msg.obj);
139 if (call != null) {
140 mCallsManager.conference(call);
141 } else {
142 Log.w(this, "conference, unknown call id: %s", msg.obj);
143 }
144
Santos Cordona1610702014-06-04 20:22:56 -0700145 break;
146 case MSG_SPLIT_FROM_CONFERENCE:
Ihab Awadff7493a2014-06-10 13:47:44 -0700147 call = mCallIdMapper.getCall(msg.obj);
148 if (call != null) {
149 call.splitFromConference();
150 } else {
151 Log.w(this, "splitFromConference, unknown call id: %s", msg.obj);
152 }
Santos Cordona1610702014-06-04 20:22:56 -0700153 break;
Sailesh Nepal10ea4602014-04-01 17:23:32 -0700154 }
155 }
156 }
Santos Cordon61d0f702014-02-19 02:52:23 -0800157
Santos Cordone3d76ab2014-01-28 17:25:20 -0800158 private final CallsManager mCallsManager;
Sailesh Nepal10ea4602014-04-01 17:23:32 -0700159 private final Handler mHandler = new InCallAdapterHandler();
Sailesh Nepale59bb192014-04-01 18:33:59 -0700160 private final CallIdMapper mCallIdMapper;
Santos Cordone3d76ab2014-01-28 17:25:20 -0800161
162 /** Persists the specified parameters. */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700163 public InCallAdapter(CallsManager callsManager, CallIdMapper callIdMapper) {
Sailesh Nepal10ea4602014-04-01 17:23:32 -0700164 ThreadUtil.checkOnMainThread();
Santos Cordone3d76ab2014-01-28 17:25:20 -0800165 mCallsManager = callsManager;
Sailesh Nepale59bb192014-04-01 18:33:59 -0700166 mCallIdMapper = callIdMapper;
Santos Cordone3d76ab2014-01-28 17:25:20 -0800167 }
168
169 /** {@inheritDoc} */
170 @Override
Sailesh Nepal10ea4602014-04-01 17:23:32 -0700171 public void answerCall(String callId) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800172 Log.d(this, "answerCall(%s)", callId);
Sailesh Nepale59bb192014-04-01 18:33:59 -0700173 mCallIdMapper.checkValidCallId(callId);
Sailesh Nepal10ea4602014-04-01 17:23:32 -0700174 mHandler.obtainMessage(MSG_ANSWER_CALL, callId).sendToTarget();
Santos Cordone3d76ab2014-01-28 17:25:20 -0800175 }
176
177 /** {@inheritDoc} */
178 @Override
Ihab Awadff7493a2014-06-10 13:47:44 -0700179 public void rejectCall(String callId, boolean rejectWithMessage, String textMessage) {
180 Log.d(this, "rejectCall(%s,%b,%s)", callId, rejectWithMessage, textMessage);
Sailesh Nepale59bb192014-04-01 18:33:59 -0700181 mCallIdMapper.checkValidCallId(callId);
Ihab Awadff7493a2014-06-10 13:47:44 -0700182 SomeArgs args = SomeArgs.obtain();
183 args.arg1 = callId;
184 args.argi1 = rejectWithMessage ? 1 : 0;
185 args.arg2 = textMessage;
186 mHandler.obtainMessage(MSG_REJECT_CALL, args).sendToTarget();
Santos Cordone3d76ab2014-01-28 17:25:20 -0800187 }
188
189 /** {@inheritDoc} */
Sailesh Nepal10ea4602014-04-01 17:23:32 -0700190 @Override
191 public void playDtmfTone(String callId, char digit) {
Ihab Awad74549ec2014-03-10 15:33:25 -0700192 Log.d(this, "playDtmfTone(%s,%c)", callId, digit);
Sailesh Nepale59bb192014-04-01 18:33:59 -0700193 mCallIdMapper.checkValidCallId(callId);
Sailesh Nepal10ea4602014-04-01 17:23:32 -0700194 mHandler.obtainMessage(MSG_PLAY_DTMF_TONE, (int) digit, 0, callId).sendToTarget();
Ihab Awad74549ec2014-03-10 15:33:25 -0700195 }
196
197 /** {@inheritDoc} */
Sailesh Nepal10ea4602014-04-01 17:23:32 -0700198 @Override
199 public void stopDtmfTone(String callId) {
Ihab Awad74549ec2014-03-10 15:33:25 -0700200 Log.d(this, "stopDtmfTone(%s)", callId);
Sailesh Nepale59bb192014-04-01 18:33:59 -0700201 mCallIdMapper.checkValidCallId(callId);
Sailesh Nepal10ea4602014-04-01 17:23:32 -0700202 mHandler.obtainMessage(MSG_STOP_DTMF_TONE, callId).sendToTarget();
Ihab Awad74549ec2014-03-10 15:33:25 -0700203 }
204
205 /** {@inheritDoc} */
Sailesh Nepal10ea4602014-04-01 17:23:32 -0700206 @Override
Evan Charlton352105c2014-06-03 14:10:54 -0700207 public void postDialContinue(String callId, boolean proceed) {
Ihab Awad74549ec2014-03-10 15:33:25 -0700208 Log.d(this, "postDialContinue(%s)", callId);
Sailesh Nepale59bb192014-04-01 18:33:59 -0700209 mCallIdMapper.checkValidCallId(callId);
Evan Charlton352105c2014-06-03 14:10:54 -0700210 mHandler.obtainMessage(MSG_POST_DIAL_CONTINUE, proceed ? 1 : 0, 0, callId).sendToTarget();
Ihab Awad74549ec2014-03-10 15:33:25 -0700211 }
212
213 /** {@inheritDoc} */
Santos Cordone3d76ab2014-01-28 17:25:20 -0800214 @Override
Sailesh Nepal10ea4602014-04-01 17:23:32 -0700215 public void disconnectCall(String callId) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700216 Log.v(this, "disconnectCall: %s", callId);
217 mCallIdMapper.checkValidCallId(callId);
Sailesh Nepal10ea4602014-04-01 17:23:32 -0700218 mHandler.obtainMessage(MSG_DISCONNECT_CALL, callId).sendToTarget();
Santos Cordone3d76ab2014-01-28 17:25:20 -0800219 }
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700220
221 /** {@inheritDoc} */
222 @Override
Sailesh Nepal10ea4602014-04-01 17:23:32 -0700223 public void holdCall(String callId) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700224 mCallIdMapper.checkValidCallId(callId);
Sailesh Nepal10ea4602014-04-01 17:23:32 -0700225 mHandler.obtainMessage(MSG_HOLD_CALL, callId).sendToTarget();
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700226 }
227
228 /** {@inheritDoc} */
229 @Override
Sailesh Nepal10ea4602014-04-01 17:23:32 -0700230 public void unholdCall(String callId) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700231 mCallIdMapper.checkValidCallId(callId);
Sailesh Nepal10ea4602014-04-01 17:23:32 -0700232 mHandler.obtainMessage(MSG_UNHOLD_CALL, callId).sendToTarget();
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700233 }
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700234
235 /** {@inheritDoc} */
236 @Override
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700237 public void handoffCall(String callId) {
238 mCallIdMapper.checkValidCallId(callId);
239 mHandler.obtainMessage(MSG_HANDOFF_CALL, callId).sendToTarget();
240 }
241
242 /** {@inheritDoc} */
243 @Override
Sailesh Nepal10ea4602014-04-01 17:23:32 -0700244 public void mute(boolean shouldMute) {
245 mHandler.obtainMessage(MSG_MUTE, shouldMute ? 1 : 0, 0).sendToTarget();
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700246 }
247
248 /** {@inheritDoc} */
249 @Override
Sailesh Nepal10ea4602014-04-01 17:23:32 -0700250 public void setAudioRoute(int route) {
251 mHandler.obtainMessage(MSG_SET_AUDIO_ROUTE, route, 0).sendToTarget();
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700252 }
Santos Cordon8f3282c2014-06-01 13:56:02 -0700253
254 /** ${inheritDoc} */
255 @Override
Santos Cordona1610702014-06-04 20:22:56 -0700256 public void conference(String callId) {
257 mHandler.obtainMessage(MSG_CONFERENCE, callId).sendToTarget();
Santos Cordon8f3282c2014-06-01 13:56:02 -0700258 }
259
260 /** ${inheritDoc} */
261 @Override
Santos Cordona1610702014-06-04 20:22:56 -0700262 public void splitFromConference(String callId) {
263 mHandler.obtainMessage(MSG_SPLIT_FROM_CONFERENCE, callId).sendToTarget();
Santos Cordon8f3282c2014-06-01 13:56:02 -0700264 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800265}