blob: 161d4a49fcf901cb2521e2b5374a3ca5a569b5e0 [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;
Nancy Chen53ceedc2014-07-08 18:56:51 -070021import android.telecomm.PhoneAccount;
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;
Sailesh Nepal10ea4602014-04-01 17:23:32 -070047
48 private final class InCallAdapterHandler extends Handler {
49 @Override
50 public void handleMessage(Message msg) {
Ihab Awadff7493a2014-06-10 13:47:44 -070051 Call call;
Sailesh Nepal10ea4602014-04-01 17:23:32 -070052 switch (msg.what) {
53 case MSG_ANSWER_CALL:
Ihab Awadff7493a2014-06-10 13:47:44 -070054 call = mCallIdMapper.getCall(msg.obj);
55 if (call != null) {
56 mCallsManager.answerCall(call);
57 } else {
58 Log.w(this, "answerCall, unknown call id: %s", msg.obj);
59 }
Sailesh Nepal10ea4602014-04-01 17:23:32 -070060 break;
Nancy Chen53ceedc2014-07-08 18:56:51 -070061 case MSG_REJECT_CALL: {
Ihab Awadff7493a2014-06-10 13:47:44 -070062 SomeArgs args = (SomeArgs) msg.obj;
63 try {
64 call = mCallIdMapper.getCall(args.arg1);
65 boolean rejectWithMessage = args.argi1 == 1;
66 String textMessage = (String) args.arg2;
67 if (call != null) {
68 mCallsManager.rejectCall(call, rejectWithMessage, textMessage);
69 } else {
70 Log.w(this, "setRingback, unknown call id: %s", args.arg1);
71 }
72 } finally {
73 args.recycle();
74 }
Sailesh Nepal10ea4602014-04-01 17:23:32 -070075 break;
Nancy Chen53ceedc2014-07-08 18:56:51 -070076 }
Sailesh Nepal10ea4602014-04-01 17:23:32 -070077 case MSG_PLAY_DTMF_TONE:
Ihab Awadff7493a2014-06-10 13:47:44 -070078 call = mCallIdMapper.getCall(msg.obj);
79 if (call != null) {
80 mCallsManager.playDtmfTone(call, (char) msg.arg1);
81 } else {
82 Log.w(this, "playDtmfTone, unknown call id: %s", msg.obj);
83 }
Sailesh Nepal10ea4602014-04-01 17:23:32 -070084 break;
85 case MSG_STOP_DTMF_TONE:
Ihab Awadff7493a2014-06-10 13:47:44 -070086 call = mCallIdMapper.getCall(msg.obj);
87 if (call != null) {
88 mCallsManager.stopDtmfTone(call);
89 } else {
90 Log.w(this, "stopDtmfTone, unknown call id: %s", msg.obj);
91 }
Sailesh Nepal10ea4602014-04-01 17:23:32 -070092 break;
93 case MSG_POST_DIAL_CONTINUE:
Ihab Awadff7493a2014-06-10 13:47:44 -070094 call = mCallIdMapper.getCall(msg.obj);
Evan Charlton352105c2014-06-03 14:10:54 -070095 mCallsManager.postDialContinue(call, msg.arg1 == 1);
Ihab Awadff7493a2014-06-10 13:47:44 -070096 call = mCallIdMapper.getCall(msg.obj);
97 if (call != null) {
98 mCallsManager.postDialContinue(call, msg.arg1 == 1);
99 } else {
100 Log.w(this, "postDialContinue, unknown call id: %s", msg.obj);
101 }
Sailesh Nepal10ea4602014-04-01 17:23:32 -0700102 break;
103 case MSG_DISCONNECT_CALL:
Ihab Awadff7493a2014-06-10 13:47:44 -0700104 call = mCallIdMapper.getCall(msg.obj);
105 if (call != null) {
106 mCallsManager.disconnectCall(call);
107 } else {
108 Log.w(this, "disconnectCall, unknown call id: %s", msg.obj);
109 }
Sailesh Nepal10ea4602014-04-01 17:23:32 -0700110 break;
111 case MSG_HOLD_CALL:
Ihab Awadff7493a2014-06-10 13:47:44 -0700112 call = mCallIdMapper.getCall(msg.obj);
113 if (call != null) {
114 mCallsManager.holdCall(call);
115 } else {
116 Log.w(this, "holdCall, unknown call id: %s", msg.obj);
117 }
Sailesh Nepal10ea4602014-04-01 17:23:32 -0700118 break;
119 case MSG_UNHOLD_CALL:
Ihab Awadff7493a2014-06-10 13:47:44 -0700120 call = mCallIdMapper.getCall(msg.obj);
121 if (call != null) {
122 mCallsManager.unholdCall(call);
123 } else {
124 Log.w(this, "unholdCall, unknown call id: %s", msg.obj);
125 }
Sailesh Nepal10ea4602014-04-01 17:23:32 -0700126 break;
Sailesh Nepal77da19e2014-07-02 21:31:16 -0700127 case MSG_PHONE_ACCOUNT_CLICKED:
Ihab Awadff7493a2014-06-10 13:47:44 -0700128 call = mCallIdMapper.getCall(msg.obj);
129 if (call != null) {
Sailesh Nepal77da19e2014-07-02 21:31:16 -0700130 mCallsManager.phoneAccountClicked(call);
Ihab Awadff7493a2014-06-10 13:47:44 -0700131 } else {
Sailesh Nepal77da19e2014-07-02 21:31:16 -0700132 Log.w(this, "phoneAccountClicked, unknown call id: %s", msg.obj);
Ihab Awadff7493a2014-06-10 13:47:44 -0700133 }
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700134 break;
Nancy Chen53ceedc2014-07-08 18:56:51 -0700135 case MSG_PHONE_ACCOUNT_SELECTED: {
136 SomeArgs args = (SomeArgs) msg.obj;
137 try {
138 call = mCallIdMapper.getCall(args.arg1);
139 if (call != null) {
140 mCallsManager.phoneAccountSelected(call, (PhoneAccount) args.arg2);
141 } else {
142 Log.w(this, "phoneAccountSelected, unknown call id: %s", args.arg1);
143 }
144 } finally {
145 args.recycle();
146 }
147 break;
148 }
Sailesh Nepal10ea4602014-04-01 17:23:32 -0700149 case MSG_MUTE:
Ihab Awadff7493a2014-06-10 13:47:44 -0700150 mCallsManager.mute(msg.arg1 == 1);
Sailesh Nepal10ea4602014-04-01 17:23:32 -0700151 break;
152 case MSG_SET_AUDIO_ROUTE:
153 mCallsManager.setAudioRoute(msg.arg1);
154 break;
Santos Cordona1610702014-06-04 20:22:56 -0700155 case MSG_CONFERENCE:
Ihab Awadff7493a2014-06-10 13:47:44 -0700156 call = mCallIdMapper.getCall(msg.obj);
157 if (call != null) {
158 mCallsManager.conference(call);
159 } else {
160 Log.w(this, "conference, unknown call id: %s", msg.obj);
161 }
Santos Cordona1610702014-06-04 20:22:56 -0700162 break;
163 case MSG_SPLIT_FROM_CONFERENCE:
Ihab Awadff7493a2014-06-10 13:47:44 -0700164 call = mCallIdMapper.getCall(msg.obj);
165 if (call != null) {
166 call.splitFromConference();
167 } else {
168 Log.w(this, "splitFromConference, unknown call id: %s", msg.obj);
169 }
Santos Cordona1610702014-06-04 20:22:56 -0700170 break;
Sailesh Nepale8ecb982014-07-11 17:19:42 -0700171 case MSG_SWAP_WITH_BACKGROUND_CALL:
172 call = mCallIdMapper.getCall(msg.obj);
173 if (call != null) {
174 call.swapWithBackgroundCall();
175 } else {
176 Log.w(this, "swapWithBackgroundCall, unknown call id: %s", msg.obj);
177 }
178 break;
Sailesh Nepal10ea4602014-04-01 17:23:32 -0700179 }
180 }
181 }
Santos Cordon61d0f702014-02-19 02:52:23 -0800182
Santos Cordone3d76ab2014-01-28 17:25:20 -0800183 private final CallsManager mCallsManager;
Sailesh Nepal10ea4602014-04-01 17:23:32 -0700184 private final Handler mHandler = new InCallAdapterHandler();
Sailesh Nepale59bb192014-04-01 18:33:59 -0700185 private final CallIdMapper mCallIdMapper;
Santos Cordone3d76ab2014-01-28 17:25:20 -0800186
187 /** Persists the specified parameters. */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700188 public InCallAdapter(CallsManager callsManager, CallIdMapper callIdMapper) {
Sailesh Nepal10ea4602014-04-01 17:23:32 -0700189 ThreadUtil.checkOnMainThread();
Santos Cordone3d76ab2014-01-28 17:25:20 -0800190 mCallsManager = callsManager;
Sailesh Nepale59bb192014-04-01 18:33:59 -0700191 mCallIdMapper = callIdMapper;
Santos Cordone3d76ab2014-01-28 17:25:20 -0800192 }
193
Santos Cordone3d76ab2014-01-28 17:25:20 -0800194 @Override
Sailesh Nepal10ea4602014-04-01 17:23:32 -0700195 public void answerCall(String callId) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800196 Log.d(this, "answerCall(%s)", callId);
Sailesh Nepale59bb192014-04-01 18:33:59 -0700197 mCallIdMapper.checkValidCallId(callId);
Sailesh Nepal10ea4602014-04-01 17:23:32 -0700198 mHandler.obtainMessage(MSG_ANSWER_CALL, callId).sendToTarget();
Santos Cordone3d76ab2014-01-28 17:25:20 -0800199 }
200
Santos Cordone3d76ab2014-01-28 17:25:20 -0800201 @Override
Ihab Awadff7493a2014-06-10 13:47:44 -0700202 public void rejectCall(String callId, boolean rejectWithMessage, String textMessage) {
203 Log.d(this, "rejectCall(%s,%b,%s)", callId, rejectWithMessage, textMessage);
Sailesh Nepale59bb192014-04-01 18:33:59 -0700204 mCallIdMapper.checkValidCallId(callId);
Ihab Awadff7493a2014-06-10 13:47:44 -0700205 SomeArgs args = SomeArgs.obtain();
206 args.arg1 = callId;
207 args.argi1 = rejectWithMessage ? 1 : 0;
208 args.arg2 = textMessage;
209 mHandler.obtainMessage(MSG_REJECT_CALL, args).sendToTarget();
Santos Cordone3d76ab2014-01-28 17:25:20 -0800210 }
211
Sailesh Nepal10ea4602014-04-01 17:23:32 -0700212 @Override
213 public void playDtmfTone(String callId, char digit) {
Ihab Awad74549ec2014-03-10 15:33:25 -0700214 Log.d(this, "playDtmfTone(%s,%c)", callId, digit);
Sailesh Nepale59bb192014-04-01 18:33:59 -0700215 mCallIdMapper.checkValidCallId(callId);
Sailesh Nepal10ea4602014-04-01 17:23:32 -0700216 mHandler.obtainMessage(MSG_PLAY_DTMF_TONE, (int) digit, 0, callId).sendToTarget();
Ihab Awad74549ec2014-03-10 15:33:25 -0700217 }
218
Sailesh Nepal10ea4602014-04-01 17:23:32 -0700219 @Override
220 public void stopDtmfTone(String callId) {
Ihab Awad74549ec2014-03-10 15:33:25 -0700221 Log.d(this, "stopDtmfTone(%s)", callId);
Sailesh Nepale59bb192014-04-01 18:33:59 -0700222 mCallIdMapper.checkValidCallId(callId);
Sailesh Nepal10ea4602014-04-01 17:23:32 -0700223 mHandler.obtainMessage(MSG_STOP_DTMF_TONE, callId).sendToTarget();
Ihab Awad74549ec2014-03-10 15:33:25 -0700224 }
225
Sailesh Nepal10ea4602014-04-01 17:23:32 -0700226 @Override
Evan Charlton352105c2014-06-03 14:10:54 -0700227 public void postDialContinue(String callId, boolean proceed) {
Ihab Awad74549ec2014-03-10 15:33:25 -0700228 Log.d(this, "postDialContinue(%s)", callId);
Sailesh Nepale59bb192014-04-01 18:33:59 -0700229 mCallIdMapper.checkValidCallId(callId);
Evan Charlton352105c2014-06-03 14:10:54 -0700230 mHandler.obtainMessage(MSG_POST_DIAL_CONTINUE, proceed ? 1 : 0, 0, callId).sendToTarget();
Ihab Awad74549ec2014-03-10 15:33:25 -0700231 }
232
Santos Cordone3d76ab2014-01-28 17:25:20 -0800233 @Override
Sailesh Nepal10ea4602014-04-01 17:23:32 -0700234 public void disconnectCall(String callId) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700235 Log.v(this, "disconnectCall: %s", callId);
236 mCallIdMapper.checkValidCallId(callId);
Sailesh Nepal10ea4602014-04-01 17:23:32 -0700237 mHandler.obtainMessage(MSG_DISCONNECT_CALL, callId).sendToTarget();
Santos Cordone3d76ab2014-01-28 17:25:20 -0800238 }
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700239
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700240 @Override
Sailesh Nepal10ea4602014-04-01 17:23:32 -0700241 public void holdCall(String callId) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700242 mCallIdMapper.checkValidCallId(callId);
Sailesh Nepal10ea4602014-04-01 17:23:32 -0700243 mHandler.obtainMessage(MSG_HOLD_CALL, callId).sendToTarget();
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700244 }
245
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700246 @Override
Sailesh Nepal10ea4602014-04-01 17:23:32 -0700247 public void unholdCall(String callId) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700248 mCallIdMapper.checkValidCallId(callId);
Sailesh Nepal10ea4602014-04-01 17:23:32 -0700249 mHandler.obtainMessage(MSG_UNHOLD_CALL, callId).sendToTarget();
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700250 }
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700251
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700252 @Override
Sailesh Nepal77da19e2014-07-02 21:31:16 -0700253 public void phoneAccountClicked(String callId) {
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700254 mCallIdMapper.checkValidCallId(callId);
Sailesh Nepal77da19e2014-07-02 21:31:16 -0700255 mHandler.obtainMessage(MSG_PHONE_ACCOUNT_CLICKED, callId).sendToTarget();
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700256 }
257
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700258 @Override
Nancy Chen53ceedc2014-07-08 18:56:51 -0700259 public void phoneAccountSelected(String callId, PhoneAccount account) {
260 mCallIdMapper.checkValidCallId(callId);
261 SomeArgs args = SomeArgs.obtain();
262 args.arg1 = callId;
263 args.arg2 = account;
264 mHandler.obtainMessage(MSG_PHONE_ACCOUNT_SELECTED, args).sendToTarget();
265 }
266
267 @Override
Sailesh Nepal10ea4602014-04-01 17:23:32 -0700268 public void mute(boolean shouldMute) {
269 mHandler.obtainMessage(MSG_MUTE, shouldMute ? 1 : 0, 0).sendToTarget();
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700270 }
271
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700272 @Override
Sailesh Nepal10ea4602014-04-01 17:23:32 -0700273 public void setAudioRoute(int route) {
274 mHandler.obtainMessage(MSG_SET_AUDIO_ROUTE, route, 0).sendToTarget();
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700275 }
Santos Cordon8f3282c2014-06-01 13:56:02 -0700276
Santos Cordon8f3282c2014-06-01 13:56:02 -0700277 @Override
Santos Cordona1610702014-06-04 20:22:56 -0700278 public void conference(String callId) {
279 mHandler.obtainMessage(MSG_CONFERENCE, callId).sendToTarget();
Santos Cordon8f3282c2014-06-01 13:56:02 -0700280 }
281
Santos Cordon8f3282c2014-06-01 13:56:02 -0700282 @Override
Santos Cordona1610702014-06-04 20:22:56 -0700283 public void splitFromConference(String callId) {
284 mHandler.obtainMessage(MSG_SPLIT_FROM_CONFERENCE, callId).sendToTarget();
Santos Cordon8f3282c2014-06-01 13:56:02 -0700285 }
Sailesh Nepale8ecb982014-07-11 17:19:42 -0700286
287 @Override
288 public void swapWithBackgroundCall(String callId) {
289 mHandler.obtainMessage(MSG_SWAP_WITH_BACKGROUND_CALL, callId).sendToTarget();
290 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800291}