blob: 902ace7c13d2c0bb9042eb8d2a34deb07d5856b2 [file] [log] [blame]
Ihab Awadc17294c2014-08-04 19:23:37 -07001/*
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
17package com.android.telecomm.testapps;
18
19import android.app.PendingIntent;
20import android.net.Uri;
Ihab Awad6fb37c82014-08-07 19:48:57 -070021import android.telecomm.AudioState;
Ihab Awadc17294c2014-08-04 19:23:37 -070022import android.telecomm.Connection;
23import android.telecomm.ConnectionRequest;
24import android.telecomm.ConnectionService;
25import android.telecomm.PhoneAccountHandle;
26import android.telecomm.RemoteConnection;
27import android.telecomm.StatusHints;
28import android.util.Log;
29
30import 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 */
36public 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 Awad39b6a982014-08-08 17:08:19 -0700136 @Override
137 public void onPlayDtmfTone(char c) {
138 mRemoteConnection.playDtmfTone(c);
139 }
140
Ihab Awadc17294c2014-08-04 19:23:37 -0700141 /** ${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 Awad6fb37c82014-08-07 19:48:57 -0700160 public void onSetAudioState(AudioState state) {
Ihab Awadc17294c2014-08-04 19:23:37 -0700161 mRemoteConnection.setAudioState(state);
162 }
163
164 private void setState(int state) {
165 log("setState: " + state);
166 switch (state) {
Ihab Awad6fb37c82014-08-07 19:48:57 -0700167 case STATE_ACTIVE:
Ihab Awadc17294c2014-08-04 19:23:37 -0700168 setActive();
169 break;
Ihab Awad6fb37c82014-08-07 19:48:57 -0700170 case STATE_HOLDING:
Ihab Awadc17294c2014-08-04 19:23:37 -0700171 setOnHold();
172 break;
Ihab Awad6fb37c82014-08-07 19:48:57 -0700173 case STATE_DIALING:
Ihab Awadc17294c2014-08-04 19:23:37 -0700174 setDialing();
175 break;
Ihab Awad6fb37c82014-08-07 19:48:57 -0700176 case STATE_RINGING:
Ihab Awadc17294c2014-08-04 19:23:37 -0700177 setRinging();
178 break;
179 }
180 }
181 }
182
183 private static void log(String msg) {
Sailesh Nepal50e41d02014-08-20 10:15:47 -0700184 Log.w("telecomtestcs", "[TestConnectionManager] " + msg);
Ihab Awadc17294c2014-08-04 19:23:37 -0700185 }
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 Nepal50e41d02014-08-20 10:15:47 -0700203 createRemoteIncomingConnection(
Ihab Awadc17294c2014-08-04 19:23:37 -0700204 request.getAccountHandle(),
205 request),
206 true);
207 }
208}