blob: da7cf186e9e4c779fe803e6a605d509ae11f7244 [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
22import com.android.internal.telecomm.IInCallAdapter;
Ben Gilad9f2bed32013-12-12 17:43:26 -080023
Santos Cordone3d76ab2014-01-28 17:25:20 -080024/**
25 * Receives call commands and updates from in-call app and passes them through to CallsManager.
26 * {@link InCallController} creates an instance of this class and passes it to the in-call app after
27 * binding to it. This adapter can receive commands and updates until the in-call app is unbound.
28 */
29class InCallAdapter extends IInCallAdapter.Stub {
Sailesh Nepal10ea4602014-04-01 17:23:32 -070030 private static final int MSG_ANSWER_CALL = 0;
31 private static final int MSG_REJECT_CALL = 1;
32 private static final int MSG_PLAY_DTMF_TONE = 2;
33 private static final int MSG_STOP_DTMF_TONE = 3;
34 private static final int MSG_POST_DIAL_CONTINUE = 4;
35 private static final int MSG_DISCONNECT_CALL = 5;
36 private static final int MSG_HOLD_CALL = 6;
37 private static final int MSG_UNHOLD_CALL = 7;
Sailesh Nepal84fa5f82014-04-02 11:01:11 -070038 private static final int MSG_HANDOFF_CALL = 8;
39 private static final int MSG_MUTE = 9;
40 private static final int MSG_SET_AUDIO_ROUTE = 10;
Sailesh Nepal10ea4602014-04-01 17:23:32 -070041
42 private final class InCallAdapterHandler extends Handler {
43 @Override
44 public void handleMessage(Message msg) {
Sailesh Nepale59bb192014-04-01 18:33:59 -070045 Call call = null;
46 if (msg.obj != null) {
47 call = mCallIdMapper.getCall(msg.obj);
48 if (call == null) {
49 Log.w(this, "Unknown call id: %s, msg: %d", msg.obj, msg.what);
50 return;
51 }
52 }
53
Sailesh Nepal10ea4602014-04-01 17:23:32 -070054 switch (msg.what) {
55 case MSG_ANSWER_CALL:
Sailesh Nepale59bb192014-04-01 18:33:59 -070056 mCallsManager.answerCall(call);
Sailesh Nepal10ea4602014-04-01 17:23:32 -070057 break;
58 case MSG_REJECT_CALL:
Sailesh Nepale59bb192014-04-01 18:33:59 -070059 mCallsManager.rejectCall(call);
Sailesh Nepal10ea4602014-04-01 17:23:32 -070060 break;
61 case MSG_PLAY_DTMF_TONE:
Sailesh Nepale59bb192014-04-01 18:33:59 -070062 mCallsManager.playDtmfTone(call, (char) msg.arg1);
Sailesh Nepal10ea4602014-04-01 17:23:32 -070063 break;
64 case MSG_STOP_DTMF_TONE:
Sailesh Nepale59bb192014-04-01 18:33:59 -070065 mCallsManager.stopDtmfTone(call);
Sailesh Nepal10ea4602014-04-01 17:23:32 -070066 break;
67 case MSG_POST_DIAL_CONTINUE:
Sailesh Nepale59bb192014-04-01 18:33:59 -070068 mCallsManager.postDialContinue(call);
Sailesh Nepal10ea4602014-04-01 17:23:32 -070069 break;
70 case MSG_DISCONNECT_CALL:
Sailesh Nepale59bb192014-04-01 18:33:59 -070071 mCallsManager.disconnectCall(call);
Sailesh Nepal10ea4602014-04-01 17:23:32 -070072 break;
73 case MSG_HOLD_CALL:
Sailesh Nepale59bb192014-04-01 18:33:59 -070074 mCallsManager.holdCall(call);
Sailesh Nepal10ea4602014-04-01 17:23:32 -070075 break;
76 case MSG_UNHOLD_CALL:
Sailesh Nepale59bb192014-04-01 18:33:59 -070077 mCallsManager.unholdCall(call);
Sailesh Nepal10ea4602014-04-01 17:23:32 -070078 break;
Sailesh Nepal84fa5f82014-04-02 11:01:11 -070079 case MSG_HANDOFF_CALL:
80 mCallsManager.startHandoffForCall(call);
81 break;
Sailesh Nepal10ea4602014-04-01 17:23:32 -070082 case MSG_MUTE:
83 mCallsManager.mute(msg.arg1 == 1 ? true : false);
84 break;
85 case MSG_SET_AUDIO_ROUTE:
86 mCallsManager.setAudioRoute(msg.arg1);
87 break;
88 }
89 }
90 }
Santos Cordon61d0f702014-02-19 02:52:23 -080091
Santos Cordone3d76ab2014-01-28 17:25:20 -080092 private final CallsManager mCallsManager;
Sailesh Nepal10ea4602014-04-01 17:23:32 -070093 private final Handler mHandler = new InCallAdapterHandler();
Sailesh Nepale59bb192014-04-01 18:33:59 -070094 private final CallIdMapper mCallIdMapper;
Santos Cordone3d76ab2014-01-28 17:25:20 -080095
96 /** Persists the specified parameters. */
Sailesh Nepale59bb192014-04-01 18:33:59 -070097 public InCallAdapter(CallsManager callsManager, CallIdMapper callIdMapper) {
Sailesh Nepal10ea4602014-04-01 17:23:32 -070098 ThreadUtil.checkOnMainThread();
Santos Cordone3d76ab2014-01-28 17:25:20 -080099 mCallsManager = callsManager;
Sailesh Nepale59bb192014-04-01 18:33:59 -0700100 mCallIdMapper = callIdMapper;
Santos Cordone3d76ab2014-01-28 17:25:20 -0800101 }
102
103 /** {@inheritDoc} */
104 @Override
Sailesh Nepal10ea4602014-04-01 17:23:32 -0700105 public void answerCall(String callId) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800106 Log.d(this, "answerCall(%s)", callId);
Sailesh Nepale59bb192014-04-01 18:33:59 -0700107 mCallIdMapper.checkValidCallId(callId);
Sailesh Nepal10ea4602014-04-01 17:23:32 -0700108 mHandler.obtainMessage(MSG_ANSWER_CALL, callId).sendToTarget();
Santos Cordone3d76ab2014-01-28 17:25:20 -0800109 }
110
111 /** {@inheritDoc} */
112 @Override
Sailesh Nepal10ea4602014-04-01 17:23:32 -0700113 public void rejectCall(String callId) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800114 Log.d(this, "rejectCall(%s)", callId);
Sailesh Nepale59bb192014-04-01 18:33:59 -0700115 mCallIdMapper.checkValidCallId(callId);
Sailesh Nepal10ea4602014-04-01 17:23:32 -0700116 mHandler.obtainMessage(MSG_REJECT_CALL, callId).sendToTarget();
Santos Cordone3d76ab2014-01-28 17:25:20 -0800117 }
118
119 /** {@inheritDoc} */
Sailesh Nepal10ea4602014-04-01 17:23:32 -0700120 @Override
121 public void playDtmfTone(String callId, char digit) {
Ihab Awad74549ec2014-03-10 15:33:25 -0700122 Log.d(this, "playDtmfTone(%s,%c)", callId, digit);
Sailesh Nepale59bb192014-04-01 18:33:59 -0700123 mCallIdMapper.checkValidCallId(callId);
Sailesh Nepal10ea4602014-04-01 17:23:32 -0700124 mHandler.obtainMessage(MSG_PLAY_DTMF_TONE, (int) digit, 0, callId).sendToTarget();
Ihab Awad74549ec2014-03-10 15:33:25 -0700125 }
126
127 /** {@inheritDoc} */
Sailesh Nepal10ea4602014-04-01 17:23:32 -0700128 @Override
129 public void stopDtmfTone(String callId) {
Ihab Awad74549ec2014-03-10 15:33:25 -0700130 Log.d(this, "stopDtmfTone(%s)", callId);
Sailesh Nepale59bb192014-04-01 18:33:59 -0700131 mCallIdMapper.checkValidCallId(callId);
Sailesh Nepal10ea4602014-04-01 17:23:32 -0700132 mHandler.obtainMessage(MSG_STOP_DTMF_TONE, callId).sendToTarget();
Ihab Awad74549ec2014-03-10 15:33:25 -0700133 }
134
135 /** {@inheritDoc} */
Sailesh Nepal10ea4602014-04-01 17:23:32 -0700136 @Override
137 public void postDialContinue(String callId) {
Ihab Awad74549ec2014-03-10 15:33:25 -0700138 Log.d(this, "postDialContinue(%s)", callId);
Sailesh Nepale59bb192014-04-01 18:33:59 -0700139 mCallIdMapper.checkValidCallId(callId);
Sailesh Nepal10ea4602014-04-01 17:23:32 -0700140 mHandler.obtainMessage(MSG_POST_DIAL_CONTINUE, callId).sendToTarget();
Ihab Awad74549ec2014-03-10 15:33:25 -0700141 }
142
143 /** {@inheritDoc} */
Santos Cordone3d76ab2014-01-28 17:25:20 -0800144 @Override
Sailesh Nepal10ea4602014-04-01 17:23:32 -0700145 public void disconnectCall(String callId) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700146 Log.v(this, "disconnectCall: %s", callId);
147 mCallIdMapper.checkValidCallId(callId);
Sailesh Nepal10ea4602014-04-01 17:23:32 -0700148 mHandler.obtainMessage(MSG_DISCONNECT_CALL, callId).sendToTarget();
Santos Cordone3d76ab2014-01-28 17:25:20 -0800149 }
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700150
151 /** {@inheritDoc} */
152 @Override
Sailesh Nepal10ea4602014-04-01 17:23:32 -0700153 public void holdCall(String callId) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700154 mCallIdMapper.checkValidCallId(callId);
Sailesh Nepal10ea4602014-04-01 17:23:32 -0700155 mHandler.obtainMessage(MSG_HOLD_CALL, callId).sendToTarget();
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700156 }
157
158 /** {@inheritDoc} */
159 @Override
Sailesh Nepal10ea4602014-04-01 17:23:32 -0700160 public void unholdCall(String callId) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700161 mCallIdMapper.checkValidCallId(callId);
Sailesh Nepal10ea4602014-04-01 17:23:32 -0700162 mHandler.obtainMessage(MSG_UNHOLD_CALL, callId).sendToTarget();
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700163 }
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700164
165 /** {@inheritDoc} */
166 @Override
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700167 public void handoffCall(String callId) {
168 mCallIdMapper.checkValidCallId(callId);
169 mHandler.obtainMessage(MSG_HANDOFF_CALL, callId).sendToTarget();
170 }
171
172 /** {@inheritDoc} */
173 @Override
Sailesh Nepal10ea4602014-04-01 17:23:32 -0700174 public void mute(boolean shouldMute) {
175 mHandler.obtainMessage(MSG_MUTE, shouldMute ? 1 : 0, 0).sendToTarget();
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700176 }
177
178 /** {@inheritDoc} */
179 @Override
Sailesh Nepal10ea4602014-04-01 17:23:32 -0700180 public void setAudioRoute(int route) {
181 mHandler.obtainMessage(MSG_SET_AUDIO_ROUTE, route, 0).sendToTarget();
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700182 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800183}