Ihab Awad | c17294c | 2014-08-04 19:23:37 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 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 | |
| 17 | package com.android.telecomm.testapps; |
| 18 | |
| 19 | import android.app.PendingIntent; |
| 20 | import android.net.Uri; |
Ihab Awad | 6fb37c8 | 2014-08-07 19:48:57 -0700 | [diff] [blame] | 21 | import android.telecomm.AudioState; |
Ihab Awad | c17294c | 2014-08-04 19:23:37 -0700 | [diff] [blame] | 22 | import android.telecomm.Connection; |
| 23 | import android.telecomm.ConnectionRequest; |
| 24 | import android.telecomm.ConnectionService; |
| 25 | import android.telecomm.PhoneAccountHandle; |
| 26 | import android.telecomm.RemoteConnection; |
| 27 | import android.telecomm.StatusHints; |
| 28 | import android.util.Log; |
| 29 | |
| 30 | import java.util.Random; |
| 31 | |
| 32 | /** |
| 33 | * Service which acts as a fake ConnectionManager if so configured. |
| 34 | * TODO(santoscordon): Rename all classes in the directory to Dummy* (e.g., DummyConnectionService). |
| 35 | */ |
| 36 | public class TestConnectionManager extends ConnectionService { |
| 37 | /** |
| 38 | * Random number generator used to generate phone numbers. |
| 39 | */ |
| 40 | private Random mRandom = new Random(); |
| 41 | |
| 42 | private final class TestManagedConnection extends Connection { |
| 43 | private final RemoteConnection.Listener mProxyListener = new RemoteConnection.Listener() { |
| 44 | @Override |
| 45 | public void onStateChanged(RemoteConnection connection, int state) { |
| 46 | setState(state); |
| 47 | } |
| 48 | |
| 49 | @Override |
| 50 | public void onDisconnected(RemoteConnection connection, int cause, String message) { |
| 51 | setDisconnected(cause, message); |
| 52 | destroy(); |
| 53 | } |
| 54 | |
| 55 | @Override |
| 56 | public void onRequestingRingback(RemoteConnection connection, boolean ringback) { |
| 57 | setRequestingRingback(ringback); |
| 58 | } |
| 59 | |
| 60 | @Override |
| 61 | public void onCallCapabilitiesChanged(RemoteConnection connection, |
| 62 | int callCapabilities) { |
| 63 | setCallCapabilities(callCapabilities); |
| 64 | } |
| 65 | |
| 66 | @Override |
| 67 | public void onPostDialWait(RemoteConnection connection, String remainingDigits) { |
| 68 | setPostDialWait(remainingDigits); |
| 69 | } |
| 70 | |
| 71 | @Override |
| 72 | public void onAudioModeIsVoipChanged(RemoteConnection connection, boolean isVoip) { |
| 73 | setAudioModeIsVoip(isVoip); |
| 74 | } |
| 75 | |
| 76 | @Override |
| 77 | public void onStatusHintsChanged(RemoteConnection connection, StatusHints statusHints) { |
| 78 | setStatusHints(statusHints); |
| 79 | } |
| 80 | |
| 81 | @Override |
| 82 | public void onVideoStateChanged(RemoteConnection connection, int videoState) { |
| 83 | setVideoState(videoState); |
| 84 | } |
| 85 | |
| 86 | @Override |
| 87 | public void onHandleChanged(RemoteConnection connection, Uri handle, int presentation) { |
| 88 | setHandle(handle, presentation); |
| 89 | } |
| 90 | |
| 91 | @Override |
| 92 | public void onCallerDisplayNameChanged( |
| 93 | RemoteConnection connection, String callerDisplayName, int presentation) { |
| 94 | setCallerDisplayName(callerDisplayName, presentation); |
| 95 | } |
| 96 | |
| 97 | @Override |
| 98 | public void onStartActivityFromInCall( |
| 99 | RemoteConnection connection, PendingIntent intent) { |
| 100 | startActivityFromInCall(intent); |
| 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public void onDestroyed(RemoteConnection connection) { |
| 105 | destroy(); |
| 106 | } |
| 107 | }; |
| 108 | |
| 109 | private final RemoteConnection mRemoteConnection; |
| 110 | private final boolean mIsIncoming; |
| 111 | |
| 112 | TestManagedConnection(RemoteConnection remoteConnection, boolean isIncoming) { |
| 113 | mRemoteConnection = remoteConnection; |
| 114 | mIsIncoming = isIncoming; |
| 115 | mRemoteConnection.addListener(mProxyListener); |
| 116 | setState(mRemoteConnection.getState()); |
| 117 | } |
| 118 | |
| 119 | @Override |
| 120 | public void onAbort() { |
| 121 | mRemoteConnection.abort(); |
| 122 | } |
| 123 | |
| 124 | /** ${inheritDoc} */ |
| 125 | @Override |
| 126 | public void onAnswer(int videoState) { |
| 127 | mRemoteConnection.answer(videoState); |
| 128 | } |
| 129 | |
| 130 | /** ${inheritDoc} */ |
| 131 | @Override |
| 132 | public void onDisconnect() { |
| 133 | mRemoteConnection.disconnect(); |
| 134 | } |
| 135 | |
Ihab Awad | 39b6a98 | 2014-08-08 17:08:19 -0700 | [diff] [blame] | 136 | @Override |
| 137 | public void onPlayDtmfTone(char c) { |
| 138 | mRemoteConnection.playDtmfTone(c); |
| 139 | } |
| 140 | |
Ihab Awad | c17294c | 2014-08-04 19:23:37 -0700 | [diff] [blame] | 141 | /** ${inheritDoc} */ |
| 142 | @Override |
| 143 | public void onHold() { |
| 144 | mRemoteConnection.hold(); |
| 145 | } |
| 146 | |
| 147 | /** ${inheritDoc} */ |
| 148 | @Override |
| 149 | public void onReject() { |
| 150 | mRemoteConnection.reject(); |
| 151 | } |
| 152 | |
| 153 | /** ${inheritDoc} */ |
| 154 | @Override |
| 155 | public void onUnhold() { |
| 156 | mRemoteConnection.unhold(); |
| 157 | } |
| 158 | |
| 159 | @Override |
Ihab Awad | 6fb37c8 | 2014-08-07 19:48:57 -0700 | [diff] [blame] | 160 | public void onSetAudioState(AudioState state) { |
Ihab Awad | c17294c | 2014-08-04 19:23:37 -0700 | [diff] [blame] | 161 | mRemoteConnection.setAudioState(state); |
| 162 | } |
| 163 | |
| 164 | private void setState(int state) { |
| 165 | log("setState: " + state); |
| 166 | switch (state) { |
Ihab Awad | 6fb37c8 | 2014-08-07 19:48:57 -0700 | [diff] [blame] | 167 | case STATE_ACTIVE: |
Ihab Awad | c17294c | 2014-08-04 19:23:37 -0700 | [diff] [blame] | 168 | setActive(); |
| 169 | break; |
Ihab Awad | 6fb37c8 | 2014-08-07 19:48:57 -0700 | [diff] [blame] | 170 | case STATE_HOLDING: |
Ihab Awad | c17294c | 2014-08-04 19:23:37 -0700 | [diff] [blame] | 171 | setOnHold(); |
| 172 | break; |
Ihab Awad | 6fb37c8 | 2014-08-07 19:48:57 -0700 | [diff] [blame] | 173 | case STATE_DIALING: |
Ihab Awad | c17294c | 2014-08-04 19:23:37 -0700 | [diff] [blame] | 174 | setDialing(); |
| 175 | break; |
Ihab Awad | 6fb37c8 | 2014-08-07 19:48:57 -0700 | [diff] [blame] | 176 | case STATE_RINGING: |
Ihab Awad | c17294c | 2014-08-04 19:23:37 -0700 | [diff] [blame] | 177 | setRinging(); |
| 178 | break; |
| 179 | } |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | private static void log(String msg) { |
Sailesh Nepal | 50e41d0 | 2014-08-20 10:15:47 -0700 | [diff] [blame] | 184 | Log.w("telecomtestcs", "[TestConnectionManager] " + msg); |
Ihab Awad | c17294c | 2014-08-04 19:23:37 -0700 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | @Override |
| 188 | public Connection onCreateOutgoingConnection( |
| 189 | PhoneAccountHandle connectionManagerAccount, |
| 190 | final ConnectionRequest request) { |
| 191 | return new TestManagedConnection( |
| 192 | createRemoteOutgoingConnection( |
| 193 | request.getAccountHandle(), |
| 194 | request), |
| 195 | false); |
| 196 | } |
| 197 | |
| 198 | @Override |
| 199 | public Connection onCreateIncomingConnection( |
| 200 | PhoneAccountHandle connectionManagerAccount, |
| 201 | final ConnectionRequest request) { |
| 202 | return new TestManagedConnection( |
Sailesh Nepal | 50e41d0 | 2014-08-20 10:15:47 -0700 | [diff] [blame] | 203 | createRemoteIncomingConnection( |
Ihab Awad | c17294c | 2014-08-04 19:23:37 -0700 | [diff] [blame] | 204 | request.getAccountHandle(), |
| 205 | request), |
| 206 | true); |
| 207 | } |
| 208 | } |