Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 1 | /* |
| 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 Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 17 | package com.android.telecomm; |
| 18 | |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 19 | import android.os.Handler; |
Sailesh Nepal | 10ea460 | 2014-04-01 17:23:32 -0700 | [diff] [blame] | 20 | import android.os.Message; |
Sailesh Nepal | a439e1b | 2014-03-11 18:19:58 -0700 | [diff] [blame] | 21 | |
| 22 | import com.android.internal.telecomm.IInCallAdapter; |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 23 | |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 24 | /** |
| 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 | */ |
| 29 | class InCallAdapter extends IInCallAdapter.Stub { |
Sailesh Nepal | 10ea460 | 2014-04-01 17:23:32 -0700 | [diff] [blame] | 30 | 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; |
| 38 | private static final int MSG_MUTE = 8; |
| 39 | private static final int MSG_SET_AUDIO_ROUTE = 9; |
| 40 | |
| 41 | private final class InCallAdapterHandler extends Handler { |
| 42 | @Override |
| 43 | public void handleMessage(Message msg) { |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame^] | 44 | Call call = null; |
| 45 | if (msg.obj != null) { |
| 46 | call = mCallIdMapper.getCall(msg.obj); |
| 47 | if (call == null) { |
| 48 | Log.w(this, "Unknown call id: %s, msg: %d", msg.obj, msg.what); |
| 49 | return; |
| 50 | } |
| 51 | } |
| 52 | |
Sailesh Nepal | 10ea460 | 2014-04-01 17:23:32 -0700 | [diff] [blame] | 53 | switch (msg.what) { |
| 54 | case MSG_ANSWER_CALL: |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame^] | 55 | mCallsManager.answerCall(call); |
Sailesh Nepal | 10ea460 | 2014-04-01 17:23:32 -0700 | [diff] [blame] | 56 | break; |
| 57 | case MSG_REJECT_CALL: |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame^] | 58 | mCallsManager.rejectCall(call); |
Sailesh Nepal | 10ea460 | 2014-04-01 17:23:32 -0700 | [diff] [blame] | 59 | break; |
| 60 | case MSG_PLAY_DTMF_TONE: |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame^] | 61 | mCallsManager.playDtmfTone(call, (char) msg.arg1); |
Sailesh Nepal | 10ea460 | 2014-04-01 17:23:32 -0700 | [diff] [blame] | 62 | break; |
| 63 | case MSG_STOP_DTMF_TONE: |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame^] | 64 | mCallsManager.stopDtmfTone(call); |
Sailesh Nepal | 10ea460 | 2014-04-01 17:23:32 -0700 | [diff] [blame] | 65 | break; |
| 66 | case MSG_POST_DIAL_CONTINUE: |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame^] | 67 | mCallsManager.postDialContinue(call); |
Sailesh Nepal | 10ea460 | 2014-04-01 17:23:32 -0700 | [diff] [blame] | 68 | break; |
| 69 | case MSG_DISCONNECT_CALL: |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame^] | 70 | mCallsManager.disconnectCall(call); |
Sailesh Nepal | 10ea460 | 2014-04-01 17:23:32 -0700 | [diff] [blame] | 71 | break; |
| 72 | case MSG_HOLD_CALL: |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame^] | 73 | mCallsManager.holdCall(call); |
Sailesh Nepal | 10ea460 | 2014-04-01 17:23:32 -0700 | [diff] [blame] | 74 | break; |
| 75 | case MSG_UNHOLD_CALL: |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame^] | 76 | mCallsManager.unholdCall(call); |
Sailesh Nepal | 10ea460 | 2014-04-01 17:23:32 -0700 | [diff] [blame] | 77 | break; |
| 78 | case MSG_MUTE: |
| 79 | mCallsManager.mute(msg.arg1 == 1 ? true : false); |
| 80 | break; |
| 81 | case MSG_SET_AUDIO_ROUTE: |
| 82 | mCallsManager.setAudioRoute(msg.arg1); |
| 83 | break; |
| 84 | } |
| 85 | } |
| 86 | } |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 87 | |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 88 | private final CallsManager mCallsManager; |
Sailesh Nepal | 10ea460 | 2014-04-01 17:23:32 -0700 | [diff] [blame] | 89 | private final Handler mHandler = new InCallAdapterHandler(); |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame^] | 90 | private final CallIdMapper mCallIdMapper; |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 91 | |
| 92 | /** Persists the specified parameters. */ |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame^] | 93 | public InCallAdapter(CallsManager callsManager, CallIdMapper callIdMapper) { |
Sailesh Nepal | 10ea460 | 2014-04-01 17:23:32 -0700 | [diff] [blame] | 94 | ThreadUtil.checkOnMainThread(); |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 95 | mCallsManager = callsManager; |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame^] | 96 | mCallIdMapper = callIdMapper; |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | /** {@inheritDoc} */ |
| 100 | @Override |
Sailesh Nepal | 10ea460 | 2014-04-01 17:23:32 -0700 | [diff] [blame] | 101 | public void answerCall(String callId) { |
Sailesh Nepal | f1c191d | 2014-03-07 18:17:39 -0800 | [diff] [blame] | 102 | Log.d(this, "answerCall(%s)", callId); |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame^] | 103 | mCallIdMapper.checkValidCallId(callId); |
Sailesh Nepal | 10ea460 | 2014-04-01 17:23:32 -0700 | [diff] [blame] | 104 | mHandler.obtainMessage(MSG_ANSWER_CALL, callId).sendToTarget(); |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | /** {@inheritDoc} */ |
| 108 | @Override |
Sailesh Nepal | 10ea460 | 2014-04-01 17:23:32 -0700 | [diff] [blame] | 109 | public void rejectCall(String callId) { |
Sailesh Nepal | f1c191d | 2014-03-07 18:17:39 -0800 | [diff] [blame] | 110 | Log.d(this, "rejectCall(%s)", callId); |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame^] | 111 | mCallIdMapper.checkValidCallId(callId); |
Sailesh Nepal | 10ea460 | 2014-04-01 17:23:32 -0700 | [diff] [blame] | 112 | mHandler.obtainMessage(MSG_REJECT_CALL, callId).sendToTarget(); |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | /** {@inheritDoc} */ |
Sailesh Nepal | 10ea460 | 2014-04-01 17:23:32 -0700 | [diff] [blame] | 116 | @Override |
| 117 | public void playDtmfTone(String callId, char digit) { |
Ihab Awad | 74549ec | 2014-03-10 15:33:25 -0700 | [diff] [blame] | 118 | Log.d(this, "playDtmfTone(%s,%c)", callId, digit); |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame^] | 119 | mCallIdMapper.checkValidCallId(callId); |
Sailesh Nepal | 10ea460 | 2014-04-01 17:23:32 -0700 | [diff] [blame] | 120 | mHandler.obtainMessage(MSG_PLAY_DTMF_TONE, (int) digit, 0, callId).sendToTarget(); |
Ihab Awad | 74549ec | 2014-03-10 15:33:25 -0700 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | /** {@inheritDoc} */ |
Sailesh Nepal | 10ea460 | 2014-04-01 17:23:32 -0700 | [diff] [blame] | 124 | @Override |
| 125 | public void stopDtmfTone(String callId) { |
Ihab Awad | 74549ec | 2014-03-10 15:33:25 -0700 | [diff] [blame] | 126 | Log.d(this, "stopDtmfTone(%s)", callId); |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame^] | 127 | mCallIdMapper.checkValidCallId(callId); |
Sailesh Nepal | 10ea460 | 2014-04-01 17:23:32 -0700 | [diff] [blame] | 128 | mHandler.obtainMessage(MSG_STOP_DTMF_TONE, callId).sendToTarget(); |
Ihab Awad | 74549ec | 2014-03-10 15:33:25 -0700 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | /** {@inheritDoc} */ |
Sailesh Nepal | 10ea460 | 2014-04-01 17:23:32 -0700 | [diff] [blame] | 132 | @Override |
| 133 | public void postDialContinue(String callId) { |
Ihab Awad | 74549ec | 2014-03-10 15:33:25 -0700 | [diff] [blame] | 134 | Log.d(this, "postDialContinue(%s)", callId); |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame^] | 135 | mCallIdMapper.checkValidCallId(callId); |
Sailesh Nepal | 10ea460 | 2014-04-01 17:23:32 -0700 | [diff] [blame] | 136 | mHandler.obtainMessage(MSG_POST_DIAL_CONTINUE, callId).sendToTarget(); |
Ihab Awad | 74549ec | 2014-03-10 15:33:25 -0700 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | /** {@inheritDoc} */ |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 140 | @Override |
Sailesh Nepal | 10ea460 | 2014-04-01 17:23:32 -0700 | [diff] [blame] | 141 | public void disconnectCall(String callId) { |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame^] | 142 | Log.v(this, "disconnectCall: %s", callId); |
| 143 | mCallIdMapper.checkValidCallId(callId); |
Sailesh Nepal | 10ea460 | 2014-04-01 17:23:32 -0700 | [diff] [blame] | 144 | mHandler.obtainMessage(MSG_DISCONNECT_CALL, callId).sendToTarget(); |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 145 | } |
Yorke Lee | cdf3ebd | 2014-03-12 18:31:41 -0700 | [diff] [blame] | 146 | |
| 147 | /** {@inheritDoc} */ |
| 148 | @Override |
Sailesh Nepal | 10ea460 | 2014-04-01 17:23:32 -0700 | [diff] [blame] | 149 | public void holdCall(String callId) { |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame^] | 150 | mCallIdMapper.checkValidCallId(callId); |
Sailesh Nepal | 10ea460 | 2014-04-01 17:23:32 -0700 | [diff] [blame] | 151 | mHandler.obtainMessage(MSG_HOLD_CALL, callId).sendToTarget(); |
Yorke Lee | cdf3ebd | 2014-03-12 18:31:41 -0700 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | /** {@inheritDoc} */ |
| 155 | @Override |
Sailesh Nepal | 10ea460 | 2014-04-01 17:23:32 -0700 | [diff] [blame] | 156 | public void unholdCall(String callId) { |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame^] | 157 | mCallIdMapper.checkValidCallId(callId); |
Sailesh Nepal | 10ea460 | 2014-04-01 17:23:32 -0700 | [diff] [blame] | 158 | mHandler.obtainMessage(MSG_UNHOLD_CALL, callId).sendToTarget(); |
Yorke Lee | cdf3ebd | 2014-03-12 18:31:41 -0700 | [diff] [blame] | 159 | } |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 160 | |
| 161 | /** {@inheritDoc} */ |
| 162 | @Override |
Sailesh Nepal | 10ea460 | 2014-04-01 17:23:32 -0700 | [diff] [blame] | 163 | public void mute(boolean shouldMute) { |
| 164 | mHandler.obtainMessage(MSG_MUTE, shouldMute ? 1 : 0, 0).sendToTarget(); |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | /** {@inheritDoc} */ |
| 168 | @Override |
Sailesh Nepal | 10ea460 | 2014-04-01 17:23:32 -0700 | [diff] [blame] | 169 | public void setAudioRoute(int route) { |
| 170 | mHandler.obtainMessage(MSG_SET_AUDIO_ROUTE, route, 0).sendToTarget(); |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 171 | } |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 172 | } |