blob: 0acb3f59adc9ffb2ff4f603ce979e0dfda2349a5 [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;
Evan Charlton89176372014-07-19 18:23:09 -070021import android.telecomm.PhoneAccountHandle;
Sailesh Nepala439e1b2014-03-11 18:19:58 -070022
Evan Charlton352105c2014-06-03 14:10:54 -070023import com.android.internal.os.SomeArgs;
Sailesh Nepala439e1b2014-03-11 18:19:58 -070024import com.android.internal.telecomm.IInCallAdapter;
Ben Gilad9f2bed32013-12-12 17:43:26 -080025
Santos Cordone3d76ab2014-01-28 17:25:20 -080026/**
27 * Receives call commands and updates from in-call app and passes them through to CallsManager.
28 * {@link InCallController} creates an instance of this class and passes it to the in-call app after
29 * binding to it. This adapter can receive commands and updates until the in-call app is unbound.
30 */
31class InCallAdapter extends IInCallAdapter.Stub {
Sailesh Nepal10ea4602014-04-01 17:23:32 -070032 private static final int MSG_ANSWER_CALL = 0;
33 private static final int MSG_REJECT_CALL = 1;
34 private static final int MSG_PLAY_DTMF_TONE = 2;
35 private static final int MSG_STOP_DTMF_TONE = 3;
36 private static final int MSG_POST_DIAL_CONTINUE = 4;
37 private static final int MSG_DISCONNECT_CALL = 5;
38 private static final int MSG_HOLD_CALL = 6;
39 private static final int MSG_UNHOLD_CALL = 7;
Sailesh Nepal77da19e2014-07-02 21:31:16 -070040 private static final int MSG_PHONE_ACCOUNT_CLICKED = 8;
Sailesh Nepal84fa5f82014-04-02 11:01:11 -070041 private static final int MSG_MUTE = 9;
42 private static final int MSG_SET_AUDIO_ROUTE = 10;
Santos Cordona1610702014-06-04 20:22:56 -070043 private static final int MSG_CONFERENCE = 11;
44 private static final int MSG_SPLIT_FROM_CONFERENCE = 12;
Sailesh Nepale8ecb982014-07-11 17:19:42 -070045 private static final int MSG_SWAP_WITH_BACKGROUND_CALL = 13;
Nancy Chen53ceedc2014-07-08 18:56:51 -070046 private static final int MSG_PHONE_ACCOUNT_SELECTED = 14;
Yorke Leed1346872014-07-28 14:38:45 -070047 private static final int MSG_TURN_ON_PROXIMITY_SENSOR = 15;
48 private static final int MSG_TURN_OFF_PROXIMITY_SENSOR = 16;
Sailesh Nepal10ea4602014-04-01 17:23:32 -070049
50 private final class InCallAdapterHandler extends Handler {
51 @Override
52 public void handleMessage(Message msg) {
Ihab Awadff7493a2014-06-10 13:47:44 -070053 Call call;
Sailesh Nepal10ea4602014-04-01 17:23:32 -070054 switch (msg.what) {
Andrew Lee38931d02014-07-16 10:17:36 -070055 case MSG_ANSWER_CALL: {
56 SomeArgs args = (SomeArgs) msg.obj;
57 try {
58 call = mCallIdMapper.getCall(args.arg1);
59 int videoState = (int) args.arg2;
60 if (call != null) {
61 mCallsManager.answerCall(call, videoState);
62 } else {
63 Log.w(this, "answerCall, unknown call id: %s", msg.obj);
64 }
65 } finally {
66 args.recycle();
Ihab Awadff7493a2014-06-10 13:47:44 -070067 }
Sailesh Nepal10ea4602014-04-01 17:23:32 -070068 break;
Andrew Lee38931d02014-07-16 10:17:36 -070069 }
Nancy Chen53ceedc2014-07-08 18:56:51 -070070 case MSG_REJECT_CALL: {
Ihab Awadff7493a2014-06-10 13:47:44 -070071 SomeArgs args = (SomeArgs) msg.obj;
72 try {
73 call = mCallIdMapper.getCall(args.arg1);
74 boolean rejectWithMessage = args.argi1 == 1;
75 String textMessage = (String) args.arg2;
76 if (call != null) {
77 mCallsManager.rejectCall(call, rejectWithMessage, textMessage);
78 } else {
79 Log.w(this, "setRingback, unknown call id: %s", args.arg1);
80 }
81 } finally {
82 args.recycle();
83 }
Sailesh Nepal10ea4602014-04-01 17:23:32 -070084 break;
Nancy Chen53ceedc2014-07-08 18:56:51 -070085 }
Sailesh Nepal10ea4602014-04-01 17:23:32 -070086 case MSG_PLAY_DTMF_TONE:
Ihab Awadff7493a2014-06-10 13:47:44 -070087 call = mCallIdMapper.getCall(msg.obj);
88 if (call != null) {
89 mCallsManager.playDtmfTone(call, (char) msg.arg1);
90 } else {
91 Log.w(this, "playDtmfTone, unknown call id: %s", msg.obj);
92 }
Sailesh Nepal10ea4602014-04-01 17:23:32 -070093 break;
94 case MSG_STOP_DTMF_TONE:
Ihab Awadff7493a2014-06-10 13:47:44 -070095 call = mCallIdMapper.getCall(msg.obj);
96 if (call != null) {
97 mCallsManager.stopDtmfTone(call);
98 } else {
99 Log.w(this, "stopDtmfTone, unknown call id: %s", msg.obj);
100 }
Sailesh Nepal10ea4602014-04-01 17:23:32 -0700101 break;
102 case MSG_POST_DIAL_CONTINUE:
Ihab Awadff7493a2014-06-10 13:47:44 -0700103 call = mCallIdMapper.getCall(msg.obj);
Evan Charlton352105c2014-06-03 14:10:54 -0700104 mCallsManager.postDialContinue(call, msg.arg1 == 1);
Ihab Awadff7493a2014-06-10 13:47:44 -0700105 call = mCallIdMapper.getCall(msg.obj);
106 if (call != null) {
107 mCallsManager.postDialContinue(call, msg.arg1 == 1);
108 } else {
109 Log.w(this, "postDialContinue, unknown call id: %s", msg.obj);
110 }
Sailesh Nepal10ea4602014-04-01 17:23:32 -0700111 break;
112 case MSG_DISCONNECT_CALL:
Ihab Awadff7493a2014-06-10 13:47:44 -0700113 call = mCallIdMapper.getCall(msg.obj);
114 if (call != null) {
115 mCallsManager.disconnectCall(call);
116 } else {
117 Log.w(this, "disconnectCall, unknown call id: %s", msg.obj);
118 }
Sailesh Nepal10ea4602014-04-01 17:23:32 -0700119 break;
120 case MSG_HOLD_CALL:
Ihab Awadff7493a2014-06-10 13:47:44 -0700121 call = mCallIdMapper.getCall(msg.obj);
122 if (call != null) {
123 mCallsManager.holdCall(call);
124 } else {
125 Log.w(this, "holdCall, unknown call id: %s", msg.obj);
126 }
Sailesh Nepal10ea4602014-04-01 17:23:32 -0700127 break;
128 case MSG_UNHOLD_CALL:
Ihab Awadff7493a2014-06-10 13:47:44 -0700129 call = mCallIdMapper.getCall(msg.obj);
130 if (call != null) {
131 mCallsManager.unholdCall(call);
132 } else {
133 Log.w(this, "unholdCall, unknown call id: %s", msg.obj);
134 }
Sailesh Nepal10ea4602014-04-01 17:23:32 -0700135 break;
Sailesh Nepal77da19e2014-07-02 21:31:16 -0700136 case MSG_PHONE_ACCOUNT_CLICKED:
Ihab Awadff7493a2014-06-10 13:47:44 -0700137 call = mCallIdMapper.getCall(msg.obj);
138 if (call != null) {
Sailesh Nepal77da19e2014-07-02 21:31:16 -0700139 mCallsManager.phoneAccountClicked(call);
Ihab Awadff7493a2014-06-10 13:47:44 -0700140 } else {
Sailesh Nepal77da19e2014-07-02 21:31:16 -0700141 Log.w(this, "phoneAccountClicked, unknown call id: %s", msg.obj);
Ihab Awadff7493a2014-06-10 13:47:44 -0700142 }
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700143 break;
Nancy Chen53ceedc2014-07-08 18:56:51 -0700144 case MSG_PHONE_ACCOUNT_SELECTED: {
145 SomeArgs args = (SomeArgs) msg.obj;
146 try {
147 call = mCallIdMapper.getCall(args.arg1);
148 if (call != null) {
Evan Charlton89176372014-07-19 18:23:09 -0700149 mCallsManager.phoneAccountSelected(call, (PhoneAccountHandle) args.arg2);
Nancy Chen53ceedc2014-07-08 18:56:51 -0700150 } else {
151 Log.w(this, "phoneAccountSelected, unknown call id: %s", args.arg1);
152 }
153 } finally {
154 args.recycle();
155 }
156 break;
157 }
Sailesh Nepal10ea4602014-04-01 17:23:32 -0700158 case MSG_MUTE:
Ihab Awadff7493a2014-06-10 13:47:44 -0700159 mCallsManager.mute(msg.arg1 == 1);
Sailesh Nepal10ea4602014-04-01 17:23:32 -0700160 break;
161 case MSG_SET_AUDIO_ROUTE:
162 mCallsManager.setAudioRoute(msg.arg1);
163 break;
Santos Cordona1610702014-06-04 20:22:56 -0700164 case MSG_CONFERENCE:
Ihab Awadff7493a2014-06-10 13:47:44 -0700165 call = mCallIdMapper.getCall(msg.obj);
166 if (call != null) {
167 mCallsManager.conference(call);
168 } else {
169 Log.w(this, "conference, unknown call id: %s", msg.obj);
170 }
Santos Cordona1610702014-06-04 20:22:56 -0700171 break;
172 case MSG_SPLIT_FROM_CONFERENCE:
Ihab Awadff7493a2014-06-10 13:47:44 -0700173 call = mCallIdMapper.getCall(msg.obj);
174 if (call != null) {
175 call.splitFromConference();
176 } else {
177 Log.w(this, "splitFromConference, unknown call id: %s", msg.obj);
178 }
Santos Cordona1610702014-06-04 20:22:56 -0700179 break;
Sailesh Nepale8ecb982014-07-11 17:19:42 -0700180 case MSG_SWAP_WITH_BACKGROUND_CALL:
181 call = mCallIdMapper.getCall(msg.obj);
182 if (call != null) {
183 call.swapWithBackgroundCall();
184 } else {
185 Log.w(this, "swapWithBackgroundCall, unknown call id: %s", msg.obj);
186 }
187 break;
Yorke Leed1346872014-07-28 14:38:45 -0700188 case MSG_TURN_ON_PROXIMITY_SENSOR:
189 mCallsManager.turnOnProximitySensor();
190 break;
191 case MSG_TURN_OFF_PROXIMITY_SENSOR:
192 mCallsManager.turnOffProximitySensor((boolean) msg.obj);
193 break;
Sailesh Nepal10ea4602014-04-01 17:23:32 -0700194 }
195 }
196 }
Santos Cordon61d0f702014-02-19 02:52:23 -0800197
Santos Cordone3d76ab2014-01-28 17:25:20 -0800198 private final CallsManager mCallsManager;
Sailesh Nepal10ea4602014-04-01 17:23:32 -0700199 private final Handler mHandler = new InCallAdapterHandler();
Sailesh Nepale59bb192014-04-01 18:33:59 -0700200 private final CallIdMapper mCallIdMapper;
Santos Cordone3d76ab2014-01-28 17:25:20 -0800201
202 /** Persists the specified parameters. */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700203 public InCallAdapter(CallsManager callsManager, CallIdMapper callIdMapper) {
Sailesh Nepal10ea4602014-04-01 17:23:32 -0700204 ThreadUtil.checkOnMainThread();
Santos Cordone3d76ab2014-01-28 17:25:20 -0800205 mCallsManager = callsManager;
Sailesh Nepale59bb192014-04-01 18:33:59 -0700206 mCallIdMapper = callIdMapper;
Santos Cordone3d76ab2014-01-28 17:25:20 -0800207 }
208
Santos Cordone3d76ab2014-01-28 17:25:20 -0800209 @Override
Andrew Lee38931d02014-07-16 10:17:36 -0700210 public void answerCall(String callId, int videoState) {
211 Log.d(this, "answerCall(%s,%d)", callId, videoState);
Sailesh Nepale59bb192014-04-01 18:33:59 -0700212 mCallIdMapper.checkValidCallId(callId);
Andrew Lee38931d02014-07-16 10:17:36 -0700213 SomeArgs args = SomeArgs.obtain();
214 args.arg1 = callId;
215 args.arg2 = videoState;
216 mHandler.obtainMessage(MSG_ANSWER_CALL, args).sendToTarget();
Santos Cordone3d76ab2014-01-28 17:25:20 -0800217 }
218
Santos Cordone3d76ab2014-01-28 17:25:20 -0800219 @Override
Ihab Awadff7493a2014-06-10 13:47:44 -0700220 public void rejectCall(String callId, boolean rejectWithMessage, String textMessage) {
221 Log.d(this, "rejectCall(%s,%b,%s)", callId, rejectWithMessage, textMessage);
Sailesh Nepale59bb192014-04-01 18:33:59 -0700222 mCallIdMapper.checkValidCallId(callId);
Ihab Awadff7493a2014-06-10 13:47:44 -0700223 SomeArgs args = SomeArgs.obtain();
224 args.arg1 = callId;
225 args.argi1 = rejectWithMessage ? 1 : 0;
226 args.arg2 = textMessage;
227 mHandler.obtainMessage(MSG_REJECT_CALL, args).sendToTarget();
Santos Cordone3d76ab2014-01-28 17:25:20 -0800228 }
229
Sailesh Nepal10ea4602014-04-01 17:23:32 -0700230 @Override
231 public void playDtmfTone(String callId, char digit) {
Ihab Awad74549ec2014-03-10 15:33:25 -0700232 Log.d(this, "playDtmfTone(%s,%c)", callId, digit);
Sailesh Nepale59bb192014-04-01 18:33:59 -0700233 mCallIdMapper.checkValidCallId(callId);
Sailesh Nepal10ea4602014-04-01 17:23:32 -0700234 mHandler.obtainMessage(MSG_PLAY_DTMF_TONE, (int) digit, 0, callId).sendToTarget();
Ihab Awad74549ec2014-03-10 15:33:25 -0700235 }
236
Sailesh Nepal10ea4602014-04-01 17:23:32 -0700237 @Override
238 public void stopDtmfTone(String callId) {
Ihab Awad74549ec2014-03-10 15:33:25 -0700239 Log.d(this, "stopDtmfTone(%s)", callId);
Sailesh Nepale59bb192014-04-01 18:33:59 -0700240 mCallIdMapper.checkValidCallId(callId);
Sailesh Nepal10ea4602014-04-01 17:23:32 -0700241 mHandler.obtainMessage(MSG_STOP_DTMF_TONE, callId).sendToTarget();
Ihab Awad74549ec2014-03-10 15:33:25 -0700242 }
243
Sailesh Nepal10ea4602014-04-01 17:23:32 -0700244 @Override
Evan Charlton352105c2014-06-03 14:10:54 -0700245 public void postDialContinue(String callId, boolean proceed) {
Ihab Awad74549ec2014-03-10 15:33:25 -0700246 Log.d(this, "postDialContinue(%s)", callId);
Sailesh Nepale59bb192014-04-01 18:33:59 -0700247 mCallIdMapper.checkValidCallId(callId);
Evan Charlton352105c2014-06-03 14:10:54 -0700248 mHandler.obtainMessage(MSG_POST_DIAL_CONTINUE, proceed ? 1 : 0, 0, callId).sendToTarget();
Ihab Awad74549ec2014-03-10 15:33:25 -0700249 }
250
Santos Cordone3d76ab2014-01-28 17:25:20 -0800251 @Override
Sailesh Nepal10ea4602014-04-01 17:23:32 -0700252 public void disconnectCall(String callId) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700253 Log.v(this, "disconnectCall: %s", callId);
254 mCallIdMapper.checkValidCallId(callId);
Sailesh Nepal10ea4602014-04-01 17:23:32 -0700255 mHandler.obtainMessage(MSG_DISCONNECT_CALL, callId).sendToTarget();
Santos Cordone3d76ab2014-01-28 17:25:20 -0800256 }
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700257
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700258 @Override
Sailesh Nepal10ea4602014-04-01 17:23:32 -0700259 public void holdCall(String callId) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700260 mCallIdMapper.checkValidCallId(callId);
Sailesh Nepal10ea4602014-04-01 17:23:32 -0700261 mHandler.obtainMessage(MSG_HOLD_CALL, callId).sendToTarget();
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700262 }
263
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700264 @Override
Sailesh Nepal10ea4602014-04-01 17:23:32 -0700265 public void unholdCall(String callId) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700266 mCallIdMapper.checkValidCallId(callId);
Sailesh Nepal10ea4602014-04-01 17:23:32 -0700267 mHandler.obtainMessage(MSG_UNHOLD_CALL, callId).sendToTarget();
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700268 }
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700269
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700270 @Override
Sailesh Nepal77da19e2014-07-02 21:31:16 -0700271 public void phoneAccountClicked(String callId) {
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700272 mCallIdMapper.checkValidCallId(callId);
Sailesh Nepal77da19e2014-07-02 21:31:16 -0700273 mHandler.obtainMessage(MSG_PHONE_ACCOUNT_CLICKED, callId).sendToTarget();
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700274 }
275
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700276 @Override
Evan Charlton89176372014-07-19 18:23:09 -0700277 public void phoneAccountSelected(String callId, PhoneAccountHandle accountHandle) {
Nancy Chen53ceedc2014-07-08 18:56:51 -0700278 mCallIdMapper.checkValidCallId(callId);
279 SomeArgs args = SomeArgs.obtain();
280 args.arg1 = callId;
Evan Charlton89176372014-07-19 18:23:09 -0700281 args.arg2 = accountHandle;
Nancy Chen53ceedc2014-07-08 18:56:51 -0700282 mHandler.obtainMessage(MSG_PHONE_ACCOUNT_SELECTED, args).sendToTarget();
283 }
284
285 @Override
Sailesh Nepal10ea4602014-04-01 17:23:32 -0700286 public void mute(boolean shouldMute) {
287 mHandler.obtainMessage(MSG_MUTE, shouldMute ? 1 : 0, 0).sendToTarget();
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700288 }
289
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700290 @Override
Sailesh Nepal10ea4602014-04-01 17:23:32 -0700291 public void setAudioRoute(int route) {
292 mHandler.obtainMessage(MSG_SET_AUDIO_ROUTE, route, 0).sendToTarget();
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700293 }
Santos Cordon8f3282c2014-06-01 13:56:02 -0700294
Santos Cordon8f3282c2014-06-01 13:56:02 -0700295 @Override
Santos Cordona1610702014-06-04 20:22:56 -0700296 public void conference(String callId) {
297 mHandler.obtainMessage(MSG_CONFERENCE, callId).sendToTarget();
Santos Cordon8f3282c2014-06-01 13:56:02 -0700298 }
299
Santos Cordon8f3282c2014-06-01 13:56:02 -0700300 @Override
Santos Cordona1610702014-06-04 20:22:56 -0700301 public void splitFromConference(String callId) {
302 mHandler.obtainMessage(MSG_SPLIT_FROM_CONFERENCE, callId).sendToTarget();
Santos Cordon8f3282c2014-06-01 13:56:02 -0700303 }
Sailesh Nepale8ecb982014-07-11 17:19:42 -0700304
305 @Override
306 public void swapWithBackgroundCall(String callId) {
307 mHandler.obtainMessage(MSG_SWAP_WITH_BACKGROUND_CALL, callId).sendToTarget();
308 }
Yorke Leed1346872014-07-28 14:38:45 -0700309
310 @Override
311 public void turnOnProximitySensor() {
312 mHandler.obtainMessage(MSG_TURN_ON_PROXIMITY_SENSOR).sendToTarget();
313 }
314
315 @Override
316 public void turnOffProximitySensor(boolean screenOnImmediately) {
317 mHandler.obtainMessage(MSG_TURN_OFF_PROXIMITY_SENSOR, screenOnImmediately).sendToTarget();
318 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800319}