1 /*
2  * Copyright (C) 2017 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 android.telecom.cts;
18 
19 import android.os.Bundle;
20 import android.telecom.CallAudioState;
21 import android.telecom.CallEndpoint;
22 import android.telecom.Connection;
23 import android.telecom.DisconnectCause;
24 import android.telecom.cts.TestUtils.InvokeCounter;
25 import android.util.Log;
26 
27 import java.util.List;
28 import java.util.concurrent.CountDownLatch;
29 
30 /**
31  * CTS Test self-managed {@link Connection} implementation.
32  */
33 public class SelfManagedConnection extends Connection {
34     private static final String TAG = SelfManagedConnection.class.getSimpleName();
35     InvokeCounter mCallAudioRouteInvokeCounter = new InvokeCounter("onCallAudioStateChanged");
36     InvokeCounter mOnShowIncomingUiInvokeCounter = new InvokeCounter(
37             "onShowIncomingUiInvokeCounter");
38     InvokeCounter mCallEventCounter = new InvokeCounter("onCallEvent");
39     InvokeCounter mHandoverCompleteCounter = new InvokeCounter("handoverCompleteCounter");
40     CountDownLatch mOnAnswerLatch = new CountDownLatch(1);
41     InvokeCounter mCallEndpointInvokeCounter = new InvokeCounter("onCallEndpointChanged");
42     InvokeCounter mAvailableEndpointsInvokeCounter =
43             new InvokeCounter("onAvailableCallEndpointsChanged");
44     CountDownLatch mOnHoldLatch = new CountDownLatch(1);
45     CountDownLatch mOnUnHoldLatch = new CountDownLatch(1);
46     CountDownLatch mOnDisconnectLatch = new CountDownLatch(1);
47     CountDownLatch mInCallServiceTrackingLatch = new CountDownLatch(1);
48     boolean mIsTracked = false;
49     boolean mIsAlternativeUiShowing = false;
50     boolean mSuppressHoldResponse = false;
51 
52     public static abstract class Listener {
onDestroyed(SelfManagedConnection connection)53         void onDestroyed(SelfManagedConnection connection) { };
54     }
55 
56     private final boolean mIsIncomingCall;
57     private final Listener mListener;
58 
SelfManagedConnection(boolean isIncomingCall, Listener listener)59     public SelfManagedConnection(boolean isIncomingCall, Listener listener) {
60         mIsIncomingCall = isIncomingCall;
61         mListener = listener;
62     }
63 
isIncomingCall()64     public boolean isIncomingCall() {
65         return mIsIncomingCall;
66     }
67 
disconnectAndDestroy()68     public void disconnectAndDestroy() {
69         setDisconnected(new DisconnectCause(DisconnectCause.LOCAL));
70         destroy();
71         mListener.onDestroyed(this);
72     }
73 
74     @Override
onAnswer(int videoState)75     public void onAnswer(int videoState) {
76         this.setActive();
77         mOnAnswerLatch.countDown();
78     }
79 
80     @Override
onAnswer()81     public void onAnswer() {
82         this.setActive();
83         mOnAnswerLatch.countDown();
84     }
85 
86     @Override
onCallAudioStateChanged(CallAudioState state)87     public void onCallAudioStateChanged(CallAudioState state) {
88         mCallAudioRouteInvokeCounter.invoke(state);
89     }
90 
91     @Override
onUsingAlternativeUi(boolean isUsingAlternativeUi)92     public void onUsingAlternativeUi(boolean isUsingAlternativeUi) {
93         mIsAlternativeUiShowing = isUsingAlternativeUi;
94         mInCallServiceTrackingLatch.countDown();
95     }
96 
97     @Override
onTrackedByNonUiService(boolean isTracked)98     public void onTrackedByNonUiService(boolean isTracked) {
99         mIsTracked = isTracked;
100         mInCallServiceTrackingLatch.countDown();
101     }
102 
103     @Override
onDisconnect()104     public void onDisconnect() {
105         super.onDisconnect();
106         disconnectAndDestroy();
107         mOnDisconnectLatch.countDown();
108     }
109 
110     @Override
onShowIncomingCallUi()111     public void onShowIncomingCallUi() {
112         mOnShowIncomingUiInvokeCounter.invoke();
113     }
114 
115     @Override
onHold()116     public void onHold() {
117         if (!mSuppressHoldResponse) {
118             this.setOnHold();
119         }
120         mOnHoldLatch.countDown();
121     }
122 
123     @Override
onUnhold()124     public void onUnhold() {
125         this.setActive();
126         mOnUnHoldLatch.countDown();
127     }
128 
129     @Override
onCallEvent(String event, Bundle extras)130     public void onCallEvent(String event, Bundle extras) {
131         mCallEventCounter.invoke(event, extras);
132     }
133 
134     @Override
onHandoverComplete()135     public void onHandoverComplete() {
136         mHandoverCompleteCounter.invoke();
137     }
138 
139     @Override
onCallEndpointChanged(CallEndpoint endpoint)140     public void onCallEndpointChanged(CallEndpoint endpoint) {
141         Log.i(TAG, String.format("onCallEndpointChanged: endpoint=[%s]", endpoint));
142         mCallEndpointInvokeCounter.invoke(endpoint);
143     }
144 
145     @Override
onAvailableCallEndpointsChanged(List<CallEndpoint> availableEndpoints)146     public void onAvailableCallEndpointsChanged(List<CallEndpoint> availableEndpoints) {
147         mAvailableEndpointsInvokeCounter.invoke(availableEndpoints);
148     }
149 
getCallAudioStateChangedInvokeCounter()150     public InvokeCounter getCallAudioStateChangedInvokeCounter() {
151         return mCallAudioRouteInvokeCounter;
152     }
153 
getOnShowIncomingUiInvokeCounter()154     public InvokeCounter getOnShowIncomingUiInvokeCounter() {
155         return mOnShowIncomingUiInvokeCounter;
156     }
157 
getCallEventCounter()158     public InvokeCounter getCallEventCounter() {
159         return mCallEventCounter;
160     }
161 
getHandoverCompleteCounter()162     public InvokeCounter getHandoverCompleteCounter() {
163         return mHandoverCompleteCounter;
164     }
165 
waitOnAnswer()166     public boolean waitOnAnswer() {
167         mOnAnswerLatch = TestUtils.waitForLock(mOnAnswerLatch);
168         return mOnAnswerLatch != null;
169     }
170 
waitOnHold()171     public boolean waitOnHold() {
172         mOnHoldLatch = TestUtils.waitForLock(mOnHoldLatch);
173         return mOnHoldLatch != null;
174     }
175 
waitOnUnHold()176     public boolean waitOnUnHold() {
177         mOnUnHoldLatch = TestUtils.waitForLock(mOnUnHoldLatch);
178         return mOnUnHoldLatch != null;
179     }
180 
waitOnDisconnect()181     public boolean waitOnDisconnect() {
182         mOnDisconnectLatch = TestUtils.waitForLock(mOnDisconnectLatch);
183         return mOnDisconnectLatch != null;
184     }
185 
waitOnInCallServiceTrackingChanged()186     public boolean waitOnInCallServiceTrackingChanged() {
187         boolean result = TestUtils.waitForLatchCountDown(mInCallServiceTrackingLatch);
188         mInCallServiceTrackingLatch = new CountDownLatch(1);
189         return result;
190     }
191 
isTracked()192     public boolean isTracked() {
193         return mIsTracked;
194     }
195 
isAlternativeUiShowing()196     public boolean isAlternativeUiShowing() {
197         return mIsAlternativeUiShowing;
198     }
199 
getCallEndpointChangedInvokeCounter()200     public InvokeCounter getCallEndpointChangedInvokeCounter() {
201         return mCallEndpointInvokeCounter;
202     }
203 
getAvailableEndpointsChangedInvokeCounter()204     public InvokeCounter getAvailableEndpointsChangedInvokeCounter() {
205         return mAvailableEndpointsInvokeCounter;
206     }
207 
setSuppressHoldResponse(boolean suppressHoldResponse)208     public void setSuppressHoldResponse(boolean suppressHoldResponse) {
209         mSuppressHoldResponse = suppressHoldResponse;
210     }
211 }
212